Debian Codegraph @

src/f32-vrsqrt/gen/f32-vrsqrt-fma3-rsqrt-u16.c

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