3 ms·
So what is the well-defined and performant way to fetch the exponent and mantissa of a double in standard C?
by jancsika 8y ago
So what is the well-defined and performant way to fetch the exponent and mantissa of a double in standard C?
- saagarjha 8y agoUse frexp from <math.h>.
- jancsika 8y agoThe implementation for frexp uses type-punning. Also, it adds a denormal and inf check which I don't need in this particular hot loop.
- saagarjha 8y agoDoes it? On my computer it's a builtin (__builtin_frexp). But even if it did use type-punning, that doesn't make it legal for your program to do it. I don't believe there's any requirements on the C standard library to follow the standard themselves. To answer the second part of your response, the correct way to do this yourself would be to memcpy the double and then inspect the result directly. Any compiler worth its salt will end up generating the assembly code you want (namely, no call to memcpy, just a couple of arithmetic shifts and masks).
- jancsika 8y ago> To answer the second part of your response, the correct way to do this yourself would be to memcpy the double and then inspect the result directly. Any compiler worth its salt will end up generating the assembly code you want (namely, no call to memcpy, just a couple of arithmetic shifts and masks). What does a compiler "worth its salt" do with the memcpy at "-O0"?
- saagarjha 8y agoSee for yourself: https://godbolt.org/z/nsg9nt https://godbolt.org/z/nsg9nt