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