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_vrndne_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 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { 29 const __m128 vx${ABC[0:4]} = _mm_loadu_ps(x); 30 $for N in range(4, BATCH_TILE, 4): 31 const __m128 vx${ABC[N:N+4]} = _mm_loadu_ps(x + ${N}); 32 x += ${BATCH_TILE}; 33 34 $for N in range(0, BATCH_TILE, 4): 35 const __m128i vintx${ABC[N:N+4]} = _mm_cvtps_epi32(vx${ABC[N:N+4]}); 36 37 $for N in range(0, BATCH_TILE, 4): 38 const __m128 vrndmask${ABC[N:N+4]} = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx${ABC[N:N+4]}, vmagic))); 39 40 $for N in range(0, BATCH_TILE, 4): 41 const __m128 vrndx${ABC[N:N+4]} = _mm_cvtepi32_ps(vintx${ABC[N:N+4]}); 42 43 $for N in range(0, BATCH_TILE, 4): 44 const __m128 vy${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]}, vrndx${ABC[N:N+4]})); 45 46 _mm_storeu_ps(y, vy${ABC[0:4]}); 47 $for N in range(4, BATCH_TILE, 4): 48 _mm_storeu_ps(y + ${N}, vy${ABC[N:N+4]}); 49 y += ${BATCH_TILE}; 50 } 51 $if BATCH_TILE > 4: 52 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) { 53 const __m128 vx = _mm_loadu_ps(x); 54 x += 4; 55 56 const __m128i vintx = _mm_cvtps_epi32(vx); 57 const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic))); 58 const __m128 vrndx = _mm_cvtepi32_ps(vintx); 59 const __m128 vy = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vrndx)); 60 61 _mm_storeu_ps(y, vy); 62 y += 4; 63 } 64 if XNN_UNLIKELY(n != 0) { 65 const __m128 vx = _mm_loadu_ps(x); 66 const __m128i vintx = _mm_cvtps_epi32(vx); 67 const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic))); 68 const __m128 vrndx = _mm_cvtepi32_ps(vintx); 69 __m128 vy = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vrndx)); 70 if (n & (2 * sizeof(float))) { 71 _mm_storel_pi((__m64*) y, vy); 72 vy = _mm_movehl_ps(vy, vy); 73 y += 2; 74 } 75 if (n & (1 * sizeof(float))) { 76 _mm_store_ss(y, vy); 77 } 78 } 79} 80