xref: /aosp_15_r20/external/XNNPACK/src/f32-f16-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_f32_f16_vcvt_ukernel__neonfp16_x${BATCH_TILE}(
18    size_t n,
19    const float* input,
20    void* output,
21    const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
22{
23  assert(n != 0);
24  assert(n % sizeof(float) == 0);
25  assert(input != NULL);
26  assert(output != NULL);
27
28  uint16_t* o = (uint16_t*) output;
29  for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
30    $for N in range(2*SIMD_TILE):
31      const float32x4_t vf${N} = vld1q_f32(input); input += 4;
32
33    $for N in range(SIMD_TILE):
34      const uint16x8_t vh${N} = vreinterpretq_u16_f16(vcombine_f16(vcvt_f16_f32(vf${2*N}), vcvt_f16_f32(vf${2*N+1})));
35
36    $for N in range(SIMD_TILE):
37      vst1q_u16(o, vh${N}); o += 8;
38  }
39  for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
40    const float32x4_t vf = vld1q_f32(input); input += 4;
41
42    const uint16x4_t vh = vreinterpret_u16_f16(vcvt_f16_f32(vf));
43
44    vst1_u16(o, vh); o += 4;
45  }
46  if XNN_UNLIKELY(n != 0) {
47    assert(n % sizeof(float) == 0);
48    assert(n >= 1 * sizeof(float));
49    assert(n <= 3 * sizeof(float));
50    const float32x4_t vf = vld1q_f32(input);
51
52    uint16x4_t vh = vreinterpret_u16_f16(vcvt_f16_f32(vf));
53
54    if (n & (2 * sizeof(float))) {
55      vst1_lane_u32((void*) o, vreinterpret_u32_u16(vh), 0); o += 2;
56      vh = vext_u16(vh, vh, 2);
57    }
58    if (n & (1 * sizeof(float))) {
59      vst1_lane_u16(o, vh, 0);
60    }
61  }
62}
63