authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-28 08:48:19-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2017-09-28 08:48:19-04:00
log0cfeefbf158e91922e367113c438c331d75ac1b4
tree205d5ac64bf5f7a896fdd576a3e284b06b6cf714
parentfd5a5db400995d80015448d19e92daaca71a7f72
parent9dfe217be38289b0c9e662d1a0e7c0231bf42a7d

Merge pull request #502 from zig-lang/f128-literals

Allow 128-bit hex float literals

5 files changed, 47 insertions(+), 27 deletions(-)

src/tokenizer.cpp+31-18
......@@ -303,7 +303,7 @@ static void end_float_token(Tokenize *t) {
303303 return;
304304 }
305305
306 if (!bigint_fits_in_bits(&t->specified_exponent, 64, true)) {
306 if (!bigint_fits_in_bits(&t->specified_exponent, 128, true)) {
307307 t->cur_tok->data.float_lit.overflow = true;
308308 return;
309309 }
......@@ -314,39 +314,52 @@ static void end_float_token(Tokenize *t) {
314314 }
315315 t->exponent_in_bin_or_dec = (int)(t->exponent_in_bin_or_dec + specified_exponent);
316316
317 if (!bigint_fits_in_bits(&t->significand, 64, false)) {
317 if (!bigint_fits_in_bits(&t->significand, 128, false)) {
318318 t->cur_tok->data.float_lit.overflow = true;
319319 return;
320320 }
321321
322 uint64_t significand = bigint_as_unsigned(&t->significand);
323 uint64_t significand_bits;
324 uint64_t exponent_bits;
325 if (significand == 0) {
326 // 0 is all 0's
327 significand_bits = 0;
328 exponent_bits = 0;
322 // A SoftFloat-3d float128 is represented internally as a standard
323 // quad-precision float with 15bit exponent and 113bit fractional.
324 union { uint64_t repr[2]; float128_t actual; } f_bits;
325
326 if (bigint_cmp_zero(&t->significand) == CmpEQ) {
327 f_bits.repr[0] = 0;
328 f_bits.repr[1] = 0;
329329 } else {
330330 // normalize the significand
331331 if (t->radix == 10) {
332332 zig_panic("TODO: decimal floats");
333333 } else {
334 int significand_magnitude_in_bin = clzll(1) - clzll(significand);
334 int significand_magnitude_in_bin = 127 - bigint_clz(&t->significand, 128);
335335 t->exponent_in_bin_or_dec += significand_magnitude_in_bin;
336 if (!(-1022 <= t->exponent_in_bin_or_dec && t->exponent_in_bin_or_dec <= 1023)) {
336 if (!(-16382 <= t->exponent_in_bin_or_dec && t->exponent_in_bin_or_dec <= 16383)) {
337337 t->cur_tok->data.float_lit.overflow = true;
338338 return;
339 }
340
341 uint64_t sig_bits[2] = {0, 0};
342 bigint_write_twos_complement(&t->significand, (uint8_t*) sig_bits, 128, false);
343
344 const uint64_t shift = 112 - significand_magnitude_in_bin;
345 const uint64_t exp_shift = 48;
346 // Mask the sign bit to 0 since always non-negative lex
347 const uint64_t exp_mask = 0xfffful << exp_shift;
348
349 if (shift >= 64) {
350 f_bits.repr[0] = 0;
351 f_bits.repr[1] = sig_bits[0] << (shift - 64);
339352 } else {
340 // this should chop off exactly one 1 bit from the top.
341 significand_bits = ((uint64_t)significand << (52 - significand_magnitude_in_bin)) & 0xfffffffffffffULL;
342 exponent_bits = t->exponent_in_bin_or_dec + 1023;
353 f_bits.repr[0] = sig_bits[0] << shift;
354 f_bits.repr[1] = ((sig_bits[1] << shift) | (sig_bits[0] >> (64 - shift)));
343355 }
356
357 f_bits.repr[1] &= ~exp_mask;
358 f_bits.repr[1] |= (uint64_t)(t->exponent_in_bin_or_dec + 16383) << exp_shift;
344359 }
345360 }
346 uint64_t double_bits = (exponent_bits << 52) | significand_bits;
347 double dbl_value;
348 safe_memcpy(&dbl_value, (double *)&double_bits, 1);
349 bigfloat_init_64(&t->cur_tok->data.float_lit.bigfloat, dbl_value);
361
362 bigfloat_init_128(&t->cur_tok->data.float_lit.bigfloat, f_bits.actual);
350363}
351364
352365static void end_token(Tokenize *t) {
std/math/expm1.zig+1-1
......@@ -255,7 +255,7 @@ fn expm1_64(x_: f64) -> f64 {
255255 if (k < 0 or k > 56) {
256256 var y = x - e + 1.0;
257257 if (k == 1024) {
258 y = y * 2.0 * 0x1.0p1022 * 10;
258 y = y * 2.0 * 0x1.0p1023;
259259 } else {
260260 y = y * twopk;
261261 }
std/math/scalbn.zig+2-2
......@@ -45,10 +45,10 @@ fn scalbn64(x: f64, n_: i32) -> f64 {
4545 var n = n_;
4646
4747 if (n > 1023) {
48 y *= 0x1.0p1022 * 2.0;
48 y *= 0x1.0p1023;
4949 n -= 1023;
5050 if (n > 1023) {
51 y *= 0x1.0p1022 * 2.0;
51 y *= 0x1.0p1023;
5252 n -= 1023;
5353 if (n > 1023) {
5454 n = 1023;
test/cases/math.zig+11-4
......@@ -241,14 +241,21 @@ test "allow signed integer division/remainder when values are comptime known and
241241 assert(-6 % 3 == 0);
242242}
243243
244test "float literal parsing" {
244test "hex float literal parsing" {
245245 comptime assert(0x1.0 == 1.0);
246246}
247247
248test "quad hex float literal parsing in range" {
249 const a = 0x1.af23456789bbaaab347645365cdep+5;
250 const b = 0x1.dedafcff354b6ae9758763545432p-9;
251 const c = 0x1.2f34dd5f437e849b4baab754cdefp+4534;
252 const d = 0x1.edcbff8ad76ab5bf46463233214fp-435;
253}
254
248255test "hex float literal within range" {
249 const a = 0x1.0p1023;
250 const b = 0x0.1p1027;
251 const c = 0x1.0p-1022;
256 const a = 0x1.0p16383;
257 const b = 0x0.1p16387;
258 const c = 0x1.0p-16382;
252259}
253260
254261test "truncating shift left" {
test/compile_errors.zig+2-2
......@@ -1901,14 +1901,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
19011901
19021902 cases.add("float literal too large error",
19031903 \\comptime {
1904 \\ const a = 0x1.0p1024;
1904 \\ const a = 0x1.0p16384;
19051905 \\}
19061906 ,
19071907 ".tmp_source.zig:2:15: error: float literal out of range of any type");
19081908
19091909 cases.add("float literal too small error (denormal)",
19101910 \\comptime {
1911 \\ const a = 0x1.0p-1023;
1911 \\ const a = 0x1.0p-16384;
19121912 \\}
19131913 ,
19141914 ".tmp_source.zig:2:15: error: float literal out of range of any type");