| 1 |
// Auto-generated file. Do not edit! |
| 2 |
// Template: src/f32-vscaleexpminusmax/avx2-p5.c.in |
| 3 |
// Generator: tools/xngen |
| 4 |
// |
| 5 |
// Copyright 2019 Google LLC |
| 6 |
// |
| 7 |
// This source code is licensed under the BSD-style license found in the |
| 8 |
// LICENSE file in the root directory of this source tree. |
| 9 |
|
| 10 |
#include <assert.h> |
| 11 |
|
| 12 |
#include <immintrin.h> |
| 13 |
|
| 14 |
#include "xnnpack/common.h" |
| 15 |
#include "xnnpack/vscaleexpminusmax.h" |
| 16 |
|
| 17 |
|
| 18 |
void xnn_f32_vscaleexpminusmax_ukernel__avx2_p5_u8( |
| 19 |
size_t batch, |
| 20 |
const float* input, |
| 21 |
float* output, |
| 22 |
float scale, |
| 23 |
float max) |
| 24 |
{ |
| 25 |
assert(batch != 0); |
| 26 |
assert(batch % sizeof(float) == 0); |
| 27 |
assert(input != NULL); |
| 28 |
assert(output != NULL); |
| 29 |
|
| 30 |
static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0}; |
| 31 |
|
| 32 |
const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); |
| 33 |
// The smallest x for which expf(x) is normalized. |
| 34 |
const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep6f); |
| 35 |
const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f); |
| 36 |
const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f); |
| 37 |
const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f); |
| 38 |
|
| 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 vscale = _mm256_set1_ps(scale); |
| 46 |
const __m256 vi_max = _mm256_set1_ps(max); |
| 47 |
|
| 48 |
for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) { |
| 49 |
// Load 8 (1x8) inputs at a time. |
| 50 |
const __m256 vi0 = _mm256_loadu_ps(input); |
| 51 |
input += 8; |
| 52 |
|
| 53 |
// Subtract maximum input x := i - i_max. This implies x <= 0. |
| 54 |
const __m256 vx0 = _mm256_sub_ps(vi0, vi_max); |
| 55 |
|
| 56 |
// Compute reduced argument batch := round(x / log(2)). |
| 57 |
__m256 vn0 = _mm256_fmadd_ps(vx0, vlog2e, vmagic_bias); |
| 58 |
|
| 59 |
// Create a floating-point number s (scale) such that s == 2**batch for inputs which don't cause underflow, i.e. |
| 60 |
// -87.33642 <= x <= 0.0, and -126 <= batch <= 0 accordingly. |
| 61 |
const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23)); |
| 62 |
|
| 63 |
// Subtract the large number back to get final batch := round(x / log(2)). |
| 64 |
vn0 = _mm256_sub_ps(vn0, vmagic_bias); |
| 65 |
|
| 66 |
// Compute reduced argument t := x - batch * log(2). |
| 67 |
// Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. |
| 68 |
__m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_hi, vx0); |
| 69 |
|
| 70 |
vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_lo, vt0); |
| 71 |
|
| 72 |
// Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. |
| 73 |
__m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4); |
| 74 |
|
| 75 |
vp0 = _mm256_fmadd_ps(vp0, vt0, vc3); |
| 76 |
|
| 77 |
vp0 = _mm256_fmadd_ps(vp0, vt0, vc2); |
| 78 |
|
| 79 |
vp0 = _mm256_fmadd_ps(vp0, vt0, vc1); |
| 80 |
|
| 81 |
// Reconstruct the final f value: |
| 82 |
// f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) |
| 83 |
// = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) |
| 84 |
// = s + (t * s) * p |
| 85 |
vt0 = _mm256_mul_ps(vt0, vs0); |
| 86 |
|
| 87 |
__m256 vf0 = _mm256_fmadd_ps(vt0, vp0, vs0); |
| 88 |
|
| 89 |
// For inputs below zero cutoff, replace output with +0.0f. |
| 90 |
// Note that for NaN inputs, comparison result is false, and outputs are left unchanged. |
| 91 |
vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vx0, vdenorm_cutoff, _CMP_LT_OS), vf0); |
| 92 |
|
| 93 |
// Multiply by scale. |
| 94 |
vf0 = _mm256_mul_ps(vf0, vscale); |
| 95 |
|
| 96 |
// Store 8 (1x8) outputs at a time. |
| 97 |
_mm256_storeu_ps(output, vf0); |
| 98 |
output += 8; |
| 99 |
} |
| 100 |
for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) { |
| 101 |
// Load 8 inputs at a time. |
| 102 |
const __m256 vi = _mm256_loadu_ps(input); |
| 103 |
input += 8; |
| 104 |
|
| 105 |
// Subtract maximum input x := i - i_max. This implies x <= 0. |
| 106 |
const __m256 vx = _mm256_sub_ps(vi, vi_max); |
| 107 |
|
| 108 |
// Compute reduced argument batch := round(x / log(2)). |
| 109 |
__m256 vn = _mm256_fmadd_ps(vx, vlog2e, vmagic_bias); |
| 110 |
|
| 111 |
// Create a floating-point number s (scale) such that s == 2**batch for inputs which don't cause underflow, i.e. |
| 112 |
// -87.33642 <= x <= 0.0, and -126 <= batch <= 0 accordingly. |
| 113 |
const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); |
| 114 |
|
| 115 |
// Subtract the large number back to get final batch := round(x / log(2)). |
| 116 |
vn = _mm256_sub_ps(vn, vmagic_bias); |
| 117 |
|
| 118 |
// Compute reduced argument t := x - batch * log(2). |
| 119 |
// Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. |
| 120 |
__m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx); |
| 121 |
vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); |
| 122 |
|
| 123 |
// Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. |
| 124 |
__m256 vp = _mm256_fmadd_ps(vc5, vt, vc4); |
| 125 |
vp = _mm256_fmadd_ps(vp, vt, vc3); |
| 126 |
vp = _mm256_fmadd_ps(vp, vt, vc2); |
| 127 |
vp = _mm256_fmadd_ps(vp, vt, vc1); |
| 128 |
|
| 129 |
// Reconstruct the final f value: |
| 130 |
// f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) |
| 131 |
// = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) |
| 132 |
// = s + (t * s) * p |
| 133 |
vt = _mm256_mul_ps(vt, vs); |
| 134 |
__m256 vf = _mm256_fmadd_ps(vt, vp, vs); |
| 135 |
|
| 136 |
// For inputs below zero cutoff, replace output with +0.0f. |
| 137 |
// Note that for NaN inputs, comparison result is false, and outputs are left unchanged. |
| 138 |
vf = _mm256_andnot_ps(_mm256_cmp_ps(vx, vdenorm_cutoff, _CMP_LT_OS), vf); |
| 139 |
|
| 140 |
// Multiply by scale. |
| 141 |
vf = _mm256_mul_ps(vf, vscale); |
| 142 |
|
| 143 |
// Store 64 (8x8) outputs at a time. |
| 144 |
_mm256_storeu_ps(output, vf); |
| 145 |
output += 8; |
| 146 |
} |
| 147 |
if (batch != 0) { |
| 148 |
assert(batch >= 1 * sizeof(float)); |
| 149 |
assert(batch <= 7 * sizeof(float)); |
| 150 |
const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - batch)); |
| 151 |
|
| 152 |
// Load up to 7 inputs at a time. |
| 153 |
const __m256 vi = _mm256_maskload_ps(input, vmask); |
| 154 |
|
| 155 |
// Subtract maximum input x := i - i_max. This implies x <= 0. |
| 156 |
const __m256 vx = _mm256_sub_ps(vi, vi_max); |
| 157 |
|
| 158 |
// Compute reduced argument batch := round(x / log(2)). |
| 159 |
__m256 vn = _mm256_fmadd_ps(vx, vlog2e, vmagic_bias); |
| 160 |
|
| 161 |
// Create a floating-point number s (scale) such that s == 2**batch for inputs which don't cause underflow, i.e. |
| 162 |
// -87.33642 <= x <= 0.0, and -126 <= batch <= 0 accordingly. |
| 163 |
const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); |
| 164 |
|
| 165 |
// Subtract the large number back to get final batch := round(x / log(2)). |
| 166 |
vn = _mm256_sub_ps(vn, vmagic_bias); |
| 167 |
|
| 168 |
// Compute reduced argument t := x - batch * log(2). |
| 169 |
// Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. |
| 170 |
__m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx); |
| 171 |
vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); |
| 172 |
|
| 173 |
// Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. |
| 174 |
__m256 vp = _mm256_fmadd_ps(vc5, vt, vc4); |
| 175 |
vp = _mm256_fmadd_ps(vp, vt, vc3); |
| 176 |
vp = _mm256_fmadd_ps(vp, vt, vc2); |
| 177 |
vp = _mm256_fmadd_ps(vp, vt, vc1); |
| 178 |
|
| 179 |
// Reconstruct the final f value: |
| 180 |
// f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) |
| 181 |
// = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) |
| 182 |
// = s + (t * s) * p |
| 183 |
vt = _mm256_mul_ps(vt, vs); |
| 184 |
__m256 vf = _mm256_fmadd_ps(vt, vp, vs); |
| 185 |
|
| 186 |
// For inputs below zero cutoff, replace output with +0.0f. |
| 187 |
// Note that for NaN inputs, comparison result is false, and outputs are left unchanged. |
| 188 |
vf = _mm256_andnot_ps(_mm256_cmp_ps(vx, vdenorm_cutoff, _CMP_LT_OS), vf); |
| 189 |
|
| 190 |
// Multiply by scale. |
| 191 |
vf = _mm256_mul_ps(vf, vscale); |
| 192 |
|
| 193 |
// Store up to 7 outputs at a time. |
| 194 |
_mm256_maskstore_ps(output, vmask, vf); |
| 195 |
} |
| 196 |
} |