diff --git a/internal/cgen/base/core-public.h b/internal/cgen/base/core-public.h index accb01a2..92ac663a 100644 --- a/internal/cgen/base/core-public.h +++ b/internal/cgen/base/core-public.h @@ -383,6 +383,80 @@ wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) { return res; } +// -------- + +typedef struct { + uint64_t hi; + uint64_t lo; +} wuffs_base__multiply_u64__output; + +// wuffs_base__multiply_u64 returns x*y as a 128-bit value. +// +// The maximum inclusive output hi_lo is 0xFFFFFFFFFFFFFFFE_0000000000000001. +static inline wuffs_base__multiply_u64__output // +wuffs_base__multiply_u64(uint64_t x, uint64_t y) { + uint64_t x0 = x & 0xFFFFFFFF; + uint64_t x1 = x >> 32; + uint64_t y0 = y & 0xFFFFFFFF; + uint64_t y1 = y >> 32; + uint64_t w0 = x0 * y0; + uint64_t t = (x1 * y0) + (w0 >> 32); + uint64_t w1 = t & 0xFFFFFFFF; + uint64_t w2 = t >> 32; + w1 += x0 * y1; + wuffs_base__multiply_u64__output o; + o.hi = (x1 * y1) + w2 + (w1 >> 32); + o.lo = x * y; + return o; +} + + // -------- + +#if defined(__GNUC__) && (__SIZEOF_LONG__ == 8) + +static inline uint32_t // +wuffs_base__count_leading_zeroes_u64(uint64_t u) { + return u ? ((uint32_t)(__builtin_clzl(u))) : 64u; +} + +#else + +static inline uint32_t // +wuffs_base__count_leading_zeroes_u64(uint64_t u) { + if (u == 0) { + return 64; + } + + uint32_t n = 0; + if ((u >> 32) == 0) { + n |= 32; + u <<= 32; + } + if ((u >> 48) == 0) { + n |= 16; + u <<= 16; + } + if ((u >> 56) == 0) { + n |= 8; + u <<= 8; + } + if ((u >> 60) == 0) { + n |= 4; + u <<= 4; + } + if ((u >> 62) == 0) { + n |= 2; + u <<= 2; + } + if ((u >> 63) == 0) { + n |= 1; + u <<= 1; + } + return n; +} + +#endif // defined(__GNUC__) && (__SIZEOF_LONG__ == 8) + // -------- #define wuffs_base__load_u8be__no_bounds_check \ diff --git a/internal/cgen/base/strconv-impl.c b/internal/cgen/base/strconv-impl.c index 3b083a5a..2345ff81 100644 --- a/internal/cgen/base/strconv-impl.c +++ b/internal/cgen/base/strconv-impl.c @@ -312,6 +312,9 @@ fail_out_of_bounds: // fixed precision floating point decimal number, augmented with ±infinity // values, but it cannot represent NaN (Not a Number). // +// "High precision" means that the mantissa holds 500 decimal digits. 500 is +// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION. +// // An HPD isn't for general purpose arithmetic, only for conversions to and // from IEEE 754 double-precision floating point, where the largest and // smallest positive, finite values are approximately 1.8e+308 and 4.9e-324. @@ -331,7 +334,7 @@ fail_out_of_bounds: // For example, if num_digits is 3 and digits is "\x07\x08\x09": // - A decimal_point of -2 means ".00789" // - A decimal_point of -1 means ".0789" -// - A decimal_point of -0 means ".789" +// - A decimal_point of +0 means ".789" // - A decimal_point of +1 means "7.89" // - A decimal_point of +2 means "78.9" // - A decimal_point of +3 means "789." @@ -861,6 +864,371 @@ wuffs_base__private_implementation__high_prec_dec__small_rshift( // -------- +// The wuffs_base__private_implementation__etc_powers_of_10 tables were printed +// by script/print-mpb-powers-of-10.go. That script has an optional -comments +// flag, whose output is not copied here, which prints further detail. +// +// These tables are used in +// wuffs_base__private_implementation__medium_prec_bin__assign_from_hpd. + +// wuffs_base__private_implementation__big_powers_of_10 contains approximations +// to the powers of 10, ranging from 1e-348 to 1e+340, with the exponent +// stepping by 8: -348, -340, -332, ..., -12, -4, +4, +12, ..., +340. Each step +// consists of three uint32_t elements. There are 87 triples, 87 * 3 = 261. +// +// For example, the third approximation, for 1e-332, consists of the uint32_t +// triple (0x3055AC76, 0x8B16FB20, 0xFFFFFB72). The first two of that triple +// are a little-endian uint64_t value: 0x8B16FB203055AC76. The last one is an +// int32_t value: -1166. Together, they represent the approximation: +// 1e-332 ≈ 0x8B16FB203055AC76 * (2 ** -1166) +// Similarly, the (0x00000000, 0x9C400000, 0xFFFFFFCE) uint32_t triple means: +// 1e+4 ≈ 0x9C40000000000000 * (2 ** -50) // This approx'n is exact. +// Similarly, the (0xD4C4FB27, 0xED63A231, 0x000000A2) uint32_t triple means: +// 1e+68 ≈ 0xED63A231D4C4FB27 * (2 ** 162) +static const uint32_t + wuffs_base__private_implementation__big_powers_of_10[261] = { + 0x081C0288, 0xFA8FD5A0, 0xFFFFFB3C, 0xA23EBF76, 0xBAAEE17F, 0xFFFFFB57, + 0x3055AC76, 0x8B16FB20, 0xFFFFFB72, 0x5DCE35EA, 0xCF42894A, 0xFFFFFB8C, + 0x55653B2D, 0x9A6BB0AA, 0xFFFFFBA7, 0x3D1A45DF, 0xE61ACF03, 0xFFFFFBC1, + 0xC79AC6CA, 0xAB70FE17, 0xFFFFFBDC, 0xBEBCDC4F, 0xFF77B1FC, 0xFFFFFBF6, + 0x416BD60C, 0xBE5691EF, 0xFFFFFC11, 0x907FFC3C, 0x8DD01FAD, 0xFFFFFC2C, + 0x31559A83, 0xD3515C28, 0xFFFFFC46, 0xADA6C9B5, 0x9D71AC8F, 0xFFFFFC61, + 0x23EE8BCB, 0xEA9C2277, 0xFFFFFC7B, 0x4078536D, 0xAECC4991, 0xFFFFFC96, + 0x5DB6CE57, 0x823C1279, 0xFFFFFCB1, 0x4DFB5637, 0xC2109436, 0xFFFFFCCB, + 0x3848984F, 0x9096EA6F, 0xFFFFFCE6, 0x25823AC7, 0xD77485CB, 0xFFFFFD00, + 0x97BF97F4, 0xA086CFCD, 0xFFFFFD1B, 0x172AACE5, 0xEF340A98, 0xFFFFFD35, + 0x2A35B28E, 0xB23867FB, 0xFFFFFD50, 0xD2C63F3B, 0x84C8D4DF, 0xFFFFFD6B, + 0x1AD3CDBA, 0xC5DD4427, 0xFFFFFD85, 0xBB25C996, 0x936B9FCE, 0xFFFFFDA0, + 0x7D62A584, 0xDBAC6C24, 0xFFFFFDBA, 0x0D5FDAF6, 0xA3AB6658, 0xFFFFFDD5, + 0xDEC3F126, 0xF3E2F893, 0xFFFFFDEF, 0xAAFF80B8, 0xB5B5ADA8, 0xFFFFFE0A, + 0x6C7C4A8B, 0x87625F05, 0xFFFFFE25, 0x34C13053, 0xC9BCFF60, 0xFFFFFE3F, + 0x91BA2655, 0x964E858C, 0xFFFFFE5A, 0x70297EBD, 0xDFF97724, 0xFFFFFE74, + 0xB8E5B88F, 0xA6DFBD9F, 0xFFFFFE8F, 0x88747D94, 0xF8A95FCF, 0xFFFFFEA9, + 0x8FA89BCF, 0xB9447093, 0xFFFFFEC4, 0xBF0F156B, 0x8A08F0F8, 0xFFFFFEDF, + 0x653131B6, 0xCDB02555, 0xFFFFFEF9, 0xD07B7FAC, 0x993FE2C6, 0xFFFFFF14, + 0x2A2B3B06, 0xE45C10C4, 0xFFFFFF2E, 0x697392D3, 0xAA242499, 0xFFFFFF49, + 0x8300CA0E, 0xFD87B5F2, 0xFFFFFF63, 0x92111AEB, 0xBCE50864, 0xFFFFFF7E, + 0x6F5088CC, 0x8CBCCC09, 0xFFFFFF99, 0xE219652C, 0xD1B71758, 0xFFFFFFB3, + 0x00000000, 0x9C400000, 0xFFFFFFCE, 0x00000000, 0xE8D4A510, 0xFFFFFFE8, + 0xAC620000, 0xAD78EBC5, 0x00000003, 0xF8940984, 0x813F3978, 0x0000001E, + 0xC90715B3, 0xC097CE7B, 0x00000038, 0x7BEA5C70, 0x8F7E32CE, 0x00000053, + 0xABE98068, 0xD5D238A4, 0x0000006D, 0x179A2245, 0x9F4F2726, 0x00000088, + 0xD4C4FB27, 0xED63A231, 0x000000A2, 0x8CC8ADA8, 0xB0DE6538, 0x000000BD, + 0x1AAB65DB, 0x83C7088E, 0x000000D8, 0x42711D9A, 0xC45D1DF9, 0x000000F2, + 0xA61BE758, 0x924D692C, 0x0000010D, 0x1A708DEA, 0xDA01EE64, 0x00000127, + 0x9AEF774A, 0xA26DA399, 0x00000142, 0xB47D6B85, 0xF209787B, 0x0000015C, + 0x79DD1877, 0xB454E4A1, 0x00000177, 0x5B9BC5C2, 0x865B8692, 0x00000192, + 0xC8965D3D, 0xC83553C5, 0x000001AC, 0xFA97A0B3, 0x952AB45C, 0x000001C7, + 0x99A05FE3, 0xDE469FBD, 0x000001E1, 0xDB398C25, 0xA59BC234, 0x000001FC, + 0xA3989F5C, 0xF6C69A72, 0x00000216, 0x54E9BECE, 0xB7DCBF53, 0x00000231, + 0xF22241E2, 0x88FCF317, 0x0000024C, 0xD35C78A5, 0xCC20CE9B, 0x00000266, + 0x7B2153DF, 0x98165AF3, 0x00000281, 0x971F303A, 0xE2A0B5DC, 0x0000029B, + 0x5CE3B396, 0xA8D9D153, 0x000002B6, 0xA4A7443C, 0xFB9B7CD9, 0x000002D0, + 0xA7A44410, 0xBB764C4C, 0x000002EB, 0xB6409C1A, 0x8BAB8EEF, 0x00000306, + 0xA657842C, 0xD01FEF10, 0x00000320, 0xE9913129, 0x9B10A4E5, 0x0000033B, + 0xA19C0C9D, 0xE7109BFB, 0x00000355, 0x623BF429, 0xAC2820D9, 0x00000370, + 0x7AA7CF85, 0x80444B5E, 0x0000038B, 0x03ACDD2D, 0xBF21E440, 0x000003A5, + 0x5E44FF8F, 0x8E679C2F, 0x000003C0, 0x9C8CB841, 0xD433179D, 0x000003DA, + 0xB4E31BA9, 0x9E19DB92, 0x000003F5, 0xBADF77D9, 0xEB96BF6E, 0x0000040F, + 0x9BF0EE6B, 0xAF87023B, 0x0000042A, +}; + +// wuffs_base__private_implementation__small_powers_of_10 contains +// approximations to the powers of 10, ranging from 1e+0 to 1e+7, with the +// exponent stepping by 1. Each step consists of three uint32_t elements. +// +// For example, the third approximation, for 1e+2, consists of the uint32_t +// triple (0x00000000, 0xC8000000, 0xFFFFFFC7). The first two of that triple +// are a little-endian uint64_t value: 0xC800000000000000. The last one is an +// int32_t value: -57. Together, they represent the approximation: +// 1e+2 ≈ 0xC800000000000000 * (2 ** -57) // This approx'n is exact. +// Similarly, the (0x00000000, 0x9C400000, 0xFFFFFFCE) uint32_t triple means: +// 1e+4 ≈ 0x9C40000000000000 * (2 ** -50) // This approx'n is exact. +static const uint32_t + wuffs_base__private_implementation__small_powers_of_10[24] = { + 0x00000000, 0x80000000, 0xFFFFFFC1, 0x00000000, 0xA0000000, 0xFFFFFFC4, + 0x00000000, 0xC8000000, 0xFFFFFFC7, 0x00000000, 0xFA000000, 0xFFFFFFCA, + 0x00000000, 0x9C400000, 0xFFFFFFCE, 0x00000000, 0xC3500000, 0xFFFFFFD1, + 0x00000000, 0xF4240000, 0xFFFFFFD4, 0x00000000, 0x98968000, 0xFFFFFFD8, +}; + +// -------- + +// wuffs_base__private_implementation__medium_prec_bin (abbreviated as MPB) is +// a fixed precision floating point binary number. Unlike IEEE 754 Floating +// Point, it cannot represent infinity or NaN (Not a Number). +// +// "Medium precision" means that the mantissa holds 64 binary digits, a little +// more than "double precision", and sizeof(MPB) > sizeof(double). 64 is +// obviously the number of bits in a uint64_t. +// +// An MPB isn't for general purpose arithmetic, only for conversions to and +// from IEEE 754 double-precision floating point. +// +// There is no implicit mantissa bit. The mantissa field is zero if and only if +// the overall floating point value is ±0. An MPB is normalized if the mantissa +// is zero or its high bit (the 1<<63 bit) is set. +// +// There is no negative bit. An MPB can only represent non-negative numbers. +// +// The "all fields are zero" value is valid, and represents the number +0. +// +// This is the "Do It Yourself Floating Point" data structure from Loitsch, +// "Printing Floating-Point Numbers Quickly and Accurately with Integers" +// (https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf). +// +// Florian Loitsch is also the primary contributor to +// https://github.com/google/double-conversion +typedef struct { + uint64_t mantissa; + int32_t exp2; +} wuffs_base__private_implementation__medium_prec_bin; + +static uint32_t // +wuffs_base__private_implementation__medium_prec_bin__normalize( + wuffs_base__private_implementation__medium_prec_bin* m) { + if (m->mantissa == 0) { + return 0; + } + uint32_t shift = wuffs_base__count_leading_zeroes_u64(m->mantissa); + m->mantissa <<= shift; + m->exp2 -= (int32_t)shift; + return shift; +} + +// wuffs_base__private_implementation__medium_prec_bin__mul_pow_10 sets m to be +// (m * pow), where pow comes from an etc_powers_of_10 triple starting at p. +// +// The result is rounded, but not necessarily normalized. +// +// Preconditions: +// - m is non-NULL. +// - m->mantissa is non-zero. +// - m->mantissa's high bit is set (i.e. m is normalized). +// +// The etc_powers_of_10 triple is already normalized. +static void // +wuffs_base__private_implementation__medium_prec_bin__mul_pow_10( + wuffs_base__private_implementation__medium_prec_bin* m, + const uint32_t* p) { + uint64_t p_mantissa = ((uint64_t)p[0]) | (((uint64_t)p[1]) << 32); + int32_t p_exp2 = (int32_t)p[2]; + + wuffs_base__multiply_u64__output o = + wuffs_base__multiply_u64(m->mantissa, p_mantissa); + // Round the mantissa up. It cannot overflow because the maximum possible + // value of o.hi is 0xFFFFFFFFFFFFFFFE. + m->mantissa = o.hi + (o.lo >> 63); + m->exp2 = m->exp2 + p_exp2 + 64; +} + +// wuffs_base__private_implementation__medium_prec_bin__as_f64 converts m to a +// double (what C calls a double-precision float64). +// +// Preconditions: +// - m is non-NULL. +// - m->mantissa is non-zero. +// - m->mantissa's high bit is set (i.e. m is normalized). +static double // +wuffs_base__private_implementation__medium_prec_bin__as_f64( + const wuffs_base__private_implementation__medium_prec_bin* m, + bool negative) { + uint64_t mantissa64 = m->mantissa; + // An mpb's mantissa has the implicit (binary) decimal point at the right + // hand end of the mantissa's explicit digits. A double-precision's mantissa + // has that decimal point near the left hand end. There's also an explicit + // versus implicit leading 1 bit (binary digit). Together, the difference in + // semantics corresponds to adding 63. + int32_t exp2 = m->exp2 + 63; + + // Ensure that exp2 is at least -1022, the minimum double-precision exponent + // for normal (as opposed to subnormal) numbers. + if (-1022 > exp2) { + uint32_t n = (uint32_t)(-1022 - exp2); + mantissa64 >>= n; + exp2 += (int32_t)n; + } + + // Extract the (1 + 52) bits from the 64-bit mantissa64. 52 is the number of + // explicit mantissa bits in a double-precision f64. + // + // Before, we have 64 bits and due to normalization, the high bit 'H' is 1. + // 63 55 47 etc 15 7 + // H210_9876_5432_1098_7654_etc_etc_etc_5432_1098_7654_3210 + // ++++_++++_++++_++++_++++_etc_etc_etc_++++_+..._...._.... Kept bits. + // ...._...._...H_2109_8765_etc_etc_etc_6543_2109_8765_4321 After shifting. + // After, we have 53 bits (and bit #52 is this 'H' bit). + uint64_t mantissa53 = mantissa64 >> 11; + + // Round up if the old bit #10 (the highest bit dropped by shifting) was set. + // We also fix any overflow from rounding up. + if (mantissa64 & 1024) { + mantissa53++; + if ((mantissa53 >> 53) != 0) { + mantissa53 >>= 1; + exp2++; + } + } + + // Handle double-precision infinity (a nominal exponent of 1024) and + // subnormals (an exponent of -1023 and no implicit mantissa bit, bit #52). + if (exp2 >= 1024) { + mantissa53 = 0; + exp2 = 1024; + } else if ((mantissa53 >> 52) == 0) { + exp2 = -1023; + } + + // Pack the bits and return. + const int32_t f64_bias = -1023; + uint64_t exp2_bits = + (uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 << 11) - 1. + uint64_t bits = (mantissa53 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1. + (exp2_bits << 52) | // + (negative ? 0x8000000000000000 : 0); // (1 << 63). + return wuffs_base__ieee_754_bit_representation__to_f64(bits); +} + +// wuffs_base__private_implementation__medium_prec_bin__parse_number_f64 +// converts from an HPD to a double, using an MPB as scratch space. It returns +// a NULL status.repr if there is no ambiguity in the truncation or rounding to +// a float64 (an IEEE 754 double-precision floating point value). +// +// It may modify m even if it returns a non-NULL status.repr. +static wuffs_base__result_f64 // +wuffs_base__private_implementation__medium_prec_bin__parse_number_f64( + wuffs_base__private_implementation__medium_prec_bin* m, + const wuffs_base__private_implementation__high_prec_dec* h) { + // m->mantissa is a uint64_t, which is an integer approximation to a rational + // value - h's underlying digits after m's normalization. This error is an + // upper bound on the difference between the approximate and actual value. + // + // The DiyFpStrtod function in https://github.com/google/double-conversion + // uses a finer grain (1/8th of the ULP, Unit in the Last Place) when + // tracking error. This implementation is coarser (1 ULP) but simpler. + // + // It is an error in the "numerical approximation" sense, not in the typical + // programming sense (as in "bad input" or "a result type"). + uint64_t error = 0; + + // Convert up to 19 decimal digits (in h->digits) to 64 binary digits (in + // m->mantissa): (1e19 < (1<<64)) and ((1<<64) < 1e20). If we have more than + // 19 digits, we're truncating (with error). + uint32_t i; + uint32_t i_end = h->num_digits; + if (i_end > 19) { + i_end = 19; + error = 1; + } + uint64_t mantissa = 0; + for (i = 0; i < i_end; i++) { + mantissa = (10 * mantissa) + h->digits[i]; + } + m->mantissa = mantissa; + m->exp2 = 0; + + // Check that exp10 lies in the (big_powers_of_10 + small_powers_of_10) + // range, -348 ..= +347, stepping big_powers_of_10 by 8 (which is 87 triples) + // and small_powers_of_10 by 1 (which is 8 triples). + int32_t exp10 = h->decimal_point - ((int32_t)(i_end)); + if (exp10 < -348) { + goto fail; + } + uint32_t bpo10 = ((uint32_t)(exp10 + 348)) / 8; + uint32_t spo10 = ((uint32_t)(exp10 + 348)) % 8; + if (bpo10 >= 87) { + goto fail; + } + + // Normalize (and scale the error). + error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m); + + // Multiplying two MPB values nominally multiplies two mantissas, call them A + // and B, which are integer approximations to the precise values (A+a) and + // (B+b) for some error terms a and b. + // + // MPB multiplication calculates (((A+a) * (B+b)) >> 64) to be ((A*B) >> 64). + // Shifting (truncating) and rounding introduces further error. The + // difference between the calculated result: + // ((A*B ) >> 64) + // and the true result: + // ((A*B + A*b + a*B + a*b) >> 64) + rounding_error + // is: + // (( A*b + a*B + a*b) >> 64) + rounding_error + // which can be re-grouped as: + // ((A*b) >> 64) + ((a*(B+b)) >> 64) + rounding_error + // + // Now, let A and a be "m->mantissa" and "error", and B and b be the + // pre-calculated power of 10. A and B are both less than (1 << 64), a is the + // "error" local variable and b is less than 1. + // + // An upper bound (in absolute value) on ((A*b) >> 64) is therefore 1. + // + // Similarly, an upper bound on ((a*(B+b)) >> 64) is a, also known as error. + // + // Finally, the rounding_error is at most 1. + // + // In total, calling mpb__mul_pow_10 will increase the worst-case error by 2. + // The subsequent re-normalization can multiply that by a further factor. + + // Multiply by small_powers_of_10[etc]. + wuffs_base__private_implementation__medium_prec_bin__mul_pow_10( + m, &wuffs_base__private_implementation__small_powers_of_10[3 * spo10]); + error += 2; + error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m); + + // Multiply by big_powers_of_10[etc]. + wuffs_base__private_implementation__medium_prec_bin__mul_pow_10( + m, &wuffs_base__private_implementation__big_powers_of_10[3 * bpo10]); + error += 2; + error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m); + + // We have a good approximation of h, but we still have to check whether the + // error is small enough. Equivalently, whether the number of surplus + // mantissa bits (the bits dropped when going from m's 64 mantissa bits to + // the smaller number of double-precision mantissa bits) would always round + // up or down, even when perturbed by ±error. We start at 11 surplus bits (m + // has 64, double-precision has 1+52), but it can be higher for subnormals. + // + // In many cases, the error is small enough and we return true. + const int32_t f64_bias = -1023; + int32_t subnormal_exp2 = f64_bias - 63; + uint32_t surplus_bits = 11; + if (subnormal_exp2 >= m->exp2) { + surplus_bits += 1 + ((uint32_t)(subnormal_exp2 - m->exp2)); + } + + uint64_t surplus_mask = (((uint64_t)1) << surplus_bits) - 1; // e.g. 0x07FF. + uint64_t surplus = m->mantissa & surplus_mask; + uint64_t halfway = ((uint64_t)1) << (surplus_bits - 1); // e.g. 0x0400. + + // Do the final calculation in *signed* arithmetic. + int64_t i_surplus = (int64_t)surplus; + int64_t i_halfway = (int64_t)halfway; + int64_t i_error = (int64_t)error; + + if ((i_surplus > (i_halfway - i_error)) && + (i_surplus < (i_halfway + i_error))) { + goto fail; + } + + wuffs_base__result_f64 ret; + ret.status.repr = NULL; + ret.value = wuffs_base__private_implementation__medium_prec_bin__as_f64( + m, h->negative); + return ret; + +fail: + do { + wuffs_base__result_f64 ret; + ret.status.repr = "#base: mpb__parse_number_f64 failed"; + ret.value = 0; + return ret; + } while (0); +} + +// -------- + wuffs_base__result_f64 // wuffs_base__parse_number_f64_special(wuffs_base__slice_u8 s, const char* fallback_status_repr) { @@ -964,6 +1332,7 @@ fallback: wuffs_base__result_f64 // wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) { + wuffs_base__private_implementation__medium_prec_bin m; wuffs_base__private_implementation__high_prec_dec h; do { @@ -990,10 +1359,17 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) { goto infinity; } + wuffs_base__result_f64 mpb_result = + wuffs_base__private_implementation__medium_prec_bin__parse_number_f64( + &m, &h); + if (mpb_result.status.repr == NULL) { + return mpb_result; + } + // Scale by powers of 2 until we're in the range [½ .. 1], which gives us // our exponent (in base-2). First we shift right, possibly a little too // far, ending with a value certainly below 1 and possibly below ½... - const int32_t bias = -1023; + const int32_t f64_bias = -1023; int32_t exp2 = 0; while (h.decimal_point > 0) { uint32_t n = (uint32_t)(+h.decimal_point); @@ -1037,9 +1413,9 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) { // We're in the range [½ .. 1] but f64 uses [1 .. 2]. exp2--; - // The minimum normal exponent is (bias + 1). - while ((bias + 1) > exp2) { - uint32_t n = (uint32_t)((bias + 1) - exp2); + // The minimum normal exponent is (f64_bias + 1). + while ((f64_bias + 1) > exp2) { + uint32_t n = (uint32_t)((f64_bias + 1) - exp2); if (n > WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) { n = WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL; } @@ -1048,7 +1424,7 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) { } // Check for overflow. - if ((exp2 - bias) >= 0x07FF) { // (1 << 11) - 1. + if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1. goto infinity; } @@ -1061,21 +1437,22 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) { if ((man2 >> 53) != 0) { man2 >>= 1; exp2++; - if ((exp2 - bias) >= 0x07FF) { // (1 << 11) - 1. + if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1. goto infinity; } } // Handle subnormal numbers. if ((man2 >> 52) == 0) { - exp2 = bias; + exp2 = f64_bias; } // Pack the bits and return. - uint64_t exp2_bits = (uint64_t)((exp2 - bias) & 0x07FF); // (1 << 11) - 1. - uint64_t bits = (man2 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1. - (exp2_bits << 52) | // - (h.negative ? 0x8000000000000000 : 0); // (1 << 63). + uint64_t exp2_bits = + (uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 << 11) - 1. + uint64_t bits = (man2 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1. + (exp2_bits << 52) | // + (h.negative ? 0x8000000000000000 : 0); // (1 << 63). wuffs_base__result_f64 ret; ret.status.repr = NULL; diff --git a/internal/cgen/data.go b/internal/cgen/data.go index eacdbcaa..7250dff3 100644 --- a/internal/cgen/data.go +++ b/internal/cgen/data.go @@ -79,14 +79,14 @@ const baseStrConvImplC = "" + "wuffs_base__parse_number__hexadecimal_digits[*p++];\n if (v == 0) {\n goto fail_bad_argument;\n }\n v &= 0x0F;\n\n for (; p < q; p++) {\n if (*p == '_') {\n continue;\n }\n uint8_t digit = wuffs_base__parse_number__hexadecimal_digits[*p];\n if (digit == 0) {\n goto fail_bad_argument;\n }\n digit &= 0x0F;\n if ((v >> 60) != 0) {\n goto fail_out_of_bounds;\n }\n v = (v << 4) | ((uint64_t)(digit));\n }\n\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = v;\n return ret;\n } while (0);\n\nok_zero:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = NULL;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_bad_argument:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = wuffs_base__error__bad_argument;\n ret.value = 0;\n return ret;\n } while (0);\n\nfail_out_of_bounds:\n do {\n wuffs_base__result_u64 ret;\n ret.status.repr = wuffs_base__error__out_of_bounds;\n ret.value = 0;\n return ret;\n " + " } while (0);\n}\n\n " + "" + - "// ---------------- IEEE 754 Floating Point\n\n#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE 1023\n#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION 500\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// 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 +1023 mean infinity, below -1023 mean zero. The ±1023\n// bounds are further away from zero than ±(324 + 500), where 500 and 1023 is\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRE" + - "CISION 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 +1023 means that the overall value is\n// infinity, lower than -1023 means zero.\n//\n// negative is a sign bit. An HP" + - "D 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\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 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 for (; (p < q) && (*p == '_'); p++) {\n }\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\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 for (; (p < q) && (*p == '_'); p++) {\n }\n } while (0);\n\n // Parse digits.\n uint32_t nd = 0;\n int32_t dp = 0;\n bool saw_digits = false;\n bool saw_non_zero_digits = false;\n bool saw_dot = false;\n for (; p < q; p++) {\n if (*p == '_') {\n // No-op.\n\n } else if ((*p == '" + - ".') || (*p == ',')) {\n // As per https://en.wikipedia.org/wiki/Decimal_separator, both '.' or\n // ',' are commonly used. We just parse either, regardless of LOCALE.\n if (saw_dot) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n saw_dot = true;\n dp = (int32_t)nd;\n\n } else if ('0' == *p) {\n if (!saw_dot && !saw_non_zero_digits && saw_digits) {\n // We don't allow unnecessary leading zeroes: \"000123\" or \"0644\".\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n saw_digits = true;\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++] = 0;\n } else {\n // Long-tail zeroes are ignored.\n }\n\n } else if (('0' < *p) && (*p <= '9')) {\n if (!saw_dot && !saw_non_zero_digits && saw_digits) {\n // We don't allow unnecessary leading zeroes: \"" + - "000123\" or \"0644\".\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n saw_digits = true;\n saw_non_zero_digits = true;\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\n } else {\n break;\n }\n }\n\n if (!saw_digits) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n if (!saw_dot) {\n dp = (int32_t)nd;\n }\n\n // Parse exponent.\n if ((p < q) && ((*p == 'E') || (*p == 'e'))) {\n p++;\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\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_P" + - "OINT__RANGE +\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION;\n bool saw_exp_digits = false;\n for (; p < q; p++) {\n if (*p == '_') {\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 }\n\n // Finish.\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 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 +WU" + - "FFS_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\n#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE 1023\n#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION 500\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 500 decimal digits. 500 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 +1023 mean infinity, below -1023 mean zero. Th" + + "e ±1023\n// bounds are further away from zero than ±(324 + 500), where 500 and 1023 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 +1023 means that the overall value is\n// infinity, lower than -1023 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\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 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 for (; (p < q) && (*p == '_'); p++) {\n }\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\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 for (; (p < q) && (*p == '_'); p++) {\n }\n } while (0);\n\n // Parse digits.\n uint32_t nd = 0;\n int32_t dp = 0;\n bool saw_digits = false;\n " + + "bool saw_non_zero_digits = false;\n bool saw_dot = false;\n for (; p < q; p++) {\n if (*p == '_') {\n // No-op.\n\n } else if ((*p == '.') || (*p == ',')) {\n // As per https://en.wikipedia.org/wiki/Decimal_separator, both '.' or\n // ',' are commonly used. We just parse either, regardless of LOCALE.\n if (saw_dot) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n saw_dot = true;\n dp = (int32_t)nd;\n\n } else if ('0' == *p) {\n if (!saw_dot && !saw_non_zero_digits && saw_digits) {\n // We don't allow unnecessary leading zeroes: \"000123\" or \"0644\".\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n saw_digits = true;\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++] = 0;\n } else {\n // Long-tail zeroes are ignored.\n }\n\n } else if (('" + + "0' < *p) && (*p <= '9')) {\n if (!saw_dot && !saw_non_zero_digits && saw_digits) {\n // We don't allow unnecessary leading zeroes: \"000123\" or \"0644\".\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n saw_digits = true;\n saw_non_zero_digits = true;\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\n } else {\n break;\n }\n }\n\n if (!saw_digits) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n if (!saw_dot) {\n dp = (int32_t)nd;\n }\n\n // Parse exponent.\n if ((p < q) && ((*p == 'E') || (*p == 'e'))) {\n p++;\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\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 // 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 }\n\n // Finish.\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 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// The etc__hpd_left_shift and etc__powers_of_5 tables were printed by\n// script/print-hpd-left-shift.go. That script has an optional -comments flag,\n// whose output is not copied here, which prints further detail.\n//\n// These tables are used in\n// wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits.\n\n// wuffs_base__private_implementation__hpd_left_shift[i] encodes the number of\n// new digits created after multiplying a positive integer by (1 << i): the\n// additional length in the decimal representation. For example, shifting \"234\"\n// by 3 (equivalent to multiplying by 8) will produce \"1872\". Going from a\n// 3-length string to a 4-length string means that 1 new digit was added (and\n// existing digits may have changed).\n//\n// Shifting by i can add either N or N-1 new digits, depending on whether the\n// original positive integer compares >= or < to the i'th power of 5 (as 10\n// equals 2 * 5). Comparison is lexicographic, not numerical.\n//\n// For example, shifting by 4 (i.e. mul" + "tiplying by 16) can add 1 or 2 new\n// digits, depending on a lexicographic comparison to (5 ** 4), i.e. \"625\":\n// - (\"1\" << 4) is \"16\", which adds 1 new digit.\n// - (\"5678\" << 4) is \"90848\", which adds 1 new digit.\n// - (\"624\" << 4) is \"9984\", which adds 1 new digit.\n// - (\"62498\" << 4) is \"999968\", which adds 1 new digit.\n// - (\"625\" << 4) is \"10000\", which adds 2 new digits.\n// - (\"625001\" << 4) is \"10000016\", which adds 2 new digits.\n// - (\"7008\" << 4) is \"112128\", which adds 2 new digits.\n// - (\"99\" << 4) is \"1584\", which adds 2 new digits.\n//\n// Thus, when i is 4, N is 2 and (5 ** i) is \"625\". This etc__hpd_left_shift\n// array encodes this as:\n// - etc__hpd_left_shift[4] is 0x1006 = (2 << 11) | 0x0006.\n// - etc__hpd_left_shift[5] is 0x1009 = (? << 11) | 0x0009.\n// where the ? isn't relevant for i == 4.\n//\n// The high 5 bits of etc__hpd_left_shift[i] is N, the higher of the two\n// possible number of new digits. The low 11 bits are an offset into the\n//" + @@ -105,14 +105,33 @@ const baseStrConvImplC = "" + " 0;\n\n // Pick up enough leading digits to cover the first shift.\n while ((n >> shift) == 0) {\n if (rx < h->num_digits) {\n // Read a digit.\n n = (10 * n) + h->digits[rx++];\n } else if (n == 0) {\n // h's number used to be zero and remains zero.\n return;\n } else {\n // Read sufficient implicit trailing zeroes.\n while ((n >> shift) == 0) {\n n = 10 * n;\n rx++;\n }\n break;\n }\n }\n h->decimal_point -= ((int32_t)(rx - 1));\n if (h->decimal_point <\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n // After the shift, h's number is effectively zero.\n h->num_digits = 0;\n h->decimal_point = 0;\n h->negative = false;\n h->truncated = false;\n return;\n }\n\n // Repeat: pick up a digit, put down a digit, left to right.\n uint64_t mask = (((uint64_t)(1)) << shift) - 1;\n while (rx < h->num_digits) {\n uint8_t new_digit = ((uint8_t)(n >> shift));\n n = (10 * (n & mask)) + h->digits[rx++];\n h->digits[wx++] = new_digi" + "t;\n }\n\n // Put down trailing digits, left to right.\n while (n > 0) {\n uint8_t new_digit = ((uint8_t)(n >> shift));\n n = 10 * (n & mask);\n if (wx < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[wx++] = new_digit;\n } else if (new_digit > 0) {\n h->truncated = true;\n }\n }\n\n // Finish.\n h->num_digits = wx;\n wuffs_base__private_implementation__high_prec_dec__trim(h);\n}\n\n" + "" + + "// --------\n\n// The wuffs_base__private_implementation__etc_powers_of_10 tables were printed\n// by script/print-mpb-powers-of-10.go. That script has an optional -comments\n// flag, whose output is not copied here, which prints further detail.\n//\n// These tables are used in\n// wuffs_base__private_implementation__medium_prec_bin__assign_from_hpd.\n\n// wuffs_base__private_implementation__big_powers_of_10 contains approximations\n// to the powers of 10, ranging from 1e-348 to 1e+340, with the exponent\n// stepping by 8: -348, -340, -332, ..., -12, -4, +4, +12, ..., +340. Each step\n// consists of three uint32_t elements. There are 87 triples, 87 * 3 = 261.\n//\n// For example, the third approximation, for 1e-332, consists of the uint32_t\n// triple (0x3055AC76, 0x8B16FB20, 0xFFFFFB72). The first two of that triple\n// are a little-endian uint64_t value: 0x8B16FB203055AC76. The last one is an\n// int32_t value: -1166. Together, they represent the approximation:\n// 1e-332 ≈ 0x8B16FB203055AC76 * (2 ** -1166)\n// Similarly," + + " the (0x00000000, 0x9C400000, 0xFFFFFFCE) uint32_t triple means:\n// 1e+4 ≈ 0x9C40000000000000 * (2 ** -50) // This approx'n is exact.\n// Similarly, the (0xD4C4FB27, 0xED63A231, 0x000000A2) uint32_t triple means:\n// 1e+68 ≈ 0xED63A231D4C4FB27 * (2 ** 162)\nstatic const uint32_t\n wuffs_base__private_implementation__big_powers_of_10[261] = {\n 0x081C0288, 0xFA8FD5A0, 0xFFFFFB3C, 0xA23EBF76, 0xBAAEE17F, 0xFFFFFB57,\n 0x3055AC76, 0x8B16FB20, 0xFFFFFB72, 0x5DCE35EA, 0xCF42894A, 0xFFFFFB8C,\n 0x55653B2D, 0x9A6BB0AA, 0xFFFFFBA7, 0x3D1A45DF, 0xE61ACF03, 0xFFFFFBC1,\n 0xC79AC6CA, 0xAB70FE17, 0xFFFFFBDC, 0xBEBCDC4F, 0xFF77B1FC, 0xFFFFFBF6,\n 0x416BD60C, 0xBE5691EF, 0xFFFFFC11, 0x907FFC3C, 0x8DD01FAD, 0xFFFFFC2C,\n 0x31559A83, 0xD3515C28, 0xFFFFFC46, 0xADA6C9B5, 0x9D71AC8F, 0xFFFFFC61,\n 0x23EE8BCB, 0xEA9C2277, 0xFFFFFC7B, 0x4078536D, 0xAECC4991, 0xFFFFFC96,\n 0x5DB6CE57, 0x823C1279, 0xFFFFFCB1, 0x4DFB5637, 0xC2109436, 0xFFFFFCCB,\n 0x3848984F, 0x909" + + "6EA6F, 0xFFFFFCE6, 0x25823AC7, 0xD77485CB, 0xFFFFFD00,\n 0x97BF97F4, 0xA086CFCD, 0xFFFFFD1B, 0x172AACE5, 0xEF340A98, 0xFFFFFD35,\n 0x2A35B28E, 0xB23867FB, 0xFFFFFD50, 0xD2C63F3B, 0x84C8D4DF, 0xFFFFFD6B,\n 0x1AD3CDBA, 0xC5DD4427, 0xFFFFFD85, 0xBB25C996, 0x936B9FCE, 0xFFFFFDA0,\n 0x7D62A584, 0xDBAC6C24, 0xFFFFFDBA, 0x0D5FDAF6, 0xA3AB6658, 0xFFFFFDD5,\n 0xDEC3F126, 0xF3E2F893, 0xFFFFFDEF, 0xAAFF80B8, 0xB5B5ADA8, 0xFFFFFE0A,\n 0x6C7C4A8B, 0x87625F05, 0xFFFFFE25, 0x34C13053, 0xC9BCFF60, 0xFFFFFE3F,\n 0x91BA2655, 0x964E858C, 0xFFFFFE5A, 0x70297EBD, 0xDFF97724, 0xFFFFFE74,\n 0xB8E5B88F, 0xA6DFBD9F, 0xFFFFFE8F, 0x88747D94, 0xF8A95FCF, 0xFFFFFEA9,\n 0x8FA89BCF, 0xB9447093, 0xFFFFFEC4, 0xBF0F156B, 0x8A08F0F8, 0xFFFFFEDF,\n 0x653131B6, 0xCDB02555, 0xFFFFFEF9, 0xD07B7FAC, 0x993FE2C6, 0xFFFFFF14,\n 0x2A2B3B06, 0xE45C10C4, 0xFFFFFF2E, 0x697392D3, 0xAA242499, 0xFFFFFF49,\n 0x8300CA0E, 0xFD87B5F2, 0xFFFFFF63, 0x92111AEB, 0xBCE50864, 0xFFFFFF7E,\n 0" + + "x6F5088CC, 0x8CBCCC09, 0xFFFFFF99, 0xE219652C, 0xD1B71758, 0xFFFFFFB3,\n 0x00000000, 0x9C400000, 0xFFFFFFCE, 0x00000000, 0xE8D4A510, 0xFFFFFFE8,\n 0xAC620000, 0xAD78EBC5, 0x00000003, 0xF8940984, 0x813F3978, 0x0000001E,\n 0xC90715B3, 0xC097CE7B, 0x00000038, 0x7BEA5C70, 0x8F7E32CE, 0x00000053,\n 0xABE98068, 0xD5D238A4, 0x0000006D, 0x179A2245, 0x9F4F2726, 0x00000088,\n 0xD4C4FB27, 0xED63A231, 0x000000A2, 0x8CC8ADA8, 0xB0DE6538, 0x000000BD,\n 0x1AAB65DB, 0x83C7088E, 0x000000D8, 0x42711D9A, 0xC45D1DF9, 0x000000F2,\n 0xA61BE758, 0x924D692C, 0x0000010D, 0x1A708DEA, 0xDA01EE64, 0x00000127,\n 0x9AEF774A, 0xA26DA399, 0x00000142, 0xB47D6B85, 0xF209787B, 0x0000015C,\n 0x79DD1877, 0xB454E4A1, 0x00000177, 0x5B9BC5C2, 0x865B8692, 0x00000192,\n 0xC8965D3D, 0xC83553C5, 0x000001AC, 0xFA97A0B3, 0x952AB45C, 0x000001C7,\n 0x99A05FE3, 0xDE469FBD, 0x000001E1, 0xDB398C25, 0xA59BC234, 0x000001FC,\n 0xA3989F5C, 0xF6C69A72, 0x00000216, 0x54E9BECE, 0xB7DCBF53, 0x000" + + "00231,\n 0xF22241E2, 0x88FCF317, 0x0000024C, 0xD35C78A5, 0xCC20CE9B, 0x00000266,\n 0x7B2153DF, 0x98165AF3, 0x00000281, 0x971F303A, 0xE2A0B5DC, 0x0000029B,\n 0x5CE3B396, 0xA8D9D153, 0x000002B6, 0xA4A7443C, 0xFB9B7CD9, 0x000002D0,\n 0xA7A44410, 0xBB764C4C, 0x000002EB, 0xB6409C1A, 0x8BAB8EEF, 0x00000306,\n 0xA657842C, 0xD01FEF10, 0x00000320, 0xE9913129, 0x9B10A4E5, 0x0000033B,\n 0xA19C0C9D, 0xE7109BFB, 0x00000355, 0x623BF429, 0xAC2820D9, 0x00000370,\n 0x7AA7CF85, 0x80444B5E, 0x0000038B, 0x03ACDD2D, 0xBF21E440, 0x000003A5,\n 0x5E44FF8F, 0x8E679C2F, 0x000003C0, 0x9C8CB841, 0xD433179D, 0x000003DA,\n 0xB4E31BA9, 0x9E19DB92, 0x000003F5, 0xBADF77D9, 0xEB96BF6E, 0x0000040F,\n 0x9BF0EE6B, 0xAF87023B, 0x0000042A,\n};\n\n// wuffs_base__private_implementation__small_powers_of_10 contains\n// approximations to the powers of 10, ranging from 1e+0 to 1e+7, with the\n// exponent stepping by 1. Each step consists of three uint32_t elements.\n//\n// For example, the third appr" + + "oximation, for 1e+2, consists of the uint32_t\n// triple (0x00000000, 0xC8000000, 0xFFFFFFC7). The first two of that triple\n// are a little-endian uint64_t value: 0xC800000000000000. The last one is an\n// int32_t value: -57. Together, they represent the approximation:\n// 1e+2 ≈ 0xC800000000000000 * (2 ** -57) // This approx'n is exact.\n// Similarly, the (0x00000000, 0x9C400000, 0xFFFFFFCE) uint32_t triple means:\n// 1e+4 ≈ 0x9C40000000000000 * (2 ** -50) // This approx'n is exact.\nstatic const uint32_t\n wuffs_base__private_implementation__small_powers_of_10[24] = {\n 0x00000000, 0x80000000, 0xFFFFFFC1, 0x00000000, 0xA0000000, 0xFFFFFFC4,\n 0x00000000, 0xC8000000, 0xFFFFFFC7, 0x00000000, 0xFA000000, 0xFFFFFFCA,\n 0x00000000, 0x9C400000, 0xFFFFFFCE, 0x00000000, 0xC3500000, 0xFFFFFFD1,\n 0x00000000, 0xF4240000, 0xFFFFFFD4, 0x00000000, 0x98968000, 0xFFFFFFD8,\n};\n\n" + + "" + + "// --------\n\n// wuffs_base__private_implementation__medium_prec_bin (abbreviated as MPB) is\n// a fixed precision floating point binary number. Unlike IEEE 754 Floating\n// Point, it cannot represent infinity or NaN (Not a Number).\n//\n// \"Medium precision\" means that the mantissa holds 64 binary digits, a little\n// more than \"double precision\", and sizeof(MPB) > sizeof(double). 64 is\n// obviously the number of bits in a uint64_t.\n//\n// An MPB isn't for general purpose arithmetic, only for conversions to and\n// from IEEE 754 double-precision floating point.\n//\n// There is no implicit mantissa bit. The mantissa field is zero if and only if\n// the overall floating point value is ±0. An MPB is normalized if the mantissa\n// is zero or its high bit (the 1<<63 bit) is set.\n//\n// There is no negative bit. An MPB can only represent non-negative numbers.\n//\n// The \"all fields are zero\" value is valid, and represents the number +0.\n//\n// This is the \"Do It Yourself Floating Point\" data structure from Loitsch,\n// \"Printin" + + "g Floating-Point Numbers Quickly and Accurately with Integers\"\n// (https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf).\n//\n// Florian Loitsch is also the primary contributor to\n// https://github.com/google/double-conversion\ntypedef struct {\n uint64_t mantissa;\n int32_t exp2;\n} wuffs_base__private_implementation__medium_prec_bin;\n\nstatic uint32_t //\nwuffs_base__private_implementation__medium_prec_bin__normalize(\n wuffs_base__private_implementation__medium_prec_bin* m) {\n if (m->mantissa == 0) {\n return 0;\n }\n uint32_t shift = wuffs_base__count_leading_zeroes_u64(m->mantissa);\n m->mantissa <<= shift;\n m->exp2 -= (int32_t)shift;\n return shift;\n}\n\n// wuffs_base__private_implementation__medium_prec_bin__mul_pow_10 sets m to be\n// (m * pow), where pow comes from an etc_powers_of_10 triple starting at p.\n//\n// The result is rounded, but not necessarily normalized.\n//\n// Preconditions:\n// - m is non-NULL.\n// - m->mantissa is non-zero.\n// - m->mantissa's high bit is set (i.e. m is " + + "normalized).\n//\n// The etc_powers_of_10 triple is already normalized.\nstatic void //\nwuffs_base__private_implementation__medium_prec_bin__mul_pow_10(\n wuffs_base__private_implementation__medium_prec_bin* m,\n const uint32_t* p) {\n uint64_t p_mantissa = ((uint64_t)p[0]) | (((uint64_t)p[1]) << 32);\n int32_t p_exp2 = (int32_t)p[2];\n\n wuffs_base__multiply_u64__output o =\n wuffs_base__multiply_u64(m->mantissa, p_mantissa);\n // Round the mantissa up. It cannot overflow because the maximum possible\n // value of o.hi is 0xFFFFFFFFFFFFFFFE.\n m->mantissa = o.hi + (o.lo >> 63);\n m->exp2 = m->exp2 + p_exp2 + 64;\n}\n\n// wuffs_base__private_implementation__medium_prec_bin__as_f64 converts m to a\n// double (what C calls a double-precision float64).\n//\n// Preconditions:\n// - m is non-NULL.\n// - m->mantissa is non-zero.\n// - m->mantissa's high bit is set (i.e. m is normalized).\nstatic double //\nwuffs_base__private_implementation__medium_prec_bin__as_f64(\n const wuffs_base__private_implementation__mediu" + + "m_prec_bin* m,\n bool negative) {\n uint64_t mantissa64 = m->mantissa;\n // An mpb's mantissa has the implicit (binary) decimal point at the right\n // hand end of the mantissa's explicit digits. A double-precision's mantissa\n // has that decimal point near the left hand end. There's also an explicit\n // versus implicit leading 1 bit (binary digit). Together, the difference in\n // semantics corresponds to adding 63.\n int32_t exp2 = m->exp2 + 63;\n\n // Ensure that exp2 is at least -1022, the minimum double-precision exponent\n // for normal (as opposed to subnormal) numbers.\n if (-1022 > exp2) {\n uint32_t n = (uint32_t)(-1022 - exp2);\n mantissa64 >>= n;\n exp2 += (int32_t)n;\n }\n\n // Extract the (1 + 52) bits from the 64-bit mantissa64. 52 is the number of\n // explicit mantissa bits in a double-precision f64.\n //\n // Before, we have 64 bits and due to normalization, the high bit 'H' is 1.\n // 63 55 47 etc 15 7\n // H210_9876_5432_1098_7654_etc_etc_etc_5432_109" + + "8_7654_3210\n // ++++_++++_++++_++++_++++_etc_etc_etc_++++_+..._...._.... Kept bits.\n // ...._...._...H_2109_8765_etc_etc_etc_6543_2109_8765_4321 After shifting.\n // After, we have 53 bits (and bit #52 is this 'H' bit).\n uint64_t mantissa53 = mantissa64 >> 11;\n\n // Round up if the old bit #10 (the highest bit dropped by shifting) was set.\n // We also fix any overflow from rounding up.\n if (mantissa64 & 1024) {\n mantissa53++;\n if ((mantissa53 >> 53) != 0) {\n mantissa53 >>= 1;\n exp2++;\n }\n }\n\n // Handle double-precision infinity (a nominal exponent of 1024) and\n // subnormals (an exponent of -1023 and no implicit mantissa bit, bit #52).\n if (exp2 >= 1024) {\n mantissa53 = 0;\n exp2 = 1024;\n } else if ((mantissa53 >> 52) == 0) {\n exp2 = -1023;\n }\n\n // Pack the bits and return.\n const int32_t f64_bias = -1023;\n uint64_t exp2_bits =\n (uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 << 11) - 1.\n uint64_t bits = (mantissa53 & 0x000FFFFFFFFFFFFF) | // (1 << 52" + + ") - 1.\n (exp2_bits << 52) | //\n (negative ? 0x8000000000000000 : 0); // (1 << 63).\n return wuffs_base__ieee_754_bit_representation__to_f64(bits);\n}\n\n// wuffs_base__private_implementation__medium_prec_bin__parse_number_f64\n// converts from an HPD to a double, using an MPB as scratch space. It returns\n// a NULL status.repr if there is no ambiguity in the truncation or rounding to\n// a float64 (an IEEE 754 double-precision floating point value).\n//\n// It may modify m even if it returns a non-NULL status.repr.\nstatic wuffs_base__result_f64 //\nwuffs_base__private_implementation__medium_prec_bin__parse_number_f64(\n wuffs_base__private_implementation__medium_prec_bin* m,\n const wuffs_base__private_implementation__high_prec_dec* h) {\n // m->mantissa is a uint64_t, which is an integer approximation to a rational\n // value - h's underlying digits after m's normalization. This error is an\n // upper bound on the difference between the approximate and actual " + + "value.\n //\n // The DiyFpStrtod function in https://github.com/google/double-conversion\n // uses a finer grain (1/8th of the ULP, Unit in the Last Place) when\n // tracking error. This implementation is coarser (1 ULP) but simpler.\n //\n // It is an error in the \"numerical approximation\" sense, not in the typical\n // programming sense (as in \"bad input\" or \"a result type\").\n uint64_t error = 0;\n\n // Convert up to 19 decimal digits (in h->digits) to 64 binary digits (in\n // m->mantissa): (1e19 < (1<<64)) and ((1<<64) < 1e20). If we have more than\n // 19 digits, we're truncating (with error).\n uint32_t i;\n uint32_t i_end = h->num_digits;\n if (i_end > 19) {\n i_end = 19;\n error = 1;\n }\n uint64_t mantissa = 0;\n for (i = 0; i < i_end; i++) {\n mantissa = (10 * mantissa) + h->digits[i];\n }\n m->mantissa = mantissa;\n m->exp2 = 0;\n\n // Check that exp10 lies in the (big_powers_of_10 + small_powers_of_10)\n // range, -348 ..= +347, stepping big_powers_of_10 by 8 (which is 87 triples)\n // and sm" + + "all_powers_of_10 by 1 (which is 8 triples).\n int32_t exp10 = h->decimal_point - ((int32_t)(i_end));\n if (exp10 < -348) {\n goto fail;\n }\n uint32_t bpo10 = ((uint32_t)(exp10 + 348)) / 8;\n uint32_t spo10 = ((uint32_t)(exp10 + 348)) % 8;\n if (bpo10 >= 87) {\n goto fail;\n }\n\n // Normalize (and scale the error).\n error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m);\n\n // Multiplying two MPB values nominally multiplies two mantissas, call them A\n // and B, which are integer approximations to the precise values (A+a) and\n // (B+b) for some error terms a and b.\n //\n // MPB multiplication calculates (((A+a) * (B+b)) >> 64) to be ((A*B) >> 64).\n // Shifting (truncating) and rounding introduces further error. The\n // difference between the calculated result:\n // ((A*B ) >> 64)\n // and the true result:\n // ((A*B + A*b + a*B + a*b) >> 64) + rounding_error\n // is:\n // (( A*b + a*B + a*b) >> 64) + rounding_error\n // which can be re-grouped as:\n /" + + "/ ((A*b) >> 64) + ((a*(B+b)) >> 64) + rounding_error\n //\n // Now, let A and a be \"m->mantissa\" and \"error\", and B and b be the\n // pre-calculated power of 10. A and B are both less than (1 << 64), a is the\n // \"error\" local variable and b is less than 1.\n //\n // An upper bound (in absolute value) on ((A*b) >> 64) is therefore 1.\n //\n // Similarly, an upper bound on ((a*(B+b)) >> 64) is a, also known as error.\n //\n // Finally, the rounding_error is at most 1.\n //\n // In total, calling mpb__mul_pow_10 will increase the worst-case error by 2.\n // The subsequent re-normalization can multiply that by a further factor.\n\n // Multiply by small_powers_of_10[etc].\n wuffs_base__private_implementation__medium_prec_bin__mul_pow_10(\n m, &wuffs_base__private_implementation__small_powers_of_10[3 * spo10]);\n error += 2;\n error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m);\n\n // Multiply by big_powers_of_10[etc].\n wuffs_base__private_implementation__medium_prec_bin__mul_pow_10(\n" + + " m, &wuffs_base__private_implementation__big_powers_of_10[3 * bpo10]);\n error += 2;\n error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m);\n\n // We have a good approximation of h, but we still have to check whether the\n // error is small enough. Equivalently, whether the number of surplus\n // mantissa bits (the bits dropped when going from m's 64 mantissa bits to\n // the smaller number of double-precision mantissa bits) would always round\n // up or down, even when perturbed by ±error. We start at 11 surplus bits (m\n // has 64, double-precision has 1+52), but it can be higher for subnormals.\n //\n // In many cases, the error is small enough and we return true.\n const int32_t f64_bias = -1023;\n int32_t subnormal_exp2 = f64_bias - 63;\n uint32_t surplus_bits = 11;\n if (subnormal_exp2 >= m->exp2) {\n surplus_bits += 1 + ((uint32_t)(subnormal_exp2 - m->exp2));\n }\n\n uint64_t surplus_mask = (((uint64_t)1) << surplus_bits) - 1; // e.g. 0x07FF.\n uint64_t surplus = m->manti" + + "ssa & surplus_mask;\n uint64_t halfway = ((uint64_t)1) << (surplus_bits - 1); // e.g. 0x0400.\n\n // Do the final calculation in *signed* arithmetic.\n int64_t i_surplus = (int64_t)surplus;\n int64_t i_halfway = (int64_t)halfway;\n int64_t i_error = (int64_t)error;\n\n if ((i_surplus > (i_halfway - i_error)) &&\n (i_surplus < (i_halfway + i_error))) {\n goto fail;\n }\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__private_implementation__medium_prec_bin__as_f64(\n m, h->negative);\n return ret;\n\nfail:\n do {\n wuffs_base__result_f64 ret;\n ret.status.repr = \"#base: mpb__parse_number_f64 failed\";\n ret.value = 0;\n return ret;\n } while (0);\n}\n\n" + + "" + "// --------\n\nwuffs_base__result_f64 //\nwuffs_base__parse_number_f64_special(wuffs_base__slice_u8 s,\n const char* fallback_status_repr) {\n do {\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 fallback;\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 fallback;\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 fallback;\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 fallback;\n }\n p += 5;\n\n if ((p >= q) || (*p == '_')) {\n break;\n }\n goto fallback;\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 fallback;\n }\n p += 3;\n\n if ((p >= q) || (*p == '_')) {\n nan = true;\n break;\n }\n goto fallback;\n\n default:\n goto fallback;\n }\n\n // Finish.\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p != q) {\n goto fallback;\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = w" + - "uffs_base__ieee_754_bit_representation__to_f64(\n (nan ? 0x7FFFFFFFFFFFFFFF : 0x7FF0000000000000) |\n (negative ? 0x8000000000000000 : 0));\n return ret;\n } while (0);\n\nfallback:\n do {\n wuffs_base__result_f64 ret;\n ret.status.repr = fallback_status_repr;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\nwuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wuffs_base__slice_u8 s) {\n wuffs_base__private_implementation__high_prec_dec h;\n\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 wuffs_base__status status =\n wuffs_base__private_implementation__high_prec_dec__parse(&h, s);\n if (status.repr) {\n" + - " return wuffs_base__parse_number_f64_special(s, status.repr);\n }\n\n // Handle 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 // 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 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,\n sh" + - "ift);\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,\n 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 (bias + 1).\n while ((bias + 1) > exp2) {\n uint32_t n = (uint32_t)((bias + 1) - exp2);\n if (n > WUFFS_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 - 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 - bias) >= 0x07FF) { // (1 << 11) - 1.\n goto infinity;\n }\n }\n\n // Handle subnormal numbers.\n if ((" + - "man2 >> 52) == 0) {\n exp2 = bias;\n }\n\n // Pack the bits and return.\n uint64_t exp2_bits = (uint64_t)((exp2 - 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 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 } whi" + - "le (0);\n}\n\n" + + "uffs_base__ieee_754_bit_representation__to_f64(\n (nan ? 0x7FFFFFFFFFFFFFFF : 0x7FF0000000000000) |\n (negative ? 0x8000000000000000 : 0));\n return ret;\n } while (0);\n\nfallback:\n do {\n wuffs_base__result_f64 ret;\n ret.status.repr = fallback_status_repr;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\nwuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wuffs_base__slice_u8 s) {\n wuffs_base__private_implementation__medium_prec_bin m;\n wuffs_base__private_implementation__high_prec_dec h;\n\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 wuffs_base__status status =\n wuffs_base__private_implementat" + + "ion__high_prec_dec__parse(&h, s);\n if (status.repr) {\n return wuffs_base__parse_number_f64_special(s, status.repr);\n }\n\n // Handle 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 wuffs_base__result_f64 mpb_result =\n wuffs_base__private_implementation__medium_prec_bin__parse_number_f64(\n &m, &h);\n if (mpb_result.status.repr == NULL) {\n return mpb_result;\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,\n 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,\n " + + " 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_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__rounde" + + "d_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 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\n" + "" + "// ---------------- Hexadecimal\n\nsize_t //\nwuffs_base__hexadecimal__decode2(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t src_len2 = src.len / 2;\n size_t len = dst.len < src_len2 ? dst.len : src_len2;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n while (n--) {\n *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[0]] << 4) |\n (wuffs_base__parse_number__hexadecimal_digits[s[1]] & 0x0F));\n d += 1;\n s += 2;\n }\n\n return len;\n}\n\nsize_t //\nwuffs_base__hexadecimal__decode4(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t src_len4 = src.len / 4;\n size_t len = dst.len < src_len4 ? dst.len : src_len4;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n while (n--) {\n *d = (uint8_t)((wuffs_base__parse_number__hexadecimal_digits[s[2]] << 4) |\n (wuffs_base__parse_number__hexadecimal_digits[s[3]] & 0x0F));\n d += 1;\n s +" + "= 4;\n }\n\n return len;\n}\n\n" + @@ -167,7 +186,11 @@ const baseCorePublicH = "" + "\nstatic inline uint32_t //\nwuffs_base__u32__max(uint32_t x, uint32_t y) {\n return x > y ? x : y;\n}\n\nstatic inline uint64_t //\nwuffs_base__u64__min(uint64_t x, uint64_t y) {\n return x < y ? x : y;\n}\n\nstatic inline uint64_t //\nwuffs_base__u64__max(uint64_t x, uint64_t y) {\n return x > y ? x : y;\n}\n\n" + "" + "// --------\n\n// Saturating arithmetic (sat_add, sat_sub) branchless bit-twiddling algorithms\n// are per https://locklessinc.com/articles/sat_arithmetic/\n//\n// It is important that the underlying types are unsigned integers, as signed\n// integer arithmetic overflow is undefined behavior in C.\n\nstatic inline uint8_t //\nwuffs_base__u8__sat_add(uint8_t x, uint8_t y) {\n uint8_t res = (uint8_t)(x + y);\n res |= (uint8_t)(-(res < x));\n return res;\n}\n\nstatic inline uint8_t //\nwuffs_base__u8__sat_sub(uint8_t x, uint8_t y) {\n uint8_t res = (uint8_t)(x - y);\n res &= (uint8_t)(-(res <= x));\n return res;\n}\n\nstatic inline uint16_t //\nwuffs_base__u16__sat_add(uint16_t x, uint16_t y) {\n uint16_t res = (uint16_t)(x + y);\n res |= (uint16_t)(-(res < x));\n return res;\n}\n\nstatic inline uint16_t //\nwuffs_base__u16__sat_sub(uint16_t x, uint16_t y) {\n uint16_t res = (uint16_t)(x - y);\n res &= (uint16_t)(-(res <= x));\n return res;\n}\n\nstatic inline uint32_t //\nwuffs_base__u32__sat_add(uint32_t x, uint32_t y) {\n uint32" + - "_t res = (uint32_t)(x + y);\n res |= (uint32_t)(-(res < x));\n return res;\n}\n\nstatic inline uint32_t //\nwuffs_base__u32__sat_sub(uint32_t x, uint32_t y) {\n uint32_t res = (uint32_t)(x - y);\n res &= (uint32_t)(-(res <= x));\n return res;\n}\n\nstatic inline uint64_t //\nwuffs_base__u64__sat_add(uint64_t x, uint64_t y) {\n uint64_t res = (uint64_t)(x + y);\n res |= (uint64_t)(-(res < x));\n return res;\n}\n\nstatic inline uint64_t //\nwuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {\n uint64_t res = (uint64_t)(x - y);\n res &= (uint64_t)(-(res <= x));\n return res;\n}\n\n " + + "_t res = (uint32_t)(x + y);\n res |= (uint32_t)(-(res < x));\n return res;\n}\n\nstatic inline uint32_t //\nwuffs_base__u32__sat_sub(uint32_t x, uint32_t y) {\n uint32_t res = (uint32_t)(x - y);\n res &= (uint32_t)(-(res <= x));\n return res;\n}\n\nstatic inline uint64_t //\nwuffs_base__u64__sat_add(uint64_t x, uint64_t y) {\n uint64_t res = (uint64_t)(x + y);\n res |= (uint64_t)(-(res < x));\n return res;\n}\n\nstatic inline uint64_t //\nwuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {\n uint64_t res = (uint64_t)(x - y);\n res &= (uint64_t)(-(res <= x));\n return res;\n}\n\n" + + "" + + "// --------\n\ntypedef struct {\n uint64_t hi;\n uint64_t lo;\n} wuffs_base__multiply_u64__output;\n\n// wuffs_base__multiply_u64 returns x*y as a 128-bit value.\n//\n// The maximum inclusive output hi_lo is 0xFFFFFFFFFFFFFFFE_0000000000000001.\nstatic inline wuffs_base__multiply_u64__output //\nwuffs_base__multiply_u64(uint64_t x, uint64_t y) {\n uint64_t x0 = x & 0xFFFFFFFF;\n uint64_t x1 = x >> 32;\n uint64_t y0 = y & 0xFFFFFFFF;\n uint64_t y1 = y >> 32;\n uint64_t w0 = x0 * y0;\n uint64_t t = (x1 * y0) + (w0 >> 32);\n uint64_t w1 = t & 0xFFFFFFFF;\n uint64_t w2 = t >> 32;\n w1 += x0 * y1;\n wuffs_base__multiply_u64__output o;\n o.hi = (x1 * y1) + w2 + (w1 >> 32);\n o.lo = x * y;\n return o;\n}\n\n " + + "" + + "// --------\n\n#if defined(__GNUC__) && (__SIZEOF_LONG__ == 8)\n\nstatic inline uint32_t //\nwuffs_base__count_leading_zeroes_u64(uint64_t u) {\n return u ? ((uint32_t)(__builtin_clzl(u))) : 64u;\n}\n\n#else\n\nstatic inline uint32_t //\nwuffs_base__count_leading_zeroes_u64(uint64_t u) {\n if (u == 0) {\n return 64;\n }\n\n uint32_t n = 0;\n if ((u >> 32) == 0) {\n n |= 32;\n u <<= 32;\n }\n if ((u >> 48) == 0) {\n n |= 16;\n u <<= 16;\n }\n if ((u >> 56) == 0) {\n n |= 8;\n u <<= 8;\n }\n if ((u >> 60) == 0) {\n n |= 4;\n u <<= 4;\n }\n if ((u >> 62) == 0) {\n n |= 2;\n u <<= 2;\n }\n if ((u >> 63) == 0) {\n n |= 1;\n u <<= 1;\n }\n return n;\n}\n\n#endif // defined(__GNUC__) && (__SIZEOF_LONG__ == 8)\n\n " + "" + "// --------\n\n#define wuffs_base__load_u8be__no_bounds_check \\\n wuffs_base__load_u8__no_bounds_check\n#define wuffs_base__load_u8le__no_bounds_check \\\n wuffs_base__load_u8__no_bounds_check\n\nstatic inline uint8_t //\nwuffs_base__load_u8__no_bounds_check(uint8_t* p) {\n return p[0];\n}\n\nstatic inline uint16_t //\nwuffs_base__load_u16be__no_bounds_check(uint8_t* p) {\n return (uint16_t)(((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0));\n}\n\nstatic inline uint16_t //\nwuffs_base__load_u16le__no_bounds_check(uint8_t* p) {\n return (uint16_t)(((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8));\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u24be__no_bounds_check(uint8_t* p) {\n return ((uint32_t)(p[0]) << 16) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 0);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u24le__no_bounds_check(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u32be__no_bounds_check(" + "uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u32le__no_bounds_check(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u40be__no_bounds_check(uint8_t* p) {\n return ((uint64_t)(p[0]) << 32) | ((uint64_t)(p[1]) << 24) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 8) |\n ((uint64_t)(p[4]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u40le__no_bounds_check(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u48be__no_bounds_check(uint8_t* p) {\n return ((uint64_t)(p[0]) << 40) | ((uint64_t)(p[1]) << 32) |\n ((uint64_t)(p[2]) << 24) | ((uint64_t)" + diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index ca1e98e4..8fca4935 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -433,6 +433,80 @@ wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) { return res; } +// -------- + +typedef struct { + uint64_t hi; + uint64_t lo; +} wuffs_base__multiply_u64__output; + +// wuffs_base__multiply_u64 returns x*y as a 128-bit value. +// +// The maximum inclusive output hi_lo is 0xFFFFFFFFFFFFFFFE_0000000000000001. +static inline wuffs_base__multiply_u64__output // +wuffs_base__multiply_u64(uint64_t x, uint64_t y) { + uint64_t x0 = x & 0xFFFFFFFF; + uint64_t x1 = x >> 32; + uint64_t y0 = y & 0xFFFFFFFF; + uint64_t y1 = y >> 32; + uint64_t w0 = x0 * y0; + uint64_t t = (x1 * y0) + (w0 >> 32); + uint64_t w1 = t & 0xFFFFFFFF; + uint64_t w2 = t >> 32; + w1 += x0 * y1; + wuffs_base__multiply_u64__output o; + o.hi = (x1 * y1) + w2 + (w1 >> 32); + o.lo = x * y; + return o; +} + + // -------- + +#if defined(__GNUC__) && (__SIZEOF_LONG__ == 8) + +static inline uint32_t // +wuffs_base__count_leading_zeroes_u64(uint64_t u) { + return u ? ((uint32_t)(__builtin_clzl(u))) : 64u; +} + +#else + +static inline uint32_t // +wuffs_base__count_leading_zeroes_u64(uint64_t u) { + if (u == 0) { + return 64; + } + + uint32_t n = 0; + if ((u >> 32) == 0) { + n |= 32; + u <<= 32; + } + if ((u >> 48) == 0) { + n |= 16; + u <<= 16; + } + if ((u >> 56) == 0) { + n |= 8; + u <<= 8; + } + if ((u >> 60) == 0) { + n |= 4; + u <<= 4; + } + if ((u >> 62) == 0) { + n |= 2; + u <<= 2; + } + if ((u >> 63) == 0) { + n |= 1; + u <<= 1; + } + return n; +} + +#endif // defined(__GNUC__) && (__SIZEOF_LONG__ == 8) + // -------- #define wuffs_base__load_u8be__no_bounds_check \ @@ -8945,6 +9019,9 @@ fail_out_of_bounds: // fixed precision floating point decimal number, augmented with ±infinity // values, but it cannot represent NaN (Not a Number). // +// "High precision" means that the mantissa holds 500 decimal digits. 500 is +// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION. +// // An HPD isn't for general purpose arithmetic, only for conversions to and // from IEEE 754 double-precision floating point, where the largest and // smallest positive, finite values are approximately 1.8e+308 and 4.9e-324. @@ -8964,7 +9041,7 @@ fail_out_of_bounds: // For example, if num_digits is 3 and digits is "\x07\x08\x09": // - A decimal_point of -2 means ".00789" // - A decimal_point of -1 means ".0789" -// - A decimal_point of -0 means ".789" +// - A decimal_point of +0 means ".789" // - A decimal_point of +1 means "7.89" // - A decimal_point of +2 means "78.9" // - A decimal_point of +3 means "789." @@ -9494,6 +9571,371 @@ wuffs_base__private_implementation__high_prec_dec__small_rshift( // -------- +// The wuffs_base__private_implementation__etc_powers_of_10 tables were printed +// by script/print-mpb-powers-of-10.go. That script has an optional -comments +// flag, whose output is not copied here, which prints further detail. +// +// These tables are used in +// wuffs_base__private_implementation__medium_prec_bin__assign_from_hpd. + +// wuffs_base__private_implementation__big_powers_of_10 contains approximations +// to the powers of 10, ranging from 1e-348 to 1e+340, with the exponent +// stepping by 8: -348, -340, -332, ..., -12, -4, +4, +12, ..., +340. Each step +// consists of three uint32_t elements. There are 87 triples, 87 * 3 = 261. +// +// For example, the third approximation, for 1e-332, consists of the uint32_t +// triple (0x3055AC76, 0x8B16FB20, 0xFFFFFB72). The first two of that triple +// are a little-endian uint64_t value: 0x8B16FB203055AC76. The last one is an +// int32_t value: -1166. Together, they represent the approximation: +// 1e-332 ≈ 0x8B16FB203055AC76 * (2 ** -1166) +// Similarly, the (0x00000000, 0x9C400000, 0xFFFFFFCE) uint32_t triple means: +// 1e+4 ≈ 0x9C40000000000000 * (2 ** -50) // This approx'n is exact. +// Similarly, the (0xD4C4FB27, 0xED63A231, 0x000000A2) uint32_t triple means: +// 1e+68 ≈ 0xED63A231D4C4FB27 * (2 ** 162) +static const uint32_t + wuffs_base__private_implementation__big_powers_of_10[261] = { + 0x081C0288, 0xFA8FD5A0, 0xFFFFFB3C, 0xA23EBF76, 0xBAAEE17F, 0xFFFFFB57, + 0x3055AC76, 0x8B16FB20, 0xFFFFFB72, 0x5DCE35EA, 0xCF42894A, 0xFFFFFB8C, + 0x55653B2D, 0x9A6BB0AA, 0xFFFFFBA7, 0x3D1A45DF, 0xE61ACF03, 0xFFFFFBC1, + 0xC79AC6CA, 0xAB70FE17, 0xFFFFFBDC, 0xBEBCDC4F, 0xFF77B1FC, 0xFFFFFBF6, + 0x416BD60C, 0xBE5691EF, 0xFFFFFC11, 0x907FFC3C, 0x8DD01FAD, 0xFFFFFC2C, + 0x31559A83, 0xD3515C28, 0xFFFFFC46, 0xADA6C9B5, 0x9D71AC8F, 0xFFFFFC61, + 0x23EE8BCB, 0xEA9C2277, 0xFFFFFC7B, 0x4078536D, 0xAECC4991, 0xFFFFFC96, + 0x5DB6CE57, 0x823C1279, 0xFFFFFCB1, 0x4DFB5637, 0xC2109436, 0xFFFFFCCB, + 0x3848984F, 0x9096EA6F, 0xFFFFFCE6, 0x25823AC7, 0xD77485CB, 0xFFFFFD00, + 0x97BF97F4, 0xA086CFCD, 0xFFFFFD1B, 0x172AACE5, 0xEF340A98, 0xFFFFFD35, + 0x2A35B28E, 0xB23867FB, 0xFFFFFD50, 0xD2C63F3B, 0x84C8D4DF, 0xFFFFFD6B, + 0x1AD3CDBA, 0xC5DD4427, 0xFFFFFD85, 0xBB25C996, 0x936B9FCE, 0xFFFFFDA0, + 0x7D62A584, 0xDBAC6C24, 0xFFFFFDBA, 0x0D5FDAF6, 0xA3AB6658, 0xFFFFFDD5, + 0xDEC3F126, 0xF3E2F893, 0xFFFFFDEF, 0xAAFF80B8, 0xB5B5ADA8, 0xFFFFFE0A, + 0x6C7C4A8B, 0x87625F05, 0xFFFFFE25, 0x34C13053, 0xC9BCFF60, 0xFFFFFE3F, + 0x91BA2655, 0x964E858C, 0xFFFFFE5A, 0x70297EBD, 0xDFF97724, 0xFFFFFE74, + 0xB8E5B88F, 0xA6DFBD9F, 0xFFFFFE8F, 0x88747D94, 0xF8A95FCF, 0xFFFFFEA9, + 0x8FA89BCF, 0xB9447093, 0xFFFFFEC4, 0xBF0F156B, 0x8A08F0F8, 0xFFFFFEDF, + 0x653131B6, 0xCDB02555, 0xFFFFFEF9, 0xD07B7FAC, 0x993FE2C6, 0xFFFFFF14, + 0x2A2B3B06, 0xE45C10C4, 0xFFFFFF2E, 0x697392D3, 0xAA242499, 0xFFFFFF49, + 0x8300CA0E, 0xFD87B5F2, 0xFFFFFF63, 0x92111AEB, 0xBCE50864, 0xFFFFFF7E, + 0x6F5088CC, 0x8CBCCC09, 0xFFFFFF99, 0xE219652C, 0xD1B71758, 0xFFFFFFB3, + 0x00000000, 0x9C400000, 0xFFFFFFCE, 0x00000000, 0xE8D4A510, 0xFFFFFFE8, + 0xAC620000, 0xAD78EBC5, 0x00000003, 0xF8940984, 0x813F3978, 0x0000001E, + 0xC90715B3, 0xC097CE7B, 0x00000038, 0x7BEA5C70, 0x8F7E32CE, 0x00000053, + 0xABE98068, 0xD5D238A4, 0x0000006D, 0x179A2245, 0x9F4F2726, 0x00000088, + 0xD4C4FB27, 0xED63A231, 0x000000A2, 0x8CC8ADA8, 0xB0DE6538, 0x000000BD, + 0x1AAB65DB, 0x83C7088E, 0x000000D8, 0x42711D9A, 0xC45D1DF9, 0x000000F2, + 0xA61BE758, 0x924D692C, 0x0000010D, 0x1A708DEA, 0xDA01EE64, 0x00000127, + 0x9AEF774A, 0xA26DA399, 0x00000142, 0xB47D6B85, 0xF209787B, 0x0000015C, + 0x79DD1877, 0xB454E4A1, 0x00000177, 0x5B9BC5C2, 0x865B8692, 0x00000192, + 0xC8965D3D, 0xC83553C5, 0x000001AC, 0xFA97A0B3, 0x952AB45C, 0x000001C7, + 0x99A05FE3, 0xDE469FBD, 0x000001E1, 0xDB398C25, 0xA59BC234, 0x000001FC, + 0xA3989F5C, 0xF6C69A72, 0x00000216, 0x54E9BECE, 0xB7DCBF53, 0x00000231, + 0xF22241E2, 0x88FCF317, 0x0000024C, 0xD35C78A5, 0xCC20CE9B, 0x00000266, + 0x7B2153DF, 0x98165AF3, 0x00000281, 0x971F303A, 0xE2A0B5DC, 0x0000029B, + 0x5CE3B396, 0xA8D9D153, 0x000002B6, 0xA4A7443C, 0xFB9B7CD9, 0x000002D0, + 0xA7A44410, 0xBB764C4C, 0x000002EB, 0xB6409C1A, 0x8BAB8EEF, 0x00000306, + 0xA657842C, 0xD01FEF10, 0x00000320, 0xE9913129, 0x9B10A4E5, 0x0000033B, + 0xA19C0C9D, 0xE7109BFB, 0x00000355, 0x623BF429, 0xAC2820D9, 0x00000370, + 0x7AA7CF85, 0x80444B5E, 0x0000038B, 0x03ACDD2D, 0xBF21E440, 0x000003A5, + 0x5E44FF8F, 0x8E679C2F, 0x000003C0, 0x9C8CB841, 0xD433179D, 0x000003DA, + 0xB4E31BA9, 0x9E19DB92, 0x000003F5, 0xBADF77D9, 0xEB96BF6E, 0x0000040F, + 0x9BF0EE6B, 0xAF87023B, 0x0000042A, +}; + +// wuffs_base__private_implementation__small_powers_of_10 contains +// approximations to the powers of 10, ranging from 1e+0 to 1e+7, with the +// exponent stepping by 1. Each step consists of three uint32_t elements. +// +// For example, the third approximation, for 1e+2, consists of the uint32_t +// triple (0x00000000, 0xC8000000, 0xFFFFFFC7). The first two of that triple +// are a little-endian uint64_t value: 0xC800000000000000. The last one is an +// int32_t value: -57. Together, they represent the approximation: +// 1e+2 ≈ 0xC800000000000000 * (2 ** -57) // This approx'n is exact. +// Similarly, the (0x00000000, 0x9C400000, 0xFFFFFFCE) uint32_t triple means: +// 1e+4 ≈ 0x9C40000000000000 * (2 ** -50) // This approx'n is exact. +static const uint32_t + wuffs_base__private_implementation__small_powers_of_10[24] = { + 0x00000000, 0x80000000, 0xFFFFFFC1, 0x00000000, 0xA0000000, 0xFFFFFFC4, + 0x00000000, 0xC8000000, 0xFFFFFFC7, 0x00000000, 0xFA000000, 0xFFFFFFCA, + 0x00000000, 0x9C400000, 0xFFFFFFCE, 0x00000000, 0xC3500000, 0xFFFFFFD1, + 0x00000000, 0xF4240000, 0xFFFFFFD4, 0x00000000, 0x98968000, 0xFFFFFFD8, +}; + +// -------- + +// wuffs_base__private_implementation__medium_prec_bin (abbreviated as MPB) is +// a fixed precision floating point binary number. Unlike IEEE 754 Floating +// Point, it cannot represent infinity or NaN (Not a Number). +// +// "Medium precision" means that the mantissa holds 64 binary digits, a little +// more than "double precision", and sizeof(MPB) > sizeof(double). 64 is +// obviously the number of bits in a uint64_t. +// +// An MPB isn't for general purpose arithmetic, only for conversions to and +// from IEEE 754 double-precision floating point. +// +// There is no implicit mantissa bit. The mantissa field is zero if and only if +// the overall floating point value is ±0. An MPB is normalized if the mantissa +// is zero or its high bit (the 1<<63 bit) is set. +// +// There is no negative bit. An MPB can only represent non-negative numbers. +// +// The "all fields are zero" value is valid, and represents the number +0. +// +// This is the "Do It Yourself Floating Point" data structure from Loitsch, +// "Printing Floating-Point Numbers Quickly and Accurately with Integers" +// (https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf). +// +// Florian Loitsch is also the primary contributor to +// https://github.com/google/double-conversion +typedef struct { + uint64_t mantissa; + int32_t exp2; +} wuffs_base__private_implementation__medium_prec_bin; + +static uint32_t // +wuffs_base__private_implementation__medium_prec_bin__normalize( + wuffs_base__private_implementation__medium_prec_bin* m) { + if (m->mantissa == 0) { + return 0; + } + uint32_t shift = wuffs_base__count_leading_zeroes_u64(m->mantissa); + m->mantissa <<= shift; + m->exp2 -= (int32_t)shift; + return shift; +} + +// wuffs_base__private_implementation__medium_prec_bin__mul_pow_10 sets m to be +// (m * pow), where pow comes from an etc_powers_of_10 triple starting at p. +// +// The result is rounded, but not necessarily normalized. +// +// Preconditions: +// - m is non-NULL. +// - m->mantissa is non-zero. +// - m->mantissa's high bit is set (i.e. m is normalized). +// +// The etc_powers_of_10 triple is already normalized. +static void // +wuffs_base__private_implementation__medium_prec_bin__mul_pow_10( + wuffs_base__private_implementation__medium_prec_bin* m, + const uint32_t* p) { + uint64_t p_mantissa = ((uint64_t)p[0]) | (((uint64_t)p[1]) << 32); + int32_t p_exp2 = (int32_t)p[2]; + + wuffs_base__multiply_u64__output o = + wuffs_base__multiply_u64(m->mantissa, p_mantissa); + // Round the mantissa up. It cannot overflow because the maximum possible + // value of o.hi is 0xFFFFFFFFFFFFFFFE. + m->mantissa = o.hi + (o.lo >> 63); + m->exp2 = m->exp2 + p_exp2 + 64; +} + +// wuffs_base__private_implementation__medium_prec_bin__as_f64 converts m to a +// double (what C calls a double-precision float64). +// +// Preconditions: +// - m is non-NULL. +// - m->mantissa is non-zero. +// - m->mantissa's high bit is set (i.e. m is normalized). +static double // +wuffs_base__private_implementation__medium_prec_bin__as_f64( + const wuffs_base__private_implementation__medium_prec_bin* m, + bool negative) { + uint64_t mantissa64 = m->mantissa; + // An mpb's mantissa has the implicit (binary) decimal point at the right + // hand end of the mantissa's explicit digits. A double-precision's mantissa + // has that decimal point near the left hand end. There's also an explicit + // versus implicit leading 1 bit (binary digit). Together, the difference in + // semantics corresponds to adding 63. + int32_t exp2 = m->exp2 + 63; + + // Ensure that exp2 is at least -1022, the minimum double-precision exponent + // for normal (as opposed to subnormal) numbers. + if (-1022 > exp2) { + uint32_t n = (uint32_t)(-1022 - exp2); + mantissa64 >>= n; + exp2 += (int32_t)n; + } + + // Extract the (1 + 52) bits from the 64-bit mantissa64. 52 is the number of + // explicit mantissa bits in a double-precision f64. + // + // Before, we have 64 bits and due to normalization, the high bit 'H' is 1. + // 63 55 47 etc 15 7 + // H210_9876_5432_1098_7654_etc_etc_etc_5432_1098_7654_3210 + // ++++_++++_++++_++++_++++_etc_etc_etc_++++_+..._...._.... Kept bits. + // ...._...._...H_2109_8765_etc_etc_etc_6543_2109_8765_4321 After shifting. + // After, we have 53 bits (and bit #52 is this 'H' bit). + uint64_t mantissa53 = mantissa64 >> 11; + + // Round up if the old bit #10 (the highest bit dropped by shifting) was set. + // We also fix any overflow from rounding up. + if (mantissa64 & 1024) { + mantissa53++; + if ((mantissa53 >> 53) != 0) { + mantissa53 >>= 1; + exp2++; + } + } + + // Handle double-precision infinity (a nominal exponent of 1024) and + // subnormals (an exponent of -1023 and no implicit mantissa bit, bit #52). + if (exp2 >= 1024) { + mantissa53 = 0; + exp2 = 1024; + } else if ((mantissa53 >> 52) == 0) { + exp2 = -1023; + } + + // Pack the bits and return. + const int32_t f64_bias = -1023; + uint64_t exp2_bits = + (uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 << 11) - 1. + uint64_t bits = (mantissa53 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1. + (exp2_bits << 52) | // + (negative ? 0x8000000000000000 : 0); // (1 << 63). + return wuffs_base__ieee_754_bit_representation__to_f64(bits); +} + +// wuffs_base__private_implementation__medium_prec_bin__parse_number_f64 +// converts from an HPD to a double, using an MPB as scratch space. It returns +// a NULL status.repr if there is no ambiguity in the truncation or rounding to +// a float64 (an IEEE 754 double-precision floating point value). +// +// It may modify m even if it returns a non-NULL status.repr. +static wuffs_base__result_f64 // +wuffs_base__private_implementation__medium_prec_bin__parse_number_f64( + wuffs_base__private_implementation__medium_prec_bin* m, + const wuffs_base__private_implementation__high_prec_dec* h) { + // m->mantissa is a uint64_t, which is an integer approximation to a rational + // value - h's underlying digits after m's normalization. This error is an + // upper bound on the difference between the approximate and actual value. + // + // The DiyFpStrtod function in https://github.com/google/double-conversion + // uses a finer grain (1/8th of the ULP, Unit in the Last Place) when + // tracking error. This implementation is coarser (1 ULP) but simpler. + // + // It is an error in the "numerical approximation" sense, not in the typical + // programming sense (as in "bad input" or "a result type"). + uint64_t error = 0; + + // Convert up to 19 decimal digits (in h->digits) to 64 binary digits (in + // m->mantissa): (1e19 < (1<<64)) and ((1<<64) < 1e20). If we have more than + // 19 digits, we're truncating (with error). + uint32_t i; + uint32_t i_end = h->num_digits; + if (i_end > 19) { + i_end = 19; + error = 1; + } + uint64_t mantissa = 0; + for (i = 0; i < i_end; i++) { + mantissa = (10 * mantissa) + h->digits[i]; + } + m->mantissa = mantissa; + m->exp2 = 0; + + // Check that exp10 lies in the (big_powers_of_10 + small_powers_of_10) + // range, -348 ..= +347, stepping big_powers_of_10 by 8 (which is 87 triples) + // and small_powers_of_10 by 1 (which is 8 triples). + int32_t exp10 = h->decimal_point - ((int32_t)(i_end)); + if (exp10 < -348) { + goto fail; + } + uint32_t bpo10 = ((uint32_t)(exp10 + 348)) / 8; + uint32_t spo10 = ((uint32_t)(exp10 + 348)) % 8; + if (bpo10 >= 87) { + goto fail; + } + + // Normalize (and scale the error). + error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m); + + // Multiplying two MPB values nominally multiplies two mantissas, call them A + // and B, which are integer approximations to the precise values (A+a) and + // (B+b) for some error terms a and b. + // + // MPB multiplication calculates (((A+a) * (B+b)) >> 64) to be ((A*B) >> 64). + // Shifting (truncating) and rounding introduces further error. The + // difference between the calculated result: + // ((A*B ) >> 64) + // and the true result: + // ((A*B + A*b + a*B + a*b) >> 64) + rounding_error + // is: + // (( A*b + a*B + a*b) >> 64) + rounding_error + // which can be re-grouped as: + // ((A*b) >> 64) + ((a*(B+b)) >> 64) + rounding_error + // + // Now, let A and a be "m->mantissa" and "error", and B and b be the + // pre-calculated power of 10. A and B are both less than (1 << 64), a is the + // "error" local variable and b is less than 1. + // + // An upper bound (in absolute value) on ((A*b) >> 64) is therefore 1. + // + // Similarly, an upper bound on ((a*(B+b)) >> 64) is a, also known as error. + // + // Finally, the rounding_error is at most 1. + // + // In total, calling mpb__mul_pow_10 will increase the worst-case error by 2. + // The subsequent re-normalization can multiply that by a further factor. + + // Multiply by small_powers_of_10[etc]. + wuffs_base__private_implementation__medium_prec_bin__mul_pow_10( + m, &wuffs_base__private_implementation__small_powers_of_10[3 * spo10]); + error += 2; + error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m); + + // Multiply by big_powers_of_10[etc]. + wuffs_base__private_implementation__medium_prec_bin__mul_pow_10( + m, &wuffs_base__private_implementation__big_powers_of_10[3 * bpo10]); + error += 2; + error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m); + + // We have a good approximation of h, but we still have to check whether the + // error is small enough. Equivalently, whether the number of surplus + // mantissa bits (the bits dropped when going from m's 64 mantissa bits to + // the smaller number of double-precision mantissa bits) would always round + // up or down, even when perturbed by ±error. We start at 11 surplus bits (m + // has 64, double-precision has 1+52), but it can be higher for subnormals. + // + // In many cases, the error is small enough and we return true. + const int32_t f64_bias = -1023; + int32_t subnormal_exp2 = f64_bias - 63; + uint32_t surplus_bits = 11; + if (subnormal_exp2 >= m->exp2) { + surplus_bits += 1 + ((uint32_t)(subnormal_exp2 - m->exp2)); + } + + uint64_t surplus_mask = (((uint64_t)1) << surplus_bits) - 1; // e.g. 0x07FF. + uint64_t surplus = m->mantissa & surplus_mask; + uint64_t halfway = ((uint64_t)1) << (surplus_bits - 1); // e.g. 0x0400. + + // Do the final calculation in *signed* arithmetic. + int64_t i_surplus = (int64_t)surplus; + int64_t i_halfway = (int64_t)halfway; + int64_t i_error = (int64_t)error; + + if ((i_surplus > (i_halfway - i_error)) && + (i_surplus < (i_halfway + i_error))) { + goto fail; + } + + wuffs_base__result_f64 ret; + ret.status.repr = NULL; + ret.value = wuffs_base__private_implementation__medium_prec_bin__as_f64( + m, h->negative); + return ret; + +fail: + do { + wuffs_base__result_f64 ret; + ret.status.repr = "#base: mpb__parse_number_f64 failed"; + ret.value = 0; + return ret; + } while (0); +} + +// -------- + wuffs_base__result_f64 // wuffs_base__parse_number_f64_special(wuffs_base__slice_u8 s, const char* fallback_status_repr) { @@ -9597,6 +10039,7 @@ fallback: wuffs_base__result_f64 // wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) { + wuffs_base__private_implementation__medium_prec_bin m; wuffs_base__private_implementation__high_prec_dec h; do { @@ -9623,10 +10066,17 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) { goto infinity; } + wuffs_base__result_f64 mpb_result = + wuffs_base__private_implementation__medium_prec_bin__parse_number_f64( + &m, &h); + if (mpb_result.status.repr == NULL) { + return mpb_result; + } + // Scale by powers of 2 until we're in the range [½ .. 1], which gives us // our exponent (in base-2). First we shift right, possibly a little too // far, ending with a value certainly below 1 and possibly below ½... - const int32_t bias = -1023; + const int32_t f64_bias = -1023; int32_t exp2 = 0; while (h.decimal_point > 0) { uint32_t n = (uint32_t)(+h.decimal_point); @@ -9670,9 +10120,9 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) { // We're in the range [½ .. 1] but f64 uses [1 .. 2]. exp2--; - // The minimum normal exponent is (bias + 1). - while ((bias + 1) > exp2) { - uint32_t n = (uint32_t)((bias + 1) - exp2); + // The minimum normal exponent is (f64_bias + 1). + while ((f64_bias + 1) > exp2) { + uint32_t n = (uint32_t)((f64_bias + 1) - exp2); if (n > WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) { n = WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL; } @@ -9681,7 +10131,7 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) { } // Check for overflow. - if ((exp2 - bias) >= 0x07FF) { // (1 << 11) - 1. + if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1. goto infinity; } @@ -9694,21 +10144,22 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) { if ((man2 >> 53) != 0) { man2 >>= 1; exp2++; - if ((exp2 - bias) >= 0x07FF) { // (1 << 11) - 1. + if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1. goto infinity; } } // Handle subnormal numbers. if ((man2 >> 52) == 0) { - exp2 = bias; + exp2 = f64_bias; } // Pack the bits and return. - uint64_t exp2_bits = (uint64_t)((exp2 - bias) & 0x07FF); // (1 << 11) - 1. - uint64_t bits = (man2 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1. - (exp2_bits << 52) | // - (h.negative ? 0x8000000000000000 : 0); // (1 << 63). + uint64_t exp2_bits = + (uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 << 11) - 1. + uint64_t bits = (man2 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1. + (exp2_bits << 52) | // + (h.negative ? 0x8000000000000000 : 0); // (1 << 63). wuffs_base__result_f64 ret; ret.status.repr = NULL; diff --git a/script/print-hpd-left-shift.go b/script/print-hpd-left-shift.go index 47141bd4..09b175af 100644 --- a/script/print-hpd-left-shift.go +++ b/script/print-hpd-left-shift.go @@ -27,6 +27,7 @@ import ( "fmt" "math" "math/big" + "os" ) var ( @@ -48,6 +49,13 @@ func ellipsize(s string) string { } func main() { + if err := main1(); err != nil { + os.Stderr.WriteString(err.Error() + "\n") + os.Exit(1) + } +} + +func main1() error { flag.Parse() const WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL = 60 @@ -111,4 +119,5 @@ func main() { fmt.Printf(" // offset=0x%04X\n", len(data)&^15) } fmt.Printf("};\n") + return nil } diff --git a/script/print-lzw-example.go b/script/print-lzw-example.go index ab09d6a5..0d588eec 100644 --- a/script/print-lzw-example.go +++ b/script/print-lzw-example.go @@ -33,6 +33,7 @@ package main import ( "fmt" + "os" ) const ( @@ -74,6 +75,13 @@ func codeString(code int) string { } func main() { + if err := main1(); err != nil { + os.Stderr.WriteString(err.Error() + "\n") + os.Exit(1) + } +} + +func main1() error { n, s, q := new(naive), new(suf1), new(sufQ) decode(n) decode(s) @@ -109,6 +117,7 @@ func main() { fmt.Println() key++ } + return nil } type implementation interface { diff --git a/script/print-mpb-powers-of-10.go b/script/print-mpb-powers-of-10.go new file mode 100644 index 00000000..6a55fe55 --- /dev/null +++ b/script/print-mpb-powers-of-10.go @@ -0,0 +1,113 @@ +// Copyright 2020 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build ignore + +package main + +// print-mpb-powers-of-10.go prints the +// wuffs_base__private_implementation__medium_prec_bin__powers_of_10 tables. +// +// Usage: go run print-mpb-powers-of-10.go -comments + +import ( + "flag" + "fmt" + "math/big" + "os" +) + +var ( + comments = flag.Bool("comments", false, "whether to print comments") +) + +func main() { + if err := main1(); err != nil { + os.Stderr.WriteString(err.Error() + "\n") + os.Exit(1) + } +} + +func main1() error { + flag.Parse() + + const bigCount = 1 + ((+340 - -348) / 8) + fmt.Printf("static const uint32_t "+ + "wuffs_base__private_implementation__big_powers_of_10[%d] = {\n", 3*bigCount) + for e := -348; e <= +340; e += 8 { + if err := do(e); err != nil { + return err + } + } + fmt.Printf("};\n\n") + + fmt.Printf("static const uint32_t " + + "wuffs_base__private_implementation__small_powers_of_10[24] = {\n") + for e := 0; e <= 7; e += 1 { + if err := do(e); err != nil { + return err + } + } + fmt.Printf("};\n") + + return nil +} + +var ( + one = big.NewInt(1) + ten = big.NewInt(10) + two64 = big.NewInt(0).Lsh(one, 64) +) + +// N is large enough so that (1<= 0 { + exp := big.NewInt(0).Exp(ten, big.NewInt(int64(+e)), nil) + z.Mul(z, exp) + } else { + exp := big.NewInt(0).Exp(ten, big.NewInt(int64(-e)), nil) + z.Div(z, exp) + } + + roundUp := false + n := int32(-N) + for z.Cmp(two64) >= 0 { + roundUp = z.Bit(0) > 0 + z.Rsh(z, 1) + n++ + } + if roundUp { + z.Add(z, one) + } + hex := fmt.Sprintf("%X", z) + if len(hex) != 16 { + return fmt.Errorf("invalid hexadecimal representation %q", hex) + } + + fmt.Printf(" 0x%s, 0x%s, 0x%08X,", hex[8:], hex[:8], uint32(n)) + if *comments { + fmt.Printf(" // 1e%-04d ≈ (0x%s ", e, hex) + if n >= 0 { + fmt.Printf("<< %4d)", +n) + } else { + fmt.Printf(">> %4d)", -n) + } + } + + fmt.Println() + return nil +} diff --git a/test/c/std/json.c b/test/c/std/json.c index be18efb1..f36ec7af 100644 --- a/test/c/std/json.c +++ b/test/c/std/json.c @@ -67,6 +67,77 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the // No mimic library. #endif +// ---------------- Numeric Types Tests + +const char* // +test_wuffs_core_count_leading_zeroes_u64() { + CHECK_FOCUS(__func__); + + struct { + uint64_t num; + uint32_t want; + } test_cases[] = { + {.num = 0x0000000000000000, .want = 64}, + {.num = 0x0000000000000001, .want = 63}, + {.num = 0x0000000000008001, .want = 48}, + {.num = 0x0000000040302010, .want = 33}, + {.num = 0x0123456789ABCDEF, .want = 7}, + {.num = 0x8000000000000001, .want = 0}, + {.num = 0xFFFFFFFFFFFFFFFF, .want = 0}, + }; + + int tc; + for (tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) { + uint32_t have = wuffs_base__count_leading_zeroes_u64(test_cases[tc].num); + if (have != test_cases[tc].want) { + RETURN_FAIL("0x%" PRIX64 ": have %" PRIu32 ", want %" PRIu32, + test_cases[tc].num, have, test_cases[tc].want); + } + } + + return NULL; +} + +const char* // +test_wuffs_core_multiply_u64() { + CHECK_FOCUS(__func__); + + struct { + uint64_t x; + uint64_t y; + uint64_t want_hi; + uint64_t want_lo; + } test_cases[] = { + {.x = 0x0000000000005678, + .y = 0x0000000000001001, + .want_hi = 0x0000000000000000, + .want_lo = 0x000000000567D678}, + {.x = 0x00000000DEADBEEF, + .y = 0x000000BEEEEEEEEF, + .want_hi = 0x00000000000000A6, + .want_lo = 0x14C912411FE97321}, + {.x = 0x0123456789ABCDEF, + .y = 0x8080707066554321, + .want_hi = 0x009234D666DAD50F, + .want_lo = 0x89B3DE09506618CF}, + }; + + int tc; + for (tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) { + wuffs_base__multiply_u64__output have = + wuffs_base__multiply_u64(test_cases[tc].x, test_cases[tc].y); + if ((have.hi != test_cases[tc].want_hi) || + (have.lo != test_cases[tc].want_lo)) { + RETURN_FAIL("0x%" PRIX64 " * 0x%" PRIX64 ": have (0x%" PRIX64 + ", 0x%" PRIX64 "), want (0x%" PRIX64 ", 0x%" PRIX64 ")", + test_cases[tc].x, test_cases[tc].y, have.hi, have.lo, + test_cases[tc].want_hi, test_cases[tc].want_lo); + } + } + + return NULL; +} + // ---------------- String Conversions Tests // wuffs_base__private_implementation__high_prec_dec__to_debug_string converts @@ -348,6 +419,134 @@ test_strconv_hpd_shift() { return NULL; } +const char* // +test_strconv_mpb_assign_from_hpd() { + CHECK_FOCUS(__func__); + + struct { + const char* str; + int32_t decimal_point; + uint64_t want_mantissa; + int32_t want_exp2; + double want_f64; + } test_cases[] = { + + // (0x818995CE7AA0E1B2 * (2 ** -1136)) is roughly 1e-323 + // + // 1e-323 is roughly twice 4.94066e-324, the minimum subnormal positive + // double-precision floating point number. + {.str = "1", + .decimal_point = -322, + .want_mantissa = 0x818995CE7AA0E1B2, + .want_exp2 = -1136, + .want_f64 = 1e-323}, + + // (0xD1B71758E219652C * (2 ** -77)) is roughly .0001 + {.str = "1", + .decimal_point = -3, + .want_mantissa = 0xD1B71758E219652C, + .want_exp2 = -77, + .want_f64 = .0001}, + + // (0xCCCCCCCCCCCCCCCD * (2 ** -67)) is roughly .1 + {.str = "1", + .decimal_point = +0, + .want_mantissa = 0xCCCCCCCCCCCCCCCD, + .want_exp2 = -67, + .want_f64 = .1}, + + // (0x8000000000000000 * (2 ** -63)) is 1. + {.str = "1", + .decimal_point = +1, + .want_mantissa = 0x8000000000000000, + .want_exp2 = -63, + .want_f64 = 1}, + + // (0xA000000000000000 * (2 ** -60)) is 10. + {.str = "1", + .decimal_point = +2, + .want_mantissa = 0xA000000000000000, + .want_exp2 = -60, + .want_f64 = 10}, + + // (0xC9F2C9CD04674EDE * (2 ** +36)) is roughly 1e30. + {.str = "1", + .decimal_point = +31, + .want_mantissa = 0xC9F2C9CD04674EDE, + .want_exp2 = +36, + .want_f64 = 1e30}, + + // (0xDE81E40A034BCF50 * (2 ** +966)) is roughly 1e310. + // + // 1e310 is almost 50 times larger than DBL_MAX (roughly 1.8e308), so it + // should be converted to +infinity. + {.str = "1", + .decimal_point = +311, + .want_mantissa = 0xDE81E40A034BCF50, + .want_exp2 = +966, + .want_f64 = (1.0 / 0.0)}, + + // (0x9A40000000000000 * (2 ** -53)) is 1234. + {.str = "1234", + .decimal_point = +4, + .want_mantissa = 0x9A40000000000000, + .want_exp2 = -53, + .want_f64 = 1234}, + + // (0xC90FCF80DC33721E * (2 ** -62)) is roughly 3.14159 + {.str = "314159", + .decimal_point = +1, + .want_mantissa = 0xC90FCF80DC33721E, + .want_exp2 = -62, + .want_f64 = 3.14159}, + }; + + int tc; + for (tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) { + wuffs_base__private_implementation__high_prec_dec hpd; + + // Initialize hpd. + uint32_t i; + for (i = 0; test_cases[tc].str[i]; i++) { + hpd.digits[i] = test_cases[tc].str[i] - '0'; + } + hpd.num_digits = i; + hpd.decimal_point = test_cases[tc].decimal_point; + hpd.negative = false; + hpd.truncated = false; + + wuffs_base__private_implementation__medium_prec_bin mpb; + wuffs_base__private_implementation__medium_prec_bin__parse_number_f64(&mpb, + &hpd); + + uint64_t have_mantissa = mpb.mantissa; + if (have_mantissa != test_cases[tc].want_mantissa) { + RETURN_FAIL("%s@%d: mantissa: have 0x%" PRIX64 ", want 0x%" PRIX64, + test_cases[tc].str, test_cases[tc].decimal_point, + have_mantissa, test_cases[tc].want_mantissa); + } + + int32_t have_exp2 = mpb.exp2; + if (have_exp2 != test_cases[tc].want_exp2) { + RETURN_FAIL("%s@%d: exp2: have %" PRId32 ", want %" PRId32, + test_cases[tc].str, test_cases[tc].decimal_point, have_exp2, + test_cases[tc].want_exp2); + } + + double have_f64 = + wuffs_base__private_implementation__medium_prec_bin__as_f64(&mpb, + false); + if (have_f64 != test_cases[tc].want_f64) { + RETURN_FAIL("%s@%d: f64: have %g, want %g", test_cases[tc].str, + test_cases[tc].decimal_point, have_f64, + test_cases[tc].want_f64); + } + } + return NULL; +} + +// ---------------- + const char* // test_strconv_hexadecimal() { CHECK_FOCUS(__func__); @@ -2270,12 +2469,15 @@ bench_wuffs_json_decode_217k_stringy() { proc g_tests[] = { - // These strconv tests are really testing the Wuffs base library. They - // aren't specific to the std/json code, but putting them here is as good - // as any other place. + // These core and strconv tests are really testing the Wuffs base library. + // They aren't specific to the std/json code, but putting them here is as + // good as any other place. + test_wuffs_core_count_leading_zeroes_u64, + test_wuffs_core_multiply_u64, test_strconv_hexadecimal, test_strconv_hpd_rounded_integer, test_strconv_hpd_shift, + test_strconv_mpb_assign_from_hpd, test_strconv_parse_number_f64, test_strconv_parse_number_i64, test_strconv_parse_number_u64,