Skip to content

Vector exp on SIMD lanes

mtl::simd::exp computes e^x on batch<float> and batch<double> lanes, and over an array. It removes the scalar std::exp per element from kernels such as softmax (issue #654). The first user is branes-ai/synapse: there, scalar std::exp was about a third of the attention kernel’s time.

#include <mtl/simd/math.hpp>
auto y = mtl::simd::exp(mtl::simd::batch<float>::load_unaligned(x)); // one batch
mtl::simd::exp(x, out, n); // an array; x == out is allowed
auto m = max(a, b); // lane-wise, found by ADL

exp is written once, on batch operations and a few internal lane operations: round to nearest, 2^k from the exponent bits, and selects on a comparison. On the Highway backend and on the portable scalar backend, each lane therefore runs the same sequence of operations.

The two backends agree bit for bit wherever both fuse fma.

  • Checked on an i7-12700K, over 1,074,578 inputs in float and double, between three builds: the scalar build at baseline x86-64 (software fma), the scalar build with -march=native, and the Highway AVX2 build.
  • Highway’s SSE4 target has no hardware FMA, so its fma is a multiply and an add, and 491 of those results differed in the last bit. This is the position fma itself takes as “the single FMA decision point”.

Each lane is independent of the others. A result does not depend on its lane, on the batch width, or on how an array is split across threads or tiles. The array form sends its last n mod W elements through a batch padded with zeros, so every element takes the same per-lane path.

The algorithm is Cephes’ expf and exp (S. Moshier):

  1. Clamp x to [lo, hi], the range where e^x is neither 0 nor infinite. Lanes outside it, and NaN lanes, are replaced at the end.
  2. Reduce the argument. k = round(x · log₂e), ties to even. Then r = x − k ln 2, by Cody–Waite with ln 2 = c₁ + c₂, where c₁ is short enough that k c₁ is exact. This gives |r| ≤ ln 2 / 2.
  3. Approximate e^r.
    • float: 1 + r + r² P(r), with P of degree 5.
    • double: the rational form 1 + 2 r P(r²) / (Q(r²) − r P(r²)).
  4. Scale by 2^k as 2^k₁ · 2^k₂, with k₁ = round(k/2).
    • Both factors are normal numbers over the whole range. 2^128 is not a float, and 2^-149 is not a normal one.
    • Each factor is built from the exponent bits.
    • The product rounds once, in the last multiplication, which also rounds subnormal results correctly.

Measured in ULPs of the result, or of the subnormal spacing for a subnormal result, against a reference in a wider type: double for float, long double for double. tests/unit/simd/test_exp.cpp asserts the bounds.

lanesboundlargest measuredsample
float< 1.5 ULP1.003 ULPevery 61st float bit pattern in range, and 20,000 around each boundary
double< 2 ULP1.66 ULP500,000 random values, and 20,000 around each boundary

The results are not correctly rounded.

xexp(x)
+inf+inf
-inf+0
NaNthe input NaN
x > hi (88.7228317 for float, 709.782712893384 for double)+inf
x < lo (−103.972076 for float, −745.133219101941 for double)+0
lo ≤ x with e^x below the normal rangesubnormal, not flushed

hi is the largest x whose exponential rounds below infinity, and lo the smallest whose exponential rounds to a nonzero subnormal. Both were computed with mpmath at 300 bits.

These hold in IEEE arithmetic. Under -ffast-math (-ffinite-math-only) the compiler may assume that no NaN or infinity occurs, and the NaN and overflow selects are then not guaranteed.

min(a, b) and max(a, b) follow std::min and std::max:

  • min(a, b) is b where b < a, and a otherwise.
  • max(a, b) is b where a < b, and a otherwise.

So a NaN in b gives a, and a NaN in a gives NaN.

The rule is written as a compare and a select, rather than the ISA’s min and max. On NaN those differ between targets: x86 returns the second operand, and NEON returns NaN. They are hidden friends of batch, so a call is unqualified and found by ADL. Softmax can use max to keep a running maximum per lane, instead of a horizontal reduce for each vector.

Measured on an i7-12700K P-core, with 16,384 floats in [−30, 10]:

buildsimd::expstd::exp (glibc, scalar)
Highway AVX2, 8 lanes0.50 ns/element1.44 ns/element
scalar fallback, -march=native2.8 ns/element1.43 ns/element

The scalar fallback is slower than glibc’s expf. It is there to give the same results, not to be fast. A kernel that needs the speed builds with -DMTL5_WITH_HIGHWAY=ON.

A subnormal result is slow on x86. Producing one takes a microcode assist of about 150 cycles. Over the whole range [−104, 89], where 9% of the results are subnormal, the AVX2 time rises to 2.7 ns/element, against 2.3 for glibc.