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/vscaleexpminusmax.h> 16 17 18void xnn_f32_vscaleexpminusmax_ukernel__avx512f_p5_scalef_x${ELEMENTS_TILE}( 19 size_t elements, 20 const float* input, 21 float* output, 22 float scale, 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 vscale = _mm512_set1_ps(scale); 39 const __m512 vi_max = _mm512_set1_ps(max); 40 41 for (; elements >= ${ELEMENTS_TILE} * sizeof(float); elements -= ${ELEMENTS_TILE} * sizeof(float)) { 42 // Load ${ELEMENTS_TILE} (${SIMD_TILE}x16) inputs at a time. 43 const __m512 vi0 = _mm512_loadu_ps(input); 44 $for N in range(1, SIMD_TILE): 45 const __m512 vi${N} = _mm512_loadu_ps(input + ${N * 16}); 46 input += ${ELEMENTS_TILE}; 47 48 // Subtract maximum input x := i - i_max. 49 $for N in range(SIMD_TILE): 50 const __m512 vx${N} = _mm512_sub_ps(vi${N}, vi_max); 51 52 // Compute reduced argument elements := round(x / log(2)). 53 $for N in range(SIMD_TILE): 54 __m512 vn${N} = _mm512_roundscale_ps(_mm512_mul_ps(vx${N}, vlog2e), 0); 55 56 // Compute reduced argument t := x - elements * log(2). 57 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. 58 $for N in range(SIMD_TILE): 59 __m512 vt${N} = _mm512_fmadd_ps(vn${N}, vminus_ln2_hi, vx${N}); 60 61 $for N in range(SIMD_TILE): 62 vt${N} = _mm512_fmadd_ps(vn${N}, vminus_ln2_lo, vt${N}); 63 64 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. 65 $for N in range(SIMD_TILE): 66 __m512 vp${N} = _mm512_fmadd_ps(vc5, vt${N}, vc4); 67 68 $for N in range(SIMD_TILE): 69 vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc3); 70 71 $for N in range(SIMD_TILE): 72 vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc2); 73 74 $for N in range(SIMD_TILE): 75 vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc1); 76 77 $for N in range(SIMD_TILE): 78 vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc0); 79 80 // Reconstruct the final f value: 81 // f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) 82 // = 2**elements * p 83 $for N in range(SIMD_TILE): 84 __m512 vf${N} = _mm512_scalef_ps(vp${N}, vn${N}); 85 86 // Multiply by scale. 87 $for N in range(SIMD_TILE): 88 vf${N} = _mm512_mul_ps(vf${N}, vscale); 89 90 // Store ${ELEMENTS_TILE} (${SIMD_TILE}x16) outputs at a time. 91 _mm512_storeu_ps(output, vf0); 92 $for N in range(SIMD_TILE): 93 _mm512_storeu_ps(output + ${N * 16}, vf${N}); 94 output += ${ELEMENTS_TILE}; 95 } 96 for (; elements >= 16 * sizeof(float); elements -= 16 * sizeof(float)) { 97 // Load 16 inputs at a time. 98 const __m512 vi = _mm512_loadu_ps(input); 99 input += 16; 100 101 // Subtract maximum input x := i - i_max. 102 const __m512 vx = _mm512_sub_ps(vi, vi_max); 103 104 // Compute reduced argument elements := round(x / log(2)). 105 __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0); 106 107 // Compute reduced argument t := x - elements * log(2). 108 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. 109 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx); 110 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); 111 112 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. 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, vc0); 118 119 // Reconstruct the final f value: 120 // f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) 121 // = 2**elements * p 122 __m512 vf = _mm512_scalef_ps(vp, vn); 123 124 // Multiply by scale. 125 vf = _mm512_mul_ps(vf, vscale); 126 127 // Store 16 outputs at a time. 128 _mm512_storeu_ps(output, vf); 129 output += 16; 130 } 131 if (elements != 0) { 132 // Prepare mask for valid 32-bit elements (depends on elements). 133 elements >>= 2 /* log2(sizeof(float)) */; 134 const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << elements) - UINT32_C(1))); 135 136 // Load up to 15 inputs at a time. 137 const __m512 vi = _mm512_mask_loadu_ps(_mm512_undefined_ps(), vmask, input); 138 139 // Subtract maximum input x := i - i_max. 140 const __m512 vx = _mm512_sub_ps(vi, vi_max); 141 142 // Compute reduced argument elements := round(x / log(2)). 143 __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vx, vlog2e), 0); 144 145 // Compute reduced argument t := x - elements * log(2). 146 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. 147 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vx); 148 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); 149 150 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. 151 __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); 152 vp = _mm512_fmadd_ps(vp, vt, vc3); 153 vp = _mm512_fmadd_ps(vp, vt, vc2); 154 vp = _mm512_fmadd_ps(vp, vt, vc1); 155 vp = _mm512_fmadd_ps(vp, vt, vc0); 156 157 // Reconstruct the final f value: 158 // f = 2**elements * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) 159 // = 2**elements * p 160 __m512 vf = _mm512_scalef_ps(vp, vn); 161 162 // Multiply by scale. 163 vf = _mm512_mul_ps(vf, vscale); 164 165 // Store up to 15 outputs at a time. 166 _mm512_mask_storeu_ps(output, vmask, vf); 167 } 168} 169