| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
//--------------------------------------------------------------------------- |
| 7 |
// |
| 8 |
// toFloat |
| 9 |
// |
| 10 |
// A program to generate the lookup table for half-to-float |
| 11 |
// conversion needed by class half. |
| 12 |
// The program loops over all 65536 possible half numbers, |
| 13 |
// converts each of them to a float, and prints the result. |
| 14 |
// |
| 15 |
//--------------------------------------------------------------------------- |
| 16 |
|
| 17 |
#include <iomanip> |
| 18 |
#include <iostream> |
| 19 |
|
| 20 |
using namespace std; |
| 21 |
|
| 22 |
//--------------------------------------------------- |
| 23 |
// Interpret an unsigned short bit pattern as a half, |
| 24 |
// and convert that half to the corresponding float's |
| 25 |
// bit pattern. |
| 26 |
//--------------------------------------------------- |
| 27 |
|
| 28 |
unsigned int |
| 29 |
halfToFloat (unsigned short y) |
| 30 |
{ |
| 31 |
|
| 32 |
int s = (y >> 15) & 0x00000001; |
| 33 |
int e = (y >> 10) & 0x0000001f; |
| 34 |
int m = y & 0x000003ff; |
| 35 |
|
| 36 |
if (e == 0) |
| 37 |
{ |
| 38 |
if (m == 0) |
| 39 |
{ |
| 40 |
// |
| 41 |
// Plus or minus zero |
| 42 |
// |
| 43 |
|
| 44 |
return s << 31; |
| 45 |
} |
| 46 |
else |
| 47 |
{ |
| 48 |
// |
| 49 |
// Denormalized number -- renormalize it |
| 50 |
// |
| 51 |
|
| 52 |
while (!(m & 0x00000400)) |
| 53 |
{ |
| 54 |
m <<= 1; |
| 55 |
e -= 1; |
| 56 |
} |
| 57 |
|
| 58 |
e += 1; |
| 59 |
m &= ~0x00000400; |
| 60 |
} |
| 61 |
} |
| 62 |
else if (e == 31) |
| 63 |
{ |
| 64 |
if (m == 0) |
| 65 |
{ |
| 66 |
// |
| 67 |
// Positive or negative infinity |
| 68 |
// |
| 69 |
|
| 70 |
return (s << 31) | 0x7f800000; |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
// |
| 75 |
// Nan -- preserve sign and significand bits |
| 76 |
// |
| 77 |
|
| 78 |
return (s << 31) | 0x7f800000 | (m << 13); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// |
| 83 |
// Normalized number |
| 84 |
// |
| 85 |
|
| 86 |
e = e + (127 - 15); |
| 87 |
m = m << 13; |
| 88 |
|
| 89 |
// |
| 90 |
// Assemble s, e and m. |
| 91 |
// |
| 92 |
|
| 93 |
return (s << 31) | (e << 23) | m; |
| 94 |
} |
| 95 |
|
| 96 |
//--------------------------------------------- |
| 97 |
// Main - prints the half-to-float lookup table |
| 98 |
//--------------------------------------------- |
| 99 |
|
| 100 |
int |
| 101 |
main() |
| 102 |
{ |
| 103 |
cout.precision (9); |
| 104 |
cout.setf (ios_base::hex, ios_base::basefield); |
| 105 |
|
| 106 |
cout << "//\n" |
| 107 |
"// This is an automatically generated file.\n" |
| 108 |
"// Do not edit.\n" |
| 109 |
"//\n\n"; |
| 110 |
|
| 111 |
cout << "{\n "; |
| 112 |
|
| 113 |
const int iMax = (1 << 16); |
| 114 |
|
| 115 |
for (int i = 0; i < iMax; i++) |
| 116 |
{ |
| 117 |
cout << "{0x" << setfill ('0') << setw (8) << halfToFloat (i) << "}, "; |
| 118 |
|
| 119 |
if (i % 4 == 3) |
| 120 |
{ |
| 121 |
cout << "\n"; |
| 122 |
|
| 123 |
if (i < iMax - 1) |
| 124 |
cout << " "; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
cout << "};\n"; |
| 129 |
return 0; |
| 130 |
} |