xref: /aosp_15_r20/external/XNNPACK/src/f32-f16-vcvt/gen/vcvt-f16c-x16.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-f16-vcvt/f16c.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2021 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9 
10 #include <assert.h>
11 
12 #include <immintrin.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/intrinsics-polyfill.h>
16 #include <xnnpack/vcvt.h>
17 
18 
xnn_f32_f16_vcvt_ukernel__f16c_x16(size_t n,const float * input,void * output,const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_f32_f16_vcvt_ukernel__f16c_x16(
20     size_t n,
21     const float* input,
22     void* output,
23     const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
24 {
25   assert(n != 0);
26   assert(n % sizeof(float) == 0);
27   assert(input != NULL);
28   assert(output != NULL);
29 
30   uint16_t* o = (uint16_t*) output;
31   for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
32     const __m256 vf0 = _mm256_loadu_ps(input);
33     const __m256 vf1 = _mm256_loadu_ps(input + 8);
34     input += 16;
35 
36     _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vf0, _MM_FROUND_NO_EXC));
37     _mm_storeu_si128((__m128i*) (o + 8), _mm256_cvtps_ph(vf1, _MM_FROUND_NO_EXC));
38     o += 16;
39   }
40   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
41     const __m256 vf = _mm256_loadu_ps(input);
42     input += 8;
43 
44     _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vf, _MM_FROUND_NO_EXC));
45     o += 8;
46   }
47   if XNN_UNLIKELY(n != 0) {
48     assert(n >= 1 * sizeof(float));
49     assert(n <= 7 * sizeof(float));
50     const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &params->f16c.mask_table[7] - n));
51 
52     const __m256 vf = _mm256_maskload_ps(input, vmask);
53 
54     __m128 vf_lo = _mm256_castps256_ps128(vf);
55     if (n & (4 * sizeof(float))) {
56       _mm_storel_epi64((__m128i*) o, _mm_cvtps_ph(vf_lo, _MM_FROUND_NO_EXC));
57       vf_lo = _mm256_extractf128_ps(vf, 1);
58       o += 4;
59     }
60     __m128i vh = _mm_cvtps_ph(vf_lo, _MM_FROUND_NO_EXC);
61     if (n & (2 * sizeof(float))) {
62       _mm_storeu_si32(o, vh);
63       vh = _mm_srli_epi64(vh, 32);
64       o += 2;
65     }
66     if (n & (1 * sizeof(float))) {
67       *((uint16_t*) o) = _mm_extract_epi16(vh, 0);
68     }
69   }
70 }
71