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 | #include "avmplus.h" |
41 | #include "BigInteger.h" |
42 | |
43 | |
44 | #if (defined(_MSC_VER) || (defined(__GNUC__4) && defined(__SSE2__1))) && (defined(AVMPLUS_IA32) || defined(AVMPLUS_AMD64)) |
45 | #include <emmintrin.h> |
46 | #endif |
47 | |
48 | namespace avmplus |
49 | { |
50 | const double kLog2_10 = 0.30102999566398119521373889472449; |
51 | |
52 | |
53 | static const double kPowersOfTen[23] = { 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, |
54 | 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, |
55 | 1e21, 1e22 }; |
56 | |
57 | static inline double quickPowTen(int32_t exp) |
58 | { |
59 | if (exp < 23 && exp > 0) { |
60 | return kPowersOfTen[exp]; |
61 | } else { |
62 | return MathUtils::pow(10,exp); |
63 | } |
64 | } |
65 | |
66 | |
67 | |
68 | static int32_t skipSpaces(const StringIndexer& s, int32_t index) |
69 | { |
70 | while (index < s->length()) |
71 | { |
72 | uint32_t ch = s[index]; |
73 | if (!(ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\f' || ch == '\v' || |
74 | (ch >= 0x2000 && ch <=0x200b) || ch == 0x2028 || ch == 0x2029 || ch == 0x205f || ch == 0x3000 )) |
75 | break; |
76 | index++; |
77 | } |
78 | return index; |
79 | } |
80 | |
81 | |
82 | |
83 | |
84 | int32_t handleSign(const StringIndexer& s, int32_t index, boolbool& negative) |
85 | { |
86 | negative = falsefalse; |
87 | if (index >= s->length()) |
88 | return index; |
89 | uint32_t ch = s[index]; |
90 | if (ch == '+') { |
91 | index++; |
92 | } else if (ch == '-') { |
93 | negative = truetrue; |
94 | index++; |
95 | } |
96 | return index; |
97 | } |
98 | |
99 | boolbool MathUtils::equals(double x, double y) |
100 | { |
101 | |
102 | if (isInfinite(x) != isInfinite(y)) { |
103 | return falsefalse; |
104 | } |
105 | |
106 | if (isNaN(x) || isNaN(y)) { |
107 | return falsefalse; |
108 | } |
109 | return x == y; |
110 | } |
111 | |
112 | static double _nan() |
113 | { |
114 | union { float f; uint32_t d; }; d = 0x7FFFFFFF; |
115 | return f; |
116 | } |
117 | |
118 | static double _infinity() |
119 | { |
120 | union { float f; uint32_t d; }; d = 0x7F800000; |
121 | return f; |
122 | } |
123 | |
124 | static double _neg_infinity() |
125 | { |
126 | union { float f; uint32_t d; }; d = 0xFF800000; |
127 | return f; |
128 | } |
129 | |
130 | |
131 | const double MathUtils::kNaN = _nan(); |
132 | const double MathUtils::kInfinity = _infinity(); |
133 | const double MathUtils::kNegInfinity = _neg_infinity(); |
134 | |
135 | #ifdef UNIX |
136 | |
137 | |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | int32_t MathUtils::isInfinite(double x) |
149 | { |
150 | double_overlay u(x); |
151 | |
152 | int32_t hx = u.words.msw; |
153 | int32_t lx = u.words.lsw; |
154 | |
155 | lx |= (hx & 0x7FFFFFFF) ^ 0x7FF00000; |
156 | lx |= -lx; |
157 | return ~(lx >> 31) & (hx >> 30); |
158 | } |
159 | |
160 | boolbool MathUtils::isNaN(double x) |
161 | { |
162 | |
163 | |
164 | if (MathUtils::isInfinite(x) != 0) { |
165 | return falsefalse; |
166 | } |
167 | |
168 | double_overlay u(x); |
169 | |
170 | int32_t hx = u.words.msw; |
171 | int32_t lx = u.words.lsw; |
172 | |
173 | hx &= 0x7FFFFFFF; |
174 | hx |= (uint32_t) (lx | (-lx)) >> 31; |
175 | hx = 0x7FF00000 - hx; |
176 | return (boolbool) (((uint32_t)(hx) >> 31) != 0); |
177 | } |
178 | |
179 | boolbool MathUtils::isNegZero(double x) |
180 | { |
181 | double_overlay u(x); |
182 | |
183 | int32_t hx = u.words.msw; |
184 | int32_t lx = u.words.lsw; |
185 | |
186 | return (hx == (int32_t)0x80000000 && lx == (int32_t)0x0); |
187 | } |
188 | |
189 | #else |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | int32_t MathUtils::isInfinite(double x) |
202 | { |
203 | double_overlay u(x); |
204 | |
205 | if ((u.bits64 & 0x7fffffffffffffffLL) != 0x7FF0000000000000LL) |
206 | return 0; |
207 | if (u.bits64 & 0x8000000000000000LL) |
208 | return -1; |
209 | else |
210 | return 1; |
211 | } |
212 | |
213 | boolbool MathUtils::isNaN(double value) |
214 | { |
215 | double_overlay u(value); |
216 | |
217 | return (u.bits64 & ~0x8000000000000000ULL) > 0x7ff0000000000000ULL; |
218 | } |
219 | |
220 | boolbool MathUtils::isNegZero(double x) |
221 | { |
222 | double_overlay d(x); |
223 | |
224 | return d.bits64 == 0x8000000000000000ULL; |
225 | } |
226 | |
227 | #endif // UNIX |
228 | |
229 | int32_t MathUtils::nextPowerOfTwo(int32_t n) |
230 | { |
231 | int32_t i = 2; |
232 | while (i < n) |
233 | { |
234 | i <<= 1; |
235 | } |
236 | |
237 | return (i); |
238 | } |
239 | |
240 | static boolbool isHexNumber(const StringIndexer& s, int32_t index) |
241 | { |
242 | if (s->length() - index < 2 || s[index] != '0') |
243 | return falsefalse; |
244 | uint32_t ch = s[index+1]; |
245 | return (ch == 'x' || ch == 'X'); |
246 | } |
247 | |
248 | |
249 | |
250 | |
251 | static int32_t parseIntDigit(wchar ch) |
252 | { |
253 | if (ch >= '0' && ch <= '9') { |
254 | return (ch - '0'); |
255 | } else if (ch >= 'a' && ch <= 'z') { |
256 | return (ch - 'a' + 10); |
257 | } else if (ch >= 'A' && ch <= 'Z') { |
258 | return (ch - 'A' + 10); |
259 | } else { |
260 | return -1; |
261 | } |
262 | } |
263 | |
264 | |
265 | double MathUtils::parseInt(Stringp inStr, int32_t radix , boolbool strict ) |
266 | { |
267 | boolbool negate; |
268 | boolbool gotDigits = falsefalse; |
269 | double result = 0; |
270 | |
271 | StringIndexer s(inStr); |
272 | int32_t index = skipSpaces(s, 0); |
273 | index = handleSign(s, index, negate); |
274 | if (isHexNumber(s, index) && (radix == 16 || radix == 0) ) { |
275 | index += 2; |
276 | if (radix == 0) |
277 | radix = 16; |
278 | } else if (radix == 0) { |
279 | |
280 | radix = 10; |
281 | } |
282 | |
283 | |
284 | if (radix >= 2 && radix <= 36 && index < s->length()) { |
285 | result = 0; |
286 | int32_t start = index; |
287 | |
288 | |
289 | while (index < s->length()) { |
290 | int32_t v = parseIntDigit(s[index]); |
291 | if (v == -1 || v >= radix) { |
292 | break; |
293 | } |
294 | result = result * radix + v; |
295 | gotDigits = truetrue; |
296 | index++; |
297 | } |
298 | |
299 | index = skipSpaces(s, index); |
300 | if (strict && index < s->length()) { |
301 | return MathUtils::kNaN; |
302 | } |
303 | |
304 | if ( result >= 0x20000000000000LL && |
305 | (radix == 2 || radix == 4 || radix == 8 || radix == 16 || radix == 32) ) { |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | result = 0; |
312 | |
313 | int32_t powOf2 = 1; |
314 | for(int32_t x = radix; (x != 1); x >>= 1) |
315 | powOf2++; |
316 | powOf2--; |
317 | index = start; |
318 | int32_t v=0; |
319 | |
320 | int32_t end,next; |
321 | |
322 | for(end=index; end < s->length() && s[end] == '0'; end++) |
323 | ; |
324 | if (end >= s->length()) |
325 | return 0; |
326 | |
327 | for (next=0; next*powOf2 <= 52; next++) { |
328 | v = parseIntDigit(s[end++]); |
329 | if (v == -1 || v >= radix) { |
330 | v = 0; |
331 | break; |
332 | } |
333 | result = result * radix + v; |
334 | if (end >= s->length()) |
335 | break; |
336 | } |
337 | if (next*powOf2 > 52) { |
338 | boolbool roundUp = falsefalse; |
339 | int32_t bit53 = 0; |
340 | int32_t bit54 = 0; |
341 | |
342 | double factor = 1; |
343 | |
344 | switch(radix) { |
345 | case 32: |
346 | bit53 = v & (1 << 2); |
347 | bit54 = v & (1 << 1); |
348 | roundUp = (v & 1); |
349 | break; |
350 | case 16: |
351 | bit53 = v & (1 << 0); |
352 | v = parseIntDigit(s[end]); |
353 | if (v != -1 && v < radix) { |
354 | factor *= radix; |
355 | bit54 = v & (1 << 3); |
356 | roundUp = (v & 0x3) != 0; |
357 | } else { |
358 | roundUp = bit53 != 0; |
359 | } |
360 | break; |
361 | case 8: |
362 | v = parseIntDigit(s[end]); |
363 | if (v == -1 || v >= radix) { |
364 | v = 0; |
365 | } |
366 | factor *= radix; |
367 | bit53 = v & (1 << 1); |
368 | bit54 = v & (1 << 0); |
369 | break; |
370 | case 4: |
371 | v = parseIntDigit(s[end]); |
372 | if (v == -1 || v >= radix) { |
373 | v = 0; |
374 | } |
375 | factor *= radix; |
376 | bit53 = v & (1 << 1); |
377 | bit54 = v & (1 << 0); |
378 | break; |
379 | case 2: |
380 | |
381 | |
382 | |
383 | |
384 | bit53 = v & (1 << 0); |
385 | v = parseIntDigit(s[end]); |
386 | if (v != -1 && v < radix) { |
387 | factor *= radix; |
388 | bit54 = (v != -1 ? (v & (1 << 0)) : 0); |
389 | } |
390 | |
391 | break; |
392 | } |
393 | |
394 | bit53 = !!bit53; |
395 | bit54 = !!bit54; |
396 | |
397 | |
398 | while(++end < s->length()) { |
399 | v = parseIntDigit(s[end]); |
400 | if (v == -1 || v >= radix) { |
401 | break; |
402 | } |
403 | roundUp |= (v != 0); |
404 | factor *= radix; |
405 | } |
406 | roundUp = bit54 && (bit53 || roundUp); |
407 | result += (roundUp ? 1.0 : 0.0); |
408 | result *= factor; |
409 | } |
410 | |
411 | } |
412 | |
413 | |
414 | |
415 | |
416 | |
417 | if (negate) { |
418 | result = -result; |
419 | } |
420 | } |
421 | return gotDigits ? result : MathUtils::kNaN; |
422 | } |
423 | |
424 | |
425 | double MathUtils::convertStringToNumber(Stringp inStr) |
426 | { |
427 | double value; |
428 | |
429 | if (inStr->length() == 0) { |
430 | value = 0; |
431 | } else if (MathUtils::convertStringToDouble(inStr, &value, truetrue)) { |
432 | |
433 | } else { |
434 | value = MathUtils::parseInt(inStr, 0,truetrue); |
435 | } |
436 | return value; |
437 | } |
438 | |
439 | |
440 | |
441 | REALLY_INLINEinline __attribute__((always_inline)) int32_t real_to_int(double value) |
442 | { |
443 | #if defined(WIN32) && defined(AVMPLUS_AMD64) |
444 | int32_t intValue = _mm_cvttsd_si32(_mm_set_sd(value)); |
445 | #elif defined(WIN32) && defined(AVMPLUS_IA32) |
446 | int32_t intValue; |
447 | _asm fld [value]; |
448 | _asm fistp [intValue]; |
449 | #elif defined(_MAC1) && (defined(AVMPLUS_IA32) || defined(AVMPLUS_AMD64)) |
450 | int32_t intValue = _mm_cvttsd_si32(_mm_set_sd(value)); |
451 | #else |
452 | int32_t intValue = int32_t(value); |
453 | #endif |
454 | return intValue; |
455 | } |
456 | |
457 | |
458 | |
459 | double MathUtils::toInt(double value) |
460 | { |
461 | int32_t intValue = real_to_int(value); |
462 | |
463 | if ((value == (double)(intValue)) && ((uint32_t)intValue != 0x80000000)) |
464 | return value; |
465 | |
466 | if (MathUtils::isNaN(value)) { |
467 | return 0; |
468 | } |
469 | if (MathUtils::isInfinite(value) || value == 0) { |
470 | return value; |
471 | } |
472 | if (value < 0) { |
473 | return -MathUtils::floor(-value); |
474 | } else { |
475 | return MathUtils::floor(value); |
476 | } |
477 | } |
478 | |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | double MathUtils::powerOfTen(int32_t exponent, double value) |
485 | { |
486 | double base = 10.0; |
487 | |
488 | if (exponent < 0) { |
489 | exponent = -exponent; |
490 | while (exponent) { |
491 | if (exponent & 1) { |
492 | value /= base; |
493 | } |
494 | exponent >>= 1; |
495 | base *= base; |
496 | } |
497 | } else { |
498 | while (exponent) { |
499 | if (exponent & 1) { |
500 | value *= base; |
501 | } |
502 | exponent >>= 1; |
503 | base *= base; |
504 | } |
505 | } |
506 | return value; |
507 | } |
508 | |
509 | |
510 | |
511 | |
512 | |
513 | |
514 | static double powInt(double base, int32_t exponent) |
515 | { |
516 | double value = 1.0; |
517 | double original_base = base; |
518 | int32_t original_exponent = exponent; |
519 | |
520 | if (MathUtils::isInfinite(base)) |
521 | { |
522 | if (exponent < 0) |
523 | return (base < 0) ? 1.0/base : 0.0; |
524 | |
525 | |
526 | if (base < 0) |
527 | return (MathUtils::mod(exponent, 2) != 0) ? base : 0-base; |
528 | |
529 | return base; |
530 | } |
531 | |
532 | if (exponent < 0) { |
533 | exponent = -exponent; |
534 | while (exponent) { |
535 | if (exponent & 1) { |
536 | value /= base; |
537 | |
538 | |
539 | |
540 | |
541 | if (value == 0 && base != 0) |
542 | return MathUtils::powInternal(original_base,(double)original_exponent); |
543 | } |
544 | exponent >>= 1; |
545 | base *= base; |
546 | } |
547 | } else { |
548 | while (exponent) { |
549 | if (exponent & 1) { |
550 | value *= base; |
551 | } |
552 | exponent >>= 1; |
553 | base *= base; |
554 | } |
555 | } |
556 | return value; |
557 | } |
558 | |
559 | |
560 | double MathUtils::pow(double x, double y) |
561 | { |
562 | if (MathUtils::isNaN(y)) { |
563 | |
564 | return MathUtils::kNaN; |
565 | } |
566 | |
567 | if (y == 0) { |
568 | |
569 | return 1; |
570 | } |
571 | |
572 | int32_t infy = MathUtils::isInfinite(y); |
573 | |
574 | if (infy == 0 && y == (int32_t)y) { |
575 | |
576 | |
577 | |
578 | return powInt(x, (int32_t)y); |
579 | } |
580 | |
581 | double absx = MathUtils::abs(x); |
582 | |
583 | if (absx < 1) { |
584 | infy = -infy; |
585 | } |
586 | if (absx == 1 && infy != 0) { |
587 | |
588 | return MathUtils::kNaN; |
589 | } |
590 | if (infy == 1) { |
591 | |
592 | return MathUtils::kInfinity; |
593 | } else if (infy == -1) { |
594 | |
595 | return +0; |
596 | } |
597 | |
598 | if (MathUtils::isInfinite(x)) |
599 | { |
600 | if (y < 0) { |
601 | |
602 | |
603 | |
604 | return 0; |
605 | } else { |
606 | if (y < 1.0) { |
607 | return MathUtils::kInfinity; |
608 | } else { |
609 | |
610 | |
611 | |
612 | return x; |
613 | } |
614 | } |
615 | } |
616 | |
617 | |
618 | |
619 | if (x < 0.0) { |
620 | |
621 | if (y != MathUtils::floor(y)) { |
622 | return MathUtils::kNaN; |
623 | } |
624 | |
625 | x = -x; |
626 | |
627 | if (MathUtils::mod(y, 2) != 0) { |
628 | return -powInternal(x, y); |
629 | } |
630 | |
631 | } |
632 | |
633 | |
634 | |
635 | if (x == 0.0) |
636 | { |
637 | if (y < 0.0) |
638 | return MathUtils::kInfinity; |
639 | else |
640 | return 0.0; |
641 | } |
642 | |
643 | return powInternal(x, y); |
644 | } |
645 | |
646 | int32_t MathUtils::nextDigit(double *value) |
647 | { |
648 | int32_t digit; |
649 | |
650 | #if defined(WIN32) && defined(AVMPLUS_AMD64) |
651 | digit = _mm_cvttsd_si32(_mm_set_sd(*value)); |
652 | #elif defined(WIN32) && defined(AVMPLUS_IA32) |
653 | |
654 | |
655 | _asm mov eax,[value]; |
656 | _asm fld qword ptr [eax]; |
657 | _asm fistp [digit]; |
658 | #elif defined(_MAC1) && (defined(AVMPLUS_IA32) || defined(AVMPLUS_AMD64)) |
659 | digit = _mm_cvttsd_si32(_mm_set_sd(*value)); |
660 | #else |
661 | digit = (int32_t) *value; |
662 | #endif |
663 | |
664 | *value -= digit; |
665 | *value *= 10; |
666 | return digit; |
667 | } |
668 | |
669 | int32_t MathUtils::roundInt(double x) |
670 | { |
671 | if (x < 0) { |
672 | return (int32_t) (x - 0.5); |
673 | } else { |
674 | return (int32_t) (x + 0.5); |
675 | } |
676 | } |
677 | |
678 | Stringp MathUtils::convertIntegerToStringRadix(AvmCore* core, |
679 | intptr_t value, |
680 | int32_t radix, |
681 | UnsignedTreatment treatAs) |
682 | { |
683 | MMgc::GC::AllocaAutoPtr _buffer; |
684 | char* buffer = (char*)VMPI_alloca(core, _buffer, kMinSizeForInt64_t_toString)(kMinSizeForInt64_t_toString > 4000 ? core->gc->allocaPush (kMinSizeForInt64_t_toString, _buffer) : __builtin_alloca(kMinSizeForInt64_t_toString )); |
685 | int32_t len = kMinSizeForInt64_t_toString; |
686 | char* p = convertIntegerToStringBuffer(value, buffer, len, radix, treatAs); |
687 | return core->newStringLatin1(p, len); |
688 | } |
689 | |
690 | Stringp MathUtils::convertIntegerToStringBase10(AvmCore* core, |
691 | int32_t value, |
692 | UnsignedTreatment treatAs) |
693 | { |
694 | char buffer[kMinSizeForInt32_t_base10_toString]; |
695 | int32_t len = kMinSizeForInt32_t_base10_toString; |
696 | #ifdef AVMPLUS_64BIT |
697 | intptr_t wideVal = treatAs == kTreatAsUnsigned ? (intptr_t)(uint32_t)value : (intptr_t)value; |
698 | #else |
699 | intptr_t wideVal = (intptr_t) value; |
700 | #endif |
701 | char* p = convertIntegerToStringBuffer(wideVal, buffer, len, 10, treatAs); |
702 | return core->newStringLatin1(p, len); |
703 | } |
704 | |
705 | char* MathUtils::convertIntegerToStringBuffer(intptr_t value, |
706 | char *buffer, |
707 | int32_t& len, |
708 | int32_t radix, |
709 | UnsignedTreatment treatAs) |
710 | { |
711 | #ifndef AVMPLUS_64BIT |
712 | |
713 | |
714 | |
715 | if ( (uint32_t)value == 0x80000000 && treatAs == kTreatAsSigned) |
716 | |
717 | { |
718 | AvmAssert(len >= 12)do { } while (0); |
719 | if (len < 12) |
720 | return NULL__null; |
721 | VMPI_memcpy::memcpy (buffer, "-2147483648", 12); |
722 | len = 11; |
723 | return buffer; |
724 | } |
725 | #endif |
726 | |
727 | if (radix < 2 || radix > 36) |
728 | { |
729 | AvmAssert( 0 )do { } while (0); |
730 | return NULL__null; |
731 | } |
732 | |
733 | char *src = buffer + len - 1; |
734 | char *srcEnd = src; |
735 | |
736 | *src-- = '\0'; |
737 | |
738 | if (value == 0) |
739 | { |
740 | *src-- = '0'; |
741 | } |
742 | else |
743 | { |
744 | uintptr_t uVal; |
745 | boolbool negative=falsefalse; |
746 | |
747 | if (treatAs == kTreatAsUnsigned) |
748 | { |
749 | uVal = (uintptr_t)value; |
750 | } |
751 | else |
752 | { |
753 | negative = (value < 0); |
754 | if (negative) |
755 | value = -value; |
756 | uVal = (uintptr_t)value; |
757 | } |
758 | |
759 | while (uVal != 0) |
760 | { |
761 | |
762 | AvmAssert(src >= buffer)do { } while (0); |
763 | uintptr_t j = uVal; |
764 | uVal = uVal / radix; |
765 | j -= (uVal * radix); |
766 | |
767 | *src-- = (char)((j < 10) ? (j + '0') : (j + ('a' - 10))); |
768 | } |
769 | |
770 | if (negative) |
771 | { |
772 | AvmAssert(src >= buffer)do { } while (0); |
773 | if (src < buffer) |
774 | |
775 | return NULL__null; |
776 | *src-- = '-'; |
777 | } |
778 | } |
779 | |
780 | src++; |
781 | len = (int32_t)(srcEnd-src); |
782 | |
783 | return src; |
784 | } |
785 | |
786 | Stringp MathUtils::convertDoubleToStringRadix(AvmCore *core, |
787 | double value, |
788 | int32_t radix) |
789 | { |
790 | if (radix < 2 || radix > 36) |
791 | { |
792 | AvmAssert( 0 )do { } while (0); |
793 | return NULL__null; |
794 | } |
795 | |
796 | MMgc::GC::AllocaAutoPtr _tmp; |
797 | char* tmp = (char*)VMPI_alloca(core, _tmp, kMinSizeForDouble_base2_toString)(kMinSizeForDouble_base2_toString > 4000 ? core->gc-> allocaPush(kMinSizeForDouble_base2_toString, _tmp) : __builtin_alloca (kMinSizeForDouble_base2_toString)); |
798 | char *src = tmp + kMinSizeForDouble_base2_toString - 1; |
799 | char *srcEnd = src; |
800 | |
801 | boolbool negative=falsefalse; |
802 | |
803 | negative = (value < 0); |
804 | if (negative) |
805 | value = -value; |
806 | |
807 | if (value < 1) |
808 | { |
809 | *src-- = '0'; |
810 | } |
811 | else |
812 | { |
813 | double uVal = MathUtils::floor(value); |
814 | |
815 | while (uVal != 0) |
816 | { |
817 | double j = uVal; |
818 | uVal = MathUtils::floor(uVal / radix); |
819 | j -= (uVal * radix); |
820 | |
821 | *src-- = (char)((j < 10) ? ((int32_t)j + '0') : ((int32_t)j + ('a' - 10))); |
822 | |
823 | AvmAssert(src >= tmp || 0 == uVal)do { } while (0); |
824 | } |
825 | |
826 | if (negative) |
827 | *src-- = '-'; |
828 | } |
829 | |
830 | int32_t len = (int32_t)(srcEnd-src); |
831 | return core->newStringLatin1(src+1, len); |
832 | } |
833 | |
834 | Stringp MathUtils::convertDoubleToString(AvmCore* core, |
835 | double value, |
836 | int32_t mode, |
837 | int32_t precision) |
838 | { |
839 | switch (isInfinite(value)) { |
| 1 | 'Default' branch taken. Execution continues on line 845 |
|
840 | case -1: |
841 | return core->newConstantStringLatin1("-Infinity"); |
842 | case 1: |
843 | return core->newConstantStringLatin1("Infinity"); |
844 | } |
845 | if (isNaN(value)) { |
| |
846 | return core->newConstantStringLatin1("NaN"); |
847 | } |
848 | |
849 | |
850 | |
851 | |
852 | if (mode == DTOSTR_NORMAL) { |
| |
853 | #if defined(WIN32) && defined(AVMPLUS_AMD64) |
854 | int32_t intValue = _mm_cvttsd_si32(_mm_set_sd(value)); |
855 | #elif defined(WIN32) && defined(AVMPLUS_IA32) |
856 | int32_t intValue; |
857 | _asm fld [value]; |
858 | _asm fistp [intValue]; |
859 | #elif defined(_MAC1) && (defined(AVMPLUS_IA32) || defined(AVMPLUS_AMD64)) |
860 | int32_t intValue = _mm_cvttsd_si32(_mm_set_sd(value)); |
861 | #else |
862 | int32_t intValue = int32_t(value); |
863 | #endif |
864 | if ((value == (double)(intValue)) && ((uint32_t)intValue != 0x80000000)) |
865 | return convertIntegerToStringBase10(core, intValue, kTreatAsSigned); |
866 | } |
867 | |
868 | int32_t i, len = 0; |
869 | |
870 | MMgc::GC::AllocaAutoPtr _buffer; |
871 | char* buffer = (char*)VMPI_alloca(core, _buffer, kMinSizeForDouble_base10_toString)(kMinSizeForDouble_base10_toString > 4000 ? core->gc-> allocaPush(kMinSizeForDouble_base10_toString, _buffer) : __builtin_alloca (kMinSizeForDouble_base10_toString)); |
872 | char *s = buffer; |
873 | boolbool negative = falsefalse; |
874 | |
875 | |
876 | if (value < 0) { |
| |
877 | value = -value; |
878 | negative = truetrue; |
879 | |
880 | s++; |
881 | } |
882 | |
883 | |
884 | D2A *d2a = mmfx_new(D2A(value, mode, precision))new (MMgc::kUseFixedMalloc) D2A(value, mode, precision); |
885 | int32_t exp10 = d2a->expBase10()-1; |
886 | |
887 | |
888 | char *sentinel = s; |
889 | |
890 | enum { |
891 | kNormal, |
892 | kExponential, |
893 | kFraction, |
894 | kFixedFraction |
895 | }; |
896 | int32_t format; |
897 | int32_t digit; |
898 | |
899 | switch (mode) { |
| 5 | Control jumps to 'case DTOSTR_EXPONENTIAL:' at line 921 |
|
900 | case DTOSTR_FIXED: |
901 | { |
902 | if (exp10 < 0) { |
903 | format = kFixedFraction; |
904 | } else { |
905 | format = kNormal; |
906 | precision++; |
907 | } |
908 | } |
909 | break; |
910 | case DTOSTR_PRECISION: |
911 | { |
912 | if (exp10 < 0) { |
913 | format = kFraction; |
914 | } else if (exp10 >= precision) { |
915 | format = kExponential; |
916 | } else { |
917 | format = kNormal; |
918 | } |
919 | } |
920 | break; |
921 | case DTOSTR_EXPONENTIAL: |
922 | format = kExponential; |
923 | precision++; |
924 | break; |
| 6 | Execution continues on line 951 |
|
925 | default: |
926 | if (exp10 < 0 && exp10 > -7) { |
927 | |
928 | if (exp10 < -precision) { |
929 | exp10 = -precision-1; |
930 | } |
931 | format = kFraction; |
932 | } else if (exp10 > 20) { |
933 | format = kExponential; |
934 | } else { |
935 | format = kNormal; |
936 | } |
937 | } |
938 | |
939 | #if 0 // ifdef WIN32 |
940 | |
941 | |
942 | volatile uint16_t oldcw; |
943 | volatile uint16_t newcw; |
944 | _asm fnstcw [oldcw]; |
945 | _asm mov ax,[oldcw]; |
946 | _asm or ax,0xc3f; |
947 | _asm mov [newcw],ax; |
948 | _asm fldcw [newcw]; |
949 | #endif |
950 | |
951 | boolbool wroteDecimal = falsefalse; |
952 | switch (format) { |
| 7 | Control jumps to 'case kExponential:' at line 1054 |
|
953 | case kNormal: |
954 | { |
955 | int32_t digits = 0; |
956 | sentinel = s; |
957 | *s++ = '0'; |
958 | digit = d2a->nextDigit(); |
959 | if (digit > 0) { |
960 | *s++ = (char)(digit + '0'); |
961 | } |
962 | while (exp10 > 0) { |
963 | digit = (d2a->finished) ? 0 : d2a->nextDigit(); |
964 | *s++ = (char)(digit + '0'); |
965 | exp10--; |
966 | digits++; |
967 | } |
968 | if (mode == DTOSTR_FIXED) { |
969 | digits = 0; |
970 | } |
971 | if (mode == DTOSTR_NORMAL) { |
972 | if (!d2a->finished) { |
973 | *s++ = '.'; |
974 | wroteDecimal = truetrue; |
975 | while( !d2a->finished ) { |
976 | *s++ = (char)(d2a->nextDigit() + '0'); |
977 | } |
978 | } |
979 | } |
980 | else if (digits < precision-1) |
981 | { |
982 | *s++ = '.'; |
983 | wroteDecimal = truetrue; |
984 | for (; digits < precision-1; digits++) { |
985 | digit = d2a->finished ? 0 : d2a->nextDigit(); |
986 | *s++ = (char)(digit + '0'); |
987 | } |
988 | } |
989 | } |
990 | break; |
991 | case kFixedFraction: |
992 | { |
993 | *s++ = '0'; |
994 | *s++ = '0'; |
995 | *s++ = '.'; |
996 | wroteDecimal = truetrue; |
997 | |
998 | int32_t digits = 0; |
999 | if (exp10 > 0) { |
1000 | while (++exp10 < 10 && digits < precision) { |
1001 | *s++ = '0'; |
1002 | digits++; |
1003 | } |
1004 | } else if (exp10 < 0) { |
1005 | while ((++exp10 < 0) && (precision-- > 0)) |
1006 | *s++ = '0'; |
1007 | } |
1008 | |
1009 | |
1010 | for ( ; digits<precision; digits++) { |
1011 | if (d2a->finished) |
1012 | { |
1013 | if (mode == DTOSTR_NORMAL) |
1014 | break; |
1015 | *s++ = '0'; |
1016 | } else { |
1017 | *s++ = (char)(d2a->nextDigit() + '0'); |
1018 | } |
1019 | } |
1020 | exp10 = 0; |
1021 | } |
1022 | break; |
1023 | case kFraction: |
1024 | { |
1025 | *s++ = '0'; |
1026 | *s++ = '0'; |
1027 | *s++ = '.'; |
1028 | wroteDecimal = truetrue; |
1029 | |
1030 | |
1031 | if (value) |
1032 | { |
1033 | for (i=exp10; i<-1; i++) { |
1034 | *s++ = '0'; |
1035 | } |
1036 | } |
1037 | |
1038 | |
1039 | i=0; |
1040 | while (!d2a->finished) |
1041 | { |
1042 | *s++ = (char)(d2a->nextDigit() + '0'); |
1043 | if (mode != DTOSTR_NORMAL && ++i >= precision) |
1044 | break; |
1045 | } |
1046 | if (mode == DTOSTR_PRECISION) |
1047 | { |
1048 | while(i++ < precision) |
1049 | *s++ = (char)(d2a->finished ? '0' : d2a->nextDigit() + '0'); |
1050 | } |
1051 | exp10 = 0; |
1052 | } |
1053 | break; |
1054 | case kExponential: |
1055 | { |
1056 | digit = d2a->finished ? 0 : d2a->nextDigit(); |
| |
1057 | *s++ = (char)(digit + '0'); |
1058 | if ( ((mode == DTOSTR_NORMAL) && !d2a->finished) || |
| |
1059 | ((mode != DTOSTR_NORMAL) && precision > 1) ) { |
1060 | *s++ = '.'; |
1061 | wroteDecimal = truetrue; |
1062 | for (i=0; i<precision-1; i++) { |
1063 | if (d2a->finished) |
1064 | { |
1065 | if (mode == DTOSTR_NORMAL) |
1066 | break; |
1067 | *s++ = '0'; |
1068 | } else { |
1069 | *s++ = (char)(d2a->nextDigit() + '0'); |
1070 | } |
1071 | } |
1072 | } |
1073 | } |
1074 | break; |
| 10 | Execution continues on line 1079 |
|
1075 | } |
1076 | |
1077 | |
1078 | |
1079 | if (d2a->bFastEstimateOk || mode == DTOSTR_FIXED || mode == DTOSTR_PRECISION) |
| |
1080 | { |
1081 | i = d2a->nextDigit(); |
1082 | if (i > 4) { |
1083 | char *ptr = s-1; |
1084 | while (ptr >= buffer) { |
1085 | if (*ptr < '0') { |
1086 | ptr--; |
1087 | continue; |
1088 | } |
1089 | (*ptr)++; |
1090 | if (*ptr != 0x3A) { |
1091 | break; |
1092 | } |
1093 | *ptr-- = '0'; |
1094 | } |
1095 | } |
1096 | if (mode == MathUtils::DTOSTR_NORMAL && wroteDecimal) { |
1097 | |
1098 | while (*(s-1) == '0') { |
1099 | s--; |
1100 | } |
1101 | if (*(s-1) == '.') { |
1102 | s--; |
1103 | } |
1104 | } |
1105 | } |
1106 | |
1107 | if (exp10) { |
| |
1108 | char *firstNonZero = buffer + int(negative); |
1109 | while (firstNonZero < s && *firstNonZero == '0') { |
1110 | firstNonZero++; |
1111 | } |
1112 | if (s == firstNonZero) { |
1113 | |
1114 | |
1115 | *s++ = '1'; |
1116 | exp10++; |
1117 | } else { |
1118 | char *lastNonZero = s; |
1119 | while (lastNonZero > firstNonZero) { |
1120 | if (*--lastNonZero != '0') { |
1121 | break; |
1122 | } |
1123 | } |
1124 | if (value && (firstNonZero == lastNonZero) ) { |
1125 | |
1126 | |
1127 | exp10 += (int32_t) (s - firstNonZero - 1); |
1128 | s = lastNonZero+1; |
1129 | } |
1130 | } |
1131 | *s++ = 'e'; |
1132 | if (exp10 > 0) { |
1133 | *s++ = '+'; |
1134 | } |
1135 | char expstr[kMinSizeForInt32_t_base10_toString]; |
1136 | len = kMinSizeForInt32_t_base10_toString; |
1137 | char* t = convertIntegerToStringBuffer(exp10, expstr, len, 10, kTreatAsSigned); |
1138 | while (*t) { *s++ = *t++; } |
1139 | } |
1140 | |
1141 | len = (int32_t)(s-buffer); |
1142 | s = buffer; |
1143 | if (negative) |
| |
1144 | s++; |
1145 | if (sentinel && sentinel[0] == '0' && sentinel[1] != '.') { |
| 14 | The left operand of '!=' is a garbage value |
|
1146 | s = sentinel + 1; |
1147 | len--; |
1148 | } |
1149 | if (negative) |
1150 | *--s = '-'; |
1151 | |
1152 | #if 0 // def WIN32 |
1153 | |
1154 | _asm fldcw [oldcw]; |
1155 | #endif |
1156 | |
1157 | mmfx_delete(d2a)::MMgcDestructTaggedScalarChecked(d2a); |
1158 | |
1159 | return core->newStringLatin1(s, len); |
1160 | } |
1161 | |
1162 | |
1163 | |
1164 | |
1165 | |
1166 | boolbool MathUtils::convertStringToDouble(Stringp inStr, |
1167 | double *value, |
1168 | boolbool strict ) |
1169 | { |
1170 | boolbool negate; |
1171 | int32_t numDigits = 0; |
1172 | int32_t exp10 = 0; |
1173 | double result = 0; |
1174 | uint32_t ch = 0; |
1175 | |
1176 | StringIndexer s(inStr); |
1177 | int32_t index = skipSpaces(s, 0); |
1178 | |
1179 | if (index >= s->length()) |
1180 | { |
1181 | *value = 0; |
1182 | return (strict ? truetrue : falsefalse); |
1183 | } |
1184 | |
1185 | index = handleSign(s, index, negate); |
1186 | |
1187 | |
1188 | int32_t start = index; |
1189 | int32_t slength = s->length(); |
1190 | while (index < slength) { |
1191 | ch = s[index]; |
1192 | if (ch >= '0' && ch <= '9') { |
1193 | numDigits++; |
1194 | index++; |
1195 | } |
1196 | else |
1197 | { |
1198 | |
1199 | if (ch == 0) |
1200 | { |
1201 | slength = index; |
1202 | } |
1203 | break; |
1204 | } |
1205 | } |
1206 | |
1207 | |
1208 | |
1209 | if (ch == '.') { |
1210 | while (++index < slength) { |
1211 | ch = s[index]; |
1212 | if (ch >= '0' && ch <= '9') { |
1213 | numDigits++; |
1214 | } |
1215 | else |
1216 | { |
1217 | |
1218 | if (ch == 0 && index < slength) |
1219 | { |
1220 | slength = index; |
1221 | } |
1222 | break; |
1223 | } |
1224 | } |
1225 | } |
1226 | |
1227 | |
1228 | if (index < slength && (s[index] == 'e' || s[index] == 'E')) { |
1229 | int32_t num = 0; |
1230 | boolbool negexp; |
1231 | index = handleSign(s, ++index, negexp); |
1232 | if (negexp && index >= slength) |
1233 | return falsefalse; |
1234 | |
1235 | while (index < slength) { |
1236 | ch = s[index]; |
1237 | if (ch >= '0' && ch <= '9') { |
1238 | num = num * 10 + (ch - '0'); |
1239 | index++; |
1240 | } |
1241 | else |
1242 | { |
1243 | |
1244 | if (ch == 0 && index < slength) |
1245 | { |
1246 | slength = index; |
1247 | } |
1248 | break; |
1249 | } |
1250 | } |
1251 | if (negexp) { |
1252 | num = -num; |
1253 | } |
1254 | exp10 += num; |
1255 | } |
1256 | |
1257 | index = skipSpaces(s, index); |
1258 | |
1259 | |
1260 | if (numDigits == 0) { |
1261 | |
1262 | if (s->matchesLatin1("Infinity", 8, index)) { |
1263 | index += 8; |
1264 | |
1265 | if (index < slength && skipSpaces(s, index) == index) |
1266 | return falsefalse; |
1267 | *value = (negate ? MathUtils::kNegInfinity : MathUtils::kInfinity); |
1268 | return truetrue; |
1269 | } |
1270 | return falsefalse; |
1271 | } |
1272 | |
1273 | |
1274 | if (index < slength && strict) { |
1275 | return falsefalse; |
1276 | } |
1277 | |
1278 | |
1279 | |
1280 | |
1281 | |
1282 | |
1283 | |
1284 | |
1285 | |
1286 | |
1287 | |
1288 | |
1289 | |
1290 | const int32_t limit = inStr->core()->currentBugCompatibility()->bugzilla513018 ? index : slength; |
1291 | index = start; |
1292 | if (numDigits > 15) |
1293 | { |
1294 | BigInteger exactInt; |
1295 | exactInt.setFromInteger(0); |
1296 | int32_t decimalDigits= -1; |
1297 | while (index < limit) { |
1298 | ch = s[index]; |
1299 | if ((ch >= '0' && ch <= '9') || ch == '.') { |
1300 | if (decimalDigits != -1) { |
1301 | decimalDigits++; |
1302 | } |
1303 | if (ch != '.') { |
1304 | exactInt.multAndIncrementBy(10, ch - '0'); |
1305 | } else { |
1306 | decimalDigits=0; |
1307 | } |
1308 | index++; |
1309 | } |
1310 | else |
1311 | break; |
1312 | } |
1313 | if (decimalDigits > 0) { |
1314 | exp10 -= decimalDigits; |
1315 | } |
1316 | if (exp10 > 0) { |
1317 | exactInt.multBy(quickPowTen(exp10)); |
1318 | exp10 = 0; |
1319 | } |
1320 | |
1321 | |
1322 | result = exactInt.doubleValueOf(); |
1323 | if (exp10 < 0) { |
1324 | if (exp10 < -307) { |
1325 | int32_t diff = exp10 + 307; |
1326 | result /= quickPowTen(-diff); |
1327 | exp10 -= diff; |
1328 | } |
1329 | result /= quickPowTen(-exp10); |
1330 | } |
1331 | } |
1332 | else |
1333 | { |
1334 | int32_t decimalDigits= -1; |
1335 | while (index < limit) |
1336 | { |
1337 | ch = s[index]; |
1338 | if ((ch >= '0' && ch <= '9') || ch == '.') { |
1339 | if (decimalDigits != -1) { |
1340 | decimalDigits++; |
1341 | } |
1342 | if (ch != '.') { |
1343 | result = result*10 + ch - '0'; |
1344 | } else { |
1345 | decimalDigits=0; |
1346 | } |
1347 | index++; |
1348 | } |
1349 | else |
1350 | break; |
1351 | } |
1352 | if (decimalDigits > 0) { |
1353 | exp10 -= decimalDigits; |
1354 | } |
1355 | if (exp10 >= 0) { |
1356 | result *= quickPowTen(exp10); |
1357 | |
1358 | } else { |
1359 | if (exp10 < -307) { |
1360 | int32_t diff = exp10 + 307; |
1361 | result /= quickPowTen(-diff); |
1362 | exp10 -= diff; |
1363 | } |
1364 | result /= quickPowTen(-exp10); |
1365 | } |
1366 | } |
1367 | |
1368 | *value = negate ? -result : result; |
1369 | return truetrue; |
1370 | } |
1371 | |
1372 | |
1373 | |
1374 | |
1375 | |
1376 | |
1377 | |
1378 | |
1379 | |
1380 | |
1381 | |
1382 | |
1383 | |
1384 | |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | |
1395 | |
1396 | |
1397 | |
1398 | |
1399 | |
1400 | |
1401 | |
1402 | |
1403 | |
1404 | |
1405 | |
1406 | |
1407 | |
1408 | |
1409 | |
1410 | |
1411 | |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | |
1417 | |
1418 | |
1419 | |
1420 | |
1421 | |
1422 | |
1423 | |
1424 | |
1425 | |
1426 | |
1427 | |
1428 | |
1429 | |
1430 | |
1431 | |
1432 | |
1433 | |
1434 | |
1435 | |
1436 | |
1437 | |
1438 | |
1439 | |
1440 | |
1441 | |
1442 | |
1443 | |
1444 | |
1445 | |
1446 | |
1447 | |
1448 | |
1449 | |
1450 | |
1451 | |
1452 | |
1453 | |
1454 | |
1455 | |
1456 | |
1457 | |
1458 | |
1459 | |
1460 | |
1461 | |
1462 | |
1463 | |
1464 | |
1465 | |
1466 | |
1467 | |
1468 | |
1469 | |
1470 | |
1471 | |
1472 | |
1473 | |
1474 | |
1475 | |
1476 | |
1477 | #define c315731L 15731L |
1478 | #define c2789221L 789221L |
1479 | #define c11376312589L 1376312589L |
1480 | |
1481 | |
1482 | static const uint32_t Random_Xor_Masks[31] = |
1483 | { |
1484 | |
1485 | |
1486 | |
1487 | |
1488 | 0x00000003L, 0x00000006L, 0x0000000CL, 0x00000014L, |
1489 | 0x00000030L, 0x00000060L, 0x000000B8L, 0x00000110L, |
1490 | 0x00000240L, 0x00000500L, 0x00000CA0L, 0x00001B00L, |
1491 | 0x00003500L, 0x00006000L, 0x0000B400L, 0x00012000L, |
1492 | 0x00020400L, 0x00072000L, 0x00090000L, 0x00140000L, |
1493 | 0x00300000L, 0x00400000L, 0x00D80000L, 0x01200000L, |
1494 | 0x03880000L, 0x07200000L, 0x09000000L, 0x14000000L, |
1495 | 0x32800000L, 0x48000000L, 0xA3000000L |
1496 | |
1497 | }; |
1498 | |
1499 | |
1500 | |
1501 | |
1502 | |
1503 | |
1504 | |
1505 | |
1506 | |
1507 | |
1508 | |
1509 | |
1510 | |
1511 | |
1512 | |
1513 | |
1514 | |
1515 | |
1516 | |
1517 | |
1518 | |
1519 | void MathUtils::RandomFastInit(pTRandomFast pRandomFast) |
1520 | { |
1521 | int32_t n = 31; |
1522 | |
1523 | pRandomFast->uValue = (uint32_t)(VMPI_getPerformanceCounter()); |
1524 | |
1525 | |
1526 | pRandomFast->uSequenceLength = (1L << n) - 1L; |
1527 | |
1528 | |
1529 | pRandomFast->uXorMask = Random_Xor_Masks[n - 2]; |
1530 | } |
1531 | |
1532 | |
1533 | |
1534 | |
1535 | |
1536 | |
1537 | |
1538 | |
1539 | |
1540 | |
1541 | |
1542 | |
1543 | |
1544 | |
1545 | |
1546 | |
1547 | |
1548 | |
1549 | |
1550 | |
1551 | |
1552 | |
1553 | #define RandomFastNext(_pRandomFast)( ((_pRandomFast)->uValue & 1L) ? ((_pRandomFast)-> uValue = ((_pRandomFast)->uValue >> 1) ^ (_pRandomFast )->uXorMask) : ((_pRandomFast)->uValue = ((_pRandomFast )->uValue >> 1)) ) \ |
1554 | ( \ |
1555 | ((_pRandomFast)->uValue & 1L) \ |
1556 | ? ((_pRandomFast)->uValue = ((_pRandomFast)->uValue >> 1) ^ (_pRandomFast)->uXorMask) \ |
1557 | : ((_pRandomFast)->uValue = ((_pRandomFast)->uValue >> 1)) \ |
1558 | ) |
1559 | |
1560 | |
1561 | |
1562 | |
1563 | |
1564 | |
1565 | |
1566 | |
1567 | |
1568 | |
1569 | |
1570 | |
1571 | |
1572 | |
1573 | |
1574 | |
1575 | |
1576 | |
1577 | |
1578 | |
1579 | |
1580 | |
1581 | |
1582 | |
1583 | |
1584 | |
1585 | |
1586 | |
1587 | |
1588 | |
1589 | |
1590 | |
1591 | |
1592 | |
1593 | |
1594 | |
1595 | int32_t MathUtils::RandomPureHasher(int32_t iSeed) |
1596 | { |
1597 | int32_t iResult; |
1598 | |
1599 | |
1600 | |
1601 | |
1602 | |
1603 | |
1604 | |
1605 | |
1606 | iSeed = ((iSeed << 13) ^ iSeed) - (iSeed >> 21); |
1607 | |
1608 | |
1609 | iResult = (iSeed*(iSeed*iSeed*c315731L + c2789221L) + c11376312589L) & kRandomPureMax0x7FFFFFFFL; |
1610 | |
1611 | |
1612 | |
1613 | |
1614 | iResult += iSeed; |
1615 | |
1616 | |
1617 | |
1618 | iResult = ((iResult << 13) ^ iResult) - (iResult >> 21); |
1619 | |
1620 | return iResult; |
1621 | } |
1622 | |
1623 | int32_t MathUtils::GenerateRandomNumber(pTRandomFast pRandomFast) |
1624 | { |
1625 | |
1626 | if (pRandomFast->uValue == 0) { |
1627 | RandomFastInit(pRandomFast); |
1628 | } |
1629 | |
1630 | long aNum = RandomFastNext(pRandomFast)( ((pRandomFast)->uValue & 1L) ? ((pRandomFast)->uValue = ((pRandomFast)->uValue >> 1) ^ (pRandomFast)-> uXorMask) : ((pRandomFast)->uValue = ((pRandomFast)->uValue >> 1)) ); |
1631 | |
1632 | |
1633 | aNum = RandomPureHasher(aNum * 71L); |
1634 | |
1635 | return aNum & kRandomPureMax0x7FFFFFFFL; |
1636 | } |
1637 | |
1638 | int32_t MathUtils::Random(int32_t range, pTRandomFast pRandomFast) |
1639 | { |
1640 | if (range > 0) { |
1641 | return GenerateRandomNumber(pRandomFast) % range; |
1642 | } else { |
1643 | return 0; |
1644 | } |
1645 | } |
1646 | |
1647 | void MathUtils::initRandom(TRandomFast *seed) |
1648 | { |
1649 | RandomFastInit(seed); |
1650 | } |
1651 | |
1652 | double MathUtils::random(TRandomFast *seed) |
1653 | { |
1654 | return (double)GenerateRandomNumber(seed) / ((double)kRandomPureMax0x7FFFFFFFL+1.0); |
1655 | } |
1656 | |
1657 | |
1658 | |
1659 | |
1660 | |
1661 | |
1662 | |
1663 | |
1664 | |
1665 | |
1666 | |
1667 | |
1668 | |
1669 | |
1670 | |
1671 | |
1672 | |
1673 | |
1674 | |
1675 | |
1676 | |
1677 | |
1678 | static inline void quickBigPowTen(int32_t exp, BigInteger* result) |
1679 | { |
1680 | if (exp < 22 && exp > 0) { |
1681 | result->setFromDouble(kPowersOfTen[exp]); |
1682 | } else if (exp > 0) { |
1683 | result->setFromDouble(kPowersOfTen[21]); |
1684 | exp -= 21; |
1685 | while (exp-- > 0) |
1686 | result->multBy((int32_t)10); |
1687 | } else { |
1688 | result->setFromDouble(MathUtils::pow(10,exp)); |
1689 | } |
1690 | } |
1691 | |
1692 | |
1693 | static inline double quickPowTwo(int32_t exp) |
1694 | { |
1695 | static uint64_t one = 1; |
1696 | if (exp < 64 && exp > 0) |
1697 | return (double)(one << exp); |
1698 | else |
1699 | return MathUtils::pow(2,exp); |
1700 | } |
1701 | |
1702 | |
1703 | |
1704 | int32_t D2A::nextDigit() |
1705 | { |
1706 | if (finished) |
1707 | return -1; |
1708 | |
1709 | boolbool withinLowEndRoundRange; |
1710 | boolbool withinHighEndRoundRange; |
1711 | int32_t quotient; |
1712 | |
1713 | if ( bFastEstimateOk ) |
1714 | { |
1715 | quotient = (int32_t)(dr / ds); |
1716 | double mod = MathUtils::mod(dr,ds); |
1717 | dr = mod; |
1718 | |
1719 | |
1720 | |
1721 | withinLowEndRoundRange = (lowOk ? (dr <= dMMinus) : (dr < dMMinus)); |
1722 | withinHighEndRoundRange = (highOk ? (dr+dMPlus >= ds) : (dr+dMPlus > ds)); |
1723 | } |
1724 | else |
1725 | { |
1726 | BigInteger bigQuotient; |
1727 | bigQuotient.setFromInteger(0); |
1728 | r.divBy(&s, &bigQuotient); |
1729 | quotient = (int32_t)(bigQuotient.wordBuffer[0]); |
1730 | |
1731 | withinLowEndRoundRange = (lowOk ? (r.compare(&mMinus) != 1) : (r.compare(&mMinus) == -1)); |
1732 | |
1733 | withinHighEndRoundRange = (highOk ? (r.compareOffset(&s,&mPlus) != -1) : (r.compareOffset(&s,&mPlus) == 1)); |
1734 | } |
1735 | |
1736 | |
1737 | if (quotient < 0 || quotient > 9) |
1738 | { |
1739 | AvmAssert(quotient >= 0)do { } while (0); |
1740 | AvmAssert(quotient < 10)do { } while (0); |
1741 | quotient = 0; |
1742 | } |
1743 | if (!withinLowEndRoundRange) |
1744 | { |
1745 | if (!withinHighEndRoundRange) |
1746 | { |
1747 | if ( bFastEstimateOk ) |
1748 | { |
1749 | dr *= 10; |
1750 | dMPlus *= 10; |
1751 | dMMinus *= 10; |
1752 | } |
1753 | else |
1754 | { |
1755 | r.multBy((int32_t)10); |
1756 | mPlus.multBy((int32_t)10); |
1757 | mMinus.multBy((int32_t)10); |
1758 | } |
1759 | } |
1760 | else |
1761 | { |
1762 | quotient++; |
1763 | finished = truetrue; |
1764 | } |
1765 | } |
1766 | else if (!withinHighEndRoundRange) |
1767 | { |
1768 | finished = truetrue; |
1769 | } |
1770 | else |
1771 | { |
1772 | if ( (bFastEstimateOk && (dr*2 < ds)) || |
1773 | (!bFastEstimateOk && (r.compareOffset(&s,&r) == -1)) ) |
1774 | { |
1775 | finished = truetrue; |
1776 | } |
1777 | else |
1778 | { |
1779 | quotient++; |
1780 | finished = truetrue; |
1781 | } |
1782 | } |
1783 | return quotient; |
1784 | } |
1785 | |
1786 | |
1787 | int32_t D2A::fixup_ExponentEstimate(int32_t exponentEstimate) |
1788 | { |
1789 | int32_t correctedEstimate; |
1790 | if (bFastEstimateOk) |
1791 | { |
1792 | if (highOk ? (dr+dMPlus) >= ds : dr+dMPlus > ds) |
1793 | { |
1794 | correctedEstimate = exponentEstimate+1; |
1795 | } |
1796 | else |
1797 | { |
1798 | dr *= 10; |
1799 | dMPlus *= 10; |
1800 | dMMinus *= 10; |
1801 | correctedEstimate = exponentEstimate; |
1802 | } |
1803 | } |
1804 | else |
1805 | { |
1806 | if (highOk ? (r.compareOffset(&s,&mPlus) != -1) : (r.compareOffset(&s,&mPlus) == 1)) |
1807 | { |
1808 | correctedEstimate = exponentEstimate+1; |
1809 | } |
1810 | else |
1811 | { |
1812 | r.multBy((int32_t)10); |
1813 | mPlus.multBy((int32_t)10); |
1814 | mMinus.multBy((int32_t)10); |
1815 | correctedEstimate = exponentEstimate; |
1816 | } |
1817 | } |
1818 | return correctedEstimate; |
1819 | } |
1820 | |
1821 | int32_t D2A::scale() |
1822 | { |
1823 | |
1824 | int32_t base2Exponent = e + mantissaPrec-1; |
1825 | int32_t exponentEstimate = (int32_t)MathUtils::ceil( (base2Exponent * kLog2_10) - 0.0000000001); |
1826 | |
1827 | |
1828 | if (bFastEstimateOk) |
1829 | { |
1830 | double scale = quickPowTen( (exponentEstimate > 0) ? exponentEstimate : -exponentEstimate); |
1831 | |
1832 | if (exponentEstimate >= 0) |
1833 | { |
1834 | ds *= scale; |
1835 | return fixup_ExponentEstimate(exponentEstimate); |
1836 | } |
1837 | else |
1838 | { |
1839 | dr *= scale; |
1840 | dMPlus *= scale; |
1841 | dMMinus *= scale; |
1842 | return fixup_ExponentEstimate(exponentEstimate); |
1843 | } |
1844 | } |
1845 | else |
1846 | { |
1847 | BigInteger scale; |
1848 | scale.setFromInteger(0); |
1849 | quickBigPowTen( (exponentEstimate > 0) ? exponentEstimate : -exponentEstimate, &scale); |
1850 | |
1851 | if (exponentEstimate >= 0) |
1852 | { |
1853 | s.multBy(&scale); |
1854 | return fixup_ExponentEstimate(exponentEstimate); |
1855 | } |
1856 | else |
1857 | { |
1858 | r.multBy(&scale); |
1859 | mPlus.multBy(&scale); |
1860 | mMinus.multBy(&scale); |
1861 | |
1862 | return fixup_ExponentEstimate(exponentEstimate); |
1863 | } |
1864 | } |
1865 | } |
1866 | |
1867 | D2A::~D2A() |
1868 | { |
1869 | } |
1870 | |
1871 | |
1872 | D2A::D2A(double avalue, int32_t mode, int32_t minDigits) |
1873 | : value(avalue), finished(falsefalse), bFastEstimateOk(falsefalse), minPrecision(minDigits) |
1874 | { |
1875 | |
1876 | |
1877 | mantissa = MathUtils::frexp(value,&e); |
1878 | |
1879 | if (mode != MathUtils::DTOSTR_NORMAL) |
1880 | { |
1881 | lowOk = highOk = truetrue; |
1882 | } |
1883 | else |
1884 | { |
1885 | boolbool round = !(mantissa & 0x1); |
1886 | lowOk = highOk = round; |
1887 | } |
1888 | |
1889 | |
1890 | long base2Precision = 53; |
1891 | mantissaPrec = base2Precision; |
1892 | while ( mantissaPrec != 0 && ((mantissa >> --mantissaPrec & 1) == 0) ) |
1893 | ; |
1894 | mantissaPrec++; |
1895 | |
1896 | int32_t absE = (e > 0 ? e : -e); |
1897 | if ((absE + mantissaPrec-1) < 50) |
1898 | bFastEstimateOk = truetrue; |
1899 | |
1900 | |
1901 | |
1902 | |
1903 | if (bFastEstimateOk) |
1904 | { |
1905 | if (e >= 0) |
1906 | { |
1907 | if (mantissa != 0x10000000000000LL) |
1908 | { |
1909 | double be = quickPowTwo(e); |
1910 | |
1911 | dr = (double)mantissa*be*2; |
1912 | ds = 2; |
1913 | dMPlus = be; |
1914 | dMMinus = be; |
1915 | } |
1916 | else |
1917 | { |
1918 | double be = quickPowTwo(e);; |
1919 | double be1 = be*2; |
1920 | |
1921 | dr = (double)mantissa*be1*2; |
1922 | ds = 4; |
1923 | dMPlus = be1; |
1924 | dMMinus = be; |
1925 | } |
1926 | } |
1927 | else if ( mantissa != MathUtils::pow(2, base2Precision-1) ) |
1928 | { |
1929 | dr = (double)mantissa*2.0; |
1930 | ds = quickPowTwo(1-e); |
1931 | dMPlus = 1; |
1932 | dMMinus = 1; |
1933 | } |
1934 | else |
1935 | { |
1936 | dr = (double)mantissa*4.0; |
1937 | ds = quickPowTwo(2-e); |
1938 | dMPlus = 2; |
1939 | dMMinus = 1; |
1940 | } |
1941 | |
1942 | |
1943 | if (mode != MathUtils::DTOSTR_NORMAL) |
1944 | { |
1945 | double fixedPrecisionPowTen = quickPowTen( minPrecision ); |
1946 | ds *= fixedPrecisionPowTen; |
1947 | dr *= fixedPrecisionPowTen; |
1948 | } |
1949 | } |
1950 | else |
1951 | { |
1952 | if (e >= 0) |
1953 | { |
1954 | if (mantissa != 0x10000000000000LL) |
1955 | { |
1956 | BigInteger be; |
1957 | be.setFromInteger(1); |
1958 | be.lshiftBy(e); |
1959 | |
1960 | r.setFromDouble(value); |
1961 | r.lshiftBy(1); |
1962 | |
1963 | s.setFromInteger(2); |
1964 | |
1965 | mPlus.setFromBigInteger(&be,0,be.numWords); |
1966 | mMinus.setFromBigInteger(&be,0,be.numWords); |
1967 | } |
1968 | else |
1969 | { |
1970 | BigInteger be; |
1971 | be.setFromInteger(1); |
1972 | be.lshiftBy(e); |
1973 | |
1974 | BigInteger be1; |
1975 | be1.setFromInteger(0); |
1976 | be.lshift(1,&be1); |
1977 | |
1978 | r.setFromDouble(value*4.0); |
1979 | s.setFromInteger(4); |
1980 | mPlus.setFromBigInteger(&be1,0,be1.numWords); |
1981 | mMinus.setFromBigInteger(&be,0,be.numWords); |
1982 | } |
1983 | } |
1984 | else if ( mantissa != MathUtils::pow(2, base2Precision-1) ) |
1985 | { |
1986 | r.setFromDouble( (double)(mantissa*2) ); |
1987 | s.setFromInteger(2); |
1988 | s.lshiftBy(-e); |
1989 | mPlus.setFromInteger(1); |
1990 | mMinus.setFromInteger(1); |
1991 | } |
1992 | else |
1993 | { |
1994 | r.setFromDouble( (double)(mantissa*4) ); |
1995 | s.setFromInteger(2); |
1996 | s.lshiftBy(1-e); |
1997 | mPlus.setFromInteger(2); |
1998 | mMinus.setFromInteger(1); |
1999 | } |
2000 | |
2001 | |
2002 | |
2003 | |
2004 | |
2005 | |
2006 | |
2007 | |
2008 | |
2009 | if (mode != MathUtils::DTOSTR_NORMAL) |
2010 | { |
2011 | BigInteger bFixedPrecisionPowTen; |
2012 | bFixedPrecisionPowTen.setFromInteger(0); |
2013 | quickBigPowTen( minPrecision, &bFixedPrecisionPowTen ); |
2014 | s.multBy(&bFixedPrecisionPowTen); |
2015 | r.multBy(&bFixedPrecisionPowTen); |
2016 | } |
2017 | } |
2018 | base10Exp = scale(); |
2019 | } |
2020 | |
2021 | |
2022 | |
2023 | |
2024 | |
2025 | |
2026 | |
2027 | |
2028 | |
2029 | |
2030 | |
2031 | |
2032 | |
2033 | |
2034 | |
2035 | |
2036 | |
2037 | |
2038 | |
2039 | |
2040 | |
2041 | |
2042 | |
2043 | |
2044 | |
2045 | |
2046 | |
2047 | |
2048 | |
2049 | |
2050 | |
2051 | |
2052 | |
2053 | |
2054 | |
2055 | |
2056 | |
2057 | |
2058 | |
2059 | |
2060 | |
2061 | |
2062 | |
2063 | |
2064 | } |