xref: /aosp_15_r20/external/XNNPACK/src/f32-vsqrt/gen/fma3-nr1fma1adj-x8.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-vsqrt/fma3-nr1fma1adj.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 <immintrin.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/vunary.h>
16 
17 
xnn_f32_vsqrt_ukernel__fma3_nr1fma1adj_x8(size_t n,const float * x,float * y,const union xnn_f32_sqrt_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f32_vsqrt_ukernel__fma3_nr1fma1adj_x8(
19     size_t n,
20     const float* x,
21     float* y,
22     const union xnn_f32_sqrt_params params[restrict XNN_MIN_ELEMENTS(1)])
23 {
24   assert(n != 0);
25   assert(n % sizeof(float) == 0);
26 
27   const __m256 vhalf = _mm256_load_ps(params->fma.half);
28   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
29     const __m256 vx = _mm256_loadu_ps(x);
30     x += 8;
31 
32     const __m256 vrsqrtx = _mm256_rsqrt_ps(vx);
33     __m256 vsqrtx = _mm256_mul_ps(vrsqrtx, vx);
34     __m256 vhalfrsqrtx = _mm256_mul_ps(vrsqrtx, vhalf);
35     const __m256 vresidual = _mm256_fnmadd_ps(vsqrtx, vhalfrsqrtx, vhalf);
36     vhalfrsqrtx = _mm256_fmadd_ps(vhalfrsqrtx, vresidual, vhalfrsqrtx);
37     vsqrtx = _mm256_fmadd_ps(vsqrtx, vresidual, vsqrtx);
38     const __m256 vadjustment = _mm256_fnmadd_ps(vsqrtx, vsqrtx, vx);
39     const __m256 vy = _mm256_fmadd_ps(vhalfrsqrtx, vadjustment, vsqrtx);
40 
41     _mm256_storeu_ps(y, vy);
42     y += 8;
43   }
44   if XNN_UNLIKELY(n != 0) {
45     assert(n >= 1 * sizeof(float));
46     assert(n <= 7 * sizeof(float));
47     const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &params->fma.mask_table[7] - n));
48 
49     const __m256 vx = _mm256_maskload_ps(x, vmask);
50 
51     const __m256 vrsqrtx = _mm256_rsqrt_ps(vx);
52     __m256 vsqrtx = _mm256_mul_ps(vrsqrtx, vx);
53     __m256 vhalfrsqrtx = _mm256_mul_ps(vrsqrtx, vhalf);
54     const __m256 vresidual = _mm256_fnmadd_ps(vsqrtx, vhalfrsqrtx, vhalf);
55     vhalfrsqrtx = _mm256_fmadd_ps(vhalfrsqrtx, vresidual, vhalfrsqrtx);
56     vsqrtx = _mm256_fmadd_ps(vsqrtx, vresidual, vsqrtx);
57     const __m256 vadjustment = _mm256_fnmadd_ps(vsqrtx, vsqrtx, vx);
58     const __m256 vy = _mm256_fmadd_ps(vhalfrsqrtx, vadjustment, vsqrtx);
59 
60     __m128 vy_lo = _mm256_castps256_ps128(vy);
61     if (n & (4 * sizeof(float))) {
62       _mm_storeu_ps(y, vy_lo);
63       vy_lo = _mm256_extractf128_ps(vy, 1);
64       y += 4;
65     }
66     if (n & (2 * sizeof(float))) {
67       _mm_storel_pi((__m64*) y, vy_lo);
68       vy_lo = _mm_movehl_ps(vy_lo, vy_lo);
69       y += 2;
70     }
71     if (n & (1 * sizeof(float))) {
72       _mm_store_ss(y, vy_lo);
73     }
74   }
75 }
76