1 // Copyright 2019 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5
6 #include <assert.h>
7 #include <stddef.h>
8
9 #include <arm_neon.h>
10
11 #include <xnnpack/common.h>
12 #include <xnnpack/math-stubs.h>
13
14
15 // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63
16 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64];
17
xnn_math_f32_expminus__neonfma_rr2_lut64_p2(size_t n,const float * input,float * output)18 void xnn_math_f32_expminus__neonfma_rr2_lut64_p2(
19 size_t n,
20 const float* input,
21 float* output)
22 {
23 assert(n % (4 * sizeof(float)) == 0);
24
25 // Large number such that ulp(magic bias) == exp2(-6)
26 const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p17f);
27 const float32x4_t vlog2e = vmovq_n_f32(0x1.715476p0f);
28 // Mask for the lowest 6 bits
29 const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F));
30 const float32x4_t vminus_ln2_hi = vmovq_n_f32(-0x1.62e43p-1f);
31 const float32x4_t vminus_ln2_lo = vmovq_n_f32(0x1.05c61p-29f);
32 // Coefficient of polynomial approximation
33 // exp(t) ~ 1 + t * (1 + t * c2)
34 // on [-log(2)/128, log(2)/128]
35 const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f);
36 // The smallest x for which expf(x) is normalized.
37 const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep6f);
38
39 for (; n != 0; n -= 4 * sizeof(float)) {
40 const float32x4_t vx = vld1q_f32(input); input += 4;
41
42 // Compute reduced argument n := round(x / log(2), 6).
43 // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then
44 // subtracing the large number back. The first addition is combined with multiplication by log2e into a single FMA
45 // instruction. The trick with adding large number is valid only within certain bounds (|x / log(2)| <= 2**16, i.e.
46 // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 0]
47 // underflow expf(x). We fixup the result for such inputs at the very end of the algorithm.
48 float32x4_t vn = vfmaq_f32(vmagic_bias, vx, vlog2e);
49
50 // Create a floating-point number s (scale) such that s := 2**n for such inputs that expf(x) is normalized, i.e.
51 // -87.336544 <= x <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s in
52 // two steps:
53 // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in
54 // the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
55 // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized
56 // number, because for -87.33642 <= x <= 0 (inputs for which expf(x) is normalized) we have -126 <= int(n) <= 0,
57 // and thus the adjusted exponent is not lower than -126.
58 //
59 // Shift bits 6:14 into 23:31 (position of floating-point exponent).
60 const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 17);
61
62 // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n).
63 const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2));
64 const uint64_t vidx01 = vgetq_lane_u64(vidx, 0);
65 const uint64_t vidx23 = vgetq_lane_u64(vidx, 1);
66 float32x2_t vl01 = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx01));
67 float32x2_t vl23 = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx23));
68 vl01 = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx01 >> 32)), vl01, 1);
69 vl23 = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx23 >> 32)), vl23, 1);
70 const float32x4_t vl = vcombine_f32(vl01, vl23);
71 // Adjust exponent of the value l fetched from the table to get the final s value.
72 const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve));
73
74 // Subtract the large number back to get the final n := round(x / log(2), 6) as a floating-point number.
75 vn = vsubq_f32(vn, vmagic_bias);
76
77 // Compute reduced argument t := x - n * log(2)
78 // Use Cody-Waite range reduction method (note the two constants representing log(2)) to improve accuracy.
79 float32x4_t vt = vfmaq_f32(vx, vn, vminus_ln2_hi);
80 vt = vfmaq_f32(vt, vn, vminus_ln2_lo);
81
82 // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
83 // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p
84 float32x4_t vp = vmulq_f32(vt, vc2);
85 vp = vfmaq_f32(vt, vt, vp);
86
87 // Reconstruct the exp(x) value:
88 // exp(x) = s * (1 + t * (1 + t * c2))
89 // = s * (1 + p)
90 // = s + s * p
91 float32x4_t vf = vfmaq_f32(vs, vs, vp);
92
93 // For inputs below denormal cutoff, replace output with +0.0f.
94 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
95 vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcltq_f32(vx, vdenorm_cutoff)));
96 vst1q_f32(output, vf); output += 4;
97 }
98 }
99