std::fma, std::fmaf, std::fmal
定义于头文件 <cmath> |
||
(1) | ||
float fma ( float x, float y, float z ); double fma ( double x, double y, double z ); |
(始于 C++11) (止于 C++23) |
|
constexpr /* floating-point-type */ fma ( /* floating-point-type */ x, |
(始于 C++23) | |
float fmaf( float x, float y, float z ); |
(2) | (始于 C++11) (constexpr 始于 C++23) |
long double fmal( long double x, long double y, long double z ); |
(3) | (始于 C++11) (constexpr 始于 C++23) |
#define FP_FAST_FMA /* implementation-defined */ |
(4) | (始于 C++11) |
#define FP_FAST_FMAF /* implementation-defined */ |
(5) | (始于 C++11) |
#define FP_FAST_FMAL /* implementation-defined */ |
(6) | (始于 C++11) |
定义于头文件 <cmath> |
||
template< class Arithmetic1, class Arithmetic2, class Arithmetic3 > /* common-floating-point-type */ |
(A) | (始于 C++11) (constexpr 始于 C++23) |
std::fma
的重载,作为参数 x、y 和 z 的类型。(始于 C++23)std::fma
的求值速度比表达式 x * y + z 更快(也更精确)。如果已定义,这些宏求值为整数 1。内容 |
[edit] 参数
x, y, z | - | 浮点值或整数值 |
[edit] 返回值
如果成功,则返回 x * y + z 的值,如同以无限精度计算,且仅舍入一次以适应结果类型(或者,作为替代方案,计算为单个三元浮点运算)。
如果发生由于溢出导致的范围错误,则返回 ±HUGE_VAL、±HUGE_VALF
或 ±HUGE_VALL
。
如果发生由于下溢导致的范围错误,则返回正确的值(舍入后)。
[edit] 错误处理
错误按照 math_errhandling 中的指定报告。
如果实现支持 IEEE 浮点算术(IEC 60559),
- 如果 x 为零且 y 为无穷大,或者 x 为无穷大且 y 为零,且
- 如果 z 不是 NaN,则返回 NaN 并引发 FE_INVALID,
- 如果 z 是 NaN,则返回 NaN 且可能引发 FE_INVALID。
- 如果 x * y 是精确的无穷大,且 z 是符号相反的无穷大,则返回 NaN 并引发 FE_INVALID。
- 如果 x 或 y 是 NaN,则返回 NaN。
- 如果 z 是 NaN,且 x * y 不是 0 * Inf 或 Inf * 0,则返回 NaN(不带 FE_INVALID)。
[edit] 注释
此操作通常在硬件中作为 融合乘加 CPU 指令实现。如果硬件支持,则应定义相应的 FP_FAST_FMA? 宏,但即使未定义宏,许多实现也使用 CPU 指令。
POSIX (fma
, fmaf
, fmal
) 此外还规定,指定返回 FE_INVALID 的情况是域错误。
由于其无限的中间精度,std::fma
是其他正确舍入的数学运算的常用构建块,例如 std::sqrt 甚至除法(在 CPU 未提供的情况下,例如 Itanium)。
与所有浮点表达式一样,除非 #pragma STDC FP_CONTRACT 关闭,否则表达式 x * y + z 可以编译为融合乘加。
附加重载不需要完全按照 (A) 的形式提供。它们只需要足以确保对于它们的第一个参数 num1、第二个参数 num2 和第三个参数 num3
|
(止于 C++23) |
如果 num1、num2 和 num3 具有算术类型,则 std::fma(num1, num2, num3) 具有与 std::fma(static_cast</*common-floating-point-type*/>(num1), 如果不存在具有最高等级和子等级的此类浮点类型,则重载解析不会从提供的重载中产生可用的候选项。 |
(始于 C++23) |
[edit] 示例
#include <cfenv> #include <cmath> #include <iomanip> #include <iostream> #ifndef __GNUC__ #pragma STDC FENV_ACCESS ON #endif int main() { // demo the difference between fma and built-in operators const double in = 0.1; std::cout << "0.1 double is " << std::setprecision(23) << in << " (" << std::hexfloat << in << std::defaultfloat << ")\n" << "0.1*10 is 1.0000000000000000555112 (0x8.0000000000002p-3), " << "or 1.0 if rounded to double\n"; const double expr_result = 0.1 * 10 - 1; const double fma_result = std::fma(0.1, 10, -1); std::cout << "0.1 * 10 - 1 = " << expr_result << " : 1 subtracted after intermediate rounding\n" << "fma(0.1, 10, -1) = " << std::setprecision(6) << fma_result << " (" << std::hexfloat << fma_result << std::defaultfloat << ")\n\n"; // fma is used in double-double arithmetic const double high = 0.1 * 10; const double low = std::fma(0.1, 10, -high); std::cout << "in double-double arithmetic, 0.1 * 10 is representable as " << high << " + " << low << "\n\n"; // error handling std::feclearexcept(FE_ALL_EXCEPT); std::cout << "fma(+Inf, 10, -Inf) = " << std::fma(INFINITY, 10, -INFINITY) << '\n'; if (std::fetestexcept(FE_INVALID)) std::cout << " FE_INVALID raised\n"; }
可能的输出
0.1 double is 0.10000000000000000555112 (0x1.999999999999ap-4) 0.1*10 is 1.0000000000000000555112 (0x8.0000000000002p-3), or 1.0 if rounded to double 0.1 * 10 - 1 = 0 : 1 subtracted after intermediate rounding fma(0.1, 10, -1) = 5.55112e-17 (0x1p-54) in double-double arithmetic, 0.1 * 10 is representable as 1 + 5.55112e-17 fma(+Inf, 10, -Inf) = -nan FE_INVALID raised
[edit] 参见
(C++11)(C++11)(C++11) |
除法运算的带符号余数 (函数) |
(C++11)(C++11)(C++11) |
带符号余数以及除法运算的最后三位 (函数) |
C 文档 for fma
|