• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9#include <assert.h>
10
11#include <immintrin.h>
12
13#include <xnnpack/common.h>
14#include <xnnpack/vunary.h>
15
16
17void xnn_f16_vclamp_ukernel__f16c_x${BATCH_TILE}(
18    size_t n,
19    const void* restrict x_ptr,
20    void* restrict y_ptr,
21    const union xnn_f16_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
22{
23  assert(n != 0);
24  assert(n % sizeof(uint16_t) == 0);
25  assert(x_ptr != NULL);
26  assert(y_ptr != NULL);
27
28  const uint16_t* x = (const uint16_t*) x_ptr;
29  uint16_t* y = (uint16_t*) y_ptr;
30
31  const __m256 vy_min = _mm256_load_ps(params->avx.min);
32  const __m256 vy_max = _mm256_load_ps(params->avx.max);
33
34  $if BATCH_TILE > 8:
35    for (; n >= ${BATCH_TILE} * sizeof(uint16_t); n -= ${BATCH_TILE} * sizeof(uint16_t)) {
36      __m256 vacc${ABC[0:8]} = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) x));
37      $for N in range(8, BATCH_TILE, 8):
38        __m256 vacc${ABC[N:N+8]} = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) (x + ${N})));
39      x += ${BATCH_TILE};
40
41      $for N in range(0, BATCH_TILE, 8):
42        vacc${ABC[N:N+8]} = _mm256_max_ps(vacc${ABC[N:N+8]}, vy_min);
43
44      $for N in range(0, BATCH_TILE, 8):
45        vacc${ABC[N:N+8]} = _mm256_min_ps(vacc${ABC[N:N+8]}, vy_max);
46
47      _mm_storeu_si128((__m128i*) y, _mm256_cvtps_ph(vacc${ABC[0:8]}, _MM_FROUND_NO_EXC));
48      $for N in range(8, BATCH_TILE, 8):
49        _mm_storeu_si128((__m128i*) (y + ${N}), _mm256_cvtps_ph(vacc${ABC[N:N+8]}, _MM_FROUND_NO_EXC));
50      y += ${BATCH_TILE};
51    }
52  for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
53    __m256 vacc = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) x));
54    x += 8;
55    vacc = _mm256_max_ps(vacc, vy_min);
56    vacc = _mm256_min_ps(vacc, vy_max);
57    _mm_storeu_si128((__m128i*) y, _mm256_cvtps_ph(vacc, _MM_FROUND_NO_EXC));
58    y += 8;
59  }
60  if XNN_UNLIKELY(n != 0) {
61    __m256 vacc = _mm256_cvtph_ps(_mm_loadu_si128((const __m128i*) x));
62    vacc = _mm256_max_ps(vacc, vy_min);
63    vacc = _mm256_min_ps(vacc, vy_max);
64
65    __m128i vh = _mm256_cvtps_ph(vacc, _MM_FROUND_NO_EXC);
66    if (n & (4 * sizeof(uint16_t))) {
67      _mm_storel_epi64((__m128i*) y, vh);
68      vh = _mm_unpackhi_epi64(vh, vh);
69      y += 4;
70    }
71    if (n & (2 * sizeof(uint16_t))) {
72      *((uint32_t*) y) = (uint32_t) _mm_cvtsi128_si32(vh);
73      vh = _mm_srli_epi64(vh, 32);
74      y += 2;
75    }
76    if (n & (1 * sizeof(uint16_t))) {
77      *y = _mm_extract_epi16(vh, 0);
78    }
79  }
80}
81