core/smn_float.cpp
author Borja Ferrer <faluco@alliedmods.net>
Mon Jan 01 23:14:20 2007 +0000 (2007-01-01)
changeset 258 2637fcf24c2f
parent 248 6ef6d19934fe
child 260 110d1d014b0c
permissions -rw-r--r--
more comments
     1 #include <math.h>
     2 #include <string.h>
     3 #include "sp_vm_api.h"
     4 #include "sp_typeutil.h"
     5 
     6 using namespace SourcePawn;
     7 
     8 //:TODO: these need to be registered....
     9 
    10 /****************************************
    11 *                                       *
    12 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    13 *                                       *
    14 ****************************************/
    15 
    16 
    17 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    18 {
    19 	float val = static_cast<float>(params[1]);
    20 
    21 	return sp_ftoc(val);
    22 }
    23 
    24 static cell_t sm_floatabs(IPluginContext *pCtx, const cell_t *params)
    25 {
    26 	float val = sp_ctof(params[1]);
    27 	val = (val >= 0) ? val : -val;
    28 
    29 	return sp_ftoc(val);
    30 }
    31 
    32 static cell_t sm_floatadd(IPluginContext *pCtx, const cell_t *params)
    33 {
    34 	float val = sp_ctof(params[1]) + sp_ctof(params[2]);
    35 
    36 	return sp_ftoc(val);
    37 }
    38 
    39 static cell_t sm_floatsub(IPluginContext *pCtx, const cell_t *params)
    40 {
    41 	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    42 
    43 	return sp_ftoc(val);
    44 }
    45 
    46 static cell_t sm_floatmul(IPluginContext *pCtx, const cell_t *params)
    47 {
    48 	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    49 
    50 	return sp_ftoc(val);
    51 }
    52 
    53 static cell_t sm_floatdiv(IPluginContext *pCtx, const cell_t *params)
    54 {
    55 	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    56 
    57 	return sp_ftoc(val);
    58 }
    59 
    60 static cell_t sm_floatcmp(IPluginContext *pCtx, const cell_t *params)
    61 {
    62 	float val1 = sp_ctof(params[1]);
    63 	float val2 = sp_ctof(params[2]);
    64 
    65 	if (val1 > val2)
    66 	{
    67 		return 1;
    68 	} else if (val1 < val2) {
    69 		return -1;
    70 	}
    71 
    72 	return 0;
    73 }
    74 
    75 static cell_t sm_floatlog(IPluginContext *pCtx, const cell_t *params)
    76 {
    77 	float val = sp_ctof(params[1]);
    78 	float base = sp_ctof(params[2]);
    79 
    80 	if ((val <= 0) || (base <= 0))
    81 	{
    82 		//:TODO: error out! logs cant take in negative numbers and log 0=-inf
    83 	}
    84 	if (base == 10.0)
    85 	{
    86 		val = log10(val);
    87 	} else {
    88 		val = log(val) / log(base);
    89 	}
    90 
    91 	return sp_ftoc(val);
    92 }
    93 
    94 static cell_t sm_floatexp(IPluginContext *pCtx, const cell_t *params)
    95 {
    96 	float val = sp_ctof(params[1]);
    97 
    98 	return sp_ftoc(exp(val));
    99 }
   100 
   101 static cell_t sm_floatpower(IPluginContext *pCtx, const cell_t *params)
   102 {
   103 	float base = sp_ctof(params[1]);
   104 	float exponent = sp_ctof(params[2]);
   105 
   106 	return sp_ftoc(pow(base, exponent));
   107 }
   108 
   109 static cell_t sm_floatsqroot(IPluginContext *pCtx, const cell_t *params)
   110 {
   111 	float val = sp_ctof(params[1]);
   112 
   113 	if (val < 0.0)
   114 	{
   115 		//:TODO: error out! we dont support complex numbers
   116 	}
   117 
   118 	return sp_ftoc(sqrt(val));
   119 }
   120 
   121 static cell_t sm_floatround(IPluginContext *pCtx, const cell_t *params)
   122 {
   123 	float val = sp_ctof(params[1]);
   124 
   125 	switch (params[2])
   126 	{
   127 	case 1:
   128 		{
   129 			val = floor(val);
   130 			break;
   131 		}
   132 	case 2:
   133 		{
   134 			val = ceil(val);
   135 			break;
   136 		}
   137 	case 3:
   138 		{
   139 			if (val >= 0.0)
   140 			{
   141 				val = floor(val);
   142 			} else {
   143 				val = ceil(val);
   144 			}
   145 			break;
   146 		}
   147 	default:
   148 		{
   149 			val = (float)floor(val + 0.5);
   150 			break;
   151 		}
   152 	}
   153 
   154 	return static_cast<int>(val);
   155 }
   156 
   157 static cell_t sm_floatstr(IPluginContext *pCtx, const cell_t *params)
   158 {
   159 	char *str;
   160 
   161 	pCtx->LocalToString(params[1], &str);
   162 	if (!strlen(str))
   163 	{
   164 		return 0;
   165 	}
   166 
   167 	return sp_ftoc((float)atof(str));
   168 }
   169 
   170 static cell_t sm_floatfract(IPluginContext *pCtx, const cell_t *params)
   171 {
   172 	float val = sp_ctof(params[1]);
   173 	val = val - floor(val);
   174 
   175 	return sp_ftoc(val);
   176 }
   177 
   178 static cell_t sm_floatsin(IPluginContext *pCtx, const cell_t *params)
   179 {
   180 	float val = sp_ctof(params[1]);
   181 	val = sin(val);
   182 
   183 	return sp_ftoc(val);
   184 }
   185 
   186 static cell_t sm_floatcos(IPluginContext *pCtx, const cell_t *params)
   187 {
   188 	float val = sp_ctof(params[1]);
   189 	val = cos(val);
   190 
   191 	return sp_ftoc(val);
   192 }
   193 
   194 static cell_t sm_floattan(IPluginContext *pCtx, const cell_t *params)
   195 {
   196 	float val = sp_ctof(params[1]);
   197 	val = tan(val);
   198 
   199 	return sp_ftoc(val);
   200 }
   201 
   202 static cell_t sm_floatasin(IPluginContext *pCtx, const cell_t *params)
   203 {
   204 	float val = sp_ctof(params[1]);
   205 	val = asin(val);
   206 
   207 	return sp_ftoc(val);
   208 }
   209 
   210 static cell_t sm_floatacos(IPluginContext *pCtx, const cell_t *params)
   211 {
   212 	float val = sp_ctof(params[1]);
   213 	val = acos(val);
   214 
   215 	return sp_ftoc(val);
   216 }
   217 
   218 static cell_t sm_floatatan(IPluginContext *pCtx, const cell_t *params)
   219 {
   220 	float val = sp_ctof(params[1]);
   221 	val = atan(val);
   222 
   223 	return sp_ftoc(val);
   224 }
   225 
   226 static cell_t sm_floatatan2(IPluginContext *pCtx, const cell_t *params)
   227 {
   228 	float val1 = sp_ctof(params[1]);
   229 	float val2 = sp_ctof(params[2]);
   230 	val1 = atan2(val1, val2);
   231 
   232 	return sp_ftoc(val1);
   233 }