1// Copyright 2019 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$assert OP in ["ADD", "DIV", "RDIV", "MAX", "MIN", "MUL", "SUB", "RSUB", "SQRDIFF"] 10$assert ACTIVATION in ["LINEAR", "MINMAX"] 11#include <assert.h> 12 13#include <immintrin.h> 14 15#include <xnnpack/common.h> 16#include <xnnpack/vbinary.h> 17 18 19static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0}; 20 21$_MM256_OP_PS = { 22$ "ADD": lambda x: "_mm256_add_ps(%s, vb)" % x, 23$ "DIV": lambda x: "_mm256_div_ps(%s, vb)" % x, 24$ "RDIV": lambda x: "_mm256_div_ps(vb, %s)" % x, 25$ "MAX": lambda x: "_mm256_max_ps(%s, vb)" % x, 26$ "MIN": lambda x: "_mm256_min_ps(%s, vb)" % x, 27$ "MUL": lambda x: "_mm256_mul_ps(%s, vb)" % x, 28$ "SUB": lambda x: "_mm256_sub_ps(%s, vb)" % x, 29$ "RSUB": lambda x: "_mm256_sub_ps(vb, %s)" % x, 30$ "SQRDIFF": lambda x: "_mm256_sub_ps(%s, vb)" % x, 31$}[OP] 32$SUFFIX = {"LINEAR": "", "MINMAX": "_minmax"}[ACTIVATION] 33$PARAMS = {"LINEAR": "xnn_f32_default_params", "MINMAX": "xnn_f32_minmax_params"}[ACTIVATION] 34void xnn_f32_v${OP.lower()}c${SUFFIX}_ukernel__avx_x${BATCH_TILE}( 35 size_t n, 36 const float* a, 37 const float* b, 38 float* y, 39 const union ${PARAMS} params[restrict XNN_MIN_ELEMENTS(1)]) 40{ 41 assert(n != 0); 42 assert(n % sizeof(float) == 0); 43 assert(a != NULL); 44 assert(b != NULL); 45 assert(y != NULL); 46 47 $if ACTIVATION == "MINMAX": 48 const __m256 vy_min = _mm256_broadcast_ps((const __m128*) params->sse.min); 49 const __m256 vy_max = _mm256_broadcast_ps((const __m128*) params->sse.max); 50 51 const __m256 vb = _mm256_broadcast_ss(b); 52 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { 53 const __m256 va${ABC[0:8]} = _mm256_loadu_ps(a); 54 $for N in range(8, BATCH_TILE, 8): 55 const __m256 va${ABC[N:N+8]} = _mm256_loadu_ps(a + ${N}); 56 a += ${BATCH_TILE}; 57 58 $for N in range(0, BATCH_TILE, 8): 59 __m256 vy${ABC[N:N+8]} = ${_MM256_OP_PS("va" + ABC[N:N+8])}; 60 61 $if OP == "SQRDIFF": 62 $for N in range(0, BATCH_TILE, 8): 63 vy${ABC[N:N+8]} = _mm256_mul_ps(vy${ABC[N:N+8]}, vy${ABC[N:N+8]}); 64 65 $if ACTIVATION == "MINMAX": 66 $for N in range(0, BATCH_TILE, 8): 67 vy${ABC[N:N+8]} = _mm256_max_ps(vy${ABC[N:N+8]}, vy_min); 68 69 $for N in range(0, BATCH_TILE, 8): 70 vy${ABC[N:N+8]} = _mm256_min_ps(vy${ABC[N:N+8]}, vy_max); 71 72 _mm256_storeu_ps(y, vy${ABC[0:8]}); 73 $for N in range(8, BATCH_TILE, 8): 74 _mm256_storeu_ps(y + ${N}, vy${ABC[N:N+8]}); 75 y += ${BATCH_TILE}; 76 } 77 $if BATCH_TILE > 8: 78 for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) { 79 const __m256 va = _mm256_loadu_ps(a); 80 a += 8; 81 82 __m256 vy = ${_MM256_OP_PS("va")}; 83 $if OP == "SQRDIFF": 84 vy = _mm256_mul_ps(vy, vy); 85 $if ACTIVATION == "MINMAX": 86 vy = _mm256_max_ps(vy, vy_min); 87 vy = _mm256_min_ps(vy, vy_max); 88 _mm256_storeu_ps(y, vy); 89 y += 8; 90 } 91 if XNN_UNLIKELY(n != 0) { 92 assert(n >= 1 * sizeof(float)); 93 assert(n <= 7 * sizeof(float)); 94 __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n)); 95 96 const __m256 va = _mm256_maskload_ps(a, vmask); 97 98 __m256 vy = ${_MM256_OP_PS("va")}; 99 $if OP == "SQRDIFF": 100 vy = _mm256_mul_ps(vy, vy); 101 $if ACTIVATION == "MINMAX": 102 vy = _mm256_max_ps(vy, vy_min); 103 vy = _mm256_min_ps(vy, vy_max); 104 105 // _mm256_maskstore_ps(y, vmask, vy) could be used here, but triggers msan failures (probably an msan bug). 106 __m128 vy_lo = _mm256_castps256_ps128(vy); 107 if (n & (4 * sizeof(float))) { 108 _mm_storeu_ps(y, vy_lo); 109 vy_lo = _mm256_extractf128_ps(vy, 1); 110 y += 4; 111 } 112 if (n & (2 * sizeof(float))) { 113 _mm_storel_pi((__m64*) y, vy_lo); 114 vy_lo = _mm_movehl_ps(vy_lo, vy_lo); 115 y += 2; 116 } 117 if (n & (1 * sizeof(float))) { 118 _mm_store_ss(y, vy_lo); 119 } 120 } 121} 122