source: trunk/test/benchmark/half.cpp @ 942

Last change on this file since 942 was 942, checked in by sam, 12 years ago

test: reorganise test suite and benchmark code.

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