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 % 4 == 0 7$assert BATCH_TILE >= 4 8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 9$assert OP in ["ADD", "DIV", "MAX", "MIN", "MUL", "SUB", "SQRDIFF"] 10$assert ACTIVATION in ["LINEAR", "MINMAX"] 11#include <assert.h> 12 13#include <xmmintrin.h> 14 15#include <xnnpack/common.h> 16#include <xnnpack/intrinsics-polyfill.h> 17#include <xnnpack/vbinary.h> 18 19 20$_MM_OP_PS = { 21$ "ADD": lambda x, y: "_mm_add_ps(%s, %s)" % (x, y), 22$ "DIV": lambda x, y: "_mm_div_ps(%s, %s)" % (x, y), 23$ "MAX": lambda x, y: "_mm_max_ps(%s, %s)" % (x, y), 24$ "MIN": lambda x, y: "_mm_min_ps(%s, %s)" % (x, y), 25$ "MUL": lambda x, y: "_mm_mul_ps(%s, %s)" % (x, y), 26$ "SUB": lambda x, y: "_mm_sub_ps(%s, %s)" % (x, y), 27$ "SQRDIFF": lambda x, y: "_mm_sub_ps(%s, %s)" % (x, y), 28$}[OP] 29$SUFFIX = {"LINEAR": "", "MINMAX": "_minmax"}[ACTIVATION] 30$PARAMS = {"LINEAR": "xnn_f32_default_params", "MINMAX": "xnn_f32_minmax_params"}[ACTIVATION] 31void xnn_f32_v${OP.lower()}${SUFFIX}_ukernel__sse_x${BATCH_TILE}( 32 size_t n, 33 const float* a, 34 const float* b, 35 float* y, 36 const union ${PARAMS} params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN 37{ 38 assert(n != 0); 39 assert(n % sizeof(float) == 0); 40 assert(a != NULL); 41 assert(b != NULL); 42 assert(y != NULL); 43 44 $if ACTIVATION == "MINMAX": 45 const __m128 vy_min = _mm_load_ps(params->sse.min); 46 const __m128 vy_max = _mm_load_ps(params->sse.max); 47 48 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { 49 const __m128 va${ABC[0:4]} = _mm_loadu_ps(a); 50 $for N in range(4, BATCH_TILE, 4): 51 const __m128 va${ABC[N:N+4]} = _mm_loadu_ps(a + ${N}); 52 a += ${BATCH_TILE}; 53 54 const __m128 vb${ABC[0:4]} = _mm_loadu_ps(b); 55 $for N in range(4, BATCH_TILE, 4): 56 const __m128 vb${ABC[N:N+4]} = _mm_loadu_ps(b + ${N}); 57 b += ${BATCH_TILE}; 58 59 $for N in range(0, BATCH_TILE, 4): 60 __m128 vy${ABC[N:N+4]} = ${_MM_OP_PS("va" + ABC[N:N+4], "vb" + ABC[N:N+4])}; 61 62 $if OP == "SQRDIFF": 63 $for N in range(0, BATCH_TILE, 4): 64 vy${ABC[N:N+4]} = _mm_mul_ps(vy${ABC[N:N+4]}, vy${ABC[N:N+4]}); 65 66 $if ACTIVATION == "MINMAX": 67 $for N in range(0, BATCH_TILE, 4): 68 vy${ABC[N:N+4]} = _mm_max_ps(vy${ABC[N:N+4]}, vy_min); 69 70 $for N in range(0, BATCH_TILE, 4): 71 vy${ABC[N:N+4]} = _mm_min_ps(vy${ABC[N:N+4]}, vy_max); 72 73 _mm_storeu_ps(y, vy${ABC[0:4]}); 74 $for N in range(4, BATCH_TILE, 4): 75 _mm_storeu_ps(y + ${N}, vy${ABC[N:N+4]}); 76 y += ${BATCH_TILE}; 77 } 78 $if BATCH_TILE > 4: 79 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) { 80 const __m128 va0123 = _mm_loadu_ps(a); 81 a += 4; 82 83 const __m128 vb0123 = _mm_loadu_ps(b); 84 b += 4; 85 86 __m128 vy0123 = ${_MM_OP_PS("va0123", "vb0123")}; 87 $if OP == "SQRDIFF": 88 vy0123 = _mm_mul_ps(vy0123, vy0123); 89 $if ACTIVATION == "MINMAX": 90 vy0123 = _mm_max_ps(vy0123, vy_min); 91 vy0123 = _mm_min_ps(vy0123, vy_max); 92 _mm_storeu_ps(y, vy0123); 93 y += 4; 94 } 95 if XNN_UNLIKELY(n != 0) { 96 const __m128 va0123 = _mm_loadu_ps(a); 97 const __m128 vb0123 = _mm_loadu_ps(b); 98 99 __m128 vy0123 = ${_MM_OP_PS("va0123", "vb0123")}; 100 $if OP == "SQRDIFF": 101 vy0123 = _mm_mul_ps(vy0123, vy0123); 102 $if ACTIVATION == "MINMAX": 103 vy0123 = _mm_max_ps(vy0123, vy_min); 104 vy0123 = _mm_min_ps(vy0123, vy_max); 105 if (n & (2 * sizeof(float))) { 106 _mm_storel_pi((__m64*) y, vy0123); 107 vy0123 = _mm_movehl_ps(vy0123, vy0123); 108 y += 2; 109 } 110 if (n & (1 * sizeof(float))) { 111 _mm_store_ss(y, vy0123); 112 } 113 } 114} 115