File: libm-bench.cpp

package info (click to toggle)
openlibm 0.6.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,744 kB
  • sloc: ansic: 26,129; asm: 1,160; pascal: 124; cpp: 108; makefile: 106
file content (144 lines) | stat: -rw-r--r-- 2,808 bytes parent folder | download | duplicates (7)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (C) Dahua Lin, 2014. Provided under the MIT license.

// Benchmark on libm functions

#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>


// Timing facilities

#ifdef __MACH__

#include <mach/mach_time.h>

class stimer
{
public:
    typedef uint64_t time_type;

    stimer()
    {
        ::mach_timebase_info(&m_baseinfo);
    }

    time_type current() const
    {
        return ::mach_absolute_time();
    }

    double span(const time_type& t0, const time_type& t1) const
    {
        uint64_t d = (m_baseinfo.numer * (t1 - t0)) / m_baseinfo.denom;
        return static_cast<double>(d) / 1.0e9;
    }

private:
    mach_timebase_info_data_t m_baseinfo;
};

#else

class stimer
{
public:
    typedef timespec time_type;

    time_type current() const
    {
        time_type t;
        ::clock_gettime(CLOCK_REALTIME, &t);
        return t;
    }

    double span(const time_type& t0, const time_type& t1) const
    {
        return double(t1.tv_sec - t0.tv_sec) +
            double(t1.tv_nsec - t0.tv_nsec) * 1.0e-9;
    }
};

#endif


inline double sec2mps(double s, long n)
{
    return n / (s * 1e6);
}


const long ARR_LEN = 1024;

double a[ARR_LEN];
double b[ARR_LEN];
double r[ARR_LEN];

#define TFUN1(FNAME) \
    void test_##FNAME(long n) { \
        for (int j = 0; j < ARR_LEN; ++j) r[j] = FNAME(a[j]); \
        stimer tm; \
        stimer::time_type t0 = tm.current(); \
        for(int i = 0; i < n; ++i) { \
            for (int j = 0; j < ARR_LEN; ++j) r[j] = FNAME(a[j]); \
        } \
        double s = tm.span(t0, tm.current()); \
        double mps = sec2mps(s, n * ARR_LEN); \
        printf("  %-8s:  %7.4f MPS\n", #FNAME, mps); }

#define TFUN2(FNAME) \
    void test_##FNAME(long n) { \
        for (int j = 0; j < ARR_LEN; ++j) r[j] = FNAME(a[j], b[j]); \
        stimer tm; \
        stimer::time_type t0 = tm.current(); \
        for(int i = 0; i < n; ++i) { \
            for (int j = 0; j < ARR_LEN; ++j) r[j] = FNAME(a[j], b[j]); \
        } \
        double s = tm.span(t0, tm.current()); \
        double mps = sec2mps(s, n * ARR_LEN); \
        printf("  %-8s:  %7.4f MPS\n", #FNAME, mps); }


#define TCALL(FNAME) test_##FNAME(20000)

// define benchmark functions

TFUN2(pow)
TFUN2(hypot)

TFUN1(exp)
TFUN1(log)
TFUN1(log10)
TFUN1(sin)
TFUN1(cos)
TFUN1(tan)
TFUN1(asin)
TFUN1(acos)
TFUN1(atan)
TFUN2(atan2)

int main(int argc, char *argv[])
{
    // initialize array contents
    for (int i = 0; i < ARR_LEN; ++i)
    {
      a[i] = rand() / (double) RAND_MAX;
      b[i] = rand() / (double) RAND_MAX;
    }

    TCALL(pow);
    TCALL(hypot);
    TCALL(exp);
    TCALL(log);
    TCALL(log10);
    TCALL(sin);
    TCALL(cos);
    TCALL(tan);
    TCALL(asin);
    TCALL(acos);
    TCALL(atan);
    TCALL(atan2);

    return 0;
}