Debian Codegraph @

src/f32-vsqrt/gen/f32-vsqrt-avx-rsqrt-u8.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_u8(
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 >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
57 const __m256 vx = _mm256_loadu_ps(input);
58 input += 8;
59
60 // Create a mask of the +/-0 inputs, which will be flushed to zero later.
61 const __m256 vinf_mask = _mm256_cmp_ps(vx, _mm256_setzero_ps(), _CMP_EQ_OQ);
62
63 // Generate the initial 12-bit approximation.
64 const __m256 vt0 = _mm256_rsqrt_ps(vx);
65
66 // Do a single Newton-Raphson step as described above.
67 const __m256 vt1 = _mm256_mul_ps(vt0, vt0);
68 const __m256 vt2 = _mm256_mul_ps(vx, vt1);
69 const __m256 vt3 = _mm256_sub_ps(kThree, vt2);
70 const __m256 vt4 = _mm256_mul_ps(kHalf, vt0);
71 const __m256 vt5 = _mm256_mul_ps(vt3, vt4);
72 const __m256 vt6 = _mm256_andnot_ps(vinf_mask, vt5);
73 const __m256 vy = _mm256_mul_ps(vx, vt6);
74
75 _mm256_storeu_ps(output, vy);
76 output += 8;
77 }
78 if XNN_UNLIKELY(batch != 0) {
79 assert(batch >= 1 * sizeof(float));
80 assert(batch <= 7 * sizeof(float));
81 const __m256i vmask = _mm256_loadu_si256(
82 (const __m256i*)((uintptr_t)&mask_table[7] - batch));
83
84 const __m256 vx = _mm256_maskload_ps(input, vmask);
85
86 // Create a mask of the +/-0 inputs, which will be flushed to zero later.
87 const __m256 vinf_mask = _mm256_cmp_ps(vx, _mm256_setzero_ps(), _CMP_EQ_OQ);
88
89 // Generate the initial 12-bit approximation.
90 const __m256 vt0 = _mm256_rsqrt_ps(vx);
91
92 // Do a single Newton-Raphson step as described above.
93 const __m256 vt1 = _mm256_mul_ps(vt0, vt0);
94 const __m256 vt2 = _mm256_mul_ps(vx, vt1);
95 const __m256 vt3 = _mm256_sub_ps(kThree, vt2);
96 const __m256 vt4 = _mm256_mul_ps(kHalf, vt0);
97 const __m256 vt5 = _mm256_mul_ps(vt3, vt4);
98 const __m256 vt6 = _mm256_andnot_ps(vinf_mask, vt5);
99 __m256 vy = _mm256_mul_ps(vx, vt6);
100
101 __m128 vy_lo = _mm256_castps256_ps128(vy);
102 if (batch & (4 * sizeof(float))) {
103 _mm_storeu_ps(output, vy_lo);
104 vy_lo = _mm256_extractf128_ps(vy, 1);
105 output += 4;
106 }
107 if (batch & (2 * sizeof(float))) {
108 _mm_storel_pi((__m64*) output, vy_lo);
109 vy_lo = _mm_movehl_ps(vy_lo, vy_lo);
110 output += 2;
111 }
112 if (batch & (1 * sizeof(float))) {
113 _mm_store_ss(output, vy_lo);
114 }
115 }
116 }