• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-vscaleextexp/avx512f-p5-scalef.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2019 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9 
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 
xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x48(size_t elements,const float * x,float * y,float scale_value,float scale_exp)19 void xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x48(
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 >= 48 * sizeof(float); elements -= 48 * sizeof(float)) {
43     // Load 48 (3x16) inputs at a time.
44     const __m512 vx0 = _mm512_loadu_ps(x);
45     const __m512 vx1 = _mm512_loadu_ps(x + 16);
46     const __m512 vx2 = _mm512_loadu_ps(x + 32);
47     x += 48;
48 
49     // Compute reduced argument elements := round(x / log(2)).
50     const __m512 vn0 = _mm512_roundscale_ps(_mm512_mul_ps(vx0, vlog2e), 0);
51     const __m512 vn1 = _mm512_roundscale_ps(_mm512_mul_ps(vx1, vlog2e), 0);
52     const __m512 vn2 = _mm512_roundscale_ps(_mm512_mul_ps(vx2, vlog2e), 0);
53 
54     // Compute reduced argument t := x - elements * log(2).
55     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
56     __m512 vt0 = _mm512_fmadd_ps(vn0, vminus_ln2_hi, vx0);
57     __m512 vt1 = _mm512_fmadd_ps(vn1, vminus_ln2_hi, vx1);
58     __m512 vt2 = _mm512_fmadd_ps(vn2, vminus_ln2_hi, vx2);
59 
60     vt0 = _mm512_fmadd_ps(vn0, vminus_ln2_lo, vt0);
61     vt1 = _mm512_fmadd_ps(vn1, vminus_ln2_lo, vt1);
62     vt2 = _mm512_fmadd_ps(vn2, vminus_ln2_lo, vt2);
63 
64     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
65     __m512 vp0 = _mm512_fmadd_ps(vc5, vt0, vc4);
66     __m512 vp1 = _mm512_fmadd_ps(vc5, vt1, vc4);
67     __m512 vp2 = _mm512_fmadd_ps(vc5, vt2, vc4);
68 
69     vp0 = _mm512_fmadd_ps(vp0, vt0, vc3);
70     vp1 = _mm512_fmadd_ps(vp1, vt1, vc3);
71     vp2 = _mm512_fmadd_ps(vp2, vt2, vc3);
72 
73     vp0 = _mm512_fmadd_ps(vp0, vt0, vc2);
74     vp1 = _mm512_fmadd_ps(vp1, vt1, vc2);
75     vp2 = _mm512_fmadd_ps(vp2, vt2, vc2);
76 
77     vp0 = _mm512_fmadd_ps(vp0, vt0, vc1);
78     vp1 = _mm512_fmadd_ps(vp1, vt1, vc1);
79     vp2 = _mm512_fmadd_ps(vp2, vt2, vc1);
80 
81     vp0 = _mm512_fmadd_ps(vp0, vt0, vc0);
82     vp1 = _mm512_fmadd_ps(vp1, vt1, vc0);
83     vp2 = _mm512_fmadd_ps(vp2, vt2, vc0);
84 
85     // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation where
86     //  - vnX is "exponent"
87     //  - vpX is "mantissa"
88     //
89     // exp2(ae) * av * exp2(be) * bv =
90     //   = exp2(ae + be) * (av * bv)
91     __m512 vf0 = _mm512_mul_ps(vp0, vscalev);
92     __m512 vf1 = _mm512_mul_ps(vp1, vscalev);
93     __m512 vf2 = _mm512_mul_ps(vp2, vscalev);
94 
95     const __m512 ve0 = _mm512_add_ps(vn0, vscalee);
96     const __m512 ve1 = _mm512_add_ps(vn1, vscalee);
97     const __m512 ve2 = _mm512_add_ps(vn2, vscalee);
98 
99     // Multiply "mantissa" by the exp2("exponent").
100     vf0 = _mm512_scalef_ps(vf0, ve0);
101     vf1 = _mm512_scalef_ps(vf1, ve1);
102     vf2 = _mm512_scalef_ps(vf2, ve2);
103 
104     // Store 128 (8x16) results at a time.
105     _mm512_storeu_ps(y, vf0);
106     _mm512_storeu_ps(y + 0, vf0);
107     _mm512_storeu_ps(y + 16, vf1);
108     _mm512_storeu_ps(y + 32, vf2);
109     y += 48;
110   }
111 
112   for (; elements >= 16 * sizeof(float); elements -= 16 * sizeof(float)) {
113     // Load 16 inputs at a time.
114     const __m512 vx = _mm512_loadu_ps(x);
115     x += 16;
116 
117     // Compute reduced argument elements := round(x / log(2)).
118     const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
119 
120     // Compute reduced argument t := x - elements * log(2).
121     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
122     __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
123     vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
124 
125     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
126     __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
127     vp = _mm512_fmadd_ps(vp, vt, vc3);
128     vp = _mm512_fmadd_ps(vp, vt, vc2);
129     vp = _mm512_fmadd_ps(vp, vt, vc1);
130     vp = _mm512_fmadd_ps(vp, vt, vc0);
131 
132     // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation.
133     __m512 vf = _mm512_mul_ps(vp, vscalev);
134     const __m512 ve = _mm512_add_ps(vn, vscalee);
135 
136     // Multiply "mantissa" by the exp2("exponent").
137     vf = _mm512_scalef_ps(vf, ve);
138 
139     // Store 16 results at a time.
140     _mm512_storeu_ps(y, vf);
141     y += 16;
142   }
143   if XNN_UNLIKELY(elements != 0) {
144     // Prepare mask for valid 32-bit elements (depends on elements).
145     elements >>= 2 /* log2(sizeof(float)) */;
146     const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << elements) - UINT32_C(1)));
147 
148     // Load up to 15 inputs at a time.
149     const __m512 vx = _mm512_maskz_loadu_ps(vmask, x);
150 
151     // Compute reduced argument elements := round(x / log(2)).
152     const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
153 
154     // Compute reduced argument t := x - elements * log(2).
155     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
156     __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
157     vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
158 
159     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
160     __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
161     vp = _mm512_fmadd_ps(vp, vt, vc3);
162     vp = _mm512_fmadd_ps(vp, vt, vc2);
163     vp = _mm512_fmadd_ps(vp, vt, vc1);
164     vp = _mm512_fmadd_ps(vp, vt, vc0);
165 
166     // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation.
167     __m512 vf = _mm512_mul_ps(vp, vscalev);
168     const __m512 ve = _mm512_add_ps(vn, vscalee);
169 
170     // Multiply "mantissa" by the exp2("exponent").
171     vf = _mm512_scalef_ps(vf, ve);
172 
173     // Store up to 15 results at a time.
174     _mm512_mask_storeu_ps(y, vmask, vf);
175   }
176   _mm256_zeroupper();
177 }
178