Debian Codegraph @

src/f32-vrsqrt/gen/f32-vrsqrt-fma3-rsqrt-u8.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_u8(
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 >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
54 const __m256 vx = _mm256_loadu_ps(input);
55 input += 8;
56
57 // Generate the initial 12-bit approximation.
58 const __m256 vt0 = _mm256_rsqrt_ps(vx);
59
60 // Do a single Newton-Raphson step as described above.
61 const __m256 vt1 = _mm256_mul_ps(vt0, vt0);
62 const __m256 vt3 = _mm256_fmsub_ps(vx, vt1, vthree);
63 const __m256 vt4 = _mm256_mul_ps(vneg_half, vt0);
64 const __m256 vy = _mm256_mul_ps(vt3, vt4);
65
66 _mm256_storeu_ps(output, vy);
67 output += 8;
68 }
69 if XNN_UNLIKELY(batch != 0) {
70 assert(batch >= 1 * sizeof(float));
71 assert(batch <= 7 * sizeof(float));
72 const __m256i vmask = _mm256_loadu_si256((const __m256i*)((uintptr_t) &mask_table[7] - batch));
73
74 const __m256 vx = _mm256_maskload_ps(input, vmask);
75
76 // Generate the initial 12-bit approximation.
77 const __m256 vt0 = _mm256_rsqrt_ps(vx);
78
79 // Do a single Newton-Raphson step as described above.
80 const __m256 vt1 = _mm256_mul_ps(vt0, vt0);
81 const __m256 vt3 = _mm256_fmsub_ps(vx, vt1, vthree);
82 const __m256 vt4 = _mm256_mul_ps(vneg_half, vt0);
83 __m256 vy = _mm256_mul_ps(vt3, vt4);
84
85 __m128 vy_lo = _mm256_castps256_ps128(vy);
86 if (batch & (4 * sizeof(float))) {
87 _mm_storeu_ps(output, vy_lo);
88 vy_lo = _mm256_extractf128_ps(vy, 1);
89 output += 4;
90 }
91 if (batch & (2 * sizeof(float))) {
92 _mm_storel_pi((__m64*) output, vy_lo);
93 vy_lo = _mm_movehl_ps(vy_lo, vy_lo);
94 output += 2;
95 }
96 if (batch & (1 * sizeof(float))) {
97 _mm_store_ss(output, vy_lo);
98 }
99 }
100 }