float.c: add support for subnormal floats

This commit is contained in:
xenia 2021-01-21 18:34:41 -05:00
parent 5bd0a65aaa
commit c9c8e94ddf
1 changed files with 23 additions and 10 deletions

View File

@ -68,21 +68,32 @@ uint32_t getbits_meow(float f) {
f2 = f3;
}
// TODO : subnormal numbers (div_count == 0 ?)
// the div count is cool and all but it allows going into subnormal range while maintaing
// equality which is not good
// so we use the mul count
uint32_t exp = 0xFE - mul_count;
for (uint32_t i = 0; i < 254-127-23; i++) {
f2 = f2 / 2.0f;
}
if (mul_count > 0xFE) {
// subnormal
uint32_t mantissa = ((uint32_t) f2) & 0b11111111111111111111111;
// add back the implicit leading 1 that would be explicit in subnormals
mantissa |= 1 << 23;
// shift into place by the number of extra times we were allowed to multiply
// (indicating the first leading 1 position)
mantissa >>= (mul_count - 0xFE + 1);
printf("result: ");
printb((sign << 31) | mantissa);
} else {
// normal
// the div count is cool and all but it allows going into subnormal range while maintaing
// equality which is not good
// so we use the mul count
uint32_t exp = 0xFE - mul_count;
uint32_t mantissa = ((uint32_t) f2) & 0b11111111111111111111111;
printf("result: ");
printb((sign << 31) | ((exp & 0xFF) << 23) | mantissa);
}
printf("----------------\n");
}
@ -93,4 +104,6 @@ int main() {
getbits_meow(10.0f);
getbits_meow(100000.0f);
getbits_meow(10000000000.0f);
getbits_meow(0.00000000000000000000000000000000000000001f);
}