• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-vscaleexpminusmax/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/intrinsics-polyfill.h>
15 #include <xnnpack/vscaleexpminusmax.h>
16 
17 
xnn_f32_vscaleexpminusmax_ukernel__avx512f_p5_scalef_x48(size_t elements,const float * input,float * output,float scale,float max)18 void xnn_f32_vscaleexpminusmax_ukernel__avx512f_p5_scalef_x48(
19     size_t elements,
20     const float* input,
21     float* output,
22     float scale,
23     float max)
24 {
25   assert(elements % sizeof(float) == 0);
26 
27   const __m512 vlog2e = _mm512_set1_ps(0x1.715476p+0f);
28   const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f);
29   const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f);
30 
31   const __m512 vc0 = _mm512_set1_ps(1.0f);
32   const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f);
33   const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f);
34   const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f);
35   const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f);
36   const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f);
37 
38   const __m512 vscale = _mm512_set1_ps(scale);
39   const __m512 vi_max = _mm512_set1_ps(max);
40 
41   for (; elements >= 48 * sizeof(float); elements -= 48 * sizeof(float)) {
42     // Load 48 (3x16) inputs at a time.
43     const __m512 vi0 = _mm512_loadu_ps(input);
44     const __m512 vi1 = _mm512_loadu_ps(input + 16);
45     const __m512 vi2 = _mm512_loadu_ps(input + 32);
46     input += 48;
47 
48     // Subtract maximum input x := i - i_max.
49     const __m512 vx0 = _mm512_sub_ps(vi0, vi_max);
50     const __m512 vx1 = _mm512_sub_ps(vi1, vi_max);
51     const __m512 vx2 = _mm512_sub_ps(vi2, vi_max);
52 
53     // Compute reduced argument elements := round(x / log(2)).
54     __m512 vn0 = _mm512_roundscale_ps(_mm512_mul_ps(vx0, vlog2e), 0);
55     __m512 vn1 = _mm512_roundscale_ps(_mm512_mul_ps(vx1, vlog2e), 0);
56     __m512 vn2 = _mm512_roundscale_ps(_mm512_mul_ps(vx2, vlog2e), 0);
57 
58     // Compute reduced argument t := x - elements * log(2).
59     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
60     __m512 vt0 = _mm512_fmadd_ps(vn0, vminus_ln2_hi, vx0);
61     __m512 vt1 = _mm512_fmadd_ps(vn1, vminus_ln2_hi, vx1);
62     __m512 vt2 = _mm512_fmadd_ps(vn2, vminus_ln2_hi, vx2);
63 
64     vt0 = _mm512_fmadd_ps(vn0, vminus_ln2_lo, vt0);
65     vt1 = _mm512_fmadd_ps(vn1, vminus_ln2_lo, vt1);
66     vt2 = _mm512_fmadd_ps(vn2, vminus_ln2_lo, vt2);
67 
68     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
69     __m512 vp0 = _mm512_fmadd_ps(vc5, vt0, vc4);
70     __m512 vp1 = _mm512_fmadd_ps(vc5, vt1, vc4);
71     __m512 vp2 = _mm512_fmadd_ps(vc5, vt2, vc4);
72 
73     vp0 = _mm512_fmadd_ps(vp0, vt0, vc3);
74     vp1 = _mm512_fmadd_ps(vp1, vt1, vc3);
75     vp2 = _mm512_fmadd_ps(vp2, vt2, vc3);
76 
77     vp0 = _mm512_fmadd_ps(vp0, vt0, vc2);
78     vp1 = _mm512_fmadd_ps(vp1, vt1, vc2);
79     vp2 = _mm512_fmadd_ps(vp2, vt2, vc2);
80 
81     vp0 = _mm512_fmadd_ps(vp0, vt0, vc1);
82     vp1 = _mm512_fmadd_ps(vp1, vt1, vc1);
83     vp2 = _mm512_fmadd_ps(vp2, vt2, vc1);
84 
85     vp0 = _mm512_fmadd_ps(vp0, vt0, vc0);
86     vp1 = _mm512_fmadd_ps(vp1, vt1, vc0);
87     vp2 = _mm512_fmadd_ps(vp2, vt2, vc0);
88 
89     // Reconstruct the final f value:
90     //   f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
91     //     = 2**elements * p
92     __m512 vf0 = _mm512_scalef_ps(vp0, vn0);
93     __m512 vf1 = _mm512_scalef_ps(vp1, vn1);
94     __m512 vf2 = _mm512_scalef_ps(vp2, vn2);
95 
96     // Multiply by scale.
97     vf0 = _mm512_mul_ps(vf0, vscale);
98     vf1 = _mm512_mul_ps(vf1, vscale);
99     vf2 = _mm512_mul_ps(vf2, vscale);
100 
101     // Store 48 (3x16) outputs at a time.
102     _mm512_storeu_ps(output, vf0);
103     _mm512_storeu_ps(output + 0, vf0);
104     _mm512_storeu_ps(output + 16, vf1);
105     _mm512_storeu_ps(output + 32, vf2);
106     output += 48;
107   }
108   for (; elements >= 16 * sizeof(float); elements -= 16 * sizeof(float)) {
109     // Load 16 inputs at a time.
110     const __m512 vi = _mm512_loadu_ps(input);
111     input += 16;
112 
113     // Subtract maximum input x := i - i_max.
114     const __m512 vx = _mm512_sub_ps(vi, vi_max);
115 
116     // Compute reduced argument elements := round(x / log(2)).
117     __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
118 
119     // Compute reduced argument t := x - elements * log(2).
120     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
121     __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
122     vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
123 
124     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
125     __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
126     vp = _mm512_fmadd_ps(vp, vt, vc3);
127     vp = _mm512_fmadd_ps(vp, vt, vc2);
128     vp = _mm512_fmadd_ps(vp, vt, vc1);
129     vp = _mm512_fmadd_ps(vp, vt, vc0);
130 
131     // Reconstruct the final f value:
132     //   f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
133     //     = 2**elements * p
134     __m512 vf = _mm512_scalef_ps(vp, vn);
135 
136     // Multiply by scale.
137     vf = _mm512_mul_ps(vf, vscale);
138 
139     // Store 16 outputs at a time.
140     _mm512_storeu_ps(output, vf);
141     output += 16;
142   }
143   if (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 vi = _mm512_mask_loadu_ps(_mm512_undefined_ps(), vmask, input);
150 
151     // Subtract maximum input x := i - i_max.
152     const __m512 vx = _mm512_sub_ps(vi, vi_max);
153 
154     // Compute reduced argument elements := round(x / log(2)).
155     __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
156 
157     // Compute reduced argument t := x - elements * log(2).
158     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
159     __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
160     vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
161 
162     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
163     __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
164     vp = _mm512_fmadd_ps(vp, vt, vc3);
165     vp = _mm512_fmadd_ps(vp, vt, vc2);
166     vp = _mm512_fmadd_ps(vp, vt, vc1);
167     vp = _mm512_fmadd_ps(vp, vt, vc0);
168 
169     // Reconstruct the final f value:
170     //   f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
171     //     = 2**elements * p
172     __m512 vf = _mm512_scalef_ps(vp, vn);
173 
174     // Multiply by scale.
175     vf = _mm512_mul_ps(vf, vscale);
176 
177     // Store up to 15 outputs at a time.
178     _mm512_mask_storeu_ps(output, vmask, vf);
179   }
180 }
181