Repository navigation
Expand file tree
/
Copy pathdtoa.reds
More file actions
1952 lines (1797 loc) · 34.7 KB
/
Copy pathdtoa.reds
File metadata and controls
1952 lines (1797 loc) · 34.7 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
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
Red/System [
Title: "Red float <-> string functions"
Author: "Xie Qingtian"
File: %dtoa.reds
Tabs: 4
Rights: "Copyright (C) 2011-2015 Xie Qingtian. All rights reserved."
License: {
Distributed under the Boost Software License, Version 1.0.
See https://github.com/red/red/blob/master/BSL-License.txt
}
Notes: {
This file partially port dtoa.c(by David M. Gay, downloaded from
http://www.netlib.org/fp/dtoa.c) to the Red runtime.
Please remember to check http://www.netlib.org/fp regularly (
and especially before any Red release) for bugfixes and updates.
FYI: a more readable version from Python, in %Python/dtoa.c
http://hg.python.org/cpython
!! For `dtoa`, only support mode 0 now !!
}
]
#define WORD_0(x) [x/int2] ;-- for little endian
#define WORD_1(x) [x/int1]
#define DTOA_BIG_INT_X(x) (as int-ptr! (as byte-ptr! x) + 20)
#define STORE_AND_INC(a b c) [
a/value: b << 16 or (c and FFFFh)
a: a + 1
]
#define DTOA_EXP_SHIFT 20
#define DTOA_EXP_SHIFT1 20
#define DTOA_EXP_MSK1 00100000h
#define DTOA_EXP_MSK11 00100000h
#define DTOA_EXP_MASK 7FF00000h
#define DTOA_NBITS 53
#define DTOA_BIAS 1023
#define DTOA_EMAX 1023
#define DTOA_EMIN -1022
#define DTOA_ETINY -1074 ;-- smallest denormal is 2**DTOA_ETINY
#define DTOA_EXP_1 3FF00000h
#define DTOA_EXP_11 3FF00000h
#define DTOA_EBITS 11
#define FRAC_MASK 000FFFFFh
#define FRAC_MASK1 000FFFFFh
#define TEN_PMAX 22
#define BNDRY_MASK 000FFFFFh
#define BNDRY_MASK1 000FFFFFh
#define DTOA_SIGN_BIT 80000000h
#define DTOA_LOG2P 1
#define DTOA_TINY0 0
#define DTOA_TINY1 1
#define DTOA_INT_MAX 14
#define FLT_RADIX 2.0 ;@@ value for machines except the IBM 360 and derivatives
#define DBL_MAX_10_EXP 308 ;@@ value for IEEE_Arith
#define DBL_MAX_EXP 1024 ;@@ value for IEEE_Arith
#define N_BIGTENS 5
#define DTOA_BIG_0 [(FRAC_MASK1 or (DBL_MAX_EXP + DTOA_BIAS - 1 * DTOA_EXP_MSK1))]
#define DTOA_BIG_1 FFFFFFFFh
dtoa: context [
P05: protect [5 25 125]
DTOA_TENS: protect [
1e0 1e1 1e2 1e3 1e4 1e5 1e6 1e7 1e8 1e9
1e10 1e11 1e12 1e13 1e14 1e15 1e16 1e17 1e18 1e19
1e20 1e21 1e22
]
BIGTENS: protect [1e16 1e32 1e64 1e128 1e256]
TINYTENS: [1e-16 1e-32 1e-64 1e-128 0.0]
TINYTENS/5: 9007199254740992.0 * 9007199254740992e-256
freelist: [null null null null null null null null]
KMax: (size? freelist) - 1
int64!: alias struct! [int1 [integer!] int2 [integer!]]
big-int!: alias struct! [
next [big-int!]
k [integer!]
maxwds [integer!]
sign [integer!]
wds [integer!]
x [integer!]
]
cmp-info!: alias struct! [
e0 [integer!]
nd [integer!]
nd0 [integer!]
scale [integer!]
]
Balloc: func [
k [integer!]
return: [big-int!]
/local
idx [integer!]
x [integer!]
len [integer!] ;@@ should be unsigned integer!
big [big-int!]
][
idx: k + 1
big: as big-int! freelist/idx
either all [
k <= KMax
big <> null
][
freelist/idx: as-integer big/next
][
x: 1 << k
len: x - 1 * 4 + 8 + (size? big-int!) - 1 / 8
big: as big-int! allocate len * 8 ;@@ check if big = null
big/k: k
big/maxwds: x
]
big/sign: 0
big/wds: 0
big
]
Bfree: func [
p [big-int!]
/local
idx [integer!]
][
if p <> null [
either p/k > KMax [
free as byte-ptr! p
][
idx: p/k + 1
p/next: as big-int! freelist/idx
freelist/idx: as-integer p
]
]
]
#define Bcopy(x y) [
copy-memory (as byte-ptr! x) + 12 (as byte-ptr! y) + 12 (y/wds * 4 + 8)
]
Bmultiply: func [ ;-- multiply two Bigints. Ignores the signs of a and b.
a [big-int!]
b [big-int!]
return: [big-int!]
/local
k wa wb wc
x xa xae xb xbe xc xc0
y carry z z2 c
][
if any [
all [a/x = 0 a/wds = 1]
all [b/x = 0 b/wds = 1]
][
c: Balloc 0 ;@@ check if c = null ?
c/wds: 1
c/x: 0
return c
]
if a/wds < b/wds [c: a a: b b: c]
k: a/k
wa: a/wds
wb: b/wds
wc: wa + wb
if wc > a/maxwds [k: k + 1]
c: Balloc k ;@@ check if c = null ?
x: DTOA_BIG_INT_X(c)
xa: x + wc
while [x < xa][
x/value: 0
x: x + 1
]
xa: DTOA_BIG_INT_X(a)
xae: xa + wa
xb: DTOA_BIG_INT_X(b)
xbe: xb + wb
xc0: DTOA_BIG_INT_X(c)
while [xb < xbe][
y: xb/value and FFFFh
if y <> 0 [
x: xa
xc: xc0
carry: 0
until [
z: x/value and FFFFh * y + (xc/value and FFFFh) + carry
carry: z >>> 16
z2: x/value >>> 16 * y + (xc/value >>> 16) + carry
carry: z2 >>> 16
STORE_AND_INC(xc z2 z)
x: x + 1
x = xae
]
xc/value: carry
]
y: xb/value >>> 16
if y <> 0 [
x: xa
xc: xc0
carry: 0
z2: xc/value
until [
z: x/value and FFFFh * y + (xc/value >>> 16) + carry
carry: z >>> 16
STORE_AND_INC(xc z z2)
z2: x/value >>> 16 * y + (xc/value and FFFFh) + carry
carry: z2 >>> 16
x: x + 1
x = xae
]
xc/value: z2
]
xb: xb + 1
xc0: xc0 + 1
]
xc0: DTOA_BIG_INT_X(c)
xc: xc0 + wc
while [xc: xc - 1 all [wc > 0 zero? xc/1]][wc: wc - 1]
c/wds: wc
c
]
Bmult-add: func [ ;-- multiply by m and add a
b [big-int!]
m [integer!]
a [integer!]
return: [big-int!]
/local
i [integer!]
wds [integer!]
carry [integer!]
x [int-ptr!]
y [integer!]
xi [integer!]
z [integer!]
b1 [big-int!]
][
wds: b/wds
x: DTOA_BIG_INT_X(b)
i: 0
carry: a
until [
xi: x/value
y: (xi and FFFFh) * m + carry
z: xi >>> 16 * m + (y >>> 16)
carry: z >>> 16
x/value: z << 16 + (y and FFFFh)
x: x + 1
i: i + 1
i = wds
]
if carry <> 0 [
if wds >= b/maxwds [
b1: Balloc b/k + 1 ;@@ check b1 = null ?
Bcopy(b1 b)
Bfree b
b: b1
]
x: DTOA_BIG_INT_X(b)
wds: wds + 1
x/wds: carry
b/wds: wds
]
b
]
Bpow5mult: func [ ;-- multiply the Bigint b by 5**k.
b [big-int!] ;-- Ignores the sign of b.
k [integer!]
return: [big-int!]
/local
b1 [big-int!]
p5 [big-int!]
p51 [big-int!]
i [integer!]
][
i: k and 3
if i <> 0 [
b: Bmult-add b P05/i 0 ;@@ check if b = null ?
]
k: k >> 2
if zero? k [return b]
p5: int-to-big 625 ;@@ check if p5 = null ?
until [
if k and 1 <> 0 [
b1: Bmultiply b p5
Bfree b
b: b1 ;@@ check if b = null ?
]
k: k >> 1
if k <> 0 [
p51: Bmultiply p5 p5
Bfree p5
p5: p51 ;@@ check if p5 = null ?
]
zero? k
]
Bfree p5
b
]
Blshift: func [ ;-- shift a Bigint b left by k bits.
b [big-int!]
k [integer!]
return: [big-int!]
/local
i k1 n n1 b1 x x1 xe z
][
if any [
zero? k
all [zero? b/x b/wds = 1]
][return b]
n: k >> 5
k1: b/k
n1: n + b/wds + 1
i: b/maxwds
while [n1 > i][
k1: k1 + 1
i: i << 1
]
b1: Balloc k1 ;@@ check if b1 = null ?
x1: DTOA_BIG_INT_X(b1)
i: 0
while [i < n][
x1/value: 0
x1: x1 + 1
i: i + 1
]
x: DTOA_BIG_INT_X(b)
xe: x + b/wds
k: k and 1Fh
either k <> 0 [
k1: 32 - k
z: 0
until [
x1/value: x/value << k or z
z: x/value >>> k1
x1: x1 + 1
x: x + 1
x = xe
]
x1/value: z
if z <> 0 [n1: n1 + 1]
][
until [
x1/value: x/value
x1: x1 + 1
x: x + 1
x = xe
]
]
b1/wds: n1 - 1
Bfree b
b1
]
Bcmp: func [
a [big-int!]
b [big-int!]
return: [integer!]
/local
xa xa0 xb xb0 i j x y
][
i: a/wds
j: b/wds
if i <> j [return i - j]
xa0: DTOA_BIG_INT_X(a)
xa: xa0 + j
xb0: DTOA_BIG_INT_X(b)
xb: xb0 + j
until [
xa: xa - 1
xb: xb - 1
x: as byte-ptr! xa/value
y: as byte-ptr! xb/value
if x <> y [
return either x < y [-1][1]
]
xb <= xb0
]
0
]
Bdiff: func [
a [big-int!]
b [big-int!]
return: [big-int!]
/local
c i wa wb xa xae xb xbe xc
borrow y z
][
i: Bcmp a b
if zero? i [ ;-- a = b
c: Balloc 0
c/wds: 1
c/x: 0
return c
]
either i < 0 [
c: a
a: b
b: c
i: 1
][
i: 0
]
c: Balloc a/k
c/sign: i
wa: a/wds
xa: DTOA_BIG_INT_X(a)
xae: xa + wa
wb: b/wds
xb: DTOA_BIG_INT_X(b)
xbe: xb + wb
xc: DTOA_BIG_INT_X(c)
borrow: 0
until [
y: xa/value and FFFFh - (xb/value and FFFFh) - borrow
borrow: y and 00010000h >>> 16
z: xa/value >>> 16 - (xb/value >>> 16) - borrow
borrow: z and 00010000h >>> 16
STORE_AND_INC(xc z y)
xa: xa + 1
xb: xb + 1
xb = xbe
]
while [xa < xae][
y: xa/value and FFFFh - borrow
borrow: y and 00010000h >>> 16
z: xa/value >>> 16 - borrow
borrow: z and 00010000h >>> 16
STORE_AND_INC(xc z y)
xa: xa + 1
]
while [
xc: xc - 1
zero? xc/value
][
wa: wa - 1
]
c/wds: wa
c
]
Bdshift: func [
b [big-int!]
p2 [integer!]
return: [integer!]
/local
rv [integer!]
x [int-ptr!]
wds [integer!]
][
x: DTOA_BIG_INT_X(b)
wds: b/wds
rv: (hi0bits x/wds) - 4
if p2 > 0 [rv: rv - p2]
rv and 31
]
;-- special case of Bigint division. The quotient is always in the range
;-- 0 <= quotient < 10, and on entry the divisor S is normalized so that
;-- its top 4 bits (28--31) are zero and bit 27 is set.
Bquorem: func [
b [big-int!]
s [big-int!]
return: [integer!]
/local
n bx bxe q sx sxe si bi
borrow carry y ys z zs
][
n: s/wds
if b/wds < n [return 0]
sx: DTOA_BIG_INT_X(s)
bx: DTOA_BIG_INT_X(b)
;@@ do unsigned int division
q: as-integer (uint-to-float bx/n) / (uint-to-float sx/n + 1) ;-- ensure q <= true quotient
n: n - 1
sxe: sx + n
bxe: bx + n
if q <> 0 [
borrow: 0
carry: 0
until [
si: sx/value
ys: si and FFFFh * q + carry
zs: si >>> 16 * q + (ys >>> 16)
carry: zs >>> 16
bi: bx/value
y: bi and FFFFh - (ys and FFFFh) - borrow
borrow: y and 00010000h >>> 16
z: bi >>> 16 - (zs and FFFFh) - borrow
borrow: z and 00010000h >>> 16
STORE_AND_INC(bx z y)
sx: sx + 1
sx > sxe
]
if zero? bxe/value [
bx: DTOA_BIG_INT_X(b)
while [
bxe: bxe - 1
all [bxe > bx zero? bxe/value]
][n: n - 1]
b/wds: n
]
]
if 0 <= Bcmp b s [
q: q + 1
borrow: 0
carry: 0
bx: DTOA_BIG_INT_X(b)
sx: DTOA_BIG_INT_X(s)
until [
si: sx/value
ys: si and FFFFh + carry
zs: si >>> 16 + (ys >>> 16)
carry: zs >>> 16
bi: bx/value
y: bi and FFFFh - (ys and FFFFh) - borrow
borrow: y and 00010000h >>> 16
z: bi >>> 16 - (zs and FFFFh) - borrow
borrow: z and 00010000h >>> 16
STORE_AND_INC(bx z y)
sx: sx + 1
sx > sxe
]
bx: DTOA_BIG_INT_X(b)
bxe: bx + n
if zero? bxe/value [
while [
bxe: bxe - 1
all [bxe > bx zero? bxe/value]
][n: n - 1]
b/wds: n
]
]
q
]
Bratio: func [ ;-- Compute the ratio of two Bigints, as a double
a [big-int!]
b [big-int!]
return: [float!]
/local
fa [float!]
fb [float!]
da [int64!]
db [int64!]
k [integer!]
ka [integer!]
kb [integer!]
][
ka: 0
kb: 0
fa: big-to-float a :ka
fb: big-to-float b :kb
da: as int64! :fa
db: as int64! :fb
k: 32 * (a/wds - b/wds) + ka - kb
either k > 0 [
da/int2: da/int2 + (k * DTOA_EXP_MSK1)
][
k: 0 - k
db/int2: db/int2 + (k * DTOA_EXP_MSK1)
]
fa / fb
]
hi0bits: func [ ;-- count leading 0 bits in the 32-bit integer x
x [integer!]
return: [integer!]
/local
k [integer!]
][
k: 0
if x and FFFF0000h = 0 [k: 16 x: x << 16]
if x and FF000000h = 0 [k: k + 8 x: x << 8 ]
if x and F0000000h = 0 [k: k + 4 x: x << 4 ]
if x and C0000000h = 0 [k: k + 2 x: x << 2 ]
if x and 80000000h = 0 [
k: k + 1
if x and 40000000h = 0 [return 32]
]
k
]
lo0bits: func [ ;-- count trailing 0 bits in the 32-bit integer y,
y [int-ptr!] ;-- and shift y right by that number of bits.
return: [integer!]
/local
k [integer!]
x [integer!]
][
x: y/value
if x and 7 <> 0 [
if x and 1 <> 0 [return 0]
if x and 2 <> 0 [y/value: x >>> 1 return 1]
y/value: x >>> 2
return 2
]
k: 0
if x and FFFFh = 0 [k: 16 x: x >>> 16]
if x and 00FFh = 0 [k: k + 8 x: x >>> 8 ]
if x and 000Fh = 0 [k: k + 4 x: x >>> 4 ]
if x and 0003h = 0 [k: k + 2 x: x >>> 2 ]
if x and 1 = 0 [
k: k + 1
x: x >>> 1
if x = 0 [return 32]
]
y/value: x
k
]
int-to-big: func [ ;-- convert a small nonnegative integer to a Bigint
i [integer!]
return: [big-int!]
/local
b [big-int!]
][
b: Balloc 1 ;@@ Check if b = null ?
b/x: i
b/wds: 1
b
]
big-to-float: func [
a [big-int!]
e [int-ptr!]
return: [float!]
/local
d [int64! value] xa xa0 w y z k f
][
f: as pointer! [float!] d
xa0: DTOA_BIG_INT_X(a)
xa: xa0 + a/wds - 1
y: xa/value
k: hi0bits y
e/value: 32 - k
if k < DTOA_EBITS [
d/int2: DTOA_EXP_1 or (y >>> (DTOA_EBITS - k))
w: either xa > xa0 [xa: xa - 1 xa/value][0]
d/int1: y << (32 - DTOA_EBITS + k) or (w >>> (DTOA_EBITS - k))
return f/value
]
z: either xa > xa0 [xa: xa - 1 xa/value][0]
k: k - DTOA_EBITS
either k <> 0 [
d/int2: DTOA_EXP_1 or (y << k) or (z >>> (32 - k))
y: either xa > xa0 [xa: xa - 1 xa/value][0]
d/int1: z << k or (y >>> (32 - k))
][
d/int2: DTOA_EXP_1 or y
d/int1: z
]
return f/value
]
float-to-big: func [
f [float!]
e [int-ptr!]
bits [int-ptr!]
return: [big-int!]
/local
d b de k x y z i w0
][
b: Balloc 1
x: DTOA_BIG_INT_X(b)
d: as int64! :f
w0: WORD_0(d)
z: w0 and FRAC_MASK
w0: w0 and 7FFFFFFFh ;-- clear sign bit, which we ignore
d/int2: w0 ;@@ little endian or big endian ?
de: w0 >>> DTOA_EXP_SHIFT
if de <> 0 [z: z or DTOA_EXP_MSK1]
y: WORD_1(d)
either zero? y [
k: lo0bits :z
x/1: z
i: 1
b/wds: 1
k: k + 32
][
k: lo0bits :y
either zero? k [
x/1: y
][
x/1: z << (32 - k) or y
z: z >>> k
]
x/2: z
i: either z <> 0 [2][1]
b/wds: i
]
either zero? de [
e/value: de - DTOA_BIAS - 52 + 1 + k
bits/value: 32 * i - hi0bits x/i
][
e/value: de - DTOA_BIAS - 52 + k
bits/value: 53 - k
]
b
]
uint-to-float: func [
i [integer!]
return: [float!]
/local
f [float!]
d [int-ptr!]
][
;-- Based on this method: http://stackoverflow.com/a/429812/494472
;-- A bit more explanation: http://lolengine.net/blog/2011/3/20/understanding-fast-float-integer-conversions
f: 6755399441055744.0
d: as int-ptr! :f
d/value: i or d/value
f - 6755399441055744.0
]
float-to-ascii: func [
f [float!]
ndigits [integer!] ;-- ndigits <= 0: mode 0 ndigits > 0: mode 4
decpt [int-ptr!]
sign [int-ptr!]
length [int-ptr!]
add-0? [logic!]
return: [c-string!]
/local
b b1 mlo mhi SS delta [big-int!]
d d2 [int64!]
s s0 [c-string!]
DTOA_RETURN_1 DTOA_RETURN DTOA_ROUND_OFF [subroutine!]
fsave ds kf [float!]
bbits b2 b5 be i j j1 k k0 ki m2 m5 s2 s5 L x w0 w1 ww0 ilim [integer!]
sign? spec_case denorm k_check [logic!]
dig [byte!]
][
s0: "-000000000000000000000000000000" ;-- 32 bits including ending null char
s: s0 + 1
s0: s
mlo: null
mhi: null
SS: null
k: 0
fsave: 0.0
kf: 0.0
d: as int64! :f
w0: WORD_0(d)
w1: WORD_1(d)
DTOA_RETURN_1: [
Bfree b
s/1: #"^@"
decpt/value: k + 1
sign/value: as-integer sign?
length/value: as-integer s - s0
return s0
]
DTOA_RETURN: [
Bfree SS
if mhi <> null [
if all [mlo <> null mlo <> mhi][
Bfree mlo
]
Bfree mhi
]
DTOA_RETURN_1
]
DTOA_ROUND_OFF: [
while [
s: s - 1
s/1 = #"9"
][
if s = s0 [
k: k + 1
s/1: #"1"
s: s + 1
DTOA_RETURN
]
]
s/1: s/1 + 1
s: s + 1
]
either zero? (w0 and DTOA_SIGN_BIT) [
sign?: no
][
sign?: yes
w0: w0 and (not DTOA_SIGN_BIT)
d/int2: w0 ;@@ WORD_0(d): w0 little endian or big endian ?
]
if w0 and DTOA_EXP_MASK = DTOA_EXP_MASK [
decpt/value: 9999
if all [
zero? w1
zero? (w0 and 000FFFFFh)
][
return either sign? ["-1.#INF"]["1.#INF"]
]
return "1.#NaN"
]
if f = 0.0 [
decpt/value: 9998
either add-0? [
return either not sign? ["0.0"]["-0.0"]
][
return either not sign? ["0"]["-0"]
]
]
be: 0
bbits: 0
b: float-to-big f :be :bbits
i: w0 >>> DTOA_EXP_SHIFT1 and (DTOA_EXP_MASK >> DTOA_EXP_SHIFT1)
fsave: f
d2: d
denorm: either i <> 0 [
ww0: WORD_0(d2)
ww0: ww0 and FRAC_MASK1
ww0: ww0 or DTOA_EXP_11
d2/int2: ww0 ;@@ little endian or big endian ?
i: i - DTOA_BIAS
no
][
i: bbits + be + DTOA_BIAS + 51
x: either i > 32 [
w0 << (64 - i) or (w1 >>> (i - 32))
][
w1 << (32 - i)
]
f: uint-to-float x
ww0: WORD_0(d2)
ww0: ww0 - (31 * DTOA_EXP_MSK1)
d2/int2: ww0 ;@@ little endian or big endian ?
i: i - (DTOA_BIAS + 52)
yes
]
ds: f - 1.5 * 0.289529654602168 + 0.1760912590558 + ((as-float i) * 0.301029995663981)
f: fsave
k: as-integer floor ds ;@@ Optimize it
k_check: yes
if all [k >= 0 k <= TEN_PMAX] [
ki: k + 1 ;-- adjust for 1-based array
kf: DTOA_TENS/ki ;@@ f < DTOA_TENS/ki not work !
if f < kf [k: k - 1]
k_check: no
]
j: bbits - i - 1
either j >= 0 [
b2: 0
s2: j
][
b2: 0 - j
s2: 0
]
either k >= 0 [
b5: 0
s5: k
s2: s2 + k
][
b2: b2 - k
b5: 0 - k
s5: 0
]
ilim: -1
if ndigits > 0 [
ilim: ndigits
]
if all [be >= 0 k <= DTOA_INT_MAX] [ ;-- Do we have a "small" integer?
ki: k + 1
ds: DTOA_TENS/ki
forever [
i: 1
L: as-integer f / ds
f: f - (ds * uint-to-float L)
s/1: #"0" + as byte! L
s: s + 1
if f = 0.0 [break]
if i = ilim [
f: f + f
if any [
f > ds
all [L and 1 = 1 f = ds]
][
while [
s: s - 1
s/1 = #"9"
][
if s = s0 [
k: k + 1
s/1: #"0"
break
]
]
s/1: s/1 + 1
s: s + 1
]
break
]
i: i + 1
f: f * 10.0
]
DTOA_RETURN_1
]
m2: b2
m5: b5
i: either denorm [be + (DTOA_BIAS + 52)][1 + 53 - bbits]
b2: b2 + i
s2: s2 + i
mhi: int-to-big 1
if all [m2 > 0 s2 > 0][
i: either m2 < s2 [m2][s2]
b2: b2 - i
m2: m2 - i
s2: s2 - i
]
if b5 > 0 [
if m5 > 0 [
mhi: Bpow5mult mhi m5
b1: Bmultiply mhi b
Bfree b
b: b1
]
j: b5 - m5
if j <> 0 [b: Bpow5mult b j]
]
SS: int-to-big 1
if s5 > 0 [SS: Bpow5mult SS s5]
spec_case: no
if all [
zero? w1
zero? (w0 and BNDRY_MASK)
w0 and (DTOA_EXP_MASK and (not DTOA_EXP_MSK1)) <> 0
][
b2: b2 + DTOA_LOG2P
s2: s2 + DTOA_LOG2P
spec_case: yes
]
i: Bdshift SS s2
b2: b2 + i
m2: m2 + i
s2: s2 + i
if b2 > 0 [b: Blshift b b2]
if s2 > 0 [SS: Blshift SS s2]
if k_check [
if 0 > Bcmp b SS [
k: k - 1
b: Bmult-add b 10 0
mhi: Bmult-add mhi 10 0
]
]
if m2 > 0 [
mhi: Blshift mhi m2
]