forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberUtilities.cpp
More file actions
643 lines (579 loc) · 19.3 KB
/
NumberUtilities.cpp
File metadata and controls
643 lines (579 loc) · 19.3 KB
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "CommonCommonPch.h"
#include "Common\UInt32Math.h"
#include "common\NumberUtilities.inl"
namespace Js
{
const double NumberConstants::MAX_VALUE = *(double*)(&NumberConstants::k_PosMax);
const double NumberConstants::MIN_VALUE = *(double*)(&NumberConstants::k_PosMin);
const double NumberConstants::NaN = *(double*)(&NumberConstants::k_Nan);
const double NumberConstants::NEGATIVE_INFINITY= *(double*)(&NumberConstants::k_NegInf);
const double NumberConstants::POSITIVE_INFINITY= *(double*)(&NumberConstants::k_PosInf );
const double NumberConstants::NEG_ZERO= *(double*)(&NumberConstants::k_NegZero );
// These are used in 128-bit operations in the JIT and inline asm
__declspec(align(16)) const BYTE NumberConstants::AbsDoubleCst[] =
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F };
__declspec(align(16)) const BYTE NumberConstants::AbsFloatCst[] =
{ 0xFF, 0xFF, 0xFF, 0x7F,
0xFF, 0xFF, 0xFF, 0x7F,
0xFF, 0xFF, 0xFF, 0x7F,
0xFF, 0xFF, 0xFF, 0x7F };
__declspec(align(16)) double const NumberConstants::UIntConvertConst[2] = { 0, 4294967296.000000 };
__declspec(align(16)) float const NumberConstants::MaskNegFloat[] = { -0.0f, -0.0f, -0.0f, -0.0f };
__declspec(align(16)) double const NumberConstants::MaskNegDouble[] = { -0.0, -0.0 };
bool NumberUtilities::IsDigit(int ch)
{
return ch >= '0' && ch <= '9';
}
BOOL NumberUtilities::FHexDigit(wchar_t ch, int *pw)
{
if ((ch -= '0') <= 9)
{
*pw = ch;
return TRUE;
}
if ((ch -= 'A' - '0') <= 5 || (ch -= 'a' - 'A') <= 5)
{
*pw = 10 + ch;
return TRUE;
}
return FALSE;
}
/***************************************************************************
Multiply two unsigned longs. Return the low ulong and fill *pluHi with
the high ulong.
***************************************************************************/
#pragma warning(push)
#pragma warning(disable:4035) // Turn off warning that there is no return value
ulong NumberUtilities::MulLu(ulong lu1, ulong lu2, ulong *pluHi)
{
#if _WIN32 || _WIN64
#if I386_ASM
__asm
{
mov eax, lu1
mul lu2
mov ebx, pluHi
mov DWORD PTR[ebx], edx
}
#else //!I386_ASM
DWORDLONG llu = UInt32x32To64(lu1, lu2);
*pluHi = (ulong)(llu >> 32);
return (ulong)llu;
#endif //!I386_ASM
#else
#error Neither _WIN32, nor _WIN64 is defined
#endif
}
#pragma warning(pop)
/***************************************************************************
Add two unsigned longs and return the carry bit.
***************************************************************************/
int NumberUtilities::AddLu(ulong *plu1, ulong lu2)
{
*plu1 += lu2;
return *plu1 < lu2;
}
bool NumberUtilities::IsFinite(double value)
{
#if defined(_M_X64_OR_ARM64)
return 0 != (~(ToSpecial(value)) & 0x7FF0000000000000ull);
#else
return 0 != (~Js::NumberUtilities::LuHiDbl(value) & 0x7FF00000);
#endif
}
int NumberUtilities::CbitZeroLeft(ulong lu)
{
int cbit = 0;
if (0 == (lu & 0xFFFF0000))
{
cbit += 16;
lu <<= 16;
}
if (0 == (lu & 0xFF000000))
{
cbit += 8;
lu <<= 8;
}
if (0 == (lu & 0xF0000000))
{
cbit += 4;
lu <<= 4;
}
if (0 == (lu & 0xC0000000))
{
cbit += 2;
lu <<= 2;
}
if (0 == (lu & 0x80000000))
{
cbit += 1;
lu <<= 1;
}
Assert(lu & 0x80000000);
return cbit;
}
charcount_t NumberUtilities::UInt16ToString(uint16 integer, __out __ecount(outBufferSize) WCHAR* outBuffer, charcount_t outBufferSize, char widthForPaddingZerosInsteadSpaces)
{
// inlined here
WORD digit;
charcount_t cchWritten = 0;
Assert(cchWritten < outBufferSize);
// word is 0 to 65,535 -- 5 digits max
if (cchWritten < outBufferSize)
{
if (integer >= 10000)
{
digit = integer / 10000;
integer %= 10000;
*outBuffer = digit + L'0';
outBuffer++;
cchWritten++;
}
else if( widthForPaddingZerosInsteadSpaces > 4 )
{
*outBuffer = L'0';
outBuffer++;
cchWritten++;
}
}
Assert(cchWritten < outBufferSize);
if (cchWritten < outBufferSize)
{
if (integer >= 1000)
{
digit = integer / 1000;
integer %= 1000;
*outBuffer = digit + L'0';
outBuffer++;
cchWritten++;
}
else if( widthForPaddingZerosInsteadSpaces > 3 )
{
*outBuffer = L'0';
outBuffer++;
cchWritten++;
}
}
Assert(cchWritten < outBufferSize);
if (cchWritten < outBufferSize)
{
if (integer >= 100)
{
digit = integer / 100;
integer %= 100;
*outBuffer = digit + L'0';
outBuffer++;
cchWritten++;
}
else if( widthForPaddingZerosInsteadSpaces > 2 )
{
*outBuffer = L'0';
outBuffer++;
cchWritten++;
}
}
Assert(cchWritten < outBufferSize);
if (cchWritten < outBufferSize)
{
if (integer >= 10)
{
digit = integer / 10;
integer %= 10;
*outBuffer = digit + L'0';
outBuffer++;
cchWritten++;
}
else if( widthForPaddingZerosInsteadSpaces > 1 )
{
*outBuffer = L'0';
outBuffer++;
cchWritten++;
}
}
Assert(cchWritten < outBufferSize);
if (cchWritten < outBufferSize)
{
*outBuffer = integer + L'0';
outBuffer++;
cchWritten++;
}
Assert(cchWritten < outBufferSize);
if (cchWritten < outBufferSize)
{
// cchWritten doesn't include the terminating char, like swprintf_s
*outBuffer = 0;
}
return cchWritten;
}
BOOL NumberUtilities::TryConvertToUInt32(const wchar_t* str, int length, uint32* intVal)
{
if (length <= 0 || length > 10)
{
return false;
}
if (length == 1)
{
if (str[0] >= L'0' && str[0] <= L'9')
{
*intVal = (uint32)(str[0] - L'0');
return true;
}
else
{
return false;
}
}
if (str[0] < L'1' || str[0] > L'9')
{
return false;
}
uint32 val = (uint32)(str[0] - L'0');
int calcLen = min(length, 9);
for (int i = 1; i < calcLen; i++)
{
if ((str[i] < L'0')|| (str[i] > L'9'))
{
return false;
}
val = (val * 10) + (uint32)(str[i] - L'0');
}
if (length == 10)
{
// check for overflow 4294967295
if (str[9] < L'0' || str[9] > L'9' ||
UInt32Math::Mul(val, 10, &val) ||
UInt32Math::Add(val, (uint32)(str[9] - L'0'), &val))
{
return false;
}
}
*intVal = val;
return true;
}
double NumberUtilities::Modulus(double dblLeft, double dblRight)
{
double value = 0;
if (!Js::NumberUtilities::IsFinite(dblRight))
{
if (NumberUtilities::IsNan(dblRight) || !Js::NumberUtilities::IsFinite(dblLeft))
{
value = NumberConstants::NaN;
}
else
{
value = dblLeft;
}
}
else if (0 == dblRight || NumberUtilities::IsNan(dblLeft))
{
value = NumberConstants::NaN;
}
else if (0 == dblLeft)
{
value = dblLeft;
}
else
{
value = fmod(dblLeft, dblRight);
}
return value;
}
long NumberUtilities::LwFromDblNearest(double dbl)
{
if (Js::NumberUtilities::IsNan(dbl))
return 0;
if (dbl > 0x7FFFFFFFL)
return 0x7FFFFFFFL;
if (dbl < (long)0x80000000L)
return (long)0x80000000L;
return (long)dbl;
}
ulong NumberUtilities::LuFromDblNearest(double dbl)
{
if (Js::NumberUtilities::IsNan(dbl))
return 0;
if (dbl >(ulong)0xFFFFFFFFUL)
return (ulong)0xFFFFFFFFUL;
if (dbl < 0)
return 0;
return (ulong)dbl;
}
BOOL NumberUtilities::FDblIsLong(double dbl, long *plw)
{
AssertMem(plw);
double dblT;
*plw = (long)dbl;
dblT = (double)*plw;
return Js::NumberUtilities::LuHiDbl(dblT) == Js::NumberUtilities::LuHiDbl(dbl) && Js::NumberUtilities::LuLoDbl(dblT) == Js::NumberUtilities::LuLoDbl(dbl);
}
template<typename EncodedChar>
double NumberUtilities::DblFromHex(const EncodedChar *psz, const EncodedChar **ppchLim)
{
double dbl;
uint uT;
byte bExtra;
int cbit;
// Skip leading zeros.
while (*psz == '0')
psz++;
dbl = 0;
Assert(Js::NumberUtilities::LuHiDbl(dbl) == 0);
Assert(Js::NumberUtilities::LuLoDbl(dbl) == 0);
// Get the first digit.
if ((uT = *psz - '0') > 9)
{
if ((uT -= 'A' - '0') <= 5 || (uT -= 'a' - 'A') <= 5)
uT += 10;
else
{
*ppchLim = psz;
return dbl;
}
}
psz++;
if (uT & 0x08)
{
cbit = 4;
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)(uT & 0x07) << 17;
}
else if (uT & 0x04)
{
cbit = 3;
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)(uT & 0x03) << 18;
}
else if (uT & 0x02)
{
cbit = 2;
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)(uT & 0x01) << 19;
}
else
{
Assert(uT & 0x01);
cbit = 1;
}
bExtra = 0;
for (; ; psz++)
{
if ((uT = (*psz - '0')) > 9)
{
if ((uT -= 'A' - '0') <= 5 || (uT -= 'a' - 'A') <= 5)
uT += 10;
else
break;
}
if (cbit <= 17)
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)uT << (17 - cbit);
else if (cbit < 21)
{
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)uT >> (cbit - 17);
Js::NumberUtilities::LuLoDbl(dbl) |= (ulong)uT << (49 - cbit);
}
else if (cbit <= 49)
Js::NumberUtilities::LuLoDbl(dbl) |= (ulong)uT << (49 - cbit);
else if (cbit <= 53)
{
Js::NumberUtilities::LuLoDbl(dbl) |= (ulong)uT >> (cbit - 49);
bExtra = (byte)(uT << (57 - cbit));
}
else if (0 != uT)
bExtra |= 1;
cbit += 4;
}
// Set the lim.
*ppchLim = psz;
// Set the exponent.
cbit += 1022;
if (cbit > 2046)
{
// Overflow to Infinity
Js::NumberUtilities::LuHiDbl(dbl) = 0x7FF00000;
Js::NumberUtilities::LuLoDbl(dbl) = 0;
return dbl;
}
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)cbit << 20;
// Use bExtra to round.
if ((bExtra & 0x80) && ((bExtra & 0x7F) || (Js::NumberUtilities::LuLoDbl(dbl) & 1)))
{
// Round up. Note that this overflows the mantissa correctly,
// even to Infinity.
if (0 == ++Js::NumberUtilities::LuLoDbl(dbl))
++Js::NumberUtilities::LuHiDbl(dbl);
}
return dbl;
}
template <typename EncodedChar>
double NumberUtilities::DblFromBinary(const EncodedChar *psz, const EncodedChar **ppchLim)
{
double dbl = 0;
Assert(Js::NumberUtilities::LuHiDbl(dbl) == 0);
Assert(Js::NumberUtilities::LuLoDbl(dbl) == 0);
uint uT;
byte bExtra = 0;
int cbit = 0;
// Skip leading zeros.
while (*psz == '0')
psz++;
// Get the first digit.
uT = *psz - '0';
if (uT > 1)
{
*ppchLim = psz;
return dbl;
}
//Now that leading zeros are skipped first bit should be one so lets
//go ahead and count it and increment psz
cbit = 1;
psz++;
// According to the existing implementations these numbers
// should n bits away from 21 and 53. The n bits are determined by the
// numerical type. for example since 4 bits are necessary to represent a
// hexadecimal number and 3 bits to represent an octal you will see that
// the hex case is represented by 21-4 = 17 and the octal case is represented
// by 21-3 = 18, thus for binary where 1 bit is need to represent 2 numbers 21-1 = 20
const uint rightShiftValue = 20;
// Why 52? 52 is the last explicit bit and 1 bit away from 53 (max bits of precision
// for double precision floating point)
const uint leftShiftValue = 52;
for (; (uT = (*psz - '0')) <= 1; psz++)
{
if (cbit <= rightShiftValue)
{
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)uT << (rightShiftValue - cbit);
}
else if (cbit <= leftShiftValue)
{
Js::NumberUtilities::LuLoDbl(dbl) |= (ulong)uT << (leftShiftValue - cbit);
}
else if (cbit == leftShiftValue + 1)//53 bits
{
Js::NumberUtilities::LuLoDbl(dbl) |= (ulong)uT >> (cbit - leftShiftValue);
bExtra = (byte)(uT << (60 - cbit));
}
else if (0 != uT)
{
bExtra |= 1;
}
cbit++;
}
// Set the lim.
*ppchLim = psz;
// Set the exponent.
cbit += 1022;
if (cbit > 2046)
{
// Overflow to Infinity
Js::NumberUtilities::LuHiDbl(dbl) = 0x7FF00000;
Js::NumberUtilities::LuLoDbl(dbl) = 0;
return dbl;
}
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)cbit << 20;
// Use bExtra to round.
if ((bExtra & 0x80) && ((bExtra & 0x7F) || (Js::NumberUtilities::LuLoDbl(dbl) & 1)))
{
// Round up. Note that this overflows the mantissa correctly,
// even to Infinity.
if (0 == ++Js::NumberUtilities::LuLoDbl(dbl))
++Js::NumberUtilities::LuHiDbl(dbl);
}
return dbl;
}
template <typename EncodedChar>
double NumberUtilities::DblFromOctal(const EncodedChar *psz, const EncodedChar **ppchLim)
{
double dbl;
uint uT;
byte bExtra;
int cbit;
// Skip leading zeros.
while (*psz == '0')
psz++;
dbl = 0;
Assert(Js::NumberUtilities::LuHiDbl(dbl) == 0);
Assert(Js::NumberUtilities::LuLoDbl(dbl) == 0);
// Get the first digit.
uT = *psz - '0';
if (uT > 7)
{
*ppchLim = psz;
return dbl;
}
psz++;
if (uT & 0x04)//is the 3rd bit set
{
cbit = 3;
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)(uT & 0x03) << 18;
}
else if (uT & 0x02)//is the 2nd bit set
{
cbit = 2;
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)(uT & 0x01) << 19;
}
else// then is the first bit set
{
Assert(uT & 0x01);
cbit = 1;
}
bExtra = 0;
for (; (uT = (*psz - '0')) <= 7; psz++)
{
if (cbit <= 18)
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)uT << (18 - cbit);
else if (cbit < 21)
{
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)uT >> (cbit - 18);
Js::NumberUtilities::LuLoDbl(dbl) |= (ulong)uT << (50 - cbit);
}
else if (cbit <= 50)
Js::NumberUtilities::LuLoDbl(dbl) |= (ulong)uT << (50 - cbit);
else if (cbit <= 53)
{
Js::NumberUtilities::LuLoDbl(dbl) |= (ulong)uT >> (cbit - 50);
bExtra = (byte)(uT << (58 - cbit));
}
else if (0 != uT)
bExtra |= 1;
cbit += 3;
}
// Set the lim.
*ppchLim = psz;
// Set the exponent.
cbit += 1022;
if (cbit > 2046)
{
// Overflow to Infinity
Js::NumberUtilities::LuHiDbl(dbl) = 0x7FF00000;
Js::NumberUtilities::LuLoDbl(dbl) = 0;
return dbl;
}
Js::NumberUtilities::LuHiDbl(dbl) |= (ulong)cbit << 20;
// Use bExtra to round.
if ((bExtra & 0x80) && ((bExtra & 0x7F) || (Js::NumberUtilities::LuLoDbl(dbl) & 1)))
{
// Round up. Note that this overflows the mantissa correctly,
// even to Infinity.
if (0 == ++Js::NumberUtilities::LuLoDbl(dbl))
++Js::NumberUtilities::LuHiDbl(dbl);
}
return dbl;
}
template <typename EncodedChar>
double NumberUtilities::StrToDbl(const EncodedChar * psz, const EncodedChar **ppchLim, Js::ScriptContext *const scriptContext)
{
Assert(scriptContext);
bool likelyInt = true;
return Js::NumberUtilities::StrToDbl<EncodedChar>(psz, ppchLim, likelyInt);
}
template double NumberUtilities::StrToDbl<wchar_t>(const wchar_t * psz, const wchar_t **ppchLim, Js::ScriptContext *const scriptContext);
template double NumberUtilities::StrToDbl<utf8char_t>(const utf8char_t * psz, const utf8char_t **ppchLim, Js::ScriptContext *const scriptContext);
template double NumberUtilities::DblFromHex<wchar_t>(const wchar_t *psz, const wchar_t **ppchLim);
template double NumberUtilities::DblFromHex<utf8char_t>(const utf8char_t *psz, const utf8char_t **ppchLim);
template double NumberUtilities::DblFromBinary<wchar_t>(const wchar_t *psz, const wchar_t **ppchLim);
template double NumberUtilities::DblFromBinary<utf8char_t>(const utf8char_t *psz, const utf8char_t **ppchLim);
template double NumberUtilities::DblFromOctal<wchar_t>(const wchar_t *psz, const wchar_t **ppchLim);
template double NumberUtilities::DblFromOctal<utf8char_t>(const utf8char_t *psz, const utf8char_t **ppchLim);
}