1
///////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6
// All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
13
// * Redistributions in binary form must reproduce the above
14
// copyright notice, this list of conditions and the following disclaimer
15
// in the documentation and/or other materials provided with the
17
// * Neither the name of Industrial Light & Magic nor the names of
18
// its contributors may be used to endorse or promote products derived
19
// from this software without specific prior written permission.
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
///////////////////////////////////////////////////////////////////////////
36
// Florian Kainz <kainz@ilm.com>
37
// Rod Bogart <rgb@ilm.com>
40
//---------------------------------------------------------------------------
43
// implementation of non-inline members
45
//---------------------------------------------------------------------------
52
//-------------------------------------------------------------
53
// Lookup tables for half-to-float and float-to-half conversion
54
//-------------------------------------------------------------
56
#if defined (OPENEXR_DLL)
57
__declspec(dllexport) half::uif _toFloat[1 << 16] =
59
__declspec(dllexport) unsigned short _eLut[1 << 9] =
62
const half::uif half::_toFloat[1 << 16] =
64
const unsigned short half::_eLut[1 << 9] =
68
//-----------------------------------------------
69
// Overflow handler for float-to-half conversion;
70
// generates a hardware floating-point overflow,
71
// which may be trapped by the operating system.
72
//-----------------------------------------------
77
volatile float f = 1e10;
79
for (int i = 0; i < 10; i++)
80
f *= f; // this will overflow before
81
// the for�loop terminates
86
//-----------------------------------------------------
87
// Float-to-half conversion -- general case, including
88
// zeroes, denormalized numbers and exponent overflows.
89
//-----------------------------------------------------
95
// Our floating point number, f, is represented by the bit
96
// pattern in integer i. Disassemble that bit pattern into
97
// the sign, s, the exponent, e, and the significand, m.
98
// Shift s into the position where it will go in in the
99
// resulting half number.
100
// Adjust e, accounting for the different exponent bias
101
// of float and half (127 versus 15).
104
register int s = (i >> 16) & 0x00008000;
105
register int e = ((i >> 23) & 0x000000ff) - (127 - 15);
106
register int m = i & 0x007fffff;
109
// Now reassemble s, e and m into a half:
117
// E is less than -10. The absolute value of f is
118
// less than HALF_MIN (f may be a small normalized
119
// float, a denormalized float or a zero).
121
// We convert f to a half zero with the same sign as f.
128
// E is between -10 and 0. F is a normalized float
129
// whose magnitude is less than HALF_NRM_MIN.
131
// We convert f to a denormalized half.
135
// Add an explicit leading 1 to the significand.
141
// Round to m to the nearest (10+e)-bit value (with e between
142
// -10 and 0); in case of a tie, round to the nearest even value.
144
// Rounding may cause the significand to overflow and make
145
// our number normalized. Because of the way a half's bits
146
// are laid out, we don't have to treat this case separately;
147
// the code below will handle it correctly.
151
int a = (1 << (t - 1)) - 1;
152
int b = (m >> t) & 1;
154
m = (m + a + b) >> t;
157
// Assemble the half from s, e (zero) and m.
162
else if (e == 0xff - (127 - 15))
167
// F is an infinity; convert f to a half
168
// infinity with the same sign as f.
176
// F is a NAN; we produce a half NAN that preserves
177
// the sign bit and the 10 leftmost bits of the
178
// significand of f, with one exception: If the 10
179
// leftmost bits are all zero, the NAN would turn
180
// into an infinity, so we have to set at least one
181
// bit in the significand.
185
return s | 0x7c00 | m | (m == 0);
191
// E is greater than zero. F is a normalized float.
192
// We try to convert f to a normalized half.
196
// Round to m to the nearest 10-bit value. In case of
197
// a tie, round to the nearest even value.
200
m = m + 0x00000fff + ((m >> 13) & 1);
204
m = 0; // overflow in significand,
205
e += 1; // adjust exponent
209
// Handle exponent overflow
214
overflow (); // Cause a hardware floating point overflow;
215
return s | 0x7c00; // if this returns, the half becomes an
216
} // infinity with the same sign as f.
219
// Assemble the half from s, e and m.
222
return s | (e << 10) | (m >> 13);
227
//---------------------
228
// Stream I/O operators
229
//---------------------
232
operator << (ostream &os, half h)
240
operator >> (istream &is, half &h)
249
//---------------------------------------
250
// Functions to print the bit-layout of
251
// floats and halfs, mostly for debugging
252
//---------------------------------------
255
printBits (ostream &os, half h)
257
unsigned short b = h.bits();
259
for (int i = 15; i >= 0; i--)
261
os << (((b >> i) & 1)? '1': '0');
263
if (i == 15 || i == 10)
270
printBits (ostream &os, float f)
275
for (int i = 31; i >= 0; i--)
277
os << (((x.i >> i) & 1)? '1': '0');
279
if (i == 31 || i == 23)
286
printBits (char c[19], half h)
288
unsigned short b = h.bits();
290
for (int i = 15, j = 0; i >= 0; i--, j++)
292
c[j] = (((b >> i) & 1)? '1': '0');
294
if (i == 15 || i == 10)
303
printBits (char c[35], float f)
308
for (int i = 31, j = 0; i >= 0; i--, j++)
310
c[j] = (((x.i >> i) & 1)? '1': '0');
312
if (i == 31 || i == 23)
1
///////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6
// All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
13
// * Redistributions in binary form must reproduce the above
14
// copyright notice, this list of conditions and the following disclaimer
15
// in the documentation and/or other materials provided with the
17
// * Neither the name of Industrial Light & Magic nor the names of
18
// its contributors may be used to endorse or promote products derived
19
// from this software without specific prior written permission.
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
///////////////////////////////////////////////////////////////////////////
36
// Florian Kainz <kainz@ilm.com>
37
// Rod Bogart <rgb@ilm.com>
40
//---------------------------------------------------------------------------
43
// implementation of non-inline members
45
//---------------------------------------------------------------------------
52
//-------------------------------------------------------------
53
// Lookup tables for half-to-float and float-to-half conversion
54
//-------------------------------------------------------------
56
#if defined (OPENEXR_DLL)
57
__declspec(dllexport) half::uif _toFloat[1 << 16] =
59
__declspec(dllexport) unsigned short _eLut[1 << 9] =
62
const half::uif half::_toFloat[1 << 16] =
64
const unsigned short half::_eLut[1 << 9] =
68
//-----------------------------------------------
69
// Overflow handler for float-to-half conversion;
70
// generates a hardware floating-point overflow,
71
// which may be trapped by the operating system.
72
//-----------------------------------------------
77
volatile float f = 1e10;
79
for (int i = 0; i < 10; i++)
80
f *= f; // this will overflow before
81
// the for�loop terminates
86
//-----------------------------------------------------
87
// Float-to-half conversion -- general case, including
88
// zeroes, denormalized numbers and exponent overflows.
89
//-----------------------------------------------------
95
// Our floating point number, f, is represented by the bit
96
// pattern in integer i. Disassemble that bit pattern into
97
// the sign, s, the exponent, e, and the significand, m.
98
// Shift s into the position where it will go in in the
99
// resulting half number.
100
// Adjust e, accounting for the different exponent bias
101
// of float and half (127 versus 15).
104
register int s = (i >> 16) & 0x00008000;
105
register int e = ((i >> 23) & 0x000000ff) - (127 - 15);
106
register int m = i & 0x007fffff;
109
// Now reassemble s, e and m into a half:
117
// E is less than -10. The absolute value of f is
118
// less than HALF_MIN (f may be a small normalized
119
// float, a denormalized float or a zero).
121
// We convert f to a half zero with the same sign as f.
128
// E is between -10 and 0. F is a normalized float
129
// whose magnitude is less than HALF_NRM_MIN.
131
// We convert f to a denormalized half.
135
// Add an explicit leading 1 to the significand.
141
// Round to m to the nearest (10+e)-bit value (with e between
142
// -10 and 0); in case of a tie, round to the nearest even value.
144
// Rounding may cause the significand to overflow and make
145
// our number normalized. Because of the way a half's bits
146
// are laid out, we don't have to treat this case separately;
147
// the code below will handle it correctly.
151
int a = (1 << (t - 1)) - 1;
152
int b = (m >> t) & 1;
154
m = (m + a + b) >> t;
157
// Assemble the half from s, e (zero) and m.
162
else if (e == 0xff - (127 - 15))
167
// F is an infinity; convert f to a half
168
// infinity with the same sign as f.
176
// F is a NAN; we produce a half NAN that preserves
177
// the sign bit and the 10 leftmost bits of the
178
// significand of f, with one exception: If the 10
179
// leftmost bits are all zero, the NAN would turn
180
// into an infinity, so we have to set at least one
181
// bit in the significand.
185
return s | 0x7c00 | m | (m == 0);
191
// E is greater than zero. F is a normalized float.
192
// We try to convert f to a normalized half.
196
// Round to m to the nearest 10-bit value. In case of
197
// a tie, round to the nearest even value.
200
m = m + 0x00000fff + ((m >> 13) & 1);
204
m = 0; // overflow in significand,
205
e += 1; // adjust exponent
209
// Handle exponent overflow
214
overflow (); // Cause a hardware floating point overflow;
215
return s | 0x7c00; // if this returns, the half becomes an
216
} // infinity with the same sign as f.
219
// Assemble the half from s, e and m.
222
return s | (e << 10) | (m >> 13);
227
//---------------------
228
// Stream I/O operators
229
//---------------------
232
operator << (ostream &os, half h)
240
operator >> (istream &is, half &h)
249
//---------------------------------------
250
// Functions to print the bit-layout of
251
// floats and halfs, mostly for debugging
252
//---------------------------------------
255
printBits (ostream &os, half h)
257
unsigned short b = h.bits();
259
for (int i = 15; i >= 0; i--)
261
os << (((b >> i) & 1)? '1': '0');
263
if (i == 15 || i == 10)
270
printBits (ostream &os, float f)
275
for (int i = 31; i >= 0; i--)
277
os << (((x.i >> i) & 1)? '1': '0');
279
if (i == 31 || i == 23)
286
printBits (char c[19], half h)
288
unsigned short b = h.bits();
290
for (int i = 15, j = 0; i >= 0; i--, j++)
292
c[j] = (((b >> i) & 1)? '1': '0');
294
if (i == 15 || i == 10)
303
printBits (char c[35], float f)
308
for (int i = 31, j = 0; i >= 0; i--, j++)
310
c[j] = (((x.i >> i) & 1)? '1': '0');
312
if (i == 31 || i == 23)