Debian Codegraph @

src/f32-vsqrt/gen/f32-vsqrt-sse-rsqrt-u8.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_u8(
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 >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
55 const __m128 vx0 = _mm_loadu_ps(input);
56 const __m128 vx1 = _mm_loadu_ps(input + 4);
57 input += 8;
58
59 // Create a mask of the +/-0 inputs, which will be flushed to zero later.
60 const __m128 vmask0 = _mm_cmpeq_ps(vx0, _mm_setzero_ps());
61 const __m128 vmask1 = _mm_cmpeq_ps(vx1, _mm_setzero_ps());
62
63 // Generate the initial 12-bit approximation.
64 const __m128 vt0_0 = _mm_rsqrt_ps(vx0);
65 const __m128 vt0_1 = _mm_rsqrt_ps(vx1);
66
67 // Do a single Newton-Raphson step as described above.
68 const __m128 vt1_0 = _mm_mul_ps(vt0_0, vt0_0);
69 const __m128 vt1_1 = _mm_mul_ps(vt0_1, vt0_1);
70 const __m128 vt2_0 = _mm_mul_ps(vx0, vt1_0);
71 const __m128 vt2_1 = _mm_mul_ps(vx1, vt1_1);
72 const __m128 vt3_0 = _mm_sub_ps(vthree, vt2_0);
73 const __m128 vt3_1 = _mm_sub_ps(vthree, vt2_1);
74 const __m128 vt4_0 = _mm_mul_ps(vhalf, vt0_0);
75 const __m128 vt4_1 = _mm_mul_ps(vhalf, vt0_1);
76 const __m128 vt5_0 = _mm_mul_ps(vt3_0, vt4_0);
77 const __m128 vt5_1 = _mm_mul_ps(vt3_1, vt4_1);
78 const __m128 vt6_0 = _mm_andnot_ps(vmask0, vt5_0);
79 const __m128 vt6_1 = _mm_andnot_ps(vmask1, vt5_1);
80 const __m128 vy0 = _mm_mul_ps(vx0, vt6_0);
81 const __m128 vy1 = _mm_mul_ps(vx1, vt6_1);
82
83 // Store the results.
84 _mm_storeu_ps(output, vy0);
85 _mm_storeu_ps(output + 4, vy1);
86 output += 8;
87 }
88 for (; batch >= 4 * sizeof(float); batch -= 4 * sizeof(float)) {
89 const __m128 vx = _mm_loadu_ps(input);
90 input += 4;
91
92 // Generate the initial 12-bit approximation.
93 const __m128 vt0 = _mm_rsqrt_ps(vx);
94
95 // Create a mask of the +/-0 inputs, which will be flushed to zero later.
96 const __m128 vmask = _mm_cmpeq_ps(vx, _mm_setzero_ps());
97
98 // Do a single Newton-Raphson step as described above.
99 const __m128 vt1 = _mm_mul_ps(vt0, vt0);
100 const __m128 vt2 = _mm_mul_ps(vx, vt1);
101 const __m128 vt3 = _mm_sub_ps(vthree, vt2);
102 const __m128 vt4 = _mm_mul_ps(vhalf, vt0);
103 const __m128 vt5 = _mm_mul_ps(vt3, vt4);
104 const __m128 vt6 = _mm_andnot_ps(vmask, vt5);
105 const __m128 vy = _mm_mul_ps(vx, vt6);
106 _mm_storeu_ps(output, vy);
107 output += 4;
108 }
109 if XNN_UNLIKELY(batch != 0) {
110 const __m128 vx = _mm_loadu_ps(input);
111 // Generate the initial 12-bit approximation.
112 const __m128 vt0 = _mm_rsqrt_ps(vx);
113
114 // Create a mask of the +/-0 inputs, which will be flushed to zero later.
115 const __m128 vmask = _mm_cmpeq_ps(vx, _mm_setzero_ps());
116
117 // Do a single Newton-Raphson step as described above.
118 const __m128 vt1 = _mm_mul_ps(vt0, vt0);
119 const __m128 vt2 = _mm_mul_ps(vx, vt1);
120 const __m128 vt3 = _mm_sub_ps(vthree, vt2);
121 const __m128 vt4 = _mm_mul_ps(vhalf, vt0);
122 const __m128 vt5 = _mm_mul_ps(vt3, vt4);
123 const __m128 vt6 = _mm_andnot_ps(vmask, vt5);
124 __m128 vy = _mm_mul_ps(vx, vt6);
125
126 if (batch & (2 * sizeof(float))) {
127 _mm_storel_pi((__m64*) output, vy);
128 vy = _mm_movehl_ps(vy, vy);
129 output += 2;
130 }
131 if (batch & (1 * sizeof(float))) {
132 _mm_store_ss(output, vy);
133 }
134 }
135 }