Skip to content

Commit

Permalink
added simd_exp2 func and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolm committed Jan 19, 2024
1 parent bd33677 commit 4105129
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
27 changes: 27 additions & 0 deletions extra/simd_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,33 @@ simd_vector simd_exp(simd_vector x)
return y;
}

//----------------------------------------------------------------------------------------------------------------------
// based on https://github.com/jeremybarnes/cephes/blob/master/single/exp2f.c
simd_vector simd_exp2(simd_vector x)
{
// clamp values
x = simd_clamp(x, simd_splat(-127.f), simd_splat(127.f));
simd_vector equal_to_zero = simd_cmp_eq(x, simd_splat_zero());

simd_vector i0 = simd_floor(x);
x = simd_sub(x, i0);

simd_vector above_half = simd_cmp_gt(x, simd_splat(.5f));
simd_vector one = simd_splat(1.f);
i0 = simd_select(i0, simd_add(i0, one), above_half);
x = simd_select(x, simd_sub(x, one), above_half);

simd_vector px = simd_fmad(x, simd_splat(1.535336188319500E-004f), simd_splat(1.339887440266574E-003f));
px = simd_fmad(px, x, simd_splat(9.618437357674640E-003f));
px = simd_fmad(px, x, simd_splat(5.550332471162809E-002f));
px = simd_fmad(px, x, simd_splat(2.402264791363012E-001f));
px = simd_fmad(px, x, simd_splat(6.931472028550421E-001f));
px = simd_fmad(px, x, one);
px = simd_ldexp(px, i0);

return simd_select(px, one, equal_to_zero);
}

//----------------------------------------------------------------------------------------------------------------------
// based on http://gruntthepeon.free.fr/ssemath/
void simd_sincos(simd_vector x, simd_vector* s, simd_vector* c)
Expand Down
3 changes: 3 additions & 0 deletions extra/simd_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ simd_vector simd_log2(simd_vector x);
// max error : 1.108270880e-07
simd_vector simd_exp(simd_vector x);

// max error : 1.042427087e-07
simd_vector simd_exp2(simd_vector x);

// max error : 4.768371582e-07
simd_vector simd_cbrt(simd_vector x);

Expand Down
1 change: 1 addition & 0 deletions tests/test_simd_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ SUITE(exponentiation)
RUN_TESTp(generic_test, logf, simd_log, FLT_EPSILON, 1000.f, 1.e-06f, true, "simd_log");
RUN_TESTp(generic_test, log2f, simd_log2, FLT_EPSILON, 1.e20f, 3.e-07f, true, "simd_log2");
RUN_TESTp(generic_test, expf, simd_exp, -87.f, 87.f, 1.e-06f, true, "simd_exp");
RUN_TESTp(generic_test, exp2f, simd_exp2, -126.f, 126.f, 2.e-07f, true, "simd_exp2");
RUN_TESTp(generic_test, expf, simd_approx_exp, -87.f, 87.f, 2.e-03f, true, "simd_approx_exp");
RUN_TESTp(generic_test, cbrtf, simd_cbrt, -100.f, 100.f, 2.e-07f, true, "simd_cbrt");
}
Expand Down

0 comments on commit 4105129

Please sign in to comment.