Debian Codegraph @

easel/esl_neon.c

raw symbols SCIP
1 /* Vectorized routines for ARM processors, using NEON intrinsics.
2 *
3 * Table of contents:
4 * 1. SIMD logf(), expf()
5 * 2. Utilities for float vectors (4 floats in an esl_neon_128f_t)
6 * 3. Benchmark
7 * 4. Unit tests
8 * 5. Test driver
9 * 6. Example
10 *
11 *****************************************************************
12 *
13 * This code is conditionally compiled, only when <eslENABLE_NEON> was
14 * set in <esl_config.h> by the configure script, and that will only
15 * happen on ARM platforms. When <eslENABLE_NEON> is not set, we
16 * include some dummy code to silence compiler and ranlib warnings
17 * about empty translation units and no symbols, and dummy drivers
18 * that do nothing but declare success.
19 *
20 *****************************************************************
21 * Credits:
22 *
23 * The logf() and expf() routines are derivatives of routines by
24 * Julien Pommier [http://gruntthepeon.free.fr/ssemath/]. Those
25 * routines were in turn based on serial implementations in the Cephes
26 * math library by Stephen Moshier [Moshier89;
27 * http://www.moshier.net/#Cephes]. Thanks and credit to both Moshier
28 * and Pommier for their clear code. Additional copyright and license
29 * information is appended at the end of the file.
30 */
31 #include <esl_config.h>
32 #ifdef eslENABLE_NEON
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <math.h>
37 #include <float.h>
38
39 #include "easel.h"
40 #include "esl_neon.h"
41
42 /* Definitions for log/exp */
43 #define c_inv_mant_mask ~0x7f800000u
44 #define c_cephes_SQRTHF 0.707106781186547524
45 #define c_cephes_log_p0 7.0376836292E-2
46 #define c_cephes_log_p1 -1.1514610310E-1
47 #define c_cephes_log_p2 1.1676998740E-1
48 #define c_cephes_log_p3 -1.2420140846E-1
49 #define c_cephes_log_p4 +1.4249322787E-1
50 #define c_cephes_log_p5 -1.6668057665E-1
51 #define c_cephes_log_p6 +2.0000714765E-1
52 #define c_cephes_log_p7 -2.4999993993E-1
53 #define c_cephes_log_p8 +3.3333331174E-1
54 #define c_cephes_log_q1 -2.12194440e-4
55 #define c_cephes_log_q2 0.693359375
56 #define c_exp_hi 88.3762626647949f
57 #define c_exp_lo -88.3762626647949f
58 #define c_cephes_LOG2EF 1.44269504088896341
59 #define c_cephes_exp_C1 0.693359375
60 #define c_cephes_exp_C2 -2.12194440e-4
61 #define c_cephes_exp_p0 1.9875691500E-4
62 #define c_cephes_exp_p1 1.3981999507E-3
63 #define c_cephes_exp_p2 8.3334519073E-3
64 #define c_cephes_exp_p3 4.1665795894E-2
65 #define c_cephes_exp_p4 1.6666665459E-1
66 #define c_cephes_exp_p5 5.0000001201E-1
67
68 /*****************************************************************
69 * 1. NEON SIMD logf(), expf()
70 *****************************************************************/
71
72 /* Function: esl_neon_logf()
73 * Synopsis: <r[z] = log x[z]>
74 * Incept: Tyler Camp, summer 2016
75 *
76 * Purpose: Given a vector <x> containing four floats, returns a
77 * vector <r> in which each element <r[z] = logf(x[z])>.
78 *
79 * Valid in the domain $x_z > 0$ for normalized IEEE754
80 * $x_z$.
81 *
82 * For <x> $< 0$, including -0, returns <NaN>. For <x> $==
83 * 0$ or subnormal <x>, returns <-inf>. For <x = inf>,
84 * returns <inf>. For <x = NaN>, returns <NaN>. For
85 * subnormal <x>, returns <-inf>.
86 *
87 * Note: Derived from an ARM implementation by Julian
88 * Pommier. Added handling of IEEE754 specials.
89 */
90 esl_neon_128f_t
91 esl_neon_logf(esl_neon_128f_t x)
92 {
93 esl_neon_128f_t one, e, tmp, z, y, inf_vector, neginf_vector;
94 esl_neon_128i_t nan_mask, emm0, ux, mask;
95 esl_neon_128i_t poszero_mask, inf_mask; /* Special IEEE754 inputs */
96 uint32_t negzero = (1 << 31);
97 esl_neon_128f_t zero_vector;
98
99 zero_vector.f32x4 = vdupq_n_f32(0.0f);
100 inf_vector.f32x4 = vdupq_n_f32(eslINFINITY); /* All floats with exponent bits high */
101 neginf_vector.f32x4 = vdupq_n_f32(-eslINFINITY); /* -inf */
102 one.f32x4 = vdupq_n_f32(1);
103
104 nan_mask.u32x4 = vcltq_f32(x.f32x4, vdupq_n_f32(0.0)); /* log(-x) = NaN */
105 nan_mask.u32x4 = vorrq_u32(vceqq_u32(vreinterpretq_u32_f32(x.f32x4),
106 vdupq_n_u32(negzero)),
107 nan_mask.u32x4); /* log(-0) = NaN */
108 /* Mask all other NaNs */
109 esl_neon_128i_t exp_hi_mask, mantissa_mask, mantissa_vector;
110 mantissa_vector.u32x4 = vdupq_n_u32(0x007FFFFF);
111 exp_hi_mask.u32x4 = vandq_u32(vreinterpretq_u32_f32(x.f32x4),
112 vreinterpretq_u32_f32(inf_vector.f32x4));
113 exp_hi_mask.u32x4 = vceqq_u32(exp_hi_mask.u32x4, vreinterpretq_u32_f32(inf_vector.f32x4));
114 mantissa_mask.u32x4 = vandq_u32(vreinterpretq_u32_f32(x.f32x4), mantissa_vector.u32x4);
115 mantissa_mask.u32x4 = vcgtq_u32(mantissa_mask.u32x4, vreinterpretq_u32_f32(zero_vector.f32x4));
116 nan_mask.u32x4 = vorrq_u32(vandq_u32(mantissa_mask.u32x4, exp_hi_mask.u32x4), nan_mask.u32x4);
117
118 ux.s32x4 = vreinterpretq_s32_f32(x.f32x4);
119 emm0.u32x4 = vshrq_n_u32(ux.u32x4, 23);
120
121 /* Mask 0 elements and infinity elements; log(0) = -inf, log(inf) = inf, log(NaN) = NaN */
122 poszero_mask.u32x4 = vceqq_f32(x.f32x4, zero_vector.f32x4);
123
124 /* (x == +0) : !(x < 0) && (x == 0) */
125 poszero_mask.u32x4 = vandq_u32(vmvnq_u32(nan_mask.u32x4), poszero_mask.u32x4);
126
127 /* +inf */
128 inf_mask.u32x4 = vceqq_f32(x.f32x4, inf_vector.f32x4);
129
130 /* keep only the fractional part */
131 ux.s32x4 = vandq_s32(ux.s32x4, vdupq_n_s32(c_inv_mant_mask));
132 ux.s32x4 = vorrq_s32(ux.s32x4, vreinterpretq_s32_f32(vdupq_n_f32(0.5f)));
133 x.f32x4 = vreinterpretq_f32_s32(ux.s32x4);
134
135 emm0.s32x4 = vsubq_s32(emm0.s32x4, vdupq_n_s32(0x7f));
136 e.f32x4 = vcvtq_f32_s32(emm0.s32x4);
137
138 e.f32x4 = vaddq_f32(e.f32x4, one.f32x4);
139
140 /* part2:
141 if( x < SQRTHF ) {
142 e -= 1;
143 x = x + x - 1.0;
144 } else { x = x - 1.0; }
145 */
146 mask.u32x4 = vcltq_f32(x.f32x4, vdupq_n_f32(c_cephes_SQRTHF));
147 tmp.f32x4 = vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(x.f32x4), mask.u32x4));
148 x.f32x4 = vsubq_f32(x.f32x4, one.f32x4);
149 e.f32x4 = vsubq_f32(e.f32x4, vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(one.f32x4), mask.u32x4)));
150 x.f32x4 = vaddq_f32(x.f32x4, tmp.f32x4);
151
152 z.f32x4 = vmulq_f32(x.f32x4, x.f32x4);
153 y.f32x4 = vdupq_n_f32(c_cephes_log_p0);
154
155 y.f32x4 = vmlaq_f32(vdupq_n_f32(c_cephes_log_p1), y.f32x4, x.f32x4);
156 y.f32x4 = vmlaq_f32(vdupq_n_f32(c_cephes_log_p2), y.f32x4, x.f32x4);
157 y.f32x4 = vmlaq_f32(vdupq_n_f32(c_cephes_log_p3), y.f32x4, x.f32x4);
158 y.f32x4 = vmlaq_f32(vdupq_n_f32(c_cephes_log_p4), y.f32x4, x.f32x4);
159 y.f32x4 = vmlaq_f32(vdupq_n_f32(c_cephes_log_p5), y.f32x4, x.f32x4);
160 y.f32x4 = vmlaq_f32(vdupq_n_f32(c_cephes_log_p6), y.f32x4, x.f32x4);
161 y.f32x4 = vmlaq_f32(vdupq_n_f32(c_cephes_log_p7), y.f32x4, x.f32x4);
162 y.f32x4 = vmlaq_f32(vdupq_n_f32(c_cephes_log_p8), y.f32x4, x.f32x4);
163
164 y.f32x4 = vmulq_f32(y.f32x4, x.f32x4);
165 y.f32x4 = vmulq_f32(y.f32x4, z.f32x4);
166 tmp.f32x4 = vmulq_f32(e.f32x4, vdupq_n_f32(c_cephes_log_q1));
167 y.f32x4 = vaddq_f32(y.f32x4, tmp.f32x4);
168 tmp.f32x4 = vmulq_f32(z.f32x4, vdupq_n_f32(0.5f));
169 y.f32x4 = vsubq_f32(y.f32x4, tmp.f32x4);
170 tmp.f32x4 = vmulq_f32(e.f32x4, vdupq_n_f32(c_cephes_log_q2));
171 x.f32x4 = vaddq_f32(x.f32x4, y.f32x4);
172 x.f32x4 = vaddq_f32(x.f32x4, tmp.f32x4);
173
174 /* IEEE754 cleanup */
175 esl_neon_128f_t inf_mask_view, poszero_mask_view;
176 neginf_vector.f32x4 = vdupq_n_f32(-eslINFINITY); /* -inf */
177 inf_mask_view.f32x4 = vreinterpretq_f32_s32(inf_mask.s32x4);
178 poszero_mask_view.f32x4 = vreinterpretq_f32_s32(poszero_mask.s32x4);
179 x.f32x4 = vreinterpretq_f32_s32(vorrq_s32(vreinterpretq_s32_f32(x.f32x4),
180 nan_mask.s32x4)); /* log(x<0, including -0, -inf)=NaN */
181 x = esl_neon_select_float(x, neginf_vector, poszero_mask_view); /* mask +0 */
182 x = esl_neon_select_float(x, inf_vector, inf_mask_view); /* mask +inf */
183 return x;
184 }
185
186 /* Function: esl_neon_expf()
187 * Synopsis: <r[z] = exp x[z]>
188 * Incept: Tyler Camp, summer 2016
189 *
190 * Purpose: Given a vector <x> containing four floats, returns a
191 * vector <r> in which each element <r[z] = expf(x[z])>.
192 * Valid for all IEEE754 floats $x_z$.
193 *
194 * Note: Derived from a NEON implementation by Julian Pommier.
195 */
196 esl_neon_128f_t
197 esl_neon_expf(esl_neon_128f_t x)
198 {
199 esl_neon_128f_t tmp, fx, one, z;
200 esl_neon_128i_t mask, maxmask, minmask;
201
202 /* handle out of range and special conditions */
203 maxmask.u32x4 = vcgtq_f32(x.f32x4, vdupq_n_f32(c_exp_hi));
204 minmask.u32x4 = vcleq_f32(x.f32x4, vdupq_n_f32(c_exp_lo));
205
206 one.f32x4 = vdupq_n_f32(1);
207 x.f32x4 = vminq_f32(x.f32x4, vdupq_n_f32(c_exp_hi));
208 x.f32x4 = vmaxq_f32(x.f32x4, vdupq_n_f32(c_exp_lo));
209
210 /* express exp(x) as exp(g + n*log(2)) */
211 fx.f32x4 = vmlaq_f32(vdupq_n_f32(0.5f), x.f32x4, vdupq_n_f32(c_cephes_LOG2EF));
212
213 /* perform a floorf */
214 tmp.f32x4 = vcvtq_f32_s32(vcvtq_s32_f32(fx.f32x4));
215
216 /* if greater, substract 1 */
217 mask.u32x4 = vcgtq_f32(tmp.f32x4, fx.f32x4);
218 mask.u32x4 = vandq_u32(mask.u32x4, vreinterpretq_u32_f32(one.f32x4));
219
220
221 fx.f32x4 = vsubq_f32(tmp.f32x4, vreinterpretq_f32_u32(mask.u32x4));
222
223 tmp.f32x4 = vmulq_f32(fx.f32x4, vdupq_n_f32(c_cephes_exp_C1));
224 z.f32x4 = vmulq_f32(fx.f32x4, vdupq_n_f32(c_cephes_exp_C2));
225 x.f32x4 = vsubq_f32(x.f32x4, tmp.f32x4);
226 x.f32x4 = vsubq_f32(x.f32x4, z.f32x4);
227
228 static const float cephes_exp_p[6] = { c_cephes_exp_p0, c_cephes_exp_p1, c_cephes_exp_p2, c_cephes_exp_p3, c_cephes_exp_p4, c_cephes_exp_p5 };
229 esl_neon_128f_t y, c1, c2, c3, c4, c5, pow2n;
230 y.f32x4 = vld1q_dup_f32(cephes_exp_p+0);
231 c1.f32x4 = vld1q_dup_f32(cephes_exp_p+1);
232 c2.f32x4 = vld1q_dup_f32(cephes_exp_p+2);
233 c3.f32x4 = vld1q_dup_f32(cephes_exp_p+3);
234 c4.f32x4 = vld1q_dup_f32(cephes_exp_p+4);
235 c5.f32x4 = vld1q_dup_f32(cephes_exp_p+5);
236
237 y.f32x4 = vmulq_f32(y.f32x4, x.f32x4);
238 z.f32x4 = vmulq_f32(x.f32x4,x.f32x4);
239 y.f32x4 = vaddq_f32(y.f32x4, c1.f32x4);
240
241 y.f32x4 = vmlaq_f32(c2.f32x4, y.f32x4, x.f32x4);
242 y.f32x4 = vmlaq_f32(c3.f32x4, y.f32x4, x.f32x4);
243 y.f32x4 = vmlaq_f32(c4.f32x4, y.f32x4, x.f32x4);
244 y.f32x4 = vmlaq_f32(c5.f32x4, y.f32x4, x.f32x4);
245 y.f32x4 = vmlaq_f32(x.f32x4, y.f32x4, z.f32x4);
246
247 y.f32x4 = vaddq_f32(y.f32x4, one.f32x4);
248
249 /* build 2^n */
250 esl_neon_128i_t mm;
251 mm.s32x4 = vcvtq_s32_f32(fx.f32x4);
252 mm.s32x4 = vaddq_s32(mm.s32x4, vdupq_n_s32(0x7f));
253 mm.s32x4 = vshlq_n_s32(mm.s32x4, 23);
254 pow2n.f32x4 = vreinterpretq_f32_s32(mm.s32x4);
255
256 y.f32x4 = vmulq_f32(y.f32x4, pow2n.f32x4);
257
258 /* special/range cleanup */
259 esl_neon_128f_t maxmask_view, minmask_view, zero_vec, inf_vec;
260 zero_vec.f32x4 = vdupq_n_f32(0.0f);
261 inf_vec.f32x4 = vdupq_n_f32(-eslINFINITY);
262 maxmask_view.f32x4 = vreinterpretq_f32_s32(maxmask.s32x4);
263 minmask_view.f32x4 = vreinterpretq_f32_s32(minmask.s32x4);
264 y = esl_neon_select_float(y, inf_vec, maxmask_view); /* exp(x) = inf for x > log(2^128) */
265 y = esl_neon_select_float(y, zero_vec, minmask_view); /* exp(x) = 0 for x < log(2^-149) */
266 return y;
267 }
268
269
270 /*****************************************************************
271 * 2. Utilities for fq vectors (4 floats in an esl_neon_128f_t)
272 *****************************************************************/
273
274 void
275 esl_neon_dump_float(FILE *fp, esl_neon_128f_t v)
276 {
277 float *p = (float *)&v;
278 fprintf(fp, "[%13.8g, %13.8g, %13.8g, %13.8g]", p[0], p[1], p[2], p[3]);
279 }
280
281
282
283
284 /*****************************************************************
285 * 3. Benchmark
286 *****************************************************************/
287 #ifdef eslNEON_BENCHMARK
288
289 /* gcc -mfpu=neon -O3 -o benchmark-neon -I ~/src/hmmer/easel -L ~/src/hmmer/easel -DeslNEON_BENCHMARK esl_neon.c -leasel -lm
290 */
291 #include <esl_config.h>
292
293 #include <stdio.h>
294 #include <math.h>
295
296 #include "easel.h"
297 #include "esl_getopts.h"
298 #include "esl_stopwatch.h"
299
300 static ESL_OPTIONS options[] = {
301 /* name type default env range toggles reqs incomp help docgroup*/
302 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
303 { "-N", eslARG_INT,"10000000", NULL, NULL, NULL, NULL, NULL, "number of trials", 0 },
304 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
305 };
306 static char usage[] = "[-options]";
307 static char banner[] = "benchmark driver for neon module";
308
309 int
310 main(int argc, char **argv)
311 {
312 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
313 ESL_STOPWATCH *w = esl_stopwatch_Create();
314 int N = esl_opt_GetInteger(go, "-N");
315 float origx = 2.0;
316 float x = origx;
317 esl_neon_128f_t xv = vdupq_n_f32(x);
318 int i;
319
320 /* First, serial time. */
321 esl_stopwatch_Start(w);
322 for (i = 0; i < N; i++) { x = logf(x); x = expf(x); }
323 esl_stopwatch_Stop(w);
324 esl_stopwatch_Display(stdout, w, "# serial CPU time: ");
325
326 /* Vector time */
327 esl_stopwatch_Start(w);
328 for (i = 0; i < N; i++) { xv = esl_neon_logf(xv); xv = esl_neon_expf(xv); }
329 esl_stopwatch_Stop(w);
330 esl_stopwatch_Display(stdout, w, "# vector CPU time: ");
331
332 /* If you don't do something with x and xv, the compiler may optimize them away */
333 printf("%g => many scalar logf,expf cycles => %g\n", origx, x);
334 printf("%g => many vector logf,expf cycles => ", origx);
335 esl_neon_dump_float(stdout, xv); printf("\n");
336
337 esl_stopwatch_Destroy(w);
338 esl_getopts_Destroy(go);
339 return 0;
340 }
341
342 #endif /*eslNEON_BENCHMARK*/
343
344
345 /*****************************************************************
346 * 4. Unit tests
347 *****************************************************************/
348 #ifdef eslNEON_TESTDRIVE
349
350 #include "esl_getopts.h"
351 #include "esl_random.h"
352
353 /* utest_logf(): Test range/domain of logf */
354 static void
355 utest_logf(ESL_GETOPTS *go)
356 {
357 esl_neon_128f_t x; /* test input */
358 union { esl_neon_128f_t v; float x[4]; } r; /* test output */
359
360 /* Test IEEE754 specials:
361 * log(-inf) = NaN log(x<0) = NaN log(-0) = NaN
362 * log(0) = -inf log(inf) = inf log(NaN) = NaN
363 */
364 float test1[4] = {-eslINFINITY, -1.0, -0.0, 0.0};
365 x.f32x4 = vld1q_f32(test1);
366 r.v = esl_neon_logf(x);
367 if (esl_opt_GetBoolean(go, "-v")) {
368 printf("logf");
369 esl_neon_dump_float(stdout, x); printf(" ==> ");
370 esl_neon_dump_float(stdout, r.v); printf("\n");
371 }
372
373 if (! isnan(r.x[0])) esl_fatal("logf(-inf) should be NaN");
374 if (! isnan(r.x[1])) esl_fatal("logf(-1) should be NaN");
375 if (! isnan(r.x[2])) esl_fatal("logf(-0) should be NaN");
376 if (! (r.x[3] < 0 && isinf(r.x[3]))) esl_fatal("logf(0) should be -inf");
377
378 float test2[4] = {eslINFINITY, eslNaN, FLT_MIN, FLT_MAX};
379 x.f32x4 = vld1q_f32(test2);
380 r.v = esl_neon_logf(x);
381 if (esl_opt_GetBoolean(go, "-v")) {
382 printf("logf");
383 esl_neon_dump_float(stdout, x); printf(" ==> ");
384 esl_neon_dump_float(stdout, r.v); printf("\n");
385 }
386 if (! isinf(r.x[0])) esl_fatal("logf(inf) should be inf");
387 if (! isnan(r.x[1])) esl_fatal("logf(NaN) should be NaN");
388
389 }
390
391 /* utest_expf(): Test range/domain of expf */
392 static void
393 utest_expf(ESL_GETOPTS *go)
394 {
395 esl_neon_128f_t x; /* test input */
396 union { esl_neon_128f_t v; float x[4]; } r; /* test output */
397
398 /* exp(-inf) = 0 exp(-0) = 1 exp(0) = 1 exp(inf) = inf exp(NaN) = NaN */
399 float test1[4] = {-eslINFINITY, -0.0, 0.0, eslINFINITY};
400 x.f32x4 = vld1q_f32(test1);
401 r.v = esl_neon_expf(x);
402 if (esl_opt_GetBoolean(go, "-v")) {
403 printf("expf");
404 esl_neon_dump_float(stdout, x); printf(" ==> ");
405 esl_neon_dump_float(stdout, r.v); printf("\n");
406 }
407 if (r.x[0] != 0.0f) esl_fatal("expf(-inf) should be 0");
408 if (r.x[1] != 1.0f) esl_fatal("expf(-0) should be 1");
409 if (r.x[2] != 1.0f) esl_fatal("expf(0) should be 1");
410 if (! isinf(r.x[3])) esl_fatal("expf(inf) should be inf");
411
412 /* exp(NaN) = NaN exp(large) = inf exp(-large) = 0 exp(1) = exp(1) */
413 float test2[4] = {eslNaN, 666.0f, -666.0f, 1.0f};
414 x.f32x4 = vld1q_f32(test2);
415 r.v = esl_neon_expf(x);
416 if (esl_opt_GetBoolean(go, "-v")) {
417 printf("expf");
418 esl_neon_dump_float(stdout, x); printf(" ==> ");
419 esl_neon_dump_float(stdout, r.v); printf("\n");
420 }
421 if (! isnan(r.x[0])) esl_fatal("expf(NaN) should be NaN");
422 if (! isinf(r.x[1])) esl_fatal("expf(large x) should be inf");
423 if (r.x[2] != 0.0f) esl_fatal("expf(-large x) should be 0");
424
425 /* Make sure we are correct around the problematic ~minlogf boundary:
426 * (1) e^x for x < -127.5 log2 + epsilon is 0, because that's our minlogf barrier.
427 * (2) e^x for -127.5 log2 < x < -126.5 log2 is 0 too, but is actually calculated
428 * (3) e^x for -126.5 log2 < x should be finite (and close to FLT_MIN)
429 *
430 * minlogf = -127.5 log(2) + epsilon = -88.3762626647949;
431 * and -126.5 log(2) = -87.68311834
432 * so for
433 * (1): expf(-88.3763) => 0
434 * (2): expf(-88.3762) => 0
435 * (3): expf(-87.6832) => 0
436 * (4): expf(-87.6831) => <FLT_MIN (subnormal) : ~8.31e-39 (may become 0 in flush-to-zero mode for subnormals)
437 */
438 float test3[4] = {-87.6831, -87.6832, -88.3762, -88.3763};
439 x.f32x4 = vld1q_f32(test3);
440
441
442 r.v = esl_neon_expf(x);
443 if (esl_opt_GetBoolean(go, "-v")) {
444 printf("expf");
445 esl_neon_dump_float(stdout, x); printf(" ==> ");
446 esl_neon_dump_float(stdout, r.v); printf("\n");
447 }
448 if ( r.x[0] >= FLT_MIN) esl_fatal("expf( -126.5 log2 + eps) should be around FLT_MIN");
449 if ( r.x[1] != 0.0f) esl_fatal("expf( -126.5 log2 - eps) should be 0.0 (by calculation)");
450 if ( r.x[2] != 0.0f) esl_fatal("expf( -127.5 log2 + eps) should be 0.0 (by calculation)");
451 if ( r.x[3] != 0.0f) esl_fatal("expf( -127.5 log2 - eps) should be 0.0 (by min bound): %g", r.x[0]);
452 }
453
454 /* utest_odds(): test accuracy of logf, expf on odds ratios,
455 * our main intended use.
456 */
457 static void
458 utest_odds(ESL_GETOPTS *go, ESL_RANDOMNESS *r)
459 {
460 int N = esl_opt_GetInteger(go, "-N");
461 int verbose = esl_opt_GetBoolean(go, "-v");
462 int very_verbose = esl_opt_GetBoolean(go, "--vv");
463 int i;
464 float p1, p2, odds;
465 union { esl_neon_128f_t v; float x[4]; } r1;
466 union { esl_neon_128f_t v; float x[4]; } r2;
467 float scalar_r1, scalar_r2;
468 double err1, maxerr1 = 0.0, avgerr1 = 0.0; /* errors on logf() */
469 double err2, maxerr2 = 0.0, avgerr2 = 0.0; /* errors on expf() */
470
471 for (i = 0; i < N; i++)
472 {
473 p1 = esl_rnd_UniformPositive(r);
474 p2 = esl_rnd_UniformPositive(r);
475 odds = p1 / p2;
476
477 if (odds == 0.0) esl_fatal("whoa, odds ratio can't be 0!\n");
478 esl_neon_128f_t tmp;
479 tmp.f32x4 = vdupq_n_f32(odds);
480 r1.v = esl_neon_logf(tmp); /* r1.x[z] = log(p1/p2) */
481 scalar_r1 = log(odds);
482
483 err1 = (r1.x[0] == 0. && scalar_r1 == 0.) ? 0.0 : 2 * fabs(r1.x[0] - scalar_r1) / fabs(r1.x[0] + scalar_r1);
484 if (err1 > maxerr1) maxerr1 = err1;
485 avgerr1 += err1 / (float) N;
486 if (isnan(avgerr1)) esl_fatal("whoa, what?\n");
487
488 r2.v = esl_neon_expf(r1.v); /* and back to odds */
489 scalar_r2 = exp(r1.x[0]);
490
491 err2 = (r2.x[0] == 0. && scalar_r2 == 0.) ? 0.0 : 2 * fabs(r2.x[0] - scalar_r2) / fabs(r2.x[0] + scalar_r2);
492 if (err2 > maxerr2) maxerr2 = err2;
493 avgerr2 += err2 / (float) N;
494
495 if (very_verbose)
496 printf("%13.7g %13.7g %13.7g %13.7g %13.7g %13.7g %13.7g\n", odds, scalar_r1, r1.x[0], scalar_r2, r2.x[0], err1, err2);
497 }
498
499 if (verbose) {
500 printf("Average [max] logf() relative error in %d odds trials: %13.8g [%13.8g]\n", N, avgerr1, maxerr1);
501 printf("Average [max] expf() relative error in %d odds trials: %13.8g [%13.8g]\n", N, avgerr2, maxerr2);
502 printf("(random seed : %" PRIu32 ")\n", esl_randomness_GetSeed(r));
503 }
504
505 if (avgerr1 > 1e-8) esl_fatal("average error on logf() is intolerable\n");
506 if (maxerr1 > 1e-6) esl_fatal("maximum error on logf() is intolerable\n");
507 if (avgerr2 > 1e-8) esl_fatal("average error on expf() is intolerable\n");
508 if (maxerr2 > 1e-6) esl_fatal("maximum error on expf() is intolerable\n");
509 }
510
511
512 static void
513 utest_hmax_u8(ESL_RANDOMNESS *rng)
514 {
515 union { esl_neon_128i_t v; uint8_t x[16]; } u;
516 uint8_t r1, r2;
517 int i,z;
518
519 for (i = 0; i < 100; i++)
520 {
521 r1 = 0;
522 for (z = 0; z < 16; z++)
523 {
524 u.x[z] = (uint8_t) (esl_rnd_Roll(rng, 256)); // 0..255
525 if (u.x[z] > r1) r1 = u.x[z];
526 }
527 r2 = esl_neon_hmax_u8(u.v);
528 if (r1 != r2) esl_fatal("hmax_u8 utest failed");
529 }
530 }
531
532 static void
533 utest_hmax_s8(ESL_RANDOMNESS *rng)
534 {
535 union { esl_neon_128i_t v; int8_t x[16]; } u;
536 int8_t r1, r2;
537 int i,z;
538
539 for (i = 0; i < 100; i++)
540 {
541 r1 = -128;
542 for (z = 0; z < 16; z++)
543 {
544 u.x[z] = (int8_t) (esl_rnd_Roll(rng, 256) - 128); // -128..127
545 if (u.x[z] > r1) r1 = u.x[z];
546 }
547 r2 = esl_neon_hmax_s8(u.v);
548 if (r1 != r2) esl_fatal("hmax_s8 utest failed");
549 }
550 }
551
552
553 static void
554 utest_hmax_s16(ESL_RANDOMNESS *rng)
555 {
556 union { esl_neon_128i_t v; int16_t x[8]; } u;
557 int16_t r1, r2;
558 int i,z;
559
560 for (i = 0; i < 100; i++)
561 {
562 r1 = -32768;
563 for (z = 0; z < 8; z++)
564 {
565 u.x[z] = (int16_t) (esl_rnd_Roll(rng, 65536) - 32768); // -32768..32767
566 if (u.x[z] > r1) r1 = u.x[z];
567 }
568 r2 = esl_neon_hmax_s16(u.v);
569 if (r1 != r2) esl_fatal("hmax_s16 utest failed: %d != %d", r1, r2);
570 }
571 }
572
573
574 static void
575 utest_hmax_f32(ESL_RANDOMNESS *rng)
576 {
577 union { esl_neon_128f_t v; float x[4]; } u;
578 float r1, r2;
579 int i,z;
580
581 for (i = 0; i < 100; i++)
582 {
583 r1 = 0;
584 for (z = 0; z < 4; z++)
585 {
586 u.x[z] = (float) esl_random(rng);
587 if (u.x[z] > r1) r1 = u.x[z];
588 }
589 r2 = esl_neon_hmax_f32(u.v);
590 if (r1 != r2) esl_fatal("hmax_f32 utest failed: %f != %f", r1, r2);
591 }
592 }
593
594
595 #endif /*eslNEON_TESTDRIVE*/
596
597
598
599
600 /*****************************************************************
601 * 5. Test driver
602 *****************************************************************/
603
604 #ifdef eslNEON_TESTDRIVE
605 /* gcc -mfpu=neon -g -Wall -o test -I. -L. -DeslNEON_TESTDRIVE esl_neon.c -leasel -lm
606 */
607 #include <esl_config.h>
608
609 #include <stdio.h>
610 #include <math.h>
611
612 #include "easel.h"
613 #include "esl_getopts.h"
614 #include "esl_random.h"
615
616
617 static ESL_OPTIONS options[] = {
618 /* name type default env range toggles reqs incomp help docgroup*/
619 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
620 { "-N", eslARG_INT, "10000", NULL, NULL, NULL, NULL, NULL, "number of random test points", 0 },
621 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
622 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose: show test report", 0 },
623 { "--vv", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be very verbose: show individual test samples", 0 },
624 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
625 };
626 static char usage[] = "[-options]";
627 static char banner[] = "test driver for neon module";
628
629 int
630 main(int argc, char **argv)
631 {
632 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
633 ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s"));;
634
635 fprintf(stderr, "## %s\n", argv[0]);
636 fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng));
637 #ifdef eslHAVE_NEON_AARCH64
638 fprintf(stderr, "# flavor = aarch64\n");
639 #else
640 fprintf(stderr, "# flavor = armv7\n");
641 #endif
642
643 utest_logf(go);
644 utest_expf(go);
645 utest_odds(go, rng);
646 utest_hmax_u8(rng);
647 utest_hmax_s8(rng);
648 utest_hmax_s16(rng);
649 utest_hmax_f32(rng);
650
651 fprintf(stderr, "# status = ok\n");
652
653 esl_randomness_Destroy(rng);
654 esl_getopts_Destroy(go);
655 return 0;
656 }
657 #endif /* eslNEON_TESTDRIVE*/
658
659
660
661
662 /*****************************************************************
663 * 6. Example
664 *****************************************************************/
665
666 #ifdef eslNEON_EXAMPLE
667 /*::cexcerpt::neon_example::begin::*/
668 /* gcc -mfpu=neon -g -Wall -o example -I. -L. -DeslNEON_EXAMPLE esl_neon.c -leasel -lm
669 */
670 #include <esl_config.h>
671
672 #include <stdio.h>
673 #include <math.h>
674
675 #include "easel.h"
676
677 int
678 main(int argc, char **argv)
679 {
680 float x; /* scalar input */
681 esl_neon_128f_t xv; /* input vector */
682 union { esl_neon_128f_t v; float x[4]; } rv; /* result vector*/
683
684 x = 2.0;
685 xv.f32x4 = vdupq_n_f32(x);
686 rv.v = esl_neon_logf(xv);
687 printf("logf(%f) = %f\n", x, rv.x[0]);
688
689 rv.v = esl_neon_expf(xv);
690 printf("expf(%f) = %f\n", x, rv.x[0]);
691
692 return 0;
693 }
694 /*::cexcerpt::neon_example::end::*/
695 #endif /* eslNEON_EXAMPLE */
696
697 #else // ! eslENABLE_NEON
698
699 /* If we don't have NEON compiled in, provide some nothingness to:
700 * a. prevent Mac OS/X ranlib from bitching about .o file that "has no symbols"
701 * b. prevent compiler from bitching about "empty compilation unit"
702 * c. compile blank drivers and automatically pass the automated tests.
703 */
704 void esl_neon_silence_hack(void) { return; }
705 #if defined eslNEON_TESTDRIVE || eslNEON_EXAMPLE || eslNEON_BENCHMARK
706 int main(void) { return 0; }
707 #endif
708
709 #endif // !eslENABLE_NEON or not
710
711
712
713 /*****************************************************************
714 * additional copyright and license information for this file
715 *****************************************************************
716 * In addition to our own copyrights, esl_neon_logf() and esl_neon_expf() are also:
717 * Copyright (C) 2007 Julien Pommier
718 * Copyright (C) 1992 Stephen Moshier
719 *
720 * These functions derived from zlib-licensed routines by
721 * Julien Pommier, http://gruntthepeon.free.fr/ssemath/. The
722 * zlib license:
723 *
724 *-------------------------------------------------------------------------
725 * Copyright (C) 2007 Julien Pommier
726 *
727 * This software is provided 'as-is', without any express or implied
728 * warranty. In no event will the authors be held liable for any damages
729 * arising from the use of this software.
730 *
731 * Permission is granted to anyone to use this software for any purpose,
732 * including commercial applications, and to alter it and redistribute it
733 * freely, subject to the following restrictions:
734 *
735 * 1. The origin of this software must not be misrepresented; you must not
736 * claim that you wrote the original software. If you use this software
737 * in a product, an acknowledgment in the product documentation would be
738 * appreciated but is not required.
739 * 2. Altered source versions must be plainly marked as such, and must not be
740 * misrepresented as being the original software.
741 * 3. This notice may not be removed or altered from any source distribution.
742 *
743 *-------------------------------------------------------------------------
744 *
745 * In turn, Pommier had derived the logf() and expf() functions from
746 * serial versions in the Cephes math library. According to its
747 * readme, Cephes is "copyrighted by the author" and "may be used
748 * freely but it comes with no support or guarantee." Cephes is
749 * available in NETLIB [http://www.netlib.org/cephes/]. NETLIB is
750 * widely considered to be a free scientific code repository, though
751 * the copyright and license status of many parts, including Cephes,
752 * is ill-defined. We have attached Moshier's copyright,
753 * to credit his original contribution. Thanks to both Pommier and
754 * Moshier for their clear code.
755 */