@fraczak/k 8.7.1 → 8.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/DOCS/CODECS.md +42 -40
- package/DOCS/FILE_FORMATS.md +28 -25
- package/DOCS/GETTING_STARTED.md +10 -15
- package/DOCS/PATTERNS.md +3 -3
- package/DOCS/REPL.md +3 -6
- package/Examples/ieee-arithmetics.k +14 -29
- package/Examples/poly.k +18 -29
- package/README.md +14 -18
- package/backend-api.mjs +1 -3
- package/backends/llvm/src/cli.mjs +1 -2
- package/backends/llvm/tests/integration.sh +2 -6
- package/backends/wasm/tests/integration.sh +5 -10
- package/browser/shims/fs.mjs +2 -6
- package/canonical-pattern.mjs +9 -29
- package/compiler.mjs +22 -12
- package/harness/suites/poly.mjs +9 -11
- package/index.mjs +119 -50
- package/lib/djson.k +981 -295
- package/lib/generic.k +701 -0
- package/lib/ieee.k +662 -638
- package/lib/integer.k +542 -0
- package/object.mjs +19 -12
- package/objects/README.md +6 -6
- package/package.json +1 -1
- package/scripts/benchmark-idempotency.mjs +16 -17
- package/scripts/benchmark.mjs +39 -29
- package/scripts/build-repl-html.mjs +45 -35
- package/scripts/perf-ieee.mjs +1 -5
- package/scripts/perf-int.mjs +12 -27
- package/scripts/perf-multiplication.mjs +8 -9
- package/scripts/perf-poly.mjs +7 -10
- package/scripts/render-benchmark.mjs +1 -1
- package/scripts/run-tests.mjs +4 -2
- package/scripts/test-package.mjs +4 -10
- package/tests/README.md +4 -5
- package/tests/helpers/cli.mjs +30 -7
- package/tests/integration.sh +5 -10
- package/tests/test-arithmetics.mjs +23 -16
- package/tests/test-djson-methods.mjs +17 -25
- package/tests/test-djson.mjs +273 -0
- package/tests/test-generic.mjs +239 -0
- package/tests/test-ieee-arithmetic.mjs +1 -5
- package/tests/test-ieee.mjs +153 -0
- package/tests/test-integer.mjs +264 -0
- package/tests/test-k-object.mjs +12 -18
- package/tests/test-kvm-polymorphic.mjs +6 -7
- package/tests/test-lib.mjs +62 -52
- package/tests/test-library-isolation.mjs +349 -26
- package/tests/test-parse-json.mjs +15 -24
- package/tests/test-poly.mjs +21 -16
- package/tests/test-repl-html.mjs +39 -44
- package/tests/test-repl.mjs +46 -59
- package/DOCS/book-combined.md +0 -3786
- package/DOCS/dependency-graph.md +0 -144
- package/Examples/arithmetics.k +0 -108
- package/Examples/esc.k +0 -9
- package/lib/arithmetics.k +0 -286
- package/lib/bit.k +0 -68
- package/lib/bits.k +0 -227
- package/lib/btrie.k +0 -187
- package/lib/list.k +0 -49
- package/lib/string.k +0 -588
- package/tests/test-ieee-text.mjs +0 -74
- package/tests/test-karatsuba.mjs +0 -25
package/lib/ieee.k
CHANGED
|
@@ -1,709 +1,733 @@
|
|
|
1
|
-
# --lib
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
--
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
--
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
un_lt = ?order[] /lt;
|
|
25
|
-
un_eq = ?order[] /eq;
|
|
26
|
-
un_gt = ?order[] /gt;
|
|
27
|
-
|
|
28
|
-
_ = {} |_ ?bits[];
|
|
29
|
-
0b = |0 ?bits[];
|
|
30
|
-
1b = |1 ?bits[];
|
|
31
|
-
un_ = ?bits[] /_;
|
|
32
|
-
un0 = ?bits[] /0;
|
|
33
|
-
un1 = ?bits[] /1;
|
|
34
|
-
_? = un_ _;
|
|
35
|
-
c1 = _ 1b;
|
|
36
|
-
|
|
37
|
-
nil = {} |nil ?list[X];
|
|
38
|
-
cons = |cons ?list[X];
|
|
39
|
-
un_nil = ?list[X] /nil;
|
|
40
|
-
car = ?list[X] /cons .car;
|
|
41
|
-
cdr = ?list[X] /cons .cdr;
|
|
42
|
-
|
|
43
|
-
---------------------------------------------------------------
|
|
44
|
-
-- §2. Patterns & Primitive Constructors/Destructors
|
|
45
|
-
---------------------------------------------------------------
|
|
1
|
+
# --lib core.k --use bit[] bits[]
|
|
2
|
+
|
|
3
|
+
-- =============================================================================
|
|
4
|
+
-- lib/ieee.k -- IEEE-754 binary64 (Double Precision) Structural Model
|
|
5
|
+
--
|
|
6
|
+
-- Self-contained module depending only on core.k. Models double-precision
|
|
7
|
+
-- floating-point numbers as an algebraic record with LSB-first exponent and
|
|
8
|
+
-- significand fields matching codecs/ieee.mjs:
|
|
9
|
+
-- sign[] = < {} "+", {} "-" >
|
|
10
|
+
-- exp11[] = { bit[] "0", ..., bit[] "10" } -- bit "0" is LSB (2^0)
|
|
11
|
+
-- frac52[] = { bit[] "0", ..., bit[] "51" } -- bit "0" is LSB (2^-52)
|
|
12
|
+
-- float64[] = { sign[] sign, exp11[] exponent, frac52[] fraction }
|
|
13
|
+
--
|
|
14
|
+
-- Internal helper names use the `f64_` prefix so lib/ieee.k can be chained
|
|
15
|
+
-- alongside lib/generic.k without name collisions.
|
|
16
|
+
-- Predicates ending in '?' are pure identity filters (?X -> ?X).
|
|
17
|
+
-- All recursive relations are tail-recursive.
|
|
18
|
+
-- =============================================================================
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
-- -----------------------------------------------------------------------------
|
|
22
|
+
-- 1. IEEE-754 binary64 Types & Constants
|
|
23
|
+
-- -----------------------------------------------------------------------------
|
|
46
24
|
|
|
47
25
|
sign[] = < {} "+", {} "-" >;
|
|
48
26
|
|
|
49
27
|
exp11[] = {
|
|
50
|
-
bit[] 0, bit[] 1, bit[] 2, bit[] 3,
|
|
51
|
-
bit[]
|
|
28
|
+
bit[] "0", bit[] "1", bit[] "2", bit[] "3",
|
|
29
|
+
bit[] "4", bit[] "5", bit[] "6", bit[] "7",
|
|
30
|
+
bit[] "8", bit[] "9", bit[] "10"
|
|
52
31
|
};
|
|
53
32
|
|
|
54
33
|
frac52[] = {
|
|
55
|
-
bit[] 0, bit[] 1, bit[] 2, bit[] 3,
|
|
56
|
-
bit[]
|
|
57
|
-
bit[]
|
|
58
|
-
bit[]
|
|
59
|
-
bit[]
|
|
60
|
-
bit[]
|
|
61
|
-
bit[]
|
|
62
|
-
bit[]
|
|
63
|
-
bit[]
|
|
34
|
+
bit[] "0", bit[] "1", bit[] "2", bit[] "3",
|
|
35
|
+
bit[] "4", bit[] "5", bit[] "6", bit[] "7",
|
|
36
|
+
bit[] "8", bit[] "9", bit[] "10", bit[] "11",
|
|
37
|
+
bit[] "12", bit[] "13", bit[] "14", bit[] "15",
|
|
38
|
+
bit[] "16", bit[] "17", bit[] "18", bit[] "19",
|
|
39
|
+
bit[] "20", bit[] "21", bit[] "22", bit[] "23",
|
|
40
|
+
bit[] "24", bit[] "25", bit[] "26", bit[] "27",
|
|
41
|
+
bit[] "28", bit[] "29", bit[] "30", bit[] "31",
|
|
42
|
+
bit[] "32", bit[] "33", bit[] "34", bit[] "35",
|
|
43
|
+
bit[] "36", bit[] "37", bit[] "38", bit[] "39",
|
|
44
|
+
bit[] "40", bit[] "41", bit[] "42", bit[] "43",
|
|
45
|
+
bit[] "44", bit[] "45", bit[] "46", bit[] "47",
|
|
46
|
+
bit[] "48", bit[] "49", bit[] "50", bit[] "51"
|
|
64
47
|
};
|
|
65
48
|
|
|
66
|
-
float64[] = {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
};
|
|
71
|
-
result[] = { string[] rest, float64[] value };
|
|
72
|
-
|
|
73
|
-
digit_value[] = < {} 0, {} 1, {} 2, {} 3, {} 4, {} 5, {} 6, {} 7, {} 8, {} 9 >;
|
|
74
|
-
digits[] = list[digit_value[]];
|
|
75
|
-
factor[] = < {} 2, {} 5 >;
|
|
76
|
-
|
|
77
|
-
-- Primitive constructors and destructors
|
|
78
|
-
+ = {} |+ ?sign[];
|
|
79
|
-
- = {} |- ?sign[];
|
|
80
|
-
un_+ = ?sign[] /+;
|
|
81
|
-
un_- = ?sign[] /-;
|
|
82
|
-
|
|
83
|
-
+? = un_+ +;
|
|
84
|
-
-? = un_- -;
|
|
85
|
-
|
|
86
|
-
d0 = {} |0 ?digit_value[];
|
|
87
|
-
d1 = {} |1 ?digit_value[];
|
|
88
|
-
d2 = {} |2 ?digit_value[];
|
|
89
|
-
d3 = {} |3 ?digit_value[];
|
|
90
|
-
d4 = {} |4 ?digit_value[];
|
|
91
|
-
d5 = {} |5 ?digit_value[];
|
|
92
|
-
d6 = {} |6 ?digit_value[];
|
|
93
|
-
d7 = {} |7 ?digit_value[];
|
|
94
|
-
d8 = {} |8 ?digit_value[];
|
|
95
|
-
d9 = {} |9 ?digit_value[];
|
|
96
|
-
|
|
97
|
-
un_d0 = ?digit_value[] /0;
|
|
98
|
-
un_d1 = ?digit_value[] /1;
|
|
99
|
-
un_d2 = ?digit_value[] /2;
|
|
100
|
-
un_d3 = ?digit_value[] /3;
|
|
101
|
-
un_d4 = ?digit_value[] /4;
|
|
102
|
-
un_d5 = ?digit_value[] /5;
|
|
103
|
-
un_d6 = ?digit_value[] /6;
|
|
104
|
-
un_d7 = ?digit_value[] /7;
|
|
105
|
-
un_d8 = ?digit_value[] /8;
|
|
106
|
-
un_d9 = ?digit_value[] /9;
|
|
107
|
-
|
|
108
|
-
f2 = {} |2 ?factor[];
|
|
109
|
-
f5 = {} |5 ?factor[];
|
|
110
|
-
un_f2 = ?factor[] /2;
|
|
111
|
-
un_f5 = ?factor[] /5;
|
|
112
|
-
|
|
113
|
-
---------------------------------------------------------------
|
|
114
|
-
-- §3. IEEE-754 Classification, Constants & Sign Operations
|
|
115
|
-
---------------------------------------------------------------
|
|
116
|
-
|
|
117
|
-
zero_exponent? = ?exp11[] {
|
|
118
|
-
.0 0? 0, .1 0? 1, .2 0? 2, .3 0? 3, .4 0? 4, .5 0? 5,
|
|
119
|
-
.6 0? 6, .7 0? 7, .8 0? 8, .9 0? 9, .10 0? 10
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
all_ones_exponent? = ?exp11[] {
|
|
123
|
-
.0 1? 0, .1 1? 1, .2 1? 2, .3 1? 3, .4 1? 4, .5 1? 5,
|
|
124
|
-
.6 1? 6, .7 1? 7, .8 1? 8, .9 1? 9, .10 1? 10
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
not_all_ones_exponent? = ?exp11[] {
|
|
128
|
-
<.10 is0, .9 is0, .8 is0, .7 is0, .6 is0, .5 is0, .4 is0, .3 is0, .2 is0, .1 is0, .0 is0> if,
|
|
129
|
-
() then
|
|
130
|
-
} .then;
|
|
131
|
-
|
|
132
|
-
zero_fraction? = ?frac52[] {
|
|
133
|
-
.0 0? 0, .1 0? 1, .2 0? 2, .3 0? 3, .4 0? 4, .5 0? 5, .6 0? 6, .7 0? 7, .8 0? 8, .9 0? 9,
|
|
134
|
-
.10 0? 10, .11 0? 11, .12 0? 12, .13 0? 13, .14 0? 14, .15 0? 15, .16 0? 16, .17 0? 17, .18 0? 18, .19 0? 19,
|
|
135
|
-
.20 0? 20, .21 0? 21, .22 0? 22, .23 0? 23, .24 0? 24, .25 0? 25, .26 0? 26, .27 0? 27, .28 0? 28, .29 0? 29,
|
|
136
|
-
.30 0? 30, .31 0? 31, .32 0? 32, .33 0? 33, .34 0? 34, .35 0? 35, .36 0? 36, .37 0? 37, .38 0? 38, .39 0? 39,
|
|
137
|
-
.40 0? 40, .41 0? 41, .42 0? 42, .43 0? 43, .44 0? 44, .45 0? 45, .46 0? 46, .47 0? 47, .48 0? 48, .49 0? 49,
|
|
138
|
-
.50 0? 50, .51 0? 51
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
nonzero_fraction? = ?frac52[] {
|
|
142
|
-
<.51 is1, .50 is1, .49 is1, .48 is1, .47 is1, .46 is1, .45 is1, .44 is1, .43 is1, .42 is1,
|
|
143
|
-
.41 is1, .40 is1, .39 is1, .38 is1, .37 is1, .36 is1, .35 is1, .34 is1, .33 is1, .32 is1,
|
|
144
|
-
.31 is1, .30 is1, .29 is1, .28 is1, .27 is1, .26 is1, .25 is1, .24 is1, .23 is1, .22 is1,
|
|
145
|
-
.21 is1, .20 is1, .19 is1, .18 is1, .17 is1, .16 is1, .15 is1, .14 is1, .13 is1, .12 is1,
|
|
146
|
-
.11 is1, .10 is1, .9 is1, .8 is1, .7 is1, .6 is1, .5 is1, .4 is1, .3 is1, .2 is1, .1 is1, .0 is1> if,
|
|
147
|
-
() then
|
|
148
|
-
} .then;
|
|
149
|
-
|
|
150
|
-
nan_float64? = ?float64[] {
|
|
151
|
-
.sign sign, .exponent all_ones_exponent? exponent, .fraction nonzero_fraction? fraction
|
|
49
|
+
float64[] = {
|
|
50
|
+
sign[] sign,
|
|
51
|
+
exp11[] exponent,
|
|
52
|
+
frac52[] fraction
|
|
152
53
|
};
|
|
153
54
|
|
|
154
|
-
|
|
155
|
-
|
|
55
|
+
exp0 = {
|
|
56
|
+
{} |0 "0", {} |0 "1", {} |0 "2", {} |0 "3",
|
|
57
|
+
{} |0 "4", {} |0 "5", {} |0 "6", {} |0 "7",
|
|
58
|
+
{} |0 "8", {} |0 "9", {} |0 "10"
|
|
156
59
|
};
|
|
157
60
|
|
|
158
|
-
|
|
159
|
-
|
|
61
|
+
f64_exp_ones = {
|
|
62
|
+
{} |1 "0", {} |1 "1", {} |1 "2", {} |1 "3",
|
|
63
|
+
{} |1 "4", {} |1 "5", {} |1 "6", {} |1 "7",
|
|
64
|
+
{} |1 "8", {} |1 "9", {} |1 "10"
|
|
160
65
|
};
|
|
161
66
|
|
|
162
|
-
zero_float64? = ?float64[] {
|
|
163
|
-
.sign sign, .exponent zero_exponent? exponent, .fraction zero_fraction? fraction
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
exp0 = { 0 0, 0 1, 0 2, 0 3, 0 4, 0 5, 0 6, 0 7, 0 8, 0 9, 0 10 };
|
|
167
|
-
exp_all1 = { 1 0, 1 1, 1 2, 1 3, 1 4, 1 5, 1 6, 1 7, 1 8, 1 9, 1 10 };
|
|
168
|
-
|
|
169
67
|
frac0 = {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
0
|
|
68
|
+
{} |0 "0", {} |0 "1", {} |0 "2", {} |0 "3",
|
|
69
|
+
{} |0 "4", {} |0 "5", {} |0 "6", {} |0 "7",
|
|
70
|
+
{} |0 "8", {} |0 "9", {} |0 "10", {} |0 "11",
|
|
71
|
+
{} |0 "12", {} |0 "13", {} |0 "14", {} |0 "15",
|
|
72
|
+
{} |0 "16", {} |0 "17", {} |0 "18", {} |0 "19",
|
|
73
|
+
{} |0 "20", {} |0 "21", {} |0 "22", {} |0 "23",
|
|
74
|
+
{} |0 "24", {} |0 "25", {} |0 "26", {} |0 "27",
|
|
75
|
+
{} |0 "28", {} |0 "29", {} |0 "30", {} |0 "31",
|
|
76
|
+
{} |0 "32", {} |0 "33", {} |0 "34", {} |0 "35",
|
|
77
|
+
{} |0 "36", {} |0 "37", {} |0 "38", {} |0 "39",
|
|
78
|
+
{} |0 "40", {} |0 "41", {} |0 "42", {} |0 "43",
|
|
79
|
+
{} |0 "44", {} |0 "45", {} |0 "46", {} |0 "47",
|
|
80
|
+
{} |0 "48", {} |0 "49", {} |0 "50", {} |0 "51"
|
|
174
81
|
};
|
|
175
82
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
0
|
|
83
|
+
f64_frac_qnan = {
|
|
84
|
+
{} |0 "0", {} |0 "1", {} |0 "2", {} |0 "3",
|
|
85
|
+
{} |0 "4", {} |0 "5", {} |0 "6", {} |0 "7",
|
|
86
|
+
{} |0 "8", {} |0 "9", {} |0 "10", {} |0 "11",
|
|
87
|
+
{} |0 "12", {} |0 "13", {} |0 "14", {} |0 "15",
|
|
88
|
+
{} |0 "16", {} |0 "17", {} |0 "18", {} |0 "19",
|
|
89
|
+
{} |0 "20", {} |0 "21", {} |0 "22", {} |0 "23",
|
|
90
|
+
{} |0 "24", {} |0 "25", {} |0 "26", {} |0 "27",
|
|
91
|
+
{} |0 "28", {} |0 "29", {} |0 "30", {} |0 "31",
|
|
92
|
+
{} |0 "32", {} |0 "33", {} |0 "34", {} |0 "35",
|
|
93
|
+
{} |0 "36", {} |0 "37", {} |0 "38", {} |0 "39",
|
|
94
|
+
{} |0 "40", {} |0 "41", {} |0 "42", {} |0 "43",
|
|
95
|
+
{} |0 "44", {} |0 "45", {} |0 "46", {} |0 "47",
|
|
96
|
+
{} |0 "48", {} |0 "49", {} |0 "50", {} |1 "51"
|
|
181
97
|
};
|
|
182
98
|
|
|
183
|
-
pos_zero = {
|
|
184
|
-
neg_zero = {
|
|
185
|
-
pos_inf = {
|
|
186
|
-
neg_inf = {
|
|
187
|
-
qnan = {
|
|
99
|
+
pos_zero = { {} |+ sign, exp0 exponent, frac0 fraction };
|
|
100
|
+
neg_zero = { {} |- sign, exp0 exponent, frac0 fraction };
|
|
101
|
+
pos_inf = { {} |+ sign, f64_exp_ones exponent, frac0 fraction };
|
|
102
|
+
neg_inf = { {} |- sign, f64_exp_ones exponent, frac0 fraction };
|
|
103
|
+
qnan = { {} |+ sign, f64_exp_ones exponent, f64_frac_qnan fraction };
|
|
188
104
|
|
|
189
|
-
flip_sign = < un_+ -, un_- + >;
|
|
190
105
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
---------------------------------------------------------------
|
|
195
|
-
-- §4. Exact Decimal Parsing & Natural-to-Float64 Conversion
|
|
196
|
-
---------------------------------------------------------------
|
|
197
|
-
|
|
198
|
-
exp_field = unpack16 {
|
|
199
|
-
.lo.0 0, .lo.1 1, .lo.2 2, .lo.3 3, .lo.4 4, .lo.5 5, .lo.6 6, .lo.7 7,
|
|
200
|
-
.hi.0 8, .hi.1 9, .hi.2 10
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
mantissa = take_byte { .byte b0, .rest take_byte tail }
|
|
204
|
-
{ .b0 b0, .tail.byte b1, .tail.rest take_byte tail }
|
|
205
|
-
{ .b0 b0, .b1 b1, .tail.byte b2, .tail.rest take_byte tail }
|
|
206
|
-
{ .b0 b0, .b1 b1, .b2 b2, .tail.byte b3, .tail.rest take_byte tail }
|
|
207
|
-
{ .b0 b0, .b1 b1, .b2 b2, .b3 b3, .tail.byte b4, .tail.rest take_byte tail }
|
|
208
|
-
{ .b0 b0, .b1 b1, .b2 b2, .b3 b3, .b4 b4, .tail.byte b5, .tail.rest take_nibble tail }
|
|
209
|
-
{ {
|
|
210
|
-
.b0.0 0, .b0.1 1, .b0.2 2, .b0.3 3,
|
|
211
|
-
.b0.4 4, .b0.5 5, .b0.6 6, .b0.7 7,
|
|
212
|
-
.b1.0 8, .b1.1 9, .b1.2 10, .b1.3 11,
|
|
213
|
-
.b1.4 12, .b1.5 13, .b1.6 14, .b1.7 15,
|
|
214
|
-
.b2.0 16, .b2.1 17, .b2.2 18, .b2.3 19,
|
|
215
|
-
.b2.4 20, .b2.5 21, .b2.6 22, .b2.7 23,
|
|
216
|
-
.b3.0 24, .b3.1 25, .b3.2 26, .b3.3 27,
|
|
217
|
-
.b3.4 28, .b3.5 29, .b3.6 30, .b3.7 31,
|
|
218
|
-
.b4.0 32, .b4.1 33, .b4.2 34, .b4.3 35,
|
|
219
|
-
.b4.4 36, .b4.5 37, .b4.6 38, .b4.7 39,
|
|
220
|
-
.b5.0 40, .b5.1 41, .b5.2 42, .b5.3 43,
|
|
221
|
-
.b5.4 44, .b5.5 45, .b5.6 46, .b5.7 47,
|
|
222
|
-
.tail.byte.0 48, .tail.byte.1 49, .tail.byte.2 50, .tail.byte.3 51
|
|
223
|
-
} fraction, .tail.rest lead };
|
|
224
|
-
|
|
225
|
-
c52 = _ 0b 0b 1b 0b 1b 1b;
|
|
226
|
-
c400 = _ 0b 0b 0b 0b 1b 0b 0b 1b 1b;
|
|
227
|
-
c2047 = _ 1b 1b 1b 1b 1b 1b 1b 1b 1b 1b 1b;
|
|
228
|
-
|
|
229
|
-
integer_digits = ?decimal[] <
|
|
230
|
-
{ .rest scan_digit digit, () decimal } {
|
|
231
|
-
.digit.rest rest,
|
|
232
|
-
{ .decimal.n tenfold x, .digit.value y } plus n,
|
|
233
|
-
.decimal.d d, .decimal.count inc count
|
|
234
|
-
} integer_digits,
|
|
235
|
-
()
|
|
236
|
-
>;
|
|
237
|
-
|
|
238
|
-
fraction_first = ?decimal[] {
|
|
239
|
-
.rest dot_char scan_digit digit, () decimal
|
|
240
|
-
} {
|
|
241
|
-
.digit.rest rest,
|
|
242
|
-
{ .decimal.n tenfold x, .digit.value y } plus n,
|
|
243
|
-
.decimal.d tenfold d, .decimal.count inc count
|
|
244
|
-
};
|
|
106
|
+
-- -----------------------------------------------------------------------------
|
|
107
|
+
-- 2. Classification Filters & Sign Negation
|
|
108
|
+
-- -----------------------------------------------------------------------------
|
|
245
109
|
|
|
246
|
-
|
|
247
|
-
{ .
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
} fraction_digits,
|
|
252
|
-
()
|
|
110
|
+
f64_bit_or = <
|
|
111
|
+
{ .a /0 _a, .b /0 _b } {} |0,
|
|
112
|
+
{ .a /0 _a, .b /1 _b } {} |1,
|
|
113
|
+
{ .a /1 _a, .b /0 _b } {} |1,
|
|
114
|
+
{ .a /1 _a, .b /1 _b } {} |1
|
|
253
115
|
>;
|
|
254
116
|
|
|
255
|
-
|
|
256
|
-
.
|
|
257
|
-
}
|
|
258
|
-
{ .
|
|
259
|
-
|
|
260
|
-
> < fraction_first fraction_digits, () >;
|
|
261
|
-
|
|
262
|
-
exponent_digits = ?digit_result[] <
|
|
263
|
-
{ .rest scan_digit next, .value tenfold high }
|
|
264
|
-
{ .next.rest rest, { .high x, .next.value y } plus value }
|
|
265
|
-
exponent_digits,
|
|
266
|
-
()
|
|
117
|
+
f64_bit_and = <
|
|
118
|
+
{ .a /1 _a, .b /1 _b } {} |1,
|
|
119
|
+
{ .a /0 _a, .b /0 _b } {} |0,
|
|
120
|
+
{ .a /0 _a, .b /1 _b } {} |0,
|
|
121
|
+
{ .a /1 _a, .b /0 _b } {} |0
|
|
267
122
|
>;
|
|
268
123
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
124
|
+
f64_or4 = {
|
|
125
|
+
{ .0 a, .1 b } f64_bit_or lo,
|
|
126
|
+
{ .2 a, .3 b } f64_bit_or hi
|
|
127
|
+
} { .lo a, .hi b } f64_bit_or;
|
|
128
|
+
|
|
129
|
+
f64_and4 = {
|
|
130
|
+
{ .0 a, .1 b } f64_bit_and lo,
|
|
131
|
+
{ .2 a, .3 b } f64_bit_and hi
|
|
132
|
+
} { .lo a, .hi b } f64_bit_and;
|
|
133
|
+
|
|
134
|
+
f64_exp_any_one = {
|
|
135
|
+
{ ."0" "0", ."1" "1", ."2" "2", ."3" "3" } f64_or4 g0,
|
|
136
|
+
{ ."4" "0", ."5" "1", ."6" "2", ."7" "3" } f64_or4 g1,
|
|
137
|
+
{ ."8" "0", ."9" "1", ."10" "2", {} |0 "3" } f64_or4 g2
|
|
138
|
+
} { .g0 a, { .g1 a, .g2 b } f64_bit_or b } f64_bit_or;
|
|
139
|
+
|
|
140
|
+
f64_exp_all_ones = {
|
|
141
|
+
{ ."0" "0", ."1" "1", ."2" "2", ."3" "3" } f64_and4 g0,
|
|
142
|
+
{ ."4" "0", ."5" "1", ."6" "2", ."7" "3" } f64_and4 g1,
|
|
143
|
+
{ ."8" "0", ."9" "1", ."10" "2", {} |1 "3" } f64_and4 g2
|
|
144
|
+
} { .g0 a, { .g1 a, .g2 b } f64_bit_and b } f64_bit_and;
|
|
145
|
+
|
|
146
|
+
f64_frac_any_one = {
|
|
147
|
+
{ ."0" "0", ."1" "1", ."2" "2", ."3" "3" } f64_or4 g0,
|
|
148
|
+
{ ."4" "0", ."5" "1", ."6" "2", ."7" "3" } f64_or4 g1,
|
|
149
|
+
{ ."8" "0", ."9" "1", ."10" "2", ."11" "3" } f64_or4 g2,
|
|
150
|
+
{ ."12" "0", ."13" "1", ."14" "2", ."15" "3" } f64_or4 g3,
|
|
151
|
+
{ ."16" "0", ."17" "1", ."18" "2", ."19" "3" } f64_or4 g4,
|
|
152
|
+
{ ."20" "0", ."21" "1", ."22" "2", ."23" "3" } f64_or4 g5,
|
|
153
|
+
{ ."24" "0", ."25" "1", ."26" "2", ."27" "3" } f64_or4 g6,
|
|
154
|
+
{ ."28" "0", ."29" "1", ."30" "2", ."31" "3" } f64_or4 g7,
|
|
155
|
+
{ ."32" "0", ."33" "1", ."34" "2", ."35" "3" } f64_or4 g8,
|
|
156
|
+
{ ."36" "0", ."37" "1", ."38" "2", ."39" "3" } f64_or4 g9,
|
|
157
|
+
{ ."40" "0", ."41" "1", ."42" "2", ."43" "3" } f64_or4 g10,
|
|
158
|
+
{ ."44" "0", ."45" "1", ."46" "2", ."47" "3" } f64_or4 g11,
|
|
159
|
+
{ ."48" "0", ."49" "1", ."50" "2", ."51" "3" } f64_or4 g12
|
|
160
|
+
} {
|
|
161
|
+
{ .g0 "0", .g1 "1", .g2 "2", .g3 "3" } f64_or4 a,
|
|
162
|
+
{ .g4 "0", .g5 "1", .g6 "2", .g7 "3" } f64_or4 b,
|
|
163
|
+
{ .g8 "0", .g9 "1", .g10 "2", .g11 "3" } f64_or4 c,
|
|
164
|
+
.g12 d
|
|
165
|
+
} {
|
|
166
|
+
{ .a a, .b b } f64_bit_or a,
|
|
167
|
+
{ .c a, .d b } f64_bit_or b
|
|
168
|
+
} f64_bit_or;
|
|
277
169
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
{ .n n, .d tenfold d, .power dec power, .direction -? direction }
|
|
283
|
-
> scale_decimal
|
|
284
|
-
>;
|
|
170
|
+
zero_exponent? = ?exp11[] {
|
|
171
|
+
f64_exp_any_one /0 _z,
|
|
172
|
+
() then
|
|
173
|
+
} .then ?exp11[];
|
|
285
174
|
|
|
286
|
-
|
|
175
|
+
all_ones_exponent? = ?exp11[] {
|
|
176
|
+
f64_exp_all_ones /1 _o,
|
|
177
|
+
() then
|
|
178
|
+
} .then ?exp11[];
|
|
287
179
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
.
|
|
292
|
-
<
|
|
293
|
-
{ .order un_gt if, .state.acc inc_lsb then } .then,
|
|
294
|
-
{ .order un_eq a, .state.acc un1 b, .state.acc inc_lsb then } .then,
|
|
295
|
-
.state.acc
|
|
296
|
-
> mantissa mantissa
|
|
297
|
-
} {
|
|
298
|
-
.mantissa.fraction fraction,
|
|
299
|
-
<
|
|
300
|
-
{ .exponent un_ a, .mantissa.lead un1 b, c1 then } .then,
|
|
301
|
-
{ .mantissa.lead un0 un1 un_ if, .exponent inc then } .then,
|
|
302
|
-
.exponent
|
|
303
|
-
> exponent
|
|
304
|
-
} {
|
|
305
|
-
() fields,
|
|
306
|
-
{ .exponent x, c2047 y } cmp order
|
|
180
|
+
-- Classifies float64[] exponent into < zero, normal, ones >.
|
|
181
|
+
f64_classify_exp = {
|
|
182
|
+
.exponent f64_exp_any_one any1,
|
|
183
|
+
.exponent f64_exp_all_ones all1
|
|
307
184
|
} <
|
|
308
|
-
{ .
|
|
309
|
-
{
|
|
185
|
+
{ .any1 /0 _a, .all1 _b } {} |zero,
|
|
186
|
+
{ .any1 /1 _a, .all1 /0 _b } {} |normal,
|
|
187
|
+
{ .any1 /1 _a, .all1 /1 _b } {} |ones
|
|
310
188
|
>;
|
|
311
189
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
.d d, .exponent exponent, .acc 0b acc, .left left
|
|
318
|
-
},
|
|
319
|
-
{
|
|
320
|
-
{ .n x, .d y } minus n,
|
|
321
|
-
.d d, .exponent exponent, .acc 1b acc, .left left
|
|
322
|
-
}
|
|
323
|
-
> fraction_loop
|
|
324
|
-
>;
|
|
190
|
+
zero_float64? = ?float64[] {
|
|
191
|
+
f64_classify_exp /zero _e,
|
|
192
|
+
.fraction f64_frac_any_one /0 _f,
|
|
193
|
+
() then
|
|
194
|
+
} .then ?float64[];
|
|
325
195
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
{ .n n, .d d, _ exponent, _ acc, c52 left } fraction_loop then
|
|
331
|
-
} .then,
|
|
332
|
-
{
|
|
333
|
-
ratio_less? if,
|
|
334
|
-
{
|
|
335
|
-
{ .n twice x, .d y } minus n,
|
|
336
|
-
.d d, .exponent dec exponent, c1 acc, c52 left
|
|
337
|
-
} fraction_loop then
|
|
338
|
-
} .then,
|
|
339
|
-
{
|
|
340
|
-
{ .n x, .d y } minus n,
|
|
341
|
-
.d d, .exponent exponent, c1 acc, c52 left
|
|
342
|
-
} fraction_loop
|
|
343
|
-
>;
|
|
196
|
+
finite_float64? = ?float64[] {
|
|
197
|
+
f64_classify_exp < /zero, /normal > _e,
|
|
198
|
+
() then
|
|
199
|
+
} .then ?float64[];
|
|
344
200
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
{
|
|
351
|
-
.exponent un1 un1 un1 un1 un1 un1 un1 un1 un1 un1 un1 un_ if,
|
|
352
|
-
{ exp_all1 exponent, frac0 fraction } then
|
|
353
|
-
} .then,
|
|
354
|
-
{ .n n, .denominator 0b denominator, .steps <un0, un1> steps, .exponent inc exponent } align_down
|
|
355
|
-
>;
|
|
201
|
+
inf_float64? = ?float64[] {
|
|
202
|
+
f64_classify_exp /ones _e,
|
|
203
|
+
.fraction f64_frac_any_one /0 _f,
|
|
204
|
+
() then
|
|
205
|
+
} .then ?float64[];
|
|
356
206
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
{ .numerator 0b numerator, .d d, .steps <un0, un1> steps, .exponent dec exponent } align_up
|
|
363
|
-
>;
|
|
207
|
+
nan_float64? = ?float64[] {
|
|
208
|
+
f64_classify_exp /ones _e,
|
|
209
|
+
.fraction f64_frac_any_one /1 _f,
|
|
210
|
+
() then
|
|
211
|
+
} .then ?float64[];
|
|
364
212
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
} .then,
|
|
371
|
-
{
|
|
372
|
-
.x un_ if,
|
|
373
|
-
{ .n inv numerator, .d d, .y steps, .exponent exponent } align_up then
|
|
374
|
-
} .then,
|
|
375
|
-
{ .n n, .d d, .x <un0, un1> x, .y <un0, un1> y, .exponent exponent } align
|
|
376
|
-
>;
|
|
213
|
+
negate_float64 = ?float64[] {
|
|
214
|
+
.sign < /+ {} |-, /- {} |+ > sign,
|
|
215
|
+
.exponent exponent,
|
|
216
|
+
.fraction fraction
|
|
217
|
+
} ?float64[];
|
|
377
218
|
|
|
378
|
-
ratio = {
|
|
379
|
-
.n n, .d d, .n x, .d y, c1023 exponent
|
|
380
|
-
} align;
|
|
381
219
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
() decimal,
|
|
386
|
-
{ .exponent.value x, { .count x, c400 y } plus y } cmp order
|
|
387
|
-
} <
|
|
388
|
-
{
|
|
389
|
-
.order un_gt a, .decimal.exponent.sign un_- b,
|
|
390
|
-
.decimal signed_zero then
|
|
391
|
-
} .then,
|
|
392
|
-
{
|
|
393
|
-
.order un_gt a, .decimal.exponent.sign un_+ b,
|
|
394
|
-
{ .decimal.sign sign, exp_all1 exponent, frac0 fraction } then
|
|
395
|
-
} .then,
|
|
396
|
-
{
|
|
397
|
-
.order <un_lt, un_eq> if,
|
|
398
|
-
{
|
|
399
|
-
{
|
|
400
|
-
.decimal.n n, .decimal.d d,
|
|
401
|
-
.decimal.exponent.value power, .decimal.exponent.sign direction
|
|
402
|
-
} scale_decimal ratio fields,
|
|
403
|
-
.decimal.sign sign
|
|
404
|
-
} { .sign sign, .fields.exponent exponent, .fields.fraction fraction } then
|
|
405
|
-
} .then
|
|
406
|
-
>
|
|
407
|
-
>;
|
|
220
|
+
-- -----------------------------------------------------------------------------
|
|
221
|
+
-- 3. Internal Small-Integer & Bit-Vector Arithmetic (f64_*)
|
|
222
|
+
-- -----------------------------------------------------------------------------
|
|
408
223
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
} {
|
|
412
|
-
.decimal decimal, .sign sign,
|
|
413
|
-
< .decimal.rest exponent_part, { .decimal.rest rest, _ value, + sign } > exponent
|
|
414
|
-
} {
|
|
415
|
-
.exponent.rest rest,
|
|
416
|
-
{
|
|
417
|
-
.decimal.n n, .decimal.d d, .decimal.count count,
|
|
418
|
-
.sign sign, .exponent exponent
|
|
419
|
-
} decimal_float value
|
|
420
|
-
};
|
|
224
|
+
f64_order[] = < {} lt, {} eq, {} gt >;
|
|
225
|
+
f64_int[] = < bits[] "+", bits[] "-" >;
|
|
421
226
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
227
|
+
f64_trim_msb = ?bits[] <
|
|
228
|
+
/_ |_ |0,
|
|
229
|
+
/1 |1,
|
|
230
|
+
/0 /_ |_ |0,
|
|
231
|
+
/0 f64_trim_msb
|
|
232
|
+
> ?bits[];
|
|
426
233
|
|
|
427
|
-
|
|
428
|
-
|
|
234
|
+
f64_msb_drop_zeros = ?bits[] <
|
|
235
|
+
/_ |_,
|
|
236
|
+
/1 |1,
|
|
237
|
+
/0 f64_msb_drop_zeros
|
|
238
|
+
> ?bits[];
|
|
429
239
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
240
|
+
f64_rev_step = ?{ bits[] in, bits[] acc } <
|
|
241
|
+
{ .in /_ _i, .acc acc } .acc,
|
|
242
|
+
<
|
|
243
|
+
{ .in /0 in, .acc |0 acc },
|
|
244
|
+
{ .in /1 in, .acc |1 acc }
|
|
245
|
+
> f64_rev_step
|
|
246
|
+
> ?bits[];
|
|
436
247
|
|
|
437
|
-
|
|
438
|
-
plus_char { () rest, + sign } signed_decimal,
|
|
439
|
-
number,
|
|
440
|
-
special
|
|
441
|
-
>;
|
|
248
|
+
f64_rev_bits = ?bits[] { () in, {} |_ acc } f64_rev_step ?bits[];
|
|
442
249
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
250
|
+
f64_lsb_trim = ?bits[] f64_rev_bits f64_msb_drop_zeros f64_rev_bits ?bits[];
|
|
251
|
+
f64_msb_to_lsb = ?bits[] f64_msb_drop_zeros f64_rev_bits ?bits[];
|
|
252
|
+
f64_lsb_to_msb = ?bits[] f64_rev_bits f64_trim_msb ?bits[];
|
|
446
253
|
|
|
447
|
-
|
|
448
|
-
un_ pos_zero,
|
|
449
|
-
{ () n, c1 d } ratio { + sign, .exponent exponent, .fraction fraction }
|
|
450
|
-
>;
|
|
254
|
+
f64_lsb_shl1 = < /_ |_, /0 |0 |0, /1 |1 |0 >;
|
|
451
255
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
zero_text = { _0_ car, nil cdr } cons;
|
|
457
|
-
negative_zero_text = { _minus_ car, zero_text cdr } cons;
|
|
458
|
-
infinity_text = {
|
|
459
|
-
_I_ car, { _n_ car, { _f_ car, { _i_ car, {
|
|
460
|
-
_n_ car, { _i_ car, { _t_ car, { _y_ car, nil cdr } cons cdr } cons cdr } cons cdr
|
|
461
|
-
} cons cdr } cons cdr } cons cdr } cons cdr
|
|
462
|
-
} cons;
|
|
463
|
-
negative_infinity_text = { _minus_ car, infinity_text cdr } cons;
|
|
464
|
-
nan_text = { _N_ car, { _a_ car, { _N_ car, nil cdr } cons cdr } cons cdr } cons;
|
|
465
|
-
|
|
466
|
-
c1074 = _ 0b 1b 0b 0b 1b 1b 0b 0b 0b 0b 1b;
|
|
467
|
-
c1075 = _ 1b 1b 0b 0b 1b 1b 0b 0b 0b 0b 1b;
|
|
468
|
-
|
|
469
|
-
exponent_bits = { () source, _ acc }
|
|
470
|
-
{ .source source,
|
|
471
|
-
{ { .source.0 0, .source.1 1, .source.2 2, .source.3 3 } x, .acc acc } push_bits4 acc
|
|
472
|
-
}
|
|
473
|
-
{ .source source,
|
|
474
|
-
{ { .source.4 0, .source.5 1, .source.6 2, .source.7 3 } x, .acc acc } push_bits4 acc
|
|
475
|
-
}
|
|
476
|
-
{ .source source,
|
|
477
|
-
{ { .source.8 0, .source.9 1, .source.10 2 } x, .acc acc } push_bits3 acc
|
|
478
|
-
}
|
|
479
|
-
.acc;
|
|
480
|
-
|
|
481
|
-
fraction_bits = { () source, _ acc }
|
|
482
|
-
{ .source source,
|
|
483
|
-
{ { .source.0 0, .source.1 1, .source.2 2, .source.3 3 } x, .acc acc } push_bits4 acc
|
|
484
|
-
}
|
|
485
|
-
{ .source source,
|
|
486
|
-
{ { .source.4 0, .source.5 1, .source.6 2, .source.7 3 } x, .acc acc } push_bits4 acc
|
|
487
|
-
}
|
|
488
|
-
{ .source source,
|
|
489
|
-
{ { .source.8 0, .source.9 1, .source.10 2, .source.11 3 } x, .acc acc } push_bits4 acc
|
|
490
|
-
}
|
|
491
|
-
{ .source source,
|
|
492
|
-
{ { .source.12 0, .source.13 1, .source.14 2, .source.15 3 } x, .acc acc } push_bits4 acc
|
|
493
|
-
}
|
|
494
|
-
{ .source source,
|
|
495
|
-
{ { .source.16 0, .source.17 1, .source.18 2, .source.19 3 } x, .acc acc } push_bits4 acc
|
|
496
|
-
}
|
|
497
|
-
{ .source source,
|
|
498
|
-
{ { .source.20 0, .source.21 1, .source.22 2, .source.23 3 } x, .acc acc } push_bits4 acc
|
|
499
|
-
}
|
|
500
|
-
{ .source source,
|
|
501
|
-
{ { .source.24 0, .source.25 1, .source.26 2, .source.27 3 } x, .acc acc } push_bits4 acc
|
|
502
|
-
}
|
|
503
|
-
{ .source source,
|
|
504
|
-
{ { .source.28 0, .source.29 1, .source.30 2, .source.31 3 } x, .acc acc } push_bits4 acc
|
|
505
|
-
}
|
|
506
|
-
{ .source source,
|
|
507
|
-
{ { .source.32 0, .source.33 1, .source.34 2, .source.35 3 } x, .acc acc } push_bits4 acc
|
|
508
|
-
}
|
|
509
|
-
{ .source source,
|
|
510
|
-
{ { .source.36 0, .source.37 1, .source.38 2, .source.39 3 } x, .acc acc } push_bits4 acc
|
|
511
|
-
}
|
|
512
|
-
{ .source source,
|
|
513
|
-
{ { .source.40 0, .source.41 1, .source.42 2, .source.43 3 } x, .acc acc } push_bits4 acc
|
|
514
|
-
}
|
|
515
|
-
{ .source source,
|
|
516
|
-
{ { .source.44 0, .source.45 1, .source.46 2, .source.47 3 } x, .acc acc } push_bits4 acc
|
|
517
|
-
}
|
|
518
|
-
{ .source source,
|
|
519
|
-
{ { .source.48 0, .source.49 1, .source.50 2, .source.51 3 } x, .acc acc } push_bits4 acc
|
|
520
|
-
}
|
|
521
|
-
.acc;
|
|
522
|
-
|
|
523
|
-
digit_ascii = <
|
|
524
|
-
un_d0 _0_, un_d1 _1_, un_d2 _2_, un_d3 _3_, un_d4 _4_,
|
|
525
|
-
un_d5 _5_, un_d6 _6_, un_d7 _7_, un_d8 _8_, un_d9 _9_
|
|
256
|
+
f64_pop1 = <
|
|
257
|
+
/_ { {} |0 b, {} |_ r },
|
|
258
|
+
/0 { {} |0 b, () r },
|
|
259
|
+
/1 { {} |1 b, () r }
|
|
526
260
|
>;
|
|
527
261
|
|
|
528
|
-
|
|
529
|
-
{ .
|
|
530
|
-
{ .
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
{ .
|
|
535
|
-
{ .
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
{ .
|
|
542
|
-
{ .
|
|
543
|
-
{ .
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
262
|
+
f64_push_bit_acc = ?{ bit[] b, bits[] acc } <
|
|
263
|
+
{ .b /0 _, .acc acc } .acc |0,
|
|
264
|
+
{ .b /1 _, .acc acc } .acc |1
|
|
265
|
+
> ?bits[];
|
|
266
|
+
|
|
267
|
+
f64_prepend_zeros = ?{ bits[] zeros, bits[] tail } <
|
|
268
|
+
{ .zeros /_ _, .tail tail } .tail,
|
|
269
|
+
{ .zeros < /0, /1 > zeros, .tail tail } {
|
|
270
|
+
.zeros zeros, .tail |0 tail
|
|
271
|
+
} f64_prepend_zeros
|
|
272
|
+
> ?bits[];
|
|
273
|
+
|
|
274
|
+
f64_lsb_inc_step = ?{ bits[] in, bits[] zeros } <
|
|
275
|
+
{ .in /_ _, .zeros zeros } { .zeros zeros, {} |_ |1 tail },
|
|
276
|
+
{ .in /0 in, .zeros zeros } { .zeros zeros, .in |1 tail },
|
|
277
|
+
{ .in /1 in, .zeros zeros } { .in in, .zeros |0 zeros } f64_lsb_inc_step
|
|
278
|
+
> ?{ bits[] zeros, bits[] tail };
|
|
279
|
+
|
|
280
|
+
f64_lsb_inc = ?bits[] {
|
|
281
|
+
() in, {} |_ zeros
|
|
282
|
+
} f64_lsb_inc_step f64_prepend_zeros ?bits[];
|
|
283
|
+
|
|
284
|
+
f64_full_add_bit = ?{ bit[] a, bit[] b, bit[] c } <
|
|
285
|
+
{ .a /0 _a, .b /0 _b, .c /0 _c } { {} |0 sum, {} |0 carry },
|
|
286
|
+
{ .a /0 _a, .b /0 _b, .c /1 _c } { {} |1 sum, {} |0 carry },
|
|
287
|
+
{ .a /0 _a, .b /1 _b, .c /0 _c } { {} |1 sum, {} |0 carry },
|
|
288
|
+
{ .a /0 _a, .b /1 _b, .c /1 _c } { {} |0 sum, {} |1 carry },
|
|
289
|
+
{ .a /1 _a, .b /0 _b, .c /0 _c } { {} |1 sum, {} |0 carry },
|
|
290
|
+
{ .a /1 _a, .b /0 _b, .c /1 _c } { {} |0 sum, {} |1 carry },
|
|
291
|
+
{ .a /1 _a, .b /1 _b, .c /0 _c } { {} |0 sum, {} |1 carry },
|
|
292
|
+
{ .a /1 _a, .b /1 _b, .c /1 _c } { {} |1 sum, {} |1 carry }
|
|
293
|
+
> ?{ bit[] sum, bit[] carry };
|
|
294
|
+
|
|
295
|
+
f64_full_sub_bit = ?{ bit[] a, bit[] b, bit[] c } <
|
|
296
|
+
{ .a /0 _a, .b /0 _b, .c /0 _c } { {} |0 diff, {} |0 borrow },
|
|
297
|
+
{ .a /0 _a, .b /0 _b, .c /1 _c } { {} |1 diff, {} |1 borrow },
|
|
298
|
+
{ .a /0 _a, .b /1 _b, .c /0 _c } { {} |1 diff, {} |1 borrow },
|
|
299
|
+
{ .a /0 _a, .b /1 _b, .c /1 _c } { {} |0 diff, {} |1 borrow },
|
|
300
|
+
{ .a /1 _a, .b /0 _b, .c /0 _c } { {} |1 diff, {} |0 borrow },
|
|
301
|
+
{ .a /1 _a, .b /0 _b, .c /1 _c } { {} |0 diff, {} |0 borrow },
|
|
302
|
+
{ .a /1 _a, .b /1 _b, .c /0 _c } { {} |0 diff, {} |0 borrow },
|
|
303
|
+
{ .a /1 _a, .b /1 _b, .c /1 _c } { {} |1 diff, {} |1 borrow }
|
|
304
|
+
> ?{ bit[] diff, bit[] borrow };
|
|
305
|
+
|
|
306
|
+
f64_lsb_add_step = ?{ bits[] x, bits[] y, bit[] c, bits[] acc } <
|
|
307
|
+
{ .x /_ _x, .y /_ _y, .c /0 _c, .acc acc } .acc,
|
|
308
|
+
{ .x /_ _x, .y /_ _y, .c /1 _c, .acc acc } .acc |1,
|
|
309
|
+
{ .x /_ _x, .y < /0 |0, /1 |1 > y, .c /0 _c, .acc acc }
|
|
310
|
+
{ .y in, .acc acc } f64_rev_step,
|
|
311
|
+
{ .x < /0 |0, /1 |1 > x, .y /_ _y, .c /0 _c, .acc acc }
|
|
312
|
+
{ .x in, .acc acc } f64_rev_step,
|
|
313
|
+
{ .x f64_pop1 px, .y f64_pop1 py, .c c, .acc acc } {
|
|
314
|
+
.px .r x,
|
|
315
|
+
.py .r y,
|
|
316
|
+
{ .px .b a, .py .b b, .c c } f64_full_add_bit s,
|
|
317
|
+
.acc acc
|
|
318
|
+
} {
|
|
319
|
+
.x x,
|
|
320
|
+
.y y,
|
|
321
|
+
.s .carry c,
|
|
322
|
+
{ .s .sum b, .acc acc } f64_push_bit_acc acc
|
|
323
|
+
} f64_lsb_add_step
|
|
324
|
+
> ?bits[];
|
|
325
|
+
|
|
326
|
+
f64_lsb_add = ?{ bits[] x, bits[] y } {
|
|
327
|
+
.x x, .y y, {} |0 c, {} |_ acc
|
|
328
|
+
} f64_lsb_add_step f64_msb_drop_zeros f64_rev_bits ?bits[];
|
|
329
|
+
|
|
330
|
+
f64_lsb_sub_step = ?{ bits[] x, bits[] y, bit[] b, bits[] acc } <
|
|
331
|
+
{ .x /_ _x, .y /_ _y, .b /0 _b, .acc acc }
|
|
332
|
+
{ {} |0 b, .acc f64_msb_drop_zeros f64_rev_bits r },
|
|
333
|
+
{ .x /_ _x, .y /_ _y, .b /1 _b, .acc _a }
|
|
334
|
+
{ {} |1 b, {} |_ r },
|
|
335
|
+
{ .x /_ _x, .y < /0, /1 > _y, .b _b, .acc _a }
|
|
336
|
+
{ {} |1 b, {} |_ r },
|
|
337
|
+
{ .x < /0 |0, /1 |1 > x, .y /_ _y, .b /0 _b, .acc acc } {
|
|
338
|
+
{} |0 b,
|
|
339
|
+
{ .x in, .acc acc } f64_rev_step f64_msb_drop_zeros f64_rev_bits r
|
|
340
|
+
},
|
|
341
|
+
{ .x < /0 |0, /1 |1 > f64_pop1 px, .y f64_pop1 py, .b b, .acc acc } {
|
|
342
|
+
.px .r x,
|
|
343
|
+
.py .r y,
|
|
344
|
+
{ .px .b a, .py .b b, .b c } f64_full_sub_bit s,
|
|
345
|
+
.acc acc
|
|
346
|
+
} {
|
|
347
|
+
.x x,
|
|
348
|
+
.y y,
|
|
349
|
+
.s .borrow b,
|
|
350
|
+
{ .s .diff b, .acc acc } f64_push_bit_acc acc
|
|
351
|
+
} f64_lsb_sub_step
|
|
352
|
+
> ?{ bit[] b, bits[] r };
|
|
353
|
+
|
|
354
|
+
f64_lsb_sub = ?{ bits[] x, bits[] y }
|
|
355
|
+
{ .x x, .y y, {} |0 b, {} |_ acc } f64_lsb_sub_step { .b /0 _b, .r r } .r ?bits[];
|
|
356
|
+
|
|
357
|
+
f64_lsb_try_sub = ?{ bits[] x, bits[] y }
|
|
358
|
+
{ .x x, .y y, {} |0 b, {} |_ acc } f64_lsb_sub_step ?{ bit[] b, bits[] r };
|
|
359
|
+
|
|
360
|
+
f64_ord_if_eq = <
|
|
361
|
+
{ .ord /eq _o, .next next } .next,
|
|
362
|
+
{ .ord /lt _o, .next _n } {} |lt,
|
|
363
|
+
{ .ord /gt _o, .next _n } {} |gt
|
|
549
364
|
>;
|
|
550
365
|
|
|
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
|
-
{ .digit un_d9 a, .carry un_d1 b } { d6 digit, d4 carry },
|
|
599
|
-
{ .digit un_d9 a, .carry un_d2 b } { d7 digit, d4 carry },
|
|
600
|
-
{ .digit un_d9 a, .carry un_d3 b } { d8 digit, d4 carry },
|
|
601
|
-
{ .digit un_d9 a, .carry un_d4 b } { d9 digit, d4 carry }
|
|
366
|
+
f64_cmp_msb_step = ?{ bits[] x, bits[] y, f64_order[] ord } <
|
|
367
|
+
{ .x /_ _x, .y /_ _y, .ord ord } .ord,
|
|
368
|
+
{ .x /_ _x, .y /0 _y, .ord _o } {} |lt,
|
|
369
|
+
{ .x /_ _x, .y /1 _y, .ord _o } {} |lt,
|
|
370
|
+
{ .x /0 _x, .y /_ _y, .ord _o } {} |gt,
|
|
371
|
+
{ .x /1 _x, .y /_ _y, .ord _o } {} |gt,
|
|
372
|
+
<
|
|
373
|
+
{ .x /0 x, .y /0 y, .ord ord },
|
|
374
|
+
{ .x /1 x, .y /1 y, .ord ord },
|
|
375
|
+
{ .x /0 x, .y /1 y, .ord ord }
|
|
376
|
+
{ .x x, .y y, { .ord ord, {} |lt next } f64_ord_if_eq ord },
|
|
377
|
+
{ .x /1 x, .y /0 y, .ord ord }
|
|
378
|
+
{ .x x, .y y, { .ord ord, {} |gt next } f64_ord_if_eq ord }
|
|
379
|
+
> f64_cmp_msb_step
|
|
380
|
+
> ?f64_order[];
|
|
381
|
+
|
|
382
|
+
f64_cmp_msb = { .x x, .y y, {} |eq ord } f64_cmp_msb_step;
|
|
383
|
+
f64_lsb_cmp = { .x f64_lsb_to_msb x, .y f64_lsb_to_msb y } f64_cmp_msb;
|
|
384
|
+
|
|
385
|
+
-- Shifts trimmed LSB-first bits[] left by an unsigned LSB bit-count `n`.
|
|
386
|
+
f64_lsb_shl_n = ?{ bits[] x, bits[] n } <
|
|
387
|
+
{ .x /_ _x, .n _n } {} |_,
|
|
388
|
+
{ .x /0 x, .n /_ _n } .x |0,
|
|
389
|
+
{ .x /1 x, .n /_ _n } .x |1,
|
|
390
|
+
<
|
|
391
|
+
{ .x /0 x, .n n } { .x |0 |0 x, { .n x, {} |_ |1 y } f64_lsb_sub n },
|
|
392
|
+
{ .x /1 x, .n n } { .x |1 |0 x, { .n x, {} |_ |1 y } f64_lsb_sub n }
|
|
393
|
+
> f64_lsb_shl_n
|
|
394
|
+
> ?bits[];
|
|
395
|
+
|
|
396
|
+
-- Signed small-integer helpers (canonical MSB f64_int[]) for IEEE exponent math.
|
|
397
|
+
f64_izero = {} |_ |0 |+;
|
|
398
|
+
f64_norm_int = ?f64_int[] <
|
|
399
|
+
/+ f64_trim_msb |+,
|
|
400
|
+
/- f64_trim_msb < /0 /_ |_ |0 |+, /1 |1 |- >
|
|
401
|
+
> ?f64_int[];
|
|
402
|
+
|
|
403
|
+
f64_i_sub_msb = {
|
|
404
|
+
.x x,
|
|
405
|
+
.y y,
|
|
406
|
+
{ .x x, .y y } f64_cmp_msb ord
|
|
407
|
+
} <
|
|
408
|
+
{ .ord /eq _o, .x _x, .y _y } f64_izero,
|
|
409
|
+
{ .ord /gt _o, .x x, .y y }
|
|
410
|
+
{ .x f64_msb_to_lsb x, .y f64_msb_to_lsb y } f64_lsb_sub f64_lsb_to_msb |+,
|
|
411
|
+
{ .ord /lt _o, .x x, .y y }
|
|
412
|
+
{ .y f64_msb_to_lsb x, .x f64_msb_to_lsb y } f64_lsb_sub f64_lsb_to_msb |-
|
|
602
413
|
>;
|
|
603
414
|
|
|
604
|
-
|
|
605
|
-
{ .x
|
|
606
|
-
{ .x
|
|
607
|
-
{
|
|
608
|
-
|
|
609
|
-
{ .x car digit, .carry carry, .factor factor } <
|
|
610
|
-
{ .factor un_f2 if, { .digit digit, .carry carry } digit_mul2 then } .then,
|
|
611
|
-
{ .factor un_f5 if, { .digit digit, .carry carry } digit_mul5 then } .then
|
|
612
|
-
> next
|
|
613
|
-
} { .x x, .factor factor, .next.carry carry, { .next.digit car, .acc cdr } cons acc }
|
|
614
|
-
digits_multiply_loop
|
|
415
|
+
f64_i_plus = { .x f64_norm_int x, .y f64_norm_int y } <
|
|
416
|
+
{ .x /+ x, .y /+ y } { .x f64_msb_to_lsb x, .y f64_msb_to_lsb y } f64_lsb_add f64_lsb_to_msb |+ f64_norm_int,
|
|
417
|
+
{ .x /- x, .y /- y } { .x f64_msb_to_lsb x, .y f64_msb_to_lsb y } f64_lsb_add f64_lsb_to_msb |- f64_norm_int,
|
|
418
|
+
{ .x /+ x, .y /- y } { .x x, .y y } f64_i_sub_msb,
|
|
419
|
+
{ .x /- x, .y /+ y } { .x y, .y x } f64_i_sub_msb
|
|
615
420
|
>;
|
|
616
421
|
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
} digits_multiply_loop;
|
|
422
|
+
f64_i_neg = f64_norm_int < /+ < /0 /_ |_ |0 |+, /1 |1 |- >, /- |+ >;
|
|
423
|
+
f64_i_minus = { .x x, .y f64_i_neg y } f64_i_plus;
|
|
620
424
|
|
|
621
|
-
|
|
622
|
-
{ .
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
> binary_digits_loop
|
|
425
|
+
f64_i_cmp = { .x f64_norm_int x, .y f64_norm_int y } <
|
|
426
|
+
{ .x /+ x, .y /+ y } { .x x, .y y } f64_cmp_msb,
|
|
427
|
+
{ .x /+ _x, .y /- _y } {} |gt,
|
|
428
|
+
{ .x /- _x, .y /+ _y } {} |lt,
|
|
429
|
+
{ .x /- x, .y /- y } { .x y, .y x } f64_cmp_msb
|
|
627
430
|
>;
|
|
628
431
|
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
{
|
|
635
|
-
{
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
432
|
+
-- Small constant exponents in MSB bits[] (MSB at root, '_' at leaf).
|
|
433
|
+
f64_bits_52 = {} |_ |0 |0 |1 |0 |1 |1; -- 0b110100 (LSB) -> 52 (LSB)
|
|
434
|
+
f64_bits_1022 = {} |_ |0 |1 |1 |1 |1 |1 |1 |1 |1 |1; -- 1022
|
|
435
|
+
f64_bits_1023 = {} |_ |1 |1 |1 |1 |1 |1 |1 |1 |1 |1; -- 1023
|
|
436
|
+
f64_bits_1024 = {} |_ |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |1;
|
|
437
|
+
f64_bits_1074 = {} |_ |0 |1 |0 |0 |1 |1 |0 |0 |0 |0 |1; -- 1074
|
|
438
|
+
f64_bits_1075 = {} |_ |1 |1 |0 |0 |1 |1 |0 |0 |0 |0 |1; -- 1075
|
|
439
|
+
|
|
440
|
+
f64_lsb_52 = f64_bits_52 f64_msb_to_lsb;
|
|
441
|
+
f64_i_1023 = f64_bits_1023 |+;
|
|
442
|
+
f64_i_1024 = f64_bits_1024 |+;
|
|
443
|
+
f64_i_neg1022 = f64_bits_1022 |-;
|
|
444
|
+
f64_i_neg1075 = f64_bits_1075 |-;
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
-- -----------------------------------------------------------------------------
|
|
448
|
+
-- 4. Packing Helpers (LSB bits[] -> exp11[], frac52[])
|
|
449
|
+
-- -----------------------------------------------------------------------------
|
|
450
|
+
|
|
451
|
+
f64_pop4 =
|
|
452
|
+
f64_pop1 { .b 0, .r f64_pop1 r1 }
|
|
453
|
+
{ .0 0, .r1 .b 1, .r1 .r f64_pop1 r2 }
|
|
454
|
+
{ .0 0, .1 1, .r2 .b 2, .r2 .r f64_pop1 r3 } {
|
|
455
|
+
{ .0 0, .1 1, .2 2, .r3 .b 3 } v,
|
|
456
|
+
.r3 .r rest
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
f64_pop11 =
|
|
460
|
+
f64_pop4 { .v g0, .rest f64_pop4 r1 }
|
|
461
|
+
{ .g0 g0, .r1 .v g1, .r1 .rest f64_pop4 r2 } {
|
|
462
|
+
{
|
|
463
|
+
.g0 .0 "0", .g0 .1 "1", .g0 .2 "2", .g0 .3 "3",
|
|
464
|
+
.g1 .0 "4", .g1 .1 "5", .g1 .2 "6", .g1 .3 "7",
|
|
465
|
+
.r2 .v .0 "8", .r2 .v .1 "9", .r2 .v .2 "10"
|
|
466
|
+
} exp,
|
|
467
|
+
.r2 .rest rest
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
f64_bits_to_exp11 = f64_msb_to_lsb f64_pop11 .exp;
|
|
471
|
+
|
|
472
|
+
f64_pop16 =
|
|
473
|
+
f64_pop4 { .v p0, .rest f64_pop4 r1 }
|
|
474
|
+
{ .p0 p0, .r1 .v p1, .r1 .rest f64_pop4 r2 }
|
|
475
|
+
{ .p0 p0, .p1 p1, .r2 .v p2, .r2 .rest f64_pop4 r3 } {
|
|
476
|
+
{ .p0 p0, .p1 p1, .p2 p2, .r3 .v p3 } blk,
|
|
477
|
+
.r3 .rest rest
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
f64_bits_to_frac52 =
|
|
481
|
+
f64_pop16 { .blk b0, .rest f64_pop16 r1 }
|
|
482
|
+
{ .b0 b0, .r1 .blk b1, .r1 .rest f64_pop16 r2 }
|
|
483
|
+
{ .b0 b0, .b1 b1, .r2 .blk b2, .r2 .rest f64_pop4 r3 } {
|
|
484
|
+
{
|
|
485
|
+
.b0 .p0 .0 "0", .b0 .p0 .1 "1", .b0 .p0 .2 "2", .b0 .p0 .3 "3",
|
|
486
|
+
.b0 .p1 .0 "4", .b0 .p1 .1 "5", .b0 .p1 .2 "6", .b0 .p1 .3 "7",
|
|
487
|
+
.b0 .p2 .0 "8", .b0 .p2 .1 "9", .b0 .p2 .2 "10", .b0 .p2 .3 "11",
|
|
488
|
+
.b0 .p3 .0 "12", .b0 .p3 .1 "13", .b0 .p3 .2 "14", .b0 .p3 .3 "15",
|
|
489
|
+
|
|
490
|
+
.b1 .p0 .0 "16", .b1 .p0 .1 "17", .b1 .p0 .2 "18", .b1 .p0 .3 "19",
|
|
491
|
+
.b1 .p1 .0 "20", .b1 .p1 .1 "21", .b1 .p1 .2 "22", .b1 .p1 .3 "23",
|
|
492
|
+
.b1 .p2 .0 "24", .b1 .p2 .1 "25", .b1 .p2 .2 "26", .b1 .p2 .3 "27",
|
|
493
|
+
.b1 .p3 .0 "28", .b1 .p3 .1 "29", .b1 .p3 .2 "30", .b1 .p3 .3 "31",
|
|
494
|
+
|
|
495
|
+
.b2 .p0 .0 "32", .b2 .p0 .1 "33", .b2 .p0 .2 "34", .b2 .p0 .3 "35",
|
|
496
|
+
.b2 .p1 .0 "36", .b2 .p1 .1 "37", .b2 .p1 .2 "38", .b2 .p1 .3 "39",
|
|
497
|
+
.b2 .p2 .0 "40", .b2 .p2 .1 "41", .b2 .p2 .2 "42", .b2 .p2 .3 "43",
|
|
498
|
+
.b2 .p3 .0 "44", .b2 .p3 .1 "45", .b2 .p3 .2 "46", .b2 .p3 .3 "47",
|
|
499
|
+
|
|
500
|
+
.r3 .v .0 "48", .r3 .v .1 "49", .r3 .v .2 "50", .r3 .v .3 "51"
|
|
501
|
+
} frac,
|
|
502
|
+
.r3 .rest tail
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
-- -----------------------------------------------------------------------------
|
|
507
|
+
-- 5. Arbitrary-Precision Conversion: ratio_to_float64 & bits_to_float64
|
|
508
|
+
-- -----------------------------------------------------------------------------
|
|
509
|
+
|
|
510
|
+
f64_strip_tz_step = ?{ bits[] x } <
|
|
511
|
+
{ .x /1 x } .x |1,
|
|
512
|
+
{ .x /0 x } { .x x } f64_strip_tz_step
|
|
513
|
+
> ?bits[];
|
|
514
|
+
|
|
515
|
+
f64_lsb_bit_len_step = ?{ bits[] x, bits[] acc } <
|
|
516
|
+
{ .x /_ _x, .acc acc } .acc,
|
|
517
|
+
<
|
|
518
|
+
{ .x /0 x, .acc acc } { .x x, .acc f64_lsb_inc acc },
|
|
519
|
+
{ .x /1 x, .acc acc } { .x x, .acc f64_lsb_inc acc }
|
|
520
|
+
> f64_lsb_bit_len_step
|
|
521
|
+
> ?bits[];
|
|
522
|
+
|
|
523
|
+
-- Measures total bit length (MSB) and strips trailing zero factors of positive MSB bits[].
|
|
524
|
+
f64_factor_msb = ?bits[] f64_msb_to_lsb {
|
|
525
|
+
{ () x, {} |_ acc } f64_lsb_bit_len_step f64_lsb_to_msb total_len,
|
|
526
|
+
{ () x } f64_strip_tz_step odd_lsb
|
|
527
|
+
} {
|
|
528
|
+
.total_len total_len,
|
|
529
|
+
.odd_lsb odd_lsb,
|
|
530
|
+
{ .odd_lsb x, {} |_ acc } f64_lsb_bit_len_step f64_lsb_to_msb odd_len
|
|
531
|
+
};
|
|
646
532
|
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
533
|
+
-- Generates `n` fractional quotient bits (`n` in trimmed LSB bits[]) into `q_lsb`.
|
|
534
|
+
-- On entry: 0 <= rem < den_lsb and q_lsb holds the leading bit(s) so far.
|
|
535
|
+
f64_div_steps = ?{ bits[] rem, bits[] den_lsb, bits[] q_lsb, bits[] n } <
|
|
536
|
+
{ .rem rem, .den_lsb _d, .q_lsb q, .n /_ _n } { .q q_lsb, .rem rem },
|
|
537
|
+
{ .rem rem, .den_lsb den_lsb, .q_lsb q, .n n } {
|
|
538
|
+
{ .n x, {} |_ |1 y } f64_lsb_sub n_next,
|
|
539
|
+
.den_lsb den_lsb,
|
|
540
|
+
.q q,
|
|
541
|
+
.rem f64_lsb_shl1 rem2
|
|
542
|
+
} {
|
|
543
|
+
.n_next n_next,
|
|
544
|
+
.den_lsb den_lsb,
|
|
545
|
+
.q q,
|
|
546
|
+
.rem2 rem2,
|
|
547
|
+
{ .rem2 x, .den_lsb y } f64_lsb_try_sub sub
|
|
548
|
+
} <
|
|
549
|
+
{ .n_next n, .den_lsb den, .q q, .rem2 _r2, .sub .b /0 _b, .sub .r rem } {
|
|
550
|
+
.rem rem,
|
|
551
|
+
.den den_lsb,
|
|
552
|
+
.q |1 q_lsb,
|
|
553
|
+
.n n
|
|
554
|
+
},
|
|
555
|
+
{ .n_next n, .den_lsb den, .q q, .rem2 rem2, .sub .b /1 _b, .sub .r _r } {
|
|
556
|
+
.rem2 rem,
|
|
557
|
+
.den den_lsb,
|
|
558
|
+
.q |0 q_lsb,
|
|
559
|
+
.n n
|
|
560
|
+
}
|
|
561
|
+
> f64_div_steps
|
|
562
|
+
> ?{ bits[] q_lsb, bits[] rem };
|
|
651
563
|
|
|
652
|
-
|
|
653
|
-
.
|
|
654
|
-
.
|
|
564
|
+
f64_round_ties_even = {
|
|
565
|
+
.q_lsb q,
|
|
566
|
+
.rem f64_lsb_shl1 rem2,
|
|
567
|
+
.den_lsb den
|
|
568
|
+
} {
|
|
569
|
+
.q q,
|
|
570
|
+
{ .rem2 x, .den y } f64_lsb_cmp ord
|
|
655
571
|
} <
|
|
656
|
-
{
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
} .
|
|
660
|
-
{
|
|
661
|
-
{ .exponent x, c1075 y } lt? if,
|
|
662
|
-
{ .fraction 1b inv reversed,
|
|
663
|
-
{ c1075 x, .exponent y } minus places } fraction_digits_print then
|
|
664
|
-
} .then,
|
|
665
|
-
{
|
|
666
|
-
.fraction 1b binary_digits digits, f2 factor,
|
|
667
|
-
{ .exponent x, c1075 y } minus steps
|
|
668
|
-
} digits_power { () digits, _ places }
|
|
572
|
+
{ .q q, .ord /lt _o } .q,
|
|
573
|
+
{ .q q, .ord /gt _o } .q f64_lsb_inc,
|
|
574
|
+
{ .q /0 q, .ord /eq _o } .q |0,
|
|
575
|
+
{ .q /1 q, .ord /eq _o } .q |1 f64_lsb_inc
|
|
669
576
|
>;
|
|
670
577
|
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
578
|
+
f64_pack_rounded = {
|
|
579
|
+
.sign sign,
|
|
580
|
+
.eb eb,
|
|
581
|
+
.q_lsb f64_bits_to_frac52 packed
|
|
582
|
+
} {
|
|
583
|
+
.sign sign,
|
|
584
|
+
.eb eb,
|
|
585
|
+
.packed .frac frac,
|
|
586
|
+
.packed .tail tail
|
|
587
|
+
} <
|
|
588
|
+
{ .sign sign, .eb /0 /_ _eb, .frac frac, .tail /_ _t } {
|
|
589
|
+
.sign sign, exp0 exponent, .frac fraction
|
|
590
|
+
},
|
|
591
|
+
{ .sign sign, .eb /0 /_ _eb, .frac frac, .tail /1 /_ _t } {
|
|
592
|
+
.sign sign, {} |_ |1 f64_bits_to_exp11 exponent, .frac fraction
|
|
593
|
+
},
|
|
594
|
+
{ .sign sign, .eb eb, .frac frac, .tail /1 /_ _t } {
|
|
595
|
+
.sign sign, .eb f64_bits_to_exp11 exponent, .frac fraction
|
|
596
|
+
},
|
|
597
|
+
{ .sign sign, .eb eb, .frac frac, .tail /0 /1 /_ _t } {
|
|
598
|
+
.sign sign,
|
|
599
|
+
.eb f64_msb_to_lsb f64_lsb_inc f64_lsb_to_msb eb1,
|
|
600
|
+
.frac frac
|
|
601
|
+
} <
|
|
602
|
+
{ .eb1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /_ _all1, .sign sign, .frac _f } {
|
|
603
|
+
.sign sign, f64_exp_ones exponent, frac0 fraction
|
|
604
|
+
},
|
|
605
|
+
{ .eb1 eb1, .sign sign, .frac frac } {
|
|
606
|
+
.sign sign, .eb1 f64_bits_to_exp11 exponent, .frac fraction
|
|
607
|
+
}
|
|
608
|
+
>
|
|
678
609
|
>;
|
|
679
610
|
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
611
|
+
-- Finishes float64 assembly given normalized k, initial rem = A - B, and den = B
|
|
612
|
+
-- where 1 <= (A / B) < 2 and num/den = (A / B) * 2^k.
|
|
613
|
+
f64_finish_normalized = {
|
|
614
|
+
.sign sign,
|
|
615
|
+
.k_int k_int,
|
|
616
|
+
.rem rem,
|
|
617
|
+
.den den,
|
|
618
|
+
{ .k_int x, f64_i_1024 y } f64_i_cmp cmp_ovf,
|
|
619
|
+
{ .k_int x, f64_i_neg1075 y } f64_i_cmp cmp_min
|
|
620
|
+
} <
|
|
621
|
+
{ .sign sign, .k_int _k, .rem _r, .den _d, .cmp_ovf < /eq, /gt > _o, .cmp_min _m } {
|
|
622
|
+
.sign sign, f64_exp_ones exponent, frac0 fraction
|
|
623
|
+
},
|
|
624
|
+
{ .sign sign, .k_int _k, .rem _r, .den _d, .cmp_ovf /lt _o, .cmp_min /lt _m } {
|
|
625
|
+
.sign sign, exp0 exponent, frac0 fraction
|
|
626
|
+
},
|
|
627
|
+
{ .sign sign, .k_int _k, .rem /_ _r, .den _d, .cmp_ovf /lt _o, .cmp_min /eq _m } {
|
|
628
|
+
.sign sign, exp0 exponent, frac0 fraction
|
|
629
|
+
},
|
|
630
|
+
{ .sign sign, .k_int _k, .rem < /0, /1 > _r, .den _d, .cmp_ovf /lt _o, .cmp_min /eq _m } {
|
|
631
|
+
.sign sign, exp0 exponent, {} |_ |1 f64_bits_to_frac52 .frac fraction
|
|
632
|
+
},
|
|
633
|
+
{ .sign sign, .k_int k_int, .rem rem, .den den, .cmp_ovf /lt _o, .cmp_min /gt _m } {
|
|
634
|
+
.sign sign,
|
|
635
|
+
.rem rem,
|
|
636
|
+
.den den,
|
|
637
|
+
.k_int k_int,
|
|
638
|
+
{ .k_int x, f64_i_neg1022 y } f64_i_cmp cmp_norm
|
|
639
|
+
} <
|
|
640
|
+
{ .sign sign, .rem rem, .den den, .k_int k_int, .cmp_norm /lt _c } {
|
|
641
|
+
.sign sign,
|
|
642
|
+
.den den,
|
|
643
|
+
{} |_ |0 eb,
|
|
644
|
+
{
|
|
645
|
+
.rem rem,
|
|
646
|
+
.den den_lsb,
|
|
647
|
+
{} |_ |1 q_lsb,
|
|
648
|
+
{ .k_int x, f64_bits_1074 |+ y } f64_i_plus /+ f64_msb_to_lsb n
|
|
649
|
+
} f64_div_steps res
|
|
650
|
+
} {
|
|
651
|
+
.sign sign,
|
|
652
|
+
.eb eb,
|
|
653
|
+
{ .res .q_lsb q_lsb, .res .rem rem, .den den_lsb } f64_round_ties_even q_lsb
|
|
654
|
+
} f64_pack_rounded,
|
|
655
|
+
{ .sign sign, .rem rem, .den den, .k_int k_int, .cmp_norm < /eq, /gt > _c } {
|
|
656
|
+
.sign sign,
|
|
657
|
+
.den den,
|
|
658
|
+
{ .k_int x, f64_i_1023 y } f64_i_plus /+ eb,
|
|
659
|
+
{ .rem rem, .den den_lsb, {} |_ |1 q_lsb, f64_lsb_52 n } f64_div_steps res
|
|
660
|
+
} {
|
|
661
|
+
.sign sign,
|
|
662
|
+
.eb eb,
|
|
663
|
+
{ .res .q_lsb q_lsb, .res .rem rem, .den den_lsb } f64_round_ties_even q_lsb
|
|
664
|
+
} f64_pack_rounded
|
|
665
|
+
>
|
|
686
666
|
>;
|
|
687
667
|
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
668
|
+
-- Converts { sign[] sign, bits[] num, bits[] den } to IEEE float64[].
|
|
669
|
+
-- Fails when den == 0. Uses odd-core alignment to avoid huge bit shifts.
|
|
670
|
+
f64_ratio_nonzero = {
|
|
671
|
+
.sign sign,
|
|
672
|
+
.num f64_factor_msb fn,
|
|
673
|
+
.den f64_factor_msb fd
|
|
674
|
+
} {
|
|
675
|
+
.sign sign,
|
|
676
|
+
{ .fn .total_len |+ x, .fd .total_len |+ y } f64_i_minus k0,
|
|
677
|
+
{ .fn .odd_len |+ x, .fd .odd_len |+ y } f64_i_minus d_odd,
|
|
678
|
+
.fn .odd_lsb na,
|
|
679
|
+
.fd .odd_lsb da
|
|
680
|
+
} {
|
|
681
|
+
.sign sign,
|
|
682
|
+
.k0 k0,
|
|
683
|
+
{ .na na, .da da, .d_odd d_odd } <
|
|
684
|
+
{ .na na, .da da, .d_odd /+ s } {
|
|
685
|
+
.na a,
|
|
686
|
+
{ .da x, .s f64_msb_to_lsb n } f64_lsb_shl_n b
|
|
687
|
+
},
|
|
688
|
+
{ .na na, .da da, .d_odd /- s } {
|
|
689
|
+
{ .na x, .s f64_msb_to_lsb n } f64_lsb_shl_n a,
|
|
690
|
+
.da b
|
|
691
|
+
}
|
|
692
|
+
> ab
|
|
693
|
+
} {
|
|
694
|
+
.sign sign,
|
|
695
|
+
.k0 k0,
|
|
696
|
+
.ab .a a,
|
|
697
|
+
.ab .b b,
|
|
698
|
+
{ .ab .a x, .ab .b y } f64_lsb_cmp cmp_ab
|
|
699
|
+
} <
|
|
700
|
+
{ .sign sign, .k0 k0, .a a, .b b, .cmp_ab /eq _c } {
|
|
701
|
+
.sign sign,
|
|
702
|
+
.k0 k_int,
|
|
703
|
+
{} |_ rem,
|
|
704
|
+
.b den
|
|
705
|
+
} f64_finish_normalized,
|
|
706
|
+
{ .sign sign, .k0 k0, .a a, .b b, .cmp_ab /gt _c } {
|
|
707
|
+
.sign sign,
|
|
708
|
+
.k0 k_int,
|
|
709
|
+
{ .a x, .b y } f64_lsb_sub rem,
|
|
710
|
+
.b den
|
|
711
|
+
} f64_finish_normalized,
|
|
712
|
+
{ .sign sign, .k0 k0, .a a, .b b, .cmp_ab /lt _c } {
|
|
713
|
+
.sign sign,
|
|
714
|
+
{ .k0 x, {} |_ |1 |+ y } f64_i_minus k_int,
|
|
715
|
+
{ .a f64_lsb_shl1 x, .b y } f64_lsb_sub rem,
|
|
716
|
+
.b den
|
|
717
|
+
} f64_finish_normalized
|
|
691
718
|
>;
|
|
692
719
|
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
720
|
+
ratio_to_float64 = {
|
|
721
|
+
.sign sign,
|
|
722
|
+
.num f64_trim_msb num,
|
|
723
|
+
.den f64_trim_msb < /1 |1 > den
|
|
724
|
+
} <
|
|
725
|
+
{ .sign sign, .num /0 /_ _n, .den _d } { .sign sign, exp0 exponent, frac0 fraction },
|
|
726
|
+
{ .sign sign, .num /1 num, .den den } { .sign sign, .num |1 num, .den den } f64_ratio_nonzero
|
|
697
727
|
>;
|
|
698
728
|
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
zero_float64? zero_text,
|
|
705
|
-
{ () value, print_ratio format_decimal text } <
|
|
706
|
-
{ .value.sign un_- if, { _minus_ car, .text cdr } cons then } .then,
|
|
707
|
-
.text
|
|
708
|
-
>
|
|
709
|
-
>;
|
|
729
|
+
bits_to_float64 = {
|
|
730
|
+
{} |+ sign,
|
|
731
|
+
() num,
|
|
732
|
+
{} |_ |1 den
|
|
733
|
+
} ratio_to_float64;
|