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

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

core: make timers second-based rather than millisecond-based.

  • Property svn:keywords set to Id
File size: 5.5 KB
RevLine 
[942]1//
2// Lol Engine - Benchmark program
3//
[1189]4// Copyright: (c) 2005-2012 Sam Hocevar <sam@hocevar.net>
[942]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
[1189]15#if defined _XBOX
[942]16#   define _USE_MATH_DEFINES /* for M_PI */
[1189]17#   include <xtl.h>
18#elif defined _WIN32
19#   define _USE_MATH_DEFINES /* for M_PI */
[942]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 */
[1309]67        timer.Get();
[942]68        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
[964]69#if defined __GNUC__ && !defined __SNC__
[942]70            pf2[i] = __builtin_sinf(pf[i]);
71#else
72            pf2[i] = sinf(pf[i]);
73#endif
[1309]74        result[0] += timer.Get();
[942]75
76        /* Fast sin */
[1309]77        timer.Get();
[942]78        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
[1082]79#if defined HAVE_FASTMATH_H && !defined __native_client__
[942]80            pf2[i] = f_sinf(pf[i]);
81#else
82            pf2[i] = sinf(pf[i]);
83#endif
[1309]84        result[1] += timer.Get();
[942]85
86        /* Lol sin */
[1309]87        timer.Get();
[942]88        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
89            pf2[i] = lol_sin(pf[i]);
[1309]90        result[2] += timer.Get();
[942]91
92        /* Cos */
[1309]93        timer.Get();
[942]94        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
[964]95#if defined __GNUC__ && !defined __SNC__
[942]96            pf2[i] = __builtin_cosf(pf[i]);
97#else
98            pf2[i] = cosf(pf[i]);
99#endif
[1309]100        result[3] += timer.Get();
[942]101
102        /* Fast cos */
[1309]103        timer.Get();
[942]104        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
[1082]105#if defined HAVE_FASTMATH_H && !defined __native_client__
[942]106            pf2[i] = f_cosf(pf[i]);
107#else
108            pf2[i] = cosf(pf[i]);
109#endif
[1309]110        result[4] += timer.Get();
[942]111
112        /* Lol cos */
[1309]113        timer.Get();
[942]114        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
115            pf2[i] = lol_cos(pf[i]);
[1309]116        result[5] += timer.Get();
[942]117
118        /* Sin & cos */
[1309]119        timer.Get();
[942]120        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
121        {
[964]122#if defined __GNUC__ && !defined __SNC__
[942]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        }
[1309]130        result[6] += timer.Get();
[942]131
132        /* Fast sin & cos */
[1309]133        timer.Get();
[942]134        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
135        {
[1082]136#if defined HAVE_FASTMATH_H && !defined __native_client__
[942]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        }
[1309]144        result[7] += timer.Get();
[942]145
146        /* Lol sincos */
[1309]147        timer.Get();
[942]148        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
149            lol_sincos(pf[i], &pf2[i], &pf3[i]);
[1309]150        result[8] += timer.Get();
[942]151
152        /* Tan */
[1309]153        timer.Get();
[942]154        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
[964]155#if defined __GNUC__ && !defined __SNC__
[942]156            pf2[i] = __builtin_tanf(pf[i]);
157#else
158            pf2[i] = tanf(pf[i]);
159#endif
[1309]160        result[9] += timer.Get();
[942]161
162        /* Fast tan */
[1309]163        timer.Get();
[942]164        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
[1082]165#if defined HAVE_FASTMATH_H && !defined __native_client__
[942]166            pf2[i] = f_tanf(pf[i]);
167#else
168            pf2[i] = tanf(pf[i]);
169#endif
[1309]170        result[10] += timer.Get();
[942]171
172        /* Lol tan */
[1309]173        timer.Get();
[942]174        for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
175            pf2[i] = lol_tan(pf[i]);
[1309]176        result[11] += timer.Get();
[942]177    }
178
179    delete[] pf;
180    delete[] pf2;
181    delete[] pf3;
182
183    for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
[1309]184        result[i] *= 1e9f / (TRIG_TABLE_SIZE * TRIG_RUNS);
[942]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.