我正在优化一个数字/统计库的排序函数,基于这样的假设:在过滤掉任何 NaN 并稍微调整一下之后, float 可以在不改变结果的情况下与 32 位整数进行比较,而 double 可以作为 64 位整数进行比较。
这似乎将这些数组的排序速度提高了大约 40%,只要 float 的位级表示是 IEEE 754,我的假设就成立。人们实际上是否有任何真实世界的 CPU使用(不包括嵌入式设备,该库不针对的嵌入式设备)使用可能会破坏此假设的其他表示形式?
- https://en.wikipedia.org/wiki/Single-precision_floating-point_format
(binary32,在使用 IEEE754 的系统中也称为float
) - https://en.wikipedia.org/wiki/Double-precision_floating-point_format
(binary64,在使用 IEEE754 的系统中也称为double
)
最佳答案
flawed Pentiums 除外,任何基于 x86 或 x64 的 CPU 都使用 IEEE 754 作为其浮点运算标准。
以下是 FPA 标准及其采用情况的简要概述。
IEEE 754: Intel x86, and all RISC systems (IBM Power
and PowerPC, Compaq/DEC Alpha, HP PA-RISC,
Motorola 68xxx and 88xxx, SGI (MIPS) R-xxxx,
Sun SPARC, and others);
VAX: Compaq/DEC
IBM S/390: IBM (however, in 1998, IBM added an IEEE 754
option to S/390)
Cray: X-MP, Y-MP, C-90; other Cray models have been
based on Alpha and SPARC processors with
IEEE-754 arithmetic.
除非您计划在相当奇特的 CPU 架构上支持您的库,否则可以安全地假设目前 99% 的 CPU 都符合 IEEE 754。
关于performance - 现实世界中的 CPU 是否不使用 IEEE 754?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52029104/