2006-12-23 03:20:53 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "sp_vm_api.h"
|
2006-12-30 08:23:17 +01:00
|
|
|
#include "sp_typeutil.h"
|
2006-12-23 03:20:53 +01:00
|
|
|
|
|
|
|
using namespace SourcePawn;
|
|
|
|
|
2007-01-01 11:35:15 +01:00
|
|
|
//:TODO: these need to be registered....
|
2006-12-23 03:20:53 +01:00
|
|
|
|
|
|
|
/****************************************
|
|
|
|
* *
|
|
|
|
* FLOATING POINT NATIVE IMPLEMENTATIONS *
|
|
|
|
* *
|
|
|
|
****************************************/
|
|
|
|
|
|
|
|
|
|
|
|
static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
|
|
|
float val = static_cast<float>(params[1]);
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatabs(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-23 03:20:53 +01:00
|
|
|
val = (val >= 0) ? val : -val;
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatadd(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]) + sp_ctof(params[2]);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatsub(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]) - sp_ctof(params[2]);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatmul(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]) * sp_ctof(params[2]);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatdiv(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]) / sp_ctof(params[2]);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatcmp(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val1 = sp_ctof(params[1]);
|
|
|
|
float val2 = sp_ctof(params[2]);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
|
|
|
if (val1 > val2)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
} else if (val1 < val2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatlog(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
|
|
|
float base = sp_ctof(params[2]);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
|
|
|
if ((val <= 0) || (base <= 0))
|
|
|
|
{
|
|
|
|
//:TODO: error out! logs cant take in negative numbers and log 0=-inf
|
|
|
|
}
|
|
|
|
if (base == 10.0)
|
|
|
|
{
|
|
|
|
val = log10(val);
|
|
|
|
} else {
|
|
|
|
val = log(val) / log(base);
|
|
|
|
}
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatexp(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(exp(val));
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatpower(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float base = sp_ctof(params[1]);
|
|
|
|
float exponent = sp_ctof(params[2]);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(pow(base, exponent));
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatsqroot(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
|
|
|
if (val < 0.0)
|
|
|
|
{
|
|
|
|
//:TODO: error out! we dont support complex numbers
|
|
|
|
}
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(sqrt(val));
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatround(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
|
|
|
switch (params[2])
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
val = floor(val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
val = ceil(val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
if (val >= 0.0)
|
|
|
|
{
|
|
|
|
val = floor(val);
|
|
|
|
} else {
|
|
|
|
val = ceil(val);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
val = (float)floor(val + 0.5);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-01 04:33:14 +01:00
|
|
|
return static_cast<int>(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatstr(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
pCtx->LocalToString(params[1], &str);
|
|
|
|
if (!strlen(str))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc((float)atof(str));
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatfract(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-23 03:20:53 +01:00
|
|
|
val = val - floor(val);
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatsin(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-30 23:16:53 +01:00
|
|
|
val = sin(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatcos(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-30 23:16:53 +01:00
|
|
|
val = cos(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floattan(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-30 23:16:53 +01:00
|
|
|
val = tan(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatasin(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-23 03:20:53 +01:00
|
|
|
val = asin(val);
|
|
|
|
|
2006-12-30 23:16:53 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatacos(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-23 03:20:53 +01:00
|
|
|
val = acos(val);
|
|
|
|
|
2006-12-30 23:16:53 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatatan(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = sp_ctof(params[1]);
|
2006-12-23 03:20:53 +01:00
|
|
|
val = atan(val);
|
|
|
|
|
2006-12-30 23:16:53 +01:00
|
|
|
return sp_ftoc(val);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floatatan2(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
float val1 = sp_ctof(params[1]);
|
|
|
|
float val2 = sp_ctof(params[2]);
|
2006-12-23 03:20:53 +01:00
|
|
|
val1 = atan2(val1, val2);
|
|
|
|
|
2006-12-30 23:16:53 +01:00
|
|
|
return sp_ftoc(val1);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|