Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/3rdparty/openexr/Half/toFloat.cpp
16337 views
1
///////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
4
// Digital Ltd. LLC
5
//
6
// All rights reserved.
7
//
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
10
// met:
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
16
// distribution.
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.
20
//
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.
32
//
33
///////////////////////////////////////////////////////////////////////////
34
35
36
37
38
//---------------------------------------------------------------------------
39
//
40
// toFloat
41
//
42
// A program to generate the lookup table for half-to-float
43
// conversion needed by class half.
44
// The program loops over all 65536 possible half numbers,
45
// converts each of them to a float, and prints the result.
46
//
47
//---------------------------------------------------------------------------
48
49
50
#include <iostream>
51
#include <iomanip>
52
53
using namespace std;
54
55
//---------------------------------------------------
56
// Interpret an unsigned short bit pattern as a half,
57
// and convert that half to the corresponding float's
58
// bit pattern.
59
//---------------------------------------------------
60
61
unsigned int
62
halfToFloat (unsigned short y)
63
{
64
65
int s = (y >> 15) & 0x00000001;
66
int e = (y >> 10) & 0x0000001f;
67
int m = y & 0x000003ff;
68
69
if (e == 0)
70
{
71
if (m == 0)
72
{
73
//
74
// Plus or minus zero
75
//
76
77
return s << 31;
78
}
79
else
80
{
81
//
82
// Denormalized number -- renormalize it
83
//
84
85
while (!(m & 0x00000400))
86
{
87
m <<= 1;
88
e -= 1;
89
}
90
91
e += 1;
92
m &= ~0x00000400;
93
}
94
}
95
else if (e == 31)
96
{
97
if (m == 0)
98
{
99
//
100
// Positive or negative infinity
101
//
102
103
return (s << 31) | 0x7f800000;
104
}
105
else
106
{
107
//
108
// Nan -- preserve sign and significand bits
109
//
110
111
return (s << 31) | 0x7f800000 | (m << 13);
112
}
113
}
114
115
//
116
// Normalized number
117
//
118
119
e = e + (127 - 15);
120
m = m << 13;
121
122
//
123
// Assemble s, e and m.
124
//
125
126
return (s << 31) | (e << 23) | m;
127
}
128
129
130
//---------------------------------------------
131
// Main - prints the half-to-float lookup table
132
//---------------------------------------------
133
134
int
135
main ()
136
{
137
cout.precision (9);
138
cout.setf (ios_base::hex, ios_base::basefield);
139
140
cout << "//\n"
141
"// This is an automatically generated file.\n"
142
"// Do not edit.\n"
143
"//\n\n";
144
145
cout << "{\n ";
146
147
const int iMax = (1 << 16);
148
149
for (int i = 0; i < iMax; i++)
150
{
151
cout << "{0x" << setfill ('0') << setw (8) << halfToFloat (i) << "}, ";
152
153
if (i % 4 == 3)
154
{
155
cout << "\n";
156
157
if (i < iMax - 1)
158
cout << " ";
159
}
160
}
161
162
cout << "};\n";
163
return 0;
164
}
165
166