3 ms·
x and y are integers represented as floating point > d = trunc(x/y); // floor works for unsigned > > // NOTE: if only want 'd' and it's being convert
by RossBencina 1mo ago
x and y are integers represented as floating point
> d = trunc(x/y); // floor works for unsigned
>
> // NOTE: if only want 'd' and it's being converted to an
> // integer then the truncate or floor operation is
> // free in the float to integer conversion.
Please show me how to portably truncate or floor a floating point value to an int in C for "free".
- mtklein 1mo agoThey're typically like a 3-cycle op, right? Obviously that's not zero, but as far as floating point ops get it's a cheap as it gets, like an add, mul, fma, that sorta thing.
- RossBencina 1mo agoI am probably dating myself here, but in C the default rounding mode is round-to-nearest and the cost of switching the x86 FPU to truncate (and back) in order to perform the truncation is extremely expensive. Perhaps other instruction sets have truncate-to-integer instructions that ignore the active rounding mode.
- ranger_danger 1mo agoPerhaps they meant implicit instead of free, since the comment also explains that `d` is an integer and the intent is to truncate, so the trunc() call is not even necessary when the assigning type is an int.
- deleted 1mo ago[deleted]
- NooneAtAll3 1mo agoI think you misread? {within float-to-integer conversion} trunc or floor is free that is, if you are converting, you already get it by default
- RossBencina 1mo agoI think you are correct. Clearly I am still traumatised by the cost of converting floats to ints in the x86 era.