• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-raddstoreexpminusmax/neon-p5.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2020 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9 
10 #include <assert.h>
11 
12 #include <arm_neon.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/raddstoreexpminusmax.h>
16 
17 
xnn_f32_raddstoreexpminusmax_ukernel__neonfma_p5_x12(size_t elements,const float * input,float * output,float * sum,float max)18 void xnn_f32_raddstoreexpminusmax_ukernel__neonfma_p5_x12(
19     size_t elements,
20     const float* input,
21     float* output,
22     float* sum,
23     float max)
24 {
25   assert(elements % sizeof(float) == 0);
26 
27   const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f);
28   // The smallest x for which expf(x) is normalized.
29   const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep6f);
30   const float32x4_t vlog2e = vmovq_n_f32(0x1.715476p+0f);
31   const float32x4_t vminus_ln2_hi = vmovq_n_f32(-0x1.62E43p-1f);
32   const float32x4_t vminus_ln2_lo = vmovq_n_f32(0x1.05C61p-29f);
33 
34   const float32x4_t vc1 = vmovq_n_f32(0x1.FFFFF6p-1f);
35   const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f);
36   const float32x4_t vc3 = vmovq_n_f32(0x1.555A80p-3f);
37   const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f);
38   const float32x4_t vc5 = vmovq_n_f32(0x1.0F9F9Cp-7f);
39 
40   const float32x4_t vi_max = vdupq_n_f32(max);
41 
42   float32x4_t vacc0 = vmovq_n_f32(0.0f);
43   for (; elements >= 12 * sizeof(float); elements -= 12 * sizeof(float)) {
44     // Load 12 (3x4) inputs at a time.
45     const float32x4_t vi0123 = vld1q_f32(input); input += 4;
46     const float32x4_t vi4567 = vld1q_f32(input); input += 4;
47     const float32x4_t vi89AB = vld1q_f32(input); input += 4;
48 
49     // Subtract maximum input x := i - i_max. This implies x <= 0.
50     const float32x4_t vx0123 = vsubq_f32(vi0123, vi_max);
51     const float32x4_t vx4567 = vsubq_f32(vi4567, vi_max);
52     const float32x4_t vx89AB = vsubq_f32(vi89AB, vi_max);
53 
54     // Compute reduced argument n := round(x / log(2)).
55     // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
56     // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
57     // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
58     // inputs outside of [-87.336540, 0.0] underflow expf(x) anyway. We fixup the result for such inputs at the very end
59     // of the algorithm.
60     float32x4_t vn0123 = vfmaq_f32(vmagic_bias, vx0123, vlog2e);
61     float32x4_t vn4567 = vfmaq_f32(vmagic_bias, vx4567, vlog2e);
62     float32x4_t vn89AB = vfmaq_f32(vmagic_bias, vx89AB, vlog2e);
63 
64     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
65     // -87.33642 <= x <= 0.0, and -126 <= n <= 0 accordingly.
66     const float32x4_t vs0123 = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn0123), 23));
67     const float32x4_t vs4567 = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn4567), 23));
68     const float32x4_t vs89AB = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn89AB), 23));
69 
70     // Subtract the large number back to get final n := round(x / log(2)).
71     vn0123 = vsubq_f32(vn0123, vmagic_bias);
72     vn4567 = vsubq_f32(vn4567, vmagic_bias);
73     vn89AB = vsubq_f32(vn89AB, vmagic_bias);
74 
75     // Compute reduced argument t := z - n * log(2).
76     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
77     float32x4_t vt0123 = vfmaq_f32(vx0123, vn0123, vminus_ln2_hi);
78     float32x4_t vt4567 = vfmaq_f32(vx4567, vn4567, vminus_ln2_hi);
79     float32x4_t vt89AB = vfmaq_f32(vx89AB, vn89AB, vminus_ln2_hi);
80 
81     vt0123 = vfmaq_f32(vt0123, vn0123, vminus_ln2_lo);
82     vt4567 = vfmaq_f32(vt4567, vn4567, vminus_ln2_lo);
83     vt89AB = vfmaq_f32(vt89AB, vn89AB, vminus_ln2_lo);
84 
85     // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
86     float32x4_t vp0123 = vfmaq_f32(vc4, vc5, vt0123);
87     float32x4_t vp4567 = vfmaq_f32(vc4, vc5, vt4567);
88     float32x4_t vp89AB = vfmaq_f32(vc4, vc5, vt89AB);
89 
90     vp0123 = vfmaq_f32(vc3, vp0123, vt0123);
91     vp4567 = vfmaq_f32(vc3, vp4567, vt4567);
92     vp89AB = vfmaq_f32(vc3, vp89AB, vt89AB);
93 
94     vp0123 = vfmaq_f32(vc2, vp0123, vt0123);
95     vp4567 = vfmaq_f32(vc2, vp4567, vt4567);
96     vp89AB = vfmaq_f32(vc2, vp89AB, vt89AB);
97 
98     vp0123 = vfmaq_f32(vc1, vp0123, vt0123);
99     vp4567 = vfmaq_f32(vc1, vp4567, vt4567);
100     vp89AB = vfmaq_f32(vc1, vp89AB, vt89AB);
101 
102     // Reconstruct the final f value:
103     //   f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
104     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
105     //     = s + (t * s) * p
106     vt0123 = vmulq_f32(vt0123, vs0123);
107     vt4567 = vmulq_f32(vt4567, vs4567);
108     vt89AB = vmulq_f32(vt89AB, vs89AB);
109 
110     float32x4_t vf0123 = vfmaq_f32(vs0123, vp0123, vt0123);
111     float32x4_t vf4567 = vfmaq_f32(vs4567, vp4567, vt4567);
112     float32x4_t vf89AB = vfmaq_f32(vs89AB, vp89AB, vt89AB);
113 
114     // For inputs below denormal cutoff, replace output with +0.0f.
115     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
116     vf0123 = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf0123), vcltq_f32(vx0123, vdenorm_cutoff)));
117     vf4567 = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf4567), vcltq_f32(vx4567, vdenorm_cutoff)));
118     vf89AB = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf89AB), vcltq_f32(vx89AB, vdenorm_cutoff)));
119 
120     // Store 12 (3x4) outputs at a time.
121     vst1q_f32(output, vf0123); output += 4;
122     vst1q_f32(output, vf4567); output += 4;
123     vst1q_f32(output, vf89AB); output += 4;
124 
125     // Accumulate computed exponents.
126     vacc0 = vaddq_f32(vacc0, vf0123);
127     vacc0 = vaddq_f32(vacc0, vf4567);
128     vacc0 = vaddq_f32(vacc0, vf89AB);
129   }
130 
131   float32x4_t vacc = vacc0;
132   for (; elements >= 4 * sizeof(float); elements -= 4 * sizeof(float)) {
133     // Load 4 inputs at a time.
134     const float32x4_t vi = vld1q_f32(input); input += 4;
135 
136     // Subtract maximum input x := i - i_max. This implies x <= 0.
137     const float32x4_t vx = vsubq_f32(vi, vi_max);
138 
139     // Compute reduced argument n := round(x / log(2)).
140     // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
141     // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
142     // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
143     // inputs outside of [-87.336540, 0.0] underflow expf(x) anyway. We fixup the result for such inputs at the very end
144     // of the algorithm.
145     float32x4_t vn = vfmaq_f32(vmagic_bias, vx, vlog2e);
146 
147     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
148     // -87.33642 <= x <= 0.0, and -126 <= n <= 0 accordingly.
149     const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23));
150 
151     // Subtract the large number back to get final n := round(x / log(2)).
152     vn = vsubq_f32(vn, vmagic_bias);
153 
154     // Compute reduced argument t := z - n * log(2).
155     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
156     float32x4_t vt = vfmaq_f32(vx, vn, vminus_ln2_hi);
157     vt = vfmaq_f32(vt, vn, vminus_ln2_lo);
158 
159     // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
160     float32x4_t vp = vfmaq_f32(vc4, vc5, vt);
161     vp = vfmaq_f32(vc3, vp, vt);
162     vp = vfmaq_f32(vc2, vp, vt);
163     vp = vfmaq_f32(vc1, vp, vt);
164 
165     // Reconstruct the final f value:
166     //   f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
167     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
168     //     = s + (t * s) * p
169     vt = vmulq_f32(vt, vs);
170     float32x4_t vf = vfmaq_f32(vs, vp, vt);
171 
172     // For inputs below denormal cutoff, replace output with +0.0f.
173     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
174     vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcltq_f32(vx, vdenorm_cutoff)));
175 
176     // Store 4 outputs at a time.
177     vst1q_f32(output, vf); output += 4;
178 
179     // Accumulate computed exponents.
180     vacc = vaddq_f32(vacc, vf);
181   }
182 #if XNN_ARCH_ARM64
183   float vacc_lo = vaddvq_f32(vacc);
184 #else
185   float32x2_t vacc_lo = vadd_f32(vget_high_f32(vacc), vget_low_f32(vacc));
186 #endif
187   if (elements != 0) {
188     assert(elements >= 1 * sizeof(float));
189     assert(elements <= 3 * sizeof(float));
190     // Load 4 inputs at a time.
191     const float32x4_t vi = vld1q_f32(input); input += 4;
192 
193     // Subtract maximum input x := i - i_max. This implies x <= 0.
194     const float32x4_t vx = vsubq_f32(vi, vi_max);
195 
196     // Compute reduced argument n := round(x / log(2)).
197     // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
198     // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
199     // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
200     // inputs outside of [-87.336540, 0.0] underflow expf(x) anyway. We fixup the result for such inputs at the very end
201     // of the algorithm.
202     float32x4_t vn = vfmaq_f32(vmagic_bias, vx, vlog2e);
203 
204     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
205     // -87.33642 <= x <= 0.0, and -126 <= n <= 0 accordingly.
206     const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23));
207 
208     // Subtract the large number back to get final n := round(x / log(2)).
209     vn = vsubq_f32(vn, vmagic_bias);
210 
211     // Compute reduced argument t := z - n * log(2).
212     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
213     float32x4_t vt = vfmaq_f32(vx, vn, vminus_ln2_hi);
214     vt = vfmaq_f32(vt, vn, vminus_ln2_lo);
215 
216     // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
217     float32x4_t vp = vfmaq_f32(vc4, vc5, vt);
218     vp = vfmaq_f32(vc3, vp, vt);
219     vp = vfmaq_f32(vc2, vp, vt);
220     vp = vfmaq_f32(vc1, vp, vt);
221 
222     // Reconstruct the final f value:
223     //   f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
224     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
225     //     = s + (t * s) * p
226     vt = vmulq_f32(vt, vs);
227     float32x4_t vf = vfmaq_f32(vs, vp, vt);
228 
229     // For inputs below denormal cutoff, replace output with +0.0f.
230     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
231     vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcltq_f32(vx, vdenorm_cutoff)));
232 
233     float32x2_t vf_lo = vget_low_f32(vf);
234     if (elements & (2 * sizeof(float))) {
235       // Store 2 outputs at a time.
236       vst1_f32(output, vf_lo); output += 2;
237 
238       // Accumulate 2 computed exponents.
239       #if XNN_ARCH_ARM64
240         vacc_lo += vaddv_f32(vf_lo);
241       #else
242         vacc_lo = vadd_f32(vacc_lo, vf_lo);
243       #endif
244 
245       vf_lo = vget_high_f32(vf);
246     }
247     if (elements & (1 * sizeof(float))) {
248       // Store 1 output at a time.
249       vst1_lane_f32(output, vf_lo, 0);
250 
251       // Accumulate 1 computed exponent.
252       #if XNN_ARCH_ARM64
253         vacc_lo += vget_lane_f32(vf_lo, 0);
254       #else
255         vacc_lo = vadd_f32(vacc_lo, vreinterpret_f32_u64(vshl_n_u64(vreinterpret_u64_f32(vf_lo), 32)));
256       #endif
257     }
258   }
259   // Reduce 4 elements in the SIMD register
260 #if XNN_ARCH_ARM64
261   *sum = vacc_lo;
262 #else
263   vst1_lane_f32(sum, vpadd_f32(vacc_lo, vacc_lo), 0);
264 #endif
265 }
266