A fast method to round a double to a 32-bit int explained

前端 未结 3 1513
谎友^
谎友^ 2020-11-28 17:18

When reading Lua\'s source code, I noticed that Lua uses a macro to round a double to a 32-bit int. I extracted the macro

3条回答
  •  臣服心动
    2020-11-28 17:59

    This kind of "trick" comes from older x86 processors, using the 8087 intructions/interface for floating point. On these machines, there's an instruction for converting floating point to integer "fist", but it uses the current fp rounding mode. Unfortunately, the C spec requires that fp->int conversions truncate towards zero, while all other fp operations round to nearest, so doing an
    fp->int conversion requires first changing the fp rounding mode, then doing a fist, then restoring the fp rounding mode.

    Now on the original 8086/8087, this wasn't too bad, but on later processors that started to get super-scalar and out-of-order execution, altering the fp rounding mode generally seriales the CPU core and is quite expensive. So on a CPU like a Pentium-III or Pentium-IV, this overall cost is quite high -- a normal fp->int conversion is 10x or more expensive than this add+store+load trick.

    On x86-64, however, floating point is done with the xmm instructions, and the cost of converting
    fp->int is pretty small, so this "optimization" is likely slower than a normal conversion.

提交回复
热议问题