1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef jsmath_h
8 #define jsmath_h
9 
10 #include "mozilla/MemoryReporting.h"
11 
12 #include <stdint.h>
13 
14 #include "NamespaceImports.h"
15 
16 namespace js {
17 
18 using UnaryMathFunctionType = double (*)(double);
19 
20 // Used for inlining calls to double => double Math functions from JIT code.
21 // Note that this list does not include all unary Math functions: abs and sqrt
22 // for example are missing because the JITs optimize them without a C++ call.
23 enum class UnaryMathFunction : uint8_t {
24   Log,
25   Sin,
26   Cos,
27   Exp,
28   Tan,
29   ACos,
30   ASin,
31   ATan,
32   Log10,
33   Log2,
34   Log1P,
35   ExpM1,
36   CosH,
37   SinH,
38   TanH,
39   ACosH,
40   ASinH,
41   ATanH,
42   Trunc,
43   Cbrt,
44   Floor,
45   Ceil,
46   Round,
47 };
48 
49 extern UnaryMathFunctionType GetUnaryMathFunctionPtr(UnaryMathFunction fun);
50 extern const char* GetUnaryMathFunctionName(UnaryMathFunction fun);
51 
52 /*
53  * JS math functions.
54  */
55 
56 extern const JSClass MathClass;
57 
58 extern uint64_t GenerateRandomSeed();
59 
60 // Fill |seed[0]| and |seed[1]| with random bits, suitable for
61 // seeding a XorShift128+ random number generator.
62 extern void GenerateXorShift128PlusSeed(mozilla::Array<uint64_t, 2>& seed);
63 
64 extern double math_random_impl(JSContext* cx);
65 
66 extern bool math_random(JSContext* cx, unsigned argc, js::Value* vp);
67 
68 extern bool math_abs_handle(JSContext* cx, js::HandleValue v,
69                             js::MutableHandleValue r);
70 
71 extern bool math_abs(JSContext* cx, unsigned argc, js::Value* vp);
72 
73 extern double math_max_impl(double x, double y);
74 
75 extern bool math_max(JSContext* cx, unsigned argc, js::Value* vp);
76 
77 extern double math_min_impl(double x, double y);
78 
79 extern bool math_min(JSContext* cx, unsigned argc, js::Value* vp);
80 
81 extern double math_sqrt_impl(double x);
82 
83 extern bool math_sqrt_handle(JSContext* cx, js::HandleValue number,
84                              js::MutableHandleValue result);
85 
86 extern bool math_sqrt(JSContext* cx, unsigned argc, js::Value* vp);
87 
88 extern bool math_pow(JSContext* cx, unsigned argc, js::Value* vp);
89 
90 extern bool minmax_impl(JSContext* cx, bool max, js::HandleValue a,
91                         js::HandleValue b, js::MutableHandleValue res);
92 
93 extern bool math_imul_handle(JSContext* cx, HandleValue lhs, HandleValue rhs,
94                              MutableHandleValue res);
95 
96 extern bool math_imul(JSContext* cx, unsigned argc, js::Value* vp);
97 
98 extern bool RoundFloat32(JSContext* cx, HandleValue v, float* out);
99 
100 extern bool RoundFloat32(JSContext* cx, HandleValue arg,
101                          MutableHandleValue res);
102 
103 extern bool math_fround(JSContext* cx, unsigned argc, js::Value* vp);
104 
105 extern bool math_log(JSContext* cx, unsigned argc, js::Value* vp);
106 
107 extern double math_log_impl(double x);
108 
109 extern bool math_log_handle(JSContext* cx, HandleValue val,
110                             MutableHandleValue res);
111 
112 extern bool math_use_fdlibm_for_sin_cos_tan();
113 
114 extern bool math_sin(JSContext* cx, unsigned argc, js::Value* vp);
115 
116 extern double math_sin_fdlibm_impl(double x);
117 extern double math_sin_native_impl(double x);
118 
119 extern bool math_sin_handle(JSContext* cx, HandleValue val,
120                             MutableHandleValue res);
121 
122 extern bool math_cos(JSContext* cx, unsigned argc, js::Value* vp);
123 
124 extern double math_cos_fdlibm_impl(double x);
125 extern double math_cos_native_impl(double x);
126 
127 extern bool math_exp(JSContext* cx, unsigned argc, js::Value* vp);
128 
129 extern double math_exp_impl(double x);
130 
131 extern bool math_tan(JSContext* cx, unsigned argc, js::Value* vp);
132 
133 extern double math_tan_fdlibm_impl(double x);
134 extern double math_tan_native_impl(double x);
135 
136 extern bool math_log10(JSContext* cx, unsigned argc, js::Value* vp);
137 
138 extern bool math_log2(JSContext* cx, unsigned argc, js::Value* vp);
139 
140 extern bool math_log1p(JSContext* cx, unsigned argc, js::Value* vp);
141 
142 extern bool math_expm1(JSContext* cx, unsigned argc, js::Value* vp);
143 
144 extern bool math_cosh(JSContext* cx, unsigned argc, js::Value* vp);
145 
146 extern bool math_sinh(JSContext* cx, unsigned argc, js::Value* vp);
147 
148 extern bool math_tanh(JSContext* cx, unsigned argc, js::Value* vp);
149 
150 extern bool math_acosh(JSContext* cx, unsigned argc, js::Value* vp);
151 
152 extern bool math_asinh(JSContext* cx, unsigned argc, js::Value* vp);
153 
154 extern bool math_atanh(JSContext* cx, unsigned argc, js::Value* vp);
155 
156 extern double ecmaHypot(double x, double y);
157 
158 extern double hypot3(double x, double y, double z);
159 
160 extern double hypot4(double x, double y, double z, double w);
161 
162 extern bool math_hypot(JSContext* cx, unsigned argc, Value* vp);
163 
164 extern bool math_hypot_handle(JSContext* cx, HandleValueArray args,
165                               MutableHandleValue res);
166 
167 extern bool math_trunc(JSContext* cx, unsigned argc, Value* vp);
168 
169 extern bool math_sign(JSContext* cx, unsigned argc, Value* vp);
170 
171 extern bool math_cbrt(JSContext* cx, unsigned argc, Value* vp);
172 
173 extern bool math_asin(JSContext* cx, unsigned argc, Value* vp);
174 
175 extern bool math_acos(JSContext* cx, unsigned argc, Value* vp);
176 
177 extern bool math_atan(JSContext* cx, unsigned argc, Value* vp);
178 
179 extern bool math_atan2_handle(JSContext* cx, HandleValue y, HandleValue x,
180                               MutableHandleValue res);
181 
182 extern bool math_atan2(JSContext* cx, unsigned argc, Value* vp);
183 
184 extern double ecmaAtan2(double x, double y);
185 
186 extern double math_atan_impl(double x);
187 
188 extern bool math_atan(JSContext* cx, unsigned argc, js::Value* vp);
189 
190 extern double math_asin_impl(double x);
191 
192 extern bool math_asin(JSContext* cx, unsigned argc, js::Value* vp);
193 
194 extern double math_acos_impl(double x);
195 
196 extern bool math_acos(JSContext* cx, unsigned argc, js::Value* vp);
197 
198 extern bool math_ceil_handle(JSContext* cx, HandleValue value,
199                              MutableHandleValue res);
200 
201 extern bool math_ceil(JSContext* cx, unsigned argc, Value* vp);
202 
203 extern double math_ceil_impl(double x);
204 
205 extern bool math_clz32(JSContext* cx, unsigned argc, Value* vp);
206 
207 extern bool math_floor_handle(JSContext* cx, HandleValue v,
208                               MutableHandleValue r);
209 
210 extern bool math_floor(JSContext* cx, unsigned argc, Value* vp);
211 
212 extern double math_floor_impl(double x);
213 
214 template <typename T>
215 extern T GetBiggestNumberLessThan(T x);
216 
217 extern bool math_round_handle(JSContext* cx, HandleValue arg,
218                               MutableHandleValue res);
219 
220 extern bool math_round(JSContext* cx, unsigned argc, Value* vp);
221 
222 extern double math_round_impl(double x);
223 
224 extern float math_roundf_impl(float x);
225 
226 extern double powi(double x, int32_t y);
227 
228 extern double ecmaPow(double x, double y);
229 
230 extern double math_log10_impl(double x);
231 
232 extern double math_log2_impl(double x);
233 
234 extern double math_log1p_impl(double x);
235 
236 extern double math_expm1_impl(double x);
237 
238 extern double math_cosh_impl(double x);
239 
240 extern double math_sinh_impl(double x);
241 
242 extern double math_tanh_impl(double x);
243 
244 extern double math_acosh_impl(double x);
245 
246 extern double math_asinh_impl(double x);
247 
248 extern double math_atanh_impl(double x);
249 
250 extern double math_trunc_impl(double x);
251 
252 extern float math_truncf_impl(float x);
253 
254 extern bool math_trunc_handle(JSContext* cx, HandleValue v,
255                               MutableHandleValue r);
256 
257 extern double math_sign_impl(double x);
258 
259 extern bool math_sign_handle(JSContext* cx, HandleValue v,
260                              MutableHandleValue r);
261 
262 extern double math_cbrt_impl(double x);
263 
264 } /* namespace js */
265 
266 #endif /* jsmath_h */
267