xref: /aosp_15_r20/external/XNNPACK/src/f16-f32-vcvt/neonfp16.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1// Copyright 2021 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 % 8 == 0
7$assert BATCH_TILE >= 8
8$SIMD_TILE = BATCH_TILE // 8
9#include <assert.h>
10
11#include <arm_neon.h>
12
13#include <xnnpack/common.h>
14#include <xnnpack/vcvt.h>
15
16
17void xnn_f16_f32_vcvt_ukernel__neonfp16_x${BATCH_TILE}(
18    size_t n,
19    const void* input,
20    float* output,
21    const union xnn_f16_f32_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
22{
23  assert(n != 0);
24  assert(n % sizeof(uint16_t) == 0);
25  assert(input != NULL);
26  assert(output != NULL);
27
28  const uint16_t* i = (const uint16_t*) input;
29  $if BATCH_TILE > 8:
30    for (; n >= ${BATCH_TILE} * sizeof(uint16_t); n -= ${BATCH_TILE} * sizeof(uint16_t)) {
31      $for N in range(SIMD_TILE):
32        const float16x8_t vh${N} = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8;
33
34      $for N in range(SIMD_TILE):
35        const float32x4_t vf${2*N} = vcvt_f32_f16(vget_low_f16(vh${N}));
36        const float32x4_t vf${2*N+1} = vcvt_f32_f16(vget_high_f16(vh${N}));
37
38      $for N in range(2*SIMD_TILE):
39        vst1q_f32(output, vf${N}); output += 4;
40    }
41  for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
42    const float16x8_t vh = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8;
43
44    const float32x4_t vf_lo = vcvt_f32_f16(vget_low_f16(vh));
45    const float32x4_t vf_hi = vcvt_f32_f16(vget_high_f16(vh));
46
47    vst1q_f32(output, vf_lo); output += 4;
48    vst1q_f32(output, vf_hi); output += 4;
49  }
50  if XNN_UNLIKELY(n != 0) {
51    assert(n >= 1 * sizeof(uint16_t));
52    assert(n <= 7 * sizeof(uint16_t));
53    const float16x8_t vh = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8;
54
55    float32x4_t vf = vcvt_f32_f16(vget_low_f16(vh));
56    if (n & (4 * sizeof(uint16_t))) {
57      vst1q_f32(output, vf); output += 4;
58      vf = vcvt_f32_f16(vget_high_f16(vh));
59    }
60    float32x2_t vf_lo = vget_low_f32(vf);
61    if (n & (2 * sizeof(uint16_t))) {
62      vst1_f32(output, vf_lo); output += 2;
63      vf_lo = vget_high_f32(vf);
64    }
65    if (n & (1 * sizeof(uint16_t))) {
66      vst1_lane_f32(output, vf_lo, 0);
67    }
68  }
69}
70