Debian Codegraph @

src/f32-vsqrt/gen/f32-vsqrt-sse-rsqrt-u4.c

raw symbols SCIP
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-vsqrt/sse-rsqrt.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2020 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 <xmmintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/vunary.h"
16
17 // In the following, we first compute the _reciprocal_ square root of an input
18 // `a` and then multiply it with `a` to produce the square root.
19 //
20 // We compute the reciprocal square root using a single Newton-Raphson step on
21 // the equation $x^{-2} - a$, which expands to:
22 //
23 // $$x_{k+1} = 0.5 * x_k * (3.0 - a * x_k^2)$$
24 //
25 // So we do the following steps:
26 //
27 // 1. t0 = x_k
28 // 2. t1 = t0 * t0 (x_k^2)
29 // 3. t2 = a * t1 (a * x_k^2)
30 // 4. t3 = 3.0 - t2 (3.0 - a * x_k^2)
31 // 5. t4 = 0.5 * t0 (0.5 * x_k)
32 // 6. t5 = t3 * t4 (0.5 * x_k * (3.0 - a * x_k^2))
33 // 7. y = a * t5 (a * a^{-1/2})
34 //
35 // Where $x_k$ is the original 14-bit approximation and `t5` contains the final
36 // 24-bit approximation $x_{k+1}$.
37
38
39 void xnn_f32_vsqrt_ukernel__sse_rsqrt_u4(
40 size_t batch,
41 const float* input,
42 float* output,
43 const struct xnn_f32_default_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
44 {
45 assert(batch != 0);
46 assert(batch % sizeof(float) == 0);
47 assert(input != NULL);
48 assert(output != NULL);
49
50 // Constants for the Newton-Raphson iteration.
51 const __m128 vthree = _mm_set1_ps(3.0f);
52 const __m128 vhalf = _mm_set1_ps(0.5f);
53
54 for (; batch >= 4 * sizeof(float); batch -= 4 * sizeof(float)) {
55 const __m128 vx = _mm_loadu_ps(input);
56 input += 4;
57
58 // Generate the initial 12-bit approximation.
59 const __m128 vt0 = _mm_rsqrt_ps(vx);
60
61 // Create a mask of the +/-0 inputs, which will be flushed to zero later.
62 const __m128 vmask = _mm_cmpeq_ps(vx, _mm_setzero_ps());
63
64 // Do a single Newton-Raphson step as described above.
65 const __m128 vt1 = _mm_mul_ps(vt0, vt0);
66 const __m128 vt2 = _mm_mul_ps(vx, vt1);
67 const __m128 vt3 = _mm_sub_ps(vthree, vt2);
68 const __m128 vt4 = _mm_mul_ps(vhalf, vt0);
69 const __m128 vt5 = _mm_mul_ps(vt3, vt4);
70 const __m128 vt6 = _mm_andnot_ps(vmask, vt5);
71 const __m128 vy = _mm_mul_ps(vx, vt6);
72 _mm_storeu_ps(output, vy);
73 output += 4;
74 }
75 if XNN_UNLIKELY(batch != 0) {
76 const __m128 vx = _mm_loadu_ps(input);
77 // Generate the initial 12-bit approximation.
78 const __m128 vt0 = _mm_rsqrt_ps(vx);
79
80 // Create a mask of the +/-0 inputs, which will be flushed to zero later.
81 const __m128 vmask = _mm_cmpeq_ps(vx, _mm_setzero_ps());
82
83 // Do a single Newton-Raphson step as described above.
84 const __m128 vt1 = _mm_mul_ps(vt0, vt0);
85 const __m128 vt2 = _mm_mul_ps(vx, vt1);
86 const __m128 vt3 = _mm_sub_ps(vthree, vt2);
87 const __m128 vt4 = _mm_mul_ps(vhalf, vt0);
88 const __m128 vt5 = _mm_mul_ps(vt3, vt4);
89 const __m128 vt6 = _mm_andnot_ps(vmask, vt5);
90 __m128 vy = _mm_mul_ps(vx, vt6);
91
92 if (batch & (2 * sizeof(float))) {
93 _mm_storel_pi((__m64*) output, vy);
94 vy = _mm_movehl_ps(vy, vy);
95 output += 2;
96 }
97 if (batch & (1 * sizeof(float))) {
98 _mm_store_ss(output, vy);
99 }
100 }
101 }