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 % 8 == 0 7$assert BATCH_TILE >= 8 8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 9#include <assert.h> 10 11#include <immintrin.h> 12 13#include <xnnpack/vunary.h> 14#include <xnnpack/common.h> 15 16 17static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0}; 18 19void xnn_f32_relu_ukernel__avx_x${BATCH_TILE}( 20 size_t n, 21 const float* x, 22 float* y, 23 const union xnn_f32_relu_params params[restrict XNN_MIN_ELEMENTS(1)]) 24{ 25 assert(n != 0); 26 assert(n % sizeof(float) == 0); 27 assert(x != NULL); 28 assert(y != NULL); 29 30 const __m256 vzero = _mm256_setzero_ps(); 31 32 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { 33 __m256 vacc${ABC[0:8]} = _mm256_loadu_ps(x); 34 $for N in range(8, BATCH_TILE, 8): 35 __m256 vacc${ABC[N:N+8]} = _mm256_loadu_ps(x + ${N}); 36 x += ${BATCH_TILE}; 37 38 $for N in range(0, BATCH_TILE, 8): 39 vacc${ABC[N:N+8]} = _mm256_max_ps(vacc${ABC[N:N+8]}, vzero); 40 41 _mm256_storeu_ps(y, vacc${ABC[0:8]}); 42 $for N in range(8, BATCH_TILE, 8): 43 _mm256_storeu_ps(y + ${N}, vacc${ABC[N:N+8]}); 44 y += ${BATCH_TILE}; 45 } 46 $if BATCH_TILE > 8: 47 for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) { 48 __m256 vacc = _mm256_loadu_ps(x); 49 x += 8; 50 51 vacc = _mm256_max_ps(vacc, vzero); 52 53 _mm256_storeu_ps(y, vacc); 54 y += 8; 55 } 56 if XNN_UNLIKELY(n != 0) { 57 assert(n >= 1 * sizeof(float)); 58 assert(n <= 7 * sizeof(float)); 59 __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n)); 60 61 __m256 vacc = _mm256_maskload_ps(x, vmask); 62 vacc = _mm256_max_ps(vacc, vzero); 63 64 // _mm256_maskstore_ps(y, vmask, vacc) could be used here, but triggers msan failures (probably an msan bug). 65 __m128 vacc_lo = _mm256_castps256_ps128(vacc); 66 if (n & (4 * sizeof(float))) { 67 _mm_storeu_ps(y, vacc_lo); 68 vacc_lo = _mm256_extractf128_ps(vacc, 1); 69 y += 4; 70 } 71 if (n & (2 * sizeof(float))) { 72 _mm_storel_pi((__m64*) y, vacc_lo); 73 vacc_lo = _mm_movehl_ps(vacc_lo, vacc_lo); 74 y += 2; 75 } 76 if (n & (1 * sizeof(float))) { 77 _mm_store_ss(y, vacc_lo); 78 } 79 } 80} 81