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$assert DIV_ALGO in ["div", "nr1fma", "nr1fma1adj"] 9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 10$SIMD_TILE = BATCH_TILE // 16 11#include <assert.h> 12 13#include <immintrin.h> 14 15#include <xnnpack/common.h> 16#include <xnnpack/intrinsics-polyfill.h> 17#include <xnnpack/vunary.h> 18 19 20void xnn_f32_vsigmoid_ukernel__avx512f_rr1_p5_scalef_${DIV_ALGO}_x${BATCH_TILE}( 21 size_t n, 22 const float* x, 23 float* y, 24 const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)]) 25{ 26 assert(n % sizeof(float) == 0); 27 28 const __m512i vsign_mask = _mm512_set1_epi32((int) params->avx512_rr1_p5.sign_mask); 29 const __m512 vlog2e = _mm512_set1_ps(params->avx512_rr1_p5.log2e); 30 const __m512 vminus_ln2 = _mm512_set1_ps(params->avx512_rr1_p5.minus_ln2); 31 const __m512 vc5 = _mm512_set1_ps(params->avx512_rr1_p5.c5); 32 const __m512 vc4 = _mm512_set1_ps(params->avx512_rr1_p5.c4); 33 const __m512 vc3 = _mm512_set1_ps(params->avx512_rr1_p5.c3); 34 const __m512 vc2 = _mm512_set1_ps(params->avx512_rr1_p5.c2); 35 const __m512 vc1 = _mm512_set1_ps(params->avx512_rr1_p5.c1); 36 const __m512 vone = _mm512_set1_ps(params->avx512_rr1_p5.one); 37 38 $if BATCH_TILE > 16: 39 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { 40 const __m512 vx${ABC[0]} = _mm512_loadu_ps(x); 41 $for N in range(1, SIMD_TILE): 42 const __m512 vx${ABC[N]} = _mm512_loadu_ps(x + ${N * 16}); 43 x += ${BATCH_TILE}; 44 45 $for N in range(SIMD_TILE): 46 const __m512 vz${ABC[N]} = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx${ABC[N]}), vsign_mask)); 47 48 $for N in range(SIMD_TILE): 49 __m512 vn${ABC[N]} = _mm512_mul_ps(vz${ABC[N]}, vlog2e); 50 51 $for N in range(SIMD_TILE): 52 vn${ABC[N]} = _mm512_roundscale_ps(vn${ABC[N]}, 0); 53 54 $for N in range(SIMD_TILE): 55 __m512 vt${ABC[N]} = _mm512_fmadd_ps(vn${ABC[N]}, vminus_ln2, vz${ABC[N]}); 56 57 $for N in range(SIMD_TILE): 58 __m512 vp${ABC[N]} = _mm512_fmadd_ps(vc5, vt${ABC[N]}, vc4); 59 60 $for N in range(SIMD_TILE): 61 vp${ABC[N]} = _mm512_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vc3); 62 63 $for N in range(SIMD_TILE): 64 vp${ABC[N]} = _mm512_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vc2); 65 66 $for N in range(SIMD_TILE): 67 vp${ABC[N]} = _mm512_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vc1); 68 69 $for N in range(SIMD_TILE): 70 vp${ABC[N]} = _mm512_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vone); 71 72 $for N in range(SIMD_TILE): 73 const __m512 ve${ABC[N]} = _mm512_scalef_ps(vp${ABC[N]}, vn${ABC[N]}); 74 75 $for N in range(SIMD_TILE): 76 const __m512 vd${ABC[N]} = _mm512_add_ps(ve${ABC[N]}, vone); 77 78 $if DIV_ALGO == "div": 79 $for N in range(SIMD_TILE): 80 __m512 vf${ABC[N]} = _mm512_div_ps(ve${ABC[N]}, vd${ABC[N]}); 81 $else: 82 $for N in range(SIMD_TILE): 83 __m512 vr${ABC[N]} = _mm512_rcp14_ps(vd${ABC[N]}); 84 85 $for N in range(SIMD_TILE): 86 vr${ABC[N]} = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr${ABC[N]}, vd${ABC[N]}, vone), vr${ABC[N]}, vr${ABC[N]}); 87 88 $for N in range(SIMD_TILE): 89 __m512 vf${ABC[N]} = _mm512_mul_ps(ve${ABC[N]}, vr${ABC[N]}); 90 91 $if DIV_ALGO == "nr1fma1adj": 92 $for N in range(SIMD_TILE): 93 vf${ABC[N]} = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf${ABC[N]}, vd${ABC[N]}, ve${ABC[N]}), vr${ABC[N]}, vf${ABC[N]}); 94 95 $for N in range(SIMD_TILE): 96 vf${ABC[N]} = _mm512_mask_sub_ps(vf${ABC[N]}, _mm512_testn_epi32_mask(_mm512_castps_si512(vx${ABC[N]}), vsign_mask), vone, vf${ABC[N]}); 97 98 _mm512_storeu_ps(y, vf${ABC[0]}); 99 $for N in range(1, SIMD_TILE): 100 _mm512_storeu_ps(y + ${N * 16}, vf${ABC[N]}); 101 y += ${BATCH_TILE}; 102 } 103 for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) { 104 const __m512 vx = _mm512_loadu_ps(x); 105 x += 16; 106 107 const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); 108 109 const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0); 110 111 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); 112 113 __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); 114 vp = _mm512_fmadd_ps(vp, vt, vc3); 115 vp = _mm512_fmadd_ps(vp, vt, vc2); 116 vp = _mm512_fmadd_ps(vp, vt, vc1); 117 vp = _mm512_fmadd_ps(vp, vt, vone); 118 119 const __m512 ve = _mm512_scalef_ps(vp, vn); 120 const __m512 vd = _mm512_add_ps(ve, vone); 121 122 $if DIV_ALGO == "div": 123 __m512 vf = _mm512_div_ps(ve, vd); 124 $else: 125 __m512 vr = _mm512_rcp14_ps(vd); 126 vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); 127 128 __m512 vf = _mm512_mul_ps(ve, vr); 129 $if DIV_ALGO == "nr1fma1adj": 130 vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf); 131 132 vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); 133 134 _mm512_storeu_ps(y, vf); 135 y += 16; 136 } 137 if XNN_UNLIKELY(n != 0) { 138 assert(n >= 1 * sizeof(float)); 139 assert(n <= 15 * sizeof(float)); 140 141 // Prepare mask for valid 32-bit elements (depends on n). 142 n >>= 2 /* log2(sizeof(float)) */; 143 const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1))); 144 145 const __m512 vx = _mm512_maskz_loadu_ps(vmask, x); 146 const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); 147 148 const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0); 149 150 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); 151 152 __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); 153 vp = _mm512_fmadd_ps(vp, vt, vc3); 154 vp = _mm512_fmadd_ps(vp, vt, vc2); 155 vp = _mm512_fmadd_ps(vp, vt, vc1); 156 vp = _mm512_fmadd_ps(vp, vt, vone); 157 158 const __m512 ve = _mm512_scalef_ps(vp, vn); 159 const __m512 vd = _mm512_add_ps(ve, vone); 160 161 $if DIV_ALGO == "div": 162 __m512 vf = _mm512_div_ps(ve, vd); 163 $else: 164 __m512 vr = _mm512_rcp14_ps(vd); 165 vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); 166 167 __m512 vf = _mm512_mul_ps(ve, vr); 168 $if DIV_ALGO == "nr1fma1adj": 169 vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf); 170 171 vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); 172 173 _mm512_mask_storeu_ps(y, vmask, vf); 174 } 175} 176