fix gu_encode_double

This commit is contained in:
Krasimir Angelov
2017-09-13 17:20:00 +02:00
parent febf01a9be
commit 1ff8dd88e8

View File

@@ -45,9 +45,9 @@ gu_decode_double(uint64_t u)
GU_INTERNAL uint64_t GU_INTERNAL uint64_t
gu_encode_double(double d) gu_encode_double(double d)
{ {
int sign = (d < 0) ? 1 : 0; int sign = signbit(d) > 0;
int rawexp; unsigned rawexp;
double mantissa; uint64_t mantissa;
switch (fpclassify(d)) { switch (fpclassify(d)) {
case FP_NAN: case FP_NAN:
@@ -58,14 +58,19 @@ gu_encode_double(double d)
rawexp = 0x7ff; rawexp = 0x7ff;
mantissa = 0; mantissa = 0;
break; break;
default: default: {
mantissa = frexp(d, &rawexp); int exp;
rawexp += 1075; mantissa = (uint64_t) scalbn(frexp(d, &exp), 53);
mantissa &= ~ (1ULL << 52);
exp -= 53;
rawexp = exp + 1075;
}
} }
uint64_t u = (((uint64_t) sign) << 63) | uint64_t u = (((uint64_t) sign) << 63) |
((((uint64_t) rawexp) << 52) & 0x7ff) | (((uint64_t) rawexp & 0x7ff) << 52) |
((uint64_t) mantissa); mantissa;
return u; return u;
} }