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/f32-sigmoid-neonfma-rr2-lut64-p2-nr1recps1fma.c
// Copyright 2019 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 <arm_neon.h> #include <xnnpack/common.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 float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__neonfma_rr2_lut64_p2_nr1recps1fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-6) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p17f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 6 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F)); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // 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 integer, 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 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) 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 floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(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_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 6) 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 = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p float32x4_t vp = vmulq_f32(vt, vc2); vp = vfmsq_f32(vt, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const float32x4_t vy = vfmsq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,314
48.724409
125
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr2-lut64-p2-nr2fma.c
// Copyright 2019 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 <arm_neon.h> #include <xnnpack/common.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 float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__neonfma_rr2_lut64_p2_nr2fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-6) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p17f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 6 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F)); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // 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 integer, 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 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) 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 floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(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_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 6) 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 = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p float32x4_t vp = vmulq_f32(vt, vc2); vp = vfmsq_f32(vt, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const float32x4_t vy = vfmsq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,316
48.740157
125
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr2-lut64-p2-nr2recps.c
// Copyright 2019 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 <arm_neon.h> #include <xnnpack/common.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 float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__neonfma_rr2_lut64_p2_nr2recps( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-6) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p17f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 6 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F)); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // 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 integer, 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 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) 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 floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(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_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 6) 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 = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p float32x4_t vp = vmulq_f32(vt, vc2); vp = vfmsq_f32(vt, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const float32x4_t vy = vfmsq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,302
48.629921
125
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr2-p5-nr1recps1fma.c
// Copyright 2019 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 <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__neonfma_rr2_p5_nr1recps1fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const float32x4_t vc5 = vmovq_n_f32(-0x1.0F9F9Cp-7f); const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555A80p-3f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f); const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFF6p-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+22 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= 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 the final n := round(-z / log(2)) 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 = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p float32x4_t vp = vfmaq_f32(vc4, vc5, vt); vp = vfmaq_f32(vc3, vp, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmaq_f32(vc1, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s * (1 + t * p) // = s + (t * s) * p vt = vmulq_f32(vt, vs); float32x4_t ve = vfmaq_f32(vs, vp, vt); // Denominator of the sigmoid fraction: 1.0 + exp(-z) float32x4_t vd = vaddq_f32(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
4,788
42.93578
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr2-p5-nr2fma.c
// Copyright 2019 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 <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__neonfma_rr2_p5_nr2fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const float32x4_t vc5 = vmovq_n_f32(-0x1.0F9F9Cp-7f); const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555A80p-3f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f); const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFF6p-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+22 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= 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 the final n := round(-z / log(2)) 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 = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p float32x4_t vp = vfmaq_f32(vc4, vc5, vt); vp = vfmaq_f32(vc3, vp, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmaq_f32(vc1, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s * (1 + t * p) // = s + (t * s) * p vt = vmulq_f32(vt, vs); float32x4_t ve = vfmaq_f32(vs, vp, vt); // Denominator of the sigmoid fraction: 1.0 + exp(-z) float32x4_t vd = vaddq_f32(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
4,790
42.954128
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr2-p5-nr2recps.c
// Copyright 2019 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 <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__neonfma_rr2_p5_nr2recps( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const float32x4_t vc5 = vmovq_n_f32(-0x1.0F9F9Cp-7f); const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555A80p-3f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f); const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFF6p-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+22 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= 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 the final n := round(-z / log(2)) 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 = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p float32x4_t vp = vfmaq_f32(vc4, vc5, vt); vp = vfmaq_f32(vc3, vp, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmaq_f32(vc1, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s * (1 + t * p) // = s + (t * s) * p vt = vmulq_f32(vt, vs); float32x4_t ve = vfmaq_f32(vs, vp, vt); // Denominator of the sigmoid fraction: 1.0 + exp(-z) float32x4_t vd = vaddq_f32(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
4,776
42.825688
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-scalar-rr2-lut2048-p1-div.c
// Copyright 2019 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 <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 2048) values decremented (as integer) by (k << 12), k = 0..2048 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_2048[2048]; void xnn_math_f32_sigmoid__scalar_rr2_lut2048_p1_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // Large number such that ulp(magic bias) == exp2(-11) const float vmagic_bias = 0x1.800000p12f; const float vminus_log2e = -0x1.715476p0f; // Mask for the lowest 11 bits const uint32_t vindex_mask = UINT32_C(0x7FF); // Last 13 bits are zeroes const float vln2_hi = 0x1.600000p-1f; const float vln2_lo = 0x1.7217F8p-8f; // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * c1 on [-log(2)/2048, log(2)/2048] const float vc1 = -0x1.FFFFFEp-1f; const float vone = 1.0f; // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float vdenorm_cutoff = 0x1.5D589Ep+6f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float vz = fabsf(vx); // Compute reduced argument n := round(-z / log(2), 11). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**11, i.e. |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float vn = vz * vminus_log2e + vmagic_bias; // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 11 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 11:19 into 23:31 (position of floating-point exponent). const uint32_t ve = float_as_uint32(vn) << 12; // Use bits 0:11 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = float_as_uint32(vn) & vindex_mask; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(xnn_table_exp2minus_k_over_2048[vidx] + ve); // Subtract the large number back to get the final n := round(-z / log(2), 11) 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 = vn * vln2_hi + vz; vt = vn * vln2_lo + vt; // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/2048, log(2)/2048]: // P(t) = 1 + t * c1 = 1 + p const float vp = vt * vc1; // Reconstruct the exp(-z) value: // e = s * (1 + t * c1) // = s * (1 + p) // = s + s * p const float vy = vp * vs + vs; // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float vf = vy / (vy + vone); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) { vf = 0.0f; } // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) if XNN_UNPREDICTABLE(vx > 0.0f) { vf = vone - vf; } *output++ = vf; } }
4,729
40.858407
118
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-scalar-rr2-lut64-p2-div.c
// Copyright 2019 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 <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_sigmoid__scalar_rr2_lut64_p2_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // Large number such that ulp(magic bias) == exp2(-6) const float vmagic_bias = 0x1.800000p17f; const float vminus_log2e = -0x1.715476p0f; // Mask for the lowest 6 bits const uint32_t vindex_mask = UINT32_C(0x3F); // Last 13 bits are zeroes const float vln2_hi = 0x1.630000p-1f; const float vln2_lo = -0x1.BD0106p-13f; // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const float vc2 = 0x1.FFFF0Ap-2f; const float vone = 1.0f; // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float vdenorm_cutoff = 0x1.5D589Ep+6f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float vz = fabsf(vx); // 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 integer, 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 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float vn = vz * vminus_log2e + vmagic_bias; // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) 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 floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const uint32_t ve = float_as_uint32(vn) << 17; // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint32_t vidx = float_as_uint32(vn) & vindex_mask; // Adjust exponent of the value l fetched from the table to get the final s value. const float vs = uint32_as_float(xnn_table_exp2minus_k_over_64[vidx] + ve); // Subtract the large number back to get the 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 = vn * vln2_hi + vz; vt = vn * vln2_lo + vt; // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p float vp = vt * vc2; vp = vt - vp * vt; // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const float vy = vs - vs * vp; // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float vf = vy / (vy + vone); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) { vf = 0.0f; } // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) if XNN_UNPREDICTABLE(vx > 0.0f) { vf = vone - vf; } *output++ = vf; } }
4,775
40.530435
118
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-scalar-rr2-p5-div.c
// Copyright 2019 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 <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__scalar_rr2_p5_div( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const float vmagic_bias = 0x1.8000FEp23f; const float vminus_log2e = -0x1.715476p+0f; // Last 7 bits are zeroes const float vln2_hi = 0x1.62E400p-1f; const float vln2_lo = 0x1.7F7D1Cp-20f; // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const float vc5 = -0x1.0F9F9Cp-7f; const float vc4 = 0x1.573A1Ap-5f; const float vc3 = -0x1.555A80p-3f; const float vc2 = 0x1.FFFDC6p-2f; const float vc1 = -0x1.FFFFF6p-1f; const float vone = 1.0f; // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float vdenorm_cutoff = 0x1.5D589Ep+6f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float vz = fabsf(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float vn = vz * vminus_log2e + vmagic_bias; // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly. const float vs = uint32_as_float(float_as_uint32(vn) << 23); // Subtract the large number back to get the final n := round(-z / log(2)) 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 = vn * vln2_hi + vz; vt = vn * vln2_lo + vt; // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p float vp = vt * vc5 + vc4; vp = vt * vp + vc3; vp = vt * vp + vc2; vp = vt * vp + vc1; // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s * (1 + t * p) // = s + (t * s) * p vt *= vs; const float ve = vt * vp + vs; // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float vf = ve / (ve + vone); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) { vf = 0.0f; } // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) if XNN_UNPREDICTABLE(vx > 0.0f) { vf = vone - vf; } *output++ = vf; } }
3,901
36.519231
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-sse2-rr2-lut64-p2-div.c
// Copyright 2020 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 <emmintrin.h> #include <xnnpack/common.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 float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__sse2_rr2_lut64_p2_div( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m128 vsign_mask = _mm_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m128 vmagic_bias = _mm_set1_ps(0x1.800000p17f); const __m128 vlog2e = _mm_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m128i vindex_mask = _mm_set1_epi32(INT32_C(0x3F)); // Last 13 bits are zeroes const __m128 vminus_ln2_hi = _mm_set1_ps(-0x1.630000p-1f); const __m128 vminus_ln2_lo = _mm_set1_ps(0x1.BD0106p-13f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m128 vc2 = _mm_set1_ps(0x1.FFFF0Ap-2f); const __m128 vone = _mm_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m128 vdenorm_cutoff = _mm_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m128 vz = _mm_or_ps(vx, vsign_mask); // 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 [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) 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 floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const __m128i ve = _mm_slli_epi32(_mm_castps_si128(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m128i vidx = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn), vindex_mask), 2); #if XNN_ARCH_X86_64 const uint64_t vidx_lo = (uint64_t) _mm_cvtsi128_si64(vidx); const uint64_t vidx_hi = (uint64_t) _mm_cvtsi128_si64(_mm_unpackhi_epi64(vidx, vidx)); const __m128i vl0 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo))); const __m128i vl2 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi))); const __m128i vl1 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)))); const __m128i vl3 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)))); #else const uint32_t vidx0 = (uint32_t) _mm_cvtsi128_si32(vidx); const uint32_t vidx1 = (uint32_t) _mm_extract_epi16(vidx, 2); const uint32_t vidx2 = (uint32_t) _mm_extract_epi16(vidx, 4); const uint32_t vidx3 = (uint32_t) _mm_extract_epi16(vidx, 6); const __m128i vl0 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx0))); const __m128i vl2 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx2))); const __m128i vl1 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx1))); const __m128i vl3 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx3))); #endif const __m128i vl = _mm_unpacklo_epi64(_mm_unpacklo_epi32(vl0, vl1), _mm_unpacklo_epi32(vl2, vl3)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m128 vs = _mm_castsi128_ps(_mm_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm_sub_ps(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. __m128 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_lo), vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m128 vp = _mm_mul_ps(vt, vc2); vp = _mm_add_ps(vt, _mm_mul_ps(vp, vt)); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m128 vy = _mm_add_ps(vs, _mm_mul_ps(vs, vp)); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m128 vd = _mm_add_ps(vy, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m128 vf = _mm_div_ps(vy, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) const __m128 vm = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx))); vf = _mm_or_ps(_mm_and_ps(vm, vf), _mm_andnot_ps(vm, _mm_sub_ps(vone, vf))); _mm_store_ps(output, vf); output += 4; } }
6,908
50.177778
132
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-sse2-rr2-lut64-p2-nr1.c
// Copyright 2020 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 <emmintrin.h> #include <xnnpack/common.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 float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__sse2_rr2_lut64_p2_nr1( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m128 vsign_mask = _mm_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m128 vmagic_bias = _mm_set1_ps(0x1.800000p17f); const __m128 vlog2e = _mm_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m128i vindex_mask = _mm_set1_epi32(INT32_C(0x3F)); // Last 13 bits are zeroes const __m128 vminus_ln2_hi = _mm_set1_ps(-0x1.630000p-1f); const __m128 vminus_ln2_lo = _mm_set1_ps(0x1.BD0106p-13f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m128 vc2 = _mm_set1_ps(0x1.FFFF0Ap-2f); const __m128 vone = _mm_set1_ps(1.0f); const __m128 vtwo = _mm_set1_ps(2.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m128 vdenorm_cutoff = _mm_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m128 vz = _mm_or_ps(vx, vsign_mask); // 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 [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) 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 floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const __m128i ve = _mm_slli_epi32(_mm_castps_si128(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m128i vidx = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn), vindex_mask), 2); #if XNN_ARCH_X86_64 const uint64_t vidx_lo = (uint64_t) _mm_cvtsi128_si64(vidx); const uint64_t vidx_hi = (uint64_t) _mm_cvtsi128_si64(_mm_unpackhi_epi64(vidx, vidx)); const __m128i vl0 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo))); const __m128i vl2 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi))); const __m128i vl1 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)))); const __m128i vl3 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)))); #else const uint32_t vidx0 = (uint32_t) _mm_cvtsi128_si32(vidx); const uint32_t vidx1 = (uint32_t) _mm_extract_epi16(vidx, 2); const uint32_t vidx2 = (uint32_t) _mm_extract_epi16(vidx, 4); const uint32_t vidx3 = (uint32_t) _mm_extract_epi16(vidx, 6); const __m128i vl0 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx0))); const __m128i vl2 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx2))); const __m128i vl1 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx1))); const __m128i vl3 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx3))); #endif const __m128i vl = _mm_unpacklo_epi64(_mm_unpacklo_epi32(vl0, vl1), _mm_unpacklo_epi32(vl2, vl3)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m128 vs = _mm_castsi128_ps(_mm_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm_sub_ps(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. __m128 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_lo), vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m128 vp = _mm_mul_ps(vt, vc2); vp = _mm_add_ps(vt, _mm_mul_ps(vp, vt)); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m128 vy = _mm_add_ps(vs, _mm_mul_ps(vs, vp)); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m128 vd = _mm_add_ps(vy, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m128 vr = _mm_rcp_ps(vd); vr = _mm_mul_ps(vr, _mm_sub_ps(vtwo, _mm_mul_ps(vr, vd))); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m128 vf = _mm_mul_ps(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) const __m128 vm = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx))); vf = _mm_or_ps(_mm_and_ps(vm, vf), _mm_andnot_ps(vm, _mm_sub_ps(vone, vf))); _mm_store_ps(output, vf); output += 4; } }
7,259
50.126761
132
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-sse2-rr2-lut64-p2-nr2.c
// Copyright 2020 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 <emmintrin.h> #include <xnnpack/common.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 float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__sse2_rr2_lut64_p2_nr2( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m128 vsign_mask = _mm_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m128 vmagic_bias = _mm_set1_ps(0x1.800000p17f); const __m128 vlog2e = _mm_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m128i vindex_mask = _mm_set1_epi32(INT32_C(0x3F)); // Last 13 bits are zeroes const __m128 vminus_ln2_hi = _mm_set1_ps(-0x1.630000p-1f); const __m128 vminus_ln2_lo = _mm_set1_ps(0x1.BD0106p-13f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m128 vc2 = _mm_set1_ps(0x1.FFFF0Ap-2f); const __m128 vone = _mm_set1_ps(1.0f); const __m128 vminus_two = _mm_set1_ps(-2.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m128 vdenorm_cutoff = _mm_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m128 vz = _mm_or_ps(vx, vsign_mask); // 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 [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) 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 floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const __m128i ve = _mm_slli_epi32(_mm_castps_si128(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m128i vidx = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn), vindex_mask), 2); #if XNN_ARCH_X86_64 const uint64_t vidx_lo = (uint64_t) _mm_cvtsi128_si64(vidx); const uint64_t vidx_hi = (uint64_t) _mm_cvtsi128_si64(_mm_unpackhi_epi64(vidx, vidx)); const __m128i vl0 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo))); const __m128i vl2 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi))); const __m128i vl1 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)))); const __m128i vl3 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)))); #else const uint32_t vidx0 = (uint32_t) _mm_cvtsi128_si32(vidx); const uint32_t vidx1 = (uint32_t) _mm_extract_epi16(vidx, 2); const uint32_t vidx2 = (uint32_t) _mm_extract_epi16(vidx, 4); const uint32_t vidx3 = (uint32_t) _mm_extract_epi16(vidx, 6); const __m128i vl0 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx0))); const __m128i vl2 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx2))); const __m128i vl1 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx1))); const __m128i vl3 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx3))); #endif const __m128i vl = _mm_unpacklo_epi64(_mm_unpacklo_epi32(vl0, vl1), _mm_unpacklo_epi32(vl2, vl3)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m128 vs = _mm_castsi128_ps(_mm_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm_sub_ps(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. __m128 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_lo), vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m128 vp = _mm_mul_ps(vt, vc2); vp = _mm_add_ps(vt, _mm_mul_ps(vp, vt)); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m128 vy = _mm_add_ps(vs, _mm_mul_ps(vs, vp)); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m128 vd = _mm_add_ps(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m128 vr = _mm_rcp_ps(vd); vr = _mm_mul_ps(vr, _mm_add_ps(_mm_mul_ps(vr, vd), vminus_two)); vr = _mm_mul_ps(vr, _mm_sub_ps(vminus_two, _mm_mul_ps(vr, vd))); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m128 vf = _mm_mul_ps(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) const __m128 vm = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx))); vf = _mm_or_ps(_mm_and_ps(vm, vf), _mm_andnot_ps(vm, _mm_sub_ps(vone, vf))); _mm_store_ps(output, vf); output += 4; } }
7,342
50.34965
132
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-sse2-rr2-p5-div.c
// Copyright 2019 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 <emmintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__sse2_rr2_p5_div( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m128 vsign_mask = _mm_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m128 vmagic_bias = _mm_set1_ps(0x1.8000FEp23f); const __m128 vlog2e = _mm_set1_ps(0x1.715476p0f); // Last 7 bits are zeroes const __m128 vminus_ln2_hi = _mm_set1_ps(-0x1.62E400p-1f); const __m128 vminus_ln2_lo = _mm_set1_ps(-0x1.7F7D1Cp-20f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m128 vc5 = _mm_set1_ps(0x1.0F9F9Cp-7f); const __m128 vc4 = _mm_set1_ps(0x1.573A1Ap-5f); const __m128 vc3 = _mm_set1_ps(0x1.555A80p-3f); const __m128 vc2 = _mm_set1_ps(0x1.FFFDC6p-2f); const __m128 vc1 = _mm_set1_ps(0x1.FFFFF6p-1f); const __m128 vone = _mm_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m128 vdenorm_cutoff = _mm_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m128 vz = _mm_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside // of [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the // result for such inputs at the very end of the algorithm. __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m128 vs = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn), 23)); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm_sub_ps(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. __m128 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_lo), vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m128 vp = _mm_add_ps(_mm_mul_ps(vc5, vt), vc4); vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc3); vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc2); vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm_mul_ps(vt, vs); __m128 ve = _mm_add_ps(_mm_mul_ps(vt, vp), vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) __m128 vd = _mm_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m128 vf = _mm_div_ps(ve, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) __m128 vm = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx))); vf = _mm_or_ps(_mm_and_ps(vf, vm), _mm_andnot_ps(vm, _mm_sub_ps(vone, vf))); _mm_storeu_ps(output, vf); input += 4; output += 4; } }
4,607
42.065421
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-sse2-rr2-p5-nr1.c
// Copyright 2020 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 <emmintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__sse2_rr2_p5_nr1( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m128 vsign_mask = _mm_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m128 vmagic_bias = _mm_set1_ps(0x1.8000FEp23f); const __m128 vlog2e = _mm_set1_ps(0x1.715476p0f); // Last 7 bits are zeroes const __m128 vminus_ln2_hi = _mm_set1_ps(-0x1.62E400p-1f); const __m128 vminus_ln2_lo = _mm_set1_ps(-0x1.7F7D1Cp-20f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m128 vc5 = _mm_set1_ps(0x1.0F9F9Cp-7f); const __m128 vc4 = _mm_set1_ps(0x1.573A1Ap-5f); const __m128 vc3 = _mm_set1_ps(0x1.555A80p-3f); const __m128 vc2 = _mm_set1_ps(0x1.FFFDC6p-2f); const __m128 vc1 = _mm_set1_ps(0x1.FFFFF6p-1f); const __m128 vone = _mm_set1_ps(1.0f); const __m128 vtwo = _mm_set1_ps(2.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m128 vdenorm_cutoff = _mm_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m128 vz = _mm_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside // of [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the // result for such inputs at the very end of the algorithm. __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m128 vs = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn), 23)); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm_sub_ps(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. __m128 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_lo), vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m128 vp = _mm_add_ps(_mm_mul_ps(vc5, vt), vc4); vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc3); vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc2); vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm_mul_ps(vt, vs); __m128 ve = _mm_add_ps(_mm_mul_ps(vt, vp), vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) __m128 vd = _mm_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m128 vr = _mm_rcp_ps(vd); vr = _mm_mul_ps(vr, _mm_sub_ps(vtwo, _mm_mul_ps(vr, vd))); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m128 vf = _mm_mul_ps(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) __m128 vm = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx))); vf = _mm_or_ps(_mm_and_ps(vf, vm), _mm_andnot_ps(vm, _mm_sub_ps(vone, vf))); _mm_storeu_ps(output, vf); input += 4; output += 4; } }
4,958
42.5
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-sse2-rr2-p5-nr2.c
// Copyright 2020 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 <emmintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__sse2_rr2_p5_nr2( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m128 vsign_mask = _mm_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m128 vmagic_bias = _mm_set1_ps(0x1.8000FEp23f); const __m128 vlog2e = _mm_set1_ps(0x1.715476p0f); // Last 7 bits are zeroes const __m128 vminus_ln2_hi = _mm_set1_ps(-0x1.62E400p-1f); const __m128 vminus_ln2_lo = _mm_set1_ps(-0x1.7F7D1Cp-20f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m128 vc5 = _mm_set1_ps(0x1.0F9F9Cp-7f); const __m128 vc4 = _mm_set1_ps(0x1.573A1Ap-5f); const __m128 vc3 = _mm_set1_ps(0x1.555A80p-3f); const __m128 vc2 = _mm_set1_ps(0x1.FFFDC6p-2f); const __m128 vc1 = _mm_set1_ps(0x1.FFFFF6p-1f); const __m128 vone = _mm_set1_ps(1.0f); const __m128 vminus_two = _mm_set1_ps(-2.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m128 vdenorm_cutoff = _mm_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m128 vz = _mm_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside // of [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the // result for such inputs at the very end of the algorithm. __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m128 vs = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn), 23)); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm_sub_ps(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. __m128 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_lo), vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m128 vp = _mm_add_ps(_mm_mul_ps(vc5, vt), vc4); vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc3); vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc2); vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm_mul_ps(vt, vs); __m128 ve = _mm_add_ps(_mm_mul_ps(vt, vp), vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) __m128 vd = _mm_add_ps(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m128 vr = _mm_rcp_ps(vd); vr = _mm_mul_ps(vr, _mm_add_ps(_mm_mul_ps(vr, vd), vminus_two)); vr = _mm_mul_ps(vr, _mm_sub_ps(vminus_two, _mm_mul_ps(vr, vd))); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m128 vf = _mm_mul_ps(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) __m128 vm = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx))); vf = _mm_or_ps(_mm_and_ps(vf, vm), _mm_andnot_ps(vm, _mm_sub_ps(vone, vf))); _mm_storeu_ps(output, vf); input += 4; output += 4; } }
5,041
42.843478
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-wasmsimd-rr2-lut64-p2-div.c
// Copyright 2020 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 <wasm_simd128.h> #include <xnnpack/common.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 float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__wasmsimd_rr2_lut64_p2_div( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-6) const v128_t vmagic_bias = wasm_f32x4_const_splat(0x1.800000p17f); const v128_t vminus_log2e = wasm_f32x4_const_splat(-0x1.715476p0f); // Mask for the lowest 6 bits const v128_t vindex_mask = wasm_i32x4_const_splat(INT32_C(0x3F)); // Last 13 bits are zeroes const v128_t vln2_hi = wasm_f32x4_const_splat(0x1.630000p-1f); const v128_t vln2_lo = wasm_f32x4_const_splat(-0x1.BD0106p-13f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const v128_t vc2 = wasm_f32x4_const_splat(0x1.FFFF0Ap-2f); const v128_t vone = wasm_f32x4_const_splat(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const v128_t vdenorm_cutoff = wasm_f32x4_const_splat(0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const v128_t vx = wasm_v128_load(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const v128_t vz = wasm_f32x4_abs(vx); // 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 integer, 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 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. v128_t vn = wasm_f32x4_add(vmagic_bias, wasm_f32x4_mul(vz, vminus_log2e)); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) 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 floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const v128_t ve = wasm_i32x4_shl(vn, 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const v128_t vidx = wasm_i32x4_shl(wasm_v128_and(vn, vindex_mask), 2); const uint32_t vidx0 = wasm_u32x4_extract_lane(vidx, 0); v128_t vl = wasm_v128_load32_zero((const void*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx0)); const uint32_t vidx1 = wasm_u32x4_extract_lane(vidx, 1); vl = wasm_v128_load32_lane((const void*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx1), vl, 1); const uint32_t vidx2 = wasm_u32x4_extract_lane(vidx, 2); vl = wasm_v128_load32_lane((const void*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx2), vl, 2); const uint32_t vidx3 = wasm_u32x4_extract_lane(vidx, 3); vl = wasm_v128_load32_lane((const void*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx3), vl, 3); // Adjust exponent of the value l fetched from the table to get the final s value. const v128_t vs = wasm_i32x4_add(vl, ve); // Subtract the large number back to get the final n := round(-z / log(2), 6) as a floating-point number. vn = wasm_f32x4_sub(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. v128_t vt = wasm_f32x4_add(vz, wasm_f32x4_mul(vn, vln2_hi)); vt = wasm_f32x4_add(vt, wasm_f32x4_mul(vn, vln2_lo)); // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p v128_t vp = wasm_f32x4_mul(vt, vc2); vp = wasm_f32x4_sub(vt, wasm_f32x4_mul(vp, vt)); // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const v128_t vy = wasm_f32x4_sub(vs, wasm_f32x4_mul(vs, vp)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) v128_t vf = wasm_f32x4_div(vy, wasm_f32x4_add(vy, vone)); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = wasm_v128_andnot(vf, wasm_f32x4_gt(vz, vdenorm_cutoff)); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) vf = wasm_v128_bitselect(vf, wasm_f32x4_sub(vone, vf), wasm_i32x4_shr(vx, 31)); wasm_v128_store(output, vf); output += 4; } }
5,949
48.173554
118
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-wasmsimd-rr2-p5-div.c
// Copyright 2020 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 <wasm_simd128.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__wasmsimd_rr2_p5_div( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const v128_t vmagic_bias = wasm_f32x4_const_splat(0x1.8000FEp23f); const v128_t vminus_log2e = wasm_f32x4_const_splat(-0x1.715476p+0f); // Last 7 bits are zeroes const v128_t vln2_hi = wasm_f32x4_const_splat(0x1.62E400p-1f); const v128_t vln2_lo = wasm_f32x4_const_splat(0x1.7F7D1Cp-20f); // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const v128_t vc5 = wasm_f32x4_const_splat(-0x1.0F9F9Cp-7f); const v128_t vc4 = wasm_f32x4_const_splat( 0x1.573A1Ap-5f); const v128_t vc3 = wasm_f32x4_const_splat(-0x1.555A80p-3f); const v128_t vc2 = wasm_f32x4_const_splat( 0x1.FFFDC6p-2f); const v128_t vc1 = wasm_f32x4_const_splat(-0x1.FFFFF6p-1f); const v128_t vone = wasm_f32x4_const_splat(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const v128_t vdenorm_cutoff = wasm_f32x4_const_splat(0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const v128_t vx = wasm_v128_load(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const v128_t vz = wasm_f32x4_abs(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. v128_t vn = wasm_f32x4_add(vmagic_bias, wasm_f32x4_mul(vz, vminus_log2e)); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly. const v128_t vs = wasm_i32x4_shl(vn, 23); // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number. vn = wasm_f32x4_sub(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. v128_t vt = wasm_f32x4_add(vz, wasm_f32x4_mul(vn, vln2_hi)); vt = wasm_f32x4_add(vt, wasm_f32x4_mul(vn, vln2_lo)); // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p v128_t vp = wasm_f32x4_add(vc4, wasm_f32x4_mul(vt, vc5)); vp = wasm_f32x4_add(vc3, wasm_f32x4_mul(vt, vp)); vp = wasm_f32x4_add(vc2, wasm_f32x4_mul(vt, vp)); vp = wasm_f32x4_add(vc1, wasm_f32x4_mul(vt, vp)); // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s * (1 + t * p) // = s + (t * s) * p vt = wasm_f32x4_mul(vt, vs); const v128_t ve = wasm_f32x4_add(vs, wasm_f32x4_mul(vt, vp)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) v128_t vf = wasm_f32x4_div(ve, wasm_f32x4_add(ve, vone)); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = wasm_v128_andnot(vf, wasm_f32x4_gt(vz, vdenorm_cutoff)); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) vf = wasm_v128_bitselect(vf, wasm_f32x4_sub(vone, vf), wasm_i32x4_shr(vx, 31)); wasm_v128_store(output, vf); output += 4; } }
4,546
43.578431
117
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-avx512f-nr1fma.c
// Copyright 2020 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 <immintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__avx512f_nr1fma( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); const __m512 vhalf = _mm512_set1_ps(0.5f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_load_ps(input); input += 16; // Initial approximation const __m512 vrsqrtx = _mm512_rsqrt14_ps(vx); __m512 vsqrtx = _mm512_mul_ps(vrsqrtx, vx); const __m512 vhalfrsqrtx = _mm512_mul_ps(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // sqrtx <- sqrtx + sqrtx * residual const __m512 vresidual = _mm512_fnmadd_ps(vsqrtx, vhalfrsqrtx, vhalf); vsqrtx = _mm512_fmadd_ps(vsqrtx, vresidual, vsqrtx); const __m512 vy = vsqrtx; _mm512_store_ps(output, vy); output += 16; } }
1,123
24.545455
74
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-avx512f-nr1fma1adj.c
// Copyright 2020 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 <immintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__avx512f_nr1fma1adj( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); const __m512 vhalf = _mm512_set1_ps(0.5f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_load_ps(input); input += 16; // Initial approximation const __m512 vrsqrtx = _mm512_rsqrt14_ps(vx); __m512 vsqrtx = _mm512_mul_ps(vrsqrtx, vx); __m512 vhalfrsqrtx = _mm512_mul_ps(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // halfrsqrtx <- halfrsqrtx + halfrsqrtx * residual // sqrtx <- sqrtx + sqrtx * residual const __m512 vresidual = _mm512_fnmadd_ps(vsqrtx, vhalfrsqrtx, vhalf); vhalfrsqrtx = _mm512_fmadd_ps(vhalfrsqrtx, vresidual, vhalfrsqrtx); vsqrtx = _mm512_fmadd_ps(vsqrtx, vresidual, vsqrtx); // Final adjustment: // adjustment <- x - sqrtx * sqrtx // sqrtx <- sqrtx + halfrsqrtx * adjustment const __m512 vadjustment = _mm512_fnmadd_ps(vsqrtx, vsqrtx, vx); vsqrtx = _mm512_fmadd_ps(vhalfrsqrtx, vadjustment, vsqrtx); const __m512 vy = vsqrtx; _mm512_store_ps(output, vy); output += 16; } }
1,506
27.980769
74
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-avx512f-nr2fma.c
// Copyright 2020 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 <immintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__avx512f_nr2fma( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); const __m512 vhalf = _mm512_set1_ps(0.5f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_load_ps(input); input += 16; // Initial approximation const __m512 vrsqrtx = _mm512_rsqrt14_ps(vx); __m512 vsqrtx = _mm512_mul_ps(vrsqrtx, vx); __m512 vhalfrsqrtx = _mm512_mul_ps(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // halfrsqrtx <- halfrsqrtx + halfrsqrtx * residual // sqrtx <- sqrtx + sqrtx * residual __m512 vresidual = _mm512_fnmadd_ps(vsqrtx, vhalfrsqrtx, vhalf); vhalfrsqrtx = _mm512_fmadd_ps(vhalfrsqrtx, vresidual, vhalfrsqrtx); vsqrtx = _mm512_fmadd_ps(vsqrtx, vresidual, vsqrtx); vresidual = _mm512_fnmadd_ps(vsqrtx, vhalfrsqrtx, vhalf); vsqrtx = _mm512_fmadd_ps(vsqrtx, vresidual, vsqrtx); const __m512 vy = vsqrtx; _mm512_store_ps(output, vy); output += 16; } }
1,361
26.795918
72
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-fma3-nr1fma.c
// Copyright 2020 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 <immintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__fma3_nr1fma( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); const __m256 vhalf = _mm256_set1_ps(0.5f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_load_ps(input); input += 8; // Initial approximation const __m256 vrsqrtx = _mm256_rsqrt_ps(vx); __m256 vsqrtx = _mm256_mul_ps(vrsqrtx, vx); const __m256 vhalfrsqrtx = _mm256_mul_ps(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // sqrtx <- sqrtx + sqrtx * residual const __m256 vresidual = _mm256_fnmadd_ps(vsqrtx, vhalfrsqrtx, vhalf); vsqrtx = _mm256_fmadd_ps(vsqrtx, vresidual, vsqrtx); const __m256 vy = vsqrtx; _mm256_store_ps(output, vy); output += 8; } }
1,114
24.340909
74
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-fma3-nr1fma1adj.c
// Copyright 2020 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 <immintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__fma3_nr1fma1adj( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); const __m256 vhalf = _mm256_set1_ps(0.5f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_load_ps(input); input += 8; // Initial approximation const __m256 vrsqrtx = _mm256_rsqrt_ps(vx); __m256 vsqrtx = _mm256_mul_ps(vrsqrtx, vx); __m256 vhalfrsqrtx = _mm256_mul_ps(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // halfrsqrtx <- halfrsqrtx + halfrsqrtx * residual // sqrtx <- sqrtx + sqrtx * residual const __m256 vresidual = _mm256_fnmadd_ps(vsqrtx, vhalfrsqrtx, vhalf); vhalfrsqrtx = _mm256_fmadd_ps(vhalfrsqrtx, vresidual, vhalfrsqrtx); vsqrtx = _mm256_fmadd_ps(vsqrtx, vresidual, vsqrtx); // Final adjustment: // adjustment <- x - sqrtx * sqrtx // sqrtx <- sqrtx + halfrsqrtx * adjustment const __m256 vadjustment = _mm256_fnmadd_ps(vsqrtx, vsqrtx, vx); vsqrtx = _mm256_fmadd_ps(vhalfrsqrtx, vadjustment, vsqrtx); const __m256 vy = vsqrtx; _mm256_store_ps(output, vy); output += 8; } }
1,497
27.807692
74
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-fma3-nr2fma.c
// Copyright 2020 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 <immintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__fma3_nr2fma( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); const __m256 vhalf = _mm256_set1_ps(0.5f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_load_ps(input); input += 8; // Initial approximation const __m256 vrsqrtx = _mm256_rsqrt_ps(vx); __m256 vsqrtx = _mm256_mul_ps(vrsqrtx, vx); __m256 vhalfrsqrtx = _mm256_mul_ps(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // halfrsqrtx <- halfrsqrtx + halfrsqrtx * residual // sqrtx <- sqrtx + sqrtx * residual __m256 vresidual = _mm256_fnmadd_ps(vsqrtx, vhalfrsqrtx, vhalf); vhalfrsqrtx = _mm256_fmadd_ps(vhalfrsqrtx, vresidual, vhalfrsqrtx); vsqrtx = _mm256_fmadd_ps(vsqrtx, vresidual, vsqrtx); vresidual = _mm256_fnmadd_ps(vsqrtx, vhalfrsqrtx, vhalf); vsqrtx = _mm256_fmadd_ps(vsqrtx, vresidual, vsqrtx); const __m256 vy = vsqrtx; _mm256_store_ps(output, vy); output += 8; } }
1,352
26.612245
72
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-neon-nr1rsqrts.c
// Copyright 2020 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 <arm_neon.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__neon_nr1rsqrts( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Initial approximation float32x4_t vrsqrtx = vrsqrteq_f32(vx); // Netwon-Raphson iteration: rsqrt_x <- rsqrt_x * ((3 - x * (rsqrt_x * rsqrt_x)) / 2) // Note: vrsqrtsq_f32(x, y) := (3 - x * y) / 2 vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vx, vmulq_f32(vrsqrtx, vrsqrtx))); // Reconstruct sqrt(x) = rsqrt(x) * x const float32x4_t vy = vmulq_f32(vrsqrtx, vx); vst1q_f32(output, vy); output += 4; } }
967
24.473684
89
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-neon-nr2rsqrts.c
// Copyright 2020 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 <arm_neon.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__neon_nr2rsqrts( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Initial approximation float32x4_t vrsqrtx = vrsqrteq_f32(vx); // Netwon-Raphson iteration: rsqrt_x <- rsqrt_x * ((3 - x * (rsqrt_x * rsqrt_x)) / 2) // Note: x * (rsqrt_x * rsqrt_x) for the first iteration and (x * rsqrt_x) * rsqrt_x for the second improves accuracy // Note: vrsqrtsq_f32(x, y) := (3 - x * y) / 2 vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vx, vmulq_f32(vrsqrtx, vrsqrtx))); vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vmulq_f32(vrsqrtx, vx), vrsqrtx)); // Reconstruct sqrt(x) = rsqrt(x) * x const float32x4_t vy = vmulq_f32(vrsqrtx, vx); vst1q_f32(output, vy); output += 4; } }
1,170
28.275
121
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-neon-nr3rsqrts.c
// Copyright 2020 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 <arm_neon.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__neon_nr3rsqrts( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Initial approximation float32x4_t vrsqrtx = vrsqrteq_f32(vx); // Netwon-Raphson iteration: rsqrt_x <- rsqrt_x * ((3 - x * rsqrt_x * rsqrt_x) / 2) // Note: x * (rsqrt_x * rsqrt_x) for the first iteration and (x * rsqrt_x) * rsqrt_x for the next two improves accuracy // Note: vrsqrtsq_f32(x, y) := (3 - x * y) / 2 vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vx, vmulq_f32(vrsqrtx, vrsqrtx))); vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vmulq_f32(vrsqrtx, vx), vrsqrtx)); vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vmulq_f32(vrsqrtx, vx), vrsqrtx)); // Reconstruct sqrt(x) = rsqrt(x) * x const float32x4_t vy = vmulq_f32(vrsqrtx, vx); vst1q_f32(output, vy); output += 4; } }
1,251
29.536585
123
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-neonfma-nr1fma.c
// Copyright 2020 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 <arm_neon.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__neonfma_nr1fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); const float32x4_t vhalf = vmovq_n_f32(0.5f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Initial approximation const float32x4_t vrsqrtx = vrsqrteq_f32(vx); float32x4_t vsqrtx = vmulq_f32(vrsqrtx, vx); const float32x4_t vhalfrsqrtx = vmulq_f32(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // sqrtx <- sqrtx + sqrtx * residual const float32x4_t vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx); const float32x4_t vy = vsqrtx; vst1q_f32(output, vy); output += 4; } }
1,105
25.333333
72
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-neonfma-nr1rsqrts1fma1adj.c
// Copyright 2020 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 <arm_neon.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__neonfma_nr1rsqrts1fma1adj( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); const float32x4_t vhalf = vmovq_n_f32(0.5f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Initial approximation float32x4_t vrsqrtx = vrsqrteq_f32(vx); // Netwon-Raphson iteration: rsqrt_x <- rsqrt_x * ((3 - x * (rsqrt_x * rsqrt_x)) / 2) // Note: vrsqrtsq_f32(x, y) := (3 - x * y) / 2 vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vx, vmulq_f32(vrsqrtx, vrsqrtx))); float32x4_t vsqrtx = vmulq_f32(vrsqrtx, vx); float32x4_t vhalfrsqrtx = vmulq_f32(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // halfrsqrtx <- halfrsqrtx + halfrsqrtx * residual // sqrtx <- sqrtx + sqrtx * residual float32x4_t vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx); vhalfrsqrtx = vfmaq_f32(vhalfrsqrtx, vresidual, vhalfrsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx); // Final adjustment: // adjustment <- x - sqrtx * sqrtx // sqrtx <- sqrtx + halfrsqrtx * adjustment const float32x4_t vadjustment = vfmsq_f32(vx, vsqrtx, vsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vhalfrsqrtx, vadjustment); const float32x4_t vy = vsqrtx; vst1q_f32(output, vy); output += 4; } }
1,693
29.8
89
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-neonfma-nr2fma.c
// Copyright 2020 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 <arm_neon.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__neonfma_nr2fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); const float32x4_t vhalf = vmovq_n_f32(0.5f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Initial approximation const float32x4_t vrsqrtx = vrsqrteq_f32(vx); float32x4_t vsqrtx = vmulq_f32(vrsqrtx, vx); float32x4_t vhalfrsqrtx = vmulq_f32(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // halfrsqrtx <- halfrsqrtx + halfrsqrtx * residual // sqrtx <- sqrtx + sqrtx * residual float32x4_t vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx); vhalfrsqrtx = vfmaq_f32(vhalfrsqrtx, vresidual, vhalfrsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx); vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx); const float32x4_t vy = vsqrtx; vst1q_f32(output, vy); output += 4; } }
1,324
27.191489
72
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-neonfma-nr2fma1adj.c
// Copyright 2020 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 <arm_neon.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__neonfma_nr2fma1adj( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); const float32x4_t vhalf = vmovq_n_f32(0.5f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Initial approximation const float32x4_t vrsqrtx = vrsqrteq_f32(vx); float32x4_t vsqrtx = vmulq_f32(vrsqrtx, vx); float32x4_t vhalfrsqrtx = vmulq_f32(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // halfrsqrtx <- halfrsqrtx + halfrsqrtx * residual // sqrtx <- sqrtx + sqrtx * residual float32x4_t vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx); vhalfrsqrtx = vfmaq_f32(vhalfrsqrtx, vresidual, vhalfrsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx); vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx); vhalfrsqrtx = vfmaq_f32(vhalfrsqrtx, vresidual, vhalfrsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx); // Final adjustment: // adjustment <- x - sqrtx * sqrtx // sqrtx <- sqrtx + halfrsqrtx * adjustment const float32x4_t vadjustment = vfmsq_f32(vx, vsqrtx, vsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vhalfrsqrtx, vadjustment); const float32x4_t vy = vsqrtx; vst1q_f32(output, vy); output += 4; } }
1,641
29.407407
72
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-neonfma-nr3fma.c
// Copyright 2020 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 <arm_neon.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__neonfma_nr3fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); const float32x4_t vhalf = vmovq_n_f32(0.5f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Initial approximation const float32x4_t vrsqrtx = vrsqrteq_f32(vx); float32x4_t vsqrtx = vmulq_f32(vrsqrtx, vx); float32x4_t vhalfrsqrtx = vmulq_f32(vrsqrtx, vhalf); // Netwon-Raphson iteration: // residual <- 0.5 - sqrtx * halfrsqrtx // halfrsqrtx <- halfrsqrtx + halfrsqrtx * residual // sqrtx <- sqrtx + sqrtx * residual float32x4_t vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx); vhalfrsqrtx = vfmaq_f32(vhalfrsqrtx, vresidual, vhalfrsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx); vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx); vhalfrsqrtx = vfmaq_f32(vhalfrsqrtx, vresidual, vhalfrsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx); vresidual = vfmsq_f32(vhalf, vsqrtx, vhalfrsqrtx); vsqrtx = vfmaq_f32(vsqrtx, vresidual, vsqrtx); const float32x4_t vy = vsqrtx; vst1q_f32(output, vy); output += 4; } }
1,497
28.372549
72
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-sse-hh1mac.c
// Copyright 2020 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 <xmmintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__sse_hh1mac( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); const __m128 vc1875 = _mm_set1_ps(1.875f); const __m128 vc0375 = _mm_set1_ps(0.375f); const __m128 vc1250 = _mm_set1_ps(1.250f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // Initial approximation __m128 vrsqrtx = _mm_rsqrt_ps(vx); // Householder (order 2) iteration: // rsqrt_x <- rsqrt_x * (1.875 + t * (0.375 * t - 1.25)) where t = x * rsqrt_x * rsqrt_x // Note: half_x * (rsqrt_x * rsqrt_x) is less accurate than (half_x * rsqrt_x) * rsqrt_x const __m128 vt = _mm_mul_ps(_mm_mul_ps(vx, vrsqrtx), vrsqrtx); vrsqrtx = _mm_mul_ps(vrsqrtx, _mm_add_ps(_mm_mul_ps(vt, _mm_sub_ps(_mm_mul_ps(vt, vc0375), vc1250)), vc1875)); // Reconstruct sqrt(x) = rsqrt(x) * x const __m128 vy = _mm_mul_ps(vrsqrtx, vx); _mm_store_ps(output, vy); output += 4; } }
1,287
27.622222
114
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-sse-nr1mac.c
// Copyright 2020 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 <xmmintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__sse_nr1mac( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); const __m128 vthree_halfs = _mm_set1_ps(1.5f); const __m128 vhalf = _mm_set1_ps(0.5f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // Initial approximation __m128 vrsqrtx = _mm_rsqrt_ps(vx); const __m128 vhalfx = _mm_mul_ps(vx, vhalf); // Netwon-Raphson iteration: rsqrt_x <- rsqrt_x * (3/2 - x/2 * rsqrt_x * rsqrt_x) // Note: (half_x * rsqrt_x) * rsqrt_x is less accurate than half_x * (rsqrt_x * rsqrt_x) vrsqrtx = _mm_mul_ps(vrsqrtx, _mm_sub_ps(vthree_halfs, _mm_mul_ps(vhalfx, _mm_mul_ps(vrsqrtx, vrsqrtx)))); // Reconstruct sqrt(x) = rsqrt(x) * x const __m128 vy = _mm_mul_ps(vrsqrtx, vx); _mm_store_ps(output, vy); output += 4; } }
1,171
26.255814
110
c
XNNPACK
XNNPACK-master/src/math/f32-sqrt-sse-nr2mac.c
// Copyright 2020 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 <xmmintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sqrt__sse_nr2mac( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); const __m128 vthree_halfs = _mm_set1_ps(1.5f); const __m128 vhalf = _mm_set1_ps(0.5f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // Initial approximation __m128 vrsqrtx = _mm_rsqrt_ps(vx); const __m128 vhalfx = _mm_mul_ps(vx, vhalf); // Netwon-Raphson iteration: rsqrt_x <- rsqrt_x * (3/2 - x/2 * rsqrt_x * rsqrt_x) // Note: half_x * (rsqrt_x * rsqrt_x) is less accurate than (half_x * rsqrt_x) * rsqrt_x vrsqrtx = _mm_mul_ps(vrsqrtx, _mm_sub_ps(_mm_mul_ps(_mm_mul_ps(vhalfx, vrsqrtx), vrsqrtx), vthree_halfs)); vrsqrtx = _mm_mul_ps(vrsqrtx, _mm_sub_ps(_mm_mul_ps(_mm_mul_ps(vhalfx, vrsqrtx), vrsqrtx), vthree_halfs)); // Reconstruct sqrt(x) = rsqrt(x) * x const __m128 vy = _mm_mul_ps(vrsqrtx, vx); _mm_store_ps(output, vy); output += 4; } }
1,282
28.159091
110
c
XNNPACK
XNNPACK-master/src/math/u32-sqrt-scalar-bitmanip.c
// Copyright 2022 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 <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_u32_sqrt__scalar_bitmanip( size_t n, const uint32_t* input, uint32_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint32_t)) { uint32_t vx = *input++; // Based on Hacker's Delight, Figure 11-4. uint32_t vm = UINT32_C(0x40000000); uint32_t vy = 0; for (uint32_t i = 0; i < 16; i++) { const uint32_t vb = vy | vm; vy >>= 1; if XNN_UNPREDICTABLE(vx >= vb) { vx -= vb; vy |= vm; } vm >>= 2; } // vy is sqrt(.) rounded down. Do the final rounding up if needed. if XNN_UNPREDICTABLE(vx > vy) { vy += 1; } *output++ = vy; } }
941
20.409091
72
c
XNNPACK
XNNPACK-master/src/math/u32-sqrt-scalar-clz-binsearch.c
// Copyright 2022 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 <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_u32_sqrt__scalar_clz_binsearch( size_t n, const uint32_t* input, uint32_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint32_t)) { const uint32_t vx = *input++; // Based on Hacker's Delight, Figure 11-3. uint32_t vb = (UINT32_C(1) << ((33 - math_clz_u32(vx)) / 2)) - 1; uint32_t va = (vb + 3) / 2; do { const uint32_t vm = (va + vb) >> 1; assert(vm <= UINT32_C(65535)); if XNN_UNPREDICTABLE(vm * vm > vx) { vb = vm - 1; } else { va = vm + 1; } } while XNN_LIKELY(vb >= va); uint32_t vy = va - 1; // vy is sqrt(vx) rounded down. Do the final rounding up if needed. if XNN_UNPREDICTABLE(va * vy < vx) { vy += 1; } *output++ = vy; } }
1,059
22.555556
72
c
XNNPACK
XNNPACK-master/src/math/u32-sqrt-scalar-clz-newton.c
// Copyright 2022 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 <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_u32_sqrt__scalar_clz_newton( size_t n, const uint32_t* input, uint32_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint32_t)) { const uint32_t vx = *input++; uint32_t vy = vx; // Based on Hacker's Delight, Figure 11-1. if (vx != 0) { const uint32_t vs = 16 - (math_clz_nonzero_u32(vx - 1) >> 1); uint32_t vg0 = UINT32_C(1) << vs; uint32_t vg1 = (vg0 + (vx >> vs)) >> 1; while XNN_LIKELY(vg1 < vg0) { vg0 = vg1; vg1 = (vg0 + vx / vg0) >> 1; } // vg0 is sqrt(vx) rounded down. Do the final rounding up if needed. if XNN_UNPREDICTABLE(vg0 * vg0 < vx - vg0) { vg0 += 1; } vy = vg0; } *output++ = vy; } }
1,035
21.521739
74
c
XNNPACK
XNNPACK-master/src/math/u32-sqrt-scalar-cvti32-sqrt-lrint.c
// Copyright 2022 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 <math.h> #include <xnnpack/math-stubs.h> void xnn_math_u32_sqrt__scalar_cvti32_sqrt_lrint( size_t n, const uint32_t* input, uint32_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint32_t)) { const uint32_t vx = *input++; const int32_t vm = (int32_t) (vx ^ UINT32_C(0x80000000)); double vf = (double) vm + 0x1.0p+31; vf = sqrt(vf); const uint32_t vy = (uint32_t) (int32_t) lrint(vf); *output++ = vy; } }
693
21.387097
72
c
XNNPACK
XNNPACK-master/src/math/u32-sqrt-scalar-cvti64-sqrt-lrint.c
// Copyright 2022 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 <math.h> #include <xnnpack/math-stubs.h> void xnn_math_u32_sqrt__scalar_cvti64_sqrt_lrint( size_t n, const uint32_t* input, uint32_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint32_t)) { const uint32_t vx = *input++; double vf = (double) (int64_t) (uint64_t) vx; vf = sqrt(vf); const uint32_t vy = (uint32_t) (int32_t) lrint(vf); *output++ = vy; } }
640
20.366667
72
c
XNNPACK
XNNPACK-master/src/math/u32-sqrt-scalar-cvti64-sqrtf-lrintf.c
// Copyright 2022 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 <math.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> void xnn_math_u32_sqrt__scalar_cvti64_sqrtf_lrintf( size_t n, const uint32_t* input, uint32_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint32_t)) { const uint32_t vx = *input++; uint32_t vy = vx; if XNN_LIKELY(vx != 0) { float vf = (float) (double) (int64_t) (uint64_t) vx; vf = sqrtf(vf); vy = (uint32_t) (int32_t) lrintf(vf); const uint32_t vsquared_y_less_x = vy * vy - vx; if XNN_UNPREDICTABLE((int32_t) (vsquared_y_less_x + vy) < 0) { vy += 1; } else if XNN_UNPREDICTABLE((int32_t) (vsquared_y_less_x - vy) >= 0) { vy -= 1; } } *output++ = vy; } }
970
23.275
76
c
XNNPACK
XNNPACK-master/src/math/u32-sqrt-scalar-cvtu32-sqrtf-lrintf.c
// Copyright 2022 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 <math.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> void xnn_math_u32_sqrt__scalar_cvtu32_sqrtf_lrintf( size_t n, const uint32_t* input, uint32_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint32_t)) { const uint32_t vx = *input++; uint32_t vy = vx; if XNN_LIKELY(vx != 0) { float vf = (float) vx; vf = sqrtf(vf); vy = (uint32_t) (int32_t) lrintf(vf); const uint32_t vsquared_y_less_x = vy * vy - vx; if XNN_UNPREDICTABLE((int32_t) (vsquared_y_less_x + vy) < 0) { vy += 1; } else if XNN_UNPREDICTABLE((int32_t) (vsquared_y_less_x - vy) >= 0) { vy -= 1; } } *output++ = vy; } }
940
22.525
76
c
XNNPACK
XNNPACK-master/src/math/u32-sqrt-scalar-hashemian.c
// Copyright 2022 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 <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_u32_sqrt__scalar_hashemian( size_t n, const uint32_t* input, uint32_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint32_t)) { const uint32_t vx = *input++; uint32_t vy = vx; if (vx != 0) { /* * Based on "Square Rooting Algorithms for Integer and Floating-Point Numbers" by Reza Hashemian * and StackOverflow answer https://stackoverflow.com/a/31149161 */ const uint32_t vn = math_clz_nonzero_u32(vx); const uint32_t vleft_shift = vn & 1; const uint32_t vm_minus_1 = 15 - (vn >> 1); const uint32_t vm_plus_1 = vm_minus_1 + 2; const uint32_t vexp2_m_minus_1 = UINT32_C(1) << vm_minus_1; const uint32_t vz = vexp2_m_minus_1 - (vx >> (vm_plus_1 - vleft_shift)); vy = vz; // Iterate until y[i] == y[i-1]. Alternatively, we can do 7 iterations: // for (uint32_t i = 0; i < 7; i++) { // vy = vz + ((vy * vy) >> vm_plus_1); // } uint32_t vy_prev; do { vy_prev = vy; vy = vz + ((vy * vy) >> vm_plus_1); } while (vy != vy_prev); // Reconstruct Y = 2**m - vy vy = (vexp2_m_minus_1 << 1) - vy; if XNN_UNPREDICTABLE(vleft_shift) { // Multiply by sqrt(0.5) by subtracting vy * (1 - sqrt(0.5)), 1 - sqrt(0.5) is represented // as a .16 fixed-point number to guarantee than the product doesn't overflow 32 bits. // Using 1 - sqrt(0.5) under these constraints is 1 bit more accurate than using sqrt(0.5) directly. vy -= (vy * UINT32_C(19195)) >> 16; } // When X has an even number of bits, Y can overestimate isqrt(X) by 1 due to truncations in fixed-point // arithmetics. When X has an odd number of bits, Y can overestimate isqrt(X) by an extra 1 (2 total) due to // truncation in the multiplication by sqrt(0.5). // We decrement Y once if X < Y * Y and decrement it once again if Y * Y - X > X - (Y - 1) * (Y - 1). uint32_t vsquared_y = vy * vy; if XNN_UNPREDICTABLE(vsquared_y > vx) { vsquared_y -= 2 * vy - 1; vy -= 1; } // Y is within a distance of 1 from properly rounded sqrt(X). // - Increment Y if (Y + 1) * (Y + 1) - X < X - Y * Y. // - Decrement Y if Y * Y - X > X - (Y - 1) * (Y - 1). // The increment + decrement are combined together to re-use the (Y * Y) value. if XNN_UNPREDICTABLE(vsquared_y < vx - vy) { vy += 1; } else if XNN_UNPREDICTABLE(vsquared_y - vy >= vx) { vy -= 1; } } *output++ = vy; } }
2,863
34.358025
114
c
XNNPACK
XNNPACK-master/src/math/u32-sqrt-scalar-tflm.c
// Copyright 2022 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 <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_u32_sqrt__scalar_tflm( size_t n, const uint32_t* input, uint32_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint32_t)) { uint32_t vx = *input++; // Algorithm adapted from tensorflow/lite/experimental/microfrontend/lib/filterbank.c in TFLite-Micro uint32_t vy = 0; if (vx != 0) { const uint32_t vn = (math_clz_nonzero_u32(vx) | 1) ^ 31; uint32_t vb = UINT32_C(1) << vn; uint32_t iterations = (vn >> 1) + 1; while (iterations--) { const uint32_t vyb = vy + vb; if (vx >= vyb) { vx -= vyb; vy = (vy >> 1) + vb; } else { vy >>= 1; } vb >>= 2; } // vy is sqrt(.) rounded down. Do the final rounding up if needed. if (vx > vy) { // This condition prevents overflowing uint16_t, but produces incorrectly // rounded result for large inputs where square root should round to 0x10000. if (vy != UINT32_C(0xFFFF)) { vy += 1; } } } *output++ = vy; } }
1,356
24.603774
105
c
XNNPACK
XNNPACK-master/src/math/u64-sqrt-scalar-cvtu32-sqrt-cvtsatu32f64.c
// Copyright 2022 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 <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_u64_sqrt__scalar_cvtu32_sqrt_cvtsatu32f64( size_t n, const uint64_t* input, uint64_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint64_t)) { const uint64_t vx = *input++; uint64_t vy = vx; if XNN_LIKELY(vx != 0) { const uint32_t vx_lo = (uint32_t) vx; const uint32_t vx_hi = (uint32_t) (vx >> 32); const double vf_hi = (double) vx_hi; const double vf_lo = (double) vx_lo; double vf = vf_hi * 0x1.0p+32 + vf_lo; vf = sqrt(vf); vy = math_cvt_sat_u32_f64(vf); #if XNN_ARCH_ARM || XNN_ARCH_X86 const uint64_t vsquared_y_less_x = math_mulext_u32((uint32_t) vy, (uint32_t) vy) - vx; #else const uint64_t vsquared_y_less_x = vy * vy - vx; #endif if XNN_UNPREDICTABLE((int64_t) (vsquared_y_less_x + vy) < 0) { vy += 1; } else if XNN_UNPREDICTABLE((int64_t) (vsquared_y_less_x - vy) >= 0) { vy -= 1; } } *output++ = vy; } }
1,322
26
94
c
XNNPACK
XNNPACK-master/src/math/u64-sqrt-scalar-cvtu32-sqrt-llrint.c
// Copyright 2022 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 <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_u64_sqrt__scalar_cvtu32_sqrt_llrint( size_t n, const uint64_t* input, uint64_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint64_t)) { const uint64_t vx = *input++; uint64_t vy = vx; if XNN_LIKELY(vx != 0) { const uint32_t vx_lo = (uint32_t) vx; const uint32_t vx_hi = (uint32_t) (vx >> 32); const double vf_hi = (double) vx_hi; const double vf_lo = (double) vx_lo; double vf = vf_hi * 0x1.0p+32 + vf_lo; vf = sqrt(vf); vy = (uint64_t) (int64_t) llrint(vf); #if XNN_ARCH_ARM || XNN_ARCH_X86 const uint64_t vsquared_y_less_x = math_mulext_u32((uint32_t) vy, (uint32_t) vy) - vx; #else const uint64_t vsquared_y_less_x = vy * vy - vx; #endif if XNN_UNPREDICTABLE((int64_t) (vsquared_y_less_x + vy) < 0) { vy += 1; } else if XNN_UNPREDICTABLE((int64_t) (vsquared_y_less_x - vy) >= 0) { vy -= 1; } } *output++ = vy; } }
1,323
26.020408
94
c
XNNPACK
XNNPACK-master/src/math/u64-sqrt-scalar-cvtu64-sqrt-llrint.c
// Copyright 2022 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 <math.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_u64_sqrt__scalar_cvtu64_sqrt_llrint( size_t n, const uint64_t* input, uint64_t* output) { assert(n % sizeof(uint32_t) == 0); for (; n != 0; n -= sizeof(uint64_t)) { const uint64_t vx = *input++; uint64_t vy = vx; if XNN_LIKELY(vx != 0) { double vf = (double) vx; vf = sqrt(vf); vy = (uint64_t) (int64_t) llrint(vf); #if XNN_ARCH_ARM || XNN_ARCH_X86 const uint64_t vsquared_y_less_x = math_mulext_u32((uint32_t) vy, (uint32_t) vy) - vx; #else const uint64_t vsquared_y_less_x = vy * vy - vx; #endif if XNN_UNPREDICTABLE((int64_t) (vsquared_y_less_x + vy) < 0) { vy += 1; } else if XNN_UNPREDICTABLE((int64_t) (vsquared_y_less_x - vy) >= 0) { vy -= 1; } } *output++ = vy; } }
1,127
24.066667
94
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-aarch64-neonfp16arith-expm1minus-rr1-p3h1ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__aarch64_neonfp16arith_expm1minus_rr1_p3h1ts_div( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x395B))); // 0x1.56Cp-1h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC08))); // -0x1.020p+0h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vmulq_f16(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 float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); vp = vfmaq_f16(vts, vp, vts); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(vemo, vtwo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vdivq_f16(vemo, vepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,075
44.72973
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-aarch64-neonfp16arith-expm1minus-rr1-p3h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__aarch64_neonfp16arith_expm1minus_rr1_p3h2ts_div( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * c3)) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBD5B))); // -0x1.56Cp+0h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4008))); // 0x1.020p+1h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * c3)) // = t * (-p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vfmsq_f16(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * c3)) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (t * s) * p const float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vts); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(vemo, vtwo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vdivq_f16(vemo, vepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
4,996
44.427273
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-avx2-expm1minus-rr1-p3h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f16-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_f16_tanh__avx2_expm1minus_rr1_p3h2ts_div( size_t n, const void* input, void* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m128i vsign_mask = _mm_set1_epi16(0x8000); // The largest z for which tanhh(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.208000p+2f); 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)) // on [-log(2)/4, log(2)/4] const __m256 vc3 = _mm256_set1_ps(0x1.560722p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.01E2A2p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(__m128i)) { const __m128i vx = _mm_load_si128((const __m128i*) i); i += 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. const __m128i vabsx = _mm_or_si128(vx, vsign_mask); __m256 vz = _mm256_cvtph_ps(vabsx); // Inverted mask for the sign of input: 0x0000 for negative x, 0x8000 for positive x. const __m128i vinvsignx = _mm_xor_si128(vx, vabsx); // The function saturates at -1 for large negative inputs: tanhh(z) == -1.0h for z <= sat_cutoff ~= -4.5078125. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhh(sat_cutoff) == -1.0h. 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 [-4.5078125, 4.5078125] (i.e. z outsize [-4.5078125, 0]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) 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. // -4.5078125 <= z <= 0, and -7 <= n <= 0 accordingly. const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // 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-3 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * c3)) // = t * p __m256 vp = 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)) + 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 __m128i vh = _mm256_cvtps_ph(vy, _MM_FROUND_TO_NEAREST_INT); vh = _mm_xor_si128(vh, vinvsignx); _mm_storeu_si128((__m128i*) o, vh); o += 8; } }
4,900
39.504132
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-avx2-expm1minus-rr1-p3h2ts-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f16-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_f16_tanh__avx2_expm1minus_rr1_p3h2ts_rcp( size_t n, const void* input, void* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m128i vsign_mask = _mm_set1_epi16(0x8000); // The largest z for which tanhh(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.208000p+2f); 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)) // on [-log(2)/4, log(2)/4] const __m256 vc3 = _mm256_set1_ps(0x1.560722p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.01E2A2p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(__m128i)) { const __m128i vx = _mm_load_si128((const __m128i*) i); i += 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. const __m128i vabsx = _mm_or_si128(vx, vsign_mask); __m256 vz = _mm256_cvtph_ps(vabsx); // Inverted mask for the sign of input: 0x0000 for negative x, 0x8000 for positive x. const __m128i vinvsignx = _mm_xor_si128(vx, vabsx); // The function saturates at -1 for large negative inputs: tanhh(z) == -1.0h for z <= sat_cutoff ~= -4.5078125. // 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 [-4.5078125, 4.5078125] (i.e. z outsize [-4.5078125, 0]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) 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. // -4.5078125 <= z <= 0, and -7 <= n <= 0 accordingly. const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // 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-3 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * c3)) // = t * p __m256 vp = 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)) + 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); // Compute approximate reciprocal of the denominator using the hardware instruction. __m256 vrepo = _mm256_rcp_ps(vepo); // 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 __m128i vh = _mm256_cvtps_ph(vy, _MM_FROUND_TO_NEAREST_INT); vh = _mm_xor_si128(vh, vinvsignx); _mm_storeu_si128((__m128i*) o, vh); o += 8; } }
5,182
40.134921
117
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-f16c-expm1minus-rr1-p3h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f16-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_f16_tanh__f16c_expm1minus_rr1_p3h2ts_div( size_t n, const void* input, void* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m128i vsign_mask = _mm_set1_epi16(0x8000); // The largest z for which tanhh(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.208000p+2f); 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)) // on [-log(2)/4, log(2)/4] const __m256 vc3 = _mm256_set1_ps(0x1.560722p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.01E2A2p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(__m128i)) { const __m128i vx = _mm_load_si128((const __m128i*) i); i += 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. const __m128i vabsx = _mm_or_si128(vx, vsign_mask); __m256 vz = _mm256_cvtph_ps(vabsx); // Inverted mask for the sign of input: 0x0000 for negative x, 0x8000 for positive x. const __m128i vinvsignx = _mm_xor_si128(vx, vabsx); // The function saturates at -1 for large negative inputs: tanhh(z) == -1.0h for z <= sat_cutoff ~= -4.5078125. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhh(sat_cutoff) == -1.0h. 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 [-4.5078125, 4.5078125] (i.e. z outsize [-4.5078125, 0]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m256 vn = _mm256_add_ps(_mm256_mul_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. // -4.5078125 <= z <= 0, and -7 <= 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_add_ps(_mm256_mul_ps(vn, vminus_ln2), vz); // Compute degree-3 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * c3)) // = t * p __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc3, vt), vc2); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * c3)) + 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_add_ps(_mm256_mul_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 __m128i vh = _mm256_cvtps_ph(vy, _MM_FROUND_TO_NEAREST_INT); vh = _mm_xor_si128(vh, vinvsignx); _mm_storeu_si128((__m128i*) o, vh); o += 8; } }
5,173
41.065041
123
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-f16c-expm1minus-rr1-p3h2ts-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f16-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_f16_tanh__f16c_expm1minus_rr1_p3h2ts_rcp( size_t n, const void* input, void* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m128i vsign_mask = _mm_set1_epi16(0x8000); // The largest z for which tanhh(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.208000p+2f); 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)) // on [-log(2)/4, log(2)/4] const __m256 vc3 = _mm256_set1_ps(0x1.560722p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.01E2A2p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(__m128i)) { const __m128i vx = _mm_load_si128((const __m128i*) i); i += 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. const __m128i vabsx = _mm_or_si128(vx, vsign_mask); __m256 vz = _mm256_cvtph_ps(vabsx); // Inverted mask for the sign of input: 0x0000 for negative x, 0x8000 for positive x. const __m128i vinvsignx = _mm_xor_si128(vx, vabsx); // The function saturates at -1 for large negative inputs: tanhh(z) == -1.0h for z <= sat_cutoff ~= -4.5078125. // 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 [-4.5078125, 4.5078125] (i.e. z outsize [-4.5078125, 0]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. __m256 vn = _mm256_add_ps(_mm256_mul_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. // -4.5078125 <= z <= 0, and -7 <= 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_add_ps(_mm256_mul_ps(vn, vminus_ln2), vz); // Compute degree-3 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * c3)) // = t * p __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc3, vt), vc2); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vtwo); // Reconstruct the exp(2z) - 1 value: // exp(2z) - 1 = s * (t * (2 + t * (c2 + t * c3)) + 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_add_ps(_mm256_mul_ps(vp, vts), vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const __m256 vepo = _mm256_add_ps(vemo, vtwo); // Compute approximate reciprocal of the denominator using the hardware instruction. __m256 vrepo = _mm256_rcp_ps(vepo); // 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 __m128i vh = _mm256_cvtps_ph(vy, _MM_FROUND_TO_NEAREST_INT); vh = _mm_xor_si128(vh, vinvsignx); _mm_storeu_si128((__m128i*) o, vh); o += 8; } }
5,455
41.625
123
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-f16c-polynomial-p17h8t2.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-avx-polynomial.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 <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f16_tanh__f16c_polynomial_p17h8t2( size_t n, const void* input, void* output) { assert(n % (8 * sizeof(uint16_t)) == 0); // The smallest number x above -0x1.208p+2h (the largest number z for which tanhh(z) is saturated at -1.0h) for which // this implementation of tanh(x) produce -1.0h output. const __m256 vneg_sat_cutoff = _mm256_set1_ps(-0x1.058000p+2f); // The largest number x below 0x1.208p+2h (the smallest number z for which tanhh(z) is saturated at 1.0h) for which // this implementation of tanh(x) produce 1.0h output. const __m256 vpos_sat_cutoff = _mm256_set1_ps(0x1.058000p+2f); // Coefficient of polynomial approximation // tanh(x) ~ x * (1 + t * (c3 + t * (c5 + t * (c7 + t * (c9 + t * (c11 + t * (c13 + t * (c15 + t * c17)))))))) // on [-0x1.208p+2h, 0x1.208p+2] where t = x * x const __m256 vc17 = _mm256_set1_ps(0x1.6B90F0p-29f); const __m256 vc15 = _mm256_set1_ps(-0x1.036B86p-22f); const __m256 vc13 = _mm256_set1_ps(0x1.3699B6p-17f); const __m256 vc11 = _mm256_set1_ps(-0x1.964AECp-13f); const __m256 vc9 = _mm256_set1_ps(0x1.3DD52Cp-9f); const __m256 vc7 = _mm256_set1_ps(-0x1.348432p-6f); const __m256 vc5 = _mm256_set1_ps(0x1.7D516Ap-4f); const __m256 vc3 = _mm256_set1_ps(-0x1.41F3C8p-2f); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= 8 * sizeof(uint16_t)) { __m256 vx = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) i)); i += 8; // tanhh(x) saturates at -1 for large negative inputs and at +1 for large positive inputs: tanhh(x) == -1.0h for // x <= -0x1.208p+2 ~= -4.5078125 and tanhh(x) == 1.0h for x >= 0x1.208p+2 ~= 4.5078125. To guarantee this // behaviour, we clip input x on [neg_sat_cutoff, pos_sat_cutoff] containing [-0x1.208p+2, 0x1.208p+2], and // leverage the fact that for our implementation tanhh(neg_sat_cutoff) == -1.0h and tanhh(pos_sat_cutoff) == 1.0h. // NaN inputs are passed unchanged. vx = _mm256_max_ps(vneg_sat_cutoff, vx); vx = _mm256_min_ps(vpos_sat_cutoff, vx); // Compute t = x * x to use for polynomial evaluation const __m256 vt = _mm256_mul_ps(vx, vx); // Compute degree-17 polynomial approximation for tanh(x) on [-0x1.208p+2, 0x1.208p+2]. // P(t) = c3 + t * (c5 + t * (c7 + t * (c9 + t * (c11 + t * (c13 + t * (c15 + t * c17)))))) __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc17, vt), vc15); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc13); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc11); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc9); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc7); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc5); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3); // Reconstruct the tanh(x) value: // tanh(x) ~ x * (1 + t * P(t)) // = x + (x * t) * P(t) const __m256 vxt = _mm256_mul_ps(vx, vt); const __m256 vy = _mm256_add_ps(_mm256_mul_ps(vp, vxt), vx); _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vy, _MM_FROUND_TO_NEAREST_INT)); o += 8; } }
3,539
41.650602
119
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-f16c-polynomial-p19h9t2.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-avx-polynomial.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 <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f16_tanh__f16c_polynomial_p19h9t2( size_t n, const void* input, void* output) { assert(n % (8 * sizeof(uint16_t)) == 0); // The smallest number x above -0x1.208p+2h (the largest number z for which tanhh(z) is saturated at -1.0h) for which // this implementation of tanh(x) produce -1.0h output. const __m256 vneg_sat_cutoff = _mm256_set1_ps(-0x1.1F0000p+2f); // The largest number x below 0x1.208p+2h (the smallest number z for which tanhh(z) is saturated at 1.0h) for which // this implementation of tanh(x) produce 1.0h output. const __m256 vpos_sat_cutoff = _mm256_set1_ps(0x1.1F0000p+2f); // Coefficient of polynomial approximation // tanh(x) ~ x * (1 + t * (c3 + t * (c5 + t * (c7 + t * (c9 + t * (c11 + t * (c13 + t * (c15 + t * (c17 + t * c19))))))))) // on [-0x1.208p+2h, 0x1.208p+2] where t = x * x const __m256 vc19 = _mm256_set1_ps(-0x1.1D841Cp-32f); const __m256 vc17 = _mm256_set1_ps(0x1.C4FC88p-26f); const __m256 vc15 = _mm256_set1_ps(-0x1.332066p-20f); const __m256 vc13 = _mm256_set1_ps(0x1.D1AEA2p-16f); const __m256 vc11 = _mm256_set1_ps(-0x1.B2782Ep-12f); const __m256 vc9 = _mm256_set1_ps(0x1.03CAEAp-8f); const __m256 vc7 = _mm256_set1_ps(-0x1.967628p-6f); const __m256 vc5 = _mm256_set1_ps(0x1.ABC35Cp-4f); const __m256 vc3 = _mm256_set1_ps(-0x1.499D08p-2f); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= 8 * sizeof(uint16_t)) { __m256 vx = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) i)); i += 8; // tanhh(x) saturates at -1 for large negative inputs and at +1 for large positive inputs: tanhh(x) == -1.0h for // x <= -0x1.208p+2 ~= -4.5078125 and tanhh(x) == 1.0h for x >= 0x1.208p+2 ~= 4.5078125. To guarantee this // behaviour, we clip input x on [neg_sat_cutoff, pos_sat_cutoff] containing [-0x1.208p+2, 0x1.208p+2], and // leverage the fact that for our implementation tanhh(neg_sat_cutoff) == -1.0h and tanhh(pos_sat_cutoff) == 1.0h. // NaN inputs are passed unchanged. vx = _mm256_max_ps(vneg_sat_cutoff, vx); vx = _mm256_min_ps(vpos_sat_cutoff, vx); // Compute t = x * x to use for polynomial evaluation const __m256 vt = _mm256_mul_ps(vx, vx); // Compute degree-19 polynomial approximation for tanh(x) on [-0x1.208p+2, 0x1.208p+2]. // P(t) = c3 + t * (c5 + t * (c7 + t * (c9 + t * (c11 + t * (c13 + t * (c15 + t * (c17 + t * c19))))))) __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc19, vt), vc17); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc15); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc13); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc11); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc9); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc7); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc5); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3); // Reconstruct the tanh(x) value: // tanh(x) ~ x * (1 + t * P(t)) // = x + (x * t) * P(t) const __m256 vxt = _mm256_mul_ps(vx, vt); const __m256 vy = _mm256_add_ps(_mm256_mul_ps(vp, vxt), vx); _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vy, _MM_FROUND_TO_NEAREST_INT)); o += 8; } }
3,672
42.211765
126
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-fma3-expm1minus-rr1-p3h2ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f16-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_f16_tanh__fma3_expm1minus_rr1_p3h2ts_div( size_t n, const void* input, void* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m128i vsign_mask = _mm_set1_epi16(0x8000); // The largest z for which tanhh(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.208000p+2f); 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)) // on [-log(2)/4, log(2)/4] const __m256 vc3 = _mm256_set1_ps(0x1.560722p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.01E2A2p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(__m128i)) { const __m128i vx = _mm_load_si128((const __m128i*) i); i += 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. const __m128i vabsx = _mm_or_si128(vx, vsign_mask); __m256 vz = _mm256_cvtph_ps(vabsx); // Inverted mask for the sign of input: 0x0000 for negative x, 0x8000 for positive x. const __m128i vinvsignx = _mm_xor_si128(vx, vabsx); // The function saturates at -1 for large negative inputs: tanhh(z) == -1.0h for z <= sat_cutoff ~= -4.5078125. // To guarantee this behaviour, we clip input z at sat_cutoff, and leverage the fact that for our implementation // tanhh(sat_cutoff) == -1.0h. 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 [-4.5078125, 4.5078125] (i.e. z outsize [-4.5078125, 0]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) 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. // -4.5078125 <= z <= 0, and -7 <= 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-3 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * c3)) // = t * p __m256 vp = 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)) + 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 __m128i vh = _mm256_cvtps_ph(vy, _MM_FROUND_TO_NEAREST_INT); vh = _mm_xor_si128(vh, vinvsignx); _mm_storeu_si128((__m128i*) o, vh); o += 8; } }
5,121
40.306452
123
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-fma3-expm1minus-rr1-p3h2ts-rcp.c
// Auto-generated file. Do not edit! // Template: src/math/f16-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_f16_tanh__fma3_expm1minus_rr1_p3h2ts_rcp( size_t n, const void* input, void* output) { assert(n % sizeof(__m256) == 0); // Mask for the sign bit. const __m128i vsign_mask = _mm_set1_epi16(0x8000); // The largest z for which tanhh(z) is saturated at -1.0f. const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.208000p+2f); 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)) // on [-log(2)/4, log(2)/4] const __m256 vc3 = _mm256_set1_ps(0x1.560722p+0f); const __m256 vc2 = _mm256_set1_ps(0x1.01E2A2p+1f); const __m256 vtwo = _mm256_set1_ps(2.0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(__m128i)) { const __m128i vx = _mm_load_si128((const __m128i*) i); i += 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. const __m128i vabsx = _mm_or_si128(vx, vsign_mask); __m256 vz = _mm256_cvtph_ps(vabsx); // Inverted mask for the sign of input: 0x0000 for negative x, 0x8000 for positive x. const __m128i vinvsignx = _mm_xor_si128(vx, vabsx); // The function saturates at -1 for large negative inputs: tanhh(z) == -1.0h for z <= sat_cutoff ~= -4.5078125. // 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 [-4.5078125, 4.5078125] (i.e. z outsize [-4.5078125, 0]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) 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. // -4.5078125 <= z <= 0, and -7 <= 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-3 polynomial approximation for exp(2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (2 + t * (c2 + t * c3)) // = t * p __m256 vp = 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)) + 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); // Compute approximate reciprocal of the denominator using the hardware instruction. __m256 vrepo = _mm256_rcp_ps(vepo); // 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 __m128i vh = _mm256_cvtps_ph(vy, _MM_FROUND_TO_NEAREST_INT); vh = _mm_xor_si128(vh, vinvsignx); _mm_storeu_si128((__m128i*) o, vh); o += 8; } }
5,403
40.891473
123
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-fma3-polynomial-p17h8t2.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-avx-polynomial.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 <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f16_tanh__fma3_polynomial_p17h8t2( size_t n, const void* input, void* output) { assert(n % (8 * sizeof(uint16_t)) == 0); // The smallest number x above -0x1.208p+2h (the largest number z for which tanhh(z) is saturated at -1.0h) for which // this implementation of tanh(x) produce -1.0h output. const __m256 vneg_sat_cutoff = _mm256_set1_ps(-0x1.05C000p+2f); // The largest number x below 0x1.208p+2h (the smallest number z for which tanhh(z) is saturated at 1.0h) for which // this implementation of tanh(x) produce 1.0h output. const __m256 vpos_sat_cutoff = _mm256_set1_ps(0x1.05C000p+2f); // Coefficient of polynomial approximation // tanh(x) ~ x * (1 + t * (c3 + t * (c5 + t * (c7 + t * (c9 + t * (c11 + t * (c13 + t * (c15 + t * c17)))))))) // on [-0x1.208p+2h, 0x1.208p+2] where t = x * x const __m256 vc17 = _mm256_set1_ps(0x1.6B90F0p-29f); const __m256 vc15 = _mm256_set1_ps(-0x1.036B86p-22f); const __m256 vc13 = _mm256_set1_ps(0x1.3699B6p-17f); const __m256 vc11 = _mm256_set1_ps(-0x1.964AECp-13f); const __m256 vc9 = _mm256_set1_ps(0x1.3DD52Cp-9f); const __m256 vc7 = _mm256_set1_ps(-0x1.348432p-6f); const __m256 vc5 = _mm256_set1_ps(0x1.7D516Ap-4f); const __m256 vc3 = _mm256_set1_ps(-0x1.41F3C8p-2f); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= 8 * sizeof(uint16_t)) { __m256 vx = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) i)); i += 8; // tanhh(x) saturates at -1 for large negative inputs and at +1 for large positive inputs: tanhh(x) == -1.0h for // x <= -0x1.208p+2 ~= -4.5078125 and tanhh(x) == 1.0h for x >= 0x1.208p+2 ~= 4.5078125. To guarantee this // behaviour, we clip input x on [neg_sat_cutoff, pos_sat_cutoff] containing [-0x1.208p+2, 0x1.208p+2], and // leverage the fact that for our implementation tanhh(neg_sat_cutoff) == -1.0h and tanhh(pos_sat_cutoff) == 1.0h. // NaN inputs are passed unchanged. vx = _mm256_max_ps(vneg_sat_cutoff, vx); vx = _mm256_min_ps(vpos_sat_cutoff, vx); // Compute t = x * x to use for polynomial evaluation const __m256 vt = _mm256_mul_ps(vx, vx); // Compute degree-17 polynomial approximation for tanh(x) on [-0x1.208p+2, 0x1.208p+2]. // P(t) = c3 + t * (c5 + t * (c7 + t * (c9 + t * (c11 + t * (c13 + t * (c15 + t * c17)))))) __m256 vp = vc17; vp = _mm256_fmadd_ps(vp, vt, vc15); vp = _mm256_fmadd_ps(vp, vt, vc13); vp = _mm256_fmadd_ps(vp, vt, vc11); vp = _mm256_fmadd_ps(vp, vt, vc9); vp = _mm256_fmadd_ps(vp, vt, vc7); vp = _mm256_fmadd_ps(vp, vt, vc5); vp = _mm256_fmadd_ps(vp, vt, vc3); // Reconstruct the tanh(x) value: // tanh(x) ~ x * (1 + t * P(t)) // = x + (x * t) * P(t) const __m256 vxt = _mm256_mul_ps(vx, vt); const __m256 vy = _mm256_fmadd_ps(vp, vxt, vx); _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vy, _MM_FROUND_TO_NEAREST_INT)); o += 8; } }
3,448
40.059524
119
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-fma3-polynomial-p19h9t2.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-avx-polynomial.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 <math.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f16_tanh__fma3_polynomial_p19h9t2( size_t n, const void* input, void* output) { assert(n % (8 * sizeof(uint16_t)) == 0); // The smallest number x above -0x1.208p+2h (the largest number z for which tanhh(z) is saturated at -1.0h) for which // this implementation of tanh(x) produce -1.0h output. const __m256 vneg_sat_cutoff = _mm256_set1_ps(-0x1.1F0000p+2f); // The largest number x below 0x1.208p+2h (the smallest number z for which tanhh(z) is saturated at 1.0h) for which // this implementation of tanh(x) produce 1.0h output. const __m256 vpos_sat_cutoff = _mm256_set1_ps(0x1.1F0000p+2f); // Coefficient of polynomial approximation // tanh(x) ~ x * (1 + t * (c3 + t * (c5 + t * (c7 + t * (c9 + t * (c11 + t * (c13 + t * (c15 + t * (c17 + t * c19))))))))) // on [-0x1.208p+2h, 0x1.208p+2] where t = x * x const __m256 vc19 = _mm256_set1_ps(-0x1.1D841Cp-32f); const __m256 vc17 = _mm256_set1_ps(0x1.C4FC88p-26f); const __m256 vc15 = _mm256_set1_ps(-0x1.332066p-20f); const __m256 vc13 = _mm256_set1_ps(0x1.D1AEA2p-16f); const __m256 vc11 = _mm256_set1_ps(-0x1.B2782Ep-12f); const __m256 vc9 = _mm256_set1_ps(0x1.03CAEAp-8f); const __m256 vc7 = _mm256_set1_ps(-0x1.967628p-6f); const __m256 vc5 = _mm256_set1_ps(0x1.ABC35Cp-4f); const __m256 vc3 = _mm256_set1_ps(-0x1.499D08p-2f); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= 8 * sizeof(uint16_t)) { __m256 vx = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) i)); i += 8; // tanhh(x) saturates at -1 for large negative inputs and at +1 for large positive inputs: tanhh(x) == -1.0h for // x <= -0x1.208p+2 ~= -4.5078125 and tanhh(x) == 1.0h for x >= 0x1.208p+2 ~= 4.5078125. To guarantee this // behaviour, we clip input x on [neg_sat_cutoff, pos_sat_cutoff] containing [-0x1.208p+2, 0x1.208p+2], and // leverage the fact that for our implementation tanhh(neg_sat_cutoff) == -1.0h and tanhh(pos_sat_cutoff) == 1.0h. // NaN inputs are passed unchanged. vx = _mm256_max_ps(vneg_sat_cutoff, vx); vx = _mm256_min_ps(vpos_sat_cutoff, vx); // Compute t = x * x to use for polynomial evaluation const __m256 vt = _mm256_mul_ps(vx, vx); // Compute degree-19 polynomial approximation for tanh(x) on [-0x1.208p+2, 0x1.208p+2]. // P(t) = c3 + t * (c5 + t * (c7 + t * (c9 + t * (c11 + t * (c13 + t * (c15 + t * (c17 + t * c19))))))) __m256 vp = vc19; vp = _mm256_fmadd_ps(vp, vt, vc17); vp = _mm256_fmadd_ps(vp, vt, vc15); vp = _mm256_fmadd_ps(vp, vt, vc13); vp = _mm256_fmadd_ps(vp, vt, vc11); vp = _mm256_fmadd_ps(vp, vt, vc9); vp = _mm256_fmadd_ps(vp, vt, vc7); vp = _mm256_fmadd_ps(vp, vt, vc5); vp = _mm256_fmadd_ps(vp, vt, vc3); // Reconstruct the tanh(x) value: // tanh(x) ~ x * (1 + t * P(t)) // = x + (x * t) * P(t) const __m256 vxt = _mm256_mul_ps(vx, vt); const __m256 vy = _mm256_fmadd_ps(vp, vxt, vx); _mm_storeu_si128((__m128i*) o, _mm256_cvtps_ph(vy, _MM_FROUND_TO_NEAREST_INT)); o += 8; } }
3,568
40.5
126
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h1ts-nr1fma.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h1ts_nr1fma( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x395B))); // 0x1.56Cp-1h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC08))); // -0x1.020p+0h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vmulq_f16(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 float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); vp = vfmaq_f16(vts, vp, vts); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(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. float16x8_t vrepo = vrecpeq_f16(vepo); const float16x8_t verepo = vfmaq_f16(vminus_one, vrepo, vepo); vrepo = vfmsq_f16(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,454
44.840336
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h1ts-nr1fmaadj.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h1ts_nr1fmaadj( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x395B))); // 0x1.56Cp-1h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC08))); // -0x1.020p+0h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vmulq_f16(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 float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); vp = vfmaq_f16(vts, vp, vts); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(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. float16x8_t vrepo = vrecpeq_f16(vepo); const float16x8_t verepo = vfmaq_f16(vminus_one, vrepo, vepo); vrepo = vfmsq_f16(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const float16x8_t vey = vfmsq_f16(vemo, vy, vepo); vy = vfmaq_f16(vy, vey, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,654
45.352459
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h1ts-nr1recps.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h1ts_nr1recps( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x395B))); // 0x1.56Cp-1h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC08))); // -0x1.020p+0h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vmulq_f16(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 float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); vp = vfmaq_f16(vts, vp, vts); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(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. float16x8_t vrepo = vrecpeq_f16(vepo); const float16x8_t verepo = vrecpsq_f16(vrepo, vepo); vrepo = vmulq_f16(vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,439
44.714286
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h1ts-nr1recpsadj.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h1ts_nr1recpsadj( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x395B))); // 0x1.56Cp-1h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC08))); // -0x1.020p+0h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vmulq_f16(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 float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); vp = vfmaq_f16(vts, vp, vts); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(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. float16x8_t vrepo = vrecpeq_f16(vepo); const float16x8_t verepo = vrecpsq_f16(vrepo, vepo); vrepo = vmulq_f16(vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const float16x8_t vey = vfmsq_f16(vemo, vy, vepo); vy = vfmaq_f16(vy, vey, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,639
45.229508
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h1ts-recpe.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h1ts_recpe( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x395B))); // 0x1.56Cp-1h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC08))); // -0x1.020p+0h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h for z >= sat_cutoff ~= 4.5078125. // 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 uint16x8_t vm = vcgeq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vmulq_f16(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 float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); vp = vfmaq_f16(vts, vp, vts); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(vemo, vtwo); // Compute approximate reciprocal of the denominator using the hardware instruction. float16x8_t vrepo = vrecpeq_f16(vepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Saturate tanh(-z) at -1 for large inputs. vy = vbslq_f16(vm, vminus_one, vy); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,341
44.65812
117
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h1ts-recpeadj.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h1ts_recpeadj( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ -2 * (t + t * (t * (c2 + t * c3))) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x395B))); // 0x1.56Cp-1h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC08))); // -0x1.020p+0h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = -2 * (t + t * (t * (c2 + t * c3))) // = -2 * (t + t * p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vmulq_f16(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 float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); vp = vfmaq_f16(vts, vp, vts); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vtwo); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(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. float16x8_t vrepo = vrecpeq_f16(vepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const float16x8_t vey = vfmsq_f16(vemo, vy, vepo); vy = vfmaq_f16(vy, vey, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,541
45.183333
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h2ts-nr1fma.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h2ts_nr1fma( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * c3)) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBD5B))); // -0x1.56Cp+0h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4008))); // 0x1.020p+1h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * c3)) // = t * (-p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vfmsq_f16(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * c3)) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (t * s) * p const float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vts); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(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. float16x8_t vrepo = vrecpeq_f16(vepo); const float16x8_t verepo = vfmaq_f16(vminus_one, vrepo, vepo); vrepo = vfmsq_f16(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,375
44.559322
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h2ts-nr1fmaadj.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h2ts_nr1fmaadj( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * c3)) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBD5B))); // -0x1.56Cp+0h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4008))); // 0x1.020p+1h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * c3)) // = t * (-p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vfmsq_f16(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * c3)) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (t * s) * p const float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vts); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(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. float16x8_t vrepo = vrecpeq_f16(vepo); const float16x8_t verepo = vfmaq_f16(vminus_one, vrepo, vepo); vrepo = vfmsq_f16(vrepo, vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const float16x8_t vey = vfmsq_f16(vemo, vy, vepo); vy = vfmaq_f16(vy, vey, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,575
45.082645
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h2ts-nr1recps.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h2ts_nr1recps( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * c3)) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBD5B))); // -0x1.56Cp+0h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4008))); // 0x1.020p+1h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * c3)) // = t * (-p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vfmsq_f16(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * c3)) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (t * s) * p const float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vts); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(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. float16x8_t vrepo = vrecpeq_f16(vepo); const float16x8_t verepo = vrecpsq_f16(vrepo, vepo); vrepo = vmulq_f16(vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,360
44.432203
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h2ts-nr1recpsadj.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h2ts_nr1recpsadj( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * c3)) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBD5B))); // -0x1.56Cp+0h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4008))); // 0x1.020p+1h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * c3)) // = t * (-p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vfmsq_f16(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * c3)) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (t * s) * p const float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vts); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(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. float16x8_t vrepo = vrecpeq_f16(vepo); const float16x8_t verepo = vrecpsq_f16(vrepo, vepo); vrepo = vmulq_f16(vrepo, verepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const float16x8_t vey = vfmsq_f16(vemo, vy, vepo); vy = vfmaq_f16(vy, vey, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,560
44.958678
116
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h2ts-recpe.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h2ts_recpe( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * c3)) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBD5B))); // -0x1.56Cp+0h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4008))); // 0x1.020p+1h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h for z >= sat_cutoff ~= 4.5078125. // 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 uint16x8_t vm = vcgeq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * c3)) // = t * (-p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vfmsq_f16(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * c3)) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (t * s) * p const float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vts); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(vemo, vtwo); // Compute approximate reciprocal of the denominator using the hardware instruction. float16x8_t vrepo = vrecpeq_f16(vepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Saturate tanh(-z) at -1 for large inputs. vy = vbslq_f16(vm, vminus_one, vy); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,262
44.37069
117
c
XNNPACK
XNNPACK-master/src/math/gen/f16-tanh-neonfp16arith-expm1minus-rr1-p3h2ts-recpeadj.c
// Auto-generated file. Do not edit! // Template: src/math/f16-tanh-neonfp16arith-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_f16_tanh__neonfp16arith_expm1minus_rr1_p3h2ts_recpeadj( size_t n, const void* input, void* output) { assert(n % sizeof(float16x8_t) == 0); // The smallest z for which tanhh(-z) is saturated at -1.0h. const float16x8_t vsat_cutoff = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4482))); // 0x1.208p+2h // Large number such that ulp(magic bias) == 0.5 and magic bias === 7.5 mod 2**8. const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x620F))); // 0x1.83Cp+9h const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBDC5))); // -0x1.714p+0h const float16x8_t vln2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x398C))); // 0x1.630p-1h // Coefficients of polynomial approximation // exp(-2t) - 1 ~ t * (-2 + t * (c2 + t * c3)) // on [-log(2)/4, log(2)/4] const float16x8_t vc3 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBD5B))); // -0x1.56Cp+0h const float16x8_t vc2 = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4008))); // 0x1.020p+1h const float16x8_t vtwo = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0x4000))); // 2.0h const float16x8_t vminus_one = vreinterpretq_f16_u16(vmovq_n_u16(UINT16_C(0xBC00))); // -1.0h // Mask for the sign bit. const uint16x8_t vsign_mask = vmovq_n_u16(UINT16_C(0x8000)); const uint16_t* i = (const uint16_t*) input; uint16_t* o = (uint16_t*) output; for (; n != 0; n -= sizeof(float16x8_t)) { const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8; // 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). float16x8_t vz = vabsq_f16(vx); // The function saturates at -1 for large positive inputs: tanhh(-z) == -1.0h 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.0h. NaN inputs are passed unchanged. vz = vminq_f16(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**10, i.e. |z| <= 0x1.630p+7 = 177.5), but that is acceptable, because inputs x // outside of [-4.5078125, 4.5078125] (i.e. z outsize [0, 4.5078125]) saturate tanhh(x). // Additionally, we fuse addition of the floating-point exponent bias (15) into the magic bias. // Note that addition-subtraction of the large number doesn't cause overflow for inputs in this range. float16x8_t vn = vfmaq_f16(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 <= 4.5078125, and -7 <= n <= 0 accordingly. const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10)); // Subtract the large number back to get final n := round(-z / log(2), 1) as a floating-point number. vn = vsubq_f16(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). const float16x8_t vt = vfmaq_f16(vz, vn, vln2); // Compute degree-3 polynomial approximation for exp(-2t) - 1 on [-log(2)/4, log(2)/4]. // P(t) = t * (-2 + t * (c2 + t * c3)) // = t * (-p) float16x8_t vp = vfmaq_f16(vc2, vc3, vt); vp = vfmsq_f16(vtwo, vp, vt); // Reconstruct the exp(-2z) - 1 value: // exp(-2z) - 1 = s * (t * (-2 + t * (c2 + t * c3)) + 1) - 1 // = s * t * (-p) + (s - 1) // = (s - 1) - (t * s) * p const float16x8_t vts = vmulq_f16(vt, vs); const float16x8_t vsmo = vaddq_f16(vs, vminus_one); const float16x8_t vemo = vfmsq_f16(vsmo, vp, vts); // Denominator of the tanh fraction: exp(-2z) + 1 = expm1(-2z) + 2 const float16x8_t vepo = vaddq_f16(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. float16x8_t vrepo = vrecpeq_f16(vepo); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float16x8_t vy = vmulq_f16(vemo, vrepo); // Adjust reconstructred expm1(-2z) / (2 + expm1(-2z)) to match the correctly rounded division result const float16x8_t vey = vfmsq_f16(vemo, vy, vepo); vy = vfmaq_f16(vy, vey, vrepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f16(vsign_mask, vx, vy); vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8; } }
5,462
44.907563
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-aarch64-neonfma-expm1minus-rr1-lut8-p4h3ps-div.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__aarch64_neonfma_expm1minus_rr1_lut8_p4h3ps_div( 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); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vdivq_f32(vemo, vepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
6,229
45.492537
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-aarch64-neonfma-expm1minus-rr1-p6h5ts-div.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__aarch64_neonfma_expm1minus_rr1_p6h5ts_div( 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); // Reconstruct y = expm1(-2z) / (expm1(-2z) + 2) float32x4_t vy = vdivq_f32(vemo, vepo); // Reconstruct tanh(x) = copysign(y, x) vy = vbslq_f32(vsign_mask, vx, vy); vst1q_f32(output, vy); output += 4; } }
4,901
42
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-expm1minus-rr1-lut4-p4h2ts-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__avx_expm1minus_rr1_lut4_p4h2ts_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 ~ 2 * (t + t * (t * (c2 + t * (c3 + t * c4)))) // on [-log(2)/16, log(2)/16] const __m256 vc4 = _mm256_set1_ps(0x1.554F9Ap-2f); const __m256 vc3 = _mm256_set1_ps(0x1.557082p-1f); const __m256 vc2 = _mm256_set1_ps(0x1.000002p+0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); const __m256 vtwo = _mm256_set1_ps(2.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_add_ps(_mm256_mul_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_add_ps(_mm256_mul_ps(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) __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc4, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_mul_ps(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 __m256 vts = _mm256_mul_ps(vt, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); vp = _mm256_add_ps(_mm256_mul_ps(vp, vts), vts); const __m256 vemo = _mm256_add_ps(_mm256_mul_ps(vp, vtwo), 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,369
44.5
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-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__avx_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_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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 = _mm256_add_ps(_mm256_mul_ps(vc4, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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,765
49.965116
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-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__avx_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_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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 = _mm256_add_ps(_mm256_mul_ps(vc6, vt), vc5); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc4); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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,377
41.68254
123
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-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__avx_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_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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 = _mm256_add_ps(_mm256_mul_ps(vc6, vt), vc5); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc4); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); // 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,878
42.548148
123
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-expm1minus-rr1-p6h5ts-nr2.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__avx_expm1minus_rr1_p6h5ts_nr2( 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_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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 = _mm256_add_ps(_mm256_mul_ps(vc6, vt), vc5); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc4); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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 (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. __m256 vrepo = _mm256_rcp_ps(vepo); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); // 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,962
42.845588
123
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-expm1minus-rr2-lut8-p4h2ts-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__avx_expm1minus_rr2_lut8_p4h2ts_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); // Last 7 bits are zeroes const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E400p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(-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 __m256 vc4 = _mm256_set1_ps(0x1.5558ECp-2f); const __m256 vc3 = _mm256_set1_ps(0x1.555C20p-1f); const __m256 vc2 = _mm256_set1_ps(0x1.000000p+0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); const __m256 vtwo = _mm256_set1_ps(2.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_add_ps(_mm256_mul_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). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm256_add_ps(_mm256_mul_ps(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) __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc4, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_mul_ps(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 __m256 vts = _mm256_mul_ps(vt, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); vp = _mm256_add_ps(_mm256_mul_ps(vp, vts), vts); const __m256 vemo = _mm256_add_ps(_mm256_mul_ps(vp, vtwo), 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); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); // 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,615
50.698925
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-expm1minus-rr2-lut8-p4h2ts-nr2.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__avx_expm1minus_rr2_lut8_p4h2ts_nr2( 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); // Last 7 bits are zeroes const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E400p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(-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 __m256 vc4 = _mm256_set1_ps(0x1.5558ECp-2f); const __m256 vc3 = _mm256_set1_ps(0x1.555C20p-1f); const __m256 vc2 = _mm256_set1_ps(0x1.000000p+0f); const __m256 vminus_one = _mm256_set1_ps(-1.0f); const __m256 vtwo = _mm256_set1_ps(2.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_add_ps(_mm256_mul_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). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm256_add_ps(_mm256_mul_ps(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) __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc4, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_mul_ps(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 __m256 vts = _mm256_mul_ps(vt, vs); const __m256 vsmo = _mm256_add_ps(vs, vminus_one); vp = _mm256_add_ps(_mm256_mul_ps(vp, vts), vts); const __m256 vemo = _mm256_add_ps(_mm256_mul_ps(vp, vtwo), vsmo); // Denominator of the tanh fraction: exp(2z) + 1 = expm1(2z) + 2 const __m256 vepo = _mm256_add_ps(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. __m256 vrepo = _mm256_rcp_ps(vepo); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); // 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,699
50.871658
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-expm1minus-rr2-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__avx_expm1minus_rr2_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); // Last 7 bits are zeroes const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E400p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(-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 __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_add_ps(_mm256_mul_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). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm256_add_ps(_mm256_mul_ps(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 __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc4, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); // 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,528
50.508108
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-expm1minus-rr2-lut8-p4h3ps-nr2.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__avx_expm1minus_rr2_lut8_p4h3ps_nr2( 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); // Last 7 bits are zeroes const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E400p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(-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 __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_add_ps(_mm256_mul_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). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm256_add_ps(_mm256_mul_ps(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 __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc4, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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 (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. __m256 vrepo = _mm256_rcp_ps(vepo); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); // 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,612
50.682796
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-expm1minus-rr2-lut8-p4h3ts-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__avx_expm1minus_rr2_lut8_p4h3ts_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); // Last 7 bits are zeroes const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E400p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(-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 __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_add_ps(_mm256_mul_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). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm256_add_ps(_mm256_mul_ps(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 __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc4, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); // 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,528
50.508108
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx-expm1minus-rr2-lut8-p4h3ts-nr2.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__avx_expm1minus_rr2_lut8_p4h3ts_nr2( 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); // Last 7 bits are zeroes const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E400p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(-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 __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_add_ps(_mm256_mul_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). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm256_add_ps(_mm256_mul_ps(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 __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc4, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_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_add_ps(_mm256_mul_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 (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. __m256 vrepo = _mm256_rcp_ps(vepo); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); vrepo = _mm256_mul_ps(vrepo, _mm256_sub_ps(vtwo, _mm256_mul_ps(vrepo, vepo))); // 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,612
50.682796
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-lut4-p4h3ts-perm-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_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 __m256 vtable = _mm256_set_ps( 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f, 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 __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 21); // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m256i vl = _mm256_castps_si256(_mm256_permutevar_ps(vtable, _mm256_castps_si256(vn))); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // 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; } }
5,881
42.25
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-lut4-p4h3ts-perm-nr1adj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_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 __m256 vtable = _mm256_set_ps( 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f, 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 __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 21); // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m256i vl = _mm256_castps_si256(_mm256_permutevar_ps(vtable, _mm256_castps_si256(vn))); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // 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,475
43.054422
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-lut8-p4h3ps-gather-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_expm1minus_rr1_lut8_p4h3ps_gather_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 __m256i vindex_mask = _mm256_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 __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m256i vidx = _mm256_and_si256(_mm256_castps_si256(vn), vindex_mask); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_8, vidx, sizeof(uint32_t)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // 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; } }
5,957
42.173913
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-lut8-p4h3ps-gather-nr1.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_expm1minus_rr1_lut8_p4h3ps_gather_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 __m256i vindex_mask = _mm256_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 __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m256i vidx = _mm256_and_si256(_mm256_castps_si256(vn), vindex_mask); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_8, vidx, sizeof(uint32_t)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // 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; } }
6,495
42.891892
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-lut8-p4h3ps-gather-nr1adj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_expm1minus_rr1_lut8_p4h3ps_gather_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 __m256i vindex_mask = _mm256_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 __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m256i vidx = _mm256_and_si256(_mm256_castps_si256(vn), vindex_mask); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_8, vidx, sizeof(uint32_t)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // 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; } }
6,551
42.973154
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-lut8-p4h3ps-perm-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_expm1minus_rr1_lut8_p4h3ps_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(-4) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p+19f); // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 const __m256i vtable = _mm256_set_epi32( 0x3F7AC0C7, 0x3F7744FD, 0x3F75672A, 0x3F7504F3, 0x3F75FED7, 0x3F7837F0, 0x3F7B95C2, 0x3F800000); 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 __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m256i vl = _mm256_permutevar8x32_epi32(vtable, _mm256_castps_si256(vn)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // 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; } }
5,836
42.237037
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-lut8-p4h3ps-perm-nr1.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_expm1minus_rr1_lut8_p4h3ps_perm_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); // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 const __m256i vtable = _mm256_set_epi32( 0x3F7AC0C7, 0x3F7744FD, 0x3F75672A, 0x3F7504F3, 0x3F75FED7, 0x3F7837F0, 0x3F7B95C2, 0x3F800000); 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 __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m256i vl = _mm256_permutevar8x32_epi32(vtable, _mm256_castps_si256(vn)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // 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; } }
6,374
42.965517
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-lut8-p4h3ps-perm-nr1adj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_expm1minus_rr1_lut8_p4h3ps_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(-4) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p+19f); // Table of exp2(k / 8) values decremented (as integer) by (k << 20), k = 0..7 const __m256i vtable = _mm256_set_epi32( 0x3F7AC0C7, 0x3F7744FD, 0x3F75672A, 0x3F7504F3, 0x3F75FED7, 0x3F7837F0, 0x3F7B95C2, 0x3F800000); 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 __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 20); // Use bits 0:3 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m256i vl = _mm256_permutevar8x32_epi32(vtable, _mm256_castps_si256(vn)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // 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; } }
6,430
43.047945
119
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-p6h5ts-div.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_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 __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // 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,067
39.870968
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-p6h5ts-nr1.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_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 __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // 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,605
40.835821
117
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx2-expm1minus-rr1-p6h5ts-nr1adj.c
// Auto-generated file. Do not edit! // Template: src/math/f32-tanh-avx2-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__avx2_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 __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // 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,661
40.940741
116
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-lut4-p4h3ts-perm-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_lut4_p4h3ts_perm_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) == exp2(-3) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p+20f); // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 const __m512 vtable = _mm512_set_ps( 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f, 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f, 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f, 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f); 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)/16, log(2)/16] const __m512 vc4 = _mm512_set1_ps(0x1.554F9Ap-1f); const __m512 vc3 = _mm512_set1_ps(-0x1.557082p+0f); const __m512 vc2 = _mm512_set1_ps(0x1.000002p+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), 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. __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 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 __m512i ve = _mm512_slli_epi32(_mm512_castps_si512(vn), 21); // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m512i vl = _mm512_castps_si512(_mm512_permutevar_ps(vtable, _mm512_castps_si512(vn))); // 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), 3) 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)/16, log(2)/16]. // 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) + (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,955
44.815385
124
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-lut4-p4h3ts-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_lut4_p4h3ts_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(-3) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p+20f); // Table of exp2(k / 4) values decremented (as integer) by (k << 21), k = 0..3 const __m512 vtable = _mm512_set_ps( 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f, 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f, 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f, 0x1.EE89FAp-1f, 0x1.EA09E6p-1f, 0x1.F06FE0p-1f, 0x1.000000p+0f); 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)/16, log(2)/16] const __m512 vc4 = _mm512_set1_ps(0x1.554F9Ap-1f); const __m512 vc3 = _mm512_set1_ps(-0x1.557082p+0f); const __m512 vc2 = _mm512_set1_ps(0x1.000002p+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), 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. __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 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 __m512i ve = _mm512_slli_epi32(_mm512_castps_si512(vn), 21); // Use bits 0:2 bits of n, as integer, as an index for table lookup of l := 2**frac(2n). const __m512i vl = _mm512_castps_si512(_mm512_permutevar_ps(vtable, _mm512_castps_si512(vn))); // 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), 3) 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)/16, log(2)/16]. // 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) + (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; } }
6,547
45.439716
124
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-lut8-p4h3ps-gather-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> // 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__avx512skx_expm1minus_rr1_lut8_p4h3ps_gather_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) == exp2(-4) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p+19f); // Mask for the lowest 3 bits const __m512i vindex_mask = _mm512_set1_epi32(0x7); 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 vidx = _mm512_and_si512(_mm512_castps_si512(vn), vindex_mask); const __m512i vl = _mm512_i32gather_epi32(vidx, xnn_table_exp2minus_k_over_8, sizeof(uint32_t)); // 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); // 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,882
44.253846
124
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-lut8-p4h3ps-gather-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> // 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__avx512skx_expm1minus_rr1_lut8_p4h3ps_gather_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); // Mask for the lowest 3 bits const __m512i vindex_mask = _mm512_set1_epi32(0x7); 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 vidx = _mm512_and_si512(_mm512_castps_si512(vn), vindex_mask); const __m512i vl = _mm512_i32gather_epi32(vidx, xnn_table_exp2minus_k_over_8, sizeof(uint32_t)); // 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,266
44.413043
124
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-lut8-p4h3ps-gather-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> // 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__avx512skx_expm1minus_rr1_lut8_p4h3ps_gather_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); // Mask for the lowest 3 bits const __m512i vindex_mask = _mm512_set1_epi32(0x7); 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 vidx = _mm512_and_si512(_mm512_castps_si512(vn), vindex_mask); const __m512i vl = _mm512_i32gather_epi32(vidx, xnn_table_exp2minus_k_over_8, sizeof(uint32_t)); // 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,474
44.921986
124
c
XNNPACK
XNNPACK-master/src/math/gen/f32-tanh-avx512skx-expm1minus-rr1-lut8-p4h3ps-perm-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_lut8_p4h3ps_perm_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) == 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); // 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,871
44.875
124
c