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 % 16 == 0 7$assert BATCH_TILE >= 16 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/intrinsics-polyfill.h> 17#include <xnnpack/vbinary.h> 18 19 20$_MM512_OP_PS = { 21$ "ADD": lambda x: "_mm512_add_ps(%s, vb)" % x, 22$ "DIV": lambda x: "_mm512_div_ps(%s, vb)" % x, 23$ "RDIV": lambda x: "_mm512_div_ps(vb, %s)" % x, 24$ "MAX": lambda x: "_mm512_max_ps(%s, vb)" % x, 25$ "MIN": lambda x: "_mm512_min_ps(%s, vb)" % x, 26$ "MUL": lambda x: "_mm512_mul_ps(%s, vb)" % x, 27$ "SUB": lambda x: "_mm512_sub_ps(%s, vb)" % x, 28$ "RSUB": lambda x: "_mm512_sub_ps(vb, %s)" % x, 29$ "SQRDIFF": lambda x: "_mm512_sub_ps(%s, vb)" % x, 30$}[OP] 31$SUFFIX = {"LINEAR": "", "MINMAX": "_minmax"}[ACTIVATION] 32$PARAMS = {"LINEAR": "xnn_f32_default_params", "MINMAX": "xnn_f32_minmax_params"}[ACTIVATION] 33void xnn_f32_v${OP.lower()}c${SUFFIX}_ukernel__avx512f_x${BATCH_TILE}( 34 size_t n, 35 const float* a, 36 const float* b, 37 float* y, 38 const union ${PARAMS} params[restrict XNN_MIN_ELEMENTS(1)]) 39{ 40 assert(n != 0); 41 assert(n % sizeof(float) == 0); 42 assert(a != NULL); 43 assert(b != NULL); 44 assert(y != NULL); 45 46 $if ACTIVATION == "MINMAX": 47 const __m512 vy_min = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.min)); 48 const __m512 vy_max = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.max)); 49 50 const __m512 vb = _mm512_set1_ps(*b); 51 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { 52 const __m512 va${ABC[0:16]} = _mm512_loadu_ps(a); 53 $for N in range(16, BATCH_TILE, 16): 54 const __m512 va${ABC[N:N+16]} = _mm512_loadu_ps(a + ${N}); 55 a += ${BATCH_TILE}; 56 57 $for N in range(0, BATCH_TILE, 16): 58 __m512 vy${ABC[N:N+16]} = ${_MM512_OP_PS("va" + ABC[N:N+16])}; 59 60 $if OP == "SQRDIFF": 61 $for N in range(0, BATCH_TILE, 16): 62 vy${ABC[N:N+16]} = _mm512_mul_ps(vy${ABC[N:N+16]}, vy${ABC[N:N+16]}); 63 64 $if ACTIVATION == "MINMAX": 65 $for N in range(0, BATCH_TILE, 16): 66 vy${ABC[N:N+16]} = _mm512_max_ps(vy${ABC[N:N+16]}, vy_min); 67 68 $for N in range(0, BATCH_TILE, 16): 69 vy${ABC[N:N+16]} = _mm512_min_ps(vy${ABC[N:N+16]}, vy_max); 70 71 _mm512_storeu_ps(y, vy${ABC[0:16]}); 72 $for N in range(16, BATCH_TILE, 16): 73 _mm512_storeu_ps(y + ${N}, vy${ABC[N:N+16]}); 74 y += ${BATCH_TILE}; 75 } 76 $if BATCH_TILE > 16: 77 for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) { 78 const __m512 va = _mm512_loadu_ps(a); 79 a += 16; 80 81 __m512 vy = ${_MM512_OP_PS("va")}; 82 $if OP == "SQRDIFF": 83 vy = _mm512_mul_ps(vy, vy); 84 $if ACTIVATION == "MINMAX": 85 vy = _mm512_max_ps(vy, vy_min); 86 vy = _mm512_min_ps(vy, vy_max); 87 _mm512_storeu_ps(y, vy); 88 y += 16; 89 } 90 if XNN_UNLIKELY(n != 0) { 91 assert(n >= 1 * sizeof(float)); 92 assert(n <= 15 * sizeof(float)); 93 // Prepare mask for valid 32-bit elements (depends on n). 94 n >>= 2 /* log2(sizeof(float)) */; 95 const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1))); 96 97 const __m512 va = _mm512_maskz_loadu_ps(vmask, a); 98 99 __m512 vy = ${_MM512_OP_PS("va")}; 100 $if OP == "SQRDIFF": 101 vy = _mm512_mul_ps(vy, vy); 102 $if ACTIVATION == "MINMAX": 103 vy = _mm512_max_ps(vy, vy_min); 104 vy = _mm512_min_ps(vy, vy_max); 105 _mm512_mask_storeu_ps(y, vmask, vy); 106 } 107} 108