1// Copyright 2020 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 % 4 == 0
7$assert BATCH_TILE >= 4
8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9#include <assert.h>
10
11#include <emmintrin.h>
12
13#include <xnnpack/common.h>
14#include <xnnpack/math.h>
15#include <xnnpack/vunary.h>
16
17
18void xnn_f32_vrndu_ukernel__sse2_x${BATCH_TILE}(
19    size_t n,
20    const float* x,
21    float* y,
22    const union xnn_f32_rnd_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
23{
24  assert(n != 0);
25  assert(n % sizeof(float) == 0);
26
27  const __m128i vmagic = _mm_load_si128((const __m128i*) params->sse2.sign_mask);
28  const __m128 vone = _mm_load_ps(params->sse2.one);
29  for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
30    const __m128 vx${ABC[0:4]} = _mm_loadu_ps(x);
31    $for N in range(4, BATCH_TILE, 4):
32      const __m128 vx${ABC[N:N+4]} = _mm_loadu_ps(x + ${N});
33    x += ${BATCH_TILE};
34
35    $for N in range(0, BATCH_TILE, 4):
36      const __m128i vintx${ABC[N:N+4]} = _mm_cvttps_epi32(vx${ABC[N:N+4]});
37
38    $for N in range(0, BATCH_TILE, 4):
39      const __m128 vrndmask${ABC[N:N+4]} = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx${ABC[N:N+4]}, vmagic)));
40
41    $for N in range(0, BATCH_TILE, 4):
42      const __m128 vprerndx${ABC[N:N+4]} = _mm_cvtepi32_ps(vintx${ABC[N:N+4]});
43
44    $for N in range(0, BATCH_TILE, 4):
45      const __m128 vrndx${ABC[N:N+4]} = _mm_or_ps(_mm_and_ps(vx${ABC[N:N+4]}, vrndmask${ABC[N:N+4]}), _mm_andnot_ps(vrndmask${ABC[N:N+4]}, vprerndx${ABC[N:N+4]}));
46
47    $for N in range(0, BATCH_TILE, 4):
48      const __m128 vadjmask${ABC[N:N+4]} = _mm_or_ps(_mm_cmpge_ps(vrndx${ABC[N:N+4]}, vx${ABC[N:N+4]}), _mm_castsi128_ps(vmagic));
49
50    $for N in range(0, BATCH_TILE, 4):
51      const __m128 vadjrndx${ABC[N:N+4]} = _mm_add_ps(vrndx${ABC[N:N+4]}, vone);
52
53    $for N in range(0, BATCH_TILE, 4):
54      const __m128 vy${ABC[N:N+4]} = _mm_or_ps(_mm_and_ps(vrndx${ABC[N:N+4]}, vadjmask${ABC[N:N+4]}), _mm_andnot_ps(vadjmask${ABC[N:N+4]}, vadjrndx${ABC[N:N+4]}));
55
56    _mm_storeu_ps(y, vy${ABC[0:4]});
57    $for N in range(4, BATCH_TILE, 4):
58      _mm_storeu_ps(y + ${N}, vy${ABC[N:N+4]});
59    y += ${BATCH_TILE};
60  }
61  $if BATCH_TILE > 4:
62    for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
63      const __m128 vx = _mm_loadu_ps(x);
64      x += 4;
65
66      const __m128i vintx = _mm_cvttps_epi32(vx);
67      const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic)));
68      const __m128 vprerndx = _mm_cvtepi32_ps(vintx);
69      const __m128 vrndx = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vprerndx));
70      const __m128 vadjmask = _mm_or_ps(_mm_cmpge_ps(vrndx, vx), _mm_castsi128_ps(vmagic));
71      const __m128 vadjrndx = _mm_add_ps(vrndx, vone);
72      const __m128 vy = _mm_or_ps(_mm_and_ps(vrndx, vadjmask), _mm_andnot_ps(vadjmask, vadjrndx));
73
74      _mm_storeu_ps(y, vy);
75      y += 4;
76    }
77  if XNN_UNLIKELY(n != 0) {
78    const __m128 vx = _mm_loadu_ps(x);
79    const __m128i vintx = _mm_cvttps_epi32(vx);
80    const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic)));
81    const __m128 vprerndx = _mm_cvtepi32_ps(vintx);
82    const __m128 vrndx = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vprerndx));
83    const __m128 vadjmask = _mm_or_ps(_mm_cmpge_ps(vrndx, vx), _mm_castsi128_ps(vmagic));
84    const __m128 vadjrndx = _mm_add_ps(vrndx, vone);
85    __m128 vy = _mm_or_ps(_mm_and_ps(vrndx, vadjmask), _mm_andnot_ps(vadjmask, vadjrndx));
86    if (n & (2 * sizeof(float))) {
87      _mm_storel_pi((__m64*) y, vy);
88      vy = _mm_movehl_ps(vy, vy);
89      y += 2;
90    }
91    if (n & (1 * sizeof(float))) {
92      _mm_store_ss(y, vy);
93    }
94  }
95}
96