1 // Auto-generated file. Do not edit!
2 // Template: src/f16-vclamp/f16c.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/vunary.h>
16
17
xnn_f16_vclamp_ukernel__f16c_x16(size_t n,const void * restrict x_ptr,void * restrict y_ptr,const union xnn_f16_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f16_vclamp_ukernel__f16c_x16(
19 size_t n,
20 const void* restrict x_ptr,
21 void* restrict y_ptr,
22 const union xnn_f16_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
23 {
24 assert(n != 0);
25 assert(n % sizeof(uint16_t) == 0);
26 assert(x_ptr != NULL);
27 assert(y_ptr != NULL);
28
29 const uint16_t* x = (const uint16_t*) x_ptr;
30 uint16_t* y = (uint16_t*) y_ptr;
31
32 const __m256 vy_min = _mm256_load_ps(params->avx.min);
33 const __m256 vy_max = _mm256_load_ps(params->avx.max);
34
35 for (; n >= 16 * sizeof(uint16_t); n -= 16 * sizeof(uint16_t)) {
36 __m256 vacc01234567 = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) x));
37 __m256 vacc89ABCDEF = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) (x + 8)));
38 x += 16;
39
40 vacc01234567 = _mm256_max_ps(vacc01234567, vy_min);
41 vacc89ABCDEF = _mm256_max_ps(vacc89ABCDEF, vy_min);
42
43 vacc01234567 = _mm256_min_ps(vacc01234567, vy_max);
44 vacc89ABCDEF = _mm256_min_ps(vacc89ABCDEF, vy_max);
45
46 _mm_storeu_si128((__m128i*) y, _mm256_cvtps_ph(vacc01234567, _MM_FROUND_NO_EXC));
47 _mm_storeu_si128((__m128i*) (y + 8), _mm256_cvtps_ph(vacc89ABCDEF, _MM_FROUND_NO_EXC));
48 y += 16;
49 }
50 for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
51 __m256 vacc = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) x));
52 x += 8;
53 vacc = _mm256_max_ps(vacc, vy_min);
54 vacc = _mm256_min_ps(vacc, vy_max);
55 _mm_storeu_si128((__m128i*) y, _mm256_cvtps_ph(vacc, _MM_FROUND_NO_EXC));
56 y += 8;
57 }
58 if XNN_UNLIKELY(n != 0) {
59 __m256 vacc = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) x));
60 vacc = _mm256_max_ps(vacc, vy_min);
61 vacc = _mm256_min_ps(vacc, vy_max);
62
63 __m128i vh = _mm256_cvtps_ph(vacc, _MM_FROUND_NO_EXC);
64 if (n & (4 * sizeof(uint16_t))) {
65 _mm_storel_epi64((__m128i*) y, vh);
66 vh = _mm_unpackhi_epi64(vh, vh);
67 y += 4;
68 }
69 if (n & (2 * sizeof(uint16_t))) {
70 *((uint32_t*) y) = (uint32_t) _mm_cvtsi128_si32(vh);
71 vh = _mm_srli_epi64(vh, 32);
72 y += 2;
73 }
74 if (n & (1 * sizeof(uint16_t))) {
75 *y = _mm_extract_epi16(vh, 0);
76 }
77 }
78 }
79