| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 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 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 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; |
| 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); |
| 101 |
neginf_vector.f32x4 = vdupq_n_f32(-eslINFINITY); |
| 102 |
one.f32x4 = vdupq_n_f32(1); |
| 103 |
|
| 104 |
nan_mask.u32x4 = vcltq_f32(x.f32x4, vdupq_n_f32(0.0)); |
| 105 |
nan_mask.u32x4 = vorrq_u32(vceqq_u32(vreinterpretq_u32_f32(x.f32x4), |
| 106 |
vdupq_n_u32(negzero)), |
| 107 |
nan_mask.u32x4); |
| 108 |
|
| 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 |
|
| 122 |
poszero_mask.u32x4 = vceqq_f32(x.f32x4, zero_vector.f32x4); |
| 123 |
|
| 124 |
|
| 125 |
poszero_mask.u32x4 = vandq_u32(vmvnq_u32(nan_mask.u32x4), poszero_mask.u32x4); |
| 126 |
|
| 127 |
|
| 128 |
inf_mask.u32x4 = vceqq_f32(x.f32x4, inf_vector.f32x4); |
| 129 |
|
| 130 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 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 |
|
| 175 |
esl_neon_128f_t inf_mask_view, poszero_mask_view; |
| 176 |
neginf_vector.f32x4 = vdupq_n_f32(-eslINFINITY); |
| 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)); |
| 181 |
x = esl_neon_select_float(x, neginf_vector, poszero_mask_view); |
| 182 |
x = esl_neon_select_float(x, inf_vector, inf_mask_view); |
| 183 |
return x; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 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 |
|
| 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 |
|
| 211 |
fx.f32x4 = vmlaq_f32(vdupq_n_f32(0.5f), x.f32x4, vdupq_n_f32(c_cephes_LOG2EF)); |
| 212 |
|
| 213 |
|
| 214 |
tmp.f32x4 = vcvtq_f32_s32(vcvtq_s32_f32(fx.f32x4)); |
| 215 |
|
| 216 |
|
| 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 |
|
| 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 |
|
| 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); |
| 265 |
y = esl_neon_select_float(y, zero_vec, minmask_view); |
| 266 |
return y; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 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 |
|
| 286 |
|
| 287 |
#ifdef eslNEON_BENCHMARK |
| 288 |
|
| 289 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
#ifdef eslNEON_TESTDRIVE |
| 349 |
|
| 350 |
#include "esl_getopts.h" |
| 351 |
#include "esl_random.h" |
| 352 |
|
| 353 |
|
| 354 |
static void |
| 355 |
utest_logf(ESL_GETOPTS *go) |
| 356 |
{ |
| 357 |
esl_neon_128f_t x; |
| 358 |
union { esl_neon_128f_t v; float x[4]; } r; |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 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 |
|
| 392 |
static void |
| 393 |
utest_expf(ESL_GETOPTS *go) |
| 394 |
{ |
| 395 |
esl_neon_128f_t x; |
| 396 |
union { esl_neon_128f_t v; float x[4]; } r; |
| 397 |
|
| 398 |
|
| 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 |
|
| 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 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 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 |
|
| 455 |
|
| 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; |
| 469 |
double err2, maxerr2 = 0.0, avgerr2 = 0.0; |
| 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); |
| 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); |
| 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)); |
| 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); |
| 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); |
| 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 |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
#ifdef eslNEON_TESTDRIVE |
| 605 |
|
| 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 |
|
| 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 |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
#ifdef eslNEON_EXAMPLE |
| 667 |
|
| 668 |
|
| 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; |
| 681 |
esl_neon_128f_t xv; |
| 682 |
union { esl_neon_128f_t v; float x[4]; } rv; |
| 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 |
|
| 695 |
#endif |
| 696 |
|
| 697 |
#else |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 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 |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|