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