News for hackers. Stuff that matters.
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 ghoward on Saturday October 23, 2021 @04:34PM (#28971986)
Or (if he did it with floats), tested it with all 4 billion floats. [1]

[1]: https://randomascii.wordpress.com/2014/01/27/theres-only-fou...

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.)

Re:Converting binary floating-point numbers to integers by CyberRabbi on Saturday October 23, 2021 @02:46AM (#28966291)

    typedef 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.
Re:Converting binary floating-point numbers to integers by Cloudef on Saturday October 23, 2021 @02:50AM (#28966306)
Its not, union is valid in C(note, not c++) for this.
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 occamrazor on Saturday October 23, 2021 @06:29AM (#28967157)
What is the alternative? Are there functions in the C standard to get mantissa and exponent?
Re:Converting binary floating-point numbers to integers by gpderetta on Saturday October 23, 2021 @09:03AM (#28967992)
Frexp modf. Last time I checked code generation wasn't particularly good.
Re:Converting binary floating-point numbers to integers by CyberRabbi on Saturday October 23, 2021 @03:37PM (#28971541)
I already mentioned the alternative. memcpy() into a char array.
Re:Converting binary floating-point numbers to integers by Cloudef on Sunday October 24, 2021 @06:05AM (#28976433)
Memcpy doesnt fix endianess
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.
Re:Converting binary floating-point numbers to integers by CyberRabbi on Saturday October 23, 2021 @03:38PM (#28971545)
lemire's code is C++
Re:Converting binary floating-point numbers to integers by chrisseaton on Saturday October 23, 2021 @10:35AM (#28968915)
> Accessing the value stored in "value" through the "fields" member is undefined behavior.

I’m surprised at that. Can you quote the relevant part of the spec that you think makes this undefined behaviour?

Re:Converting binary floating-point numbers to integers by CyberRabbi on Saturday October 23, 2021 @03:37PM (#28971532)
It's undefined behavior in C++. In C it is not certain if it is undefined or unspecified behavior. https://stackoverflow.com/questions/11373203/accessing-inact...
Re:Converting binary floating-point numbers to integers by chrisseaton on Saturday October 23, 2021 @04:21PM (#28971873)
I think the standard and the link you've giving makes it pretty explicit that it's unspecified. I don't really see where you can get undefined from?