哪种算法用于计算 GNU C++ 标准库的指数函数?
Which algorithm is used to compute exponential functions the GNU C++ Standard Library?
请考虑std::exp defined in the header cmath in C++ numerics library. Now, please consider an implementation of the C++ Standard Library, say libstdc++。
考虑到有多种算法可以计算初等函数,例如arithmetic-geometric mean iteration algorithm to compute the exponential function and three others shown here;
如果可能的话,您能否说出用于计算 libstdc++ 中指数函数的特定算法?
PS:恐怕我无法确定包含 std::exp 实现的正确 tarball 或理解相关文件内容。
它根本不使用任何复杂的算法。请注意,std::exp
仅针对非常有限数量的类型定义:float
、double
和 long double
+ 任何可转换为 double
的整数类型。这使得无需执行复杂的数学运算。
目前,它使用内置的 __builtin_expf
可以从源代码中验证。这会编译为在我的机器上对 expf
的调用,这是对来自 glibc
的对 libm
的调用。让我们看看我们在他们的 source code. When we search for expf
we find that this internally calls __ieee754_expf
which is a system-dependant implementation. Both i686 and x86_64 just include a glibc/sysdeps/ieee754/flt-32/e_expf.c
which finally gives us an implementation (reduced for brevity, the look into the sources
中找到了什么
它基本上是浮点数的 3 阶多项式逼近:
static inline uint32_t
top12 (float x)
{
return asuint (x) >> 20;
}
float
__expf (float x)
{
uint64_t ki, t;
/* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
double_t kd, xd, z, r, r2, y, s;
xd = (double_t) x;
// [...] skipping fast under/overflow handling
/* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k. */
z = InvLn2N * xd;
/* Round and convert z to int, the result is in [-150*N, 128*N] and
ideally ties-to-even rule is used, otherwise the magnitude of r
can be bigger which gives larger approximation error. */
kd = roundtoint (z);
ki = converttoint (z);
r = z - kd;
/* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
t = T[ki % N];
t += ki << (52 - EXP2F_TABLE_BITS);
s = asdouble (t);
z = C[0] * r + C[1];
r2 = r * r;
y = C[2] * r + 1;
y = z * r2 + y;
y = y * s;
return (float) y;
}
同样,对于 128 位 long double
,这是我现在无法理解的 order 7 approximation and for double
they use more complicated algorithm。
请考虑std::exp defined in the header cmath in C++ numerics library. Now, please consider an implementation of the C++ Standard Library, say libstdc++。
考虑到有多种算法可以计算初等函数,例如arithmetic-geometric mean iteration algorithm to compute the exponential function and three others shown here;
如果可能的话,您能否说出用于计算 libstdc++ 中指数函数的特定算法?
PS:恐怕我无法确定包含 std::exp 实现的正确 tarball 或理解相关文件内容。
它根本不使用任何复杂的算法。请注意,std::exp
仅针对非常有限数量的类型定义:float
、double
和 long double
+ 任何可转换为 double
的整数类型。这使得无需执行复杂的数学运算。
目前,它使用内置的 __builtin_expf
可以从源代码中验证。这会编译为在我的机器上对 expf
的调用,这是对来自 glibc
的对 libm
的调用。让我们看看我们在他们的 source code. When we search for expf
we find that this internally calls __ieee754_expf
which is a system-dependant implementation. Both i686 and x86_64 just include a glibc/sysdeps/ieee754/flt-32/e_expf.c
which finally gives us an implementation (reduced for brevity, the look into the sources
它基本上是浮点数的 3 阶多项式逼近:
static inline uint32_t
top12 (float x)
{
return asuint (x) >> 20;
}
float
__expf (float x)
{
uint64_t ki, t;
/* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
double_t kd, xd, z, r, r2, y, s;
xd = (double_t) x;
// [...] skipping fast under/overflow handling
/* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k. */
z = InvLn2N * xd;
/* Round and convert z to int, the result is in [-150*N, 128*N] and
ideally ties-to-even rule is used, otherwise the magnitude of r
can be bigger which gives larger approximation error. */
kd = roundtoint (z);
ki = converttoint (z);
r = z - kd;
/* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
t = T[ki % N];
t += ki << (52 - EXP2F_TABLE_BITS);
s = asdouble (t);
z = C[0] * r + C[1];
r2 = r * r;
y = C[2] * r + 1;
y = z * r2 + y;
y = y * s;
return (float) y;
}
同样,对于 128 位 long double
,这是我现在无法理解的 order 7 approximation and for double
they use more complicated algorithm。