Moving integer division to floating-point is trivial

(marc-b-reynolds.github.io)

14 points | by matt_d 3 days ago

4 comments

  • taeric 22 minutes ago
    I'm curious that the latency of these is actually worse than the latency of floating operations. Yes, they are worse than many integer instructions, but they seem to be on basically the same order as the equivalent float operations?
    • mtklein 6 minutes ago
      My rule of thumb is that integer ops cost 1 cycle except divides, floats 3 but maybe divide is a bit more, then integer divides are like infinity at 20+ cycles that cannot be amortized by vectorization.

      When you code simd it's best to assume the integer divide instruction does not exist. Just an impossibility, if you need to divide ints, rethink your whole program.

  • juancn 25 minutes ago
    I would love to have some benchmarks for this on some reasonably practical scenario.

    In many common cases shift and masking can replace integer division (i.e. hash tables) and you avoid division altogether, and that probably has about the cost of converting an int to a float.

  • RossBencina 45 minutes 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".
    • NooneAtAll3 10 minutes ago
      I 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

    • mtklein 31 minutes ago
      They'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.
    • ranger_danger 27 minutes ago
      Perhaps 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.