/work/_deps/imath-src/src/Imath/half.cpp
Line | Count | Source (jump to first uncovered line) |
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 <kainz@ilm.com> |
9 | | // Rod Bogart <rgb@ilm.com> |
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 | 0 | { |
58 | 0 | os << float (h); |
59 | 0 | return os; |
60 | 0 | } |
61 | | |
62 | | IMATH_EXPORT istream& |
63 | | operator>> (istream& is, half& h) |
64 | 0 | { |
65 | 0 | float f; |
66 | 0 | is >> f; |
67 | 0 | h = half (f); |
68 | 0 | return is; |
69 | 0 | } |
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 | 0 | { |
79 | 0 | unsigned short b = h.bits (); |
80 | |
|
81 | 0 | for (int i = 15; i >= 0; i--) |
82 | 0 | { |
83 | 0 | os << (((b >> i) & 1) ? '1' : '0'); |
84 | |
|
85 | 0 | if (i == 15 || i == 10) os << ' '; |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | IMATH_EXPORT void |
90 | | printBits (ostream& os, float f) |
91 | 0 | { |
92 | 0 | half::uif x; |
93 | 0 | x.f = f; |
94 | |
|
95 | 0 | for (int i = 31; i >= 0; i--) |
96 | 0 | { |
97 | 0 | os << (((x.i >> i) & 1) ? '1' : '0'); |
98 | |
|
99 | 0 | if (i == 31 || i == 23) os << ' '; |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | | IMATH_EXPORT void |
104 | | printBits (char c[19], half h) |
105 | 0 | { |
106 | 0 | unsigned short b = h.bits (); |
107 | |
|
108 | 0 | for (int i = 15, j = 0; i >= 0; i--, j++) |
109 | 0 | { |
110 | 0 | c[j] = (((b >> i) & 1) ? '1' : '0'); |
111 | |
|
112 | 0 | if (i == 15 || i == 10) c[++j] = ' '; |
113 | 0 | } |
114 | |
|
115 | 0 | c[18] = 0; |
116 | 0 | } |
117 | | |
118 | | IMATH_EXPORT void |
119 | | printBits (char c[35], float f) |
120 | 0 | { |
121 | 0 | half::uif x; |
122 | 0 | x.f = f; |
123 | |
|
124 | 0 | for (int i = 31, j = 0; i >= 0; i--, j++) |
125 | 0 | { |
126 | 0 | c[j] = (((x.i >> i) & 1) ? '1' : '0'); |
127 | |
|
128 | 0 | if (i == 31 || i == 23) c[++j] = ' '; |
129 | 0 | } |
130 | |
|
131 | 0 | c[34] = 0; |
132 | 0 | } |