Code::Blocks  SVN r11506
sqstdmath.cpp
Go to the documentation of this file.
1 /* see copyright notice in squirrel.h */
2 #include <squirrel.h>
3 #include <math.h>
4 #include <stdlib.h>
5 #include <sqstdmath.h>
6 
7 #define SINGLE_ARG_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
8  SQFloat f; \
9  sq_getfloat(v,2,&f); \
10  sq_pushfloat(v,(SQFloat)_funcname(f)); \
11  return 1; \
12 }
13 
14 #define TWO_ARGS_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
15  SQFloat p1,p2; \
16  sq_getfloat(v,2,&p1); \
17  sq_getfloat(v,3,&p2); \
18  sq_pushfloat(v,(SQFloat)_funcname(p1,p2)); \
19  return 1; \
20 }
21 
22 static SQInteger math_srand(HSQUIRRELVM v)
23 {
24  SQInteger i;
25  if(SQ_FAILED(sq_getinteger(v,2,&i)))
26  return sq_throwerror(v,_SC("invalid param"));
27  srand((unsigned int)i);
28  return 0;
29 }
30 
31 static SQInteger math_rand(HSQUIRRELVM v)
32 {
33  sq_pushinteger(v,rand());
34  return 1;
35 }
36 
37 static SQInteger math_abs(HSQUIRRELVM v)
38 {
39  SQInteger n;
40  sq_getinteger(v,2,&n);
41  sq_pushinteger(v,(SQInteger)abs((int)n));
42  return 1;
43 }
44 
45 SINGLE_ARG_FUNC(sqrt)
46 SINGLE_ARG_FUNC(fabs)
47 SINGLE_ARG_FUNC(sin)
48 SINGLE_ARG_FUNC(cos)
49 SINGLE_ARG_FUNC(asin)
50 SINGLE_ARG_FUNC(acos)
51 SINGLE_ARG_FUNC(log)
52 SINGLE_ARG_FUNC(log10)
53 SINGLE_ARG_FUNC(tan)
54 SINGLE_ARG_FUNC(atan)
55 TWO_ARGS_FUNC(atan2)
56 TWO_ARGS_FUNC(pow)
57 SINGLE_ARG_FUNC(floor)
58 SINGLE_ARG_FUNC(ceil)
59 SINGLE_ARG_FUNC(exp)
60 
61 #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),math_##name,nparams,tycheck}
62 static SQRegFunction mathlib_funcs[] = {
63  _DECL_FUNC(sqrt,2,_SC(".n")),
64  _DECL_FUNC(sin,2,_SC(".n")),
65  _DECL_FUNC(cos,2,_SC(".n")),
66  _DECL_FUNC(asin,2,_SC(".n")),
67  _DECL_FUNC(acos,2,_SC(".n")),
68  _DECL_FUNC(log,2,_SC(".n")),
69  _DECL_FUNC(log10,2,_SC(".n")),
70  _DECL_FUNC(tan,2,_SC(".n")),
71  _DECL_FUNC(atan,2,_SC(".n")),
72  _DECL_FUNC(atan2,3,_SC(".nn")),
73  _DECL_FUNC(pow,3,_SC(".nn")),
74  _DECL_FUNC(floor,2,_SC(".n")),
75  _DECL_FUNC(ceil,2,_SC(".n")),
76  _DECL_FUNC(exp,2,_SC(".n")),
77  _DECL_FUNC(srand,2,_SC(".n")),
78  _DECL_FUNC(rand,1,NULL),
79  _DECL_FUNC(fabs,2,_SC(".n")),
80  _DECL_FUNC(abs,2,_SC(".n")),
81  {0,0},
82 };
83 
84 #ifndef M_PI
85 #define M_PI (3.14159265358979323846)
86 #endif
87 
88 SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
89 {
90  SQInteger i=0;
91  while(mathlib_funcs[i].name!=0) {
92  sq_pushstring(v,mathlib_funcs[i].name,-1);
93  sq_newclosure(v,mathlib_funcs[i].f,0);
94  sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
96  sq_createslot(v,-3);
97  i++;
98  }
99  sq_pushstring(v,_SC("RAND_MAX"),-1);
100  sq_pushinteger(v,RAND_MAX);
101  sq_createslot(v,-3);
102  sq_pushstring(v,_SC("PI"),-1);
103  sq_pushfloat(v,(SQFloat)M_PI);
104  sq_createslot(v,-3);
105  return SQ_OK;
106 }
#define _DECL_FUNC(name, nparams, tycheck)
Definition: sqstdmath.cpp:61
SQRESULT sq_throwerror(HSQUIRRELVM v, const SQChar *err)
Definition: sqapi.cpp:918
#define TWO_ARGS_FUNC(_funcname)
Definition: sqstdmath.cpp:14
void sq_pushfloat(HSQUIRRELVM v, SQFloat n)
Definition: sqapi.cpp:212
#define SINGLE_ARG_FUNC(_funcname)
Definition: sqstdmath.cpp:7
SQRESULT sq_getinteger(HSQUIRRELVM v, SQInteger idx, SQInteger *i)
Definition: sqapi.cpp:509
void sq_pushinteger(HSQUIRRELVM v, SQInteger n)
Definition: sqapi.cpp:202
void sq_newclosure(HSQUIRRELVM v, SQFUNCTION func, SQUnsignedInteger nfreevars)
Definition: sqapi.cpp:338
SQRESULT sq_setnativeclosurename(HSQUIRRELVM v, SQInteger idx, const SQChar *name)
Definition: sqapi.cpp:362
SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
Definition: sqstdmath.cpp:88
SQRESULT sq_setparamscheck(HSQUIRRELVM v, SQInteger nparamscheck, const SQChar *typemask)
Definition: sqapi.cpp:373
void sq_pushstring(HSQUIRRELVM v, const SQChar *s, SQInteger len)
Definition: sqapi.cpp:195
#define M_PI
Definition: sqstdmath.cpp:85
static SQInteger math_abs(HSQUIRRELVM v)
Definition: sqstdmath.cpp:37
static SQInteger math_srand(HSQUIRRELVM v)
Definition: sqstdmath.cpp:22
static SQInteger math_rand(HSQUIRRELVM v)
Definition: sqstdmath.cpp:31
static SQRegFunction mathlib_funcs[]
Definition: sqstdmath.cpp:62
#define NULL
Definition: prefix.cpp:59