sqstdmath.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* see copyright notice in squirrel.h */
  2. #include <squirrel.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <sqstdmath.h>
  6. #define SINGLE_ARG_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
  7. SQFloat f; \
  8. sq_getfloat(v,2,&f); \
  9. sq_pushfloat(v,(SQFloat)_funcname(f)); \
  10. return 1; \
  11. }
  12. #define TWO_ARGS_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
  13. SQFloat p1,p2; \
  14. sq_getfloat(v,2,&p1); \
  15. sq_getfloat(v,3,&p2); \
  16. sq_pushfloat(v,(SQFloat)_funcname(p1,p2)); \
  17. return 1; \
  18. }
  19. static SQInteger math_srand(HSQUIRRELVM v)
  20. {
  21. SQInteger i;
  22. if(SQ_FAILED(sq_getinteger(v,2,&i)))
  23. return sq_throwerror(v,_SC("invalid param"));
  24. srand((unsigned int)i);
  25. return 0;
  26. }
  27. static SQInteger math_rand(HSQUIRRELVM v)
  28. {
  29. sq_pushinteger(v,rand());
  30. return 1;
  31. }
  32. static SQInteger math_abs(HSQUIRRELVM v)
  33. {
  34. SQInteger n;
  35. sq_getinteger(v,2,&n);
  36. sq_pushinteger(v,(SQInteger)abs((int)n));
  37. return 1;
  38. }
  39. SINGLE_ARG_FUNC(sqrt)
  40. SINGLE_ARG_FUNC(fabs)
  41. SINGLE_ARG_FUNC(sin)
  42. SINGLE_ARG_FUNC(cos)
  43. SINGLE_ARG_FUNC(asin)
  44. SINGLE_ARG_FUNC(acos)
  45. SINGLE_ARG_FUNC(log)
  46. SINGLE_ARG_FUNC(log10)
  47. SINGLE_ARG_FUNC(tan)
  48. SINGLE_ARG_FUNC(atan)
  49. TWO_ARGS_FUNC(atan2)
  50. TWO_ARGS_FUNC(pow)
  51. SINGLE_ARG_FUNC(floor)
  52. SINGLE_ARG_FUNC(ceil)
  53. SINGLE_ARG_FUNC(exp)
  54. #define _DECL_FUNC(name,nparamsmin,nparamsmax,tycheck) {_SC(#name),math_##name,nparamsmin,nparamsmax,tycheck}
  55. static const SQRegFunction mathlib_funcs[] = {
  56. _DECL_FUNC(sqrt,2,2,_SC(".n")),
  57. _DECL_FUNC(sin,2,2,_SC(".n")),
  58. _DECL_FUNC(cos,2,2,_SC(".n")),
  59. _DECL_FUNC(asin,2,2,_SC(".n")),
  60. _DECL_FUNC(acos,2,2,_SC(".n")),
  61. _DECL_FUNC(log,2,2,_SC(".n")),
  62. _DECL_FUNC(log10,2,2,_SC(".n")),
  63. _DECL_FUNC(tan,2,2,_SC(".n")),
  64. _DECL_FUNC(atan,2,2,_SC(".n")),
  65. _DECL_FUNC(atan2,3,3,_SC(".nn")),
  66. _DECL_FUNC(pow,3,3,_SC(".nn")),
  67. _DECL_FUNC(floor,2,2,_SC(".n")),
  68. _DECL_FUNC(ceil,2,2,_SC(".n")),
  69. _DECL_FUNC(exp,2,2,_SC(".n")),
  70. _DECL_FUNC(srand,2,2,_SC(".n")),
  71. _DECL_FUNC(rand,1,1,NULL),
  72. _DECL_FUNC(fabs,2,2,_SC(".n")),
  73. _DECL_FUNC(abs,2,2,_SC(".n")),
  74. {NULL,(SQFUNCTION)0,0,0,NULL}
  75. };
  76. #undef _DECL_FUNC
  77. #ifndef M_PI
  78. #define M_PI (3.14159265358979323846)
  79. #endif
  80. SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
  81. {
  82. SQInteger i=0;
  83. while(mathlib_funcs[i].name!=0) {
  84. sq_pushstring(v,mathlib_funcs[i].name,-1);
  85. sq_newclosure(v,mathlib_funcs[i].f,0);
  86. sq_setparamscheck(v,mathlib_funcs[i].nparamscheckmin,mathlib_funcs[i].nparamscheckmax,mathlib_funcs[i].typemask);
  87. sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
  88. sq_newslot(v,-3,SQFalse);
  89. i++;
  90. }
  91. sq_pushstring(v,_SC("RAND_MAX"),-1);
  92. sq_pushinteger(v,RAND_MAX);
  93. sq_newslot(v,-3,SQFalse);
  94. sq_pushstring(v,_SC("PI"),-1);
  95. sq_pushfloat(v,(SQFloat)M_PI);
  96. sq_newslot(v,-3,SQFalse);
  97. return SQ_OK;
  98. }