repo
stringlengths
1
152
file
stringlengths
14
221
code
stringlengths
501
25k
file_length
int64
501
25k
avg_line_length
float64
20
99.5
max_line_length
int64
21
134
extension_type
stringclasses
2 values
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-lut8-p4h3ps-perm-nr1.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx512skx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__avx512skx_expm1minus_rr1_lut8_p4h3ps_perm_nr1( size_t n, const float* input, float* output) { assert(n % sizeof(__m512) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const __m512 vsat_cutoff = _mm512_set1_ps(0x1.205968p+3f); const __m512 vminus_log2e = _mm512_set1_ps(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p+19f); // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 const __m512i vtable = _mm512_set_epi32( 0x3F7AC0C7, 0x3F7744FD, 0x3F75672A, 0x3F7504F3, 0x3F75FED7, 0x3F7837F0, 0x3F7B95C2, 0x3F800000, 0x3F7AC0C7, 0x3F7744FD, 0x3F75672A, 0x3F7504F3, 0x3F75FED7, 0x3F7837F0, 0x3F7B95C2, 0x3F800000); const __m512 vln2 = _mm512_set1_ps(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const __m512 vc4 = _mm512_set1_ps(0x1.5558ECp-1f); const __m512 vc3 = _mm512_set1_ps(-0x1.555C20p+0f); const __m512 vc2 = _mm512_set1_ps(0x1.000000p+1f); const __m512 vminus_two = _mm512_set1_ps(-2.0f); const __m512 vone = _mm512_set1_ps(1.0f); // Mask for the sign bit. const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); for (; n != 0; n -= sizeof(__m512)) { const __m512 vx = _mm512_load_ps(input); input += 16; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). // // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. const __m512 vz = _mm512_range_ps(vsat_cutoff, vx, 0xA); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m512 vn = _mm512_fmadd_ps(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const __m512i ve = _mm512_slli_epi32(_mm512_castps_si512(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m512i vl = _mm512_permutexvar_epi32(_mm512_castps_si512(vn), vtable); // Adjust exponent of the value l fetched from the table to get the final s value. const __m512 vs = _mm512_castsi512_ps(_mm512_add_epi32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const __m512 vt = _mm512_fmadd_ps(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p __m512 vp = vc4; vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const __m512 vps = _mm512_mul_ps(vp, vs); const __m512 vsmo = _mm512_sub_ps(vs, vone); const __m512 vemo = _mm512_fmadd_ps(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const __m512 vepo = _mm512_sub_ps(vemo, vminus_two); // Use Newton-Raphson method (1 iteration) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(2z) <= 1. // Thus the reciprocal of the denominator never overflows. __m512 vrepo = _mm512_rcp14_ps(vepo); const __m512 verepo = _mm512_fnmadd_ps(vrepo, vepo, vone); vrepo = _mm512_fmadd_ps(verepo, vrepo, vrepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) __m512 vy = _mm512_mul_ps(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = _mm512_castsi512_ps(_mm512_ternarylogic_epi32(_mm512_castps_si512(vy), _mm512_castps_si512(vx), vsign_mask, 0xD8)); _mm512_store_ps(output, vy); output += 16; } }
6,255
45
124
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-lut8-p4h3ps-perm-nr1adj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx512skx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__avx512skx_expm1minus_rr1_lut8_p4h3ps_perm_nr1adj( size_t n, const float* input, float* output) { assert(n % sizeof(__m512) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const __m512 vsat_cutoff = _mm512_set1_ps(0x1.205968p+3f); const __m512 vminus_log2e = _mm512_set1_ps(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p+19f); // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 const __m512i vtable = _mm512_set_epi32( 0x3F7AC0C7, 0x3F7744FD, 0x3F75672A, 0x3F7504F3, 0x3F75FED7, 0x3F7837F0, 0x3F7B95C2, 0x3F800000, 0x3F7AC0C7, 0x3F7744FD, 0x3F75672A, 0x3F7504F3, 0x3F75FED7, 0x3F7837F0, 0x3F7B95C2, 0x3F800000); const __m512 vln2 = _mm512_set1_ps(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const __m512 vc4 = _mm512_set1_ps(0x1.5558ECp-1f); const __m512 vc3 = _mm512_set1_ps(-0x1.555C20p+0f); const __m512 vc2 = _mm512_set1_ps(0x1.000000p+1f); const __m512 vminus_two = _mm512_set1_ps(-2.0f); const __m512 vone = _mm512_set1_ps(1.0f); // Mask for the sign bit. const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); for (; n != 0; n -= sizeof(__m512)) { const __m512 vx = _mm512_load_ps(input); input += 16; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). // // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. const __m512 vz = _mm512_range_ps(vsat_cutoff, vx, 0xA); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m512 vn = _mm512_fmadd_ps(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const __m512i ve = _mm512_slli_epi32(_mm512_castps_si512(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m512i vl = _mm512_permutexvar_epi32(_mm512_castps_si512(vn), vtable); // Adjust exponent of the value l fetched from the table to get the final s value. const __m512 vs = _mm512_castsi512_ps(_mm512_add_epi32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const __m512 vt = _mm512_fmadd_ps(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p __m512 vp = vc4; vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const __m512 vps = _mm512_mul_ps(vp, vs); const __m512 vsmo = _mm512_sub_ps(vs, vone); const __m512 vemo = _mm512_fmadd_ps(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const __m512 vepo = _mm512_sub_ps(vemo, vminus_two); // Use Newton-Raphson method (1 iteration) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(2z) <= 1. // Thus the reciprocal of the denominator never overflows. __m512 vrepo = _mm512_rcp14_ps(vepo); const __m512 verepo = _mm512_fnmadd_ps(vrepo, vepo, vone); vrepo = _mm512_fmadd_ps(verepo, vrepo, vrepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) __m512 vy = _mm512_mul_ps(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const __m512 vey = _mm512_fnmadd_ps(vy, vepo, vemo); vy = _mm512_fmadd_ps(vey, vrepo, vy); // Reconstruct tanh(x) = copysign(y, x) vy = _mm512_castsi512_ps(_mm512_ternarylogic_epi32(_mm512_castps_si512(vy), _mm512_castps_si512(vx), vsign_mask, 0xD8)); _mm512_store_ps(output, vy); output += 16; } }
6,463
45.503597
124
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-p6h5ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx512skx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__avx512skx_expm1minus_rr1_p6h5ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(__m512) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const __m512 vsat_cutoff = _mm512_set1_ps(0x1.205968p+3f); const __m512 vminus_log2e = _mm512_set1_ps(-0x1.715476p+0f); // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const __m512 vmagic_bias = _mm512_set1_ps(0x1.8000FEp+22f); const __m512 vln2 = _mm512_set1_ps(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const __m512 vc6 = _mm512_set1_ps(0x1.6B7338p-4f); const __m512 vc5 = _mm512_set1_ps(-0x1.12278Ep-2f); const __m512 vc4 = _mm512_set1_ps(0x1.555716p-1f); const __m512 vc3 = _mm512_set1_ps(-0x1.5554B0p+0f); const __m512 vc2 = _mm512_set1_ps(0x1.FFFFFEp+0f); const __m512 vminus_two = _mm512_set1_ps(-2.0f); const __m512 vone = _mm512_set1_ps(1.0f); // Mask for the sign bit. const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); for (; n != 0; n -= sizeof(__m512)) { const __m512 vx = _mm512_load_ps(input); input += 16; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). // // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. const __m512 vz = _mm512_range_ps(vsat_cutoff, vx, 0xA); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m512 vn = _mm512_fmadd_ps(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const __m512 vs = _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_castps_si512(vn), 23)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const __m512 vt = _mm512_fmadd_ps(vn, vln2, vz); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p __m512 vp = vc6; vp = _mm512_fmadd_ps(vp, vt, vc5); vp = _mm512_fmadd_ps(vp, vt, vc4); vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const __m512 vts = _mm512_mul_ps(vt, vs); const __m512 vsmo = _mm512_sub_ps(vs, vone); const __m512 vemo = _mm512_fmadd_ps(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const __m512 vepo = _mm512_sub_ps(vemo, vminus_two); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) __m512 vy = _mm512_div_ps(vemo, vepo); // Reconstruct tanh(x) = copysign(y, x) vy = _mm512_castsi512_ps(_mm512_ternarylogic_epi32(_mm512_castps_si512(vy), _mm512_castps_si512(vx), vsign_mask, 0xD8)); _mm512_store_ps(output, vy); output += 16; } }
5,007
42.172414
124
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-p6h5ts-nr1.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx512skx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__avx512skx_expm1minus_rr1_p6h5ts_nr1( size_t n, const float* input, float* output) { assert(n % sizeof(__m512) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const __m512 vsat_cutoff = _mm512_set1_ps(0x1.205968p+3f); const __m512 vminus_log2e = _mm512_set1_ps(-0x1.715476p+0f); // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const __m512 vmagic_bias = _mm512_set1_ps(0x1.8000FEp+22f); const __m512 vln2 = _mm512_set1_ps(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const __m512 vc6 = _mm512_set1_ps(0x1.6B7338p-4f); const __m512 vc5 = _mm512_set1_ps(-0x1.12278Ep-2f); const __m512 vc4 = _mm512_set1_ps(0x1.555716p-1f); const __m512 vc3 = _mm512_set1_ps(-0x1.5554B0p+0f); const __m512 vc2 = _mm512_set1_ps(0x1.FFFFFEp+0f); const __m512 vminus_two = _mm512_set1_ps(-2.0f); const __m512 vone = _mm512_set1_ps(1.0f); // Mask for the sign bit. const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); for (; n != 0; n -= sizeof(__m512)) { const __m512 vx = _mm512_load_ps(input); input += 16; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). // // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. const __m512 vz = _mm512_range_ps(vsat_cutoff, vx, 0xA); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m512 vn = _mm512_fmadd_ps(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const __m512 vs = _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_castps_si512(vn), 23)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const __m512 vt = _mm512_fmadd_ps(vn, vln2, vz); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p __m512 vp = vc6; vp = _mm512_fmadd_ps(vp, vt, vc5); vp = _mm512_fmadd_ps(vp, vt, vc4); vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const __m512 vts = _mm512_mul_ps(vt, vs); const __m512 vsmo = _mm512_sub_ps(vs, vone); const __m512 vemo = _mm512_fmadd_ps(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const __m512 vepo = _mm512_sub_ps(vemo, vminus_two); // Use Newton-Raphson method (1 iteration) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(2z) <= 1. // Thus the reciprocal of the denominator never overflows. __m512 vrepo = _mm512_rcp14_ps(vepo); const __m512 verepo = _mm512_fnmadd_ps(vrepo, vepo, vone); vrepo = _mm512_fmadd_ps(verepo, vrepo, vrepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) __m512 vy = _mm512_mul_ps(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = _mm512_castsi512_ps(_mm512_ternarylogic_epi32(_mm512_castps_si512(vy), _mm512_castps_si512(vx), vsign_mask, 0xD8)); _mm512_store_ps(output, vy); output += 16; } }
5,391
42.483871
124
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-p6h5ts-nr1adj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx512skx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__avx512skx_expm1minus_rr1_p6h5ts_nr1adj( size_t n, const float* input, float* output) { assert(n % sizeof(__m512) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const __m512 vsat_cutoff = _mm512_set1_ps(0x1.205968p+3f); const __m512 vminus_log2e = _mm512_set1_ps(-0x1.715476p+0f); // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const __m512 vmagic_bias = _mm512_set1_ps(0x1.8000FEp+22f); const __m512 vln2 = _mm512_set1_ps(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const __m512 vc6 = _mm512_set1_ps(0x1.6B7338p-4f); const __m512 vc5 = _mm512_set1_ps(-0x1.12278Ep-2f); const __m512 vc4 = _mm512_set1_ps(0x1.555716p-1f); const __m512 vc3 = _mm512_set1_ps(-0x1.5554B0p+0f); const __m512 vc2 = _mm512_set1_ps(0x1.FFFFFEp+0f); const __m512 vminus_two = _mm512_set1_ps(-2.0f); const __m512 vone = _mm512_set1_ps(1.0f); // Mask for the sign bit. const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); for (; n != 0; n -= sizeof(__m512)) { const __m512 vx = _mm512_load_ps(input); input += 16; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). // // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. const __m512 vz = _mm512_range_ps(vsat_cutoff, vx, 0xA); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m512 vn = _mm512_fmadd_ps(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const __m512 vs = _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_castps_si512(vn), 23)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const __m512 vt = _mm512_fmadd_ps(vn, vln2, vz); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p __m512 vp = vc6; vp = _mm512_fmadd_ps(vp, vt, vc5); vp = _mm512_fmadd_ps(vp, vt, vc4); vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const __m512 vts = _mm512_mul_ps(vt, vs); const __m512 vsmo = _mm512_sub_ps(vs, vone); const __m512 vemo = _mm512_fmadd_ps(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const __m512 vepo = _mm512_sub_ps(vemo, vminus_two); // Use Newton-Raphson method (1 iteration) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(2z) <= 1. // Thus the reciprocal of the denominator never overflows. __m512 vrepo = _mm512_rcp14_ps(vepo); const __m512 verepo = _mm512_fnmadd_ps(vrepo, vepo, vone); vrepo = _mm512_fmadd_ps(verepo, vrepo, vrepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) __m512 vy = _mm512_mul_ps(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const __m512 vey = _mm512_fnmadd_ps(vy, vepo, vemo); vy = _mm512_fmadd_ps(vey, vrepo, vy); // Reconstruct tanh(x) = copysign(y, x) vy = _mm512_castsi512_ps(_mm512_ternarylogic_epi32(_mm512_castps_si512(vy), _mm512_castps_si512(vx), vsign_mask, 0xD8)); _mm512_store_ps(output, vy); output += 16; } }
5,599
43.094488
124
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut16-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut16_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/64, log(2)/64] const float vc3 = 0x1.55561Cp-1f; const float vc2 = -0x1.0001ECp+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,159
40.612903
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut16-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut16_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/64, log(2)/64] const float vc4 = -0x1.55563Ap-2f; const float vc3 = 0x1.555708p-1f; const float vc2 = -0x1.000000p+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,257
40.730159
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut16-p4h2ts-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut16_p4h2ts_rcp( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/64, log(2)/64] const float vc4 = -0x1.55563Ap-2f; const float vc3 = 0x1.555708p-1f; const float vc2 = -0x1.000000p+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Compute reciprocal of denominator. const float vrepo = vone / vepo; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo * vrepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,338
40.387597
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut16-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut16_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/64, log(2)/64] const float vc4 = 0x1.55563Ap-1f; const float vc3 = -0x1.555708p+0f; const float vc2 = 0x1.000000p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,185
40.488
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut16-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut16_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/64, log(2)/64] const float vc4 = 0x1.55563Ap-1f; const float vc3 = -0x1.555708p+0f; const float vc2 = 0x1.000000p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,185
40.488
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut32-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 32) values decremented (as integer) by (k << 18), k = 0..31 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_32[32]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut32_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-6) const float vmagic_bias = 0x1.800000p+17f; // Mask for the lowest 5 bits const uint32_t vindex_mask = UINT32_C(0x1F); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/128, log(2)/128] const float vc3 = 0x1.555582p-1f; const float vc2 = -0x1.00007Ap+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**16, i.e. |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 6 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 5 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 5:13 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 18; // Use bits 0:5 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_32[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 6) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/128, log(2)/128]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,165
40.66129
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut4-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut4_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/16, log(2)/16] const float vc4 = -0x1.554F9Ap-2f; const float vc3 = 0x1.557082p-1f; const float vc2 = -0x1.000002p+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,250
40.674603
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut4-p4h2ts-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut4_p4h2ts_rcp( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/16, log(2)/16] const float vc4 = -0x1.554F9Ap-2f; const float vc3 = 0x1.557082p-1f; const float vc2 = -0x1.000002p+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Compute reciprocal of denominator. const float vrepo = vone / vepo; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo * vrepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,331
40.333333
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut4-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut4_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-1f; const float vc3 = -0x1.557082p+0f; const float vc2 = 0x1.000002p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,178
40.432
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut4-p4h3ps-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut4_p4h3ps_rcp( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-1f; const float vc3 = -0x1.557082p+0f; const float vc2 = 0x1.000002p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Compute reciprocal of denominator. const float vrepo = vone / vepo; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo * vrepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,259
40.09375
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut4-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut4_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-1f; const float vc3 = -0x1.557082p+0f; const float vc2 = 0x1.000002p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,178
40.432
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut4-p4h3ts-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut4_p4h3ts_rcp( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-1f; const float vc3 = -0x1.557082p+0f; const float vc2 = 0x1.000002p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Compute reciprocal of denominator. const float vrepo = vone / vepo; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo * vrepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,259
40.09375
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut64-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut64_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-7) const float vmagic_bias = 0x1.800000p+16f; // Mask for the lowest 6 bits const uint32_t vindex_mask = UINT32_C(0x3F); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/256, log(2)/256] const float vc3 = 0x1.55555Ep-1f; const float vc2 = -0x1.00001Ep+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 7). // We do it by adding a large number (magic bias), which cause rounding of the result to 7 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**15, i.e. |z| <= 0x1.62E43p+14 = 22713.046875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 7 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 17; // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_64[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 7) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/256, log(2)/256]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,166
40.669355
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut8-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut8_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/32, log(2)/32] const float vc3 = 0x1.555862p-1f; const float vc2 = -0x1.0007ACp+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,153
40.564516
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut8-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut8_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/32, log(2)/32] const float vc4 = -0x1.5558ECp-2f; const float vc3 = 0x1.555C20p-1f; const float vc2 = -0x1.000000p+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,251
40.68254
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut8-p4h2ts-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut8_p4h2ts_rcp( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/32, log(2)/32] const float vc4 = -0x1.5558ECp-2f; const float vc3 = 0x1.555C20p-1f; const float vc2 = -0x1.000000p+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Compute reciprocal of denominator. const float vrepo = vone / vepo; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo * vrepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,332
40.341085
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut8-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut8_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-1f; const float vc3 = -0x1.555C20p+0f; const float vc2 = 0x1.000000p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,179
40.44
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut8-p4h3ps-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut8_p4h3ps_rcp( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-1f; const float vc3 = -0x1.555C20p+0f; const float vc2 = 0x1.000000p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Compute reciprocal of denominator. const float vrepo = vone / vepo; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo * vrepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,260
40.101563
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut8-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut8_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-1f; const float vc3 = -0x1.555C20p+0f; const float vc2 = 0x1.000000p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,179
40.44
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-lut8-p4h3ts-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr1_lut8_p4h3ts_rcp( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-1f; const float vc3 = -0x1.555C20p+0f; const float vc2 = 0x1.000000p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Compute reciprocal of denominator. const float vrepo = vone / vepo; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo * vrepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,260
40.101563
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-p6h4ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1minus_rr1_p6h4ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) // on [-log(2)/4, log(2)/4] const float vc6 = -0x1.6B7338p-5f; const float vc5 = 0x1.12278Ep-3f; const float vc4 = -0x1.555716p-2f; const float vc3 = 0x1.5554B0p-1f; const float vc2 = -0x1.FFFFFEp-1f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) // = -2 * (t + t * p) float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,402
38.666667
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-p6h5ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1minus_rr1_p6h5ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-4f; const float vc5 = -0x1.12278Ep-2f; const float vc4 = 0x1.555716p-1f; const float vc3 = -0x1.5554B0p+0f; const float vc2 = 0x1.FFFFFEp+0f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,330
38.372727
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-p6h5ps-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1minus_rr1_p6h5ps_rcp( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-4f; const float vc5 = -0x1.12278Ep-2f; const float vc4 = 0x1.555716p-1f; const float vc3 = -0x1.5554B0p+0f; const float vc2 = 0x1.FFFFFEp+0f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Compute reciprocal of denominator. const float vrepo = vone / vepo; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo * vrepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,411
38.044248
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-p6h5ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1minus_rr1_p6h5ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-4f; const float vc5 = -0x1.12278Ep-2f; const float vc4 = 0x1.555716p-1f; const float vc3 = -0x1.5554B0p+0f; const float vc2 = 0x1.FFFFFEp+0f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,330
38.372727
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr1-p6h5ts-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1minus_rr1_p6h5ts_rcp( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vln2 = 0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-4f; const float vc5 = -0x1.12278Ep-2f; const float vc4 = 0x1.555716p-1f; const float vc3 = -0x1.5554B0p+0f; const float vc2 = 0x1.FFFFFEp+0f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float vt = fmaf(vn, vln2, vz); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Compute reciprocal of denominator. const float vrepo = vone / vepo; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo * vrepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,411
38.044248
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut16-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut16_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/64, log(2)/64] const float vc3 = 0x1.55561Cp-1f; const float vc2 = -0x1.0001ECp+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,340
41.055118
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut16-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut16_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/64, log(2)/64] const float vc4 = -0x1.55563Ap-2f; const float vc3 = 0x1.555708p-1f; const float vc2 = -0x1.000000p+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,438
41.162791
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut16-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut16_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/64, log(2)/64] const float vc4 = 0x1.55563Ap-1f; const float vc3 = -0x1.555708p+0f; const float vc2 = 0x1.000000p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,366
40.929688
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut16-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut16_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/64, log(2)/64] const float vc4 = 0x1.55563Ap-1f; const float vc3 = -0x1.555708p+0f; const float vc2 = 0x1.000000p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,366
40.929688
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut32-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 32) values decremented (as integer) by (k << 18), k = 0..31 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_32[32]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut32_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-6) const float vmagic_bias = 0x1.800000p+17f; // Mask for the lowest 5 bits const uint32_t vindex_mask = UINT32_C(0x1F); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/128, log(2)/128] const float vc3 = 0x1.555582p-1f; const float vc2 = -0x1.00007Ap+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**16, i.e. |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 6 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 5 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 5:13 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 18; // Use bits 0:5 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_32[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 6) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/128, log(2)/128]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,346
41.102362
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut4-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut4_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/16, log(2)/16] const float vc4 = -0x1.554F9Ap-2f; const float vc3 = 0x1.557082p-1f; const float vc2 = -0x1.000002p+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,431
41.108527
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut4-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut4_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-1f; const float vc3 = -0x1.557082p+0f; const float vc2 = 0x1.000002p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,359
40.875
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut4-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut4_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-1f; const float vc3 = -0x1.557082p+0f; const float vc2 = 0x1.000002p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,359
40.875
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut64-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut64_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-7) const float vmagic_bias = 0x1.800000p+16f; // Mask for the lowest 6 bits const uint32_t vindex_mask = UINT32_C(0x3F); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/256, log(2)/256] const float vc3 = 0x1.55555Ep-1f; const float vc2 = -0x1.00001Ep+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 7). // We do it by adding a large number (magic bias), which cause rounding of the result to 7 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**15, i.e. |z| <= 0x1.62E43p+14 = 22713.046875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 7 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 17; // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_64[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 7) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/256, log(2)/256]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,347
41.110236
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut8-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut8_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/32, log(2)/32] const float vc3 = 0x1.555862p-1f; const float vc2 = -0x1.0007ACp+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,334
41.007874
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut8-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut8_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/32, log(2)/32] const float vc4 = -0x1.5558ECp-2f; const float vc3 = 0x1.555C20p-1f; const float vc2 = -0x1.000000p+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,432
41.116279
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut8-p4h2ts-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut8_p4h2ts_rcp( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/32, log(2)/32] const float vc4 = -0x1.5558ECp-2f; const float vc3 = 0x1.555C20p-1f; const float vc2 = -0x1.000000p+0f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Compute reciprocal of denominator. const float vrepo = vone / vepo; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo * vrepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,513
40.772727
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut8-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut8_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-1f; const float vc3 = -0x1.555C20p+0f; const float vc2 = 0x1.000000p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,360
40.882813
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-lut8-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1minus_rr2_lut8_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-1f; const float vc3 = -0x1.555C20p+0f; const float vc2 = 0x1.000000p+1f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,360
40.882813
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-p6h4ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1minus_rr2_p6h4ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) // on [-log(2)/4, log(2)/4] const float vc6 = -0x1.6B7338p-5f; const float vc5 = 0x1.12278Ep-3f; const float vc4 = -0x1.555716p-2f; const float vc3 = 0x1.5554B0p-1f; const float vc2 = -0x1.FFFFFEp-1f; const float vone = 1.0f; const float vminus_two = -2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) // = -2 * (t + t * p) float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vminus_two, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,583
39.210526
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-p6h5ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1minus_rr2_p6h5ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-4f; const float vc5 = -0x1.12278Ep-2f; const float vc4 = 0x1.555716p-1f; const float vc3 = -0x1.5554B0p+0f; const float vc2 = 0x1.FFFFFEp+0f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,511
38.929204
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1minus-rr2-p6h5ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1minus_rr2_p6h5ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vminus_log2e = -0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vln2_hi = 0x1.62E430p-1f; const float vln2_lo = -0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-4f; const float vc5 = -0x1.12278Ep-2f; const float vc4 = 0x1.555716p-1f; const float vc3 = -0x1.5554B0p+0f; const float vc2 = 0x1.FFFFFEp+0f; const float vminus_two = -2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vminus_log2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vln2_hi, vz); vt = fmaf(vn, vln2_lo, vt); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vminus_two); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float vepo = vemo - vminus_two; // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,511
38.929204
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut16-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut16_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/64, log(2)/64] const float vc3 = 0x1.55561Cp-1f; const float vc2 = 0x1.0001ECp+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-3 polynomial approximation for exp(2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = 2 * (t + t * (t * (c2 + t * c3))) // = 2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,080
39.975806
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut16-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut16_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/64, log(2)/64] const float vc4 = 0x1.55563Ap-2f; const float vc3 = 0x1.555708p-1f; const float vc2 = 0x1.000000p+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = 2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,177
40.095238
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut16-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut16_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/64, log(2)/64] const float vc4 = 0x1.55563Ap-1f; const float vc3 = 0x1.555708p+0f; const float vc2 = 0x1.000000p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,109
39.88
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut16-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut16_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/64, log(2)/64] const float vc4 = 0x1.55563Ap-1f; const float vc3 = 0x1.555708p+0f; const float vc2 = 0x1.000000p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,109
39.88
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut32-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 32) values decremented (as integer) by (k << 18), k = 0..31 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_32[32]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut32_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-6) const float vmagic_bias = 0x1.800000p+17f; // Mask for the lowest 5 bits const uint32_t vindex_mask = UINT32_C(0x1F); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/128, log(2)/128] const float vc3 = 0x1.555582p-1f; const float vc2 = 0x1.00007Ap+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**16, i.e. |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 6 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 5 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 5:13 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 18; // Use bits 0:5 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_32[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 6) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-3 polynomial approximation for exp(2t) - 1 on [-log(2)/128, log(2)/128]. // P(t) = 2 * (t + t * (t * (c2 + t * c3))) // = 2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,086
40.024194
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut4-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut4_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-2f; const float vc3 = 0x1.557082p-1f; const float vc2 = 0x1.000002p+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = 2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,170
40.039683
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut4-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut4_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-1f; const float vc3 = 0x1.557082p+0f; const float vc2 = 0x1.000002p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,102
39.824
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut4-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut4_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-1f; const float vc3 = 0x1.557082p+0f; const float vc2 = 0x1.000002p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,102
39.824
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut64-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut64_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-7) const float vmagic_bias = 0x1.800000p+16f; // Mask for the lowest 6 bits const uint32_t vindex_mask = UINT32_C(0x3F); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/256, log(2)/256] const float vc3 = 0x1.55555Ep-1f; const float vc2 = 0x1.00001Ep+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 7). // We do it by adding a large number (magic bias), which cause rounding of the result to 7 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**15, i.e. |z| <= 0x1.62E43p+14 = 22713.046875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 7 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 17; // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_64[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 7) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-3 polynomial approximation for exp(2t) - 1 on [-log(2)/256, log(2)/256]. // P(t) = 2 * (t + t * (t * (c2 + t * c3))) // = 2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,087
40.032258
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut8-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut8_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/32, log(2)/32] const float vc3 = 0x1.555862p-1f; const float vc2 = 0x1.0007ACp+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-3 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = 2 * (t + t * (t * (c2 + t * c3))) // = 2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,074
39.927419
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut8-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut8_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-2f; const float vc3 = 0x1.555C20p-1f; const float vc2 = 0x1.000000p+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = 2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,171
40.047619
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut8-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut8_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-1f; const float vc3 = 0x1.555C20p+0f; const float vc2 = 0x1.000000p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,103
39.832
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-lut8-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1plus_rr1_lut8_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-1f; const float vc3 = 0x1.555C20p+0f; const float vc2 = 0x1.000000p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,103
39.832
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-p6h4ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1plus_rr1_p6h4ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-5f; const float vc5 = 0x1.12278Ep-3f; const float vc4 = 0x1.555716p-2f; const float vc3 = 0x1.5554B0p-1f; const float vc2 = 0x1.FFFFFEp-1f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-6 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = 2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) // = 2 * (t + t * p) float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,321
37.936937
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-p6h5ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1plus_rr1_p6h5ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-4f; const float vc5 = 0x1.12278Ep-2f; const float vc4 = 0x1.555716p-1f; const float vc3 = 0x1.5554B0p+0f; const float vc2 = 0x1.FFFFFEp+0f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-6 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,253
37.672727
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr1-p6h5ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1plus_rr1_p6h5ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vminus_ln2 = -0x1.62E430p-1f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-4f; const float vc5 = 0x1.12278Ep-2f; const float vc4 = 0x1.555716p-1f; const float vc3 = 0x1.5554B0p+0f; const float vc2 = 0x1.FFFFFEp+0f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). const float vt = fmaf(vn, vminus_ln2, vz); // Compute degree-6 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,253
37.672727
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut16-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut16_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/64, log(2)/64] const float vc3 = 0x1.55561Cp-1f; const float vc2 = 0x1.0001ECp+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-3 polynomial approximation for exp(2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = 2 * (t + t * (t * (c2 + t * c3))) // = 2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,272
40.519685
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut16-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut16_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/64, log(2)/64] const float vc4 = 0x1.55563Ap-2f; const float vc3 = 0x1.555708p-1f; const float vc2 = 0x1.000000p+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = 2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,369
40.627907
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut16-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut16_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/64, log(2)/64] const float vc4 = 0x1.55563Ap-1f; const float vc3 = 0x1.555708p+0f; const float vc2 = 0x1.000000p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,301
40.421875
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut16-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 16) values decremented (as integer) by (k << 19), k = 0..15 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut16_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-5) const float vmagic_bias = 0x1.800000p+18f; // Mask for the lowest 4 bits const uint32_t vindex_mask = UINT32_C(0xF); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/64, log(2)/64] const float vc4 = 0x1.55563Ap-1f; const float vc3 = 0x1.555708p+0f; const float vc2 = 0x1.000000p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**17, i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 5 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 4 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 4:12 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 19; // Use bits 0:4 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_16[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 5) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/64, log(2)/64]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,301
40.421875
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut32-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 32) values decremented (as integer) by (k << 18), k = 0..31 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_32[32]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut32_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-6) const float vmagic_bias = 0x1.800000p+17f; // Mask for the lowest 5 bits const uint32_t vindex_mask = UINT32_C(0x1F); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/128, log(2)/128] const float vc3 = 0x1.555582p-1f; const float vc2 = 0x1.00007Ap+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**16, i.e. |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 6 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 5 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 5:13 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 18; // Use bits 0:5 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_32[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 6) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-3 polynomial approximation for exp(2t) - 1 on [-log(2)/128, log(2)/128]. // P(t) = 2 * (t + t * (t * (c2 + t * c3))) // = 2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,278
40.566929
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut4-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut4_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-2f; const float vc3 = 0x1.557082p-1f; const float vc2 = 0x1.000002p+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = 2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,362
40.573643
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut4-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut4_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-1f; const float vc3 = 0x1.557082p+0f; const float vc2 = 0x1.000002p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,294
40.367188
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut4-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_4[4]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut4_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-3) const float vmagic_bias = 0x1.800000p+20f; // Mask for the lowest 2 bits const uint32_t vindex_mask = UINT32_C(0x3); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const float vc4 = 0x1.554F9Ap-1f; const float vc3 = 0x1.557082p+0f; const float vc2 = 0x1.000002p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 21; // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_4[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 3) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,294
40.367188
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut64-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut64_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-7) const float vmagic_bias = 0x1.800000p+16f; // Mask for the lowest 6 bits const uint32_t vindex_mask = UINT32_C(0x3F); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/256, log(2)/256] const float vc3 = 0x1.55555Ep-1f; const float vc2 = 0x1.00001Ep+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 7). // We do it by adding a large number (magic bias), which cause rounding of the result to 7 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**15, i.e. |z| <= 0x1.62E43p+14 = 22713.046875), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 7 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 17; // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_64[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 7) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-3 polynomial approximation for exp(2t) - 1 on [-log(2)/256, log(2)/256]. // P(t) = 2 * (t + t * (t * (c2 + t * c3))) // = 2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,279
40.574803
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut8-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut8_p3h1ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/32, log(2)/32] const float vc3 = 0x1.555862p-1f; const float vc2 = 0x1.0007ACp+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-3 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = 2 * (t + t * (t * (c2 + t * c3))) // = 2 * (t + t * p) float vp = fmaf(vc3, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * c3))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,266
40.472441
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut8-p4h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut8_p4h2ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-2f; const float vc3 = 0x1.555C20p-1f; const float vc2 = 0x1.000000p+0f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = 2 * (t + t * p) float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,363
40.581395
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut8-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut8_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-1f; const float vc3 = 0x1.555C20p+0f; const float vc2 = 0x1.000000p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,295
40.375
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-lut8-p4h3ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma_expm1plus_rr2_lut8_p4h3ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == exp2(-4) const float vmagic_bias = 0x1.800000p+19f; // Mask for the lowest 3 bits const uint32_t vindex_mask = UINT32_C(0x7); const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float vc4 = 0x1.5558ECp-1f; const float vc3 = 0x1.555C20p+0f; const float vc2 = 0x1.000000p+1f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have 0 <= int(n) <= 13, and thus the adjusted exponent is not // greater than 13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32_t vb = float_as_uint32(vn); const uint32_t ve = vb << 20; // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = vb & vindex_mask; const uint32_t vl = xnn_table_exp2minus_k_over_8[vidx]; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(vl + ve); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p float vp = fmaf(vc4, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
5,295
40.375
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-p6h4ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1plus_rr2_p6h4ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ 2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-5f; const float vc5 = 0x1.12278Ep-3f; const float vc4 = 0x1.555716p-2f; const float vc3 = 0x1.5554B0p-1f; const float vc2 = 0x1.FFFFFEp-1f; const float vone = 1.0f; const float vtwo = 2.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-6 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = 2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) // = 2 * (t + t * p) float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp *= vt; // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (2 * (t + t * (t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6)))))) + 1) - 1 // = s * (2 * (t + t * p) + 1) - 1 // = (s - 1) + 2 * ((t * s) + (t * s) * p) const float vts = vt * vs; const float vsmo = vs - vone; vp = fmaf(vp, vts, vts); const float vemo = fmaf(vp, vtwo, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,513
38.596491
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-p6h5ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1plus_rr2_p6h5ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-4f; const float vc5 = 0x1.12278Ep-2f; const float vc4 = 0x1.555716p-1f; const float vc3 = 0x1.5554B0p+0f; const float vc2 = 0x1.FFFFFEp+0f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-6 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const float vps = vp * vs; const float vsmo = vs - vone; const float vemo = fmaf(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,445
38.345133
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma-expm1plus-rr2-p6h5ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-scalar-expm1plus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma_expm1plus_rr2_p6h5ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // The smallest z for which tanhf(z) is saturated at 1.0f. const float vsat_cutoff = 0x1.205968p+3f; const float vlog2e = 0x1.715476p+0f; // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float vmagic_bias = 0x1.8000FEp+22f; const float vminus_ln2_hi = -0x1.62E430p-1f; const float vminus_ln2_lo = 0x1.05C610p-29f; // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float vc6 = 0x1.6B7338p-4f; const float vc5 = 0x1.12278Ep-2f; const float vc4 = 0x1.555716p-1f; const float vc3 = 0x1.5554B0p+0f; const float vc2 = 0x1.FFFFFEp+0f; const float vtwo = 2.0f; const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(2z) / (2 + expm1(2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float vz = fabsf(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = math_pmin_f32(vz, vsat_cutoff); // Compute reduced argument n := round(z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float vn = fmaf(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get final n := round(z / log(2), 1) as a floating-point number. vn -= vmagic_bias; // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float vt = fmaf(vn, vminus_ln2_hi, vz); vt = fmaf(vn, vminus_ln2_lo, vt); // Compute degree-6 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p float vp = fmaf(vc6, vt, vc5); vp = fmaf(vp, vt, vc4); vp = fmaf(vp, vt, vc3); vp = fmaf(vp, vt, vc2); vp = fmaf(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const float vts = vt * vs; const float vsmo = vs - vone; const float vemo = fmaf(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const float vepo = vemo + vtwo; // Reconstruct y = expm1(2z) / (expm1(2z) + 2) float vy = vemo / vepo; // Reconstruct tanh(x) = copysign(y, x) vy = copysignf(vy, vx); *output++ = vy; } }
4,445
38.345133
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma3-expm1minus-rr1-lut4-p4h3ts-perm-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma3_expm1minus_rr1_lut4_p4h3ts_perm_div( size_t n, const float* input, float* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // The largest z for which tanhf(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.205968p+3f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-3) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p+20f); // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 const __m128 vtable = _mm_set_ps( 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const __m256 vc4 = _mm256_set1_ps(0x1.554F9Ap-1f); const __m256 vc3 = _mm256_set1_ps(0x1.557082p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.000002p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); for (; n != 0; n -= sizeof(__m256)) { const __m256 vx = _mm256_load_ps(input); input += 8; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x <= 0 // f(x) := // \ -f(-x) if x >= 0 // // First we compute f(z) := expm1(2z) / (2 + expm1(2z)) where z = -abs(x), then negate the result if x >= 0. __m256 vz = _mm256_or_ps(vx, vsign_mask); // Inverted mask for the sign of input: 0x00000000 for negative x, 0x80000000 for positive x. const __m256 vinvsignx = _mm256_xor_ps(vx, vz); // The function saturates at -1 for large negative inputs: tanhf(z) == -1.0f for z <= sat_cutoff ~= -9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = _mm256_max_ps(vsat_cutoff, vz); // Compute reduced argument n := round(z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [-9.010913, 0]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. -9.010913 <= z <= 0. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for -9.010913 <= z <= 0 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const __m128 vn_hi = _mm256_extractf128_ps(vn, 1); __m128i ve_lo = _mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 21); __m128i ve_hi = _mm_slli_epi32(_mm_castps_si128(vn_hi), 21); // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m128i vl_lo = _mm_castps_si128(_mm_permutevar_ps(vtable, _mm_castps_si128(_mm256_castps256_ps128(vn)))); const __m128i vl_hi = _mm_castps_si128(_mm_permutevar_ps(vtable, _mm_castps_si128(vn_hi))); // Adjust exponent of the value l fetched from the table to get the final s value. const __m128 vs_lo = _mm_castsi128_ps(_mm_add_epi32(ve_lo, vl_lo)); const __m128 vs_hi = _mm_castsi128_ps(_mm_add_epi32(ve_hi, vl_hi)); const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1); // Subtract the large number back to get final n := round(z / log(2), 3) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p __m256 vp = vc4; vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const __m256 vts = _mm256_mul_ps(vt, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); const __m256 vemo = _mm256_fmadd_ps(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const __m256 vepo = _mm256_add_ps(vemo, vtwo); // Reconstruct tanh(z) = expm1(2z) / (expm1(2z) + 2) __m256 vy = _mm256_div_ps(vemo, vepo); // Reconstruct tanh(x): // // / tanh(z) if x <= 0 // tanh(x) = // \ -tanh(z) if x >= 0 vy = _mm256_xor_ps(vy, vinvsignx); _mm256_store_ps(output, vy); output += 8; } }
6,218
43.421429
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma3-expm1minus-rr1-lut4-p4h3ts-perm-nr1adj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma3_expm1minus_rr1_lut4_p4h3ts_perm_nr1adj( size_t n, const float* input, float* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // The largest z for which tanhf(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.205968p+3f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-3) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p+20f); // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 const __m128 vtable = _mm_set_ps( 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/16, log(2)/16] const __m256 vc4 = _mm256_set1_ps(0x1.554F9Ap-1f); const __m256 vc3 = _mm256_set1_ps(0x1.557082p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.000002p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); for (; n != 0; n -= sizeof(__m256)) { const __m256 vx = _mm256_load_ps(input); input += 8; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x <= 0 // f(x) := // \ -f(-x) if x >= 0 // // First we compute f(z) := expm1(2z) / (2 + expm1(2z)) where z = -abs(x), then negate the result if x >= 0. __m256 vz = _mm256_or_ps(vx, vsign_mask); // Inverted mask for the sign of input: 0x00000000 for negative x, 0x80000000 for positive x. const __m256 vinvsignx = _mm256_xor_ps(vx, vz); // The function saturates at -1 for large negative inputs: tanhf(z) == -1.0f for z <= sat_cutoff ~= -9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = _mm256_max_ps(vsat_cutoff, vz); // Compute reduced argument n := round(z / log(2), 3). // We do it by adding a large number (magic bias), which cause rounding of the result to 3 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**19, i.e. |z| <= 0x1.62E43p+18 = 363408.75), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [-9.010913, 0]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. -9.010913 <= z <= 0. As // n has 3 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 2 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for -9.010913 <= z <= 0 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 2:10 into 23:31 (position of floating-point exponent). const __m128 vn_hi = _mm256_extractf128_ps(vn, 1); __m128i ve_lo = _mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 21); __m128i ve_hi = _mm_slli_epi32(_mm_castps_si128(vn_hi), 21); // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m128i vl_lo = _mm_castps_si128(_mm_permutevar_ps(vtable, _mm_castps_si128(_mm256_castps256_ps128(vn)))); const __m128i vl_hi = _mm_castps_si128(_mm_permutevar_ps(vtable, _mm_castps_si128(vn_hi))); // Adjust exponent of the value l fetched from the table to get the final s value. const __m128 vs_lo = _mm_castsi128_ps(_mm_add_epi32(ve_lo, vl_lo)); const __m128 vs_hi = _mm_castsi128_ps(_mm_add_epi32(ve_hi, vl_hi)); const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1); // Subtract the large number back to get final n := round(z / log(2), 3) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/16, log(2)/16]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p __m256 vp = vc4; vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const __m256 vts = _mm256_mul_ps(vt, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); const __m256 vemo = _mm256_fmadd_ps(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const __m256 vepo = _mm256_add_ps(vemo, vtwo); // Use Newton-Raphson method (1 iteration) to compute reciprocal of the denominator. // Note: 2 < exp(2z) + 1 <= 3, because z <= 0 and 0 < exp(2z) <= 1. // Thus the reciprocal of the denominator never overflows. __m256 vrepo = _mm256_rcp_ps(vepo); const __m256 verepo = _mm256_fnmsub_ps(vrepo, vepo, vminus_one); vrepo = _mm256_fmadd_ps(verepo, vrepo, vrepo); // Reconstruct tanh(z) := expm1(2z) / (2 + expm1(2z)) __m256 vy = _mm256_mul_ps(vemo, vrepo); // Adjust reconstructred expm1(2z) / (2 + expm1(2z)) to match the correctly rounded division result const __m256 vey = _mm256_fnmadd_ps(vy, vepo, vemo); vy = _mm256_fmadd_ps(vey, vrepo, vy); // Reconstruct tanh(x): // // / tanh(z) if x <= 0 // tanh(x) = // \ -tanh(z) if x >= 0 vy = _mm256_xor_ps(vy, vinvsignx); _mm256_store_ps(output, vy); output += 8; } }
6,812
44.119205
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma3-expm1minus-rr1-lut8-p4h3ps-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma3_expm1minus_rr1_lut8_p4h3ps_div( size_t n, const float* input, float* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // The largest z for which tanhf(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.205968p+3f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p+19f); // Mask for the lowest 3 bits const __m128i vindex_mask = _mm_set1_epi32(0x7); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const __m256 vc4 = _mm256_set1_ps(0x1.5558ECp-1f); const __m256 vc3 = _mm256_set1_ps(0x1.555C20p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.000000p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); for (; n != 0; n -= sizeof(__m256)) { const __m256 vx = _mm256_load_ps(input); input += 8; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x <= 0 // f(x) := // \ -f(-x) if x >= 0 // // First we compute f(z) := expm1(2z) / (2 + expm1(2z)) where z = -abs(x), then negate the result if x >= 0. __m256 vz = _mm256_or_ps(vx, vsign_mask); // Inverted mask for the sign of input: 0x00000000 for negative x, 0x80000000 for positive x. const __m256 vinvsignx = _mm256_xor_ps(vx, vz); // The function saturates at -1 for large negative inputs: tanhf(z) == -1.0f for z <= sat_cutoff ~= -9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = _mm256_max_ps(vsat_cutoff, vz); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [-9.010913, 0]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. -9.010913 <= z <= 0. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for -9.010913 <= z <= 0 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const __m128 vn_hi = _mm256_extractf128_ps(vn, 1); const __m128i ve_lo = _mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 20); const __m128i ve_hi = _mm_slli_epi32(_mm_castps_si128(vn_hi), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const __m128i vidx_lo = _mm_and_si128(_mm_castps_si128(_mm256_castps256_ps128(vn)), vindex_mask); const __m128i vidx_hi = _mm_and_si128(_mm_castps_si128(vn_hi), vindex_mask); #if XNN_ARCH_X86_64 const uint64_t vidx01 = (uint64_t) _mm_cvtsi128_si64(vidx_lo); const uint64_t vidx45 = (uint64_t) _mm_cvtsi128_si64(vidx_hi); __m128i vl_lo = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx01]); __m128i vl_hi = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx45]); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx01 >> 32)], 1); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx45 >> 32)], 1); const uint64_t vidx23 = (uint64_t) _mm_extract_epi64(vidx_lo, 1); const uint64_t vidx67 = (uint64_t) _mm_extract_epi64(vidx_hi, 1); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx23], 2); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx67], 2); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx23 >> 32)], 3); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx67 >> 32)], 3); #else const uint32_t vidx0 = (uint32_t) _mm_cvtsi128_si32(vidx_lo); const uint32_t vidx4 = (uint32_t) _mm_cvtsi128_si32(vidx_hi); __m128i vl_lo = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx0]); __m128i vl_hi = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx4]); const uint32_t vidx1 = (uint32_t) _mm_extract_epi32(vidx_lo, 1); const uint32_t vidx5 = (uint32_t) _mm_extract_epi32(vidx_hi, 1); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx1], 1); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx5], 1); const uint32_t vidx2 = (uint32_t) _mm_extract_epi32(vidx_lo, 2); const uint32_t vidx6 = (uint32_t) _mm_extract_epi32(vidx_hi, 2); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx2], 2); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx6], 2); const uint32_t vidx3 = (uint32_t) _mm_extract_epi32(vidx_lo, 3); const uint32_t vidx7 = (uint32_t) _mm_extract_epi32(vidx_hi, 3); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx3], 3); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx7], 3); #endif // Adjust exponent of the value l fetched from the table to get the final s value. const __m128 vs_lo = _mm_castsi128_ps(_mm_add_epi32(vl_lo, ve_lo)); const __m128 vs_hi = _mm_castsi128_ps(_mm_add_epi32(vl_hi, ve_hi)); const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p __m256 vp = vc4; vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const __m256 vps = _mm256_mul_ps(vp, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); const __m256 vemo = _mm256_fmadd_ps(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const __m256 vepo = _mm256_add_ps(vemo, vtwo); // Reconstruct tanh(z) = expm1(2z) / (expm1(2z) + 2) __m256 vy = _mm256_div_ps(vemo, vepo); // Reconstruct tanh(x): // // / tanh(z) if x <= 0 // tanh(x) = // \ -tanh(z) if x >= 0 vy = _mm256_xor_ps(vy, vinvsignx); _mm256_store_ps(output, vy); output += 8; } }
8,701
49.300578
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma3-expm1minus-rr1-lut8-p4h3ps-nr1.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma3_expm1minus_rr1_lut8_p4h3ps_nr1( size_t n, const float* input, float* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // The largest z for which tanhf(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.205968p+3f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p+19f); // Mask for the lowest 3 bits const __m128i vindex_mask = _mm_set1_epi32(0x7); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const __m256 vc4 = _mm256_set1_ps(0x1.5558ECp-1f); const __m256 vc3 = _mm256_set1_ps(0x1.555C20p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.000000p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); for (; n != 0; n -= sizeof(__m256)) { const __m256 vx = _mm256_load_ps(input); input += 8; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x <= 0 // f(x) := // \ -f(-x) if x >= 0 // // First we compute f(z) := expm1(2z) / (2 + expm1(2z)) where z = -abs(x), then negate the result if x >= 0. __m256 vz = _mm256_or_ps(vx, vsign_mask); // Inverted mask for the sign of input: 0x00000000 for negative x, 0x80000000 for positive x. const __m256 vinvsignx = _mm256_xor_ps(vx, vz); // The function saturates at -1 for large negative inputs: tanhf(z) == -1.0f for z <= sat_cutoff ~= -9.010913. // To guarantee this behaviour, we compute the saturation mask here, and later use it to replace computed outputs // with the saturation value (-1). Note that for NaN inputs the saturation mask is inactive. const __m256 vm = _mm256_cmp_ps(vz, vsat_cutoff, _CMP_LE_OS); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [-9.010913, 0]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. -9.010913 <= z <= 0. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for -9.010913 <= z <= 0 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const __m128 vn_hi = _mm256_extractf128_ps(vn, 1); const __m128i ve_lo = _mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 20); const __m128i ve_hi = _mm_slli_epi32(_mm_castps_si128(vn_hi), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const __m128i vidx_lo = _mm_and_si128(_mm_castps_si128(_mm256_castps256_ps128(vn)), vindex_mask); const __m128i vidx_hi = _mm_and_si128(_mm_castps_si128(vn_hi), vindex_mask); #if XNN_ARCH_X86_64 const uint64_t vidx01 = (uint64_t) _mm_cvtsi128_si64(vidx_lo); const uint64_t vidx45 = (uint64_t) _mm_cvtsi128_si64(vidx_hi); __m128i vl_lo = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx01]); __m128i vl_hi = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx45]); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx01 >> 32)], 1); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx45 >> 32)], 1); const uint64_t vidx23 = (uint64_t) _mm_extract_epi64(vidx_lo, 1); const uint64_t vidx67 = (uint64_t) _mm_extract_epi64(vidx_hi, 1); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx23], 2); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx67], 2); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx23 >> 32)], 3); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx67 >> 32)], 3); #else const uint32_t vidx0 = (uint32_t) _mm_cvtsi128_si32(vidx_lo); const uint32_t vidx4 = (uint32_t) _mm_cvtsi128_si32(vidx_hi); __m128i vl_lo = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx0]); __m128i vl_hi = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx4]); const uint32_t vidx1 = (uint32_t) _mm_extract_epi32(vidx_lo, 1); const uint32_t vidx5 = (uint32_t) _mm_extract_epi32(vidx_hi, 1); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx1], 1); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx5], 1); const uint32_t vidx2 = (uint32_t) _mm_extract_epi32(vidx_lo, 2); const uint32_t vidx6 = (uint32_t) _mm_extract_epi32(vidx_hi, 2); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx2], 2); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx6], 2); const uint32_t vidx3 = (uint32_t) _mm_extract_epi32(vidx_lo, 3); const uint32_t vidx7 = (uint32_t) _mm_extract_epi32(vidx_hi, 3); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx3], 3); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx7], 3); #endif // Adjust exponent of the value l fetched from the table to get the final s value. const __m128 vs_lo = _mm_castsi128_ps(_mm_add_epi32(vl_lo, ve_lo)); const __m128 vs_hi = _mm_castsi128_ps(_mm_add_epi32(vl_hi, ve_hi)); const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p __m256 vp = vc4; vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const __m256 vps = _mm256_mul_ps(vp, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); const __m256 vemo = _mm256_fmadd_ps(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const __m256 vepo = _mm256_add_ps(vemo, vtwo); // Use Newton-Raphson method (1 iteration) to compute reciprocal of the denominator. // Note: 2 < exp(2z) + 1 <= 3, because z <= 0 and 0 < exp(2z) <= 1. // Thus the reciprocal of the denominator never overflows. __m256 vrepo = _mm256_rcp_ps(vepo); const __m256 verepo = _mm256_fnmsub_ps(vrepo, vepo, vminus_one); vrepo = _mm256_fmadd_ps(verepo, vrepo, vrepo); // Reconstruct tanh(z) := expm1(2z) / (2 + expm1(2z)) __m256 vy = _mm256_mul_ps(vemo, vrepo); // Saturate tanh(z) at -1 for large inputs. vy = _mm256_blendv_ps(vy, vminus_one, vm); // Reconstruct tanh(x): // // / tanh(z) if x <= 0 // tanh(x) = // \ -tanh(z) if x >= 0 vy = _mm256_xor_ps(vy, vinvsignx); _mm256_store_ps(output, vy); output += 8; } }
9,239
49.491803
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma3-expm1minus-rr1-lut8-p4h3ps-nr1adj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__fma3_expm1minus_rr1_lut8_p4h3ps_nr1adj( size_t n, const float* input, float* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // The largest z for which tanhf(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.205968p+3f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p+19f); // Mask for the lowest 3 bits const __m128i vindex_mask = _mm_set1_epi32(0x7); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const __m256 vc4 = _mm256_set1_ps(0x1.5558ECp-1f); const __m256 vc3 = _mm256_set1_ps(0x1.555C20p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.000000p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); for (; n != 0; n -= sizeof(__m256)) { const __m256 vx = _mm256_load_ps(input); input += 8; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x <= 0 // f(x) := // \ -f(-x) if x >= 0 // // First we compute f(z) := expm1(2z) / (2 + expm1(2z)) where z = -abs(x), then negate the result if x >= 0. __m256 vz = _mm256_or_ps(vx, vsign_mask); // Inverted mask for the sign of input: 0x00000000 for negative x, 0x80000000 for positive x. const __m256 vinvsignx = _mm256_xor_ps(vx, vz); // The function saturates at -1 for large negative inputs: tanhf(z) == -1.0f for z <= sat_cutoff ~= -9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = _mm256_max_ps(vsat_cutoff, vz); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [-9.010913, 0]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. -9.010913 <= z <= 0. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for -9.010913 <= z <= 0 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const __m128 vn_hi = _mm256_extractf128_ps(vn, 1); const __m128i ve_lo = _mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 20); const __m128i ve_hi = _mm_slli_epi32(_mm_castps_si128(vn_hi), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const __m128i vidx_lo = _mm_and_si128(_mm_castps_si128(_mm256_castps256_ps128(vn)), vindex_mask); const __m128i vidx_hi = _mm_and_si128(_mm_castps_si128(vn_hi), vindex_mask); #if XNN_ARCH_X86_64 const uint64_t vidx01 = (uint64_t) _mm_cvtsi128_si64(vidx_lo); const uint64_t vidx45 = (uint64_t) _mm_cvtsi128_si64(vidx_hi); __m128i vl_lo = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx01]); __m128i vl_hi = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx45]); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx01 >> 32)], 1); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx45 >> 32)], 1); const uint64_t vidx23 = (uint64_t) _mm_extract_epi64(vidx_lo, 1); const uint64_t vidx67 = (uint64_t) _mm_extract_epi64(vidx_hi, 1); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx23], 2); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx67], 2); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx23 >> 32)], 3); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) (vidx67 >> 32)], 3); #else const uint32_t vidx0 = (uint32_t) _mm_cvtsi128_si32(vidx_lo); const uint32_t vidx4 = (uint32_t) _mm_cvtsi128_si32(vidx_hi); __m128i vl_lo = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx0]); __m128i vl_hi = _mm_cvtsi32_si128((int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx4]); const uint32_t vidx1 = (uint32_t) _mm_extract_epi32(vidx_lo, 1); const uint32_t vidx5 = (uint32_t) _mm_extract_epi32(vidx_hi, 1); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx1], 1); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx5], 1); const uint32_t vidx2 = (uint32_t) _mm_extract_epi32(vidx_lo, 2); const uint32_t vidx6 = (uint32_t) _mm_extract_epi32(vidx_hi, 2); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx2], 2); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx6], 2); const uint32_t vidx3 = (uint32_t) _mm_extract_epi32(vidx_lo, 3); const uint32_t vidx7 = (uint32_t) _mm_extract_epi32(vidx_hi, 3); vl_lo = _mm_insert_epi32(vl_lo, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx3], 3); vl_hi = _mm_insert_epi32(vl_hi, (int) xnn_table_exp2minus_k_over_8[(uint32_t) vidx7], 3); #endif // Adjust exponent of the value l fetched from the table to get the final s value. const __m128 vs_lo = _mm_castsi128_ps(_mm_add_epi32(vl_lo, ve_lo)); const __m128 vs_hi = _mm_castsi128_ps(_mm_add_epi32(vl_hi, ve_hi)); const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1); // Subtract the large number back to get final n := round(z / log(2), 4) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-4 polynomial approximation for exp(2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * c4))) // = t * p __m256 vp = vc4; vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (p * s) * t const __m256 vps = _mm256_mul_ps(vp, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); const __m256 vemo = _mm256_fmadd_ps(vt, vps, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const __m256 vepo = _mm256_add_ps(vemo, vtwo); // Use Newton-Raphson method (1 iteration) to compute reciprocal of the denominator. // Note: 2 < exp(2z) + 1 <= 3, because z <= 0 and 0 < exp(2z) <= 1. // Thus the reciprocal of the denominator never overflows. __m256 vrepo = _mm256_rcp_ps(vepo); const __m256 verepo = _mm256_fnmsub_ps(vrepo, vepo, vminus_one); vrepo = _mm256_fmadd_ps(verepo, vrepo, vrepo); // Reconstruct tanh(z) := expm1(2z) / (2 + expm1(2z)) __m256 vy = _mm256_mul_ps(vemo, vrepo); // Adjust reconstructred expm1(2z) / (2 + expm1(2z)) to match the correctly rounded division result const __m256 vey = _mm256_fnmadd_ps(vy, vepo, vemo); vy = _mm256_fmadd_ps(vey, vrepo, vy); // Reconstruct tanh(x): // // / tanh(z) if x <= 0 // tanh(x) = // \ -tanh(z) if x >= 0 vy = _mm256_xor_ps(vy, vinvsignx); _mm256_store_ps(output, vy); output += 8; } }
9,295
49.521739
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma3-expm1minus-rr1-p6h5ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma3_expm1minus_rr1_p6h5ts_div( size_t n, const float* input, float* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // The largest z for which tanhf(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.205968p+3f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f); // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp+22f); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const __m256 vc6 = _mm256_set1_ps(0x1.6B7338p-4f); const __m256 vc5 = _mm256_set1_ps(0x1.12278Ep-2f); const __m256 vc4 = _mm256_set1_ps(0x1.555716p-1f); const __m256 vc3 = _mm256_set1_ps(0x1.5554B0p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFFFEp+0f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); for (; n != 0; n -= sizeof(__m256)) { const __m256 vx = _mm256_load_ps(input); input += 8; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x <= 0 // f(x) := // \ -f(-x) if x >= 0 // // First we compute f(z) := expm1(2z) / (2 + expm1(2z)) where z = -abs(x), then negate the result if x >= 0. __m256 vz = _mm256_or_ps(vx, vsign_mask); // Inverted mask for the sign of input: 0x00000000 for negative x, 0x80000000 for positive x. const __m256 vinvsignx = _mm256_xor_ps(vx, vz); // The function saturates at -1 for large negative inputs: tanhf(z) == -1.0f for z <= sat_cutoff ~= -9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = _mm256_max_ps(vsat_cutoff, vz); // Compute reduced argument n := round(z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [-9.010913, 0]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // -9.010913 <= z <= 0, and -13 <= n <= 0 accordingly. const __m128 vn_hi = _mm256_extractf128_ps(vn, 1); __m256 vs = _mm256_castps128_ps256(_mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23))); const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn_hi), 23)); vs = _mm256_insertf128_ps(vs, vs_hi, 1); // Subtract the large number back to get final n := round(z / log(2), 1) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-6 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p __m256 vp = vc6; vp = _mm256_fmadd_ps(vp, vt, vc5); vp = _mm256_fmadd_ps(vp, vt, vc4); vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const __m256 vts = _mm256_mul_ps(vt, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); const __m256 vemo = _mm256_fmadd_ps(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const __m256 vepo = _mm256_add_ps(vemo, vtwo); // Reconstruct tanh(z) = expm1(2z) / (expm1(2z) + 2) __m256 vy = _mm256_div_ps(vemo, vepo); // Reconstruct tanh(x): // // / tanh(z) if x <= 0 // tanh(x) = // \ -tanh(z) if x >= 0 vy = _mm256_xor_ps(vy, vinvsignx); _mm256_store_ps(output, vy); output += 8; } }
5,287
40.637795
123
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma3-expm1minus-rr1-p6h5ts-nr1.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma3_expm1minus_rr1_p6h5ts_nr1( size_t n, const float* input, float* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // The largest z for which tanhf(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.205968p+3f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f); // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp+22f); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const __m256 vc6 = _mm256_set1_ps(0x1.6B7338p-4f); const __m256 vc5 = _mm256_set1_ps(0x1.12278Ep-2f); const __m256 vc4 = _mm256_set1_ps(0x1.555716p-1f); const __m256 vc3 = _mm256_set1_ps(0x1.5554B0p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFFFEp+0f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); for (; n != 0; n -= sizeof(__m256)) { const __m256 vx = _mm256_load_ps(input); input += 8; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x <= 0 // f(x) := // \ -f(-x) if x >= 0 // // First we compute f(z) := expm1(2z) / (2 + expm1(2z)) where z = -abs(x), then negate the result if x >= 0. __m256 vz = _mm256_or_ps(vx, vsign_mask); // Inverted mask for the sign of input: 0x00000000 for negative x, 0x80000000 for positive x. const __m256 vinvsignx = _mm256_xor_ps(vx, vz); // The function saturates at -1 for large negative inputs: tanhf(z) == -1.0f for z <= sat_cutoff ~= -9.010913. // To guarantee this behaviour, we compute the saturation mask here, and later use it to replace computed outputs // with the saturation value (-1). Note that for NaN inputs the saturation mask is inactive. const __m256 vm = _mm256_cmp_ps(vz, vsat_cutoff, _CMP_LE_OS); // Compute reduced argument n := round(z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [-9.010913, 0]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // -9.010913 <= z <= 0, and -13 <= n <= 0 accordingly. const __m128 vn_hi = _mm256_extractf128_ps(vn, 1); __m256 vs = _mm256_castps128_ps256(_mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23))); const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn_hi), 23)); vs = _mm256_insertf128_ps(vs, vs_hi, 1); // Subtract the large number back to get final n := round(z / log(2), 1) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-6 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p __m256 vp = vc6; vp = _mm256_fmadd_ps(vp, vt, vc5); vp = _mm256_fmadd_ps(vp, vt, vc4); vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const __m256 vts = _mm256_mul_ps(vt, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); const __m256 vemo = _mm256_fmadd_ps(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const __m256 vepo = _mm256_add_ps(vemo, vtwo); // Use Newton-Raphson method (1 iteration) to compute reciprocal of the denominator. // Note: 2 < exp(2z) + 1 <= 3, because z <= 0 and 0 < exp(2z) <= 1. // Thus the reciprocal of the denominator never overflows. __m256 vrepo = _mm256_rcp_ps(vepo); const __m256 verepo = _mm256_fnmsub_ps(vrepo, vepo, vminus_one); vrepo = _mm256_fmadd_ps(verepo, vrepo, vrepo); // Reconstruct tanh(z) := expm1(2z) / (2 + expm1(2z)) __m256 vy = _mm256_mul_ps(vemo, vrepo); // Saturate tanh(z) at -1 for large inputs. vy = _mm256_blendv_ps(vy, vminus_one, vm); // Reconstruct tanh(x): // // / tanh(z) if x <= 0 // tanh(x) = // \ -tanh(z) if x >= 0 vy = _mm256_xor_ps(vy, vinvsignx); _mm256_store_ps(output, vy); output += 8; } }
5,825
41.525547
123
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-fma3-expm1minus-rr1-p6h5ts-nr1adj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__fma3_expm1minus_rr1_p6h5ts_nr1adj( size_t n, const float* input, float* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // The largest z for which tanhf(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.205968p+3f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f); // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp+22f); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(2t) - 1 ~ t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const __m256 vc6 = _mm256_set1_ps(0x1.6B7338p-4f); const __m256 vc5 = _mm256_set1_ps(0x1.12278Ep-2f); const __m256 vc4 = _mm256_set1_ps(0x1.555716p-1f); const __m256 vc3 = _mm256_set1_ps(0x1.5554B0p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFFFEp+0f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); for (; n != 0; n -= sizeof(__m256)) { const __m256 vx = _mm256_load_ps(input); input += 8; // General structure of the algorithm: // // / expm1(2x) / (2 + expm1(2x)) if x <= 0 // f(x) := // \ -f(-x) if x >= 0 // // First we compute f(z) := expm1(2z) / (2 + expm1(2z)) where z = -abs(x), then negate the result if x >= 0. __m256 vz = _mm256_or_ps(vx, vsign_mask); // Inverted mask for the sign of input: 0x00000000 for negative x, 0x80000000 for positive x. const __m256 vinvsignx = _mm256_xor_ps(vx, vz); // The function saturates at -1 for large negative inputs: tanhf(z) == -1.0f for z <= sat_cutoff ~= -9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = _mm256_max_ps(vsat_cutoff, vz); // Compute reduced argument n := round(z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [-9.010913, 0]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // -9.010913 <= z <= 0, and -13 <= n <= 0 accordingly. const __m128 vn_hi = _mm256_extractf128_ps(vn, 1); __m256 vs = _mm256_castps128_ps256(_mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23))); const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn_hi), 23)); vs = _mm256_insertf128_ps(vs, vs_hi, 1); // Subtract the large number back to get final n := round(z / log(2), 1) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-6 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * p __m256 vp = vc6; vp = _mm256_fmadd_ps(vp, vt, vc5); vp = _mm256_fmadd_ps(vp, vt, vc4); vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * p + (s - 1) // = (s - 1) + (t * s) * p const __m256 vts = _mm256_mul_ps(vt, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); const __m256 vemo = _mm256_fmadd_ps(vp, vts, vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const __m256 vepo = _mm256_add_ps(vemo, vtwo); // Use Newton-Raphson method (1 iteration) to compute reciprocal of the denominator. // Note: 2 < exp(2z) + 1 <= 3, because z <= 0 and 0 < exp(2z) <= 1. // Thus the reciprocal of the denominator never overflows. __m256 vrepo = _mm256_rcp_ps(vepo); const __m256 verepo = _mm256_fnmsub_ps(vrepo, vepo, vminus_one); vrepo = _mm256_fmadd_ps(verepo, vrepo, vrepo); // Reconstruct tanh(z) := expm1(2z) / (2 + expm1(2z)) __m256 vy = _mm256_mul_ps(vemo, vrepo); // Adjust reconstructred expm1(2z) / (2 + expm1(2z)) to match the correctly rounded division result const __m256 vey = _mm256_fnmadd_ps(vy, vepo, vemo); vy = _mm256_fmadd_ps(vey, vrepo, vy); // Reconstruct tanh(x): // // / tanh(z) if x <= 0 // tanh(x) = // \ -tanh(z) if x >= 0 vy = _mm256_xor_ps(vy, vinvsignx); _mm256_store_ps(output, vy); output += 8; } }
5,881
41.623188
123
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neon-expm1minus-rr1-p6h5ts-nr2recps.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__neon_expm1minus_rr1_p6h5ts_nr2recps( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp+22f); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float32x4_t vc6 = vmovq_n_f32(0x1.6B7338p-4f); const float32x4_t vc5 = vmovq_n_f32(-0x1.12278Ep-2f); const float32x4_t vc4 = vmovq_n_f32(0x1.555716p-1f); const float32x4_t vc3 = vmovq_n_f32(-0x1.5554B0p+0f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFFFEp+0f); const float32x4_t vtwo = vmovq_n_f32(2.0f); const float32x4_t vone = vmovq_n_f32(1.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vmlaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vmlaq_f32(vz, vn, vln2); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * (-p) float32x4_t vp = vmlaq_f32(vc5, vc6, vt); vp = vmlaq_f32(vc4, vp, vt); vp = vmlaq_f32(vc3, vp, vt); vp = vmlaq_f32(vc2, vp, vt); vp = vmlsq_f32(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (t * s) * p const float32x4_t vts = vmulq_f32(vt, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); const float32x4_t vemo = vmlsq_f32(vsmo, vp, vts); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
5,334
42.024194
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neon-expm1minus-rr2-lut8-p4h2ts-nr2recps.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neon_expm1minus_rr2_lut8_p4h2ts_nr2recps( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); // Last 7 bits are zeroes const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E400p-1f); const float32x4_t vln2_lo = vmovq_n_f32(0x1.7F7D1Cp-20f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(-0x1.5558ECp-2f); const float32x4_t vc3 = vmovq_n_f32(0x1.555C20p-1f); const float32x4_t vc2 = vmovq_n_f32(-0x1.000000p+0f); const float32x4_t vone = vmovq_n_f32(1.0f); const float32x4_t vtwo = vmovq_n_f32(2.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vmlaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vmlaq_f32(vz, vn, vln2_hi); vt = vmlaq_f32(vt, vn, vln2_lo); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float32x4_t vp = vmlaq_f32(vc3, vc4, vt); vp = vmlaq_f32(vc2, vp, vt); vp = vmulq_f32(vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float32x4_t vts = vmulq_f32(vt, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); vp = vmlaq_f32(vts, vp, vts); const float32x4_t vemo = vmlsq_f32(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,974
45.812081
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neon-expm1minus-rr2-lut8-p4h3ps-nr2recps.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neon_expm1minus_rr2_lut8_p4h3ps_nr2recps( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); // Last 7 bits are zeroes const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E400p-1f); const float32x4_t vln2_lo = vmovq_n_f32(0x1.7F7D1Cp-20f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(0x1.5558ECp-1f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555C20p+0f); const float32x4_t vc2 = vmovq_n_f32(0x1.000000p+1f); const float32x4_t vtwo = vmovq_n_f32(2.0f); const float32x4_t vone = vmovq_n_f32(1.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vmlaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vmlaq_f32(vz, vn, vln2_hi); vt = vmlaq_f32(vt, vn, vln2_lo); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * (-p) float32x4_t vp = vmlaq_f32(vc3, vc4, vt); vp = vmlaq_f32(vc2, vp, vt); vp = vmlsq_f32(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (p * s) * t const float32x4_t vps = vmulq_f32(vp, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); const float32x4_t vemo = vmlsq_f32(vsmo, vt, vps); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,894
45.587838
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neonfma-expm1minus-rr1-lut8-p4h2ts-nr1recps1fma.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neonfma_expm1minus_rr1_lut8_p4h2ts_nr1recps1fma( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(-0x1.5558ECp-2f); const float32x4_t vc3 = vmovq_n_f32(0x1.555C20p-1f); const float32x4_t vc2 = vmovq_n_f32(-0x1.000000p+0f); const float32x4_t vone = vmovq_n_f32(1.0f); const float32x4_t vtwo = vmovq_n_f32(2.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float32x4_t vp = vfmaq_f32(vc3, vc4, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vmulq_f32(vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float32x4_t vts = vmulq_f32(vt, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); vp = vfmaq_f32(vts, vp, vts); const float32x4_t vemo = vfmsq_f32(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); verepo = vfmsq_f32(vone, vrepo, vepo); vrepo = vfmaq_f32(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,760
45.627586
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neonfma-expm1minus-rr1-lut8-p4h2ts-nr2fma.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neonfma_expm1minus_rr1_lut8_p4h2ts_nr2fma( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(-0x1.5558ECp-2f); const float32x4_t vc3 = vmovq_n_f32(0x1.555C20p-1f); const float32x4_t vc2 = vmovq_n_f32(-0x1.000000p+0f); const float32x4_t vone = vmovq_n_f32(1.0f); const float32x4_t vtwo = vmovq_n_f32(2.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float32x4_t vp = vfmaq_f32(vc3, vc4, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vmulq_f32(vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float32x4_t vts = vmulq_f32(vt, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); vp = vfmaq_f32(vts, vp, vts); const float32x4_t vemo = vfmsq_f32(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vfmsq_f32(vone, vrepo, vepo); vrepo = vfmaq_f32(vrepo, vrepo, verepo); verepo = vfmsq_f32(vone, vrepo, vepo); vrepo = vfmaq_f32(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,765
45.662069
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neonfma-expm1minus-rr1-lut8-p4h2ts-nr2recps.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neonfma_expm1minus_rr1_lut8_p4h2ts_nr2recps( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(-0x1.5558ECp-2f); const float32x4_t vc3 = vmovq_n_f32(0x1.555C20p-1f); const float32x4_t vc2 = vmovq_n_f32(-0x1.000000p+0f); const float32x4_t vone = vmovq_n_f32(1.0f); const float32x4_t vtwo = vmovq_n_f32(2.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = -2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // = -2 * (t + t * p) float32x4_t vp = vfmaq_f32(vc3, vc4, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vmulq_f32(vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (-2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) + 1) - 1 // = s * (-2 * (t + t * p) + 1) - 1 // = (s - 1) - 2 * ((t * s) + (t * s) * p) const float32x4_t vts = vmulq_f32(vt, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); vp = vfmaq_f32(vts, vp, vts); const float32x4_t vemo = vfmsq_f32(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,745
45.524138
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neonfma-expm1minus-rr1-lut8-p4h3ps-nr1recps1fma.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neonfma_expm1minus_rr1_lut8_p4h3ps_nr1recps1fma( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(0x1.5558ECp-1f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555C20p+0f); const float32x4_t vc2 = vmovq_n_f32(0x1.000000p+1f); const float32x4_t vtwo = vmovq_n_f32(2.0f); const float32x4_t vone = vmovq_n_f32(1.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * (-p) float32x4_t vp = vfmaq_f32(vc3, vc4, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmsq_f32(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (p * s) * t const float32x4_t vps = vmulq_f32(vp, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); const float32x4_t vemo = vfmsq_f32(vsmo, vt, vps); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); verepo = vfmsq_f32(vone, vrepo, vepo); vrepo = vfmaq_f32(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,680
45.395833
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neonfma-expm1minus-rr1-lut8-p4h3ps-nr1recps1fmaadj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neonfma_expm1minus_rr1_lut8_p4h3ps_nr1recps1fmaadj( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(0x1.5558ECp-1f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555C20p+0f); const float32x4_t vc2 = vmovq_n_f32(0x1.000000p+1f); const float32x4_t vtwo = vmovq_n_f32(2.0f); const float32x4_t vone = vmovq_n_f32(1.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * (-p) float32x4_t vp = vfmaq_f32(vc3, vc4, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmsq_f32(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (p * s) * t const float32x4_t vps = vmulq_f32(vp, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); const float32x4_t vemo = vfmsq_f32(vsmo, vt, vps); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); verepo = vfmsq_f32(vone, vrepo, vepo); vrepo = vfmaq_f32(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const float32x4_t vey = vfmsq_f32(vemo, vy, vepo); vy = vfmaq_f32(vy, vey, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,880
45.809524
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neonfma-expm1minus-rr1-lut8-p4h3ps-nr2fma.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neonfma_expm1minus_rr1_lut8_p4h3ps_nr2fma( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(0x1.5558ECp-1f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555C20p+0f); const float32x4_t vc2 = vmovq_n_f32(0x1.000000p+1f); const float32x4_t vtwo = vmovq_n_f32(2.0f); const float32x4_t vone = vmovq_n_f32(1.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * (-p) float32x4_t vp = vfmaq_f32(vc3, vc4, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmsq_f32(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (p * s) * t const float32x4_t vps = vmulq_f32(vp, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); const float32x4_t vemo = vfmsq_f32(vsmo, vt, vps); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vfmsq_f32(vone, vrepo, vepo); vrepo = vfmaq_f32(vrepo, vrepo, verepo); verepo = vfmsq_f32(vone, vrepo, vepo); vrepo = vfmaq_f32(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,685
45.430556
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neonfma-expm1minus-rr1-lut8-p4h3ps-nr2fmaadj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neonfma_expm1minus_rr1_lut8_p4h3ps_nr2fmaadj( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(0x1.5558ECp-1f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555C20p+0f); const float32x4_t vc2 = vmovq_n_f32(0x1.000000p+1f); const float32x4_t vtwo = vmovq_n_f32(2.0f); const float32x4_t vone = vmovq_n_f32(1.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * (-p) float32x4_t vp = vfmaq_f32(vc3, vc4, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmsq_f32(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (p * s) * t const float32x4_t vps = vmulq_f32(vp, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); const float32x4_t vemo = vfmsq_f32(vsmo, vt, vps); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vfmsq_f32(vone, vrepo, vepo); vrepo = vfmaq_f32(vrepo, vrepo, verepo); verepo = vfmsq_f32(vone, vrepo, vepo); vrepo = vfmaq_f32(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const float32x4_t vey = vfmsq_f32(vemo, vy, vepo); vy = vfmaq_f32(vy, vey, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,885
45.843537
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neonfma-expm1minus-rr1-lut8-p4h3ps-nr2recps.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neonfma_expm1minus_rr1_lut8_p4h3ps_nr2recps( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(0x1.5558ECp-1f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555C20p+0f); const float32x4_t vc2 = vmovq_n_f32(0x1.000000p+1f); const float32x4_t vtwo = vmovq_n_f32(2.0f); const float32x4_t vone = vmovq_n_f32(1.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * (-p) float32x4_t vp = vfmaq_f32(vc3, vc4, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmsq_f32(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (p * s) * t const float32x4_t vps = vmulq_f32(vp, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); const float32x4_t vemo = vfmsq_f32(vsmo, vt, vps); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,665
45.291667
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neonfma-expm1minus-rr1-lut8-p4h3ps-nr2recpsadj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_8[8]; void xnn_math_f32_tanh__neonfma_expm1minus_rr1_lut8_p4h3ps_nr2recpsadj( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == exp2(-4) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p+19f); // Mask for the lowest 3 bits const uint64x2_t vindex_mask = vreinterpretq_u64_u32(vmovq_n_u32(UINT32_C(0x7))); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * c4))) // on [-log(2)/32, log(2)/32] const float32x4_t vc4 = vmovq_n_f32(0x1.5558ECp-1f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555C20p+0f); const float32x4_t vc2 = vmovq_n_f32(0x1.000000p+1f); const float32x4_t vtwo = vmovq_n_f32(2.0f); const float32x4_t vone = vmovq_n_f32(1.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**18, i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**(2n) for valid inputs, i.e. 0 <= z <= 9.010913. As // n has 4 fractional bits, we split s == 2**(2n) = 2**int(2n) * 2**frac(2n). We create s in two steps: // 1. Fetch 2**frac(2n) from the table using the 3 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their unbiased floating-point exponent is 0. // 2. Adjust fetched value by addition of int(2n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 9.010913 we have -13 <= int(n) <= 0, and thus the adjusted exponent is not // lower than -13. // // Shift bits 3:11 into 23:31 (position of floating-point exponent). const uint32x4_t ve = vshlq_n_u32(vreinterpretq_u32_f32(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vandq_u64(vreinterpretq_u64_f32(vn), vindex_mask); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); uint32x2_t vl_lo = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_lo]); uint32x2_t vl_hi = vld1_dup_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) vidx_hi]); vl_lo = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_lo >> 32)], vl_lo, 1); vl_hi = vld1_lane_u32(&xnn_table_exp2minus_k_over_8[(uint32_t) (vidx_hi >> 32)], vl_hi, 1); const uint32x4_t vl = vcombine_u32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_u32(vaddq_u32(vl, ve)); // Subtract the large number back to get final n := round(-z / log(2), 4) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-4 polynomial approximation for exp(-2t) - 1 on [-log(2)/32, log(2)/32]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * c4))) // = t * (-p) float32x4_t vp = vfmaq_f32(vc3, vc4, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmsq_f32(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * c4))) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (p * s) * t const float32x4_t vps = vmulq_f32(vp, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); const float32x4_t vemo = vfmsq_f32(vsmo, vt, vps); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const float32x4_t vey = vfmsq_f32(vemo, vy, vepo); vy = vfmaq_f32(vy, vey, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,865
45.707483
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-neonfma-expm1minus-rr1-p6h5ts-nr1recps1fma.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-neon-expm1minus.c.in // Generator: tools/xngen // // Copyright 2023 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <math.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_tanh__neonfma_expm1minus_rr1_p6h5ts_nr1recps1fma( size_t n, const float* input, float* output) { assert(n % sizeof(float32x4_t) == 0); // The smallest z for which tanhf(-z) is saturated at -1.0f. const float32x4_t vsat_cutoff = vmovq_n_f32(0x1.205968p+3f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Large number such that ulp(magic bias) == 0.5 and magic bias === 63.5 mod 2**21. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp+22f); const float32x4_t vln2 = vmovq_n_f32(0x1.62E430p-1f); // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // on [-log(2)/4, log(2)/4] const float32x4_t vc6 = vmovq_n_f32(0x1.6B7338p-4f); const float32x4_t vc5 = vmovq_n_f32(-0x1.12278Ep-2f); const float32x4_t vc4 = vmovq_n_f32(0x1.555716p-1f); const float32x4_t vc3 = vmovq_n_f32(-0x1.5554B0p+0f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFFFEp+0f); const float32x4_t vtwo = vmovq_n_f32(2.0f); const float32x4_t vone = vmovq_n_f32(1.0f); // Mask for the sign bit. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= sizeof(float32x4_t)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / -expm1(-2x) / (2 + expm1(-2x)) if x >= 0 // f(x) := // \ -f(-x) if x <= 0 // // First we compute y := expm1(-2z) / (2 + expm1(-2z)) where z = abs(x), // then set its sign according to the sign of x: f(x) := sign(x) * abs(y). float32x4_t vz = vabsq_f32(vx); // The function saturates at -1 for large positive inputs: tanhf(-z) == -1.0f for z >= sat_cutoff ~= 9.010913. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhf(sat_cutoff) == -1.0f. NaN inputs are passed unchanged. vz = vminq_f32(vz, vsat_cutoff); // Compute reduced argument n := round(-z / log(2), 1). // We do it by adding a large number (magic bias), which cause rounding of the result to 1 fractional bit, // then subtracing the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**21, i.e. |z| <= 0x1.62E43p+20 = 1453635.0), but that is acceptable, because inputs x // outside of [-9.010913, 9.010913] (i.e. z outsize [0, 9.010913]) saturate tanhf(x). // Additionally, we fuse addition of the floating-point exponent bias (127) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**(2n) for inputs which don't cause underflow, i.e. // 0 <= z <= 9.010913, and -13 <= n <= 0 accordingly. const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-6 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) // = t * (-p) float32x4_t vp = vfmaq_f32(vc5, vc6, vt); vp = vfmaq_f32(vc4, vp, vt); vp = vfmaq_f32(vc3, vp, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmsq_f32(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (t * s) * p const float32x4_t vts = vmulq_f32(vt, vs); const float32x4_t vsmo = vsubq_f32(vs, vone); const float32x4_t vemo = vfmsq_f32(vsmo, vp, vts); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float32x4_t vepo = vaddq_f32(vemo, vtwo); // Use Newton-Raphson method (2 iterations) to compute reciprocal of the denominator. // Note: 2 < exp(-2z) + 1 <= 3, because z <= 0 and 0 < exp(-2z) <= 1. // Thus the reciprocal of the denominator never overflows. float32x4_t vrepo = vrecpeq_f32(vepo); float32x4_t verepo = vrecpsq_f32(vrepo, vepo); vrepo = vmulq_f32(vrepo, verepo); verepo = vfmsq_f32(vone, vrepo, vepo); vrepo = vfmaq_f32(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vmulq_f32(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
5,352
42.169355
116
c