Debian Codegraph @

src/f32-vsqrt/gen/f32-vsqrt-avx-rsqrt-u32.c

raw symbols SCIP
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-vsqrt/avx-rsqrt.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2024 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 #include <immintrin.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <xmmintrin.h>
15 #include "xnnpack/common.h"
16 #include "xnnpack/microparams.h"
17 #include "xnnpack/vunary.h"
18
19
20 // In the following, we first compute the _reciprocal_ square root of an input
21 // `a` and then multiply it with `a` to produce the square root.
22 //
23 // We compute the reciprocal square root using a single Newton-Raphson step on
24 // the equation $x^{-2} - a$, which expands to:
25 //
26 // $$x_{k+1} = 0.5 * x_k * (3.0 - a * x_k^2)$$
27 //
28 // So we do the following steps:
29 //
30 // 1. t0 = x_k
31 // 2. t1 = t0 * t0 (x_k^2)
32 // 3. t2 = a * t1 (a * x_k^2)
33 // 4. t3 = 3.0 - t2 (3.0 - a * x_k^2)
34 // 5. t4 = 0.5 * t0 (0.5 * x_k)
35 // 6. t5 = t3 * t4 (0.5 * x_k * (3.0 - a * x_k^2))
36 // 7. y = a * t5 (a * a^{-1/2})
37 //
38 // Where $x_k$ is the original 14-bit approximation and `t5` contains the final
39 // 24-bit approximation $x_{k+1}$.
40
41 void xnn_f32_vsqrt_ukernel__avx_rsqrt_u32(
42 size_t batch, const float* input, float* output,
43 const struct xnn_f32_default_params params[restrict XNN_MIN_ELEMENTS(1)])
44 XNN_OOB_READS {
45 static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
46
47 assert(batch != 0);
48 assert(batch % sizeof(float) == 0);
49 assert(input != NULL);
50 assert(output != NULL);
51
52 // Constants for the Newton-Raphson iteration.
53 const __m256 kThree = _mm256_set1_ps(3.0f);
54 const __m256 kHalf = _mm256_set1_ps(0.5f);
55
56 for (; batch >= 32 * sizeof(float); batch -= 32 * sizeof(float)) {
57 const __m256 vx0 = _mm256_loadu_ps(input);
58 const __m256 vx1 = _mm256_loadu_ps(input + 8);
59 const __m256 vx2 = _mm256_loadu_ps(input + 16);
60 const __m256 vx3 = _mm256_loadu_ps(input + 24);
61 input += 32;
62
63 // Create a mask of the +/-0 inputs, which will be flushed to zero later.
64 const __m256 vinf_mask_0 = _mm256_cmp_ps(vx0, _mm256_setzero_ps(), _CMP_EQ_OQ);
65 const __m256 vinf_mask_1 = _mm256_cmp_ps(vx1, _mm256_setzero_ps(), _CMP_EQ_OQ);
66 const __m256 vinf_mask_2 = _mm256_cmp_ps(vx2, _mm256_setzero_ps(), _CMP_EQ_OQ);
67 const __m256 vinf_mask_3 = _mm256_cmp_ps(vx3, _mm256_setzero_ps(), _CMP_EQ_OQ);
68
69 // Generate the initial 12-bit approximation.
70 const __m256 vt0_0 = _mm256_rsqrt_ps(vx0);
71 const __m256 vt0_1 = _mm256_rsqrt_ps(vx1);
72 const __m256 vt0_2 = _mm256_rsqrt_ps(vx2);
73 const __m256 vt0_3 = _mm256_rsqrt_ps(vx3);
74
75 // Do a single Newton-Raphson step as described above.
76 const __m256 vt1_0 = _mm256_mul_ps(vt0_0, vt0_0);
77 const __m256 vt1_1 = _mm256_mul_ps(vt0_1, vt0_1);
78 const __m256 vt1_2 = _mm256_mul_ps(vt0_2, vt0_2);
79 const __m256 vt1_3 = _mm256_mul_ps(vt0_3, vt0_3);
80 const __m256 vt2_0 = _mm256_mul_ps(vx0, vt1_0);
81 const __m256 vt2_1 = _mm256_mul_ps(vx1, vt1_1);
82 const __m256 vt2_2 = _mm256_mul_ps(vx2, vt1_2);
83 const __m256 vt2_3 = _mm256_mul_ps(vx3, vt1_3);
84 const __m256 vt3_0 = _mm256_sub_ps(kThree, vt2_0);
85 const __m256 vt3_1 = _mm256_sub_ps(kThree, vt2_1);
86 const __m256 vt3_2 = _mm256_sub_ps(kThree, vt2_2);
87 const __m256 vt3_3 = _mm256_sub_ps(kThree, vt2_3);
88 const __m256 vt4_0 = _mm256_mul_ps(kHalf, vt0_0);
89 const __m256 vt4_1 = _mm256_mul_ps(kHalf, vt0_1);
90 const __m256 vt4_2 = _mm256_mul_ps(kHalf, vt0_2);
91 const __m256 vt4_3 = _mm256_mul_ps(kHalf, vt0_3);
92 const __m256 vt5_0 = _mm256_mul_ps(vt3_0, vt4_0);
93 const __m256 vt5_1 = _mm256_mul_ps(vt3_1, vt4_1);
94 const __m256 vt5_2 = _mm256_mul_ps(vt3_2, vt4_2);
95 const __m256 vt5_3 = _mm256_mul_ps(vt3_3, vt4_3);
96 const __m256 vt6_0 = _mm256_andnot_ps(vinf_mask_0, vt5_0);
97 const __m256 vt6_1 = _mm256_andnot_ps(vinf_mask_1, vt5_1);
98 const __m256 vt6_2 = _mm256_andnot_ps(vinf_mask_2, vt5_2);
99 const __m256 vt6_3 = _mm256_andnot_ps(vinf_mask_3, vt5_3);
100 const __m256 vy0 = _mm256_mul_ps(vx0, vt6_0);
101 const __m256 vy1 = _mm256_mul_ps(vx1, vt6_1);
102 const __m256 vy2 = _mm256_mul_ps(vx2, vt6_2);
103 const __m256 vy3 = _mm256_mul_ps(vx3, vt6_3);
104
105 // Store the results.
106 _mm256_storeu_ps(output, vy0);
107 _mm256_storeu_ps(output + 8, vy1);
108 _mm256_storeu_ps(output + 16, vy2);
109 _mm256_storeu_ps(output + 24, vy3);
110 output += 32;
111 }
112 for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
113 const __m256 vx = _mm256_loadu_ps(input);
114 input += 8;
115
116 // Create a mask of the +/-0 inputs, which will be flushed to zero later.
117 const __m256 vinf_mask = _mm256_cmp_ps(vx, _mm256_setzero_ps(), _CMP_EQ_OQ);
118
119 // Generate the initial 12-bit approximation.
120 const __m256 vt0 = _mm256_rsqrt_ps(vx);
121
122 // Do a single Newton-Raphson step as described above.
123 const __m256 vt1 = _mm256_mul_ps(vt0, vt0);
124 const __m256 vt2 = _mm256_mul_ps(vx, vt1);
125 const __m256 vt3 = _mm256_sub_ps(kThree, vt2);
126 const __m256 vt4 = _mm256_mul_ps(kHalf, vt0);
127 const __m256 vt5 = _mm256_mul_ps(vt3, vt4);
128 const __m256 vt6 = _mm256_andnot_ps(vinf_mask, vt5);
129 const __m256 vy = _mm256_mul_ps(vx, vt6);
130
131 _mm256_storeu_ps(output, vy);
132 output += 8;
133 }
134 if XNN_UNLIKELY(batch != 0) {
135 assert(batch >= 1 * sizeof(float));
136 assert(batch <= 7 * sizeof(float));
137 const __m256i vmask = _mm256_loadu_si256(
138 (const __m256i*)((uintptr_t)&mask_table[7] - batch));
139
140 const __m256 vx = _mm256_maskload_ps(input, vmask);
141
142 // Create a mask of the +/-0 inputs, which will be flushed to zero later.
143 const __m256 vinf_mask = _mm256_cmp_ps(vx, _mm256_setzero_ps(), _CMP_EQ_OQ);
144
145 // Generate the initial 12-bit approximation.
146 const __m256 vt0 = _mm256_rsqrt_ps(vx);
147
148 // Do a single Newton-Raphson step as described above.
149 const __m256 vt1 = _mm256_mul_ps(vt0, vt0);
150 const __m256 vt2 = _mm256_mul_ps(vx, vt1);
151 const __m256 vt3 = _mm256_sub_ps(kThree, vt2);
152 const __m256 vt4 = _mm256_mul_ps(kHalf, vt0);
153 const __m256 vt5 = _mm256_mul_ps(vt3, vt4);
154 const __m256 vt6 = _mm256_andnot_ps(vinf_mask, vt5);
155 __m256 vy = _mm256_mul_ps(vx, vt6);
156
157 __m128 vy_lo = _mm256_castps256_ps128(vy);
158 if (batch & (4 * sizeof(float))) {
159 _mm_storeu_ps(output, vy_lo);
160 vy_lo = _mm256_extractf128_ps(vy, 1);
161 output += 4;
162 }
163 if (batch & (2 * sizeof(float))) {
164 _mm_storel_pi((__m64*) output, vy_lo);
165 vy_lo = _mm_movehl_ps(vy_lo, vy_lo);
166 output += 2;
167 }
168 if (batch & (1 * sizeof(float))) {
169 _mm_store_ss(output, vy_lo);
170 }
171 }
172 }