-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathhalf_perf_test.cpp
More file actions
401 lines (336 loc) · 9.93 KB
/
half_perf_test.cpp
File metadata and controls
401 lines (336 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenEXR Project.
//
#ifdef _WIN32
# define _CRT_RAND_S
#endif
#include <ImathConfig.h>
#include <ImathRandom.h>
#include <half.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
# include <windows.h>
#else
# include <time.h>
#endif
#include <memory>
using namespace IMATH_NAMESPACE;
#ifdef IMATH_USE_HALF_LOOKUP_TABLE
static const unsigned short imath_float_half_exp_table[1 << 9] =
#include "eLut.h"
static inline float
table_half_cast (const half& h)
{
return imath_half_to_float_table[h.bits ()].f;
}
//-----------------------------------------------
// Overflow handler for float-to-half conversion;
// generates a hardware floating-point overflow,
// which may be trapped by the operating system.
//-----------------------------------------------
static float
half_overflow ()
{
float f = 1e10;
for (int i = 0; i < 10; i++)
f *= f; // this will overflow before the for loop terminates
return f;
}
//-----------------------------------------------------
// Float-to-half conversion -- general case, including
// zeroes, denormalized numbers and exponent overflows.
//-----------------------------------------------------
static uint16_t
long_convert (int i)
{
//
// Our floating point number, f, is represented by the bit
// pattern in integer i. Disassemble that bit pattern into
// the sign, s, the exponent, e, and the significand, m.
// Shift s into the position where it will go in in the
// resulting half number.
// Adjust e, accounting for the different exponent bias
// of float and half (127 versus 15).
//
int s = (i >> 16) & 0x00008000;
int e = ((i >> 23) & 0x000000ff) - (127 - 15);
int m = i & 0x007fffff;
//
// Now reassemble s, e and m into a half:
//
if (e <= 0)
{
if (e < -10)
{
//
// E is less than -10. The absolute value of f is
// less than HALF_DENORM_MIN (f may be a small normalized
// float, a denormalized float or a zero).
//
// We convert f to a half zero with the same sign as f.
//
return s;
}
//
// E is between -10 and 0. F is a normalized float
// whose magnitude is less than HALF_NRM_MIN.
//
// We convert f to a denormalized half.
//
//
// Add an explicit leading 1 to the significand.
//
m = m | 0x00800000;
//
// Round to m to the nearest (10+e)-bit value (with e between
// -10 and 0); in case of a tie, round to the nearest even value.
//
// Rounding may cause the significand to overflow and make
// our number normalized. Because of the way a half's bits
// are laid out, we don't have to treat this case separately;
// the code below will handle it correctly.
//
int t = 14 - e;
int a = (1 << (t - 1)) - 1;
int b = (m >> t) & 1;
m = (m + a + b) >> t;
//
// Assemble the half from s, e (zero) and m.
//
return s | m;
}
else if (e == 0xff - (127 - 15))
{
if (m == 0)
{
//
// F is an infinity; convert f to a half
// infinity with the same sign as f.
//
return s | 0x7c00;
}
else
{
//
// F is a NAN; we produce a half NAN that preserves
// the sign bit and the 10 leftmost bits of the
// significand of f, with one exception: If the 10
// leftmost bits are all zero, the NAN would turn
// into an infinity, so we have to set at least one
// bit in the significand.
//
m >>= 13;
return s | 0x7c00 | m | (m == 0);
}
}
else
{
//
// E is greater than zero. F is a normalized float.
// We try to convert f to a normalized half.
//
//
// Round to m to the nearest 10-bit value. In case of
// a tie, round to the nearest even value.
//
m = m + 0x00000fff + ((m >> 13) & 1);
if (m & 0x00800000)
{
m = 0; // overflow in significand,
e += 1; // adjust exponent
}
//
// Handle exponent overflow
//
if (e > 30)
{
half_overflow (); // Cause a hardware floating point overflow;
return s | 0x7c00; // if this returns, the half becomes an
} // infinity with the same sign as f.
//
// Assemble the half from s, e and m.
//
return s | (e << 10) | (m >> 13);
}
}
static inline half
exptable_half_constructor (float f)
{
half ret;
imath_half_uif x;
x.f = f;
if (f == 0)
{
//
// Common special case - zero.
// Preserve the zero's sign bit.
//
ret.setBits ((x.i >> 16));
}
else
{
//
// We extract the combined sign and exponent, e, from our
// floating-point number, f. Then we convert e to the sign
// and exponent of the half number via a table lookup.
//
// For the most common case, where a normalized half is produced,
// the table lookup returns a non-zero value; in this case, all
// we have to do is round f's significand to 10 bits and combine
// the result with e.
//
// For all other cases (overflow, zeroes, denormalized numbers
// resulting from underflow, infinities and NANs), the table
// lookup returns zero, and we call a longer, non-inline function
// to do the float-to-half conversion.
//
int e = (x.i >> 23) & 0x000001ff;
e = imath_float_half_exp_table[e];
if (e)
{
//
// Simple case - round the significand, m, to 10
// bits and combine it with the sign and exponent.
//
int m = x.i & 0x007fffff;
ret.setBits (e + ((m + 0x00000fff + ((m >> 13) & 1)) >> 13));
}
else
{
//
// Difficult case - call a function.
//
ret.setBits (long_convert (x.i));
}
}
return ret;
}
#else
// provide a wrapping function for consistency/readability
static inline float
table_half_cast (const half& h)
{
return static_cast<float> (h);
}
static inline half
exptable_half_constructor (float f)
{
return half{f};
}
#endif
int64_t
get_ticks (void)
{
#ifdef _WIN32
static uint64_t scale = 0;
if (scale == 0)
{
LARGE_INTEGER freq;
QueryPerformanceFrequency (&freq);
scale = (1000000000 / freq.QuadPart);
}
LARGE_INTEGER ticks;
QueryPerformanceCounter (&ticks);
return ticks.QuadPart * scale;
#else
struct timespec t;
uint64_t nsecs;
static uint64_t start = 0;
if (start == 0)
{
clock_gettime (CLOCK_MONOTONIC, &t);
start = t.tv_sec;
}
clock_gettime (CLOCK_MONOTONIC, &t);
nsecs = (t.tv_sec - start) * 1000000000;
nsecs += t.tv_nsec;
return nsecs;
#endif
}
void
perf_test_half_to_float (float* floats, const uint16_t* halfs, int numentries)
{
const half* halfvals = reinterpret_cast<const half*> (halfs);
int64_t st = get_ticks ();
for (int i = 0; i < numentries; ++i)
floats[i] = imath_half_to_float (halfs[i]);
int64_t et = get_ticks ();
int64_t ost = get_ticks ();
for (int i = 0; i < numentries; ++i)
floats[i] = table_half_cast (halfvals[i]);
int64_t oet = get_ticks ();
int64_t onanos = (oet - ost);
int64_t nnanos = (et - st);
fprintf (
stderr,
"half -> float Old: %10lld (%g ns) New: %10lld (%g ns) (%10lld)\n",
(long long) onanos,
(double) onanos / ((double) numentries),
(long long) nnanos,
(double) nnanos / ((double) numentries),
((long long) (onanos - nnanos)));
}
void
perf_test_float_to_half (uint16_t* halfs, const float* floats, int numentries)
{
half* halfvals = reinterpret_cast<half*> (halfs);
int64_t st = get_ticks ();
for (int i = 0; i < numentries; ++i)
halfs[i] = imath_float_to_half (floats[i]);
int64_t et = get_ticks ();
int64_t ost = get_ticks ();
for (int i = 0; i < numentries; ++i)
halfvals[i] = exptable_half_constructor (floats[i]);
int64_t oet = get_ticks ();
int64_t onanos = (oet - ost);
int64_t nnanos = (et - st);
fprintf (
stderr,
"float -> half Old: %10lld (%g ns) New: %10lld (%g ns) (%10lld)\n",
(long long) onanos,
(double) onanos / ((double) numentries),
(long long) nnanos,
(double) nnanos / ((double) numentries),
((long long) (onanos - nnanos)));
}
int
main (int argc, char* argv[])
{
int ret = 0;
int numentries = 1920 * 1080 * 3;
if (argc > 1)
{
numentries = atoi (argv[1]);
if (numentries <= 0)
{
fprintf (stderr, "Bad entry count '%s'\n", argv[1]);
ret = 1;
}
}
if (numentries > 0)
{
uint16_t* halfs = new uint16_t[numentries];
float* floats = new float[numentries];
if (halfs && floats)
{
Rand48 r(numentries);
for (int i = 0; i < numentries; ++i)
{
halfs[i] = (uint16_t) r.nexti();
floats[i] = imath_half_to_float (halfs[i]);
}
perf_test_half_to_float (floats, halfs, numentries);
// test float -> half with real-world values
for (int i = 0; i < numentries; ++i)
floats[i] = float(r.nextf(-65504, 65504));
perf_test_float_to_half (halfs, floats, numentries);
}
delete[] halfs;
delete[] floats;
}
return ret;
}