1// Copyright 2019 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$assert ELEMENTS_TILE % 16 == 0
7$assert ELEMENTS_TILE >= 16
8$SIMD_TILE = ELEMENTS_TILE // 16
9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10#include <assert.h>
11
12#include <immintrin.h>
13
14#include <xnnpack/intrinsics-polyfill.h>
15#include <xnnpack/raddexpminusmax.h>
16
17
18void xnn_f32_raddexpminusmax_ukernel__avx512f_p5_scalef_x${ELEMENTS_TILE}${"" if ACCUMULATORS == 1 else "_acc%d" % ACCUMULATORS}(
19    size_t elements,
20    const float* input,
21    float* sum,
22    float max)
23{
24  assert(elements % sizeof(float) == 0);
25
26  const __m512 vlog2e = _mm512_set1_ps(0x1.715476p+0f);
27  const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f);
28  const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f);
29
30  const __m512 vc0 = _mm512_set1_ps(1.0f);
31  const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f);
32  const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f);
33  const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f);
34  const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f);
35  const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f);
36
37  const __m512 vi_max = _mm512_set1_ps(max);
38
39  $for K in range(ACCUMULATORS):
40    __m512 vacc${K} = _mm512_setzero_ps();
41  for (; elements >= ${ELEMENTS_TILE} * sizeof(float); elements -= ${ELEMENTS_TILE} * sizeof(float)) {
42    // Load ${ELEMENTS_TILE} (${SIMD_TILE}x16) inputs at a time.
43    const __m512 vi0 = _mm512_loadu_ps(input);
44    $for N in range(1, SIMD_TILE):
45      const __m512 vi${N} = _mm512_loadu_ps(input + ${N * 16});
46    input += ${ELEMENTS_TILE};
47
48    // Subtract maximum input x := i - i_max.
49    $for N in range(SIMD_TILE):
50      const __m512 vx${N} = _mm512_sub_ps(vi${N}, vi_max);
51
52    // Compute reduced argument elements := round(x / log(2)).
53    $for N in range(SIMD_TILE):
54      const __m512 vn${N} = _mm512_roundscale_ps(_mm512_mul_ps(vx${N}, vlog2e), 0);
55
56    // Compute reduced argument t := x - elements * log(2).
57    // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
58    $for N in range(SIMD_TILE):
59      __m512 vt${N} = _mm512_fmadd_ps(vn${N}, vminus_ln2_hi, vx${N});
60
61    $for N in range(SIMD_TILE):
62      vt${N} = _mm512_fmadd_ps(vn${N}, vminus_ln2_lo, vt${N});
63
64    // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
65    $for N in range(SIMD_TILE):
66      __m512 vp${N} = _mm512_fmadd_ps(vc5, vt${N}, vc4);
67
68    $for N in range(SIMD_TILE):
69      vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc3);
70
71    $for N in range(SIMD_TILE):
72      vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc2);
73
74    $for N in range(SIMD_TILE):
75      vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc1);
76
77    $for N in range(SIMD_TILE):
78      vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc0);
79
80    // Reconstruct the final f value:
81    //   f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
82    //     = 2**elements * p
83    $for N in range(SIMD_TILE):
84      const __m512 vf${N} = _mm512_scalef_ps(vp${N}, vn${N});
85
86    // Accumulate computed exponents.
87    $for N in range(SIMD_TILE):
88      vacc${N % ACCUMULATORS} = _mm512_add_ps(vacc${N % ACCUMULATORS}, vf${N});
89  }
90  $if ACCUMULATORS > 1:
91    // Add up all accumulators to vacc0
92    $ACC_SLICE = 1
93    $while ACC_SLICE < ACCUMULATORS:
94      $for A in range(0, ACCUMULATORS, ACC_SLICE * 2):
95        $if A + ACC_SLICE < ACCUMULATORS:
96          vacc${A} = _mm512_add_ps(vacc${A}, vacc${A + ACC_SLICE});
97      $ACC_SLICE *= 2
98
99  __m512 vacc = vacc0;
100  for (; elements >= 16 * sizeof(float); elements -= 16 * sizeof(float)) {
101    // Load 16 inputs at a time.
102    const __m512 vi = _mm512_loadu_ps(input);
103    input += 16;
104
105    // Subtract maximum input x := i - i_max.
106    const __m512 vx = _mm512_sub_ps(vi, vi_max);
107
108    // Compute reduced argument elements := round(x / log(2)).
109    const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
110
111    // Compute reduced argument t := x - elements * log(2).
112    // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
113    __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
114    vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
115
116    // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
117    __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
118    vp = _mm512_fmadd_ps(vp, vt, vc3);
119    vp = _mm512_fmadd_ps(vp, vt, vc2);
120    vp = _mm512_fmadd_ps(vp, vt, vc1);
121    vp = _mm512_fmadd_ps(vp, vt, vc0);
122
123    // Reconstruct the final f value:
124    //   f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
125    //     = 2**elements * p
126    const __m512 vf = _mm512_scalef_ps(vp, vn);
127
128    // Accumulate computed exponents.
129    vacc = _mm512_add_ps(vacc, vf);
130  }
131  if (elements != 0) {
132    // Prepare mask for valid 32-bit elements (depends on elements).
133    elements >>= 2 /* log2(sizeof(float)) */;
134    const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << elements) - UINT32_C(1)));
135
136    // Load up to 15 inputs at a time.
137    const __m512 vi = _mm512_maskz_loadu_ps(vmask, input);
138
139    // Subtract maximum input x := i - i_max.
140    const __m512 vx = _mm512_sub_ps(vi, vi_max);
141
142    // Compute reduced argument elements := round(x / log(2)).
143    const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
144
145    // Compute reduced argument t := x - elements * log(2).
146    // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
147    __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
148    vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
149
150    // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
151    __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
152    vp = _mm512_fmadd_ps(vp, vt, vc3);
153    vp = _mm512_fmadd_ps(vp, vt, vc2);
154    vp = _mm512_fmadd_ps(vp, vt, vc1);
155    vp = _mm512_fmadd_ps(vp, vt, vc0);
156
157    // Reconstruct the final f value:
158    //   f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
159    //     = 2**elements * p
160    const __m512 vf = _mm512_scalef_ps(vp, vn);
161
162    // Accumulate computed exponents.
163    vacc = _mm512_mask_add_ps(vacc, vmask, vacc, vf);
164  }
165  *sum = _mm512_reduce_add_ps(vacc);
166}
167