forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsmath.cpp
1590 lines (1334 loc) · 35.6 KB
/
jsmath.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* JS math package.
*/
#include "jsmath.h"
#include "mozilla/Constants.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/unused.h"
#include <algorithm> // for std::max
#include <fcntl.h>
#ifdef XP_UNIX
# include <unistd.h>
#endif
#include "jsapi.h"
#include "jsatom.h"
#include "jscntxt.h"
#include "jscompartment.h"
#include "jslibmath.h"
#include "jstypes.h"
#include "prmjtime.h"
#include "jsobjinlines.h"
using namespace js;
using mozilla::Abs;
using mozilla::NumberEqualsInt32;
using mozilla::NumberIsInt32;
using mozilla::ExponentComponent;
using mozilla::FloatingPoint;
using mozilla::IsFinite;
using mozilla::IsInfinite;
using mozilla::IsNaN;
using mozilla::IsNegative;
using mozilla::IsNegativeZero;
using mozilla::PositiveInfinity;
using mozilla::NegativeInfinity;
using JS::ToNumber;
using JS::GenericNaN;
static const JSConstDoubleSpec math_constants[] = {
{"E" , M_E },
{"LOG2E" , M_LOG2E },
{"LOG10E" , M_LOG10E },
{"LN2" , M_LN2 },
{"LN10" , M_LN10 },
{"PI" , M_PI },
{"SQRT2" , M_SQRT2 },
{"SQRT1_2", M_SQRT1_2 },
{0,0}
};
MathCache::MathCache() {
memset(table, 0, sizeof(table));
/* See comments in lookup(). */
MOZ_ASSERT(IsNegativeZero(-0.0));
MOZ_ASSERT(!IsNegativeZero(+0.0));
MOZ_ASSERT(hash(-0.0, MathCache::Sin) != hash(+0.0, MathCache::Sin));
}
size_t
MathCache::sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf)
{
return mallocSizeOf(this);
}
const Class js::MathClass = {
js_Math_str,
JSCLASS_HAS_CACHED_PROTO(JSProto_Math)
};
bool
js::math_abs_handle(JSContext *cx, js::HandleValue v, js::MutableHandleValue r)
{
double x;
if (!ToNumber(cx, v, &x))
return false;
double z = Abs(x);
r.setNumber(z);
return true;
}
bool
js::math_abs(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
return math_abs_handle(cx, args[0], args.rval());
}
#if defined(SOLARIS) && defined(__GNUC__)
#define ACOS_IF_OUT_OF_RANGE(x) if (x < -1 || 1 < x) return GenericNaN();
#else
#define ACOS_IF_OUT_OF_RANGE(x)
#endif
double
js::math_acos_impl(MathCache *cache, double x)
{
ACOS_IF_OUT_OF_RANGE(x);
return cache->lookup(acos, x, MathCache::Acos);
}
double
js::math_acos_uncached(double x)
{
ACOS_IF_OUT_OF_RANGE(x);
return acos(x);
}
#undef ACOS_IF_OUT_OF_RANGE
bool
js::math_acos(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
double x;
if (!ToNumber(cx, args[0], &x))
return false;
MathCache *mathCache = cx->runtime()->getMathCache(cx);
if (!mathCache)
return false;
double z = math_acos_impl(mathCache, x);
args.rval().setDouble(z);
return true;
}
#if defined(SOLARIS) && defined(__GNUC__)
#define ASIN_IF_OUT_OF_RANGE(x) if (x < -1 || 1 < x) return GenericNaN();
#else
#define ASIN_IF_OUT_OF_RANGE(x)
#endif
double
js::math_asin_impl(MathCache *cache, double x)
{
ASIN_IF_OUT_OF_RANGE(x);
return cache->lookup(asin, x, MathCache::Asin);
}
double
js::math_asin_uncached(double x)
{
ASIN_IF_OUT_OF_RANGE(x);
return asin(x);
}
#undef ASIN_IF_OUT_OF_RANGE
bool
js::math_asin(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
double x;
if (!ToNumber(cx, args[0], &x))
return false;
MathCache *mathCache = cx->runtime()->getMathCache(cx);
if (!mathCache)
return false;
double z = math_asin_impl(mathCache, x);
args.rval().setDouble(z);
return true;
}
double
js::math_atan_impl(MathCache *cache, double x)
{
return cache->lookup(atan, x, MathCache::Atan);
}
double
js::math_atan_uncached(double x)
{
return atan(x);
}
bool
js::math_atan(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
double x;
if (!ToNumber(cx, args[0], &x))
return false;
MathCache *mathCache = cx->runtime()->getMathCache(cx);
if (!mathCache)
return false;
double z = math_atan_impl(mathCache, x);
args.rval().setDouble(z);
return true;
}
double
js::ecmaAtan2(double y, double x)
{
#if defined(_MSC_VER)
/*
* MSVC's atan2 does not yield the result demanded by ECMA when both x
* and y are infinite.
* - The result is a multiple of pi/4.
* - The sign of y determines the sign of the result.
* - The sign of x determines the multiplicator, 1 or 3.
*/
if (IsInfinite(y) && IsInfinite(x)) {
double z = js_copysign(M_PI / 4, y);
if (x < 0)
z *= 3;
return z;
}
#endif
#if defined(SOLARIS) && defined(__GNUC__)
if (y == 0) {
if (IsNegativeZero(x))
return js_copysign(M_PI, y);
if (x == 0)
return y;
}
#endif
return atan2(y, x);
}
bool
js::math_atan2_handle(JSContext *cx, HandleValue y, HandleValue x, MutableHandleValue res)
{
double dy;
if (!ToNumber(cx, y, &dy))
return false;
double dx;
if (!ToNumber(cx, x, &dx))
return false;
double z = ecmaAtan2(dy, dx);
res.setDouble(z);
return true;
}
bool
js::math_atan2(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
return math_atan2_handle(cx, args.get(0), args.get(1), args.rval());
}
double
js::math_ceil_impl(double x)
{
#ifdef __APPLE__
if (x < 0 && x > -1.0)
return js_copysign(0, -1);
#endif
return ceil(x);
}
bool
js::math_ceil_handle(JSContext *cx, HandleValue v, MutableHandleValue res)
{
double d;
if(!ToNumber(cx, v, &d))
return false;
double result = math_ceil_impl(d);
res.setDouble(result);
return true;
}
bool
js::math_ceil(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
return math_ceil_handle(cx, args[0], args.rval());
}
bool
js::math_clz32(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setInt32(32);
return true;
}
uint32_t n;
if (!ToUint32(cx, args[0], &n))
return false;
if (n == 0) {
args.rval().setInt32(32);
return true;
}
args.rval().setInt32(mozilla::CountLeadingZeroes32(n));
return true;
}
double
js::math_cos_impl(MathCache *cache, double x)
{
return cache->lookup(cos, x, MathCache::Cos);
}
double
js::math_cos_uncached(double x)
{
return cos(x);
}
bool
js::math_cos(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
double x;
if (!ToNumber(cx, args[0], &x))
return false;
MathCache *mathCache = cx->runtime()->getMathCache(cx);
if (!mathCache)
return false;
double z = math_cos_impl(mathCache, x);
args.rval().setDouble(z);
return true;
}
#ifdef _WIN32
#define EXP_IF_OUT_OF_RANGE(x) \
if (!IsNaN(x)) { \
if (x == PositiveInfinity<double>()) \
return PositiveInfinity<double>(); \
if (x == NegativeInfinity<double>()) \
return 0.0; \
}
#else
#define EXP_IF_OUT_OF_RANGE(x)
#endif
double
js::math_exp_impl(MathCache *cache, double x)
{
EXP_IF_OUT_OF_RANGE(x);
return cache->lookup(exp, x, MathCache::Exp);
}
double
js::math_exp_uncached(double x)
{
EXP_IF_OUT_OF_RANGE(x);
return exp(x);
}
#undef EXP_IF_OUT_OF_RANGE
bool
js::math_exp(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
double x;
if (!ToNumber(cx, args[0], &x))
return false;
MathCache *mathCache = cx->runtime()->getMathCache(cx);
if (!mathCache)
return false;
double z = math_exp_impl(mathCache, x);
args.rval().setNumber(z);
return true;
}
double
js::math_floor_impl(double x)
{
return floor(x);
}
bool
js::math_floor_handle(JSContext *cx, HandleValue v, MutableHandleValue r)
{
double d;
if (!ToNumber(cx, v, &d))
return false;
double z = math_floor_impl(d);
r.setNumber(z);
return true;
}
bool
js::math_floor(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
return math_floor_handle(cx, args[0], args.rval());
}
bool
js::math_imul(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
uint32_t a = 0, b = 0;
if (args.hasDefined(0) && !ToUint32(cx, args[0], &a))
return false;
if (args.hasDefined(1) && !ToUint32(cx, args[1], &b))
return false;
uint32_t product = a * b;
args.rval().setInt32(product > INT32_MAX
? int32_t(INT32_MIN + (product - INT32_MAX - 1))
: int32_t(product));
return true;
}
// Implements Math.fround (20.2.2.16) up to step 3
bool
js::RoundFloat32(JSContext *cx, HandleValue v, float *out)
{
double d;
bool success = ToNumber(cx, v, &d);
*out = static_cast<float>(d);
return success;
}
bool
js::RoundFloat32(JSContext *cx, HandleValue arg, MutableHandleValue res)
{
float f;
if (!RoundFloat32(cx, arg, &f))
return false;
res.setDouble(static_cast<double>(f));
return true;
}
bool
js::math_fround(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
return RoundFloat32(cx, args[0], args.rval());
}
#if defined(SOLARIS) && defined(__GNUC__)
#define LOG_IF_OUT_OF_RANGE(x) if (x < 0) return GenericNaN();
#else
#define LOG_IF_OUT_OF_RANGE(x)
#endif
double
js::math_log_impl(MathCache *cache, double x)
{
LOG_IF_OUT_OF_RANGE(x);
return cache->lookup(math_log_uncached, x, MathCache::Log);
}
double
js::math_log_uncached(double x)
{
LOG_IF_OUT_OF_RANGE(x);
return log(x);
}
#undef LOG_IF_OUT_OF_RANGE
bool
js::math_log_handle(JSContext *cx, HandleValue val, MutableHandleValue res)
{
double in;
if (!ToNumber(cx, val, &in))
return false;
MathCache *mathCache = cx->runtime()->getMathCache(cx);
if (!mathCache)
return false;
double out = math_log_impl(mathCache, in);
res.setNumber(out);
return true;
}
bool
js::math_log(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
return math_log_handle(cx, args[0], args.rval());
}
double
js::math_max_impl(double x, double y)
{
// Math.max(num, NaN) => NaN, Math.max(-0, +0) => +0
if (x > y || IsNaN(x) || (x == y && IsNegative(y)))
return x;
return y;
}
bool
js::math_max(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
double maxval = NegativeInfinity<double>();
for (unsigned i = 0; i < args.length(); i++) {
double x;
if (!ToNumber(cx, args[i], &x))
return false;
maxval = math_max_impl(x, maxval);
}
args.rval().setNumber(maxval);
return true;
}
double
js::math_min_impl(double x, double y)
{
// Math.min(num, NaN) => NaN, Math.min(-0, +0) => -0
if (x < y || IsNaN(x) || (x == y && IsNegativeZero(x)))
return x;
return y;
}
bool
js::math_min(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
double minval = PositiveInfinity<double>();
for (unsigned i = 0; i < args.length(); i++) {
double x;
if (!ToNumber(cx, args[i], &x))
return false;
minval = math_min_impl(x, minval);
}
args.rval().setNumber(minval);
return true;
}
bool
js::minmax_impl(JSContext *cx, bool max, HandleValue a, HandleValue b, MutableHandleValue res)
{
double x, y;
if (!ToNumber(cx, a, &x))
return false;
if (!ToNumber(cx, b, &y))
return false;
if (max)
res.setNumber(math_max_impl(x, y));
else
res.setNumber(math_min_impl(x, y));
return true;
}
double
js::powi(double x, int y)
{
unsigned n = (y < 0) ? -y : y;
double m = x;
double p = 1;
while (true) {
if ((n & 1) != 0) p *= m;
n >>= 1;
if (n == 0) {
if (y < 0) {
// Unfortunately, we have to be careful when p has reached
// infinity in the computation, because sometimes the higher
// internal precision in the pow() implementation would have
// given us a finite p. This happens very rarely.
double result = 1.0 / p;
return (result == 0 && IsInfinite(p))
? pow(x, static_cast<double>(y)) // Avoid pow(double, int).
: result;
}
return p;
}
m *= m;
}
}
double
js::ecmaPow(double x, double y)
{
/*
* Use powi if the exponent is an integer-valued double. We don't have to
* check for NaN since a comparison with NaN is always false.
*/
int32_t yi;
if (NumberEqualsInt32(y, &yi))
return powi(x, yi);
/*
* Because C99 and ECMA specify different behavior for pow(),
* we need to wrap the libm call to make it ECMA compliant.
*/
if (!IsFinite(y) && (x == 1.0 || x == -1.0))
return GenericNaN();
/* pow(x, +-0) is always 1, even for x = NaN (MSVC gets this wrong). */
if (y == 0)
return 1;
/*
* Special case for square roots. Note that pow(x, 0.5) != sqrt(x)
* when x = -0.0, so we have to guard for this.
*/
if (IsFinite(x) && x != 0.0) {
if (y == 0.5)
return sqrt(x);
if (y == -0.5)
return 1.0 / sqrt(x);
}
return pow(x, y);
}
bool
js::math_pow_handle(JSContext *cx, HandleValue base, HandleValue power, MutableHandleValue result)
{
double x;
if (!ToNumber(cx, base, &x))
return false;
double y;
if (!ToNumber(cx, power, &y))
return false;
double z = ecmaPow(x, y);
result.setNumber(z);
return true;
}
bool
js::math_pow(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
return math_pow_handle(cx, args.get(0), args.get(1), args.rval());
}
static uint64_t
random_generateSeed()
{
union {
uint8_t u8[8];
uint32_t u32[2];
uint64_t u64;
} seed;
seed.u64 = 0;
#if defined(XP_WIN)
errno_t error = rand_s(&seed.u32[0]);
MOZ_ASSERT(error == 0, "rand_s() error?!");
error = rand_s(&seed.u32[1]);
MOZ_ASSERT(error == 0, "rand_s() error?!");
#elif defined(XP_UNIX)
/*
* In the unlikely event we can't read /dev/urandom, there's not much we can
* do, so just mix in the fd error code and the current time.
*/
int fd = open("/dev/urandom", O_RDONLY);
MOZ_ASSERT(fd >= 0, "Can't open /dev/urandom?!");
if (fd >= 0) {
ssize_t nread = read(fd, seed.u8, mozilla::ArrayLength(seed.u8));
MOZ_ASSERT(nread == 8, "Can't read /dev/urandom?!");
mozilla::unused << nread;
close(fd);
}
seed.u32[0] ^= fd;
#else
# error "Platform needs to implement random_generateSeed()"
#endif
seed.u64 ^= PRMJ_Now();
return seed.u64;
}
static const uint64_t RNG_MULTIPLIER = 0x5DEECE66DLL;
static const uint64_t RNG_ADDEND = 0xBLL;
static const uint64_t RNG_MASK = (1LL << 48) - 1;
/*
* Math.random() support, lifted from java.util.Random.java.
*/
void
js::random_initState(uint64_t *rngState)
{
/* Our PRNG only uses 48 bits, so squeeze our entropy into those bits. */
uint64_t seed = random_generateSeed();
seed ^= (seed >> 16);
*rngState = (seed ^ RNG_MULTIPLIER) & RNG_MASK;
}
uint64_t
js::random_next(uint64_t *rngState, int bits)
{
MOZ_ASSERT((*rngState & 0xffff000000000000ULL) == 0, "Bad rngState");
MOZ_ASSERT(bits > 0 && bits <= 48, "bits is out of range");
if (*rngState == 0) {
random_initState(rngState);
}
uint64_t nextstate = *rngState * RNG_MULTIPLIER;
nextstate += RNG_ADDEND;
nextstate &= RNG_MASK;
*rngState = nextstate;
return nextstate >> (48 - bits);
}
double
js::math_random_no_outparam(JSContext *cx)
{
/* Calculate random without memory traffic, for use in the JITs. */
return random_nextDouble(&cx->compartment()->rngState);
}
bool
js::math_random(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
double z = random_nextDouble(&cx->compartment()->rngState);
args.rval().setDouble(z);
return true;
}
bool
js::math_round_handle(JSContext *cx, HandleValue arg, MutableHandleValue res)
{
double d;
if (!ToNumber(cx, arg, &d))
return false;
d = math_round_impl(d);
res.setNumber(d);
return true;
}
template<typename T>
T
js::GetBiggestNumberLessThan(T x)
{
MOZ_ASSERT(!IsNegative(x));
MOZ_ASSERT(IsFinite(x));
typedef typename mozilla::FloatingPoint<T>::Bits Bits;
Bits bits = mozilla::BitwiseCast<Bits>(x);
MOZ_ASSERT(bits > 0, "will underflow");
return mozilla::BitwiseCast<T>(bits - 1);
}
template double js::GetBiggestNumberLessThan<>(double x);
template float js::GetBiggestNumberLessThan<>(float x);
double
js::math_round_impl(double x)
{
int32_t ignored;
if (NumberIsInt32(x, &ignored))
return x;
/* Some numbers are so big that adding 0.5 would give the wrong number. */
if (ExponentComponent(x) >= int_fast16_t(FloatingPoint<double>::kExponentShift))
return x;
double add = (x >= 0) ? GetBiggestNumberLessThan(0.5) : 0.5;
return js_copysign(floor(x + add), x);
}
float
js::math_roundf_impl(float x)
{
int32_t ignored;
if (NumberIsInt32(x, &ignored))
return x;
/* Some numbers are so big that adding 0.5 would give the wrong number. */
if (ExponentComponent(x) >= int_fast16_t(FloatingPoint<float>::kExponentShift))
return x;
float add = (x >= 0) ? GetBiggestNumberLessThan(0.5f) : 0.5f;
return js_copysign(floorf(x + add), x);
}
bool /* ES5 15.8.2.15. */
js::math_round(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
return math_round_handle(cx, args[0], args.rval());
}
double
js::math_sin_impl(MathCache *cache, double x)
{
return cache->lookup(math_sin_uncached, x, MathCache::Sin);
}
double
js::math_sin_uncached(double x)
{
#ifdef _WIN64
// Workaround MSVC bug where sin(-0) is +0 instead of -0 on x64 on
// CPUs without FMA3 (pre-Haswell). See bug 1076670.
if (IsNegativeZero(x))
return -0.0;
#endif
return sin(x);
}
bool
js::math_sin_handle(JSContext *cx, HandleValue val, MutableHandleValue res)
{
double in;
if (!ToNumber(cx, val, &in))
return false;
MathCache *mathCache = cx->runtime()->getMathCache(cx);
if (!mathCache)
return false;
double out = math_sin_impl(mathCache, in);
res.setDouble(out);
return true;
}
bool
js::math_sin(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
return math_sin_handle(cx, args[0], args.rval());
}
bool
js::math_sqrt_handle(JSContext *cx, HandleValue number, MutableHandleValue result)
{
double x;
if (!ToNumber(cx, number, &x))
return false;
MathCache *mathCache = cx->runtime()->getMathCache(cx);
if (!mathCache)
return false;
double z = mathCache->lookup(sqrt, x, MathCache::Sqrt);
result.setDouble(z);
return true;
}
bool
js::math_sqrt(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
return math_sqrt_handle(cx, args[0], args.rval());
}
double
js::math_tan_impl(MathCache *cache, double x)
{
return cache->lookup(tan, x, MathCache::Tan);
}
double
js::math_tan_uncached(double x)
{
return tan(x);
}
bool
js::math_tan(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNaN();
return true;
}
double x;
if (!ToNumber(cx, args[0], &x))
return false;
MathCache *mathCache = cx->runtime()->getMathCache(cx);
if (!mathCache)
return false;
double z = math_tan_impl(mathCache, x);
args.rval().setDouble(z);
return true;
}
typedef double (*UnaryMathFunctionType)(MathCache *cache, double);
template <UnaryMathFunctionType F>
static bool math_function(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() == 0) {
args.rval().setNumber(GenericNaN());
return true;