diff options
author | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
commit | 16cbf3c076a0aca6d47adaf3f719493674cf2363 (patch) | |
tree | e6530480020f62a2bdbf249988ec3e2a751d3927 /test/nxt_exp_approximation.c | |
download | unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.gz unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.bz2 |
Initial version.
Diffstat (limited to 'test/nxt_exp_approximation.c')
-rw-r--r-- | test/nxt_exp_approximation.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/test/nxt_exp_approximation.c b/test/nxt_exp_approximation.c new file mode 100644 index 00000000..1f79c068 --- /dev/null +++ b/test/nxt_exp_approximation.c @@ -0,0 +1,72 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> +#include <math.h> + + +nxt_int_t +nxt_exp_approximation(nxt_thread_t *thr) +{ + double n, e0, e1, diff; + nxt_nsec_t start, end; + + nxt_thread_time_update(thr); + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "exp approximation unit test started"); + + for (n = 0.0; n > -20.0; n -= 0.00001) { + + e0 = nxt_event_conn_exponential_approximation(n); + e1 = exp(n); + + diff = fabs(e0 - e1); + + /* 0.028993 is max difference with libm exp(). */ + if (diff > 0.028993) { + nxt_log_alert(thr->log, + "exp approximation unit test failed: %0.6f %0.6f", + n, diff); + return NXT_ERROR; + } + } + + + nxt_thread_time_update(thr); + start = nxt_thread_monotonic_time(thr); + + e0 = 0; + for (n = 0.0; n > -20.0; n -= 0.00001) { + e0 += nxt_event_conn_exponential_approximation(n); + } + + nxt_thread_time_update(thr); + end = nxt_thread_monotonic_time(thr); + + /* e0 is passed but not output to eliminate optimization. */ + nxt_log_error(NXT_LOG_NOTICE, thr->log, "exp approximation: %0.1fns", + (end - start) / 20000000.0, e0); + + + nxt_thread_time_update(thr); + start = nxt_thread_monotonic_time(thr); + + e0 = 0; + for (n = 0.0; n > -20.0; n -= 0.000001) { + e0 += exp(n); + } + + nxt_thread_time_update(thr); + end = nxt_thread_monotonic_time(thr); + + /* e0 is passed but not output to eliminate optimization. */ + nxt_log_error(NXT_LOG_NOTICE, thr->log, "libm exp(): %0.1fns", + (end - start) / 20000000.0, e0); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "exp approximation unit test passed"); + return NXT_OK; +} |