xref: /aosp_15_r20/external/XNNPACK/src/f32-vsqrt/neonfma-nr2fma1adj.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1// Copyright 2020 Google LLC
2//
3// This source code is licensed under the BSD-style license found in the
4// LICENSE file in the root directory of this source tree.
5
6$assert BATCH_TILE % 4 == 0
7$assert BATCH_TILE >= 4
8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9#include <assert.h>
10#include <math.h>
11
12#include <arm_neon.h>
13
14#include <xnnpack/common.h>
15#include <xnnpack/vunary.h>
16
17
18void xnn_f32_vsqrt_ukernel__neonfma_nr2fma1adj_x${BATCH_TILE}(
19    size_t n,
20    const float* x,
21    float* y,
22    const union xnn_f32_sqrt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
23{
24  assert(n != 0);
25  assert(n % sizeof(float) == 0);
26
27  const float32x4_t vhalf = vmovq_n_f32(0.5f);
28  $if BATCH_TILE > 4:
29    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
30      $for N in range(0, BATCH_TILE, 4):
31        const float32x4_t vx${ABC[N:N+4]} = vld1q_f32(x); x += 4;
32
33      $for N in range(0, BATCH_TILE, 4):
34        const float32x4_t vrsqrtx${ABC[N:N+4]} = vrsqrteq_f32(vx${ABC[N:N+4]});
35
36      $for N in range(0, BATCH_TILE, 4):
37        float32x4_t vsqrtx${ABC[N:N+4]} = vmulq_f32(vrsqrtx${ABC[N:N+4]}, vx${ABC[N:N+4]});
38        float32x4_t vhalfrsqrtx${ABC[N:N+4]} = vmulq_f32(vrsqrtx${ABC[N:N+4]}, vhalf);
39
40      $for N in range(0, BATCH_TILE, 4):
41        float32x4_t vresidual${ABC[N:N+4]} = vfmsq_f32(vhalf, vsqrtx${ABC[N:N+4]}, vhalfrsqrtx${ABC[N:N+4]});
42
43      $for N in range(0, BATCH_TILE, 4):
44        vhalfrsqrtx${ABC[N:N+4]} = vfmaq_f32(vhalfrsqrtx${ABC[N:N+4]}, vresidual${ABC[N:N+4]}, vhalfrsqrtx${ABC[N:N+4]});
45        vsqrtx${ABC[N:N+4]} = vfmaq_f32(vsqrtx${ABC[N:N+4]}, vresidual${ABC[N:N+4]}, vsqrtx${ABC[N:N+4]});
46
47      $for N in range(0, BATCH_TILE, 4):
48        vresidual${ABC[N:N+4]} = vfmsq_f32(vhalf, vsqrtx${ABC[N:N+4]}, vhalfrsqrtx${ABC[N:N+4]});
49
50      $for N in range(0, BATCH_TILE, 4):
51        vhalfrsqrtx${ABC[N:N+4]} = vfmaq_f32(vhalfrsqrtx${ABC[N:N+4]}, vresidual${ABC[N:N+4]}, vhalfrsqrtx${ABC[N:N+4]});
52        vsqrtx${ABC[N:N+4]} = vfmaq_f32(vsqrtx${ABC[N:N+4]}, vresidual${ABC[N:N+4]}, vsqrtx${ABC[N:N+4]});
53
54      $for N in range(0, BATCH_TILE, 4):
55        const float32x4_t vadjustment${ABC[N:N+4]} = vfmsq_f32(vx${ABC[N:N+4]}, vsqrtx${ABC[N:N+4]}, vsqrtx${ABC[N:N+4]});
56
57      $for N in range(0, BATCH_TILE, 4):
58        const float32x4_t vy${ABC[N:N+4]} = vfmaq_f32(vsqrtx${ABC[N:N+4]}, vhalfrsqrtx${ABC[N:N+4]}, vadjustment${ABC[N:N+4]});
59
60      $for N in range(0, BATCH_TILE, 4):
61        vst1q_f32(y, vy${ABC[N:N+4]}); y += 4;
62    }
63  for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
64    const float32x4_t vx = vld1q_f32(x); x += 4;
65    const float32x4_t vrsqrtx = vrsqrteq_f32(vx);
66    float32x4_t vsqrtx = vmulq_f32(vrsqrtx, vx);
67    float32x4_t vhalfrsqrtx = vmulq_f32(vrsqrtx, vhalf);
68    float32x4_t vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx);
69    vhalfrsqrtx = vfmaq_f32(vhalfrsqrtx, vresidual, vhalfrsqrtx);
70    vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx);
71    vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx);
72    vhalfrsqrtx = vfmaq_f32(vhalfrsqrtx, vresidual, vhalfrsqrtx);
73    vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx);
74    const float32x4_t vadjustment = vfmsq_f32(vx, vsqrtx, vsqrtx);
75    const float32x4_t vy = vfmaq_f32(vsqrtx, vhalfrsqrtx, vadjustment);
76    vst1q_f32(y, vy); y += 4;
77  }
78  if XNN_UNLIKELY(n != 0) {
79    const float32x4_t vx = vld1q_f32(x);
80    const float32x4_t vrsqrtx = vrsqrteq_f32(vx);
81    float32x4_t vsqrtx = vmulq_f32(vrsqrtx, vx);
82    float32x4_t vhalfrsqrtx = vmulq_f32(vrsqrtx, vhalf);
83    float32x4_t vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx);
84    vhalfrsqrtx = vfmaq_f32(vhalfrsqrtx, vresidual, vhalfrsqrtx);
85    vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx);
86    vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx);
87    vhalfrsqrtx = vfmaq_f32(vhalfrsqrtx, vresidual, vhalfrsqrtx);
88    vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx);
89    const float32x4_t vadjustment = vfmsq_f32(vx, vsqrtx, vsqrtx);
90    const float32x4_t vy = vfmaq_f32(vsqrtx, vhalfrsqrtx, vadjustment);
91
92    float32x2_t vy_lo = vget_low_f32(vy);
93    if (n & (2 * sizeof(float))) {
94      vst1_f32(y, vy_lo); y += 2;
95      vy_lo = vget_high_f32(vy);
96    }
97    if (n & (1 * sizeof(float))) {
98      vst1_lane_f32(y, vy_lo, 0);
99    }
100  }
101}
102