comparison mupdf-source/thirdparty/mujs/jsmath.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 #include "jsi.h"
2
3 #if defined(_MSC_VER) && (_MSC_VER < 1700) /* VS2012 has stdint.h */
4 typedef unsigned int uint32_t;
5 typedef unsigned __int64 uint64_t;
6 #else
7 #include <stdint.h>
8 #endif
9
10 #include <time.h>
11
12 static double jsM_round(double x)
13 {
14 if (isnan(x)) return x;
15 if (isinf(x)) return x;
16 if (x == 0) return x;
17 if (x > 0 && x < 0.5) return 0;
18 if (x < 0 && x >= -0.5) return -0;
19 return floor(x + 0.5);
20 }
21
22 static void Math_abs(js_State *J)
23 {
24 js_pushnumber(J, fabs(js_tonumber(J, 1)));
25 }
26
27 static void Math_acos(js_State *J)
28 {
29 js_pushnumber(J, acos(js_tonumber(J, 1)));
30 }
31
32 static void Math_asin(js_State *J)
33 {
34 js_pushnumber(J, asin(js_tonumber(J, 1)));
35 }
36
37 static void Math_atan(js_State *J)
38 {
39 js_pushnumber(J, atan(js_tonumber(J, 1)));
40 }
41
42 static void Math_atan2(js_State *J)
43 {
44 double y = js_tonumber(J, 1);
45 double x = js_tonumber(J, 2);
46 js_pushnumber(J, atan2(y, x));
47 }
48
49 static void Math_ceil(js_State *J)
50 {
51 js_pushnumber(J, ceil(js_tonumber(J, 1)));
52 }
53
54 static void Math_cos(js_State *J)
55 {
56 js_pushnumber(J, cos(js_tonumber(J, 1)));
57 }
58
59 static void Math_exp(js_State *J)
60 {
61 js_pushnumber(J, exp(js_tonumber(J, 1)));
62 }
63
64 static void Math_floor(js_State *J)
65 {
66 js_pushnumber(J, floor(js_tonumber(J, 1)));
67 }
68
69 static void Math_log(js_State *J)
70 {
71 js_pushnumber(J, log(js_tonumber(J, 1)));
72 }
73
74 static void Math_pow(js_State *J)
75 {
76 double x = js_tonumber(J, 1);
77 double y = js_tonumber(J, 2);
78 if (!isfinite(y) && fabs(x) == 1)
79 js_pushnumber(J, NAN);
80 else
81 js_pushnumber(J, pow(x,y));
82 }
83
84 static void Math_random(js_State *J)
85 {
86 /* Lehmer generator with a=48271 and m=2^31-1 */
87 /* Park & Miller (1988). Random Number Generators: Good ones are hard to find. */
88 J->seed = (uint64_t) J->seed * 48271 % 0x7fffffff;
89 js_pushnumber(J, (double) J->seed / 0x7fffffff);
90 }
91
92 static void Math_init_random(js_State *J)
93 {
94 /* Pick initial seed by scrambling current time with Xorshift. */
95 /* Marsaglia (2003). Xorshift RNGs. */
96 J->seed = time(0) + 123;
97 J->seed ^= J->seed << 13;
98 J->seed ^= J->seed >> 17;
99 J->seed ^= J->seed << 5;
100 J->seed %= 0x7fffffff;
101 }
102
103 static void Math_round(js_State *J)
104 {
105 double x = js_tonumber(J, 1);
106 js_pushnumber(J, jsM_round(x));
107 }
108
109 static void Math_sin(js_State *J)
110 {
111 js_pushnumber(J, sin(js_tonumber(J, 1)));
112 }
113
114 static void Math_sqrt(js_State *J)
115 {
116 js_pushnumber(J, sqrt(js_tonumber(J, 1)));
117 }
118
119 static void Math_tan(js_State *J)
120 {
121 js_pushnumber(J, tan(js_tonumber(J, 1)));
122 }
123
124 static void Math_max(js_State *J)
125 {
126 int i, n = js_gettop(J);
127 double x = -INFINITY;
128 for (i = 1; i < n; ++i) {
129 double y = js_tonumber(J, i);
130 if (isnan(y)) {
131 x = y;
132 break;
133 }
134 if (signbit(x) == signbit(y))
135 x = x > y ? x : y;
136 else if (signbit(x))
137 x = y;
138 }
139 js_pushnumber(J, x);
140 }
141
142 static void Math_min(js_State *J)
143 {
144 int i, n = js_gettop(J);
145 double x = INFINITY;
146 for (i = 1; i < n; ++i) {
147 double y = js_tonumber(J, i);
148 if (isnan(y)) {
149 x = y;
150 break;
151 }
152 if (signbit(x) == signbit(y))
153 x = x < y ? x : y;
154 else if (signbit(y))
155 x = y;
156 }
157 js_pushnumber(J, x);
158 }
159
160 void jsB_initmath(js_State *J)
161 {
162 Math_init_random(J);
163 js_pushobject(J, jsV_newobject(J, JS_CMATH, J->Object_prototype));
164 {
165 jsB_propn(J, "E", 2.7182818284590452354);
166 jsB_propn(J, "LN10", 2.302585092994046);
167 jsB_propn(J, "LN2", 0.6931471805599453);
168 jsB_propn(J, "LOG2E", 1.4426950408889634);
169 jsB_propn(J, "LOG10E", 0.4342944819032518);
170 jsB_propn(J, "PI", 3.1415926535897932);
171 jsB_propn(J, "SQRT1_2", 0.7071067811865476);
172 jsB_propn(J, "SQRT2", 1.4142135623730951);
173
174 jsB_propf(J, "Math.abs", Math_abs, 1);
175 jsB_propf(J, "Math.acos", Math_acos, 1);
176 jsB_propf(J, "Math.asin", Math_asin, 1);
177 jsB_propf(J, "Math.atan", Math_atan, 1);
178 jsB_propf(J, "Math.atan2", Math_atan2, 2);
179 jsB_propf(J, "Math.ceil", Math_ceil, 1);
180 jsB_propf(J, "Math.cos", Math_cos, 1);
181 jsB_propf(J, "Math.exp", Math_exp, 1);
182 jsB_propf(J, "Math.floor", Math_floor, 1);
183 jsB_propf(J, "Math.log", Math_log, 1);
184 jsB_propf(J, "Math.max", Math_max, 0); /* 2 */
185 jsB_propf(J, "Math.min", Math_min, 0); /* 2 */
186 jsB_propf(J, "Math.pow", Math_pow, 2);
187 jsB_propf(J, "Math.random", Math_random, 0);
188 jsB_propf(J, "Math.round", Math_round, 1);
189 jsB_propf(J, "Math.sin", Math_sin, 1);
190 jsB_propf(J, "Math.sqrt", Math_sqrt, 1);
191 jsB_propf(J, "Math.tan", Math_tan, 1);
192 }
193 js_defglobal(J, "Math", JS_DONTENUM);
194 }