| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// |
| 7 |
// Primary original authors: |
| 8 |
// Florian Kainz <[email protected]> |
| 9 |
// Rod Bogart <[email protected]> |
| 10 |
// |
| 11 |
|
| 12 |
//--------------------------------------------------------------------------- |
| 13 |
// |
| 14 |
// class half -- |
| 15 |
// implementation of non-inline members |
| 16 |
// |
| 17 |
//--------------------------------------------------------------------------- |
| 18 |
|
| 19 |
#include "half.h" |
| 20 |
#include <assert.h> |
| 21 |
|
| 22 |
using namespace std; |
| 23 |
|
| 24 |
#if defined(IMATH_DLL) |
| 25 |
# define EXPORT_CONST __declspec(dllexport) |
| 26 |
#else |
| 27 |
# define EXPORT_CONST |
| 28 |
#endif |
| 29 |
|
| 30 |
//------------------------------------------------------------- |
| 31 |
// Lookup tables for half-to-float and float-to-half conversion |
| 32 |
//------------------------------------------------------------- |
| 33 |
|
| 34 |
// clang-format off |
| 35 |
|
| 36 |
#if !defined(IMATH_HALF_NO_LOOKUP_TABLE) |
| 37 |
// Omit the table entirely if IMATH_HALF_NO_LOOKUP_TABLE is |
| 38 |
// defined. Half-to-float conversion must be accomplished either by |
| 39 |
// F16C instructions or the bit-shift algorithm. |
| 40 |
const imath_half_uif_t imath_half_to_float_table_data[1 << 16] = |
| 41 |
#include "toFloat.h" |
| 42 |
|
| 43 |
extern "C" { |
| 44 |
EXPORT_CONST const imath_half_uif_t *imath_half_to_float_table = imath_half_to_float_table_data; |
| 45 |
} // extern "C" |
| 46 |
|
| 47 |
#endif |
| 48 |
|
| 49 |
// clang-format on |
| 50 |
|
| 51 |
//--------------------- |
| 52 |
// Stream I/O operators |
| 53 |
//--------------------- |
| 54 |
|
| 55 |
IMATH_EXPORT ostream& |
| 56 |
operator<< (ostream& os, half h) |
| 57 |
{ |
| 58 |
os << float (h); |
| 59 |
return os; |
| 60 |
} |
| 61 |
|
| 62 |
IMATH_EXPORT istream& |
| 63 |
operator>> (istream& is, half& h) |
| 64 |
{ |
| 65 |
float f; |
| 66 |
is >> f; |
| 67 |
h = half (f); |
| 68 |
return is; |
| 69 |
} |
| 70 |
|
| 71 |
//--------------------------------------- |
| 72 |
// Functions to print the bit-layout of |
| 73 |
// floats and halfs, mostly for debugging |
| 74 |
//--------------------------------------- |
| 75 |
|
| 76 |
IMATH_EXPORT void |
| 77 |
printBits (ostream& os, half h) |
| 78 |
{ |
| 79 |
unsigned short b = h.bits(); |
| 80 |
|
| 81 |
for (int i = 15; i >= 0; i--) |
| 82 |
{ |
| 83 |
os << (((b >> i) & 1) ? '1' : '0'); |
| 84 |
|
| 85 |
if (i == 15 || i == 10) |
| 86 |
os << ' '; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
IMATH_EXPORT void |
| 91 |
printBits (ostream& os, float f) |
| 92 |
{ |
| 93 |
half::uif x; |
| 94 |
x.f = f; |
| 95 |
|
| 96 |
for (int i = 31; i >= 0; i--) |
| 97 |
{ |
| 98 |
os << (((x.i >> i) & 1) ? '1' : '0'); |
| 99 |
|
| 100 |
if (i == 31 || i == 23) |
| 101 |
os << ' '; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
IMATH_EXPORT void |
| 106 |
printBits (char c[19], half h) |
| 107 |
{ |
| 108 |
unsigned short b = h.bits(); |
| 109 |
|
| 110 |
for (int i = 15, j = 0; i >= 0; i--, j++) |
| 111 |
{ |
| 112 |
c[j] = (((b >> i) & 1) ? '1' : '0'); |
| 113 |
|
| 114 |
if (i == 15 || i == 10) |
| 115 |
c[++j] = ' '; |
| 116 |
} |
| 117 |
|
| 118 |
c[18] = 0; |
| 119 |
} |
| 120 |
|
| 121 |
IMATH_EXPORT void |
| 122 |
printBits (char c[35], float f) |
| 123 |
{ |
| 124 |
half::uif x; |
| 125 |
x.f = f; |
| 126 |
|
| 127 |
for (int i = 31, j = 0; i >= 0; i--, j++) |
| 128 |
{ |
| 129 |
c[j] = (((x.i >> i) & 1) ? '1' : '0'); |
| 130 |
|
| 131 |
if (i == 31 || i == 23) |
| 132 |
c[++j] = ' '; |
| 133 |
} |
| 134 |
|
| 135 |
c[34] = 0; |
| 136 |
} |