Re:Converting binary floating-point numbers to integers
by eeegnu on Saturday October 23, 2021 @01:56AM
(#28966112)
Looking 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.
Re:Converting binary floating-point numbers to integers
by kevingadd on Saturday October 23, 2021 @05:42AM
(#28967004)
If 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.
Re:Converting binary floating-point numbers to integers
by nly on Saturday October 23, 2021 @05:36AM
(#28966977)
Unit 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.
Re:Converting binary floating-point numbers to integers
by userbinator on Saturday October 23, 2021 @01:50AM
(#28966089)
Instead 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.
Re:Converting binary floating-point numbers to integers
by pm215 on Saturday October 23, 2021 @06:11AM
(#28967100)
Hmm? 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.)
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.
Re:Converting binary floating-point numbers to integers
by nly on Saturday October 23, 2021 @05:43AM
(#28967012)
It 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
Re:Converting binary floating-point numbers to integers
by CyberRabbi on Saturday November 06, 2021 @08:25PM
(#29135207)
It was never claimed to deal with endianness and while we're on the subject of binary-layout, union-based binary manipulation is even worse on that front as no specific binary layout is mandated for structures or floating points.
for (double x = 1; x < 1000000000; x += 0.5) {
}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.
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.
[1]: https://randomascii.wordpress.com/2014/01/27/theres-only-fou...
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.
(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.)
Iām surprised at that. Can you quote the relevant part of the spec that you think makes this undefined behaviour?