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_vrndd_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 vy${ABC[N:N+4]} = _mm_sub_ps(vrndx${ABC[N:N+4]}, _mm_and_ps(_mm_cmpgt_ps(vrndx${ABC[N:N+4]}, vx${ABC[N:N+4]}), vone));
49
50    _mm_storeu_ps(y, vy${ABC[0:4]});
51    $for N in range(4, BATCH_TILE, 4):
52      _mm_storeu_ps(y + ${N}, vy${ABC[N:N+4]});
53    y += ${BATCH_TILE};
54  }
55  $if BATCH_TILE > 4:
56    for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
57      const __m128 vx = _mm_loadu_ps(x);
58      x += 4;
59
60      const __m128i vintx = _mm_cvttps_epi32(vx);
61      const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic)));
62      const __m128 vprerndx = _mm_cvtepi32_ps(vintx);
63      const __m128 vrndx = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vprerndx));
64      const __m128 vy = _mm_sub_ps(vrndx, _mm_and_ps(_mm_cmpgt_ps(vrndx, vx), vone));
65
66      _mm_storeu_ps(y, vy);
67      y += 4;
68    }
69  if XNN_UNLIKELY(n != 0) {
70    const __m128 vx = _mm_loadu_ps(x);
71    const __m128i vintx = _mm_cvttps_epi32(vx);
72    const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic)));
73    const __m128 vprerndx = _mm_cvtepi32_ps(vintx);
74    const __m128 vrndx = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vprerndx));
75    __m128 vy = _mm_sub_ps(vrndx, _mm_and_ps(_mm_cmpgt_ps(vrndx, vx), vone));
76    if (n & (2 * sizeof(float))) {
77      _mm_storel_pi((__m64*) y, vy);
78      vy = _mm_movehl_ps(vy, vy);
79      y += 2;
80    }
81    if (n & (1 * sizeof(float))) {
82      _mm_store_ss(y, vy);
83    }
84  }
85}
86