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 % 8 == 0 7$assert ELEMENTS_TILE >= 8 8$SIMD_TILE = ELEMENTS_TILE // 8 9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 10#include <assert.h> 11 12#include <immintrin.h> 13 14#include <xnnpack/common.h> 15#include <xnnpack/vscaleextexp.h> 16 17 18static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0}; 19 20void xnn_f32_vscaleextexp_ukernel__avx2_p5_x${ELEMENTS_TILE}( 21 size_t elements, 22 const float* x, 23 float* y, 24 float scale_value, 25 float scale_exp) 26{ 27 assert(elements % sizeof(float) == 0); 28 29 const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f); 30 const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f); 31 const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f); 32 33 // The smallest elements such that 2**elements is considered non-negligible. 34 // For smaller elements, 2**elements is replaced with zero. 35 const __m256 vmin_exponent = _mm256_set1_ps(-127.0f); 36 const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); 37 38 const __m256 vc0 = _mm256_set1_ps(1.0f); 39 const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f); 40 const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f); 41 const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f); 42 const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f); 43 const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f); 44 45 const __m256 vscalev = _mm256_set1_ps(scale_value); 46 const __m256 vscalee = _mm256_set1_ps(scale_exp); 47 48 for (; elements >= ${ELEMENTS_TILE} * sizeof(float); elements -= ${ELEMENTS_TILE} * sizeof(float)) { 49 // Load ${ELEMENTS_TILE} (${SIMD_TILE}x8) inputs at a time. 50 const __m256 vx0 = _mm256_loadu_ps(x); 51 $for N in range(1, SIMD_TILE): 52 const __m256 vx${N} = _mm256_loadu_ps(x + ${N * 8}); 53 x += ${ELEMENTS_TILE}; 54 55 // Compute reduced argument elements := round(x / log(2)). 56 $for N in range(SIMD_TILE): 57 const __m256 vn${N} = _mm256_round_ps(_mm256_mul_ps(vx${N}, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); 58 59 // Compute reduced argument t := x - elements * log(2). 60 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. 61 $for N in range(SIMD_TILE): 62 __m256 vt${N} = _mm256_fmadd_ps(vn${N}, vminus_ln2_hi, vx${N}); 63 64 $for N in range(SIMD_TILE): 65 vt${N} = _mm256_fmadd_ps(vn${N}, vminus_ln2_lo, vt${N}); 66 67 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2]. 68 $for N in range(SIMD_TILE): 69 __m256 vp${N} = _mm256_fmadd_ps(vc5, vt${N}, vc4); 70 71 $for N in range(SIMD_TILE): 72 vp${N} = _mm256_fmadd_ps(vp${N}, vt${N}, vc3); 73 74 $for N in range(SIMD_TILE): 75 vp${N} = _mm256_fmadd_ps(vp${N}, vt${N}, vc2); 76 77 $for N in range(SIMD_TILE): 78 vp${N} = _mm256_fmadd_ps(vp${N}, vt${N}, vc1); 79 80 $for N in range(SIMD_TILE): 81 vp${N} = _mm256_fmadd_ps(vp${N}, vt${N}, vc0); 82 83 // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation where 84 // - vnX is "exponent" 85 // - vpX is "mantissa" 86 // 87 // exp2(ae) * av * exp2(be) * bv = 88 // = exp2(ae + be) * (av * bv) 89 $for N in range(SIMD_TILE): 90 __m256 vf${N} = _mm256_mul_ps(vp${N}, vscalev); 91 92 $for N in range(SIMD_TILE): 93 __m256 ve${N} = _mm256_add_ps(vn${N}, vscalee); 94 95 // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0. 96 // This replacement is done in two steps: 97 // 1. Clamp minimum e at -127.0. 98 // 2. Map e to scale factor 0.0 when e == -127.0 99 $for N in range(SIMD_TILE): 100 ve${N} = _mm256_max_ps(ve${N}, vmin_exponent); 101 102 // Convert exponents into scale factors: 103 // - s = exp2(e) when e > -127.0 104 // - s = 0.0 when e <= -127.0 105 $for N in range(SIMD_TILE): 106 const __m256 vs${N} = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve${N}, vmagic_bias)), 23)); 107 108 // Multiply "mantissa" by the scale factor. 109 $for N in range(SIMD_TILE): 110 vf${N} = _mm256_mul_ps(vf${N}, vs${N}); 111 112 // Store ${ELEMENTS_TILE} (${SIMD_TILE}x8) outputs at a time. 113 _mm256_storeu_ps(y, vf0); 114 $for N in range(1, SIMD_TILE): 115 _mm256_storeu_ps(y + ${N * 8}, vf${N}); 116 y += ${ELEMENTS_TILE}; 117 } 118 119 for (; elements >= 8 * sizeof(float); elements -= 8 * sizeof(float)) { 120 // Load 8 inputs at a time. 121 const __m256 vx = _mm256_loadu_ps(x); 122 x += 8; 123 124 // Compute reduced argument elements := round(x / log(2)). 125 const __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); 126 127 // Compute reduced argument t := x - elements * log(2). 128 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. 129 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx); 130 vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); 131 132 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2]. 133 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4); 134 vp = _mm256_fmadd_ps(vp, vt, vc3); 135 vp = _mm256_fmadd_ps(vp, vt, vc2); 136 vp = _mm256_fmadd_ps(vp, vt, vc1); 137 vp = _mm256_fmadd_ps(vp, vt, vc0); 138 139 // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation. 140 __m256 vf = _mm256_mul_ps(vp, vscalev); 141 __m256 ve = _mm256_add_ps(vn, vscalee); 142 143 // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0. 144 ve = _mm256_max_ps(ve, vmin_exponent); 145 146 // Convert exponents into scale factors. 147 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve, vmagic_bias)), 23)); 148 149 // Multiply "mantissa" by the scale factor. 150 vf = _mm256_mul_ps(vf, vs); 151 152 // Store 8 results at a time. 153 _mm256_storeu_ps(y, vf); 154 y += 8; 155 } 156 if XNN_UNLIKELY(elements != 0) { 157 assert(elements >= 1 * sizeof(float)); 158 assert(elements <= 7 * sizeof(float)); 159 const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - elements)); 160 161 // Load up to 7 inputs at a time. 162 const __m256 vx = _mm256_maskload_ps(x, vmask); 163 164 // Compute reduced argument elements := round(x / log(2)). 165 const __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); 166 167 // Compute reduced argument t := x - elements * log(2). 168 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. 169 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx); 170 vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); 171 172 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2]. 173 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4); 174 vp = _mm256_fmadd_ps(vp, vt, vc3); 175 vp = _mm256_fmadd_ps(vp, vt, vc2); 176 vp = _mm256_fmadd_ps(vp, vt, vc1); 177 vp = _mm256_fmadd_ps(vp, vt, vc0); 178 179 // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation. 180 __m256 vf = _mm256_mul_ps(vp, vscalev); 181 __m256 ve = _mm256_add_ps(vn, vscalee); 182 183 // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0. 184 ve = _mm256_max_ps(ve, vmin_exponent); 185 186 // Convert exponents into scale factors. 187 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve, vmagic_bias)), 23)); 188 189 // Multiply "mantissa" by the scale factor. 190 vf = _mm256_mul_ps(vf, vs); 191 192 // Store up to 7 inputs at a time. 193 _mm256_maskstore_ps(y, vmask, vf); 194 } 195 _mm256_zeroupper(); 196} 197