source: trunk/test/benchmark/trig.cpp @ 964

Last change on this file since 964 was 964, checked in by sam, 8 years ago

test: fix the benchmark suite compilation on SNC.

  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1//
2// Lol Engine - Benchmark program
3//
4// Copyright: (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#ifdef WIN32
16#   define _USE_MATH_DEFINES /* for M_PI */
17#   define WIN32_LEAN_AND_MEAN
18#   include <windows.h>
19#endif
20
21#include <cstdio>
22
23#if defined HAVE_FASTMATH_H
24#   include <fastmath.h>
25#endif
26
27#include "core.h"
28
29using namespace std;
30using namespace lol;
31
32static size_t const TRIG_TABLE_SIZE = 128 * 1024;
33static size_t const TRIG_RUNS = 50;
34
35void bench_trig(int mode)
36{
37    float result[12] = { 0.0f };
38    Timer timer;
39
40    /* Set up tables */
41    float *pf = new float[TRIG_TABLE_SIZE];
42    float *pf2 = new float[TRIG_TABLE_SIZE];
43    float *pf3 = new float[TRIG_TABLE_SIZE];
44
45    for (size_t run = 0; run < TRIG_RUNS; run++)
46    {
47        switch (mode)
48        {
49        case 1:
50            for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
51                pf[i] = RandF(-1e5f, 1e5f);
52            break;
53        case 2:
54            for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
55                pf[i] = RandF(-M_PI, M_PI);
56            break;
57        case 3:
58            for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
59                pf[i] = RandF(-1e-2f, 1e-2f);
60            break;
61        }
62
63        /* Sin */
64        timer.GetMs();
65        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
66#if defined __GNUC__ && !defined __SNC__
67            pf2[i] = __builtin_sinf(pf[i]);
68#else
69            pf2[i] = sinf(pf[i]);
70#endif
71        result[0] += timer.GetMs();
72
73        /* Fast sin */
74        timer.GetMs();
75        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
76#if defined HAVE_FASTMATH_H
77            pf2[i] = f_sinf(pf[i]);
78#else
79            pf2[i] = sinf(pf[i]);
80#endif
81        result[1] += timer.GetMs();
82
83        /* Lol sin */
84        timer.GetMs();
85        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
86            pf2[i] = lol_sin(pf[i]);
87        result[2] += timer.GetMs();
88
89        /* Cos */
90        timer.GetMs();
91        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
92#if defined __GNUC__ && !defined __SNC__
93            pf2[i] = __builtin_cosf(pf[i]);
94#else
95            pf2[i] = cosf(pf[i]);
96#endif
97        result[3] += timer.GetMs();
98
99        /* Fast cos */
100        timer.GetMs();
101        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
102#if defined HAVE_FASTMATH_H
103            pf2[i] = f_cosf(pf[i]);
104#else
105            pf2[i] = cosf(pf[i]);
106#endif
107        result[4] += timer.GetMs();
108
109        /* Lol cos */
110        timer.GetMs();
111        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
112            pf2[i] = lol_cos(pf[i]);
113        result[5] += timer.GetMs();
114
115        /* Sin & cos */
116        timer.GetMs();
117        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
118        {
119#if defined __GNUC__ && !defined __SNC__
120            pf2[i] = __builtin_sinf(pf[i]);
121            pf3[i] = __builtin_cosf(pf[i]);
122#else
123            pf2[i] = sinf(pf[i]);
124            pf3[i] = cosf(pf[i]);
125#endif
126        }
127        result[6] += timer.GetMs();
128
129        /* Fast sin & cos */
130        timer.GetMs();
131        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
132        {
133#if defined HAVE_FASTMATH_H
134            pf2[i] = f_sinf(pf[i]);
135            pf3[i] = f_cosf(pf[i]);
136#else
137            pf2[i] = sinf(pf[i]);
138            pf3[i] = cosf(pf[i]);
139#endif
140        }
141        result[7] += timer.GetMs();
142
143        /* Lol sincos */
144        timer.GetMs();
145        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
146            lol_sincos(pf[i], &pf2[i], &pf3[i]);
147        result[8] += timer.GetMs();
148
149        /* Tan */
150        timer.GetMs();
151        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
152#if defined __GNUC__ && !defined __SNC__
153            pf2[i] = __builtin_tanf(pf[i]);
154#else
155            pf2[i] = tanf(pf[i]);
156#endif
157        result[9] += timer.GetMs();
158
159        /* Fast tan */
160        timer.GetMs();
161        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
162#if defined HAVE_FASTMATH_H
163            pf2[i] = f_tanf(pf[i]);
164#else
165            pf2[i] = tanf(pf[i]);
166#endif
167        result[10] += timer.GetMs();
168
169        /* Lol tan */
170        timer.GetMs();
171        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
172            pf2[i] = lol_tan(pf[i]);
173        result[11] += timer.GetMs();
174    }
175
176    delete[] pf;
177    delete[] pf2;
178    delete[] pf3;
179
180    for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
181        result[i] *= 1000000.0f / (TRIG_TABLE_SIZE * TRIG_RUNS);
182
183    Log::Info("                              ns/elem\n");
184    Log::Info("float = sinf(float)          %7.3f\n", result[0]);
185    Log::Info("float = f_sinf(float)        %7.3f\n", result[1]);
186    Log::Info("float = lol_sin(float)       %7.3f\n", result[2]);
187    Log::Info("float = cosf(float)          %7.3f\n", result[3]);
188    Log::Info("float = f_cosf(float)        %7.3f\n", result[4]);
189    Log::Info("float = lol_cos(float)       %7.3f\n", result[5]);
190    Log::Info("float = sinf,cosf(float)     %7.3f\n", result[6]);
191    Log::Info("float = f_sinf,f_cosf(float) %7.3f\n", result[7]);
192    Log::Info("float = lol_sincos(float)    %7.3f\n", result[8]);
193    Log::Info("float = tanf(float)          %7.3f\n", result[9]);
194    Log::Info("float = f_tanf(float)        %7.3f\n", result[10]);
195    Log::Info("float = lol_tanf(float)      %7.3f\n", result[11]);
196}
197
Note: See TracBrowser for help on using the repository browser.