Skip to content

Commit

Permalink
Explicitly specify the float type for numeric literals
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Jan 14, 2025
1 parent dfdfbc6 commit 24993bd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Average cycles per 32 elements
| logf_v | 25.90 | 244.19 | 9.4x |
| expf_v | 17.52 | 225.42 | 12.9x |
**Note**: Don't use these functions for small `n` because they are slow.
**Note**: Don't use these functions for small `n` (< 16) because they are slow.
# License
Expand Down
21 changes: 10 additions & 11 deletions test/log_v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ float putDiff(float begin, float end, float step, const F& f)

CYBOZU_TEST_AUTO(first)
{
fmath_init();
const size_t N = 8;
float x[N], y[N];
for (size_t i = 0; i < N; i++) {
x[i] = i*0.3+1;
x[i] = i*0.3f+1.0f;
y[i] = 0;
}
fmath::logf_v(y, x, N);
Expand All @@ -104,12 +103,12 @@ CYBOZU_TEST_AUTO(first)
CYBOZU_TEST_AUTO(setMaxE)
{
puts("fmath::logf_v");
putDiff(1, 2, 1e-6, fmath_logf_slow);
putDiff(2, 3, 1e-6, fmath_logf_slow);
putDiff(0.99, 1.01, 1e-6, fmath_logf_slow);
putDiff(1, 1.01, 1e-6, fmath_logf_slow);
putDiff(0.99, 1, 1e-6, fmath_logf_slow);
g_maxe = putDiff(1e-6, 4, 1e-6, fmath_logf_slow);
putDiff(1.0f, 2.0f, 1e-6f, fmath_logf_slow);
putDiff(2.0f, 3.0f, 1e-6f, fmath_logf_slow);
putDiff(0.99f, 1.01f, 1e-6f, fmath_logf_slow);
putDiff(1.0f, 1.01f, 1e-6f, fmath_logf_slow);
putDiff(0.99f, 1.0f, 1e-6f, fmath_logf_slow);
g_maxe = putDiff(1e-6f, 4.0f, 1e-6f, fmath_logf_slow);
printf("g_maxe=%e\n", g_maxe);
}

Expand Down Expand Up @@ -144,7 +143,7 @@ CYBOZU_TEST_AUTO(bench)
y1.resize(n);
const int C = 30000;
for (size_t i = 0; i < n; i++) {
x[i] = fabs(sin(i / double(n) * 7) * 20 + 1e-8);
x[i] = fabs(sin(i / float(n) * 7) * 20 + 1e-8f);
}
printf("for float x[%zd];\n", n);

Expand All @@ -162,7 +161,7 @@ CYBOZU_TEST_AUTO(bench)

CYBOZU_TEST_AUTO(limit)
{
float x[] = { -3, 0, FLT_MIN, 1e-8, 1, 1 + FLT_EPSILON, 1 - 1.0/3, 1 - 1e-5, 1 + 1.0/3, 1 + 1e-5, 2, 100, 1e8, FLT_MAX };
float x[] = { -3.0f, 0.0f, FLT_MIN, 1.0e-8f, 1.0f, 1.0f + FLT_EPSILON, 1.0f - 1.0f/3, 1.0f - 1e-5f, 1.0f + 1.0f/3, 1.0f + 1e-5f, 2.0f, 100.0f, 1e8f, FLT_MAX };
const size_t n = sizeof(x) / sizeof(x[0]);
float y0[n];
float y1[n];
Expand Down Expand Up @@ -191,7 +190,7 @@ void bench()

int main(int argc, char *argv[])
{
fmath_init();
// fmath_init();
if (argc > 1) {
bench();
return 0;
Expand Down
12 changes: 6 additions & 6 deletions test/reference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ if (t > g_maxt) g_maxt = t;
float logs = logc_tbl[idx].mlog;

#if logc_L == 3
float A = -.4999993134703166062199020;
float B = .3333377208103342588645233;
float C = -.2507196324449547040133221;
float D = .1983421366559079527220503;
float A = -.4999993134703166062199020f;
float B = .3333377208103342588645233f;
float C = -.2507196324449547040133221f;
float D = .1983421366559079527220503f;
float poly = fma(fma(fma(fma(D, t, C), t, B), t, A), t, 1.0f);
#else
float A = -.5000027035498107640250172;
float B = .3333281180811569374969424;
float A = -.5000027035498107640250172f;
float B = .3333281180811569374969424f;
float poly = fma(fma(B, t, A), t, 1.0f);
#endif
const float log2 = 0x1.62e430p-1f;
Expand Down

0 comments on commit 24993bd

Please sign in to comment.