xref: /aosp_15_r20/external/XNNPACK/src/f16-vsqrt/f16c-sqrt.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1// Copyright 2022 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$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10#include <assert.h>
11
12#include <immintrin.h>
13
14#include <xnnpack/common.h>
15#include <xnnpack/intrinsics-polyfill.h>
16#include <xnnpack/vunary.h>
17
18
19void xnn_f16_vsqrt_ukernel__f16c_sqrt_x${BATCH_TILE}(
20    size_t n,
21    const void* input,
22    void* output,
23    const union xnn_f16_sqrt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
24{
25  assert(n != 0);
26  assert(n % sizeof(uint16_t) == 0);
27  assert(input != NULL);
28  assert(output != NULL);
29
30  const uint16_t* i = (const uint16_t*) input;
31  uint16_t* o = (uint16_t*) output;
32  $if BATCH_TILE > 8:
33    for (; n >= ${BATCH_TILE} * sizeof(uint16_t); n -= ${BATCH_TILE} * sizeof(uint16_t)) {
34      __m256 vacc${ABC[0]} = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) i));
35      $for N in range(1, SIMD_TILE):
36        __m256 vacc${ABC[N]} = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) (i + ${N*8})));
37      i += ${BATCH_TILE};
38
39      $for N in range(SIMD_TILE):
40        vacc${ABC[N]} = _mm256_sqrt_ps(vacc${ABC[N]});
41
42      _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vacc${ABC[0]}, _MM_FROUND_NO_EXC));
43      $for N in range(1, SIMD_TILE):
44        _mm_storeu_si128((__m128i*) (o + ${N*8}), _mm256_cvtps_ph(vacc${ABC[N]}, _MM_FROUND_NO_EXC));
45      o += ${BATCH_TILE};
46    }
47  for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
48    __m256 vacc = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) i));
49    i += 8;
50    vacc = _mm256_sqrt_ps(vacc);
51    _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vacc, _MM_FROUND_NO_EXC));
52    o += 8;
53  }
54  if XNN_UNLIKELY(n != 0) {
55    __m256 vacc = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) i));
56    vacc = _mm256_sqrt_ps(vacc);
57    __m128i vh = _mm256_cvtps_ph(vacc, _MM_FROUND_NO_EXC);
58    if (n & (4 * sizeof(uint16_t))) {
59      _mm_storel_epi64((__m128i*) o, vh);
60      o += 4;
61      vh = _mm_unpackhi_epi64(vh, vh);
62    }
63    if (n & (2 * sizeof(uint16_t))) {
64      _mm_storeu_si32(o, vh);
65      o += 2;
66      vh = _mm_srli_epi64(vh, 32);
67    }
68    if (n & (1 * sizeof(uint16_t))) {
69      *o = (uint16_t) _mm_extract_epi16(vh, 0);
70    }
71  }
72}
73