Add IEEE 754 from_u{16,32}_to_f64 functions

This commit is contained in:
Nigel Tao committed 2020-07-12 13:23:04 +10:00
1 parent 4d449dc663
commit 0899b82c42
4 files changed
+282 -1

No files matched your search

+37
View File
@@ -158,6 +158,43 @@ wuffs_base__ieee_754_bit_representation__from_f64_to_u64(double f) {
return u;
}
static inline double //
wuffs_base__ieee_754_bit_representation__from_u16_to_f64(uint16_t u) {
uint64_t v = ((uint64_t)(u & 0x8000)) << 48;
do {
uint64_t exp = (u >> 10) & 0x1F;
uint64_t man = u & 0x3FF;
if (exp == 0x1F) { // Infinity or NaN.
exp = 2047;
} else if (exp != 0) { // Normal.
exp += 1008; // 1008 = 1023 - 15, the difference in biases.
} else if (man != 0) { // Subnormal but non-zero.
uint32_t clz = wuffs_base__count_leading_zeroes_u64(man);
exp = 1062 - clz; // 1062 = 1008 + 64 - 10.
man = 0x3FF & (man << (clz - 53));
} else { // Zero.
break;
}
v |= (exp << 52) | (man << 42);
} while (0);
double f = 0;
if (sizeof(uint64_t) == sizeof(double)) {
memcpy(&f, &v, sizeof(uint64_t));
}
return f;
}
static inline double //
wuffs_base__ieee_754_bit_representation__from_u32_to_f64(uint32_t u) {
float f = 0;
if (sizeof(uint32_t) == sizeof(float)) {
memcpy(&f, &u, sizeof(uint32_t));
}
return (double)f;
}
static inline double //
wuffs_base__ieee_754_bit_representation__from_u64_to_f64(uint64_t u) {
double f = 0;
+2 -1
View File
@@ -520,7 +520,8 @@ const BaseStrConvPublicH = "" +
"u8 s, uint32_t options);\n\n" +
"" +
"// --------\n\n// wuffs_base__ieee_754_bit_representation__etc converts between a double\n// precision numerical value and its IEEE 754 representations:\n// - 16-bit: 1 sign bit, 5 exponent bits, 10 explicit significand bits.\n// - 32-bit: 1 sign bit, 8 exponent bits, 23 explicit significand bits.\n// - 64-bit: 1 sign bit, 11 exponent bits, 52 explicit significand bits.\n//\n// For example, it converts between:\n// - +1.0 and 0x3C00, 0x3F80_0000 or 0x3FF0_0000_0000_0000.\n// - +5.5 and 0x4580, 0x40B0_0000 or 0x4016_0000_0000_0000.\n// - -inf and 0xFC00, 0xFF80_0000 or 0xFFF0_0000_0000_0000.\n//\n// Converting from f64 to shorter formats may be lossy.\n//\n// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format\n\nstatic inline uint64_t //\nwuffs_base__ieee_754_bit_representation__from_f64_to_u64(double f) {\n uint64_t u = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&u, &f, sizeof(uint64_t));\n }\n return u;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__from_u" +
"64_to_f64(uint64_t u) {\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &u, sizeof(uint64_t));\n }\n return f;\n}\n\n" +
"16_to_f64(uint16_t u) {\n uint64_t v = ((uint64_t)(u & 0x8000)) << 48;\n\n do {\n uint64_t exp = (u >> 10) & 0x1F;\n uint64_t man = u & 0x3FF;\n if (exp == 0x1F) { // Infinity or NaN.\n exp = 2047;\n } else if (exp != 0) { // Normal.\n exp += 1008; // 1008 = 1023 - 15, the difference in biases.\n } else if (man != 0) { // Subnormal but non-zero.\n uint32_t clz = wuffs_base__count_leading_zeroes_u64(man);\n exp = 1062 - clz; // 1062 = 1008 + 64 - 10.\n man = 0x3FF & (man << (clz - 53));\n } else { // Zero.\n break;\n }\n v |= (exp << 52) | (man << 42);\n } while (0);\n\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &v, sizeof(uint64_t));\n }\n return f;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__from_u32_to_f64(uint32_t u) {\n float f = 0;\n if (sizeof(uint32_t) == sizeof(float)) {\n memcpy(&f, &u, sizeof(uint32_t));\n }\n return (double)f;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representat" +
"ion__from_u64_to_f64(uint64_t u) {\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &u, sizeof(uint64_t));\n }\n return f;\n}\n\n" +
"" +
"// ---------------- Integer\n\n// wuffs_base__parse_number_i64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"-123\" then it will return the int64_t -123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow an int64_t.\n//\n// It is similar to wuffs_base__parse_number_u64 but it returns a signed\n// integer, not an unsigned integer. It also allows a leading '+' or '-'.\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__INTCONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 //\nwuffs_base__parse_number_i64(wuffs_base__slice_u8 s, uint32_t options);\n\n// wuffs_base__parse_number_u64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"123\" then it will return the uint64_t 123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow a uint64" +
"_t.\n//\n// It is similar to the C standard library's strtoull function, but:\n// - Errors are returned in-band (in a result type), not out-of-band (errno).\n// - It takes a slice (a pointer and length), not a NUL-terminated C string.\n// - It does not take an optional endptr argument. It does not allow a partial\n// parse: it returns an error unless all of s is consumed.\n// - It does not allow whitespace, leading or otherwise.\n// - It does not allow a leading '+' or '-'.\n// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it\n// always accepts both decimal (e.g \"1234\", \"0d5678\") and hexadecimal (e.g.\n// \"0x9aBC\"). The caller is responsible for prior filtering of e.g. hex\n// numbers if they are unwanted. For example, Wuffs' JSON decoder will only\n// produce a wuffs_base__token for decimal numbers, not hexadecimal.\n// - It is not affected by i18n / l10n settings such as environment variables.\n//\n// The options argument can change these, but by default, it:\n// - Rejects und" +
+37
View File
@@ -4077,6 +4077,43 @@ wuffs_base__ieee_754_bit_representation__from_f64_to_u64(double f) {
return u;
}
static inline double //
wuffs_base__ieee_754_bit_representation__from_u16_to_f64(uint16_t u) {
uint64_t v = ((uint64_t)(u & 0x8000)) << 48;
do {
uint64_t exp = (u >> 10) & 0x1F;
uint64_t man = u & 0x3FF;
if (exp == 0x1F) { // Infinity or NaN.
exp = 2047;
} else if (exp != 0) { // Normal.
exp += 1008; // 1008 = 1023 - 15, the difference in biases.
} else if (man != 0) { // Subnormal but non-zero.
uint32_t clz = wuffs_base__count_leading_zeroes_u64(man);
exp = 1062 - clz; // 1062 = 1008 + 64 - 10.
man = 0x3FF & (man << (clz - 53));
} else { // Zero.
break;
}
v |= (exp << 52) | (man << 42);
} while (0);
double f = 0;
if (sizeof(uint64_t) == sizeof(double)) {
memcpy(&f, &v, sizeof(uint64_t));
}
return f;
}
static inline double //
wuffs_base__ieee_754_bit_representation__from_u32_to_f64(uint32_t u) {
float f = 0;
if (sizeof(uint32_t) == sizeof(float)) {
memcpy(&f, &u, sizeof(uint32_t));
}
return (double)f;
}
static inline double //
wuffs_base__ieee_754_bit_representation__from_u64_to_f64(uint64_t u) {
double f = 0;
+206
View File
@@ -423,6 +423,210 @@ test_wuffs_strconv_hpd_shift() {
// ----------------
const char* //
test_wuffs_strconv_ieee_754_bit_representation_from_u16() {
CHECK_FOCUS(__func__);
double inf = 1.0 / 0.0;
uint64_t nan_u64 = 0x7FFFFFFFFFFFFFFF;
double nan =
wuffs_base__ieee_754_bit_representation__from_u64_to_f64(nan_u64);
struct {
uint16_t u16_bits;
uint64_t want_u64;
double want_f64;
} test_cases[] = {
{
.u16_bits = 0x0000,
.want_u64 = 0x0000000000000000,
.want_f64 = 0.0,
},
{
.u16_bits = 0x0001,
.want_u64 = 0x3E70000000000000,
.want_f64 = 0.000000059604644775390625,
},
{
.u16_bits = 0x0123,
.want_u64 = 0x3EF2300000000000,
.want_f64 = 0.000017344951629638671875,
},
{
.u16_bits = 0x03FF,
.want_u64 = 0x3F0FF80000000000,
.want_f64 = 0.000060975551605224609375,
},
{
.u16_bits = 0x0400,
.want_u64 = 0x3F10000000000000,
.want_f64 = 0.00006103515625,
},
{
.u16_bits = 0x0401,
.want_u64 = 0x3F10040000000000,
.want_f64 = 0.000061094760894775390625,
},
{
.u16_bits = 0x3C00,
.want_u64 = 0x3FF0000000000000,
.want_f64 = 1.0,
},
{
.u16_bits = 0x4580,
.want_u64 = 0x4016000000000000,
.want_f64 = 5.5,
},
{
.u16_bits = 0x7BFF,
.want_u64 = 0x40EFFC0000000000,
.want_f64 = 65504.0,
},
{
.u16_bits = 0x7C00,
.want_u64 = 0x7FF0000000000000,
.want_f64 = inf,
},
{
.u16_bits = 0x7E00,
.want_u64 = nan_u64,
.want_f64 = nan,
},
{
.u16_bits = 0xFC00,
.want_u64 = 0xFFF0000000000000,
.want_f64 = -inf,
},
};
int tc;
for (tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) {
double have_f64 = wuffs_base__ieee_754_bit_representation__from_u16_to_f64(
test_cases[tc].u16_bits);
double want_f64 = test_cases[tc].want_f64;
// See if they're equal, considering that NaN != NaN.
bool equal = ((have_f64 == want_f64) ||
((have_f64 != have_f64) && (want_f64 != want_f64)));
if (!equal) {
RETURN_FAIL("tc=%d: to_f64: have %g, want %g", tc, have_f64, want_f64);
}
uint64_t have_u64 =
wuffs_base__ieee_754_bit_representation__from_f64_to_u64(have_f64);
uint64_t want_u64 = test_cases[tc].want_u64;
// There's more than one representation of NaN.
if ((have_u64 != want_u64) && (want_u64 != nan_u64)) {
RETURN_FAIL("tc=%d: to_u64: have 0x%016" PRIX64 ", want 0x%016" PRIX64,
tc, have_u64, want_u64);
}
}
return NULL;
}
const char* //
test_wuffs_strconv_ieee_754_bit_representation_from_u32() {
CHECK_FOCUS(__func__);
double inf = 1.0 / 0.0;
uint64_t nan_u64 = 0x7FFFFFFFFFFFFFFF;
double nan =
wuffs_base__ieee_754_bit_representation__from_u64_to_f64(nan_u64);
struct {
uint32_t u32_bits;
uint64_t want_u64;
double want_f64;
} test_cases[] = {
{
.u32_bits = 0x00000000,
.want_u64 = 0x0000000000000000,
.want_f64 = 0.0,
},
{
.u32_bits = 0x00000001,
.want_u64 = 0x36A0000000000000,
.want_f64 = 1.4012984643248170709237295832899161312802619418765e-45,
},
{
.u32_bits = 0x00000123,
.want_u64 = 0x3722300000000000,
.want_f64 = 4.0777785311852176763880530873736559420255622508607e-43,
},
{
.u32_bits = 0x007FFFFF,
.want_u64 = 0x380FFFFFC0000000,
.want_f64 = 1.1754942106924410754870294448492873488270524287459e-38,
},
{
.u32_bits = 0x00800000,
.want_u64 = 0x3810000000000000,
.want_f64 = 1.1754943508222875079687365372222456778186655567721e-38,
},
{
.u32_bits = 0x00800001,
.want_u64 = 0x3810000020000000,
.want_f64 = 1.1754944909521339404504436295952040068102786847983e-38,
},
{
.u32_bits = 0x3F800000,
.want_u64 = 0x3FF0000000000000,
.want_f64 = 1.0,
},
{
.u32_bits = 0x40B00000,
.want_u64 = 0x4016000000000000,
.want_f64 = 5.5,
},
{
.u32_bits = 0x7F7FFFFF,
.want_u64 = 0x47EFFFFFE0000000,
.want_f64 = 3.40282346638528859811704183484516925440e38,
},
{
.u32_bits = 0x7F800000,
.want_u64 = 0x7FF0000000000000,
.want_f64 = inf,
},
{
.u32_bits = 0x7FC00000,
.want_u64 = nan_u64,
.want_f64 = nan,
},
{
.u32_bits = 0xFF800000,
.want_u64 = 0xFFF0000000000000,
.want_f64 = -inf,
},
};
int tc;
for (tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) {
double have_f64 = wuffs_base__ieee_754_bit_representation__from_u32_to_f64(
test_cases[tc].u32_bits);
double want_f64 = test_cases[tc].want_f64;
// See if they're equal, considering that NaN != NaN.
bool equal = ((have_f64 == want_f64) ||
((have_f64 != have_f64) && (want_f64 != want_f64)));
if (!equal) {
RETURN_FAIL("tc=%d: to_f64: have %g, want %g", tc, have_f64, want_f64);
}
uint64_t have_u64 =
wuffs_base__ieee_754_bit_representation__from_f64_to_u64(have_f64);
uint64_t want_u64 = test_cases[tc].want_u64;
// There's more than one representation of NaN.
if ((have_u64 != want_u64) && (want_u64 != nan_u64)) {
RETURN_FAIL("tc=%d: to_u64: have 0x%016" PRIX64 ", want 0x%016" PRIX64,
tc, have_u64, want_u64);
}
}
return NULL;
}
// ----------------
const char* //
test_wuffs_strconv_base_16() {
CHECK_FOCUS(__func__);
@@ -3640,6 +3844,8 @@ proc g_tests[] = {
test_wuffs_strconv_base_64,
test_wuffs_strconv_hpd_rounded_integer,
test_wuffs_strconv_hpd_shift,
test_wuffs_strconv_ieee_754_bit_representation_from_u16,
test_wuffs_strconv_ieee_754_bit_representation_from_u32,
test_wuffs_strconv_parse_number_f64_options,
test_wuffs_strconv_parse_number_f64_regular,
test_wuffs_strconv_parse_number_i64,