Debian Codegraph @

src/f32-raddexpminusmax/gen/f32-raddexpminusmax-avx512f-p5-scalef-u64.c

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