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

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

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