| 1 |
// Auto-generated file. Do not edit! |
| 2 |
// Template: src/f32-vrsqrt/sse-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 |
|
| 12 |
#include <xmmintrin.h> |
| 13 |
|
| 14 |
#include "xnnpack/common.h" |
| 15 |
#include "xnnpack/vunary.h" |
| 16 |
|
| 17 |
// In the following, we do a single Newton-Raphson step on the equation |
| 18 |
// $x^{-2} - a$, which expands to: |
| 19 |
// |
| 20 |
// $$x_{k+1} = 0.5 * x_k * (3.0 - a * x_k^2)$$ |
| 21 |
// |
| 22 |
// So we do the following steps: |
| 23 |
// |
| 24 |
// 1. t0 = x_k |
| 25 |
// 2. t1 = t0 * t0 (x_k^2) |
| 26 |
// 3. t2 = a * t1 (a * x_k^2) |
| 27 |
// 4. t3 = 3.0 - t2 (3.0 - a * x_k^2) |
| 28 |
// 5. t4 = 0.5 * t0 (0.5 * x_k) |
| 29 |
// 6. y = t3 * t4 (0.5 * x_k * (3.0 - a * x_k^2)) |
| 30 |
// |
| 31 |
// Where $x_k$ is the original 12-bit approximation and `y` contains the final |
| 32 |
// 24-bit approximation $x_{k+1}$. |
| 33 |
|
| 34 |
|
| 35 |
void xnn_f32_vrsqrt_ukernel__sse_rsqrt_u16( |
| 36 |
size_t batch, |
| 37 |
const float* input, |
| 38 |
float* output, |
| 39 |
const struct xnn_f32_default_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS |
| 40 |
{ |
| 41 |
assert(batch != 0); |
| 42 |
assert(batch % sizeof(float) == 0); |
| 43 |
assert(input != NULL); |
| 44 |
assert(output != NULL); |
| 45 |
|
| 46 |
// Constants for the Newton-Raphson iteration. |
| 47 |
const __m128 vthree = _mm_set1_ps(3.0f); |
| 48 |
const __m128 vhalf = _mm_set1_ps(0.5f); |
| 49 |
|
| 50 |
for (; batch >= 16 * sizeof(float); batch -= 16 * sizeof(float)) { |
| 51 |
const __m128 vx0123 = _mm_loadu_ps(input); |
| 52 |
const __m128 vx4567 = _mm_loadu_ps(input + 4); |
| 53 |
const __m128 vx89AB = _mm_loadu_ps(input + 8); |
| 54 |
const __m128 vxCDEF = _mm_loadu_ps(input + 12); |
| 55 |
input += 16; |
| 56 |
|
| 57 |
// Generate the initial 12-bit approximation. |
| 58 |
const __m128 vt0_0123 = _mm_rsqrt_ps(vx0123); |
| 59 |
const __m128 vt0_4567 = _mm_rsqrt_ps(vx4567); |
| 60 |
const __m128 vt0_89AB = _mm_rsqrt_ps(vx89AB); |
| 61 |
const __m128 vt0_CDEF = _mm_rsqrt_ps(vxCDEF); |
| 62 |
|
| 63 |
// Do a single Newton-Raphson step as described above. |
| 64 |
const __m128 vt1_0123 = _mm_mul_ps(vt0_0123, vt0_0123); |
| 65 |
const __m128 vt1_4567 = _mm_mul_ps(vt0_4567, vt0_4567); |
| 66 |
const __m128 vt1_89AB = _mm_mul_ps(vt0_89AB, vt0_89AB); |
| 67 |
const __m128 vt1_CDEF = _mm_mul_ps(vt0_CDEF, vt0_CDEF); |
| 68 |
const __m128 vt2_0123 = _mm_mul_ps(vx0123, vt1_0123); |
| 69 |
const __m128 vt2_4567 = _mm_mul_ps(vx4567, vt1_4567); |
| 70 |
const __m128 vt2_89AB = _mm_mul_ps(vx89AB, vt1_89AB); |
| 71 |
const __m128 vt2_CDEF = _mm_mul_ps(vxCDEF, vt1_CDEF); |
| 72 |
const __m128 vt3_0123 = _mm_sub_ps(vthree, vt2_0123); |
| 73 |
const __m128 vt3_4567 = _mm_sub_ps(vthree, vt2_4567); |
| 74 |
const __m128 vt3_89AB = _mm_sub_ps(vthree, vt2_89AB); |
| 75 |
const __m128 vt3_CDEF = _mm_sub_ps(vthree, vt2_CDEF); |
| 76 |
const __m128 vt4_0123 = _mm_mul_ps(vhalf, vt0_0123); |
| 77 |
const __m128 vt4_4567 = _mm_mul_ps(vhalf, vt0_4567); |
| 78 |
const __m128 vt4_89AB = _mm_mul_ps(vhalf, vt0_89AB); |
| 79 |
const __m128 vt4_CDEF = _mm_mul_ps(vhalf, vt0_CDEF); |
| 80 |
const __m128 vy0123 = _mm_mul_ps(vt3_0123, vt4_0123); |
| 81 |
const __m128 vy4567 = _mm_mul_ps(vt3_4567, vt4_4567); |
| 82 |
const __m128 vy89AB = _mm_mul_ps(vt3_89AB, vt4_89AB); |
| 83 |
const __m128 vyCDEF = _mm_mul_ps(vt3_CDEF, vt4_CDEF); |
| 84 |
|
| 85 |
// Store the results. |
| 86 |
_mm_storeu_ps(output, vy0123); |
| 87 |
_mm_storeu_ps(output + 4, vy4567); |
| 88 |
_mm_storeu_ps(output + 8, vy89AB); |
| 89 |
_mm_storeu_ps(output + 12, vyCDEF); |
| 90 |
output += 16; |
| 91 |
} |
| 92 |
for (; batch >= 4 * sizeof(float); batch -= 4 * sizeof(float)) { |
| 93 |
const __m128 vx = _mm_loadu_ps(input); |
| 94 |
input += 4; |
| 95 |
|
| 96 |
// Generate the initial 12-bit approximation. |
| 97 |
const __m128 vt0 = _mm_rsqrt_ps(vx); |
| 98 |
|
| 99 |
// Do a single Newton-Raphson step as described above. |
| 100 |
const __m128 vt1 = _mm_mul_ps(vt0, vt0); |
| 101 |
const __m128 vt2 = _mm_mul_ps(vx, vt1); |
| 102 |
const __m128 vt3 = _mm_sub_ps(vthree, vt2); |
| 103 |
const __m128 vt4 = _mm_mul_ps(vhalf, vt0); |
| 104 |
const __m128 vy = _mm_mul_ps(vt3, vt4); |
| 105 |
|
| 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 |
|
| 112 |
// Generate the initial 12-bit approximation. |
| 113 |
const __m128 vt0 = _mm_rsqrt_ps(vx); |
| 114 |
|
| 115 |
// Do a single Newton-Raphson step as described above. |
| 116 |
const __m128 vt1 = _mm_mul_ps(vt0, vt0); |
| 117 |
const __m128 vt2 = _mm_mul_ps(vx, vt1); |
| 118 |
const __m128 vt3 = _mm_sub_ps(vthree, vt2); |
| 119 |
const __m128 vt4 = _mm_mul_ps(vhalf, vt0); |
| 120 |
__m128 vy = _mm_mul_ps(vt3, vt4); |
| 121 |
|
| 122 |
if (batch & (2 * sizeof(float))) { |
| 123 |
_mm_storel_pi((__m64*) output, vy); |
| 124 |
vy = _mm_movehl_ps(vy, vy); |
| 125 |
output += 2; |
| 126 |
} |
| 127 |
if (batch & (1 * sizeof(float))) { |
| 128 |
_mm_store_ss(output, vy); |
| 129 |
} |
| 130 |
} |
| 131 |
} |