source: trunk/test/lol-bench.cpp @ 879

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

core: tune the half precision code so that the best variants are being
used on the PS3 platform.

  • Property svn:keywords set to Id
File size: 3.7 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#include <cstdio>
16
17#include "core.h"
18#include "loldebug.h"
19
20using namespace std;
21using namespace lol;
22
23static size_t const HALF_TABLE_SIZE = 1024 * 1024;
24static size_t const HALF_RUNS = 100;
25
26static void bench_half(int mode);
27
28int main(int argc, char **argv)
29{
30    Log::Info("-----------------------------------\n");
31    Log::Info("Half precision floats (random bits)\n");
32    Log::Info("-----------------------------------\n");
33    bench_half(1);
34
35    Log::Info("---------------------------------\n");
36    Log::Info("Half precision floats [-2.0, 2.0]\n");
37    Log::Info("---------------------------------\n");
38    bench_half(2);
39
40    return EXIT_SUCCESS;
41}
42
43static void bench_half(int mode)
44{
45    float result[8] = { 0.0f };
46    Timer timer;
47
48    /* Set up tables */
49    float *pf = new float[HALF_TABLE_SIZE];
50    half *ph = new half[HALF_TABLE_SIZE];
51
52    switch (mode)
53    {
54    case 1:
55        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
56            ph[i] = half::makebits(rand());
57        break;
58    case 2:
59        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
60            ph[i] = RandF(-2.0f, 2.0f);
61        break;
62    }
63
64    for (size_t run = 0; run < HALF_RUNS; run++)
65    {
66        /* Convert half to float (array) */
67        timer.GetMs();
68        half::convert(pf, ph, HALF_TABLE_SIZE);
69        result[0] += timer.GetMs();
70
71        /* Convert half to float (fast) */
72        timer.GetMs();
73        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
74            pf[i] = (float)ph[i];
75        result[1] += timer.GetMs();
76
77        /* Convert float to half (array) */
78        timer.GetMs();
79        half::convert(ph, pf, HALF_TABLE_SIZE);
80        result[2] += timer.GetMs();
81
82        /* Convert float to half (fast) */
83        timer.GetMs();
84        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
85            ph[i] = (half)pf[i];
86        result[3] += timer.GetMs();
87
88        /* Convert float to half (accurate) */
89        timer.GetMs();
90        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
91            ph[i] = half::makeaccurate(pf[i]);
92        result[4] += timer.GetMs();
93
94        /* Change sign of every half */
95        timer.GetMs();
96        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
97            ph[i] = -ph[i];
98        result[5] += timer.GetMs();
99
100        /* Add a half to every float */
101        timer.GetMs();
102        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
103            pf[i] += ph[i];
104        result[6] += timer.GetMs();
105
106        /* Add a float to every half */
107        timer.GetMs();
108        for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
109            ph[i] += pf[i];
110        result[7] += timer.GetMs();
111    }
112
113    delete[]pf;
114    delete[]ph;
115
116    for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
117        result[i] *= 1000000.0f / (HALF_TABLE_SIZE * HALF_RUNS);
118
119    Log::Info("                          ns/elem\n");
120    Log::Info("float[] = half[]         %7.3f\n", result[0]);
121    Log::Info("float = half             %7.3f\n", result[1]);
122    Log::Info("half[] = float[]         %7.3f\n", result[2]);
123    Log::Info("half = float (fast)      %7.3f\n", result[3]);
124    Log::Info("half = float (accurate)  %7.3f\n", result[4]);
125    Log::Info("half = -half             %7.3f\n", result[5]);
126    Log::Info("float += half            %7.3f\n", result[6]);
127    Log::Info("half += float            %7.3f\n", result[7]);
128}
129
Note: See TracBrowser for help on using the repository browser.