xref: /aosp_15_r20/external/XNNPACK/src/f16-vsqrt/gen/f16c-sqrt-x16.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f16-vsqrt/f16c-sqrt.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2022 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/vunary.h>
17 
18 
xnn_f16_vsqrt_ukernel__f16c_sqrt_x16(size_t n,const void * input,void * output,const union xnn_f16_sqrt_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_f16_vsqrt_ukernel__f16c_sqrt_x16(
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   for (; n >= 16 * sizeof(uint16_t); n -= 16 * sizeof(uint16_t)) {
33     __m256 vacc0 = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) i));
34     __m256 vacc1 = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) (i + 8)));
35     i += 16;
36 
37     vacc0 = _mm256_sqrt_ps(vacc0);
38     vacc1 = _mm256_sqrt_ps(vacc1);
39 
40     _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vacc0, _MM_FROUND_NO_EXC));
41     _mm_storeu_si128((__m128i*) (o + 8), _mm256_cvtps_ph(vacc1, _MM_FROUND_NO_EXC));
42     o += 16;
43   }
44   for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
45     __m256 vacc = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) i));
46     i += 8;
47     vacc = _mm256_sqrt_ps(vacc);
48     _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vacc, _MM_FROUND_NO_EXC));
49     o += 8;
50   }
51   if XNN_UNLIKELY(n != 0) {
52     __m256 vacc = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) i));
53     vacc = _mm256_sqrt_ps(vacc);
54     __m128i vh = _mm256_cvtps_ph(vacc, _MM_FROUND_NO_EXC);
55     if (n & (4 * sizeof(uint16_t))) {
56       _mm_storel_epi64((__m128i*) o, vh);
57       o += 4;
58       vh = _mm_unpackhi_epi64(vh, vh);
59     }
60     if (n & (2 * sizeof(uint16_t))) {
61       _mm_storeu_si32(o, vh);
62       o += 2;
63       vh = _mm_srli_epi64(vh, 32);
64     }
65     if (n & (1 * sizeof(uint16_t))) {
66       *o = (uint16_t) _mm_extract_epi16(vh, 0);
67     }
68   }
69 }
70