• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/common.h>
15#include <xnnpack/intrinsics-polyfill.h>
16#include <xnnpack/vscaleextexp.h>
17
18
19void xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x${ELEMENTS_TILE}(
20    size_t elements,
21    const float* x,
22    float* y,
23    float scale_value,
24    float scale_exp)
25{
26  assert(elements % sizeof(float) == 0);
27
28  const __m512 vlog2e = _mm512_set1_ps(0x1.715476p+0f);
29  const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f);
30  const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f);
31
32  const __m512 vc0 = _mm512_set1_ps(1.0f);
33  const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f);
34  const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f);
35  const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f);
36  const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f);
37  const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f);
38
39  const __m512 vscalev = _mm512_set1_ps(scale_value);
40  const __m512 vscalee = _mm512_set1_ps(scale_exp);
41
42  for (; elements >= ${ELEMENTS_TILE} * sizeof(float); elements -= ${ELEMENTS_TILE} * sizeof(float)) {
43    // Load ${ELEMENTS_TILE} (${SIMD_TILE}x16) inputs at a time.
44    const __m512 vx0 = _mm512_loadu_ps(x);
45    $for N in range(1, SIMD_TILE):
46      const __m512 vx${N} = _mm512_loadu_ps(x + ${N * 16});
47    x += ${ELEMENTS_TILE};
48
49    // Compute reduced argument elements := round(x / log(2)).
50    $for N in range(SIMD_TILE):
51      const __m512 vn${N} = _mm512_roundscale_ps(_mm512_mul_ps(vx${N}, vlog2e), 0);
52
53    // Compute reduced argument t := x - elements * log(2).
54    // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
55    $for N in range(SIMD_TILE):
56      __m512 vt${N} = _mm512_fmadd_ps(vn${N}, vminus_ln2_hi, vx${N});
57
58    $for N in range(SIMD_TILE):
59      vt${N} = _mm512_fmadd_ps(vn${N}, vminus_ln2_lo, vt${N});
60
61    // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
62    $for N in range(SIMD_TILE):
63      __m512 vp${N} = _mm512_fmadd_ps(vc5, vt${N}, vc4);
64
65    $for N in range(SIMD_TILE):
66      vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc3);
67
68    $for N in range(SIMD_TILE):
69      vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc2);
70
71    $for N in range(SIMD_TILE):
72      vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc1);
73
74    $for N in range(SIMD_TILE):
75      vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc0);
76
77    // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation where
78    //  - vnX is "exponent"
79    //  - vpX is "mantissa"
80    //
81    // exp2(ae) * av * exp2(be) * bv =
82    //   = exp2(ae + be) * (av * bv)
83    $for N in range(SIMD_TILE):
84      __m512 vf${N} = _mm512_mul_ps(vp${N}, vscalev);
85
86    $for N in range(SIMD_TILE):
87      const __m512 ve${N} = _mm512_add_ps(vn${N}, vscalee);
88
89    // Multiply "mantissa" by the exp2("exponent").
90    $for N in range(SIMD_TILE):
91      vf${N} = _mm512_scalef_ps(vf${N}, ve${N});
92
93    // Store 128 (8x16) results at a time.
94    _mm512_storeu_ps(y, vf0);
95    $for N in range(SIMD_TILE):
96      _mm512_storeu_ps(y + ${N * 16}, vf${N});
97    y += ${ELEMENTS_TILE};
98  }
99
100  for (; elements >= 16 * sizeof(float); elements -= 16 * sizeof(float)) {
101    // Load 16 inputs at a time.
102    const __m512 vx = _mm512_loadu_ps(x);
103    x += 16;
104
105    // Compute reduced argument elements := round(x / log(2)).
106    const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
107
108    // Compute reduced argument t := x - elements * log(2).
109    // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
110    __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
111    vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
112
113    // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
114    __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
115    vp = _mm512_fmadd_ps(vp, vt, vc3);
116    vp = _mm512_fmadd_ps(vp, vt, vc2);
117    vp = _mm512_fmadd_ps(vp, vt, vc1);
118    vp = _mm512_fmadd_ps(vp, vt, vc0);
119
120    // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation.
121    __m512 vf = _mm512_mul_ps(vp, vscalev);
122    const __m512 ve = _mm512_add_ps(vn, vscalee);
123
124    // Multiply "mantissa" by the exp2("exponent").
125    vf = _mm512_scalef_ps(vf, ve);
126
127    // Store 16 results at a time.
128    _mm512_storeu_ps(y, vf);
129    y += 16;
130  }
131  if XNN_UNLIKELY(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 vx = _mm512_maskz_loadu_ps(vmask, x);
138
139    // Compute reduced argument elements := round(x / log(2)).
140    const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
141
142    // Compute reduced argument t := x - elements * log(2).
143    // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
144    __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
145    vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
146
147    // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
148    __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
149    vp = _mm512_fmadd_ps(vp, vt, vc3);
150    vp = _mm512_fmadd_ps(vp, vt, vc2);
151    vp = _mm512_fmadd_ps(vp, vt, vc1);
152    vp = _mm512_fmadd_ps(vp, vt, vc0);
153
154    // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation.
155    __m512 vf = _mm512_mul_ps(vp, vscalev);
156    const __m512 ve = _mm512_add_ps(vn, vscalee);
157
158    // Multiply "mantissa" by the exp2("exponent").
159    vf = _mm512_scalef_ps(vf, ve);
160
161    // Store up to 15 results at a time.
162    _mm512_mask_storeu_ps(y, vmask, vf);
163  }
164  _mm256_zeroupper();
165}
166