Debian Codegraph @

easel/esl_sse.c

raw symbols SCIP
1 /* Vectorized routines for Intel/AMD processors, using Streaming SIMD Extensions (SSE).
2 *
3 * Contents:
4 * 1. SIMD logf(), expf()
5 * 2. Utilities for ps vectors (4 floats in a __m128)
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_SSE> or
14 * <eslENABLE_SSE4> was set in <esl_config.h> by the configure script,
15 * and that will only happen on x86 platforms. When neither
16 * <eslENABLE_SSE> nor <eslENABLE_SSE4> are set, we include some dummy
17 * code to silence compiler and ranlib warnings about empty
18 * translation units and no symbols, and dummy drivers that do nothing
19 * but declare success.
20 *
21 *****************************************************************
22 * Credits:
23 *
24 * The logf() and expf() routines are derivatives of routines by
25 * Julien Pommier [http://gruntthepeon.free.fr/ssemath/]. Those
26 * routines were in turn based on serial implementations in the Cephes
27 * math library by Stephen Moshier [Moshier89;
28 * http://www.moshier.net/#Cephes]. Thanks and credit to both Moshier
29 * and Pommier for their clear code. Additional copyright and license
30 * information is appended at the end of the file.
31 */
32 #include <esl_config.h>
33 #if defined(eslENABLE_SSE) || defined(eslENABLE_SSE4)
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <math.h>
38 #include <float.h>
39
40 #include <x86intrin.h>
41
42 #include "easel.h"
43 #include "esl_sse.h"
44
45
46 /*****************************************************************
47 * 1. SSE SIMD logf(), expf()
48 *****************************************************************/
49
50 /* Function: esl_sse_logf()
51 * Synopsis: <r[z] = log x[z]>
52 * Incept: SRE, Fri Dec 14 11:32:54 2007 [Janelia]
53 *
54 * Purpose: Given a vector <x> containing four floats, returns a
55 * vector <r> in which each element <r[z] = logf(x[z])>.
56 *
57 * Valid in the domain $x_z > 0$ for normalized IEEE754
58 * $x_z$.
59 *
60 * For <x> $< 0$, including -0, returns <NaN>. For <x> $==
61 * 0$ or subnormal <x>, returns <-inf>. For <x = inf>,
62 * returns <inf>. For <x = NaN>, returns <NaN>. For
63 * subnormal <x>, returns <-inf>.
64 *
65 * Xref: J2/71.
66 *
67 * Note: Derived from an SSE1 implementation by Julian
68 * Pommier. Converted to SSE2 and added handling
69 * of IEEE754 specials.
70 */
71 __m128
72 esl_sse_logf(__m128 x)
73 {
74 static float cephes_p[9] = { 7.0376836292E-2f, -1.1514610310E-1f, 1.1676998740E-1f,
75 -1.2420140846E-1f, 1.4249322787E-1f, -1.6668057665E-1f,
76 2.0000714765E-1f, -2.4999993993E-1f, 3.3333331174E-1f };
77 __m128 onev = _mm_set1_ps(1.0f); /* all elem = 1.0 */
78 __m128 v0p5 = _mm_set1_ps(0.5f); /* all elem = 0.5 */
79 __m128i vneg = _mm_set1_epi32(0x80000000); /* all elem have IEEE sign bit up */
80 __m128i vexp = _mm_set1_epi32(0x7f800000); /* all elem have IEEE exponent bits up */
81 __m128i ei;
82 __m128 e;
83 __m128 invalid_mask, zero_mask, inf_mask; /* masks used to handle special IEEE754 inputs */
84 __m128 mask;
85 __m128 origx;
86 __m128 tmp;
87 __m128 y;
88 __m128 z;
89
90 /* first, split x apart: x = frexpf(x, &e); */
91 ei = _mm_srli_epi32( _mm_castps_si128(x), 23); /* shift right 23: IEEE754 floats: ei = biased exponents */
92 invalid_mask = _mm_castsi128_ps ( _mm_cmpeq_epi32( _mm_and_si128(_mm_castps_si128(x), vneg), vneg)); /* mask any elem that's negative; these become NaN */
93 zero_mask = _mm_castsi128_ps ( _mm_cmpeq_epi32(ei, _mm_setzero_si128())); /* mask any elem zero or subnormal; these become -inf */
94 inf_mask = _mm_castsi128_ps ( _mm_cmpeq_epi32( _mm_and_si128(_mm_castps_si128(x), vexp), vexp)); /* mask any elem inf or NaN; log(inf)=inf, log(NaN)=NaN */
95 origx = x; /* store original x, used for log(inf) = inf, log(NaN) = NaN */
96
97 x = _mm_and_ps(x, _mm_castsi128_ps(_mm_set1_epi32(~0x7f800000))); /* x now the stored 23 bits of the 24-bit significand */
98 x = _mm_or_ps (x, v0p5); /* sets hidden bit b[0] */
99
100 ei = _mm_sub_epi32(ei, _mm_set1_epi32(126)); /* -127 (ei now signed base-2 exponent); then +1 */
101 e = _mm_cvtepi32_ps(ei);
102
103 /* now, calculate the log */
104 mask = _mm_cmplt_ps(x, _mm_set1_ps(0.707106781186547524f)); /* avoid conditional branches. */
105 tmp = _mm_and_ps(x, mask); /* tmp contains x values < 0.707, else 0 */
106 x = _mm_sub_ps(x, onev);
107 e = _mm_sub_ps(e, _mm_and_ps(onev, mask));
108 x = _mm_add_ps(x, tmp);
109 z = _mm_mul_ps(x,x);
110
111 y = _mm_set1_ps(cephes_p[0]); y = _mm_mul_ps(y, x);
112 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[1])); y = _mm_mul_ps(y, x);
113 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[2])); y = _mm_mul_ps(y, x);
114 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[3])); y = _mm_mul_ps(y, x);
115 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[4])); y = _mm_mul_ps(y, x);
116 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[5])); y = _mm_mul_ps(y, x);
117 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[6])); y = _mm_mul_ps(y, x);
118 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[7])); y = _mm_mul_ps(y, x);
119 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[8])); y = _mm_mul_ps(y, x);
120 y = _mm_mul_ps(y, z);
121
122 tmp = _mm_mul_ps(e, _mm_set1_ps(-2.12194440e-4f));
123 y = _mm_add_ps(y, tmp);
124
125 tmp = _mm_mul_ps(z, v0p5);
126 y = _mm_sub_ps(y, tmp);
127
128 tmp = _mm_mul_ps(e, _mm_set1_ps(0.693359375f));
129 x = _mm_add_ps(x, y);
130 x = _mm_add_ps(x, tmp);
131
132 /* IEEE754 cleanup: */
133 x = esl_sse_select_ps(x, origx, inf_mask); /* log(inf)=inf; log(NaN) = NaN */
134 x = _mm_or_ps(x, invalid_mask); /* log(x<0, including -0,-inf) = NaN */
135 x = esl_sse_select_ps(x, _mm_set1_ps(-eslINFINITY), zero_mask); /* x zero or subnormal = -inf */
136 return x;
137 }
138
139 /* Function: esl_sse_expf()
140 * Synopsis: <r[z] = exp x[z]>
141 * Incept: SRE, Fri Dec 14 14:46:27 2007 [Janelia]
142 *
143 * Purpose: Given a vector <x> containing four floats, returns a
144 * vector <r> in which each element <r[z] = expf(x[z])>.
145 *
146 * Valid for all IEEE754 floats $x_z$.
147 *
148 * Xref: J2/71
149 * J10/62: bugfix, minlogf/maxlogf range was too wide;
150 * (k+127) must be >=0 and <=255, so (k+127)<<23
151 * is a valid IEEE754 float, without touching
152 * the sign bit. Pommier had this right in the
153 * first place, and I didn't understand.
154 *
155 * Note: Derived from an SSE1 implementation by Julian
156 * Pommier. Converted to SSE2.
157 *
158 * Note on maxlogf/minlogf, which are close to but not
159 * exactly 127.5/log2 [J10/63]. We need -127<=k<=128, so
160 * k+127 is 0..255, a valid IEEE754 8-bit exponent
161 * (0..255), so the bit pattern (k+127)<<23 is IEEE754
162 * single-precision for 2^k. If k=-127, we get IEEE754 0.
163 * If k=128, we get IEEE754 +inf. If k<-127, k+127 is
164 * negative and we get screwed up. If k>128, k+127
165 * overflows the 8-bit exponent and sets the sign bit. So
166 * for x' (base 2) < -127.5 we must definitely return e^x ~
167 * 0; for x' < 126.5 we're going to calculate 0 anyway
168 * (because k=floor(-126.5-epsilon+0.5) = -127). So any
169 * minlogf between -126.5 log2 ... -127.5 log2 will suffice
170 * as the cutoff. Ditto for 126.5 log2 .. 127.5log2.
171 * That's 87.68312 .. 88.3762655. I think Pommier's
172 * thinking is, you don't want to get to close to the
173 * edges, lest fp roundoff error screw you (he may have
174 * consider 1 ulp carefully, I can't tell), but otherwise
175 * you may as well put your bounds close to the outer edge;
176 * so
177 * maxlogf = 127.5 log(2) - epsilon
178 * minlogf = -127.5 log(2) + epsilon
179 * for an epsilon that happen to be ~ 3e-6.
180 */
181 __m128
182 esl_sse_expf(__m128 x)
183 {
184 static float cephes_p[6] = { 1.9875691500E-4f, 1.3981999507E-3f, 8.3334519073E-3f,
185 4.1665795894E-2f, 1.6666665459E-1f, 5.0000001201E-1f };
186 static float cephes_c[2] = { 0.693359375f, -2.12194440e-4f };
187 static float maxlogf = 88.3762626647949f; /* 127.5 log(2) - epsilon. above this, 0.5+x/log2 gives k>128 and breaks 2^k "float" construction, because (k+127)<<23 must be a valid IEEE754 exponent 0..255 */
188 static float minlogf = -88.3762626647949f; /*-127.5 log(2) + epsilon. below this, 0.5+x/log2 gives k<-127 and breaks 2^k, see above */
189 __m128i k;
190 __m128 mask, tmp, fx, z, y, minmask, maxmask;
191
192 /* handle out-of-range and special conditions */
193 maxmask = _mm_cmpgt_ps(x, _mm_set1_ps(maxlogf));
194 minmask = _mm_cmple_ps(x, _mm_set1_ps(minlogf));
195
196 /* range reduction: exp(x) = 2^k e^f = exp(f + k log 2); k = floorf(0.5 + x / log2): */
197 fx = _mm_mul_ps(x, _mm_set1_ps(eslCONST_LOG2R));
198 fx = _mm_add_ps(fx, _mm_set1_ps(0.5f));
199
200 /* floorf() with SSE: */
201 k = _mm_cvttps_epi32(fx); /* cast to int with truncation */
202 tmp = _mm_cvtepi32_ps(k); /* cast back to float */
203 mask = _mm_cmpgt_ps(tmp, fx); /* if it increased (i.e. if it was negative...) */
204 mask = _mm_and_ps(mask, _mm_set1_ps(1.0f)); /* ...without a conditional branch... */
205 fx = _mm_sub_ps(tmp, mask); /* then subtract one. */
206 k = _mm_cvttps_epi32(fx); /* k is now ready for the 2^k part. */
207
208 /* polynomial approx for e^f for f in range [-0.5, 0.5] */
209 tmp = _mm_mul_ps(fx, _mm_set1_ps(cephes_c[0]));
210 z = _mm_mul_ps(fx, _mm_set1_ps(cephes_c[1]));
211 x = _mm_sub_ps(x, tmp);
212 x = _mm_sub_ps(x, z);
213 z = _mm_mul_ps(x, x);
214
215 y = _mm_set1_ps(cephes_p[0]); y = _mm_mul_ps(y, x);
216 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[1])); y = _mm_mul_ps(y, x);
217 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[2])); y = _mm_mul_ps(y, x);
218 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[3])); y = _mm_mul_ps(y, x);
219 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[4])); y = _mm_mul_ps(y, x);
220 y = _mm_add_ps(y, _mm_set1_ps(cephes_p[5])); y = _mm_mul_ps(y, z);
221 y = _mm_add_ps(y, x);
222 y = _mm_add_ps(y, _mm_set1_ps(1.0f));
223
224 /* build 2^k by hand, by creating a IEEE754 float */
225 k = _mm_add_epi32(k, _mm_set1_epi32(127));
226 k = _mm_slli_epi32(k, 23);
227 fx = _mm_castsi128_ps(k);
228
229 /* put 2^k e^f together (fx = 2^k, y = e^f) and we're done */
230 y = _mm_mul_ps(y, fx);
231
232 /* special/range cleanup */
233 y = esl_sse_select_ps(y, _mm_set1_ps(eslINFINITY), maxmask); /* exp(x) = inf for x > log(2^128) */
234 y = esl_sse_select_ps(y, _mm_set1_ps(0.0f), minmask); /* exp(x) = 0 for x < log(2^-149) */
235 return y;
236 }
237
238
239 /*****************************************************************
240 * 2. Utilities for ps vectors (4 floats in a __m128)
241 *****************************************************************/
242
243 void
244 esl_sse_dump_ps(FILE *fp, __m128 v)
245 {
246 float *p = (float *)&v;
247 fprintf(fp, "[%13.8g, %13.8g, %13.8g, %13.8g]", p[0], p[1], p[2], p[3]);
248 }
249
250
251
252
253 /*****************************************************************
254 * 3. Benchmark
255 *****************************************************************/
256 #ifdef eslSSE_BENCHMARK
257
258 #include <esl_config.h>
259
260 #include <stdio.h>
261 #include <math.h>
262
263 #include "easel.h"
264 #include "esl_getopts.h"
265 #include "esl_stopwatch.h"
266
267 static ESL_OPTIONS options[] = {
268 /* name type default env range toggles reqs incomp help docgroup*/
269 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
270 { "-N", eslARG_INT,"10000000", NULL, NULL, NULL, NULL, NULL, "number of trials", 0 },
271 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
272 };
273 static char usage[] = "[-options]";
274 static char banner[] = "benchmark driver for sse module";
275
276 int
277 main(int argc, char **argv)
278 {
279 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
280 ESL_STOPWATCH *w = esl_stopwatch_Create();
281 int N = esl_opt_GetInteger(go, "-N");
282 float origx = 2.0;
283 float x = origx;
284 __m128 xv = _mm_set1_ps(x);
285 int i;
286
287 /* First, serial time. */
288 esl_stopwatch_Start(w);
289 for (i = 0; i < N; i++) { x = logf(x); x = expf(x); }
290 esl_stopwatch_Stop(w);
291 esl_stopwatch_Display(stdout, w, "# serial CPU time: ");
292
293 /* Vector time */
294 esl_stopwatch_Start(w);
295 for (i = 0; i < N; i++) { xv = esl_sse_logf(xv); xv = esl_sse_expf(xv); }
296 esl_stopwatch_Stop(w);
297 esl_stopwatch_Display(stdout, w, "# vector CPU time: ");
298
299 /* If you don't do something with x and xv, the compiler may optimize them away */
300 printf("%g => many scalar logf,expf cycles => %g\n", origx, x);
301 printf("%g => many vector logf,expf cycles => ", origx);
302 esl_sse_dump_ps(stdout, xv); printf("\n");
303
304 esl_stopwatch_Destroy(w);
305 esl_getopts_Destroy(go);
306 return 0;
307 }
308
309 #endif /*eslSSE_BENCHMARK*/
310
311
312 /*****************************************************************
313 * 4. Unit tests
314 *****************************************************************/
315 #ifdef eslSSE_TESTDRIVE
316
317 #include "esl_getopts.h"
318 #include "esl_random.h"
319
320 /* utest_logf(): Test range/domain of logf */
321 static void
322 utest_logf(ESL_GETOPTS *go)
323 {
324 __m128 x; /* test input */
325 union { __m128 v; float x[4]; } r; /* test output */
326
327 /* Test IEEE754 specials:
328 * log(-inf) = NaN log(x<0) = NaN log(-0) = NaN
329 * log(0) = -inf log(inf) = inf log(NaN) = NaN
330 */
331 x = _mm_set_ps(0.0, -0.0, -1.0, -eslINFINITY); /* set_ps() is in order 3 2 1 0 */
332 r.v = esl_sse_logf(x);
333 if (esl_opt_GetBoolean(go, "-v")) {
334 printf("logf");
335 esl_sse_dump_ps(stdout, x); printf(" ==> ");
336 esl_sse_dump_ps(stdout, r.v); printf("\n");
337 }
338 if (! isnan(r.x[0])) esl_fatal("logf(-inf) should be NaN");
339 if (! isnan(r.x[1])) esl_fatal("logf(-1) should be NaN");
340 if (! isnan(r.x[2])) esl_fatal("logf(-0) should be NaN");
341 if (! (r.x[3] < 0 && isinf(r.x[3]))) esl_fatal("logf(0) should be -inf");
342
343 x = _mm_set_ps(FLT_MAX, FLT_MIN, eslNaN, eslINFINITY);
344 r.v = esl_sse_logf(x);
345 if (esl_opt_GetBoolean(go, "-v")) {
346 printf("logf");
347 esl_sse_dump_ps(stdout, x); printf(" ==> ");
348 esl_sse_dump_ps(stdout, r.v); printf("\n");
349 }
350 if (! isinf(r.x[0])) esl_fatal("logf(inf) should be inf");
351 if (! isnan(r.x[1])) esl_fatal("logf(NaN) should be NaN");
352
353 }
354
355 /* utest_expf(): Test range/domain of expf */
356 static void
357 utest_expf(ESL_GETOPTS *go)
358 {
359 __m128 x; /* test input */
360 union { __m128 v; float x[4]; } r; /* test output */
361
362 /* exp(-inf) = 0 exp(-0) = 1 exp(0) = 1 exp(inf) = inf exp(NaN) = NaN */
363 x = _mm_set_ps(eslINFINITY, 0.0, -0.0, -eslINFINITY); /* set_ps() is in order 3 2 1 0 */
364 r.v = esl_sse_expf(x);
365 if (esl_opt_GetBoolean(go, "-v")) {
366 printf("expf");
367 esl_sse_dump_ps(stdout, x); printf(" ==> ");
368 esl_sse_dump_ps(stdout, r.v); printf("\n");
369 }
370 if (r.x[0] != 0.0f) esl_fatal("expf(-inf) should be 0");
371 if (r.x[1] != 1.0f) esl_fatal("expf(-0) should be 1");
372 if (r.x[2] != 1.0f) esl_fatal("expf(0) should be 1");
373 if (! isinf(r.x[3])) esl_fatal("expf(inf) should be inf");
374
375 /* exp(NaN) = NaN exp(large) = inf exp(-large) = 0 exp(1) = exp(1) */
376 x = _mm_set_ps(1.0f, -666.0f, 666.0f, eslNaN); /* set_ps() is in order 3 2 1 0 */
377 r.v = esl_sse_expf(x);
378 if (esl_opt_GetBoolean(go, "-v")) {
379 printf("expf");
380 esl_sse_dump_ps(stdout, x); printf(" ==> ");
381 esl_sse_dump_ps(stdout, r.v); printf("\n");
382 }
383 if (! isnan(r.x[0])) esl_fatal("expf(NaN) should be NaN");
384 if (! isinf(r.x[1])) esl_fatal("expf(large x) should be inf");
385 if (r.x[2] != 0.0f) esl_fatal("expf(-large x) should be 0");
386
387 /* Make sure we are correct around the problematic ~minlogf boundary:
388 * (1) e^x for x < -127.5 log2 + epsilon is 0, because that's our minlogf barrier.
389 * (2) e^x for -127.5 log2 < x < -126.5 log2 is 0 too, but is actually calculated
390 * (3) e^x for -126.5 log2 < x should be finite (and close to FLT_MIN)
391 *
392 * minlogf = -127.5 log(2) + epsilon = -88.3762626647949;
393 * and -126.5 log(2) = -87.68311834
394 * so for
395 * (1): expf(-88.3763) => 0
396 * (2): expf(-88.3762) => 0
397 * (3): expf(-87.6832) => 0
398 * (4): expf(-87.6831) => <FLT_MIN (subnormal) : ~8.31e-39 (may become 0 in flush-to-zero mode for subnormals)
399 */
400 x = _mm_set_ps(-88.3763, -88.3762, -87.6832, -87.6831);
401 r.v = esl_sse_expf(x);
402 if (esl_opt_GetBoolean(go, "-v")) {
403 printf("expf");
404 esl_sse_dump_ps(stdout, x); printf(" ==> ");
405 esl_sse_dump_ps(stdout, r.v); printf("\n");
406 }
407 if ( r.x[0] >= FLT_MIN) esl_fatal("expf( -126.5 log2 + eps) should be around FLT_MIN");
408 if ( r.x[1] != 0.0f) esl_fatal("expf( -126.5 log2 - eps) should be 0.0 (by calculation)");
409 if ( r.x[2] != 0.0f) esl_fatal("expf( -127.5 log2 + eps) should be 0.0 (by calculation)");
410 if ( r.x[3] != 0.0f) esl_fatal("expf( -127.5 log2 - eps) should be 0.0 (by min bound): %g", r.x[0]);
411 }
412
413 /* utest_odds(): test accuracy of logf, expf on odds ratios,
414 * our main intended use.
415 */
416 static void
417 utest_odds(ESL_GETOPTS *go, ESL_RANDOMNESS *r)
418 {
419 int N = esl_opt_GetInteger(go, "-N");
420 int verbose = esl_opt_GetBoolean(go, "-v");
421 int very_verbose = esl_opt_GetBoolean(go, "--vv");
422 int i;
423 float p1, p2, odds;
424 union { __m128 v; float x[4]; } r1;
425 union { __m128 v; float x[4]; } r2;
426 float scalar_r1, scalar_r2;
427 double err1, maxerr1 = 0.0, avgerr1 = 0.0; /* errors on logf() */
428 double err2, maxerr2 = 0.0, avgerr2 = 0.0; /* errors on expf() */
429
430 for (i = 0; i < N; i++)
431 {
432 p1 = esl_rnd_UniformPositive(r);
433 p2 = esl_rnd_UniformPositive(r);
434 odds = p1 / p2;
435
436 if (odds == 0.0) esl_fatal("whoa, odds ratio can't be 0!\n");
437
438 r1.v = esl_sse_logf(_mm_set1_ps(odds)); /* r1.x[z] = log(p1/p2) */
439 scalar_r1 = log(odds);
440
441 err1 = (r1.x[0] == 0. && scalar_r1 == 0.) ? 0.0 : 2 * fabs(r1.x[0] - scalar_r1) / fabs(r1.x[0] + scalar_r1);
442 if (err1 > maxerr1) maxerr1 = err1;
443 avgerr1 += err1 / (float) N;
444 if (isnan(avgerr1)) esl_fatal("whoa, what?\n");
445
446 r2.v = esl_sse_expf(r1.v); /* and back to odds */
447 scalar_r2 = exp(r1.x[0]);
448
449 err2 = (r2.x[0] == 0. && scalar_r2 == 0.) ? 0.0 : 2 * fabs(r2.x[0] - scalar_r2) / fabs(r2.x[0] + scalar_r2);
450 if (err2 > maxerr2) maxerr2 = err2;
451 avgerr2 += err2 / (float) N;
452
453 if (very_verbose)
454 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);
455 }
456
457 if (verbose) {
458 printf("Average [max] logf() relative error in %d odds trials: %13.8g [%13.8g]\n", N, avgerr1, maxerr1);
459 printf("Average [max] expf() relative error in %d odds trials: %13.8g [%13.8g]\n", N, avgerr2, maxerr2);
460 printf("(random seed : %" PRIu32 ")\n", esl_randomness_GetSeed(r));
461 }
462
463 if (avgerr1 > 1e-8) esl_fatal("average error on logf() is intolerable\n");
464 if (maxerr1 > 1e-6) esl_fatal("maximum error on logf() is intolerable\n");
465 if (avgerr2 > 1e-8) esl_fatal("average error on expf() is intolerable\n");
466 if (maxerr2 > 1e-6) esl_fatal("maximum error on expf() is intolerable\n");
467 }
468
469
470 static void
471 utest_hmax_epu8(ESL_RANDOMNESS *rng)
472 {
473 union { __m128i v; uint8_t x[16]; } u;
474 uint8_t r1, r2;
475 int i,z;
476
477 for (i = 0; i < 100; i++)
478 {
479 r1 = 0;
480 for (z = 0; z < 16; z++)
481 {
482 u.x[z] = (uint8_t) (esl_rnd_Roll(rng, 256)); // 0..255
483 if (u.x[z] > r1) r1 = u.x[z];
484 }
485 r2 = esl_sse_hmax_epu8(u.v);
486 if (r1 != r2) esl_fatal("hmax_epu8 utest failed");
487 }
488 }
489
490 static void
491 utest_hmax_epi8(ESL_RANDOMNESS *rng)
492 {
493 #ifdef eslENABLE_SSE4 // no-op if eslENABLE_SSE only
494 union { __m128i v; int8_t x[16]; } u;
495 int8_t r1, r2;
496 int i,z;
497
498 for (i = 0; i < 100; i++)
499 {
500 r1 = -128;
501 for (z = 0; z < 16; z++)
502 {
503 u.x[z] = (int8_t) (esl_rnd_Roll(rng, 256) - 128); // -128..127
504 if (u.x[z] > r1) r1 = u.x[z];
505 }
506 r2 = esl_sse_hmax_epi8(u.v);
507 if (r1 != r2) esl_fatal("hmax_epi8 utest failed");
508 }
509 #endif // eslENABLE_SSE4
510 }
511
512 static void
513 utest_hmax_epi16(ESL_RANDOMNESS *rng)
514 {
515 union { __m128i v; int16_t x[8]; } u;
516 int16_t r1, r2;
517 int i,z;
518
519 for (i = 0; i < 100; i++)
520 {
521 r1 = -32768;
522 for (z = 0; z < 8; z++)
523 {
524 u.x[z] = (int16_t) (esl_rnd_Roll(rng, 65536) - 32768); // -32768..32767
525 if (u.x[z] > r1) r1 = u.x[z];
526 }
527 r2 = esl_sse_hmax_epi16(u.v);
528 if (r1 != r2) esl_fatal("hmax_epi16 utest failed: %d != %d", r1, r2);
529 }
530 }
531
532 static void
533 utest_select_ps(ESL_RANDOMNESS *rng)
534 {
535 union { __m128 v; float x[4]; } a, b, mask, res;
536 int i, z;
537
538 for (i = 0; i < 100; i++)
539 {
540 for (z = 0; z < 4; z++)
541 {
542 a.x[z] = (float) esl_random(rng);
543 b.x[z] = (float) esl_random(rng);
544 mask.x[z] = (float) (esl_random(rng) > 0.5 ? 1.0 : 0.0);
545 }
546 mask.v = _mm_cmpeq_ps(mask.v, _mm_setzero_ps());
547 res.v = esl_sse_select_ps(a.v, b.v, mask.v);
548 for (z = 0; z < 4; z++)
549 {
550 if (mask.x[z] == 0 && res.x[z] != a.x[z])
551 esl_fatal("select_ps utest failed: %d != %d", res.x[z], a.x[z]);
552 else if (mask.x[z] == 1 && res.x[z] != b.x[z])
553 esl_fatal("select_ps utest failed: %d != %d", res.x[z], b.x[z]);
554 }
555 esl_sse_dump_ps(stdout, a.v);
556 }
557 }
558 #endif /*eslSSE_TESTDRIVE*/
559
560
561
562
563
564 /*****************************************************************
565 * 5. Test driver
566 *****************************************************************/
567
568 #ifdef eslSSE_TESTDRIVE
569
570 #include <esl_config.h>
571
572 #include <stdio.h>
573 #include <math.h>
574
575 #include "easel.h"
576 #include "esl_getopts.h"
577 #include "esl_random.h"
578 #include "esl_sse.h"
579
580 static ESL_OPTIONS options[] = {
581 /* name type default env range toggles reqs incomp help docgroup*/
582 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
583 { "-N", eslARG_INT, "10000", NULL, NULL, NULL, NULL, NULL, "number of random test points", 0 },
584 { "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
585 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose: show test report", 0 },
586 { "--vv", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be very verbose: show individual test samples", 0 },
587 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
588 };
589 static char usage[] = "[-options]";
590 static char banner[] = "test driver for sse module";
591
592 int
593 main(int argc, char **argv)
594 {
595 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
596 ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s"));;
597
598 fprintf(stderr, "## %s\n", argv[0]);
599 fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng));
600
601 utest_logf(go);
602 utest_expf(go);
603 utest_odds(go, rng);
604 utest_hmax_epu8(rng);
605 utest_hmax_epi8(rng);
606 utest_hmax_epi16(rng);
607 utest_select_ps(rng);
608
609 fprintf(stderr, "# status = ok\n");
610
611 esl_randomness_Destroy(rng);
612 esl_getopts_Destroy(go);
613 return 0;
614 }
615 #endif /* eslSSE_TESTDRIVE*/
616
617
618
619
620 /*****************************************************************
621 * 6. Example
622 *****************************************************************/
623
624 #ifdef eslSSE_EXAMPLE
625 /*::cexcerpt::sse_example::begin::*/
626 #include <esl_config.h>
627
628 #include <stdio.h>
629 #include <math.h>
630
631 #include "easel.h"
632 #include "esl_sse.h"
633
634 int
635 main(int argc, char **argv)
636 {
637 float x; /* scalar input */
638 __m128 xv; /* input vector */
639 union { __m128 v; float x[4]; } rv; /* result vector*/
640
641 x = 2.0;
642 xv = _mm_set1_ps(x);
643 rv.v = esl_sse_logf(xv);
644 printf("logf(%f) = %f\n", x, rv.x[0]);
645
646 rv.v = esl_sse_expf(xv);
647 printf("expf(%f) = %f\n", x, rv.x[0]);
648
649 return 0;
650 }
651 /*::cexcerpt::sse_example::end::*/
652 #endif /*eslSSE_EXAMPLE*/
653
654
655
656
657
658 #else // ! (eslENABLE_SSE || eslENABLE_SSE4)
659
660 /* If we don't have SSE compiled in, provide some nothingness to:
661 * a. prevent Mac OS/X ranlib from bitching about .o file that "has no symbols"
662 * b. prevent compiler from bitching about "empty compilation unit"
663 * c. compile blank drivers and automatically pass the automated tests.
664 */
665 void esl_sse_silence_hack(void) { return; }
666 #if defined eslSSE_TESTDRIVE || eslSSE_EXAMPLE || eslSSE_BENCHMARK
667 int main(void) { return 0; }
668 #endif
669 #endif // (eslENABLE_SSE || eslENABLE_SSE4) or not
670
671
672
673
674
675 /*****************************************************************
676 * additional copyright and license information for this file
677 *****************************************************************
678 * In addition to our own copyrights, esl_sse_logf() and esl_sse_expf() are also:
679 * Copyright (C) 2007 Julien Pommier
680 * Copyright (C) 1992 Stephen Moshier
681 *
682 * These functions derived from zlib-licensed routines by
683 * Julien Pommier, http://gruntthepeon.free.fr/ssemath/. The
684 * zlib license:
685 *
686 *-------------------------------------------------------------------------
687 * Copyright (C) 2007 Julien Pommier
688 *
689 * This software is provided 'as-is', without any express or implied
690 * warranty. In no event will the authors be held liable for any damages
691 * arising from the use of this software.
692 *
693 * Permission is granted to anyone to use this software for any purpose,
694 * including commercial applications, and to alter it and redistribute it
695 * freely, subject to the following restrictions:
696 *
697 * 1. The origin of this software must not be misrepresented; you must not
698 * claim that you wrote the original software. If you use this software
699 * in a product, an acknowledgment in the product documentation would be
700 * appreciated but is not required.
701 * 2. Altered source versions must be plainly marked as such, and must not be
702 * misrepresented as being the original software.
703 * 3. This notice may not be removed or altered from any source distribution.
704 *
705 *-------------------------------------------------------------------------
706 *
707 * In turn, Pommier had derived the logf() and expf() functions from
708 * serial versions in the Cephes math library. According to its
709 * readme, Cephes is "copyrighted by the author" and "may be used
710 * freely but it comes with no support or guarantee." Cephes is
711 * available in NETLIB [http://www.netlib.org/cephes/]. NETLIB is
712 * widely considered to be a free scientific code repository, though
713 * the copyright and license status of many parts, including Cephes,
714 * is ill-defined. We have attached Moshier's copyright,
715 * to credit his original contribution. Thanks to both Pommier and
716 * Moshier for their clear code.
717 */
718