xref: /aosp_15_r20/external/XNNPACK/src/math/expm1minus-f16-avx2-rr1-p3.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Copyright 2022 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <assert.h>
7 #include <stddef.h>
8 
9 #include <immintrin.h>
10 
11 #include <xnnpack/math-stubs.h>
12 
13 
xnn_math_f16_expm1minus__avx2_rr1_p3(size_t n,const void * input,void * output)14 void xnn_math_f16_expm1minus__avx2_rr1_p3(
15     size_t n,
16     const void* input,
17     void* output)
18 {
19   assert(n % (8 * sizeof(uint16_t)) == 0);
20 
21   // The largest x for which expm1f(x) is saturated at -1.0f.
22   const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.0A4000p+3f);
23   // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22.
24   const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
25   const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f);
26   const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
27   // Coefficient of polynomial approximation
28   //   exp(t) - 1 ~ t * (1 + t * (c2 + t * c3))
29   // on [-log(2)/2, log(2)/2]
30   const __m256 vc3 = _mm256_set1_ps(0x1.5554DCp-3f);
31   const __m256 vc2 = _mm256_set1_ps(0x1.01EBB2p-1f);
32   const __m256 vc1 = _mm256_set1_ps(0x1.0002F2p0f);
33   const __m256 vone = _mm256_set1_ps(1.0f);
34 
35   const uint16_t* i = (const uint16_t*) input;
36   uint16_t* o = (uint16_t*) output;
37   for (; n != 0; n -= 8 * sizeof(uint16_t)) {
38     __m256 vx = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) i));
39     i += 8;
40 
41     // The function saturates at -1 for large negative inputs: expm1h(x) == -1.0h for x <= sat_cutoff ~= -8.3203125.
42     // To guarantee this behaviour, we clip input at sat_cutoff, and leverage the fact that for our implementation
43     // expm1m(sat_cutoff) == -1.0f. NaN inputs are passed unchanged.
44     vx = _mm256_max_ps(vx, vsat_cutoff);
45 
46     // Compute reduced argument n := round(x / log(2)).
47     // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing
48     // the large number back. The addition is combined with multiplication by log2e into a single FMA instruction. The
49     // trick with adding large number is valid only within certain bounds (|x / log(2)| <= 2**9, i.e.
50     // |x| <= 0x1.630p+8 = 355.0), but that is acceptable, because inputs x are restricted to [-8.3203125, 0].
51     // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range.
52     __m256 vn = _mm256_fmadd_ps(vx, vlog2e, vmagic_bias);
53 
54     // Create a floating-point number s (scale) such that s == 2**n for valid inputs, i.e.
55     // -8.3203125 <= x <= 0.0, and -12 <= n <= 0 accordingly.
56     // For NaN inputs, s would have zero mantissa and can have arbitrary sign and exponent, depending on the input
57     // NaN payload. In these cases, n and t are NaNs with the same payload as input while s is non-NaN, and thus
58     // input payload would be propagated in all computations.
59     __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
60 
61     // Subtract the large number back to get final n := round(x / log(2)).
62     vn = _mm256_sub_ps(vn, vmagic_bias);
63 
64     // Compute reduced argument t := x - n * log(2).
65     __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vx);
66 
67     // Compute degree-3 polynomial approximation for exp(t) - 1 on [-log(2)/2, log(2)/2].
68     //   P(t) = t * (c1 + t * (c2 + t * c3))
69     //        = t * p
70     __m256 vp = _mm256_fmadd_ps(vc3, vt, vc2);
71     vp = _mm256_fmadd_ps(vp, vt, vc1);
72 
73     // Reconstruct the exp(x) - 1 value:
74     //   exp(x) - 1 = s * (1 + t * p) - 1
75     //              = (s - 1) + (s * t) * p
76     //              = (t * s) * p + (s - 1)
77     vt = _mm256_mul_ps(vt, vs);
78     vs = _mm256_sub_ps(vs, vone);
79     const __m256 vf = _mm256_fmadd_ps(vp, vt, vs);
80 
81     _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vf, _MM_FROUND_NO_EXC));
82     o += 8;
83   }
84 }
85