-
Notifications
You must be signed in to change notification settings - Fork 5
/
float64.c
369 lines (349 loc) · 9.78 KB
/
float64.c
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
#include "float64.h"
#include "uint128.h"
// Count leading zero bits.
static inline Sint8 count_leading_zeros_u64(Uint64 a) {
return a == 0 ? 64 : __builtin_clzl(a);
}
// Take two double-precision float values, one which must be NaN, and produce
// the correct NaN result, taking care to raise an invalid exception when either
// is a signaling NaN.
static Float64 float64_propagate_nan(Context *ctx, Float64 a, Float64 b) {
const Flag a_is_nan = float64_is_nan(a);
const Flag a_is_snan = float64_is_snan(a);
const Flag b_is_nan = float64_is_nan(b);
const Flag b_is_snan = float64_is_snan(b);
a.bits |= LIT64(0x0008000000000000);
b.bits |= LIT64(0x0008000000000000);
if (a_is_snan | b_is_snan) {
context_raise(ctx, EXCEPTION_INVALID);
}
if (a_is_nan) {
return (a_is_snan & b_is_nan) ? b : a;
}
return b;
}
CanonicalNaN float64_to_canonical_nan(Context* ctx, Float64 a) {
if (float64_is_snan(a)) {
context_raise(ctx, EXCEPTION_INVALID);
}
CanonicalNaN nan;
nan.sign = a.bits >> 63;
nan.lo = 0;
nan.hi = a.bits << 12;
return nan;
}
Float64 float64_round_and_pack(Context *ctx, Flag sign, Sint32 exp, Uint64 sig) {
const Round rounding_mode = ctx->round;
const Flag round_nearest_even = rounding_mode == ROUND_NEAREST_EVEN;
Sint16 round_increment = 0x200;
if (!round_nearest_even) {
if (rounding_mode == ROUND_TO_ZERO) {
round_increment = 0;
} else {
round_increment = 0x3ff;
if (sign) {
if (rounding_mode == ROUND_UP) {
round_increment = 0;
}
} else {
if (rounding_mode == ROUND_DOWN) {
round_increment = 0;
}
}
}
}
Sint16 round_bits = sig & 0x3ff;
if (round_bits) {
ctx->roundings++;
}
if (0x7fd <= (Uint16)exp) {
if ((0x7fd < exp) || ((exp == 0x7fd) && ((Sint64)(sig + round_increment) < 0))) {
context_raise(ctx, EXCEPTION_OVERFLOW | EXCEPTION_INEXACT);
const Float64 pack = float64_pack(sign, 0x7ff, 0);
return (Float64){pack.bits - (round_increment == 0 ? 0 : 1)};
}
if (exp < 0) {
const Flag is_tiny = (ctx->tininess == TININESS_BEFORE_ROUNDING)
|| (exp < -1)
|| (sig + round_increment < LIT64(0x8000000000000000));
sig = rshr64(sig, -exp);
exp = 0;
round_bits = sig & 0x3ff;
if (is_tiny && round_bits) {
context_raise(ctx, EXCEPTION_UNDERFLOW);
}
}
}
if (round_bits) {
context_raise(ctx, EXCEPTION_INEXACT);
}
sig = (sig + round_increment) >> 10;
sig &= ~(((round_bits ^ 0x200) == 0) & round_nearest_even);
return float64_pack(sign, sig == 0 ? 0 : exp, sig);
}
static inline Float64 float64_normalize_round_and_pack(Context *ctx, Flag sign, Sint16 exp, Uint64 sig) {
const Sint8 shift = count_leading_zeros_u64(sig) - 1;
return float64_round_and_pack(ctx, sign, exp - shift, sig << shift);
}
Normal64 float64_normalize_subnormal(Uint64 sig) {
const Sint8 shift = count_leading_zeros_u64(sig) - 11;
return (Normal64){sig << shift, 1 - shift};
}
static Float64 float64_add_sig(Context *ctx, Float64 a, Float64 b, Flag sign) {
Sint16 a_exp = float64_exp(a);
Sint16 b_exp = float64_exp(b);
Uint64 a_sig = float64_fract(a) << 9;
Uint64 b_sig = float64_fract(b) << 9;
Sint16 exp_diff = a_exp - b_exp;
Sint16 exp;
Uint64 sig;
if (0 < exp_diff) {
if (a_exp == 0x7ff) {
return a_sig ? float64_propagate_nan(ctx, a, b) : a;
}
if (b_exp == 0) {
exp_diff--;
} else {
b_sig |= LIT64(0x2000000000000000);
}
b_sig = rshr64(b_sig, exp_diff);
exp = a_exp;
} else if (exp_diff < 0) {
if (b_exp == 0x7ff) {
return b_sig
? float64_propagate_nan(ctx, a, b)
: float64_pack(sign, 0x7ff, 0);
}
if (a_exp == 0) {
exp_diff++;
} else {
a_sig |= LIT64(0x2000000000000000);
}
a_sig = rshr64(a_sig, -exp_diff);
exp = b_exp;
} else {
if (a_exp == 0x7ff) {
return (a_sig | b_sig) ? float64_propagate_nan(ctx, a, b) : a;
}
if (a_exp == 0) {
return float64_pack(sign, 0, (a_sig + b_sig) >> 9);
}
sig = LIT64(0x4000000000000000) + a_sig + b_sig;
exp = a_exp;
goto round_and_pack;
}
a_sig |= LIT64(0x2000000000000000);
sig = (a_sig + b_sig) << 1;
exp--;
if ((Sint64)sig < 0) {
sig = a_sig + b_sig;
exp++;
}
round_and_pack:
return float64_round_and_pack(ctx, sign, exp, sig);
}
static Float64 float64_sub_sig(Context *ctx, Float64 a, Float64 b, Flag sign) {
Sint16 a_exp = float64_exp(a);
Sint16 b_exp = float64_exp(b);
Uint64 a_sig = float64_fract(a) << 10;
Uint64 b_sig = float64_fract(b) << 10;
Sint16 exp_diff = a_exp - b_exp;
// Needed because goto crosses initialization.
Sint16 exp;
Uint64 sig;
if (0 < exp_diff) {
goto a_exp_bigger;
}
if (exp_diff < 0) {
goto b_exp_bigger;
}
if (a_exp == 0x7ff) {
if (a_sig | b_sig) {
return float64_propagate_nan(ctx, a, b);
}
context_raise(ctx, EXCEPTION_INVALID);
return FLOAT64_NAN;
}
if (a_exp == 0) {
a_exp = 1;
b_exp = 1;
}
if (b_sig < a_sig) {
goto a_bigger;
}
if (a_sig < b_sig) {
goto b_bigger;
}
return float64_pack(ctx->round == ROUND_DOWN, 0, 0);
b_exp_bigger:
if (b_exp == 0x7ff) {
return b_sig
? float64_propagate_nan(ctx, a, b)
: float64_pack(sign ^ 1, 0xff, 0);
}
if (a_exp == 0) {
exp_diff++;
} else {
a_sig |= LIT64(0x4000000000000000);
}
a_sig = rshr64(a_sig, -exp_diff);
b_sig |= LIT64(0x4000000000000000);
b_bigger:
sig = b_sig - a_sig;
exp = b_exp;
sign ^= 1;
goto normalize_round_and_pack;
a_exp_bigger:
if (a_exp == 0x7ff) {
return a_sig ? float64_propagate_nan(ctx, a, b) : a;
}
if (b_exp == 0) {
exp_diff--;
} else {
b_sig |= LIT64(0x4000000000000000);
}
b_sig = rshr64(b_sig, exp_diff);
a_sig |= LIT64(0x4000000000000000);
a_bigger:
sig = a_sig - b_sig;
exp = a_exp;
normalize_round_and_pack:
exp--;
return float64_normalize_round_and_pack(ctx, sign, exp, sig);
}
Float64 float64_add(Context *ctx, Float64 a, Float64 b) {
const Flag a_sign = float64_sign(a);
const Flag b_sign = float64_sign(b);
return a_sign == b_sign
? float64_add_sig(ctx, a, b, a_sign)
: float64_sub_sig(ctx, a, b, b_sign);
}
Float64 float64_sub(Context *ctx, Float64 a, Float64 b) {
const Flag a_sign = float64_sign(a);
const Flag b_sign = float64_sign(b);
return a_sign == b_sign
? float64_sub_sig(ctx, a, b, a_sign)
: float64_add_sig(ctx, a, b, a_sign);
}
Float64 float64_mul(Context *ctx, Float64 a, Float64 b) {
Sint16 a_exp = float64_exp(a);
Sint16 b_exp = float64_exp(b);
Uint64 a_sig = float64_fract(a);
Uint64 b_sig = float64_fract(b);
Flag a_sign = float64_sign(a);
Flag b_sign = float64_sign(b);
Flag sign = a_sign ^ b_sign;
if (a_exp == 0x7ff) {
if (a_sig || (b_exp == 0x7ff && b_sig)) {
return float64_propagate_nan(ctx, a, b);
}
if ((b_exp | b_sig) == 0) {
context_raise(ctx, EXCEPTION_INVALID);
return FLOAT64_NAN;
}
return float64_pack(sign, 0x7ff, 0);
}
if (b_exp == 0x7ff) {
if (b_sig) {
return float64_propagate_nan(ctx, a, b);
}
if ((a_exp | a_sig) == 0) {
context_raise(ctx, EXCEPTION_INVALID);
return FLOAT64_NAN;
}
return float64_pack(sign, 0x7ff, 0);
}
if (a_exp == 0) {
if (a_sig == 0) {
return float64_pack(sign, 0, 0);
}
const Normal64 n = float64_normalize_subnormal(a_sig);
a_exp = n.exp;
a_sig = n.sig;
}
if (b_exp == 0) {
if (b_sig == 0) {
return float64_pack(sign, 0, 0);
const Normal64 n = float64_normalize_subnormal(b_sig);
b_exp = n.exp;
b_sig = n.sig;
}
}
Sint16 exp = a_exp + b_exp - 0x3ff;
a_sig = (a_sig | LIT64(0x0010000000000000)) << 10;
b_sig = (b_sig | LIT64(0x0010000000000000)) << 11;
// Compute with 128-bit mul, truncate to 64-bit.
Uint128 mul = uint128_mul64x64(a_sig, b_sig);
mul.z0 |= mul.z1 != 0;
if (0 <= (Sint64)(mul.z0 << 1)) {
mul.z0 <<= 1;
exp--;
}
return float64_round_and_pack(ctx, sign, exp, mul.z0);
}
Float64 float64_div(Context *ctx, Float64 a, Float64 b) {
Sint16 a_exp = float64_exp(a);
Sint16 b_exp = float64_exp(b);
Uint64 a_sig = float64_fract(a);
Uint64 b_sig = float64_fract(b);
Flag a_sign = float64_sign(a);
Flag b_sign = float64_sign(b);
Flag sign = a_sign ^ b_sign;
if (a_exp == 0x7ff) {
if (a_sig) {
return float64_propagate_nan(ctx, a, b);
}
if (b_exp == 0x7ff) {
if (b_sig) {
return float64_propagate_nan(ctx, a, b);
}
context_raise(ctx, EXCEPTION_INVALID);
return FLOAT64_NAN;
}
return float64_pack(sign, 0xff, 0);
}
if (b_exp == 0x7ff) {
return b_sig
? float64_propagate_nan(ctx, a, b)
: float64_pack(sign, 0, 0);
}
if (b_exp == 0) {
if (b_sig == 0) {
if ((a_exp | a_sig) == 0) {
context_raise(ctx, EXCEPTION_INVALID);
return FLOAT64_NAN;
}
context_raise(ctx, EXCEPTION_INFINITE);
return float64_pack(sign, 0xff, 0);
}
const Normal64 n = float64_normalize_subnormal(b_sig);
b_exp = n.exp;
b_sig = n.sig;
}
if (a_exp == 0) {
if (a_sig == 0) {
return float64_pack(sign, 0, 0);
}
const Normal64 n = float64_normalize_subnormal(a_sig);
a_exp = n.exp;
a_sig = n.sig;
}
Sint16 exp = a_exp - b_exp + 0x7d;
a_sig = (a_sig | LIT64(0x0010000000000000)) << 10;
b_sig = (b_sig | LIT64(0x0010000000000000)) << 11;
if (b_sig <= a_sig + a_sig) {
a_sig >>= 1;
exp++;
}
Uint64 sig = uint128_div128x64((Uint128){a_sig, 0}, b_sig);
if ((sig & 0x1ff) <= 2) {
Uint128 term = uint128_mul64x64(b_sig, sig);
Uint128 rem = uint128_sub((Uint128){a_sig, 0}, term);
while ((Sint64)rem.z0 < 0) {
sig--;
rem = uint128_add(rem, (Uint128){0, b_sig});
}
sig |= rem.z1 != 0;
}
return float64_round_and_pack(ctx, sign, exp, sig);
}