core/smn_float.cpp
author David Anderson <dvander@alliedmods.net>
Mon Feb 26 08:50:25 2007 +0000 (2007-02-26)
changeset 537 3fc99cb190ce
parent 470 6b1da524b1b7
child 653 ef60870067ff
permissions -rw-r--r--
removed redundant float native
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include <math.h>
    15 #include <string.h>
    16 #include <stdlib.h>
    17 #include "sm_globals.h"
    18 
    19 /****************************************
    20 *                                       *
    21 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    22 *                                       *
    23 ****************************************/
    24 
    25 
    26 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    27 {
    28 	float val = static_cast<float>(params[1]);
    29 
    30 	return sp_ftoc(val);
    31 }
    32 
    33 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
    34 {
    35 	float val = sp_ctof(params[1]);
    36 	val = (val >= 0) ? val : -val;
    37 
    38 	return sp_ftoc(val);
    39 }
    40 
    41 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
    42 {
    43 	float val = sp_ctof(params[1]) + sp_ctof(params[2]);
    44 
    45 	return sp_ftoc(val);
    46 }
    47 
    48 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
    49 {
    50 	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    51 
    52 	return sp_ftoc(val);
    53 }
    54 
    55 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
    56 {
    57 	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    58 
    59 	return sp_ftoc(val);
    60 }
    61 
    62 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
    63 {
    64 	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    65 
    66 	return sp_ftoc(val);
    67 }
    68 
    69 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
    70 {
    71 	float val1 = sp_ctof(params[1]);
    72 	float val2 = sp_ctof(params[2]);
    73 
    74 	if (val1 > val2)
    75 	{
    76 		return 1;
    77 	} else if (val1 < val2) {
    78 		return -1;
    79 	}
    80 
    81 	return 0;
    82 }
    83 
    84 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
    85 {
    86 	float val = sp_ctof(params[1]);
    87 	float base = sp_ctof(params[2]);
    88 
    89 	if ((val <= 0) || (base <= 0))
    90 	{
    91 		return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f)", val, base);
    92 	}
    93 	if (base == 10.0)
    94 	{
    95 		val = log10(val);
    96 	} else {
    97 		val = log(val) / log(base);
    98 	}
    99 
   100 	return sp_ftoc(val);
   101 }
   102 
   103 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
   104 {
   105 	float val = sp_ctof(params[1]);
   106 
   107 	return sp_ftoc(exp(val));
   108 }
   109 
   110 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
   111 {
   112 	float base = sp_ctof(params[1]);
   113 	float exponent = sp_ctof(params[2]);
   114 
   115 	return sp_ftoc(pow(base, exponent));
   116 }
   117 
   118 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
   119 {
   120 	float val = sp_ctof(params[1]);
   121 
   122 	if (val < 0.0)
   123 	{
   124 		return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f)", val);
   125 	}
   126 
   127 	return sp_ftoc(sqrt(val));
   128 }
   129 
   130 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
   131 {
   132 	float val = sp_ctof(params[1]);
   133 
   134 	switch (params[2])
   135 	{
   136 	case 1:
   137 		{
   138 			val = floor(val);
   139 			break;
   140 		}
   141 	case 2:
   142 		{
   143 			val = ceil(val);
   144 			break;
   145 		}
   146 	case 3:
   147 		{
   148 			if (val >= 0.0)
   149 			{
   150 				val = floor(val);
   151 			} else {
   152 				val = ceil(val);
   153 			}
   154 			break;
   155 		}
   156 	default:
   157 		{
   158 			val = (float)floor(val + 0.5);
   159 			break;
   160 		}
   161 	}
   162 
   163 	return static_cast<int>(val);
   164 }
   165 
   166 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
   167 {
   168 	float val = sp_ctof(params[1]);
   169 	val = val - floor(val);
   170 
   171 	return sp_ftoc(val);
   172 }
   173 
   174 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
   175 {
   176 	float val = sp_ctof(params[1]);
   177 	val = sin(val);
   178 
   179 	return sp_ftoc(val);
   180 }
   181 
   182 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
   183 {
   184 	float val = sp_ctof(params[1]);
   185 	val = cos(val);
   186 
   187 	return sp_ftoc(val);
   188 }
   189 
   190 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
   191 {
   192 	float val = sp_ctof(params[1]);
   193 	val = tan(val);
   194 
   195 	return sp_ftoc(val);
   196 }
   197 
   198 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
   199 {
   200 	float val = sp_ctof(params[1]);
   201 	val = asin(val);
   202 
   203 	return sp_ftoc(val);
   204 }
   205 
   206 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
   207 {
   208 	float val = sp_ctof(params[1]);
   209 	val = acos(val);
   210 
   211 	return sp_ftoc(val);
   212 }
   213 
   214 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
   215 {
   216 	float val = sp_ctof(params[1]);
   217 	val = atan(val);
   218 
   219 	return sp_ftoc(val);
   220 }
   221 
   222 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
   223 {
   224 	float val1 = sp_ctof(params[1]);
   225 	float val2 = sp_ctof(params[2]);
   226 	val1 = atan2(val1, val2);
   227 
   228 	return sp_ftoc(val1);
   229 }
   230 
   231 REGISTER_NATIVES(floatnatives)
   232 {
   233 	{"float",			sm_float},
   234 	{"FloatMul",		sm_FloatMul},
   235 	{"FloatDiv",		sm_FloatDiv},
   236 	{"FloatAdd",		sm_FloatAdd},
   237 	{"FloatSub",		sm_FloatSub},
   238 	{"FloatFraction",	sm_FloatFraction},
   239 	{"FloatRound",		sm_FloatRound},
   240 	{"FloatCompare",	sm_FloatCompare},
   241 	{"SquareRoot",		sm_SquareRoot},
   242 	{"Pow",				sm_Pow},
   243 	{"Exponential",		sm_Exponential},
   244 	{"Logarithm",		sm_Logarithm},
   245 	{"Sine",			sm_Sine},
   246 	{"Cosine",			sm_Cosine},
   247 	{"Tangent",			sm_Tangent},
   248 	{"FloatAbs",		sm_FloatAbs},
   249 	{"ArcTangent",		sm_ArcTangent},
   250 	{"ArcCosine",		sm_ArcCosine},
   251 	{"ArcSine",			sm_ArcSine},
   252 	{"ArcTangent2",		sm_ArcTangent2},
   253 	{NULL,				NULL}
   254 };