From 4d449dc663adf9bfccdb0ebea628efbaa9926680 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 12 Jul 2020 11:00:47 +1000 Subject: [PATCH] Rename ieee_754_bit_representation functions --- internal/cgen/base/floatconv-submodule-code.c | 14 +++---- internal/cgen/base/strconv-public.h | 18 ++++---- internal/cgen/data/data.go | 41 ++++++++++--------- release/c/wuffs-unsupported-snapshot.c | 32 ++++++++------- test/c/std/json.c | 15 +++---- 5 files changed, 65 insertions(+), 55 deletions(-) diff --git a/internal/cgen/base/floatconv-submodule-code.c b/internal/cgen/base/floatconv-submodule-code.c index 2c88623a..0885066d 100644 --- a/internal/cgen/base/floatconv-submodule-code.c +++ b/internal/cgen/base/floatconv-submodule-code.c @@ -1083,7 +1083,7 @@ wuffs_base__private_implementation__parse_number_f64_special( } wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64( + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64( (nan ? 0x7FFFFFFFFFFFFFFF : 0x7FF0000000000000) | (negative ? 0x8000000000000000 : 0)); return ret; @@ -1138,7 +1138,7 @@ wuffs_base__private_implementation__high_prec_dec__to_f64( if (r >= 0) { wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64( + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64( ((uint64_t)r) | (((uint64_t)(h->negative)) << 63)); return ret; } @@ -1233,7 +1233,7 @@ wuffs_base__private_implementation__high_prec_dec__to_f64( wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits); + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(bits); return ret; } while (0); @@ -1243,7 +1243,7 @@ zero: wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits); + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(bits); return ret; } while (0); @@ -1260,7 +1260,7 @@ infinity: wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits); + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(bits); return ret; } while (0); } @@ -1476,7 +1476,7 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) { } wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64( + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64( ((uint64_t)r) | (((uint64_t)negative) << 63)); return ret; } while (0); @@ -1692,7 +1692,7 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst, uint32_t options) { // Decompose x (64 bits) into negativity (1 bit), base-2 exponent (11 bits // with a -1023 bias) and mantissa (52 bits). - uint64_t bits = wuffs_base__ieee_754_bit_representation__from_f64(x); + uint64_t bits = wuffs_base__ieee_754_bit_representation__from_f64_to_u64(x); bool neg = (bits >> 63) != 0; int32_t exp2 = ((int32_t)(bits >> 52)) & 0x7FF; uint64_t man = bits & 0x000FFFFFFFFFFFFFul; diff --git a/internal/cgen/base/strconv-public.h b/internal/cgen/base/strconv-public.h index 40222136..67e52baa 100644 --- a/internal/cgen/base/strconv-public.h +++ b/internal/cgen/base/strconv-public.h @@ -135,18 +135,22 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options); // -------- // wuffs_base__ieee_754_bit_representation__etc converts between a double -// precision numerical value and its IEEE 754 64-bit representation (1 sign -// bit, 11 exponent bits, 52 explicit significand bits). +// precision numerical value and its IEEE 754 representations: +// - 16-bit: 1 sign bit, 5 exponent bits, 10 explicit significand bits. +// - 32-bit: 1 sign bit, 8 exponent bits, 23 explicit significand bits. +// - 64-bit: 1 sign bit, 11 exponent bits, 52 explicit significand bits. // // For example, it converts between: -// - +1.0 and 0x3FF0_0000_0000_0000. -// - +5.5 and 0x4016_0000_0000_0000. -// - -inf and 0xFFF0_0000_0000_0000. +// - +1.0 and 0x3C00, 0x3F80_0000 or 0x3FF0_0000_0000_0000. +// - +5.5 and 0x4580, 0x40B0_0000 or 0x4016_0000_0000_0000. +// - -inf and 0xFC00, 0xFF80_0000 or 0xFFF0_0000_0000_0000. +// +// Converting from f64 to shorter formats may be lossy. // // See https://en.wikipedia.org/wiki/Double-precision_floating-point_format static inline uint64_t // -wuffs_base__ieee_754_bit_representation__from_f64(double f) { +wuffs_base__ieee_754_bit_representation__from_f64_to_u64(double f) { uint64_t u = 0; if (sizeof(uint64_t) == sizeof(double)) { memcpy(&u, &f, sizeof(uint64_t)); @@ -155,7 +159,7 @@ wuffs_base__ieee_754_bit_representation__from_f64(double f) { } static inline double // -wuffs_base__ieee_754_bit_representation__to_f64(uint64_t u) { +wuffs_base__ieee_754_bit_representation__from_u64_to_f64(uint64_t u) { double f = 0; if (sizeof(uint64_t) == sizeof(double)) { memcpy(&f, &u, sizeof(uint64_t)); diff --git a/internal/cgen/data/data.go b/internal/cgen/data/data.go index ecbec2db..d230ed70 100644 --- a/internal/cgen/data/data.go +++ b/internal/cgen/data/data.go @@ -73,31 +73,31 @@ const BaseFloatConvSubmoduleCodeC = "" + "" + "// --------\n\nstatic wuffs_base__result_f64 //\nwuffs_base__private_implementation__parse_number_f64_special(\n wuffs_base__slice_u8 s,\n uint32_t options) {\n do {\n if (options & WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN) {\n goto fail;\n }\n\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p >= q) {\n goto fail;\n }\n\n // Parse sign.\n bool negative = false;\n do {\n if (*p == '+') {\n p++;\n } else if (*p == '-') {\n negative = true;\n p++;\n } else {\n break;\n }\n for (; (p < q) && (*p == '_'); p++) {\n }\n } while (0);\n if (p >= q) {\n goto fail;\n }\n\n bool nan = false;\n switch (p[0]) {\n case 'I':\n case 'i':\n if (((q - p) < 3) || //\n ((p[1] != 'N') && (p[1] != 'n')) || //\n ((p[2] != 'F') && (p[2] != 'f'))) {\n goto fail;\n }\n p += 3;\n\n if ((p >= q) || (*p == '_" + "')) {\n break;\n } else if (((q - p) < 5) || //\n ((p[0] != 'I') && (p[0] != 'i')) || //\n ((p[1] != 'N') && (p[1] != 'n')) || //\n ((p[2] != 'I') && (p[2] != 'i')) || //\n ((p[3] != 'T') && (p[3] != 't')) || //\n ((p[4] != 'Y') && (p[4] != 'y'))) {\n goto fail;\n }\n p += 5;\n\n if ((p >= q) || (*p == '_')) {\n break;\n }\n goto fail;\n\n case 'N':\n case 'n':\n if (((q - p) < 3) || //\n ((p[1] != 'A') && (p[1] != 'a')) || //\n ((p[2] != 'N') && (p[2] != 'n'))) {\n goto fail;\n }\n p += 3;\n\n if ((p >= q) || (*p == '_')) {\n nan = true;\n break;\n }\n goto fail;\n\n default:\n goto fail;\n }\n\n // Finish.\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p != q) {\n goto fail;\n }\n wuffs_base__result_f64 ret;\n" + - " ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(\n (nan ? 0x7FFFFFFFFFFFFFFF : 0x7FF0000000000000) |\n (negative ? 0x8000000000000000 : 0));\n return ret;\n } while (0);\n\nfail:\n do {\n wuffs_base__result_f64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__private_implementation__high_prec_dec__to_f64(\n wuffs_base__private_implementation__high_prec_dec* h,\n uint32_t options) {\n do {\n // powers converts decimal powers of 10 to binary powers of 2. For example,\n // (10000 >> 13) is 1. It stops before the elements exceed 60, also known\n // as WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL.\n static const uint32_t num_powers = 19;\n static const uint8_t powers[19] = {\n 0, 3, 6, 9, 13, 16, 19, 23, 26, 29, //\n 33, 36, 39, 43, 46, 49, 53, 56, 59, //\n };\n\n // Handle zero an" + - "d obvious extremes. The largest and smallest positive\n // finite f64 values are approximately 1.8e+308 and 4.9e-324.\n if ((h->num_digits == 0) || (h->decimal_point < -326)) {\n goto zero;\n } else if (h->decimal_point > 310) {\n goto infinity;\n }\n\n // Try the fast Eisel algorithm again. Calculating the (man, exp10) pair\n // from the high_prec_dec h is more correct but slower than the approach\n // taken in wuffs_base__parse_number_f64. The latter is optimized for the\n // common cases (e.g. assuming no underscores or a leading '+' sign) rather\n // than the full set of cases allowed by the Wuffs API.\n if (h->num_digits <= 19) {\n uint64_t man = 0;\n uint32_t i;\n for (i = 0; i < h->num_digits; i++) {\n man = (10 * man) + h->digits[i];\n }\n int32_t exp10 = h->decimal_point - ((int32_t)(h->num_digits));\n if ((man != 0) && (-326 <= exp10) && (exp10 <= 310)) {\n int64_t r = wuffs_base__private_implementation__parse_number_f64_eisel(\n " + - " man, exp10);\n if (r >= 0) {\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(\n ((uint64_t)r) | (((uint64_t)(h->negative)) << 63));\n return ret;\n }\n }\n }\n\n // Scale by powers of 2 until we're in the range [½ .. 1], which gives us\n // our exponent (in base-2). First we shift right, possibly a little too\n // far, ending with a value certainly below 1 and possibly below ½...\n const int32_t f64_bias = -1023;\n int32_t exp2 = 0;\n while (h->decimal_point > 0) {\n uint32_t n = (uint32_t)(+h->decimal_point);\n uint32_t shift =\n (n < num_powers)\n ? powers[n]\n : WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n\n wuffs_base__private_implementation__high_prec_dec__small_rshift(h, shift);\n if (h->decimal_point <\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n goto zero;\n" + - " }\n exp2 += (int32_t)shift;\n }\n // ...then we shift left, putting us in [½ .. 1].\n while (h->decimal_point <= 0) {\n uint32_t shift;\n if (h->decimal_point == 0) {\n if (h->digits[0] >= 5) {\n break;\n }\n shift = (h->digits[0] <= 2) ? 2 : 1;\n } else {\n uint32_t n = (uint32_t)(-h->decimal_point);\n shift = (n < num_powers)\n ? powers[n]\n : WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n }\n\n wuffs_base__private_implementation__high_prec_dec__small_lshift(h, shift);\n if (h->decimal_point >\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n goto infinity;\n }\n exp2 -= (int32_t)shift;\n }\n\n // We're in the range [½ .. 1] but f64 uses [1 .. 2].\n exp2--;\n\n // The minimum normal exponent is (f64_bias + 1).\n while ((f64_bias + 1) > exp2) {\n uint32_t n = (uint32_t)((f64_bias + 1) - exp2);\n if (n > WUFFS_BASE__PRIVATE_IM" + - "PLEMENTATION__HPD__SHIFT__MAX_INCL) {\n n = WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n }\n wuffs_base__private_implementation__high_prec_dec__small_rshift(h, n);\n exp2 += (int32_t)n;\n }\n\n // Check for overflow.\n if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1.\n goto infinity;\n }\n\n // Extract 53 bits for the mantissa (in base-2).\n wuffs_base__private_implementation__high_prec_dec__small_lshift(h, 53);\n uint64_t man2 =\n wuffs_base__private_implementation__high_prec_dec__rounded_integer(h);\n\n // Rounding might have added one bit. If so, shift and re-check overflow.\n if ((man2 >> 53) != 0) {\n man2 >>= 1;\n exp2++;\n if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1.\n goto infinity;\n }\n }\n\n // Handle subnormal numbers.\n if ((man2 >> 52) == 0) {\n exp2 = f64_bias;\n }\n\n // Pack the bits and return.\n uint64_t exp2_bits =\n (uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 <<" + - " 11) - 1.\n uint64_t bits = (man2 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1.\n (exp2_bits << 52) | //\n (h->negative ? 0x8000000000000000 : 0); // (1 << 63).\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits);\n return ret;\n } while (0);\n\nzero:\n do {\n uint64_t bits = h->negative ? 0x8000000000000000 : 0;\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits);\n return ret;\n } while (0);\n\ninfinity:\n do {\n if (options & WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN) {\n wuffs_base__result_f64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n }\n\n uint64_t bits = h->negative ? 0xFFF0000000000000 : 0x7FF0000000000000;\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_" + - "bit_representation__to_f64(bits);\n return ret;\n } while (0);\n}\n\nstatic inline bool //\nwuffs_base__private_implementation__is_decimal_digit(uint8_t c) {\n return ('0' <= c) && (c <= '9');\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {\n // In practice, almost all \"dd.ddddE±xxx\" numbers can be represented\n // losslessly by a uint64_t mantissa \"dddddd\" and an int32_t base-10\n // exponent, adjusting \"xxx\" for the position (if present) of the decimal\n // separator '.' or ','.\n //\n // This (u64 man, i32 exp10) data structure is superficially similar to the\n // \"Do It Yourself Floating Point\" type from Loitsch (†), but the exponent\n // here is base-10, not base-2.\n //\n // If s's number fits in a (man, exp10), parse that pair with the Eisel\n // algorithm. If not, or if Eisel fails, parsing s with the fallback\n // algorithm is slower but comprehensive.\n //\n // † \"Printing Floating-Point Numbers Quickly and Accuratel" + - "y with Integers\"\n // (https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf).\n // Florian Loitsch is also the primary contributor to\n // https://github.com/google/double-conversion\n do {\n // Calculating that (man, exp10) pair needs to stay within s's bounds.\n // Provided that s isn't extremely long, work on a NUL-terminated copy of\n // s's contents. The NUL byte isn't a valid part of \"±dd.ddddE±xxx\".\n //\n // As the pointer p walks the contents, it's faster to repeatedly check \"is\n // *p a valid digit\" than \"is p within bounds and *p a valid digit\".\n if (s.len >= 256) {\n goto fallback;\n }\n uint8_t z[256];\n memcpy(&z[0], s.ptr, s.len);\n z[s.len] = 0;\n const uint8_t* p = &z[0];\n\n // Look for a leading minus sign. Technically, we could also look for an\n // optional plus sign, but the \"script/process-json-numbers.c with -p\"\n // benchmark is noticably slower if we do. It's optional and, in practice,\n // usually absent. Let the fallback catch" + - " it.\n bool negative = (*p == '-');\n if (negative) {\n p++;\n }\n\n // After walking \"dd.dddd\", comparing p later with p now will produce the\n // number of \"d\"s and \".\"s.\n const uint8_t* const start_of_digits_ptr = p;\n\n // Walk the \"d\"s before a '.', 'E', NUL byte, etc. If it starts with '0',\n // it must be a single '0'. If it starts with a non-zero decimal digit, it\n // can be a sequence of decimal digits.\n //\n // Update the man variable during the walk. It's OK if man overflows now.\n // We'll detect that later.\n uint64_t man;\n if (*p == '0') {\n man = 0;\n p++;\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n } else if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n man = ((uint8_t)(*p - '0'));\n p++;\n for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {\n man = (10 * man) + ((uint8_t)(*p - '0'));\n }\n } else {\n goto fallback;\n }\n\n /" + - "/ Walk the \"d\"s after the optional decimal separator ('.' or ','),\n // updating the man and exp10 variables.\n int32_t exp10 = 0;\n if (*p ==\n ((options & WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n const uint8_t* first_after_separator_ptr = p;\n if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n man = (10 * man) + ((uint8_t)(*p - '0'));\n p++;\n for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {\n man = (10 * man) + ((uint8_t)(*p - '0'));\n }\n exp10 = ((int32_t)(first_after_separator_ptr - p));\n }\n\n // Count the number of digits:\n // - for an input of \"314159\", digit_count is 6.\n // - for an input of \"3.14159\", digit_count is 7.\n //\n // This is off-by-one if there is a decimal separator. That's OK for now.\n // We'll correct for that later. The \"script/process-json-numbers.c with\n // -p\" benchmark " + - "is noticably slower if we try to correct for that now.\n uint32_t digit_count = (uint32_t)(p - start_of_digits_ptr);\n\n // Update exp10 for the optional exponent, starting with 'E' or 'e'.\n if ((*p | 0x20) == 'e') {\n p++;\n int32_t exp_sign = +1;\n if (*p == '-') {\n p++;\n exp_sign = -1;\n } else if (*p == '+') {\n p++;\n }\n if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n int32_t exp_num = ((uint8_t)(*p - '0'));\n p++;\n // The rest of the exp_num walking has a peculiar control flow but, once\n // again, the \"script/process-json-numbers.c with -p\" benchmark is\n // sensitive to alternative formulations.\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n " + - " }\n while (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n if (exp_num > 0x1000000) {\n goto fallback;\n }\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n exp10 += exp_sign * exp_num;\n }\n\n // The Wuffs API is that the original slice has no trailing data. It also\n // allows underscores, which we don't catch here but the fallback should.\n if (p != &z[s.len]) {\n goto fallback;\n }\n\n // Check that the uint64_t typed man variable has not overflowed, based on\n // digit_count.\n //\n // For reference:\n // - (1 << 63) is 9223372036854775808, which has 19 decimal digits.\n // - (1 << 64) is 18446744073709551616, which has 20 decimal digits.\n // - 19 nines, 9999999999999999999, is 0x8AC7230489E7FFFF, which has 64\n // bits and 16 hexadecimal digits.\n // - 20 nines, 99999999999999999999, is 0x56BC75E2D630FFFFF, which has 67\n // bits and 17 hexadecimal digits.\n if (digit_count" + - " > 19) {\n // Even if we have more than 19 pseudo-digits, it's not yet definitely an\n // overflow. Recall that digit_count might be off-by-one (too large) if\n // there's a decimal separator. It will also over-report the number of\n // meaningful digits if the input looks something like \"0.000dddExxx\".\n //\n // We adjust by the number of leading '0's and '.'s and re-compare to 19.\n // Once again, technically, we could skip ','s too, but that perturbs the\n // \"script/process-json-numbers.c with -p\" benchmark.\n const uint8_t* q = start_of_digits_ptr;\n for (; (*q == '0') || (*q == '.'); q++) {\n }\n digit_count -= (uint32_t)(q - start_of_digits_ptr);\n if (digit_count > 19) {\n goto fallback;\n }\n }\n\n // The wuffs_base__private_implementation__parse_number_f64_eisel\n // preconditions include that exp10 is in the range -326 ..= 310.\n if ((exp10 < -326) || (310 < exp10)) {\n goto fallback;\n }\n\n // If man and exp10 are small e" + - "nough, all three of (man), (10 ** exp10) and\n // (man ** (10 ** exp10)) are exactly representable by a double. We don't\n // need to run the Eisel algorithm.\n if ((-22 <= exp10) && (exp10 <= 22) && ((man >> 53) == 0)) {\n double d = (double)man;\n if (exp10 >= 0) {\n d *= wuffs_base__private_implementation__f64_powers_of_10[+exp10];\n } else {\n d /= wuffs_base__private_implementation__f64_powers_of_10[-exp10];\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = negative ? -d : +d;\n return ret;\n }\n\n // The wuffs_base__private_implementation__parse_number_f64_eisel\n // preconditions include that man is non-zero. Parsing \"0\" should be caught\n // by the \"If man and exp10 are small enough\" above, but \"0e99\" might not.\n if (man == 0) {\n goto fallback;\n }\n\n // Our man and exp10 are in range. Run the Eisel algorithm.\n int64_t r =\n wuffs_base__private_implementation__parse_number_f64_eisel(man, exp10);\n i" + - "f (r < 0) {\n goto fallback;\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(\n ((uint64_t)r) | (((uint64_t)negative) << 63));\n return ret;\n } while (0);\n\nfallback:\n do {\n wuffs_base__private_implementation__high_prec_dec h;\n wuffs_base__status status =\n wuffs_base__private_implementation__high_prec_dec__parse(&h, s,\n options);\n if (status.repr) {\n return wuffs_base__private_implementation__parse_number_f64_special(\n s, options);\n }\n return wuffs_base__private_implementation__high_prec_dec__to_f64(&h,\n options);\n } while (0);\n}\n\n" + + " ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n (nan ? 0x7FFFFFFFFFFFFFFF : 0x7FF0000000000000) |\n (negative ? 0x8000000000000000 : 0));\n return ret;\n } while (0);\n\nfail:\n do {\n wuffs_base__result_f64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__private_implementation__high_prec_dec__to_f64(\n wuffs_base__private_implementation__high_prec_dec* h,\n uint32_t options) {\n do {\n // powers converts decimal powers of 10 to binary powers of 2. For example,\n // (10000 >> 13) is 1. It stops before the elements exceed 60, also known\n // as WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL.\n static const uint32_t num_powers = 19;\n static const uint8_t powers[19] = {\n 0, 3, 6, 9, 13, 16, 19, 23, 26, 29, //\n 33, 36, 39, 43, 46, 49, 53, 56, 59, //\n };\n\n // Handl" + + "e zero and obvious extremes. The largest and smallest positive\n // finite f64 values are approximately 1.8e+308 and 4.9e-324.\n if ((h->num_digits == 0) || (h->decimal_point < -326)) {\n goto zero;\n } else if (h->decimal_point > 310) {\n goto infinity;\n }\n\n // Try the fast Eisel algorithm again. Calculating the (man, exp10) pair\n // from the high_prec_dec h is more correct but slower than the approach\n // taken in wuffs_base__parse_number_f64. The latter is optimized for the\n // common cases (e.g. assuming no underscores or a leading '+' sign) rather\n // than the full set of cases allowed by the Wuffs API.\n if (h->num_digits <= 19) {\n uint64_t man = 0;\n uint32_t i;\n for (i = 0; i < h->num_digits; i++) {\n man = (10 * man) + h->digits[i];\n }\n int32_t exp10 = h->decimal_point - ((int32_t)(h->num_digits));\n if ((man != 0) && (-326 <= exp10) && (exp10 <= 310)) {\n int64_t r = wuffs_base__private_implementation__parse_number_f64_eisel(\n" + + " man, exp10);\n if (r >= 0) {\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n ((uint64_t)r) | (((uint64_t)(h->negative)) << 63));\n return ret;\n }\n }\n }\n\n // Scale by powers of 2 until we're in the range [½ .. 1], which gives us\n // our exponent (in base-2). First we shift right, possibly a little too\n // far, ending with a value certainly below 1 and possibly below ½...\n const int32_t f64_bias = -1023;\n int32_t exp2 = 0;\n while (h->decimal_point > 0) {\n uint32_t n = (uint32_t)(+h->decimal_point);\n uint32_t shift =\n (n < num_powers)\n ? powers[n]\n : WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n\n wuffs_base__private_implementation__high_prec_dec__small_rshift(h, shift);\n if (h->decimal_point <\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n " + + " goto zero;\n }\n exp2 += (int32_t)shift;\n }\n // ...then we shift left, putting us in [½ .. 1].\n while (h->decimal_point <= 0) {\n uint32_t shift;\n if (h->decimal_point == 0) {\n if (h->digits[0] >= 5) {\n break;\n }\n shift = (h->digits[0] <= 2) ? 2 : 1;\n } else {\n uint32_t n = (uint32_t)(-h->decimal_point);\n shift = (n < num_powers)\n ? powers[n]\n : WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n }\n\n wuffs_base__private_implementation__high_prec_dec__small_lshift(h, shift);\n if (h->decimal_point >\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n goto infinity;\n }\n exp2 -= (int32_t)shift;\n }\n\n // We're in the range [½ .. 1] but f64 uses [1 .. 2].\n exp2--;\n\n // The minimum normal exponent is (f64_bias + 1).\n while ((f64_bias + 1) > exp2) {\n uint32_t n = (uint32_t)((f64_bias + 1) - exp2);\n if (n > WUFF" + + "S_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) {\n n = WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n }\n wuffs_base__private_implementation__high_prec_dec__small_rshift(h, n);\n exp2 += (int32_t)n;\n }\n\n // Check for overflow.\n if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1.\n goto infinity;\n }\n\n // Extract 53 bits for the mantissa (in base-2).\n wuffs_base__private_implementation__high_prec_dec__small_lshift(h, 53);\n uint64_t man2 =\n wuffs_base__private_implementation__high_prec_dec__rounded_integer(h);\n\n // Rounding might have added one bit. If so, shift and re-check overflow.\n if ((man2 >> 53) != 0) {\n man2 >>= 1;\n exp2++;\n if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1.\n goto infinity;\n }\n }\n\n // Handle subnormal numbers.\n if ((man2 >> 52) == 0) {\n exp2 = f64_bias;\n }\n\n // Pack the bits and return.\n uint64_t exp2_bits =\n (uint64_t)((exp2 - f64_bias) & 0x07FF); " + + " // (1 << 11) - 1.\n uint64_t bits = (man2 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1.\n (exp2_bits << 52) | //\n (h->negative ? 0x8000000000000000 : 0); // (1 << 63).\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(bits);\n return ret;\n } while (0);\n\nzero:\n do {\n uint64_t bits = h->negative ? 0x8000000000000000 : 0;\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(bits);\n return ret;\n } while (0);\n\ninfinity:\n do {\n if (options & WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN) {\n wuffs_base__result_f64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n }\n\n uint64_t bits = h->negative ? 0xFFF0000000000000 : 0x7FF0000000000000;\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n " + + " ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(bits);\n return ret;\n } while (0);\n}\n\nstatic inline bool //\nwuffs_base__private_implementation__is_decimal_digit(uint8_t c) {\n return ('0' <= c) && (c <= '9');\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {\n // In practice, almost all \"dd.ddddE±xxx\" numbers can be represented\n // losslessly by a uint64_t mantissa \"dddddd\" and an int32_t base-10\n // exponent, adjusting \"xxx\" for the position (if present) of the decimal\n // separator '.' or ','.\n //\n // This (u64 man, i32 exp10) data structure is superficially similar to the\n // \"Do It Yourself Floating Point\" type from Loitsch (†), but the exponent\n // here is base-10, not base-2.\n //\n // If s's number fits in a (man, exp10), parse that pair with the Eisel\n // algorithm. If not, or if Eisel fails, parsing s with the fallback\n // algorithm is slower but comprehensive.\n //\n // † \"Printing" + + " Floating-Point Numbers Quickly and Accurately with Integers\"\n // (https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf).\n // Florian Loitsch is also the primary contributor to\n // https://github.com/google/double-conversion\n do {\n // Calculating that (man, exp10) pair needs to stay within s's bounds.\n // Provided that s isn't extremely long, work on a NUL-terminated copy of\n // s's contents. The NUL byte isn't a valid part of \"±dd.ddddE±xxx\".\n //\n // As the pointer p walks the contents, it's faster to repeatedly check \"is\n // *p a valid digit\" than \"is p within bounds and *p a valid digit\".\n if (s.len >= 256) {\n goto fallback;\n }\n uint8_t z[256];\n memcpy(&z[0], s.ptr, s.len);\n z[s.len] = 0;\n const uint8_t* p = &z[0];\n\n // Look for a leading minus sign. Technically, we could also look for an\n // optional plus sign, but the \"script/process-json-numbers.c with -p\"\n // benchmark is noticably slower if we do. It's optional and, in practice,\n" + + " // usually absent. Let the fallback catch it.\n bool negative = (*p == '-');\n if (negative) {\n p++;\n }\n\n // After walking \"dd.dddd\", comparing p later with p now will produce the\n // number of \"d\"s and \".\"s.\n const uint8_t* const start_of_digits_ptr = p;\n\n // Walk the \"d\"s before a '.', 'E', NUL byte, etc. If it starts with '0',\n // it must be a single '0'. If it starts with a non-zero decimal digit, it\n // can be a sequence of decimal digits.\n //\n // Update the man variable during the walk. It's OK if man overflows now.\n // We'll detect that later.\n uint64_t man;\n if (*p == '0') {\n man = 0;\n p++;\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n } else if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n man = ((uint8_t)(*p - '0'));\n p++;\n for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {\n man = (10 * man) + ((uint8_t)(*p - '0'));\n }\n " + + " } else {\n goto fallback;\n }\n\n // Walk the \"d\"s after the optional decimal separator ('.' or ','),\n // updating the man and exp10 variables.\n int32_t exp10 = 0;\n if (*p ==\n ((options & WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n const uint8_t* first_after_separator_ptr = p;\n if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n man = (10 * man) + ((uint8_t)(*p - '0'));\n p++;\n for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {\n man = (10 * man) + ((uint8_t)(*p - '0'));\n }\n exp10 = ((int32_t)(first_after_separator_ptr - p));\n }\n\n // Count the number of digits:\n // - for an input of \"314159\", digit_count is 6.\n // - for an input of \"3.14159\", digit_count is 7.\n //\n // This is off-by-one if there is a decimal separator. That's OK for now.\n // We'll correct for that later. The \"script/proc" + + "ess-json-numbers.c with\n // -p\" benchmark is noticably slower if we try to correct for that now.\n uint32_t digit_count = (uint32_t)(p - start_of_digits_ptr);\n\n // Update exp10 for the optional exponent, starting with 'E' or 'e'.\n if ((*p | 0x20) == 'e') {\n p++;\n int32_t exp_sign = +1;\n if (*p == '-') {\n p++;\n exp_sign = -1;\n } else if (*p == '+') {\n p++;\n }\n if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n int32_t exp_num = ((uint8_t)(*p - '0'));\n p++;\n // The rest of the exp_num walking has a peculiar control flow but, once\n // again, the \"script/process-json-numbers.c with -p\" benchmark is\n // sensitive to alternative formulations.\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n exp_num = (10 * exp" + + "_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n while (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n if (exp_num > 0x1000000) {\n goto fallback;\n }\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n exp10 += exp_sign * exp_num;\n }\n\n // The Wuffs API is that the original slice has no trailing data. It also\n // allows underscores, which we don't catch here but the fallback should.\n if (p != &z[s.len]) {\n goto fallback;\n }\n\n // Check that the uint64_t typed man variable has not overflowed, based on\n // digit_count.\n //\n // For reference:\n // - (1 << 63) is 9223372036854775808, which has 19 decimal digits.\n // - (1 << 64) is 18446744073709551616, which has 20 decimal digits.\n // - 19 nines, 9999999999999999999, is 0x8AC7230489E7FFFF, which has 64\n // bits and 16 hexadecimal digits.\n // - 20 nines, 99999999999999999999, is 0x56BC75E2D630FFFFF, which has 67\n // bits a" + + "nd 17 hexadecimal digits.\n if (digit_count > 19) {\n // Even if we have more than 19 pseudo-digits, it's not yet definitely an\n // overflow. Recall that digit_count might be off-by-one (too large) if\n // there's a decimal separator. It will also over-report the number of\n // meaningful digits if the input looks something like \"0.000dddExxx\".\n //\n // We adjust by the number of leading '0's and '.'s and re-compare to 19.\n // Once again, technically, we could skip ','s too, but that perturbs the\n // \"script/process-json-numbers.c with -p\" benchmark.\n const uint8_t* q = start_of_digits_ptr;\n for (; (*q == '0') || (*q == '.'); q++) {\n }\n digit_count -= (uint32_t)(q - start_of_digits_ptr);\n if (digit_count > 19) {\n goto fallback;\n }\n }\n\n // The wuffs_base__private_implementation__parse_number_f64_eisel\n // preconditions include that exp10 is in the range -326 ..= 310.\n if ((exp10 < -326) || (310 < exp10)) {\n goto fallbac" + + "k;\n }\n\n // If man and exp10 are small enough, all three of (man), (10 ** exp10) and\n // (man ** (10 ** exp10)) are exactly representable by a double. We don't\n // need to run the Eisel algorithm.\n if ((-22 <= exp10) && (exp10 <= 22) && ((man >> 53) == 0)) {\n double d = (double)man;\n if (exp10 >= 0) {\n d *= wuffs_base__private_implementation__f64_powers_of_10[+exp10];\n } else {\n d /= wuffs_base__private_implementation__f64_powers_of_10[-exp10];\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = negative ? -d : +d;\n return ret;\n }\n\n // The wuffs_base__private_implementation__parse_number_f64_eisel\n // preconditions include that man is non-zero. Parsing \"0\" should be caught\n // by the \"If man and exp10 are small enough\" above, but \"0e99\" might not.\n if (man == 0) {\n goto fallback;\n }\n\n // Our man and exp10 are in range. Run the Eisel algorithm.\n int64_t r =\n wuffs_base__private_implementati" + + "on__parse_number_f64_eisel(man, exp10);\n if (r < 0) {\n goto fallback;\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n ((uint64_t)r) | (((uint64_t)negative) << 63));\n return ret;\n } while (0);\n\nfallback:\n do {\n wuffs_base__private_implementation__high_prec_dec h;\n wuffs_base__status status =\n wuffs_base__private_implementation__high_prec_dec__parse(&h, s,\n options);\n if (status.repr) {\n return wuffs_base__private_implementation__parse_number_f64_special(\n s, options);\n }\n return wuffs_base__private_implementation__high_prec_dec__to_f64(&h,\n options);\n } while (0);\n}\n\n" + "" + "// --------\n\nstatic inline size_t //\nwuffs_base__private_implementation__render_inf(wuffs_base__slice_u8 dst,\n bool neg,\n uint32_t options) {\n if (neg) {\n if (dst.len < 4) {\n return 0;\n }\n wuffs_base__store_u32le__no_bounds_check(dst.ptr, 0x666E492D); // '-Inf'le.\n return 4;\n }\n\n if (options & WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN) {\n if (dst.len < 4) {\n return 0;\n }\n wuffs_base__store_u32le__no_bounds_check(dst.ptr, 0x666E492B); // '+Inf'le.\n return 4;\n }\n\n if (dst.len < 3) {\n return 0;\n }\n wuffs_base__store_u24le__no_bounds_check(dst.ptr, 0x666E49); // 'Inf'le.\n return 3;\n}\n\nstatic inline size_t //\nwuffs_base__private_implementation__render_nan(wuffs_base__slice_u8 dst) {\n if (dst.len < 3) {\n return 0;\n }\n wuffs_base__store_u24le__no_bounds_check(dst.ptr, 0x4E614E); // 'NaN'le.\n return 3;\n}\n\nstatic size_t //\nwuffs_base__private_implementation__high" + "_prec_dec__render_exponent_absent(\n wuffs_base__slice_u8 dst,\n wuffs_base__private_implementation__high_prec_dec* h,\n uint32_t precision,\n uint32_t options) {\n size_t n = (h->negative ||\n (options & WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN))\n ? 1\n : 0;\n if (h->decimal_point <= 0) {\n n += 1;\n } else {\n n += (size_t)(h->decimal_point);\n }\n if (precision > 0) {\n n += precision + 1; // +1 for the '.'.\n }\n\n // Don't modify dst if the formatted number won't fit.\n if (n > dst.len) {\n return 0;\n }\n\n // Align-left or align-right.\n uint8_t* ptr = (options & WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT)\n ? &dst.ptr[dst.len - n]\n : &dst.ptr[0];\n\n // Leading \"±\".\n if (h->negative) {\n *ptr++ = '-';\n } else if (options & WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN) {\n *ptr++ = '+';\n }\n\n // Integral digits.\n if (h->decimal_point <= 0) {\n *ptr++ = '0';\n } else {\n uint32_t m =\n" + " wuffs_base__u32__min(h->num_digits, (uint32_t)(h->decimal_point));\n uint32_t i = 0;\n for (; i < m; i++) {\n *ptr++ = (uint8_t)('0' | h->digits[i]);\n }\n for (; i < (uint32_t)(h->decimal_point); i++) {\n *ptr++ = '0';\n }\n }\n\n // Separator and then fractional digits.\n if (precision > 0) {\n *ptr++ =\n (options & WUFFS_BASE__RENDER_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.';\n uint32_t i = 0;\n for (; i < precision; i++) {\n uint32_t j = ((uint32_t)(h->decimal_point)) + i;\n *ptr++ = (uint8_t)('0' | ((j < h->num_digits) ? h->digits[j] : 0));\n }\n }\n\n return n;\n}\n\nstatic size_t //\nwuffs_base__private_implementation__high_prec_dec__render_exponent_present(\n wuffs_base__slice_u8 dst,\n wuffs_base__private_implementation__high_prec_dec* h,\n uint32_t precision,\n uint32_t options) {\n int32_t exp = 0;\n if (h->num_digits > 0) {\n exp = h->decimal_point - 1;\n }\n bool negative_exp = exp < 0;\n if (negative_exp) {\n" + " exp = -exp;\n }\n\n size_t n = (h->negative ||\n (options & WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN))\n ? 4\n : 3; // Mininum 3 bytes: first digit and then \"e±\".\n if (precision > 0) {\n n += precision + 1; // +1 for the '.'.\n }\n n += (exp < 100) ? 2 : 3;\n\n // Don't modify dst if the formatted number won't fit.\n if (n > dst.len) {\n return 0;\n }\n\n // Align-left or align-right.\n uint8_t* ptr = (options & WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT)\n ? &dst.ptr[dst.len - n]\n : &dst.ptr[0];\n\n // Leading \"±\".\n if (h->negative) {\n *ptr++ = '-';\n } else if (options & WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN) {\n *ptr++ = '+';\n }\n\n // Integral digit.\n if (h->num_digits > 0) {\n *ptr++ = (uint8_t)('0' | h->digits[0]);\n } else {\n *ptr++ = '0';\n }\n\n // Separator and then fractional digits.\n if (precision > 0) {\n *ptr++ =\n (options & WUFFS_BASE__RENDER_NUMBER_FXX__DECIMAL_SEPA" + "RATOR_IS_A_COMMA)\n ? ','\n : '.';\n uint32_t i = 1;\n uint32_t j = wuffs_base__u32__min(h->num_digits, precision + 1);\n for (; i < j; i++) {\n *ptr++ = (uint8_t)('0' | h->digits[i]);\n }\n for (; i <= precision; i++) {\n *ptr++ = '0';\n }\n }\n\n // Exponent: \"e±\" and then 2 or 3 digits.\n *ptr++ = 'e';\n *ptr++ = negative_exp ? '-' : '+';\n if (exp < 10) {\n *ptr++ = '0';\n *ptr++ = (uint8_t)('0' | exp);\n } else if (exp < 100) {\n *ptr++ = (uint8_t)('0' | (exp / 10));\n *ptr++ = (uint8_t)('0' | (exp % 10));\n } else {\n int32_t e = exp / 100;\n exp -= e * 100;\n *ptr++ = (uint8_t)('0' | e);\n *ptr++ = (uint8_t)('0' | (exp / 10));\n *ptr++ = (uint8_t)('0' | (exp % 10));\n }\n\n return n;\n}\n\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_f64(wuffs_base__slice_u8 dst,\n double x,\n uint32_t precision,\n uint32_t options) {\n // Decompose x (64 bits) into " + - "negativity (1 bit), base-2 exponent (11 bits\n // with a -1023 bias) and mantissa (52 bits).\n uint64_t bits = wuffs_base__ieee_754_bit_representation__from_f64(x);\n bool neg = (bits >> 63) != 0;\n int32_t exp2 = ((int32_t)(bits >> 52)) & 0x7FF;\n uint64_t man = bits & 0x000FFFFFFFFFFFFFul;\n\n // Apply the exponent bias and set the implicit top bit of the mantissa,\n // unless x is subnormal. Also take care of Inf and NaN.\n if (exp2 == 0x7FF) {\n if (man != 0) {\n return wuffs_base__private_implementation__render_nan(dst);\n }\n return wuffs_base__private_implementation__render_inf(dst, neg, options);\n } else if (exp2 == 0) {\n exp2 = -1022;\n } else {\n exp2 -= 1023;\n man |= 0x0010000000000000ul;\n }\n\n // Ensure that precision isn't too large.\n if (precision > 4095) {\n precision = 4095;\n }\n\n // Convert from the (neg, exp2, man) tuple to an HPD.\n wuffs_base__private_implementation__high_prec_dec h;\n wuffs_base__private_implementation__high_prec_dec__assign(&h, man, neg);\n if (h.n" + - "um_digits > 0) {\n wuffs_base__private_implementation__high_prec_dec__lshift(\n &h, exp2 - 52); // 52 mantissa bits.\n }\n\n // Handle the \"%e\" and \"%f\" formats.\n switch (options & (WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT |\n WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT)) {\n case WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT: // The \"%\"f\" format.\n if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {\n wuffs_base__private_implementation__high_prec_dec__round_just_enough(\n &h, exp2, man);\n int32_t p = ((int32_t)(h.num_digits)) - h.decimal_point;\n precision = ((uint32_t)(wuffs_base__i32__max(0, p)));\n } else {\n wuffs_base__private_implementation__high_prec_dec__round_nearest(\n &h, ((int32_t)precision) + h.decimal_point);\n }\n return wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(\n dst, &h, precision, options);\n\n case WUFFS_BASE__RENDER_NUMBER_FXX__" + - "EXPONENT_PRESENT: // The \"%e\" format.\n if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {\n wuffs_base__private_implementation__high_prec_dec__round_just_enough(\n &h, exp2, man);\n precision = (h.num_digits > 0) ? (h.num_digits - 1) : 0;\n } else {\n wuffs_base__private_implementation__high_prec_dec__round_nearest(\n &h, ((int32_t)precision) + 1);\n }\n return wuffs_base__private_implementation__high_prec_dec__render_exponent_present(\n dst, &h, precision, options);\n }\n\n // We have the \"%g\" format and so precision means the number of significant\n // digits, not the number of digits after the decimal separator. Perform\n // rounding and determine whether to use \"%e\" or \"%f\".\n int32_t e_threshold = 0;\n if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {\n wuffs_base__private_implementation__high_prec_dec__round_just_enough(\n &h, exp2, man);\n precision = h.num_digits;\n e_threshold = 6;\n } el" + - "se {\n if (precision == 0) {\n precision = 1;\n }\n wuffs_base__private_implementation__high_prec_dec__round_nearest(\n &h, ((int32_t)precision));\n e_threshold = ((int32_t)precision);\n int32_t nd = ((int32_t)(h.num_digits));\n if ((e_threshold > nd) && (nd >= h.decimal_point)) {\n e_threshold = nd;\n }\n }\n\n // Use the \"%e\" format if the exponent is large.\n int32_t e = h.decimal_point - 1;\n if ((e < -4) || (e_threshold <= e)) {\n uint32_t p = wuffs_base__u32__min(precision, h.num_digits);\n return wuffs_base__private_implementation__high_prec_dec__render_exponent_present(\n dst, &h, (p > 0) ? (p - 1) : 0, options);\n }\n\n // Use the \"%f\" format otherwise.\n int32_t p = ((int32_t)precision);\n if (p > h.decimal_point) {\n p = ((int32_t)(h.num_digits));\n }\n precision = ((uint32_t)(wuffs_base__i32__max(0, p - h.decimal_point)));\n return wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(\n dst, &h, precision, options);\n}\n" + + "negativity (1 bit), base-2 exponent (11 bits\n // with a -1023 bias) and mantissa (52 bits).\n uint64_t bits = wuffs_base__ieee_754_bit_representation__from_f64_to_u64(x);\n bool neg = (bits >> 63) != 0;\n int32_t exp2 = ((int32_t)(bits >> 52)) & 0x7FF;\n uint64_t man = bits & 0x000FFFFFFFFFFFFFul;\n\n // Apply the exponent bias and set the implicit top bit of the mantissa,\n // unless x is subnormal. Also take care of Inf and NaN.\n if (exp2 == 0x7FF) {\n if (man != 0) {\n return wuffs_base__private_implementation__render_nan(dst);\n }\n return wuffs_base__private_implementation__render_inf(dst, neg, options);\n } else if (exp2 == 0) {\n exp2 = -1022;\n } else {\n exp2 -= 1023;\n man |= 0x0010000000000000ul;\n }\n\n // Ensure that precision isn't too large.\n if (precision > 4095) {\n precision = 4095;\n }\n\n // Convert from the (neg, exp2, man) tuple to an HPD.\n wuffs_base__private_implementation__high_prec_dec h;\n wuffs_base__private_implementation__high_prec_dec__assign(&h, man, neg);\n " + + "if (h.num_digits > 0) {\n wuffs_base__private_implementation__high_prec_dec__lshift(\n &h, exp2 - 52); // 52 mantissa bits.\n }\n\n // Handle the \"%e\" and \"%f\" formats.\n switch (options & (WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT |\n WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT)) {\n case WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT: // The \"%\"f\" format.\n if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {\n wuffs_base__private_implementation__high_prec_dec__round_just_enough(\n &h, exp2, man);\n int32_t p = ((int32_t)(h.num_digits)) - h.decimal_point;\n precision = ((uint32_t)(wuffs_base__i32__max(0, p)));\n } else {\n wuffs_base__private_implementation__high_prec_dec__round_nearest(\n &h, ((int32_t)precision) + h.decimal_point);\n }\n return wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(\n dst, &h, precision, options);\n\n case WUFFS_BASE__RENDER_NUMBE" + + "R_FXX__EXPONENT_PRESENT: // The \"%e\" format.\n if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {\n wuffs_base__private_implementation__high_prec_dec__round_just_enough(\n &h, exp2, man);\n precision = (h.num_digits > 0) ? (h.num_digits - 1) : 0;\n } else {\n wuffs_base__private_implementation__high_prec_dec__round_nearest(\n &h, ((int32_t)precision) + 1);\n }\n return wuffs_base__private_implementation__high_prec_dec__render_exponent_present(\n dst, &h, precision, options);\n }\n\n // We have the \"%g\" format and so precision means the number of significant\n // digits, not the number of digits after the decimal separator. Perform\n // rounding and determine whether to use \"%e\" or \"%f\".\n int32_t e_threshold = 0;\n if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {\n wuffs_base__private_implementation__high_prec_dec__round_just_enough(\n &h, exp2, man);\n precision = h.num_digits;\n e_threshold = 6;" + + "\n } else {\n if (precision == 0) {\n precision = 1;\n }\n wuffs_base__private_implementation__high_prec_dec__round_nearest(\n &h, ((int32_t)precision));\n e_threshold = ((int32_t)precision);\n int32_t nd = ((int32_t)(h.num_digits));\n if ((e_threshold > nd) && (nd >= h.decimal_point)) {\n e_threshold = nd;\n }\n }\n\n // Use the \"%e\" format if the exponent is large.\n int32_t e = h.decimal_point - 1;\n if ((e < -4) || (e_threshold <= e)) {\n uint32_t p = wuffs_base__u32__min(precision, h.num_digits);\n return wuffs_base__private_implementation__high_prec_dec__render_exponent_present(\n dst, &h, (p > 0) ? (p - 1) : 0, options);\n }\n\n // Use the \"%f\" format otherwise.\n int32_t p = ((int32_t)precision);\n if (p > h.decimal_point) {\n p = ((int32_t)(h.num_digits));\n }\n precision = ((uint32_t)(wuffs_base__i32__max(0, p - h.decimal_point)));\n return wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(\n dst, &h, precision, options);\n}\n" + "" const BaseFloatConvSubmoduleDataC = "" + @@ -519,7 +519,8 @@ const BaseStrConvPublicH = "" + "tespace, leading or otherwise.\n// - It does not allow hexadecimal floating point numbers.\n// - It is not affected by i18n / l10n settings such as environment variables.\n//\n// The options argument can change these, but by default, it:\n// - Allows \"inf\", \"+Infinity\" and \"-NAN\", case insensitive. Similarly,\n// without an explicit opt-out, it would successfully parse \"1e999\" as\n// infinity, even though it overflows double-precision floating point.\n// - Rejects underscores. With an explicit opt-in, \"_3.141_592\" would\n// successfully parse as an approximation to π.\n// - Rejects unnecessary leading zeroes: \"00\", \"0644\" and \"00.7\".\n// - Uses a dot '1.5' instead of a comma '1,5' for the decimal separator.\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__FLOATCONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wuffs_base__slice_" + "u8 s, uint32_t options);\n\n" + "" + - "// --------\n\n// wuffs_base__ieee_754_bit_representation__etc converts between a double\n// precision numerical value and its IEEE 754 64-bit representation (1 sign\n// bit, 11 exponent bits, 52 explicit significand bits).\n//\n// For example, it converts between:\n// - +1.0 and 0x3FF0_0000_0000_0000.\n// - +5.5 and 0x4016_0000_0000_0000.\n// - -inf and 0xFFF0_0000_0000_0000.\n//\n// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format\n\nstatic inline uint64_t //\nwuffs_base__ieee_754_bit_representation__from_f64(double f) {\n uint64_t u = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&u, &f, sizeof(uint64_t));\n }\n return u;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__to_f64(uint64_t u) {\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &u, sizeof(uint64_t));\n }\n return f;\n}\n\n" + + "// --------\n\n// wuffs_base__ieee_754_bit_representation__etc converts between a double\n// precision numerical value and its IEEE 754 representations:\n// - 16-bit: 1 sign bit, 5 exponent bits, 10 explicit significand bits.\n// - 32-bit: 1 sign bit, 8 exponent bits, 23 explicit significand bits.\n// - 64-bit: 1 sign bit, 11 exponent bits, 52 explicit significand bits.\n//\n// For example, it converts between:\n// - +1.0 and 0x3C00, 0x3F80_0000 or 0x3FF0_0000_0000_0000.\n// - +5.5 and 0x4580, 0x40B0_0000 or 0x4016_0000_0000_0000.\n// - -inf and 0xFC00, 0xFF80_0000 or 0xFFF0_0000_0000_0000.\n//\n// Converting from f64 to shorter formats may be lossy.\n//\n// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format\n\nstatic inline uint64_t //\nwuffs_base__ieee_754_bit_representation__from_f64_to_u64(double f) {\n uint64_t u = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&u, &f, sizeof(uint64_t));\n }\n return u;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__from_u" + + "64_to_f64(uint64_t u) {\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &u, sizeof(uint64_t));\n }\n return f;\n}\n\n" + "" + "// ---------------- Integer\n\n// wuffs_base__parse_number_i64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"-123\" then it will return the int64_t -123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow an int64_t.\n//\n// It is similar to wuffs_base__parse_number_u64 but it returns a signed\n// integer, not an unsigned integer. It also allows a leading '+' or '-'.\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__INTCONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 //\nwuffs_base__parse_number_i64(wuffs_base__slice_u8 s, uint32_t options);\n\n// wuffs_base__parse_number_u64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"123\" then it will return the uint64_t 123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow a uint64" + "_t.\n//\n// It is similar to the C standard library's strtoull function, but:\n// - Errors are returned in-band (in a result type), not out-of-band (errno).\n// - It takes a slice (a pointer and length), not a NUL-terminated C string.\n// - It does not take an optional endptr argument. It does not allow a partial\n// parse: it returns an error unless all of s is consumed.\n// - It does not allow whitespace, leading or otherwise.\n// - It does not allow a leading '+' or '-'.\n// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it\n// always accepts both decimal (e.g \"1234\", \"0d5678\") and hexadecimal (e.g.\n// \"0x9aBC\"). The caller is responsible for prior filtering of e.g. hex\n// numbers if they are unwanted. For example, Wuffs' JSON decoder will only\n// produce a wuffs_base__token for decimal numbers, not hexadecimal.\n// - It is not affected by i18n / l10n settings such as environment variables.\n//\n// The options argument can change these, but by default, it:\n// - Rejects und" + diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index ed609e79..9e854e9e 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -4054,18 +4054,22 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options); // -------- // wuffs_base__ieee_754_bit_representation__etc converts between a double -// precision numerical value and its IEEE 754 64-bit representation (1 sign -// bit, 11 exponent bits, 52 explicit significand bits). +// precision numerical value and its IEEE 754 representations: +// - 16-bit: 1 sign bit, 5 exponent bits, 10 explicit significand bits. +// - 32-bit: 1 sign bit, 8 exponent bits, 23 explicit significand bits. +// - 64-bit: 1 sign bit, 11 exponent bits, 52 explicit significand bits. // // For example, it converts between: -// - +1.0 and 0x3FF0_0000_0000_0000. -// - +5.5 and 0x4016_0000_0000_0000. -// - -inf and 0xFFF0_0000_0000_0000. +// - +1.0 and 0x3C00, 0x3F80_0000 or 0x3FF0_0000_0000_0000. +// - +5.5 and 0x4580, 0x40B0_0000 or 0x4016_0000_0000_0000. +// - -inf and 0xFC00, 0xFF80_0000 or 0xFFF0_0000_0000_0000. +// +// Converting from f64 to shorter formats may be lossy. // // See https://en.wikipedia.org/wiki/Double-precision_floating-point_format static inline uint64_t // -wuffs_base__ieee_754_bit_representation__from_f64(double f) { +wuffs_base__ieee_754_bit_representation__from_f64_to_u64(double f) { uint64_t u = 0; if (sizeof(uint64_t) == sizeof(double)) { memcpy(&u, &f, sizeof(uint64_t)); @@ -4074,7 +4078,7 @@ wuffs_base__ieee_754_bit_representation__from_f64(double f) { } static inline double // -wuffs_base__ieee_754_bit_representation__to_f64(uint64_t u) { +wuffs_base__ieee_754_bit_representation__from_u64_to_f64(uint64_t u) { double f = 0; if (sizeof(uint64_t) == sizeof(double)) { memcpy(&f, &u, sizeof(uint64_t)); @@ -10964,7 +10968,7 @@ wuffs_base__private_implementation__parse_number_f64_special( } wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64( + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64( (nan ? 0x7FFFFFFFFFFFFFFF : 0x7FF0000000000000) | (negative ? 0x8000000000000000 : 0)); return ret; @@ -11019,7 +11023,7 @@ wuffs_base__private_implementation__high_prec_dec__to_f64( if (r >= 0) { wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64( + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64( ((uint64_t)r) | (((uint64_t)(h->negative)) << 63)); return ret; } @@ -11114,7 +11118,7 @@ wuffs_base__private_implementation__high_prec_dec__to_f64( wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits); + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(bits); return ret; } while (0); @@ -11124,7 +11128,7 @@ zero: wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits); + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(bits); return ret; } while (0); @@ -11141,7 +11145,7 @@ infinity: wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits); + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(bits); return ret; } while (0); } @@ -11357,7 +11361,7 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) { } wuffs_base__result_f64 ret; ret.status.repr = NULL; - ret.value = wuffs_base__ieee_754_bit_representation__to_f64( + ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64( ((uint64_t)r) | (((uint64_t)negative) << 63)); return ret; } while (0); @@ -11573,7 +11577,7 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst, uint32_t options) { // Decompose x (64 bits) into negativity (1 bit), base-2 exponent (11 bits // with a -1023 bias) and mantissa (52 bits). - uint64_t bits = wuffs_base__ieee_754_bit_representation__from_f64(x); + uint64_t bits = wuffs_base__ieee_754_bit_representation__from_f64_to_u64(x); bool neg = (bits >> 63) != 0; int32_t exp2 = ((int32_t)(bits >> 52)) & 0x7FF; uint64_t man = bits & 0x000FFFFFFFFFFFFFul; diff --git a/test/c/std/json.c b/test/c/std/json.c index f6e498ba..898a48f3 100644 --- a/test/c/std/json.c +++ b/test/c/std/json.c @@ -601,7 +601,7 @@ test_wuffs_strconv_parse_number_f64_options() { CHECK_STATUS("ALLOW_MULTIPLE_LEADING_ZEROES on", r.status); uint64_t have = - wuffs_base__ieee_754_bit_representation__from_f64(r.value); + wuffs_base__ieee_754_bit_representation__from_f64_to_u64(r.value); uint64_t want = 0x3FF4000000000000; if (have != want) { RETURN_FAIL("ALLOW_MULTIPLE_LEADING_ZEROES on: have 0x%016" PRIX64 @@ -631,7 +631,7 @@ test_wuffs_strconv_parse_number_f64_options() { CHECK_STATUS("ALLOW_UNDERSCORES on", r.status); uint64_t have = - wuffs_base__ieee_754_bit_representation__from_f64(r.value); + wuffs_base__ieee_754_bit_representation__from_f64_to_u64(r.value); uint64_t want = 0x3FF4000000000000; if (have != want) { RETURN_FAIL("ALLOW_UNDERSCORES on: have 0x%016" PRIX64 @@ -662,7 +662,7 @@ test_wuffs_strconv_parse_number_f64_options() { CHECK_STATUS("DECIMAL_SEPARATOR_IS_A_COMMA on", r.status); uint64_t have = - wuffs_base__ieee_754_bit_representation__from_f64(r.value); + wuffs_base__ieee_754_bit_representation__from_f64_to_u64(r.value); uint64_t want = 0x3FFC000000000000; if (have != want) { RETURN_FAIL("DECIMAL_SEPARATOR_IS_A_COMMA on: have 0x%016" PRIX64 @@ -829,7 +829,7 @@ test_wuffs_strconv_parse_number_f64_regular() { WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES); uint64_t have = (r.status.repr == NULL) - ? wuffs_base__ieee_754_bit_representation__from_f64(r.value) + ? wuffs_base__ieee_754_bit_representation__from_f64_to_u64(r.value) : fail; if (have != test_cases[tc].want) { RETURN_FAIL("\"%s\": have 0x%" PRIX64 ", want 0x%" PRIX64, @@ -1690,8 +1690,8 @@ test_wuffs_strconv_render_number_f64() { int tc; for (tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) { - double f64 = - wuffs_base__ieee_754_bit_representation__to_f64(test_cases[tc].x); + double f64 = wuffs_base__ieee_754_bit_representation__from_u64_to_f64( + test_cases[tc].x); int o; for (o = 0; o < 6; o++) { uint32_t precision = 0; @@ -3530,7 +3530,8 @@ do_bench_wuffs_strconv_render_number_f64(wuffs_base__slice_u64 test_cases, for (tc = 0; tc < test_cases.len; tc++) { size_t n = wuffs_base__render_number_f64( g_have_slice_u8, - wuffs_base__ieee_754_bit_representation__to_f64(test_cases.ptr[tc]), + wuffs_base__ieee_754_bit_representation__from_u64_to_f64( + test_cases.ptr[tc]), 0, WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION); if (n == 0) { RETURN_FAIL("0x%016" PRIX64 ": failed", test_cases.ptr[tc]);