Debian Codegraph @

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

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-vrsqrt/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
12 #include <immintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/vunary.h"
16
17 // In the following, we do a single Newton-Raphson step on the equation
18 // $x^{-2} - a$, which expands to:
19 //
20 // $$x_{k+1} = 0.5 * x_k * (3.0 - a * x_k^2)$$
21 //
22 // So we do the following steps:
23 //
24 // 1. t0 = x_k
25 // 2. t1 = t0 * t0 (x_k^2)
26 // 3. t2 = a * t1 (a * x_k^2)
27 // 4. t3 = 3.0 - t2 (3.0 - a * x_k^2)
28 // 5. t4 = 0.5 * t0 (0.5 * x_k)
29 // 6. y = t3 * t4 (0.5 * x_k * (3.0 - a * x_k^2))
30 //
31 // Where $x_k$ is the original 12-bit approximation and `y` contains the final
32 // 24-bit approximation $x_{k+1}$.
33
34
35 void xnn_f32_vrsqrt_ukernel__avx_rsqrt_u32(
36 size_t batch,
37 const float* input,
38 float* output,
39 const struct xnn_f32_default_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
40 {
41 static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
42
43 assert(batch != 0);
44 assert(batch % sizeof(float) == 0);
45 assert(input != NULL);
46 assert(output != NULL);
47
48 // Constants for the Newton-Raphson iteration.
49 const __m256 vthree = _mm256_set1_ps(3.0f);
50 const __m256 vhalf = _mm256_set1_ps(0.5f);
51
52 for (; batch >= 32 * sizeof(float); batch -= 32 * sizeof(float)) {
53 const __m256 vx0 = _mm256_loadu_ps(input);
54 const __m256 vx1 = _mm256_loadu_ps(input + 8);
55 const __m256 vx2 = _mm256_loadu_ps(input + 16);
56 const __m256 vx3 = _mm256_loadu_ps(input + 24);
57 input += 32;
58
59 // Generate the initial 12-bit approximation.
60 const __m256 vt0_0 = _mm256_rsqrt_ps(vx0);
61 const __m256 vt0_1 = _mm256_rsqrt_ps(vx1);
62 const __m256 vt0_2 = _mm256_rsqrt_ps(vx2);
63 const __m256 vt0_3 = _mm256_rsqrt_ps(vx3);
64
65 // Do a single Newton-Raphson step as described above.
66 const __m256 vt1_0 = _mm256_mul_ps(vt0_0, vt0_0);
67 const __m256 vt1_1 = _mm256_mul_ps(vt0_1, vt0_1);
68 const __m256 vt1_2 = _mm256_mul_ps(vt0_2, vt0_2);
69 const __m256 vt1_3 = _mm256_mul_ps(vt0_3, vt0_3);
70 const __m256 vt2_0 = _mm256_mul_ps(vx0, vt1_0);
71 const __m256 vt2_1 = _mm256_mul_ps(vx1, vt1_1);
72 const __m256 vt2_2 = _mm256_mul_ps(vx2, vt1_2);
73 const __m256 vt2_3 = _mm256_mul_ps(vx3, vt1_3);
74 const __m256 vt3_0 = _mm256_sub_ps(vthree, vt2_0);
75 const __m256 vt3_1 = _mm256_sub_ps(vthree, vt2_1);
76 const __m256 vt3_2 = _mm256_sub_ps(vthree, vt2_2);
77 const __m256 vt3_3 = _mm256_sub_ps(vthree, vt2_3);
78 const __m256 vt4_0 = _mm256_mul_ps(vhalf, vt0_0);
79 const __m256 vt4_1 = _mm256_mul_ps(vhalf, vt0_1);
80 const __m256 vt4_2 = _mm256_mul_ps(vhalf, vt0_2);
81 const __m256 vt4_3 = _mm256_mul_ps(vhalf, vt0_3);
82 const __m256 vy0 = _mm256_mul_ps(vt3_0, vt4_0);
83 const __m256 vy1 = _mm256_mul_ps(vt3_1, vt4_1);
84 const __m256 vy2 = _mm256_mul_ps(vt3_2, vt4_2);
85 const __m256 vy3 = _mm256_mul_ps(vt3_3, vt4_3);
86
87 // Store the results.
88 _mm256_storeu_ps(output, vy0);
89 _mm256_storeu_ps(output + 8, vy1);
90 _mm256_storeu_ps(output + 16, vy2);
91 _mm256_storeu_ps(output + 24, vy3);
92 output += 32;
93 }
94 for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
95 const __m256 vx = _mm256_loadu_ps(input);
96 input += 8;
97
98 // Generate the initial 12-bit approximation.
99 const __m256 vt0 = _mm256_rsqrt_ps(vx);
100
101 // Do a single Newton-Raphson step as described above.
102 const __m256 vt1 = _mm256_mul_ps(vt0, vt0);
103 const __m256 vt2 = _mm256_mul_ps(vx, vt1);
104 const __m256 vt3 = _mm256_sub_ps(vthree, vt2);
105 const __m256 vt4 = _mm256_mul_ps(vhalf, vt0);
106 const __m256 vy = _mm256_mul_ps(vt3, vt4);
107
108 _mm256_storeu_ps(output, vy);
109 output += 8;
110 }
111 if XNN_UNLIKELY(batch != 0) {
112 assert(batch >= 1 * sizeof(float));
113 assert(batch <= 7 * sizeof(float));
114 const __m256i vmask = _mm256_loadu_si256((const __m256i*)((uintptr_t) &mask_table[7] - batch));
115
116 const __m256 vx = _mm256_maskload_ps(input, vmask);
117
118 // Generate the initial 12-bit approximation.
119 const __m256 vt0 = _mm256_rsqrt_ps(vx);
120
121 // Do a single Newton-Raphson step as described above.
122 const __m256 vt1 = _mm256_mul_ps(vt0, vt0);
123 const __m256 vt2 = _mm256_mul_ps(vx, vt1);
124 const __m256 vt3 = _mm256_sub_ps(vthree, vt2);
125 const __m256 vt4 = _mm256_mul_ps(vhalf, vt0);
126 __m256 vy = _mm256_mul_ps(vt3, vt4);
127
128 __m128 vy_lo = _mm256_castps256_ps128(vy);
129 if (batch & (4 * sizeof(float))) {
130 _mm_storeu_ps(output, vy_lo);
131 vy_lo = _mm256_extractf128_ps(vy, 1);
132 output += 4;
133 }
134 if (batch & (2 * sizeof(float))) {
135 _mm_storel_pi((__m64*) output, vy_lo);
136 vy_lo = _mm_movehl_ps(vy_lo, vy_lo);
137 output += 2;
138 }
139 if (batch & (1 * sizeof(float))) {
140 _mm_store_ss(output, vy_lo);
141 }
142 }
143 }