mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Add IEEE 754 from_f64_to_u{16,32} functions
This commit is contained in:
1 parent
0899b82c42
commit
7bf7cf28b9
5 files changed
+370
-18
No files matched your search
@@ -16,6 +16,136 @@
|
||||
|
||||
// ---------------- IEEE 754 Floating Point
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u16 //
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u16(double f) {
|
||||
uint64_t u = 0;
|
||||
if (sizeof(uint64_t) == sizeof(double)) {
|
||||
memcpy(&u, &f, sizeof(uint64_t));
|
||||
}
|
||||
uint16_t neg = ((uint16_t)(u >> 63)) << 15;
|
||||
u &= 0x7FFFFFFFFFFFFFFF;
|
||||
uint64_t exp = u >> 52;
|
||||
uint64_t man = u & 0x000FFFFFFFFFFFFF;
|
||||
|
||||
if (exp == 0x7FF) {
|
||||
if (man == 0) { // Infinity.
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg | 0x7C00;
|
||||
ret.lossy = false;
|
||||
return ret;
|
||||
}
|
||||
// NaN. Shift the 52 mantissa bits to 10 mantissa bits, keeping the most
|
||||
// significant mantissa bit (quiet vs signaling NaNs). Also set the low 9
|
||||
// bits of ret.value so that the 10-bit mantissa is non-zero.
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg | 0x7DFF | ((uint16_t)(man >> 42));
|
||||
ret.lossy = false;
|
||||
return ret;
|
||||
|
||||
} else if (exp > 0x40E) { // Truncate to the largest finite f16.
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg | 0x7BFF;
|
||||
ret.lossy = true;
|
||||
return ret;
|
||||
|
||||
} else if (exp <= 0x3E6) { // Truncate to zero.
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg;
|
||||
ret.lossy = (u != 0);
|
||||
return ret;
|
||||
|
||||
} else if (exp <= 0x3F0) { // Normal f64, subnormal f16.
|
||||
// Convert from a 53-bit mantissa (after realizing the implicit bit) to a
|
||||
// 10-bit mantissa and then adjust for the exponent.
|
||||
man |= 0x0010000000000000;
|
||||
uint32_t shift = 1051 - exp; // 1051 = 0x3F0 + 53 - 10.
|
||||
uint64_t shifted_man = man >> shift;
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg | ((uint16_t)shifted_man);
|
||||
ret.lossy = (shifted_man << shift) != man;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Normal f64, normal f16.
|
||||
|
||||
// Re-bias from 1023 to 15 and shift above f16's 10 mantissa bits.
|
||||
exp = (exp - 1008) << 10; // 1008 = 1023 - 15 = 0x3FF - 0xF.
|
||||
|
||||
// Convert from a 52-bit mantissa (excluding the implicit bit) to a 10-bit
|
||||
// mantissa (again excluding the implicit bit). We lose some information if
|
||||
// any of the bottom 42 bits are non-zero.
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg | ((uint16_t)exp) | ((uint16_t)(man >> 42));
|
||||
ret.lossy = (man << 22) != 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u32 //
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u32(double f) {
|
||||
uint64_t u = 0;
|
||||
if (sizeof(uint64_t) == sizeof(double)) {
|
||||
memcpy(&u, &f, sizeof(uint64_t));
|
||||
}
|
||||
uint32_t neg = ((uint32_t)(u >> 63)) << 31;
|
||||
u &= 0x7FFFFFFFFFFFFFFF;
|
||||
uint64_t exp = u >> 52;
|
||||
uint64_t man = u & 0x000FFFFFFFFFFFFF;
|
||||
|
||||
if (exp == 0x7FF) {
|
||||
if (man == 0) { // Infinity.
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg | 0x7F800000;
|
||||
ret.lossy = false;
|
||||
return ret;
|
||||
}
|
||||
// NaN. Shift the 52 mantissa bits to 23 mantissa bits, keeping the most
|
||||
// significant mantissa bit (quiet vs signaling NaNs). Also set the low 22
|
||||
// bits of ret.value so that the 23-bit mantissa is non-zero.
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg | 0x7FBFFFFF | ((uint32_t)(man >> 29));
|
||||
ret.lossy = false;
|
||||
return ret;
|
||||
|
||||
} else if (exp > 0x47E) { // Truncate to the largest finite f32.
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg | 0x7F7FFFFF;
|
||||
ret.lossy = true;
|
||||
return ret;
|
||||
|
||||
} else if (exp <= 0x369) { // Truncate to zero.
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg;
|
||||
ret.lossy = (u != 0);
|
||||
return ret;
|
||||
|
||||
} else if (exp <= 0x380) { // Normal f64, subnormal f32.
|
||||
// Convert from a 53-bit mantissa (after realizing the implicit bit) to a
|
||||
// 23-bit mantissa and then adjust for the exponent.
|
||||
man |= 0x0010000000000000;
|
||||
uint32_t shift = 926 - exp; // 926 = 0x380 + 53 - 23.
|
||||
uint64_t shifted_man = man >> shift;
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg | ((uint32_t)shifted_man);
|
||||
ret.lossy = (shifted_man << shift) != man;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Normal f64, normal f32.
|
||||
|
||||
// Re-bias from 1023 to 127 and shift above f32's 23 mantissa bits.
|
||||
exp = (exp - 896) << 23; // 896 = 1023 - 127 = 0x3FF - 0x7F.
|
||||
|
||||
// Convert from a 52-bit mantissa (excluding the implicit bit) to a 23-bit
|
||||
// mantissa (again excluding the implicit bit). We lose some information if
|
||||
// any of the bottom 29 bits are non-zero.
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg | ((uint32_t)exp) | ((uint32_t)(man >> 29));
|
||||
ret.lossy = (man << 35) != 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// --------
|
||||
|
||||
#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE 2047
|
||||
#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION 800
|
||||
|
||||
|
||||
@@ -145,10 +145,32 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options);
|
||||
// - +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.
|
||||
// Converting from f64 to shorter formats (f16 or f32, represented in C as
|
||||
// uint16_t and uint32_t) may be lossy. Converting finite numbers truncate,
|
||||
// producing equal or smaller (closer-to-zero) finite numbers. Converting
|
||||
// infinities or NaNs produces infinities or NaNs and always report no loss,
|
||||
// even though there a multiple NaN representations so that round-tripping a
|
||||
// f64 NaN may produce a different 64 bits. Nonetheless, a NaN's "quiet vs
|
||||
// signaling" bit is preserved.
|
||||
//
|
||||
// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format
|
||||
|
||||
typedef struct {
|
||||
uint16_t value;
|
||||
bool lossy;
|
||||
} wuffs_base__lossy_value_u16;
|
||||
|
||||
typedef struct {
|
||||
uint32_t value;
|
||||
bool lossy;
|
||||
} wuffs_base__lossy_value_u32;
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u16 //
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u16(double f);
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u32 //
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u32(double f);
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u64(double f) {
|
||||
uint64_t u = 0;
|
||||
|
||||
+20
-14
@@ -29,17 +29,23 @@ const BaseAllImplC = "" +
|
||||
""
|
||||
|
||||
const BaseFloatConvSubmoduleCodeC = "" +
|
||||
"// ---------------- IEEE 754 Floating Point\n\n#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE 2047\n#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION 800\n\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL is the largest N\n// such that ((10 << N) < (1 << 64)).\n#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL 60\n\n// wuffs_base__private_implementation__high_prec_dec (abbreviated as HPD) is a\n// fixed precision floating point decimal number, augmented with ±infinity\n// values, but it cannot represent NaN (Not a Number).\n//\n// \"High precision\" means that the mantissa holds 800 decimal digits. 800 is\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION.\n//\n// An HPD isn't for general purpose arithmetic, only for conversions to and\n// from IEEE 754 double-precision floating point, where the largest and\n// smallest positive, finite values are approximately 1.8e+308 and 4.9e-324.\n// HPD exponents above +2047 mean infinity, below -2047 mean zero. Th" +
|
||||
"e ±2047\n// bounds are further away from zero than ±(324 + 800), where 800 and 2047 is\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION and\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE.\n//\n// digits[.. num_digits] are the number's digits in big-endian order. The\n// uint8_t values are in the range [0 ..= 9], not ['0' ..= '9'], where e.g. '7'\n// is the ASCII value 0x37.\n//\n// decimal_point is the index (within digits) of the decimal point. It may be\n// negative or be larger than num_digits, in which case the explicit digits are\n// padded with implicit zeroes.\n//\n// For example, if num_digits is 3 and digits is \"\\x07\\x08\\x09\":\n// - A decimal_point of -2 means \".00789\"\n// - A decimal_point of -1 means \".0789\"\n// - A decimal_point of +0 means \".789\"\n// - A decimal_point of +1 means \"7.89\"\n// - A decimal_point of +2 means \"78.9\"\n// - A decimal_point of +3 means \"789.\"\n// - A decimal_point of +4 means \"7890.\"\n// - A decimal_point of +5 means \"78900.\"\n//\n// As above, a" +
|
||||
" decimal_point higher than +2047 means that the overall value is\n// infinity, lower than -2047 means zero.\n//\n// negative is a sign bit. An HPD can distinguish positive and negative zero.\n//\n// truncated is whether there are more than\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION digits, and at\n// least one of those extra digits are non-zero. The existence of long-tail\n// digits can affect rounding.\n//\n// The \"all fields are zero\" value is valid, and represents the number +0.\ntypedef struct {\n uint32_t num_digits;\n int32_t decimal_point;\n bool negative;\n bool truncated;\n uint8_t digits[WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION];\n} wuffs_base__private_implementation__high_prec_dec;\n\n// wuffs_base__private_implementation__high_prec_dec__trim trims trailing\n// zeroes from the h->digits[.. h->num_digits] slice. They have no benefit,\n// since we explicitly track h->decimal_point.\n//\n// Preconditions:\n// - h is non-NULL.\nstatic inline void //\nwuffs_base__private_implementation_" +
|
||||
"_high_prec_dec__trim(\n wuffs_base__private_implementation__high_prec_dec* h) {\n while ((h->num_digits > 0) && (h->digits[h->num_digits - 1] == 0)) {\n h->num_digits--;\n }\n}\n\n// wuffs_base__private_implementation__high_prec_dec__assign sets h to\n// represent the number x.\n//\n// Preconditions:\n// - h is non-NULL.\nstatic void //\nwuffs_base__private_implementation__high_prec_dec__assign(\n wuffs_base__private_implementation__high_prec_dec* h,\n uint64_t x,\n bool negative) {\n uint32_t n = 0;\n\n // Set h->digits.\n if (x > 0) {\n // Calculate the digits, working right-to-left. After we determine n (how\n // many digits there are), copy from buf to h->digits.\n //\n // UINT64_MAX, 18446744073709551615, is 20 digits long. It can be faster to\n // copy a constant number of bytes than a variable number (20 instead of\n // n). Make buf large enough (and start writing to it from the middle) so\n // that can we always copy 20 bytes: the slice buf[(20-n) .. (40-n)].\n uint8_t buf[40] = {0};" +
|
||||
"\n uint8_t* ptr = &buf[20];\n do {\n uint64_t remaining = x / 10;\n x -= remaining * 10;\n ptr--;\n *ptr = (uint8_t)x;\n n++;\n x = remaining;\n } while (x > 0);\n memcpy(h->digits, ptr, 20);\n }\n\n // Set h's other fields.\n h->num_digits = n;\n h->decimal_point = (int32_t)n;\n h->negative = negative;\n h->truncated = false;\n wuffs_base__private_implementation__high_prec_dec__trim(h);\n}\n\nstatic wuffs_base__status //\nwuffs_base__private_implementation__high_prec_dec__parse(\n wuffs_base__private_implementation__high_prec_dec* h,\n wuffs_base__slice_u8 s,\n uint32_t options) {\n if (!h) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n h->num_digits = 0;\n h->decimal_point = 0;\n h->negative = false;\n h->truncated = false;\n\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_ar" +
|
||||
"gument);\n } else if (*p != '_') {\n break;\n }\n }\n }\n\n // Parse sign.\n do {\n if (*p == '+') {\n p++;\n } else if (*p == '-') {\n h->negative = true;\n p++;\n } else {\n break;\n }\n if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != '_') {\n break;\n }\n }\n }\n } while (0);\n\n // Parse digits, up to (and including) a '.', 'E' or 'e'. Examples for each\n // limb in this if-else chain:\n // - \"0.789\"\n // - \"1002.789\"\n // - \".789\"\n // - Other (invalid input).\n uint32_t nd = 0;\n int32_t dp = 0;\n bool no_digits_before_separator = false;\n if (('0' == *p) &&\n !(options &\n WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES)) {\n p++;\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if (*p ==\n ((options &\n " +
|
||||
" WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n goto after_sep;\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if ((*p != '_') ||\n !(options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES)) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\n } else if (('0' <= *p) && (*p <= '9')) {\n if (*p == '0') {\n for (; (p < q) && (*p == '0'); p++) {\n }\n } else {\n h->digits[nd++] = (uint8_t)(*p - '0');\n dp = (int32_t)nd;\n p++;\n }\n\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if (('0' <= *p) && (*p <= '9')) {\n if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n dp = (int32_t)nd;\n } else if ('0' != *p) {\n // Long-tail non-zeroes set the truncated bit.\n " +
|
||||
" h->truncated = true;\n }\n } else if (*p ==\n ((options &\n WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n goto after_sep;\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if ((*p != '_') ||\n !(options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES)) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\n } else if (*p == ((options &\n WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n no_digits_before_separator = true;\n\n } else {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n\nafter_sep:\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if ('0' == *p) {\n if (nd == 0) {\n // Track leading zeroes implicitly.\n " +
|
||||
" dp--;\n } else if (nd <\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n }\n } else if (('0' < *p) && (*p <= '9')) {\n if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n } else {\n // Long-tail non-zeroes set the truncated bit.\n h->truncated = true;\n }\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if ((*p != '_') ||\n !(options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES)) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\nafter_exp:\n do {\n if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != '_') {\n break;\n }\n }\n }\n\n int32_t exp_sign = +1;\n if (*p ==" +
|
||||
" '+') {\n p++;\n } else if (*p == '-') {\n exp_sign = -1;\n p++;\n }\n\n int32_t exp = 0;\n const int32_t exp_large =\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE +\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION;\n bool saw_exp_digits = false;\n for (; p < q; p++) {\n if ((*p == '_') &&\n (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES)) {\n // No-op.\n } else if (('0' <= *p) && (*p <= '9')) {\n saw_exp_digits = true;\n if (exp < exp_large) {\n exp = (10 * exp) + ((int32_t)(*p - '0'));\n }\n } else {\n break;\n }\n }\n if (!saw_exp_digits) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n dp += exp_sign * exp;\n } while (0);\n\nafter_all:\n if (p != q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n h->num_digits = nd;\n if (nd == 0) {\n if (no_digits_before_separator) {\n return wuffs_base__make_status" +
|
||||
"(wuffs_base__error__bad_argument);\n }\n h->decimal_point = 0;\n } else if (dp <\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n h->decimal_point =\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE - 1;\n } else if (dp >\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n h->decimal_point =\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE + 1;\n } else {\n h->decimal_point = dp;\n }\n wuffs_base__private_implementation__high_prec_dec__trim(h);\n return wuffs_base__make_status(NULL);\n}\n\n" +
|
||||
"// ---------------- IEEE 754 Floating Point\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u16 //\nwuffs_base__ieee_754_bit_representation__from_f64_to_u16(double f) {\n uint64_t u = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&u, &f, sizeof(uint64_t));\n }\n uint16_t neg = ((uint16_t)(u >> 63)) << 15;\n u &= 0x7FFFFFFFFFFFFFFF;\n uint64_t exp = u >> 52;\n uint64_t man = u & 0x000FFFFFFFFFFFFF;\n\n if (exp == 0x7FF) {\n if (man == 0) { // Infinity.\n wuffs_base__lossy_value_u16 ret;\n ret.value = neg | 0x7C00;\n ret.lossy = false;\n return ret;\n }\n // NaN. Shift the 52 mantissa bits to 10 mantissa bits, keeping the most\n // significant mantissa bit (quiet vs signaling NaNs). Also set the low 9\n // bits of ret.value so that the 10-bit mantissa is non-zero.\n wuffs_base__lossy_value_u16 ret;\n ret.value = neg | 0x7DFF | ((uint16_t)(man >> 42));\n ret.lossy = false;\n return ret;\n\n } else if (exp > 0x40E) { // Truncate to the largest finite f16.\n wuffs_" +
|
||||
"base__lossy_value_u16 ret;\n ret.value = neg | 0x7BFF;\n ret.lossy = true;\n return ret;\n\n } else if (exp <= 0x3E6) { // Truncate to zero.\n wuffs_base__lossy_value_u16 ret;\n ret.value = neg;\n ret.lossy = (u != 0);\n return ret;\n\n } else if (exp <= 0x3F0) { // Normal f64, subnormal f16.\n // Convert from a 53-bit mantissa (after realizing the implicit bit) to a\n // 10-bit mantissa and then adjust for the exponent.\n man |= 0x0010000000000000;\n uint32_t shift = 1051 - exp; // 1051 = 0x3F0 + 53 - 10.\n uint64_t shifted_man = man >> shift;\n wuffs_base__lossy_value_u16 ret;\n ret.value = neg | ((uint16_t)shifted_man);\n ret.lossy = (shifted_man << shift) != man;\n return ret;\n }\n\n // Normal f64, normal f16.\n\n // Re-bias from 1023 to 15 and shift above f16's 10 mantissa bits.\n exp = (exp - 1008) << 10; // 1008 = 1023 - 15 = 0x3FF - 0xF.\n\n // Convert from a 52-bit mantissa (excluding the implicit bit) to a 10-bit\n // mantissa (again excluding the implicit bit). We lo" +
|
||||
"se some information if\n // any of the bottom 42 bits are non-zero.\n wuffs_base__lossy_value_u16 ret;\n ret.value = neg | ((uint16_t)exp) | ((uint16_t)(man >> 42));\n ret.lossy = (man << 22) != 0;\n return ret;\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u32 //\nwuffs_base__ieee_754_bit_representation__from_f64_to_u32(double f) {\n uint64_t u = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&u, &f, sizeof(uint64_t));\n }\n uint32_t neg = ((uint32_t)(u >> 63)) << 31;\n u &= 0x7FFFFFFFFFFFFFFF;\n uint64_t exp = u >> 52;\n uint64_t man = u & 0x000FFFFFFFFFFFFF;\n\n if (exp == 0x7FF) {\n if (man == 0) { // Infinity.\n wuffs_base__lossy_value_u32 ret;\n ret.value = neg | 0x7F800000;\n ret.lossy = false;\n return ret;\n }\n // NaN. Shift the 52 mantissa bits to 23 mantissa bits, keeping the most\n // significant mantissa bit (quiet vs signaling NaNs). Also set the low 22\n // bits of ret.value so that the 23-bit mantissa is non-zero.\n wuffs_base__lossy_value_u32 ret;" +
|
||||
"\n ret.value = neg | 0x7FBFFFFF | ((uint32_t)(man >> 29));\n ret.lossy = false;\n return ret;\n\n } else if (exp > 0x47E) { // Truncate to the largest finite f32.\n wuffs_base__lossy_value_u32 ret;\n ret.value = neg | 0x7F7FFFFF;\n ret.lossy = true;\n return ret;\n\n } else if (exp <= 0x369) { // Truncate to zero.\n wuffs_base__lossy_value_u32 ret;\n ret.value = neg;\n ret.lossy = (u != 0);\n return ret;\n\n } else if (exp <= 0x380) { // Normal f64, subnormal f32.\n // Convert from a 53-bit mantissa (after realizing the implicit bit) to a\n // 23-bit mantissa and then adjust for the exponent.\n man |= 0x0010000000000000;\n uint32_t shift = 926 - exp; // 926 = 0x380 + 53 - 23.\n uint64_t shifted_man = man >> shift;\n wuffs_base__lossy_value_u32 ret;\n ret.value = neg | ((uint32_t)shifted_man);\n ret.lossy = (shifted_man << shift) != man;\n return ret;\n }\n\n // Normal f64, normal f32.\n\n // Re-bias from 1023 to 127 and shift above f32's 23 mantissa bits.\n exp = (exp - " +
|
||||
"896) << 23; // 896 = 1023 - 127 = 0x3FF - 0x7F.\n\n // Convert from a 52-bit mantissa (excluding the implicit bit) to a 23-bit\n // mantissa (again excluding the implicit bit). We lose some information if\n // any of the bottom 29 bits are non-zero.\n wuffs_base__lossy_value_u32 ret;\n ret.value = neg | ((uint32_t)exp) | ((uint32_t)(man >> 29));\n ret.lossy = (man << 35) != 0;\n return ret;\n}\n\n" +
|
||||
"" +
|
||||
"// --------\n\n#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE 2047\n#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION 800\n\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL is the largest N\n// such that ((10 << N) < (1 << 64)).\n#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL 60\n\n// wuffs_base__private_implementation__high_prec_dec (abbreviated as HPD) is a\n// fixed precision floating point decimal number, augmented with ±infinity\n// values, but it cannot represent NaN (Not a Number).\n//\n// \"High precision\" means that the mantissa holds 800 decimal digits. 800 is\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION.\n//\n// An HPD isn't for general purpose arithmetic, only for conversions to and\n// from IEEE 754 double-precision floating point, where the largest and\n// smallest positive, finite values are approximately 1.8e+308 and 4.9e-324.\n// HPD exponents above +2047 mean infinity, below -2047 mean zero. The ±2047\n// bounds are further a" +
|
||||
"way from zero than ±(324 + 800), where 800 and 2047 is\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION and\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE.\n//\n// digits[.. num_digits] are the number's digits in big-endian order. The\n// uint8_t values are in the range [0 ..= 9], not ['0' ..= '9'], where e.g. '7'\n// is the ASCII value 0x37.\n//\n// decimal_point is the index (within digits) of the decimal point. It may be\n// negative or be larger than num_digits, in which case the explicit digits are\n// padded with implicit zeroes.\n//\n// For example, if num_digits is 3 and digits is \"\\x07\\x08\\x09\":\n// - A decimal_point of -2 means \".00789\"\n// - A decimal_point of -1 means \".0789\"\n// - A decimal_point of +0 means \".789\"\n// - A decimal_point of +1 means \"7.89\"\n// - A decimal_point of +2 means \"78.9\"\n// - A decimal_point of +3 means \"789.\"\n// - A decimal_point of +4 means \"7890.\"\n// - A decimal_point of +5 means \"78900.\"\n//\n// As above, a decimal_point higher than +2047" +
|
||||
" means that the overall value is\n// infinity, lower than -2047 means zero.\n//\n// negative is a sign bit. An HPD can distinguish positive and negative zero.\n//\n// truncated is whether there are more than\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION digits, and at\n// least one of those extra digits are non-zero. The existence of long-tail\n// digits can affect rounding.\n//\n// The \"all fields are zero\" value is valid, and represents the number +0.\ntypedef struct {\n uint32_t num_digits;\n int32_t decimal_point;\n bool negative;\n bool truncated;\n uint8_t digits[WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION];\n} wuffs_base__private_implementation__high_prec_dec;\n\n// wuffs_base__private_implementation__high_prec_dec__trim trims trailing\n// zeroes from the h->digits[.. h->num_digits] slice. They have no benefit,\n// since we explicitly track h->decimal_point.\n//\n// Preconditions:\n// - h is non-NULL.\nstatic inline void //\nwuffs_base__private_implementation__high_prec_dec__trim(\n wuffs_" +
|
||||
"base__private_implementation__high_prec_dec* h) {\n while ((h->num_digits > 0) && (h->digits[h->num_digits - 1] == 0)) {\n h->num_digits--;\n }\n}\n\n// wuffs_base__private_implementation__high_prec_dec__assign sets h to\n// represent the number x.\n//\n// Preconditions:\n// - h is non-NULL.\nstatic void //\nwuffs_base__private_implementation__high_prec_dec__assign(\n wuffs_base__private_implementation__high_prec_dec* h,\n uint64_t x,\n bool negative) {\n uint32_t n = 0;\n\n // Set h->digits.\n if (x > 0) {\n // Calculate the digits, working right-to-left. After we determine n (how\n // many digits there are), copy from buf to h->digits.\n //\n // UINT64_MAX, 18446744073709551615, is 20 digits long. It can be faster to\n // copy a constant number of bytes than a variable number (20 instead of\n // n). Make buf large enough (and start writing to it from the middle) so\n // that can we always copy 20 bytes: the slice buf[(20-n) .. (40-n)].\n uint8_t buf[40] = {0};\n uint8_t* ptr = &buf[20];\n " +
|
||||
" do {\n uint64_t remaining = x / 10;\n x -= remaining * 10;\n ptr--;\n *ptr = (uint8_t)x;\n n++;\n x = remaining;\n } while (x > 0);\n memcpy(h->digits, ptr, 20);\n }\n\n // Set h's other fields.\n h->num_digits = n;\n h->decimal_point = (int32_t)n;\n h->negative = negative;\n h->truncated = false;\n wuffs_base__private_implementation__high_prec_dec__trim(h);\n}\n\nstatic wuffs_base__status //\nwuffs_base__private_implementation__high_prec_dec__parse(\n wuffs_base__private_implementation__high_prec_dec* h,\n wuffs_base__slice_u8 s,\n uint32_t options) {\n if (!h) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n h->num_digits = 0;\n h->decimal_point = 0;\n h->negative = false;\n h->truncated = false;\n\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != " +
|
||||
"'_') {\n break;\n }\n }\n }\n\n // Parse sign.\n do {\n if (*p == '+') {\n p++;\n } else if (*p == '-') {\n h->negative = true;\n p++;\n } else {\n break;\n }\n if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != '_') {\n break;\n }\n }\n }\n } while (0);\n\n // Parse digits, up to (and including) a '.', 'E' or 'e'. Examples for each\n // limb in this if-else chain:\n // - \"0.789\"\n // - \"1002.789\"\n // - \".789\"\n // - Other (invalid input).\n uint32_t nd = 0;\n int32_t dp = 0;\n bool no_digits_before_separator = false;\n if (('0' == *p) &&\n !(options &\n WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES)) {\n p++;\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if (*p ==\n ((options &\n WUFFS_BASE__PARSE_NUMBER_FXX" +
|
||||
"__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n goto after_sep;\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if ((*p != '_') ||\n !(options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES)) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\n } else if (('0' <= *p) && (*p <= '9')) {\n if (*p == '0') {\n for (; (p < q) && (*p == '0'); p++) {\n }\n } else {\n h->digits[nd++] = (uint8_t)(*p - '0');\n dp = (int32_t)nd;\n p++;\n }\n\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if (('0' <= *p) && (*p <= '9')) {\n if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n dp = (int32_t)nd;\n } else if ('0' != *p) {\n // Long-tail non-zeroes set the truncated bit.\n h->truncated = true;\n }" +
|
||||
"\n } else if (*p ==\n ((options &\n WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n goto after_sep;\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if ((*p != '_') ||\n !(options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES)) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\n } else if (*p == ((options &\n WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n no_digits_before_separator = true;\n\n } else {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n\nafter_sep:\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if ('0' == *p) {\n if (nd == 0) {\n // Track leading zeroes implicitly.\n dp--;\n } else if (nd <\n " +
|
||||
" WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n }\n } else if (('0' < *p) && (*p <= '9')) {\n if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n } else {\n // Long-tail non-zeroes set the truncated bit.\n h->truncated = true;\n }\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if ((*p != '_') ||\n !(options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES)) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\nafter_exp:\n do {\n if (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES) {\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != '_') {\n break;\n }\n }\n }\n\n int32_t exp_sign = +1;\n if (*p == '+') {\n p++;\n } else if" +
|
||||
" (*p == '-') {\n exp_sign = -1;\n p++;\n }\n\n int32_t exp = 0;\n const int32_t exp_large =\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE +\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION;\n bool saw_exp_digits = false;\n for (; p < q; p++) {\n if ((*p == '_') &&\n (options & WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES)) {\n // No-op.\n } else if (('0' <= *p) && (*p <= '9')) {\n saw_exp_digits = true;\n if (exp < exp_large) {\n exp = (10 * exp) + ((int32_t)(*p - '0'));\n }\n } else {\n break;\n }\n }\n if (!saw_exp_digits) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n dp += exp_sign * exp;\n } while (0);\n\nafter_all:\n if (p != q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n h->num_digits = nd;\n if (nd == 0) {\n if (no_digits_before_separator) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument" +
|
||||
");\n }\n h->decimal_point = 0;\n } else if (dp <\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n h->decimal_point =\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE - 1;\n } else if (dp >\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n h->decimal_point =\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE + 1;\n } else {\n h->decimal_point = dp;\n }\n wuffs_base__private_implementation__high_prec_dec__trim(h);\n return wuffs_base__make_status(NULL);\n}\n\n" +
|
||||
"" +
|
||||
"// --------\n\n// wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits\n// returns the number of additional decimal digits when left-shifting by shift.\n//\n// See below for preconditions.\nstatic uint32_t //\nwuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits(\n wuffs_base__private_implementation__high_prec_dec* h,\n uint32_t shift) {\n // Masking with 0x3F should be unnecessary (assuming the preconditions) but\n // it's cheap and ensures that we don't overflow the\n // wuffs_base__private_implementation__hpd_left_shift array.\n shift &= 63;\n\n uint32_t x_a = wuffs_base__private_implementation__hpd_left_shift[shift];\n uint32_t x_b = wuffs_base__private_implementation__hpd_left_shift[shift + 1];\n uint32_t num_new_digits = x_a >> 11;\n uint32_t pow5_a = 0x7FF & x_a;\n uint32_t pow5_b = 0x7FF & x_b;\n\n const uint8_t* pow5 =\n &wuffs_base__private_implementation__powers_of_5[pow5_a];\n uint32_t i = 0;\n uint32_t n = pow5_b - pow5_a;\n for (; i < n; i++) {\n if (i >" +
|
||||
"= h->num_digits) {\n return num_new_digits - 1;\n } else if (h->digits[i] == pow5[i]) {\n continue;\n } else if (h->digits[i] < pow5[i]) {\n return num_new_digits - 1;\n } else {\n return num_new_digits;\n }\n }\n return num_new_digits;\n}\n\n" +
|
||||
@@ -519,9 +525,9 @@ 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 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" +
|
||||
"16_to_f64(uint16_t u) {\n uint64_t v = ((uint64_t)(u & 0x8000)) << 48;\n\n do {\n uint64_t exp = (u >> 10) & 0x1F;\n uint64_t man = u & 0x3FF;\n if (exp == 0x1F) { // Infinity or NaN.\n exp = 2047;\n } else if (exp != 0) { // Normal.\n exp += 1008; // 1008 = 1023 - 15, the difference in biases.\n } else if (man != 0) { // Subnormal but non-zero.\n uint32_t clz = wuffs_base__count_leading_zeroes_u64(man);\n exp = 1062 - clz; // 1062 = 1008 + 64 - 10.\n man = 0x3FF & (man << (clz - 53));\n } else { // Zero.\n break;\n }\n v |= (exp << 52) | (man << 42);\n } while (0);\n\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &v, sizeof(uint64_t));\n }\n return f;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__from_u32_to_f64(uint32_t u) {\n float f = 0;\n if (sizeof(uint32_t) == sizeof(float)) {\n memcpy(&f, &u, sizeof(uint32_t));\n }\n return (double)f;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representat" +
|
||||
"ion__from_u64_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 (f16 or f32, represented in C as\n// uint16_t and uint32_t) may be lossy. Converting finite numbers truncate,\n// producing equal or smaller (closer-to-zero) finite numbers. Converting\n// infinities or NaNs produces infinities or NaNs and always report no loss,\n// even though there a multiple NaN representations so that round-tripping a\n// f64 NaN may produce a different 64 bits. " +
|
||||
"Nonetheless, a NaN's \"quiet vs\n// signaling\" bit is preserved.\n//\n// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format\n\ntypedef struct {\n uint16_t value;\n bool lossy;\n} wuffs_base__lossy_value_u16;\n\ntypedef struct {\n uint32_t value;\n bool lossy;\n} wuffs_base__lossy_value_u32;\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u16 //\nwuffs_base__ieee_754_bit_representation__from_f64_to_u16(double f);\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u32 //\nwuffs_base__ieee_754_bit_representation__from_f64_to_u32(double f);\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_u16_to_f64(uint16_t u) {\n uint64_t v = ((uint64_t)(u & 0x8000)) << 48;\n\n do {\n uint64_t exp = (u >> 10) & 0x1F;\n uint64_t man = u & 0x3FF;\n if (exp == 0x1F) { // Infinit" +
|
||||
"y or NaN.\n exp = 2047;\n } else if (exp != 0) { // Normal.\n exp += 1008; // 1008 = 1023 - 15, the difference in biases.\n } else if (man != 0) { // Subnormal but non-zero.\n uint32_t clz = wuffs_base__count_leading_zeroes_u64(man);\n exp = 1062 - clz; // 1062 = 1008 + 64 - 10.\n man = 0x3FF & (man << (clz - 53));\n } else { // Zero.\n break;\n }\n v |= (exp << 52) | (man << 42);\n } while (0);\n\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &v, sizeof(uint64_t));\n }\n return f;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__from_u32_to_f64(uint32_t u) {\n float f = 0;\n if (sizeof(uint32_t) == sizeof(float)) {\n memcpy(&f, &u, sizeof(uint32_t));\n }\n return (double)f;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__from_u64_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" +
|
||||
|
||||
@@ -4064,10 +4064,32 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options);
|
||||
// - +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.
|
||||
// Converting from f64 to shorter formats (f16 or f32, represented in C as
|
||||
// uint16_t and uint32_t) may be lossy. Converting finite numbers truncate,
|
||||
// producing equal or smaller (closer-to-zero) finite numbers. Converting
|
||||
// infinities or NaNs produces infinities or NaNs and always report no loss,
|
||||
// even though there a multiple NaN representations so that round-tripping a
|
||||
// f64 NaN may produce a different 64 bits. Nonetheless, a NaN's "quiet vs
|
||||
// signaling" bit is preserved.
|
||||
//
|
||||
// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format
|
||||
|
||||
typedef struct {
|
||||
uint16_t value;
|
||||
bool lossy;
|
||||
} wuffs_base__lossy_value_u16;
|
||||
|
||||
typedef struct {
|
||||
uint32_t value;
|
||||
bool lossy;
|
||||
} wuffs_base__lossy_value_u32;
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u16 //
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u16(double f);
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u32 //
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u32(double f);
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u64(double f) {
|
||||
uint64_t u = 0;
|
||||
@@ -9938,6 +9960,136 @@ static const double wuffs_base__private_implementation__f64_powers_of_10[23] = {
|
||||
|
||||
// ---------------- IEEE 754 Floating Point
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u16 //
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u16(double f) {
|
||||
uint64_t u = 0;
|
||||
if (sizeof(uint64_t) == sizeof(double)) {
|
||||
memcpy(&u, &f, sizeof(uint64_t));
|
||||
}
|
||||
uint16_t neg = ((uint16_t)(u >> 63)) << 15;
|
||||
u &= 0x7FFFFFFFFFFFFFFF;
|
||||
uint64_t exp = u >> 52;
|
||||
uint64_t man = u & 0x000FFFFFFFFFFFFF;
|
||||
|
||||
if (exp == 0x7FF) {
|
||||
if (man == 0) { // Infinity.
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg | 0x7C00;
|
||||
ret.lossy = false;
|
||||
return ret;
|
||||
}
|
||||
// NaN. Shift the 52 mantissa bits to 10 mantissa bits, keeping the most
|
||||
// significant mantissa bit (quiet vs signaling NaNs). Also set the low 9
|
||||
// bits of ret.value so that the 10-bit mantissa is non-zero.
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg | 0x7DFF | ((uint16_t)(man >> 42));
|
||||
ret.lossy = false;
|
||||
return ret;
|
||||
|
||||
} else if (exp > 0x40E) { // Truncate to the largest finite f16.
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg | 0x7BFF;
|
||||
ret.lossy = true;
|
||||
return ret;
|
||||
|
||||
} else if (exp <= 0x3E6) { // Truncate to zero.
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg;
|
||||
ret.lossy = (u != 0);
|
||||
return ret;
|
||||
|
||||
} else if (exp <= 0x3F0) { // Normal f64, subnormal f16.
|
||||
// Convert from a 53-bit mantissa (after realizing the implicit bit) to a
|
||||
// 10-bit mantissa and then adjust for the exponent.
|
||||
man |= 0x0010000000000000;
|
||||
uint32_t shift = 1051 - exp; // 1051 = 0x3F0 + 53 - 10.
|
||||
uint64_t shifted_man = man >> shift;
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg | ((uint16_t)shifted_man);
|
||||
ret.lossy = (shifted_man << shift) != man;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Normal f64, normal f16.
|
||||
|
||||
// Re-bias from 1023 to 15 and shift above f16's 10 mantissa bits.
|
||||
exp = (exp - 1008) << 10; // 1008 = 1023 - 15 = 0x3FF - 0xF.
|
||||
|
||||
// Convert from a 52-bit mantissa (excluding the implicit bit) to a 10-bit
|
||||
// mantissa (again excluding the implicit bit). We lose some information if
|
||||
// any of the bottom 42 bits are non-zero.
|
||||
wuffs_base__lossy_value_u16 ret;
|
||||
ret.value = neg | ((uint16_t)exp) | ((uint16_t)(man >> 42));
|
||||
ret.lossy = (man << 22) != 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__lossy_value_u32 //
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u32(double f) {
|
||||
uint64_t u = 0;
|
||||
if (sizeof(uint64_t) == sizeof(double)) {
|
||||
memcpy(&u, &f, sizeof(uint64_t));
|
||||
}
|
||||
uint32_t neg = ((uint32_t)(u >> 63)) << 31;
|
||||
u &= 0x7FFFFFFFFFFFFFFF;
|
||||
uint64_t exp = u >> 52;
|
||||
uint64_t man = u & 0x000FFFFFFFFFFFFF;
|
||||
|
||||
if (exp == 0x7FF) {
|
||||
if (man == 0) { // Infinity.
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg | 0x7F800000;
|
||||
ret.lossy = false;
|
||||
return ret;
|
||||
}
|
||||
// NaN. Shift the 52 mantissa bits to 23 mantissa bits, keeping the most
|
||||
// significant mantissa bit (quiet vs signaling NaNs). Also set the low 22
|
||||
// bits of ret.value so that the 23-bit mantissa is non-zero.
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg | 0x7FBFFFFF | ((uint32_t)(man >> 29));
|
||||
ret.lossy = false;
|
||||
return ret;
|
||||
|
||||
} else if (exp > 0x47E) { // Truncate to the largest finite f32.
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg | 0x7F7FFFFF;
|
||||
ret.lossy = true;
|
||||
return ret;
|
||||
|
||||
} else if (exp <= 0x369) { // Truncate to zero.
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg;
|
||||
ret.lossy = (u != 0);
|
||||
return ret;
|
||||
|
||||
} else if (exp <= 0x380) { // Normal f64, subnormal f32.
|
||||
// Convert from a 53-bit mantissa (after realizing the implicit bit) to a
|
||||
// 23-bit mantissa and then adjust for the exponent.
|
||||
man |= 0x0010000000000000;
|
||||
uint32_t shift = 926 - exp; // 926 = 0x380 + 53 - 23.
|
||||
uint64_t shifted_man = man >> shift;
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg | ((uint32_t)shifted_man);
|
||||
ret.lossy = (shifted_man << shift) != man;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Normal f64, normal f32.
|
||||
|
||||
// Re-bias from 1023 to 127 and shift above f32's 23 mantissa bits.
|
||||
exp = (exp - 896) << 23; // 896 = 1023 - 127 = 0x3FF - 0x7F.
|
||||
|
||||
// Convert from a 52-bit mantissa (excluding the implicit bit) to a 23-bit
|
||||
// mantissa (again excluding the implicit bit). We lose some information if
|
||||
// any of the bottom 29 bits are non-zero.
|
||||
wuffs_base__lossy_value_u32 ret;
|
||||
ret.value = neg | ((uint32_t)exp) | ((uint32_t)(man >> 29));
|
||||
ret.lossy = (man << 35) != 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// --------
|
||||
|
||||
#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE 2047
|
||||
#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION 800
|
||||
|
||||
|
||||
+44
-2
@@ -488,7 +488,7 @@ test_wuffs_strconv_ieee_754_bit_representation_from_u16() {
|
||||
.want_f64 = inf,
|
||||
},
|
||||
{
|
||||
.u16_bits = 0x7E00,
|
||||
.u16_bits = 0x7FFF,
|
||||
.want_u64 = nan_u64,
|
||||
.want_f64 = nan,
|
||||
},
|
||||
@@ -519,6 +519,27 @@ test_wuffs_strconv_ieee_754_bit_representation_from_u16() {
|
||||
RETURN_FAIL("tc=%d: to_u64: have 0x%016" PRIX64 ", want 0x%016" PRIX64,
|
||||
tc, have_u64, want_u64);
|
||||
}
|
||||
|
||||
int noise;
|
||||
for (noise = 0; noise < 2; noise++) {
|
||||
if ((noise > 0) && ((want_f64 == inf) || (want_f64 == -inf))) {
|
||||
continue;
|
||||
}
|
||||
wuffs_base__lossy_value_u16 lv =
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u16(
|
||||
wuffs_base__ieee_754_bit_representation__from_u64_to_f64(
|
||||
want_u64 ^ ((uint64_t)noise)));
|
||||
if (lv.value != test_cases[tc].u16_bits) {
|
||||
RETURN_FAIL("tc=%d: noise=%d: to_u16 value: have 0x%04" PRIX16
|
||||
", want 0x%04" PRIX16,
|
||||
tc, noise, lv.value, test_cases[tc].u16_bits);
|
||||
}
|
||||
int want_lossy = (want_f64 == want_f64) ? noise : 0;
|
||||
if (((int)lv.lossy) != want_lossy) {
|
||||
RETURN_FAIL("tc=%d: noise=%d: to_u16 lossy: have %d, want %d", tc,
|
||||
noise, ((int)(lv.lossy)), want_lossy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -589,7 +610,7 @@ test_wuffs_strconv_ieee_754_bit_representation_from_u32() {
|
||||
.want_f64 = inf,
|
||||
},
|
||||
{
|
||||
.u32_bits = 0x7FC00000,
|
||||
.u32_bits = 0x7FFFFFFF,
|
||||
.want_u64 = nan_u64,
|
||||
.want_f64 = nan,
|
||||
},
|
||||
@@ -620,6 +641,27 @@ test_wuffs_strconv_ieee_754_bit_representation_from_u32() {
|
||||
RETURN_FAIL("tc=%d: to_u64: have 0x%016" PRIX64 ", want 0x%016" PRIX64,
|
||||
tc, have_u64, want_u64);
|
||||
}
|
||||
|
||||
int noise;
|
||||
for (noise = 0; noise < 2; noise++) {
|
||||
if ((noise > 0) && ((want_f64 == inf) || (want_f64 == -inf))) {
|
||||
continue;
|
||||
}
|
||||
wuffs_base__lossy_value_u32 lv =
|
||||
wuffs_base__ieee_754_bit_representation__from_f64_to_u32(
|
||||
wuffs_base__ieee_754_bit_representation__from_u64_to_f64(
|
||||
want_u64 ^ ((uint64_t)noise)));
|
||||
if (lv.value != test_cases[tc].u32_bits) {
|
||||
RETURN_FAIL("tc=%d: noise=%d: to_u32 value: have 0x%08" PRIX32
|
||||
", want 0x%08" PRIX32,
|
||||
tc, noise, lv.value, test_cases[tc].u32_bits);
|
||||
}
|
||||
int want_lossy = (want_f64 == want_f64) ? noise : 0;
|
||||
if (((int)lv.lossy) != want_lossy) {
|
||||
RETURN_FAIL("tc=%d: noise=%d: to_u32 lossy: have %d, want %d", tc,
|
||||
noise, ((int)(lv.lossy)), want_lossy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
Reference in new issue
Block a user