xref: /aosp_15_r20/external/XNNPACK/src/f16-f32-vcvt/f16c.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 <immintrin.h>
12
13#include <xnnpack/common.h>
14#include <xnnpack/vcvt.h>
15
16
17void xnn_f16_f32_vcvt_ukernel__f16c_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      const __m256 vacc0 = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) i));
32      $for N in range(1, SIMD_TILE):
33        const __m256 vacc${N} = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) (i + ${N * 8})));
34      i += ${BATCH_TILE};
35
36      _mm256_storeu_ps(output, vacc0);
37      $for N in range(1, SIMD_TILE):
38        _mm256_storeu_ps(output + ${N * 8}, vacc${N});
39      output += ${BATCH_TILE};
40    }
41  for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
42    const __m256 vacc = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) i));
43    i += 8;
44
45    _mm256_storeu_ps(output, vacc);
46    output += 8;
47  }
48  if XNN_UNLIKELY(n != 0) {
49    assert(n >= 1 * sizeof(uint16_t));
50    assert(n <= 7 * sizeof(uint16_t));
51    const __m256 vacc = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) i));
52
53    __m128 vacc_lo = _mm256_castps256_ps128(vacc);
54    if (n & (4 * sizeof(uint16_t))) {
55      _mm_storeu_ps(output, vacc_lo);
56      vacc_lo = _mm256_extractf128_ps(vacc, 1);
57      output += 4;
58    }
59    if (n & (2 * sizeof(uint16_t))) {
60      _mm_storel_pi((__m64*) output, vacc_lo);
61      vacc_lo = _mm_movehl_ps(vacc_lo, vacc_lo);
62      output += 2;
63    }
64    if (n & (1 * sizeof(uint16_t))) {
65      _mm_store_ss(output, vacc_lo);
66    }
67  }
68}
69