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 % 16 == 0 7$assert BATCH_TILE >= 16 8$SIMD_TILE = BATCH_TILE // 16 9#include <assert.h> 10 11#include <immintrin.h> 12 13#include <xnnpack/common.h> 14#include <xnnpack/intrinsics-polyfill.h> 15#include <xnnpack/vunary.h> 16 17 18void xnn_f32_velu_ukernel__avx512f_rr1_lut16_p3_perm_x${BATCH_TILE}( 19 size_t n, 20 const float* x, 21 float* y, 22 const union xnn_f32_elu_params params[restrict XNN_MIN_ELEMENTS(1)]) 23{ 24 assert(n != 0); 25 assert(n % sizeof(float) == 0); 26 27 const __m512 vprescale = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.prescale)); 28 const __m512 valpha = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.alpha)); 29 const __m512 vbeta = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.beta)); 30 31 const __m512 vsat_cutoff = _mm512_set1_ps(-0x1.154246p+4f); 32 const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p19f); 33 const __m512 vlog2e = _mm512_set1_ps(0x1.715476p+0f); 34 const __m512i vtable = _mm512_set_epi32( 35 0x3F7D257D, 0x3F7AC0C7, 0x3F78CCDF, 0x3F7744FD, 0x3F76248C, 0x3F75672A, 0x3F7508A4, 0x3F7504F3, 36 0x3F75583F, 0x3F75FED7, 0x3F76F532, 0x3F7837F0, 0x3F79C3D3, 0x3F7B95C2, 0x3F7DAAC3, 0x3F800000); 37 const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f); 38 const __m512 vc3 = _mm512_set1_ps(0x1.55561Cp-3f); 39 const __m512 vc2 = _mm512_set1_ps(0x1.0001ECp-1f); 40 41 $if BATCH_TILE > 16: 42 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { 43 __m512 vx0 = _mm512_loadu_ps(x); 44 $for N in range(1, SIMD_TILE): 45 __m512 vx${N} = _mm512_loadu_ps(x + ${N * 16}); 46 x += ${BATCH_TILE}; 47 48 $for N in range(SIMD_TILE): 49 const __m512 vz${N} = _mm512_max_ps(vsat_cutoff, _mm512_mul_ps(vx${N}, vprescale)); 50 51 $for N in range(SIMD_TILE): 52 __m512 vn${N} = _mm512_fmadd_ps(vz${N}, vlog2e, vmagic_bias); 53 54 $for N in range(SIMD_TILE): 55 const __m512i ven${N} = _mm512_slli_epi32(_mm512_castps_si512(vn${N}), 19); 56 const __m512i vl${N} = _mm512_permutexvar_epi32(_mm512_castps_si512(vn${N}), vtable); 57 58 $for N in range(SIMD_TILE): 59 __m512 vs${N} = _mm512_castsi512_ps(_mm512_add_epi32(vl${N}, ven${N})); 60 vn${N} = _mm512_sub_ps(vn${N}, vmagic_bias); 61 62 $for N in range(SIMD_TILE): 63 __m512 vt${N} = _mm512_fmadd_ps(vn${N}, vminus_ln2, vz${N}); 64 65 $for N in range(SIMD_TILE): 66 __m512 vp${N} = _mm512_fmadd_ps(vc3, vt${N}, vc2); 67 68 $for N in range(SIMD_TILE): 69 vp${N} = _mm512_mul_ps(vp${N}, vt${N}); 70 vt${N} = _mm512_mul_ps(vt${N}, vs${N}); 71 72 $for N in range(SIMD_TILE): 73 vs${N} = _mm512_fmsub_ps(vs${N}, valpha, valpha); 74 75 $for N in range(SIMD_TILE): 76 vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vt${N}); 77 78 const __m512 vzero = _mm512_setzero_ps(); 79 $for N in range(SIMD_TILE): 80 __m512 vy${N} = _mm512_fmadd_ps(vp${N}, valpha, vs${N}); 81 const __mmask16 vsign${N} = _mm512_cmp_ps_mask(vx${N}, vzero, _CMP_NLT_US); 82 83 $for N in range(SIMD_TILE): 84 vy${N} = _mm512_mask_mul_ps(vy${N}, vsign${N}, vx${N}, vbeta); 85 86 _mm512_storeu_ps(y, vy0); 87 $for N in range(1, SIMD_TILE): 88 _mm512_storeu_ps(y + ${N * 16}, vy${N}); 89 y += ${BATCH_TILE}; 90 } 91 for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) { 92 __m512 vx = _mm512_loadu_ps(x); 93 x += 16; 94 95 const __m512 vz = _mm512_max_ps(vsat_cutoff, _mm512_mul_ps(vx, vprescale)); 96 const __mmask16 vsign = _mm512_cmp_ps_mask(vx, _mm512_setzero_ps(), _CMP_NLT_US); 97 98 __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); 99 const __m512i ven = _mm512_slli_epi32(_mm512_castps_si512(vn), 19); 100 const __m512i vl = _mm512_permutexvar_epi32(_mm512_castps_si512(vn), vtable); 101 __m512 vs = _mm512_castsi512_ps(_mm512_add_epi32(vl, ven)); 102 vn = _mm512_sub_ps(vn, vmagic_bias); 103 104 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); 105 106 __m512 vp = _mm512_fmadd_ps(vc3, vt, vc2); 107 vp = _mm512_mul_ps(vp, vt); 108 109 vt = _mm512_mul_ps(vt, vs); 110 vs = _mm512_fmsub_ps(vs, valpha, valpha); 111 vp = _mm512_fmadd_ps(vp, vt, vt); 112 __m512 vy = _mm512_fmadd_ps(vp, valpha, vs); 113 114 vy = _mm512_mask_mul_ps(vy, vsign, vx, vbeta); 115 116 _mm512_storeu_ps(y, vy); 117 y += 16; 118 } 119 if XNN_UNLIKELY(n != 0) { 120 assert(n >= 1 * sizeof(float)); 121 assert(n <= 15 * sizeof(float)); 122 // Prepare mask for valid 32-bit elements (depends on n). 123 n >>= 2 /* log2(sizeof(float)) */; 124 const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1))); 125 126 __m512 vx = _mm512_maskz_loadu_ps(vmask, x); 127 128 const __m512 vz = _mm512_max_ps(vsat_cutoff, _mm512_mul_ps(vx, vprescale)); 129 const __mmask16 vsign = _mm512_cmp_ps_mask(vx, _mm512_setzero_ps(), _CMP_NLT_US); 130 131 __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); 132 const __m512i ven = _mm512_slli_epi32(_mm512_castps_si512(vn), 19); 133 const __m512i vl = _mm512_permutexvar_epi32(_mm512_castps_si512(vn), vtable); 134 __m512 vs = _mm512_castsi512_ps(_mm512_add_epi32(vl, ven)); 135 vn = _mm512_sub_ps(vn, vmagic_bias); 136 137 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); 138 139 __m512 vp = _mm512_fmadd_ps(vc3, vt, vc2); 140 vp = _mm512_mul_ps(vp, vt); 141 142 vt = _mm512_mul_ps(vt, vs); 143 vs = _mm512_fmsub_ps(vs, valpha, valpha); 144 vp = _mm512_fmadd_ps(vp, vt, vt); 145 __m512 vy = _mm512_fmadd_ps(vp, valpha, vs); 146 147 vy = _mm512_mask_mul_ps(vy, vsign, vx, vbeta); 148 149 _mm512_mask_storeu_ps(y, vmask, vy); 150 } 151} 152