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 ELEMENTS_TILE % 16 == 0 7$assert ELEMENTS_TILE >= 16 8$SIMD_TILE = ELEMENTS_TILE // 16 9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 10#include <assert.h> 11 12#include <immintrin.h> 13 14#include <xnnpack/intrinsics-polyfill.h> 15#include <xnnpack/raddstoreexpminusmax.h> 16 17 18void xnn_f32_raddstoreexpminusmax_ukernel__avx512f_p5_scalef_x${ELEMENTS_TILE}${"" if ACCUMULATORS == 1 else "_acc%d" % ACCUMULATORS}( 19 size_t elements, 20 const float* input, 21 float* output, 22 float* sum, 23 float max) 24{ 25 assert(elements % sizeof(float) == 0); 26 27 const __m512 vlog2e = _mm512_set1_ps(0x1.715476p+0f); 28 const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f); 29 const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f); 30 31 const __m512 vc0 = _mm512_set1_ps(1.0f); 32 const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f); 33 const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f); 34 const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f); 35 const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f); 36 const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f); 37 38 const __m512 vi_max = _mm512_set1_ps(max); 39 40 $for K in range(ACCUMULATORS): 41 __m512 vacc${K} = _mm512_setzero_ps(); 42 for (; elements >= ${ELEMENTS_TILE} * sizeof(float); elements -= ${ELEMENTS_TILE} * sizeof(float)) { 43 // Load ${ELEMENTS_TILE} (${SIMD_TILE}x16) inputs at a time. 44 const __m512 vi0 = _mm512_loadu_ps(input); 45 $for N in range(1, SIMD_TILE): 46 const __m512 vi${N} = _mm512_loadu_ps(input + ${N * 16}); 47 input += ${ELEMENTS_TILE}; 48 49 // Subtract maximum input x := i - i_max. 50 $for N in range(SIMD_TILE): 51 const __m512 vx${N} = _mm512_sub_ps(vi${N}, vi_max); 52 53 // Compute reduced argument elements := round(x / log(2)). 54 $for N in range(SIMD_TILE): 55 const __m512 vn${N} = _mm512_roundscale_ps(_mm512_mul_ps(vx${N}, vlog2e), 0); 56 57 // Compute reduced argument t := x - elements * log(2). 58 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. 59 $for N in range(SIMD_TILE): 60 __m512 vt${N} = _mm512_fmadd_ps(vn${N}, vminus_ln2_hi, vx${N}); 61 62 $for N in range(SIMD_TILE): 63 vt${N} = _mm512_fmadd_ps(vn${N}, vminus_ln2_lo, vt${N}); 64 65 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2]. 66 $for N in range(SIMD_TILE): 67 __m512 vp${N} = _mm512_fmadd_ps(vc5, vt${N}, vc4); 68 69 $for N in range(SIMD_TILE): 70 vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc3); 71 72 $for N in range(SIMD_TILE): 73 vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc2); 74 75 $for N in range(SIMD_TILE): 76 vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc1); 77 78 $for N in range(SIMD_TILE): 79 vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc0); 80 81 // Reconstruct the final f value: 82 // f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) 83 // = 2**elements * p 84 $for N in range(SIMD_TILE): 85 const __m512 vf${N} = _mm512_scalef_ps(vp${N}, vn${N}); 86 87 // Store ${ELEMENTS_TILE} (${SIMD_TILE}x16) outputs at a time. 88 _mm512_storeu_ps(output, vf0); 89 $for N in range(1, SIMD_TILE): 90 _mm512_storeu_ps(output + ${N * 16}, vf${N}); 91 output += ${ELEMENTS_TILE}; 92 93 // Accumulate computed exponents. 94 $for N in range(SIMD_TILE): 95 vacc${N % ACCUMULATORS} = _mm512_add_ps(vacc${N % ACCUMULATORS}, vf${N}); 96 } 97 $if ACCUMULATORS > 1: 98 // Add up all accumulators to vacc0 99 $ACC_SLICE = 1 100 $while ACC_SLICE < ACCUMULATORS: 101 $for A in range(0, ACCUMULATORS, ACC_SLICE * 2): 102 $if A + ACC_SLICE < ACCUMULATORS: 103 vacc${A} = _mm512_add_ps(vacc${A}, vacc${A + ACC_SLICE}); 104 $ACC_SLICE *= 2 105 106 __m512 vacc = vacc0; 107 for (; elements >= 16 * sizeof(float); elements -= 16 * sizeof(float)) { 108 // Load 16 inputs at a time. 109 const __m512 vi = _mm512_loadu_ps(input); 110 input += 16; 111 112 // Subtract maximum input x := i - i_max. 113 const __m512 vx = _mm512_sub_ps(vi, vi_max); 114 115 // Compute reduced argument elements := round(x / log(2)). 116 const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0); 117 118 // Compute reduced argument t := x - elements * log(2). 119 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. 120 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx); 121 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); 122 123 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2]. 124 __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); 125 vp = _mm512_fmadd_ps(vp, vt, vc3); 126 vp = _mm512_fmadd_ps(vp, vt, vc2); 127 vp = _mm512_fmadd_ps(vp, vt, vc1); 128 vp = _mm512_fmadd_ps(vp, vt, vc0); 129 130 // Reconstruct the final f value: 131 // f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) 132 // = 2**elements * p 133 const __m512 vf = _mm512_scalef_ps(vp, vn); 134 135 // Store 16 outputs at a time. 136 _mm512_storeu_ps(output, vf); 137 output += 16; 138 139 // Accumulate computed exponents. 140 vacc = _mm512_add_ps(vacc, vf); 141 } 142 if (elements != 0) { 143 // Prepare mask for valid 32-bit elements (depends on elements). 144 elements >>= 2 /* log2(sizeof(float)) */; 145 const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << elements) - UINT32_C(1))); 146 147 // Load up to 15 inputs at a time. 148 const __m512 vi = _mm512_maskz_loadu_ps(vmask, input); 149 150 // Subtract maximum input x := i - i_max. 151 const __m512 vx = _mm512_sub_ps(vi, vi_max); 152 153 // Compute reduced argument elements := round(x / log(2)). 154 const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0); 155 156 // Compute reduced argument t := x - elements * log(2). 157 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. 158 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx); 159 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); 160 161 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2]. 162 __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); 163 vp = _mm512_fmadd_ps(vp, vt, vc3); 164 vp = _mm512_fmadd_ps(vp, vt, vc2); 165 vp = _mm512_fmadd_ps(vp, vt, vc1); 166 vp = _mm512_fmadd_ps(vp, vt, vc0); 167 168 // Reconstruct the final f value: 169 // f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) 170 // = 2**elements * p 171 const __m512 vf = _mm512_scalef_ps(vp, vn); 172 173 // Store up to 15 outputs at a time. 174 _mm512_mask_storeu_ps(output, vmask, vf); 175 176 // Accumulate computed exponents. 177 vacc = _mm512_mask_add_ps(vacc, vmask, vacc, vf); 178 } 179 *sum = _mm512_reduce_add_ps(vacc); 180} 181