summaryrefslogtreecommitdiffhomepage
path: root/test/nxt_exp_approximation.c
blob: 1f79c068dd0e2948196719bf71a3d48d3e10ea76 (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
}