Dieter B?r?ner wrote:Gerd Isenberg wrote:Since leading zero count is also necessary to convert ints to floats/double there are also some more or less portable tricks to interprete the binary representation of a double, the base two exponent of a normalized mantissa.
Interesting thought. Especially in C99, it should be very portable for 32-bit integers. C99 has the ilogb() function (it was available since long on many systems). ilogb() should even get inlined by a good compiler. I think, it will not work on 64 bit integers, however. We can pretty much assume IEEE 754 floating point representation (and it can even be checked), but we cannot assume, that we have a floating point type, that has enough accuracy for 64 bit numbers.
Cheers,
Dieter
Hi Dieter,
didn't you once post some c-code in CCC where 64-bit x &-x isolated LSB was converted to a double, while an unsigned char pointer was alialising that double, interpreting the exponent (and sign bit)?
Since we are only interested in MSB some rounding toward zero or down
AMD64 Architecture Programmer?s Manual
Volume 1: Application Programming
Page 158 4.4.9 Floating-Point Rounding
affects only the lowest bit of the normalzed mantissa without any overflow. I guess even a float convert is exact enough to determine the MSB by the exponent that way.
But if i look for appropriate sse or 3dnow instructions, converting 64-bit int, there seems only CVTSI2SD - with 11 cycles double dispatch not particular fast, and - converting to scalar 4 byte float with CVTSI2SS is even 14 cycles vector path.
There are faster SIMD instructions, like CVTDQ2PS (5 cycles double dispatch), converting vectors of four 32-bit ints to four 32-bit floats - but i fear it is not worth, also due to the hussle with none default rounding control Bits 14?13 of the MXCSR control and status register.
I once tried LSB by 3DNow/mmx-version via PI2FD which is only 4 cycles direct path - not that bad if you like to bitscan in mmx
Gerd