diff --git a/ieee754/float.c b/ieee754/float.c index e04e72b..272eab5 100644 --- a/ieee754/float.c +++ b/ieee754/float.c @@ -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; } - uint32_t mantissa = ((uint32_t) f2) & 0b11111111111111111111111; + 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) | ((exp & 0xFF) << 23) | mantissa); + 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); }