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 const 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 | {NULL,(SQFUNCTION)0,0,NULL} |
82 | }; |
83 | #undef _DECL_FUNC |
84 | |
85 | #ifndef M_PI |
86 | #define M_PI (3.14159265358979323846) |
87 | #endif |
88 | |
89 | SQRESULT sqstd_register_mathlib(HSQUIRRELVM v) |
90 | { |
91 | SQInteger i=0; |
92 | while(mathlib_funcs[i].name!=0) { |
93 | sq_pushstring(v,mathlib_funcs[i].name,-1); |
94 | sq_newclosure(v,mathlib_funcs[i].f,0); |
95 | sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask); |
96 | sq_setnativeclosurename(v,-1,mathlib_funcs[i].name); |
97 | sq_newslot(v,-3,SQFalse); |
98 | i++; |
99 | } |
100 | sq_pushstring(v,_SC("RAND_MAX" ),-1); |
101 | sq_pushinteger(v,RAND_MAX); |
102 | sq_newslot(v,-3,SQFalse); |
103 | sq_pushstring(v,_SC("PI" ),-1); |
104 | sq_pushfloat(v,(SQFloat)M_PI); |
105 | sq_newslot(v,-3,SQFalse); |
106 | return SQ_OK; |
107 | } |
108 | |