3 ms·
Converting binary floating-point numbers to integers
- userbinator 5y agoInstead of working with high-level instructions, you could copy your binary floating-point number to a 64-bit word and use your knowledge of the IEEE binary64 standard to extract the mantissa and the exponent The results aren't too surprising. The ARM, which is a RISC, doesn't show much difference because it doesn't have much in the way of good complex instructions suited to the task, and in this case you can actually beat the compiler by optimising at the source level. x86 is still fundamentally CISC in nature and making use of the complex instructions is better especially when they are optimised in hardware. I don't have those machines nor environment to compare, but if you look at the code size, you might find the compiler has generated very similar code for the long and short version on ARM, whereas it certainly won't be smart enough to "recompress" your long version back into CISC instructions.
- pm215 5y agoHmm? The A64 instruction set includes instructions to convert from floating point to integer and set the Inexact FPCR flag if the conversion isn't exact, which is pretty much exactly the operation we want here (it's one of the various FCVT insns). The catch is that Inexact is a sticky flag, so you'd need to clear it first. That might work out slower than "convert float to int, then int to float and see if you got back the same value" as the first C++ function does. It's also going to be awkward for a compiler to do so you'd be dropping down to inline asm. Would be interesting to benchmark, though. (Edit: and as one of the blog post comments notes, for the special case of double to 32 bit integer the FCVTJS insn does this and puts the 'exact or not' information conveniently in the Z flag to branch on.)
- eeegnu 5y agoLooking through the source where the tests are conducted, for (double x = 1; x < 1000000000; x += 0.5) { counter++; int64_t tmp; if (to_int64_simple(x, &tmp)) { sum += tmp; } } The fractional part is always mostly 0.5, or 0.10000... While that shouldn't really affect things, I think a proper test would sample randomly, though this runs into the problem of the random number generator itself influencing the time spent, so I can see the appeal for this method. Perhaps generating a random float array with N elements and calculating the results for x + arr[x%N] would accomplish this.
- nly 5y agoUnit test cases should be explicit. For determining if his algorithm is correct he could implement it 2 completely different ways and do bitwise fuzz testing to try and find cases where the output differs.
- ghoward 5y agoOr (if he did it with floats), tested it with all 4 billion floats. [1] [1]: https://randomascii.wordpress.com/2014/01/27/theres-only-four-billion-floatsso-test-them-all/ https://randomascii.wordpress.com/2014/01/27/theres-only-fou...
- kevingadd 5y agoIf your goal is to benchmark something like to_int64, you should be benchmarking it on a wide range of values instead of just ones and one-halves. You can always pre-generate a diverse data set and shove it in memory - how expensive that is doesn't matter, since all your benchmarks will be paying the same cost to read the test data (unless one of your benchmark test cases is so profoundly slow that it pushes the test data out of cache) Similarly you definitely should have a table of known numbers you want to test and not just go through the space by 0.5. You want to test denormals, stuff like 1/3, etc.
- CyberRabbi 5y agotypedef union { struct { uint64_t fraction : 52; uint32_t exp_bias : 11; uint32_t sign : 1; } fields; double value; } binary64_float; Accessing the value stored in "value" through the "fields" member is undefined behavior. I'm legitimately surprised that lemire published this code. The correct and well-defined way to do this is to memcpy() the double value to a char array and access the relevant bits that way.
- Cloudef 5y agoIts not, union is valid in C(note, not c++) for this.
- nly 5y agoIt is probably still unwise in C as well in this case because the layout of bits in a bitfield is still implementation defined, and varied based on endianness of the uint64 for example
- occamrazor 5y agoWhat is the alternative? Are there functions in the C standard to get mantissa and exponent?
- gpderetta 5y agoFrexp modf. Last time I checked code generation wasn't particularly good.
- CyberRabbi 5y agoI already mentioned the alternative. memcpy() into a char array.
- Cloudef 5y agoMemcpy doesnt fix endianess