# 饱和算术

饱和算术是计算机算术的一种形式，其中运算结果若超出可表示范围，则返回最接近的边界值——即该类型可表示的最大数或最小数——而不是回绕或发出错误信号。<sup>[1](https://open-std.org/JTC1/SC22/WG21/docs/papers/2023/p0543r3.html)</sup> 它是数字信号处理硬件和 SIMD 指令集中的标准溢出行为，并且在 Rust 等语言中作为显式选项提供，自 C++26 起也进入了 C++ 标准库。

| 关键事实 | 详情 |
|---|---|
| 定义 | 运算按无限精度计算；若真实结果不可表示，则返回该类型的最大值或最小值中较接近者<sup>[1](https://open-std.org/JTC1/SC22/WG21/docs/papers/2023/p0543r3.html)</sup><sup> • </sup><sup>[2](https://www.cppreference.com/Template%3Acpp/numeric/op_sat)</sup> |
| 示例 | 在 16 位有符号算术中，32767 + 1 在二进制补码回绕下变为 −32768，但在饱和算术下饱和为 32767<sup>[3](https://lay007.github.io/zynq-sdr-course/en/block01/18-fixed-point-effects/)</sup> |
| 硬件支持 | ARM 高级 SIMD 饱和指令（Q 前缀）饱和至类型相关的限值并置位 QC 状态标志；x86 提供打包饱和加法指令，如 PADDSB/PADDSW 和 PADDUSB/PADDUSW<sup>[4](https://documentation-service.arm.com/static/5f3e8d48b13d4764d4613a13?token=)</sup><sup> • </sup><sup>[5](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-2a-manual.pdf)</sup> |
| WebAssembly SIMD | 饱和整数加减法仅对 8 位和 16 位通道定义，分有符号（`add_sat_s`）和无符号（`add_sat_u`）两种形式<sup>[6](https://github.com/WebAssembly/spec/blob/main/proposals/simd/SIMD.md)</sup> |
| C++26 | `<numeric>` 中的 `saturating_add`、`saturating_sub`、`saturating_mul`、`saturating_div` 和 `saturating_cast`，特性测试宏 `__cpp_lib_saturation_arithmetic` = 202311L<sup>[2](https://www.cppreference.com/Template%3Acpp/numeric/op_sat)</sup> |
| Rust | 整数原生类型上的 `saturating_add` 及相关方法，以及 `Saturating<T>` 包装类型（于 Rust 1.74.0 稳定），后者为所有标准运算赋予饱和语义<sup>[7](https://doc.rust-lang.org/core/num/saturating/struct.Saturating.html)</sup> |
| 主要用途 | 音频、控制和通信信号处理，其中饱和远优于回绕，因为削顶的结果仍接近真实值<sup>[8](https://ns1.dsponlineconference.com/glossary/fixed-point)</sup> |

## 工作原理

在回绕（模）算术中，超出数据范围的结果只保留低位，因此数值会从范围的另一端重新出现。饱和则进行钳位：任何高于最大值的结果变为最大值，任何低于最小值的结果变为最小值。对于位宽为 w 的无符号整数，范围为 0 到 2^w − 1；对于有符号二进制补码类型，限值为 −2^(w−1) 和 2^(w−1) − 1。<sup>[9](https://p4.org/wp-content/uploads/sites/53/2024/10/P4-16-spec-v1.2.5.pdf)</sup>

C++ 标准将语义描述为按无限范围执行的运算：数学结果 q 在可表示时返回，否则返回该类型的最大值或最小值中较接近 q 者。<sup>[2](https://www.cppreference.com/Template%3Acpp/numeric/op_sat)</sup> C++ 饱和提案给出了像素颜色的动机示例：提亮一个白色像素应得到白色，而不是回绕所产生的黑色或深灰色像素。<sup>[1](https://open-std.org/JTC1/SC22/WG21/docs/papers/2023/p0543r3.html)</sup>

饱和不仅适用于加减法。左移相当于乘以 2 的幂，同样可能溢出：将 Q15 定点值 24576（约 0.75）左移一位得到 49152，超出 32767 的限值，无饱和时会回绕为 −16384，有饱和时则钳位为 32767。<sup>[10](https://doppler-dsp.github.io/doppler/guide/fixed-point/)</sup> 饱和转换在整数类型之间转换数值时钳位至目标类型的范围。<sup>[2](https://www.cppreference.com/Template%3Acpp/numeric/op_sat)</sup>

## 信号处理为何偏好饱和

实际论据在于误差的形态。当 8 位有符号运算的正确结果为 130 时，饱和返回 127，接近真实值；模算术返回 −126，符号和量级都错了。对于无符号 8 位算术，正确结果 258 饱和为 255，而不是回绕为 2。<sup>[9](https://p4.org/wp-content/uploads/sites/53/2024/10/P4-16-spec-v1.2.5.pdf)</sup> 在音频中，溢出的样本回绕会产生一个符号相反的大数值，听起来是响亮的咔嗒声，而饱和的样本只是在最大值处削顶。<sup>[11](https://karam-nus.github.io/chips/18_dsps)</sup>

饱和算术在 ARMv7-M 内核（如 Cortex-M3 和 Cortex-M4，提供 SSAT、USAT，并在存在 DSP 扩展时提供 QADD）以及许多 DSP 内核上作为硬件指令提供，在处理音频、控制或通信信号时远优于回绕。<sup>[8](https://ns1.dsponlineconference.com/glossary/fixed-point)</sup> 数字信号处理器通常将其作为乘累加单元的硬件模式提供，有时也用于其他运算；饱和还可以防止递归算法中的寄生振荡。<sup>[12](https://www.diva-portal.org/smash/get/diva2%3A24168/FULLTEXT01.pdf)</sup> 一项已发表的准则表明，饱和算术可以消除数字滤波器中的一类溢出振荡——这是一种失效模式，滤波器被推入溢出后，即使输入恢复正常仍持续振荡。<sup>[13](https://www.sciencedirect.com/science/article/pii/S105120040500031X)</sup>

当硬件饱和不可用时，同样的行为需要额外的软件开销，产生更大更慢的程序；一些设计为安全性接受这一代价，另一些则接受回绕以保持软件最小最快。<sup>[14](https://www.mathworks.com/help/fixedpoint/ug/saturation-and-wrapping.html)</sup> Simulink 通过“整数溢出时饱和”复选框为每个模块暴露该选择。<sup>[14](https://www.mathworks.com/help/fixedpoint/ug/saturation-and-wrapping.html)</sup>

## 硬件与指令集支持

**ARM。** 高级 SIMD 饱和指令以 Q 前缀书写，在 A32/T32 中插入 V 与助记符之间，在 A64 中插入 S 或 U 与助记符之间。饱和限值取决于指令的数据类型：有符号字节结果饱和至 −2^7 ≤ x < 2^7，有符号半字至 −2^15 ≤ x < 2^15，直至有符号双字 −2^63 ≤ x < 2^63，无符号类型范围为 0 到 2^宽度 − 1。饱和 SIMD 算术置位浮点状态寄存器（AArch32 中为 FPSCR，AArch64 中为 FPSR）中的 QC 位，以指示发生了饱和。<sup>[4](https://documentation-service.arm.com/static/5f3e8d48b13d4764d4613a13?token=)</sup> 在 AArch64 通用寄存器代码中没有饱和算术指令，Q 标志也无法在那里读写；只有 SIMD 指令会置位 QC。<sup>[4](https://documentation-service.arm.com/static/5f3e8d48b13d4764d4613a13?token=)</sup>

**x86。** 英特尔的打包整数指令集自 MMX 起就包含饱和运算：PADDSB 和 PADDSW 对有符号字节和字进行带符号饱和的打包加法，PADDUSB 和 PADDUSW 对无符号值进行无符号饱和的打包加法，并有对应的饱和减法。<sup>[5](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-2a-manual.pdf)</sup>

**WebAssembly。** SIMD 提案仅为 8 位和 16 位通道定义饱和加减法，分有符号和无符号变体，另有 Q15 格式的饱和舍入乘法。有符号饱和钳位至通道的有符号最小值和最大值；无符号饱和将负结果钳位为 0，将超过无符号最大值的结果钳位为该最大值。<sup>[6](https://github.com/WebAssembly/spec/blob/main/proposals/simd/SIMD.md)</sup>

宽度限制反映了一个普遍模式：用于像素和音频样本的窄通道具有廉价的饱和硬件，而宽标量类型通常没有。一份 C++ 提案报告展示了生成代码中的后果：16 位饱和加法编译为单条原生 `vpaddsw` 指令，而 32 位饱和加法必须由普通加法、比较和符号扩展指令合成。<sup>[15](https://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2025/p2956r1.html)</sup>

## 语言支持

**Rust** 将饱和作为每个整数原生类型的显式方法暴露，如 `u32::saturating_add`，同时还有 `wrapping_add`、`checked_add` 和 `overflowing_add` 变体对应其他溢出行为。<sup>[16](https://doc.rust-lang.org/core/intrinsics/fn.saturating_add.html)</sup><sup> • </sup><sup>[7](https://doc.rust-lang.org/core/num/saturating/struct.Saturating.html)</sup> 对于需要全程饱和的代码，标准库提供了 `Saturating<T>` 包装类型，于 Rust 1.74.0 稳定，使包装值的所有标准算术运算都饱和；底层值通过 `.0` 访问。<sup>[7](https://doc.rust-lang.org/core/num/saturating/struct.Saturating.html)</sup> Rust 的默认整数算术从不使行为未定义：它要么回绕，要么在调试配置中检测溢出并触发恐慌，因此需要饱和的代码必须显式选择。<sup>[7](https://doc.rust-lang.org/core/num/saturating/struct.Saturating.html)</sup>

**C++** 正在为 C++26 向标准库添加饱和算术，位于 `<numeric>` 头文件中：`saturating_add`、`saturating_sub`、`saturating_mul`、`saturating_div` 和 `saturating_cast`，由特性测试宏 `__cpp_lib_saturation_arithmetic`（值为 202311L）保护。<sup>[2](https://www.cppreference.com/Template%3Acpp/numeric/op_sat)</sup> 与内置算术运算符不同，这些函数不对参数应用整型提升，传入两个不同类型的参数会编译失败而非静默提升。<sup>[1](https://open-std.org/JTC1/SC22/WG21/docs/papers/2023/p0543r3.html)</sup> GCC 的 libstdc++ 在编译器内置的溢出检查算术基础上实现这些函数，无符号溢出返回类型最大值，有符号溢出根据操作数符号返回最小值或最大值。<sup>[17](https://gcc.gnu.org/onlinedocs/gcc-16.1.0/libstdc%2B%2B/api/a00812_source.html)</sup> 后续提案将同样的运算逐元素扩展到 `std::simd` 数据并行类型。<sup>[15](https://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2025/p2956r1.html)</sup>

## 设计与综合

饱和在硬件中并非免费，设计者需在成本与字长之间权衡。伦敦帝国理工学院的 George Constantinides、Peter Cheung 和 Wayne Luk 提出的一种综合方法学为数字滤波器等线性 DSP 系统自动化这一选择：它解析地估计候选实现的饱和误差，并优化出满足用户指定的饱和与舍入误差界限的最小电路。在 FPGA 实现上，该方法报告相比标准 DSP 设计技术面积改善高达 22%，速度改善高达 28%。<sup>[18](https://cir.nii.ac.jp/crid/1363388844547966208)</sup>

## References

1. [P0543R3: Saturation arithmetic, ISO/IEC JTC1/SC22/WG21](https://open-std.org/JTC1/SC22/WG21/docs/papers/2023/p0543r3.html)
2. [std::op_sat (Saturation arithmetic, C++26), cppreference.com](https://www.cppreference.com/Template%3Acpp/numeric/op_sat)
3. [Fixed-Point Effects in DSP and FPGA, Zynq SDR Course](https://lay007.github.io/zynq-sdr-course/en/block01/18-fixed-point-effects/)
4. [ARM Compiler armasm User Guide, section 11.18 Saturating Advanced SIMD instructions](https://documentation-service.arm.com/static/5f3e8d48b13d4764d4613a13?token=)
5. [Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 2A](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-2a-manual.pdf)
6. [WebAssembly SIMD proposal specification](https://github.com/WebAssembly/spec/blob/main/proposals/simd/SIMD.md)
7. [Saturating in core::num, Rust standard library documentation](https://doc.rust-lang.org/core/num/saturating/struct.Saturating.html)
8. [Fixed-Point Arithmetic, DSPRelated glossary](https://ns1.dsponlineconference.com/glossary/fixed-point)
9. [P4-16 Language Specification v1.2.5, P4.org](https://p4.org/wp-content/uploads/sites/53/2024/10/P4-16-spec-v1.2.5.pdf)
10. [Saturation arithmetic, a closer look, doppler fixed-point guide](https://doppler-dsp.github.io/doppler/guide/fixed-point/)
11. [Chapter 18, DSPs (Digital Signal Processors), The Complete Guide to Chips](https://karam-nus.github.io/chips/18_dsps)
12. [Overflow effects in DSP processors, DiVA report](https://www.diva-portal.org/smash/get/diva2%3A24168/FULLTEXT01.pdf)
13. [Elimination of overflow oscillations in digital filters employing saturation arithmetic, Digital Signal Processing 15(6), 2005](https://www.sciencedirect.com/science/article/pii/S105120040500031X)
14. [Saturation and Wrapping, MATLAB & Simulink fixed-point documentation](https://www.mathworks.com/help/fixedpoint/ug/saturation-and-wrapping.html)
15. [P2956R1: Allow std::simd overloads for saturating operations, ISO/IEC JTC1/SC22/WG21](https://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2025/p2956r1.html)
16. [saturating_add in core::intrinsics, Rust documentation](https://doc.rust-lang.org/core/intrinsics/fn.saturating_add.html)
17. [libstdc++ sat_arith.h source, GCC 16.1 documentation](https://gcc.gnu.org/onlinedocs/gcc-16.1.0/libstdc%2B%2B/api/a00812_source.html)
18. [Constantinides, Cheung, and Luk (2003). Synthesis of saturation arithmetic architectures, ACM TODAES 8(3), 334–354](https://cir.nii.ac.jp/crid/1363388844547966208)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Computational arithmetic › Saturation arithmetic

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/saturation-arithmetic

English original: https://www.edgepedia.org/saturation-arithmetic
