core/smn_float.cpp
author Borja Ferrer <faluco@alliedmods.net>
Sun Feb 04 22:41:44 2007 +0000 (2007-02-04)
changeset 430 ed37e9767ed4
parent 370 d3a484f98f6d
child 432 77112e9620fa
permissions -rw-r--r--
Optimized forwards, now paused functions are stored in a temp list so we dont have to check if they're runnable on each function execution.
     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_autonatives.h"
    18 #include "sp_vm_api.h"
    19 #include "sp_typeutil.h"
    20 
    21 using namespace SourcePawn;
    22 
    23 /****************************************
    24 *                                       *
    25 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    26 *                                       *
    27 ****************************************/
    28 
    29 
    30 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    31 {
    32 	float val = static_cast<float>(params[1]);
    33 
    34 	return sp_ftoc(val);
    35 }
    36 
    37 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
    38 {
    39 	float val = sp_ctof(params[1]);
    40 	val = (val >= 0) ? val : -val;
    41 
    42 	return sp_ftoc(val);
    43 }
    44 
    45 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
    46 {
    47 	float val = sp_ctof(params[1]) + sp_ctof(params[2]);
    48 
    49 	return sp_ftoc(val);
    50 }
    51 
    52 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
    53 {
    54 	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    55 
    56 	return sp_ftoc(val);
    57 }
    58 
    59 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
    60 {
    61 	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    62 
    63 	return sp_ftoc(val);
    64 }
    65 
    66 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
    67 {
    68 	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    69 
    70 	return sp_ftoc(val);
    71 }
    72 
    73 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
    74 {
    75 	float val1 = sp_ctof(params[1]);
    76 	float val2 = sp_ctof(params[2]);
    77 
    78 	if (val1 > val2)
    79 	{
    80 		return 1;
    81 	} else if (val1 < val2) {
    82 		return -1;
    83 	}
    84 
    85 	return 0;
    86 }
    87 
    88 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
    89 {
    90 	float val = sp_ctof(params[1]);
    91 	float base = sp_ctof(params[2]);
    92 
    93 	if ((val <= 0) || (base <= 0))
    94 	{
    95 		return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f).", val, base);
    96 	}
    97 	if (base == 10.0)
    98 	{
    99 		val = log10(val);
   100 	} else {
   101 		val = log(val) / log(base);
   102 	}
   103 
   104 	return sp_ftoc(val);
   105 }
   106 
   107 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
   108 {
   109 	float val = sp_ctof(params[1]);
   110 
   111 	return sp_ftoc(exp(val));
   112 }
   113 
   114 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
   115 {
   116 	float base = sp_ctof(params[1]);
   117 	float exponent = sp_ctof(params[2]);
   118 
   119 	return sp_ftoc(pow(base, exponent));
   120 }
   121 
   122 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
   123 {
   124 	float val = sp_ctof(params[1]);
   125 
   126 	if (val < 0.0)
   127 	{
   128 		return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f).", val);
   129 	}
   130 
   131 	return sp_ftoc(sqrt(val));
   132 }
   133 
   134 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
   135 {
   136 	float val = sp_ctof(params[1]);
   137 
   138 	switch (params[2])
   139 	{
   140 	case 1:
   141 		{
   142 			val = floor(val);
   143 			break;
   144 		}
   145 	case 2:
   146 		{
   147 			val = ceil(val);
   148 			break;
   149 		}
   150 	case 3:
   151 		{
   152 			if (val >= 0.0)
   153 			{
   154 				val = floor(val);
   155 			} else {
   156 				val = ceil(val);
   157 			}
   158 			break;
   159 		}
   160 	default:
   161 		{
   162 			val = (float)floor(val + 0.5);
   163 			break;
   164 		}
   165 	}
   166 
   167 	return static_cast<int>(val);
   168 }
   169 
   170 static cell_t sm_FloatStr(IPluginContext *pCtx, const cell_t *params)
   171 {
   172 	char *str;
   173 
   174 	pCtx->LocalToString(params[1], &str);
   175 	if (!strlen(str))
   176 	{
   177 		return 0;
   178 	}
   179 
   180 	return sp_ftoc((float)atof(str));
   181 }
   182 
   183 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
   184 {
   185 	float val = sp_ctof(params[1]);
   186 	val = val - floor(val);
   187 
   188 	return sp_ftoc(val);
   189 }
   190 
   191 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
   192 {
   193 	float val = sp_ctof(params[1]);
   194 	val = sin(val);
   195 
   196 	return sp_ftoc(val);
   197 }
   198 
   199 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
   200 {
   201 	float val = sp_ctof(params[1]);
   202 	val = cos(val);
   203 
   204 	return sp_ftoc(val);
   205 }
   206 
   207 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
   208 {
   209 	float val = sp_ctof(params[1]);
   210 	val = tan(val);
   211 
   212 	return sp_ftoc(val);
   213 }
   214 
   215 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
   216 {
   217 	float val = sp_ctof(params[1]);
   218 	val = asin(val);
   219 
   220 	return sp_ftoc(val);
   221 }
   222 
   223 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
   224 {
   225 	float val = sp_ctof(params[1]);
   226 	val = acos(val);
   227 
   228 	return sp_ftoc(val);
   229 }
   230 
   231 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
   232 {
   233 	float val = sp_ctof(params[1]);
   234 	val = atan(val);
   235 
   236 	return sp_ftoc(val);
   237 }
   238 
   239 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
   240 {
   241 	float val1 = sp_ctof(params[1]);
   242 	float val2 = sp_ctof(params[2]);
   243 	val1 = atan2(val1, val2);
   244 
   245 	return sp_ftoc(val1);
   246 }
   247 
   248 REGISTER_NATIVES(floatnatives)
   249 {
   250 	{"float",			sm_float},
   251 	{"FloatStr",		sm_FloatStr},
   252 	{"FloatMul",		sm_FloatMul},
   253 	{"FloatDiv",		sm_FloatDiv},
   254 	{"FloatAdd",		sm_FloatAdd},
   255 	{"FloatSub",		sm_FloatSub},
   256 	{"FloatFraction",	sm_FloatFraction},
   257 	{"FloatRound",		sm_FloatRound},
   258 	{"FloatCompare",	sm_FloatCompare},
   259 	{"SquareRoot",		sm_SquareRoot},
   260 	{"Pow",				sm_Pow},
   261 	{"Exponential",		sm_Exponential},
   262 	{"Logarithm",		sm_Logarithm},
   263 	{"Sine",			sm_Sine},
   264 	{"Cosine",			sm_Cosine},
   265 	{"Tangent",			sm_Tangent},
   266 	{"FloatAbs",		sm_FloatAbs},
   267 	{"ArcTangent",		sm_ArcTangent},
   268 	{"ArcCosine",		sm_ArcCosine},
   269 	{"ArcSine",			sm_ArcSine},
   270 	{"ArcTangent2",		sm_ArcTangent2},
   271 	{NULL,				NULL}
   272 };