# 快速平方根倒数

**快速平方根倒数**（又称 Fast InvSqrt()，或以十六进制常数 0x5F3759DF 闻名）是一种算法，用于估算 \( 1/\sqrt{x} \)，即 IEEE 754 格式 32 位浮点数的平方根的倒数。其做法是将该数的位视为整数，用一个“魔术常数”执行一次整数运算，再用一次牛顿-拉弗森迭代精化结果。<sup>[8](https://www.lomont.org/papers/2003/InvSqrt.pdf)</sup> 它最著名的是在 1999 年第一人称射击游戏《雷神之锤III竞技场》（Quake III Arena）的源代码中的实现，该游戏大量使用 3D 图形。<sup>[11](https://groovy.apache.org/blog/quake3-inverse-square-root)</sup>

| 关键事实 | 详情 |
|---|---|
| 计算内容 | 32 位 IEEE 754 浮点数 x 的平方根倒数 \( 1/\sqrt{x} \) 的近似值，主要用于 3D 图形中的向量归一化<sup>[8](https://www.lomont.org/papers/2003/InvSqrt.pdf)</sup> |
| 魔术常数 | 0x5f3759df，通过从该常数中减去浮点数位模式右移一位后的值，形成初始猜测<sup>[1](https://github.com/id-Software/Quake-III-arena/blob/master/code/game/q_math.c)</sup> |
| 精化 | 一次牛顿-拉弗森迭代 \( y \cdot (1.5 - 0.5x \cdot y^2) \)；源代码注释指出第二次迭代可以去掉<sup>[1](https://github.com/id-Software/Quake-III-Arena/blob/master/code/game/q_math.c)</sup> |
| 精度 | 典型误差约 0.1–0.2%，对图形渲染而言不可察觉<sup>[2](https://github.com/raduacg/game-mechanics-optimizations/blob/main/11_fast_inverse_square_root.md)</sup> |
| 速度 | 约 5–10 个 CPU 周期，而库函数 sqrt 加除法需 40–80 个周期；硬件 SIMD 指令完成同样工作约需 2–3 个周期<sup>[2](https://github.com/raduacg/game-mechanics-optimizations/blob/main/11_fast_inverse_square_root.md)</sup> |
| 起源 | 归于 1980 年代末 Ardent Computer 的格雷格·沃尔什（Greg Walsh），他与数值分析师克莱夫·莫勒（Cleve Moler）合作；后经加里·塔罗利（Gary Tarolli）传入 id Software 的《雷神之锤III》代码<sup>[3](https://retrocomputing.stackexchange.com/questions/4615/where-did-fast-invsqrt-come-from?noredirect=1)</sup> |
| 今日地位 | 已被专用硬件指令取代，如 x86 SSE 的 `rsqrtss` 和 ARM NEON 的 `VRSQRTE`<sup>[4](https://support.arm.com/documentation/ddi0487/mc/-Part-E-The-AArch32-Application-Level-Architecture/-Chapter-E1-The-AArch32-Application-Level-Programmers--Model/-E1-3-Advanced-SIMD-and-floating-point-instructions/-E1-3-11-Pseudocode-description-of-floating-point-operations?lang=en)</sup> |

## 算法原理

技巧基于 IEEE 754 单精度浮点数的布局：第一位是符号位，接下来 8 位编码指数，最后 23 位编码尾数。<sup>[5](https://www.sciencedirect.com/science/article/pii/S0096300317305763)</sup> 将其视为整数读取时，这一位模式表现得如同该数经过缩放的对数，因此对位进行整数运算可以同时近似指数和尾数的运算。<sup>[2](https://github.com/raduacg/game-mechanics-optimizations/blob/main/11_fast_inverse_square_root.md)</sup>

《雷神之锤III》中的实现是 `q_math.c` 文件中名为 `Q_rsqrt` 的函数，分四步运行：<sup>[1](https://github.com/id-Software/Quake-III-arena/blob/master/code/game/q_math.c)</sup>

1. 将输入浮点数的位复制到一个长整数中（`i = *(long*)&y`，注释为“邪恶的浮点位级黑客手段”）。
2. 计算 `i = 0x5f3759df - (i >> 1)`，注释为“这他妈是什么？”。右移将指数字段减半，近似于开平方，而魔术常数则修正了缩放和尾数项。
3. 将该整数的位复制回浮点数，得到 \( 1/\sqrt{x} \) 的粗略估计。
4. 应用一次牛顿-拉弗森迭代，`y = y * (threehalfs - (x2 * y * y))`，其中 `threehalfs` 为 1.5，`x2` 为输入的一半。被注释掉的第二次迭代注明“可以去掉”。

牛顿-拉弗森步骤是平方根倒数的标准迭代：当 \( x_0 \) 是合适的初始估计时，\( x_{n+1} = x_n (3 - d \cdot x_n^2)/2 \) 收敛到 \( 1/\sqrt{d} \)。<sup>[4](https://support.arm.com/documentation/ddi0487/mc/-Part-E-The-AArch32-Application-Level-Architecture/-Chapter-E1-The-AArch32-Application-Level-Programmers--Model/-E1-3-Advanced-SIMD-and-floating-point-instructions/-E1-3-11-Pseudocode-description-of-floating-point-operations?lang=en)</sup> 重复该步骤，将每次输出作为下一次输入反馈回去，即可将估计值推向真值。<sup>[8](https://www.lomont.org/papers/2003/InvSqrt.pdf)</sup>

速度提升来自避免昂贵的运算。当时，通过库函数开平方再除法的途径计算平方根倒数，代价约为 40–80 个 CPU 周期，而位黑客加一次迭代只需约 5–10 个周期。对于为光照、物理和粒子效果执行数百万次向量归一化的游戏而言，这一改进可转化为每帧节省数毫秒。<sup>[2](https://github.com/raduacg/game-mechanics-optimizations/blob/main/11_fast_inverse_square_root.md)</sup>

## 起源与归属

2005 年 8 月 19 日《雷神之锤III竞技场》源代码发布后，该函数一举成名，关于作者是谁的讨论随即展开。<sup>[6](https://www.beyond3d.com/content/articles/15)</sup> 早在 2002 或 2003 年，代码副本就已在 Usenet 及其他论坛上流传，早于正式发布。<sup>[8](https://lomont.org/papers/2003/InvSqrt.pdf)</sup>

id Software 的首席程序员约翰·卡马克（John Carmack）被广泛认为是作者，但他本人并未声称发明了它。<sup>[3](https://retrocomputing.stackexchange.com/questions/4615/where-did-fast-invsqrt-come-from?noredirect=1)</sup> 里斯·索默费尔特（Rys Sommefeldt）在 Beyond3D 上发表的两篇文章中进行的调查，将该算法追溯到格雷格·沃尔什。沃尔什于 1980 年代末在 Ardent Computer 为 Titan 图形小型机工作时，构思了平方根倒数和立方根倒数方法，并借鉴了 MathWorks 创始人、MATLAB 创造者克莱夫·莫勒的想法，莫勒当时也在 Ardent 与沃尔什共事。<sup>[3](https://retrocomputing.stackexchange.com/questions/4615/where-did-fast-invsqrt-come-from?noredirect=1)</sup> 为久保田（Kubota，Ardent 的出资方）担任顾问的加里·塔罗利在 1994 年前后将算法从那里带到了 3dfx Interactive。<sup>[7](https://ocathain.ie/articles/why-i-love-fast-inverse-square-root)</sup>

这一基本思想在《雷神之锤III》之前就已见于出版物。

## 分析与改进的常数

魔术常数本身也引起了数学界的关注。克里斯·洛蒙特（Chris Lomont）2003 年的论文《快速平方根倒数》分析了该常数的选择，指出此前的解释——包括大卫·埃伯利（David Eberly）的一个——接近但不完全正确。<sup>[8](https://www.lomont.org/papers/2003/InvSqrt.pdf)</sup> 莫罗兹（Moroz）及其同事 2018 年在《应用数学与计算》（*Applied Mathematics and Computation*）上发表的论文，首次对单精度浮点数给出了该算法的严格数学描述，并系统性地推导了最优魔术常数，以最小化相对误差或绝对误差；论文表明最优常数取决于后续牛顿-拉弗森迭代的次数，并给出了一次和两次迭代的结果。<sup>[5](https://www.sciencedirect.com/science/article/pii/S0096300317305763)</sup>

对这一系列工作的综述——包括洛蒙特、皮泽（Pizer）、卡德莱茨（Kadlec）、莫罗兹等人以及瓦尔奇克（Walczyk）等人的分析——得出结论：仅改变数值常数即可在执行成本不变的情况下将误差降至原来的约三分之一。<sup>[9](https://arxiv.org/abs/2307.15600)</sup> 同一篇 2023 年的综述将该技术从平方根倒数推广到任意有理幂，为多次迭代精化构造了可证明最优的常数。<sup>[9](https://arxiv.org/abs/2307.15600)</sup>

## 硬件替代

在微处理器内置平方根倒数计算硬件支持之前，该算法被广泛使用。<sup>[9](https://arxiv.org/abs/2307.15600)</sup> 现代指令集提供了专用的估算指令，使软件黑客手段过时：x86 SSE 提供 `rsqrtss`，ARM Advanced SIMD 提供 `VRSQRTE`，后者产生平方根倒数的初始估计，而 `VRSQRTS` 执行用于精化的 \( (3 - op_1 \cdot op_2)/2 \) 步骤。<sup>[4](https://support.arm.com/documentation/ddi0487/mc/-Part-E-The-AArch32-Application-Level-Architecture/-Chapter-E1-The-AArch32-Application-Level-Programmers--Model/-E1-3-Advanced-SIMD-and-floating-point-instructions/-E1-3-11-Pseudocode-description-of-floating-point-operations?lang=en)</sup> 在许多 GPU 上，`rsqrt` 是一条硬件指令，比直接计算 `1.0 / sqrt(x)` 更快。<sup>[10](https://www.bwsl.dev/docs/intrinsics/rsqrt)</sup> 硬件估算每个向量约需 2–3 个周期，比软件版本快数倍，误差约 0.2–0.4%，对图形而言仍可接受。<sup>[2](https://github.com/raduacg/game-mechanics-optimizations/blob/main/11_fast_inverse_square_root.md)</sup> 现代游戏引擎会自动使用这些指令，该算法主要作为利用浮点表示法的广泛研究范例而存留。<sup>[2](https://github.com/raduacg/game-mechanics-optimizations/blob/main/11_fast_inverse_square_root.md)</sup>

## References

1. [id Software, Quake III Arena source code, q_math.c (Q_rsqrt function)](https://github.com/id-Software/Quake-III-arena/blob/master/code/game/q_math.c)
2. [Fast Inverse Square Root, game-mechanics-optimizations (GitHub)](https://github.com/raduacg/game-mechanics-optimizations/blob/main/11_fast_inverse_square_root.md)
3. [Where did Fast InvSqrt() come from? Retrocomputing Stack Exchange (summarizing Beyond3D's investigation)](https://retrocomputing.stackexchange.com/questions/4615/where-did-fast-invsqrt-come-from?noredirect=1)
4. [Arm Architecture Reference Manual for A-profile architecture, VRSQRTE and VRSQRTS pseudocode](https://support.arm.com/documentation/ddi0487/mc/-Part-E-The-AArch32-Application-Level-Architecture/-Chapter-E1-The-AArch32-Application-Level-Programmers--Model/-E1-3-Advanced-SIMD-and-floating-point-instructions/-E1-3-11-Pseudocode-description-of-floating-point-operations?lang=en)
5. [Moroz et al., Fast calculation of inverse square root with the use of magic constant - analytical approach, Applied Mathematics and Computation 316 (2018)](https://www.sciencedirect.com/science/article/pii/S0096300317305763)
6. [Sommefeldt, R., Origin of Quake3's Fast InvSqrt() - Part Two, Beyond3D (2006)](https://www.beyond3d.com/content/articles/15)
7. [Why I Love: Fast Inverse Square Root, ocathain.ie](https://ocathain.ie/articles/why-i-love-fast-inverse-square-root)
8. [Lomont, C., Fast Inverse Square Root (2003)](https://www.lomont.org/papers/2003/InvSqrt.pdf)
9. [Day, M., Generalising the Fast Reciprocal Square Root Algorithm, arXiv:2307.15600 (2023)](https://arxiv.org/abs/2307.15600)
10. [rsqrt intrinsic documentation, BWSL Docs](https://www.bwsl.dev/docs/intrinsics/rsqrt)
11. [Quake III Arena and the fast inverse square root algorithm, Apache Groovy](https://groovy.apache.org/blog/quake3-inverse-square-root)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Computational arithmetic › Floating-point and mixed-precision arithmetic

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/fast-inverse-square-root

English original: https://www.edgepedia.org/fast-inverse-square-root
