mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 08:47:28 +01:00
Add IEEE 754 from_u{16,32}_to_f64 functions
This commit is contained in:
1 parent
4d449dc663
commit
0899b82c42
4 files changed
+282
-1
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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" +
|
||||
|
||||
Reference in new issue
Block a user