1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2006, 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 //	b44ExpLogTable
38 //
39 //	A program to generate lookup tables for
40 //
41 //		y = exp (x / 8)
42 //
43 //	and
44 //		x = 8 * log (x);
45 //
46 //	where x and y are 16-bit floating-point numbers
47 //
48 //	The tables are used by class B44Compressor.
49 //
50 //---------------------------------------------------------------------------
51 
52 #include <half.h>
53 #include <math.h>
54 #include <iostream>
55 #include <iomanip>
56 
57 using namespace std;
58 
59 //---------------------------------------------
60 // Main - prints the half-to-float lookup table
61 //---------------------------------------------
62 
63 int
main()64 main ()
65 {
66 #ifndef HAVE_IOS_BASE
67     cout.setf (ios::hex, ios::basefield);
68 #else
69     cout.setf (ios_base::hex, ios_base::basefield);
70 #endif
71 
72     cout << "//\n"
73 	    "// This is an automatically generated file.\n"
74 	    "// Do not edit.\n"
75 	    "//\n\n";
76 
77     const int iMax = (1 << 16);
78 
79     cout << "const unsigned short expTable[] =\n"
80 	    "{\n"
81 	    "    ";
82 
83     for (int i = 0; i < iMax; i++)
84     {
85 	half h;
86 	h.setBits (i);
87 
88 	if (!h.isFinite())
89 	    h = 0;
90 	else if (h >= 8 * log (HALF_MAX))
91 	    h = HALF_MAX;
92 	else
93 	    h = exp (h / 8);
94 
95 	cout << "0x" << setfill ('0') << setw (4) << h.bits() << ", ";
96 
97 	if (i % 8 == 7)
98 	{
99 	    cout << "\n";
100 
101 	    if (i < iMax - 1)
102 		cout << "    ";
103 	}
104     }
105 
106     cout << "};\n\n";
107 
108     cout << "const unsigned short logTable[] =\n"
109 	    "{\n"
110 	    "    ";
111 
112     for (int i = 0; i < iMax; i++)
113     {
114 	half h;
115 	h.setBits (i);
116 
117 	if (!h.isFinite() || h < 0)
118 	    h = 0;
119 	else
120 	    h = 8 * log (h);
121 
122 	cout << "0x" << setfill ('0') << setw (4) << h.bits() << ", ";
123 
124 	if (i % 8 == 7)
125 	{
126 	    cout << "\n";
127 
128 	    if (i < iMax - 1)
129 		cout << "    ";
130 	}
131     }
132 
133     cout << "};\n";
134 
135     return 0;
136 }
137