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_x16(size_t elements,const float * input,float * output,float scale,float max)18 void xnn_f32_vscaleexpminusmax_ukernel__avx512f_p5_scalef_x16(
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 >= 16 * sizeof(float); elements -= 16 * sizeof(float)) {
42 // Load 16 (1x16) inputs at a time.
43 const __m512 vi0 = _mm512_loadu_ps(input);
44 input += 16;
45
46 // Subtract maximum input x := i - i_max.
47 const __m512 vx0 = _mm512_sub_ps(vi0, vi_max);
48
49 // Compute reduced argument elements := round(x / log(2)).
50 __m512 vn0 = _mm512_roundscale_ps(_mm512_mul_ps(vx0, vlog2e), 0);
51
52 // Compute reduced argument t := x - elements * log(2).
53 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
54 __m512 vt0 = _mm512_fmadd_ps(vn0, vminus_ln2_hi, vx0);
55
56 vt0 = _mm512_fmadd_ps(vn0, vminus_ln2_lo, vt0);
57
58 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
59 __m512 vp0 = _mm512_fmadd_ps(vc5, vt0, vc4);
60
61 vp0 = _mm512_fmadd_ps(vp0, vt0, vc3);
62
63 vp0 = _mm512_fmadd_ps(vp0, vt0, vc2);
64
65 vp0 = _mm512_fmadd_ps(vp0, vt0, vc1);
66
67 vp0 = _mm512_fmadd_ps(vp0, vt0, vc0);
68
69 // Reconstruct the final f value:
70 // f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
71 // = 2**elements * p
72 __m512 vf0 = _mm512_scalef_ps(vp0, vn0);
73
74 // Multiply by scale.
75 vf0 = _mm512_mul_ps(vf0, vscale);
76
77 // Store 16 (1x16) outputs at a time.
78 _mm512_storeu_ps(output, vf0);
79 _mm512_storeu_ps(output + 0, vf0);
80 output += 16;
81 }
82 for (; elements >= 16 * sizeof(float); elements -= 16 * sizeof(float)) {
83 // Load 16 inputs at a time.
84 const __m512 vi = _mm512_loadu_ps(input);
85 input += 16;
86
87 // Subtract maximum input x := i - i_max.
88 const __m512 vx = _mm512_sub_ps(vi, vi_max);
89
90 // Compute reduced argument elements := round(x / log(2)).
91 __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
92
93 // Compute reduced argument t := x - elements * log(2).
94 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
95 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
96 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
97
98 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
99 __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
100 vp = _mm512_fmadd_ps(vp, vt, vc3);
101 vp = _mm512_fmadd_ps(vp, vt, vc2);
102 vp = _mm512_fmadd_ps(vp, vt, vc1);
103 vp = _mm512_fmadd_ps(vp, vt, vc0);
104
105 // Reconstruct the final f value:
106 // f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
107 // = 2**elements * p
108 __m512 vf = _mm512_scalef_ps(vp, vn);
109
110 // Multiply by scale.
111 vf = _mm512_mul_ps(vf, vscale);
112
113 // Store 16 outputs at a time.
114 _mm512_storeu_ps(output, vf);
115 output += 16;
116 }
117 if (elements != 0) {
118 // Prepare mask for valid 32-bit elements (depends on elements).
119 elements >>= 2 /* log2(sizeof(float)) */;
120 const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << elements) - UINT32_C(1)));
121
122 // Load up to 15 inputs at a time.
123 const __m512 vi = _mm512_mask_loadu_ps(_mm512_undefined_ps(), vmask, input);
124
125 // Subtract maximum input x := i - i_max.
126 const __m512 vx = _mm512_sub_ps(vi, vi_max);
127
128 // Compute reduced argument elements := round(x / log(2)).
129 __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0);
130
131 // Compute reduced argument t := x - elements * log(2).
132 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
133 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx);
134 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
135
136 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
137 __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
138 vp = _mm512_fmadd_ps(vp, vt, vc3);
139 vp = _mm512_fmadd_ps(vp, vt, vc2);
140 vp = _mm512_fmadd_ps(vp, vt, vc1);
141 vp = _mm512_fmadd_ps(vp, vt, vc0);
142
143 // Reconstruct the final f value:
144 // f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
145 // = 2**elements * p
146 __m512 vf = _mm512_scalef_ps(vp, vn);
147
148 // Multiply by scale.
149 vf = _mm512_mul_ps(vf, vscale);
150
151 // Store up to 15 outputs at a time.
152 _mm512_mask_storeu_ps(output, vmask, vf);
153 }
154 }
155