00001 00002 // 00003 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas 00004 // Digital Ltd. LLC 00005 // 00006 // All rights reserved. 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are 00010 // met: 00011 // * Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // * Redistributions in binary form must reproduce the above 00014 // copyright notice, this list of conditions and the following disclaimer 00015 // in the documentation and/or other materials provided with the 00016 // distribution. 00017 // * Neither the name of Industrial Light & Magic nor the names of 00018 // its contributors may be used to endorse or promote products derived 00019 // from this software without specific prior written permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00024 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00025 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00027 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00028 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00029 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00030 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00031 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00034 00035 00036 00037 00038 //--------------------------------------------------------------------------- 00039 // 00040 // toFloat 00041 // 00042 // A program to generate the lookup table for half-to-float 00043 // conversion needed by class half. 00044 // The program loops over all 65536 possible half numbers, 00045 // converts each of them to a float, and prints the result. 00046 // 00047 //--------------------------------------------------------------------------- 00048 00049 00050 #include <iostream> 00051 #include <iomanip> 00052 00053 using namespace std; 00054 00055 //--------------------------------------------------- 00056 // Interpret an unsigned short bit pattern as a half, 00057 // and convert that half to the corresponding float's 00058 // bit pattern. 00059 //--------------------------------------------------- 00060 00061 unsigned int 00062 halfToFloat (unsigned short y) 00063 { 00064 00065 int s = (y >> 15) & 0x00000001; 00066 int e = (y >> 10) & 0x0000001f; 00067 int m = y & 0x000003ff; 00068 00069 if (e == 0) 00070 { 00071 if (m == 0) 00072 { 00073 // 00074 // Plus or minus zero 00075 // 00076 00077 return s << 31; 00078 } 00079 else 00080 { 00081 // 00082 // Denormalized number -- renormalize it 00083 // 00084 00085 while (!(m & 0x00000400)) 00086 { 00087 m <<= 1; 00088 e -= 1; 00089 } 00090 00091 e += 1; 00092 m &= ~0x00000400; 00093 } 00094 } 00095 else if (e == 31) 00096 { 00097 if (m == 0) 00098 { 00099 // 00100 // Positive or negative infinity 00101 // 00102 00103 return (s << 31) | 0x7f800000; 00104 } 00105 else 00106 { 00107 // 00108 // Nan -- preserve sign and significand bits 00109 // 00110 00111 return (s << 31) | 0x7f800000 | (m << 13); 00112 } 00113 } 00114 00115 // 00116 // Normalized number 00117 // 00118 00119 e = e + (127 - 15); 00120 m = m << 13; 00121 00122 // 00123 // Assemble s, e and m. 00124 // 00125 00126 return (s << 31) | (e << 23) | m; 00127 } 00128 00129 00130 //--------------------------------------------- 00131 // Main - prints the half-to-float lookup table 00132 //--------------------------------------------- 00133 00134 int 00135 main () 00136 { 00137 cout.precision (9); 00138 #ifndef HAVE_IOS_BASE 00139 cout.setf (ios::hex, ios::basefield); 00140 #else 00141 cout.setf (ios_base::hex, ios_base::basefield); 00142 #endif 00143 00144 cout << "//\n" 00145 "// This is an automatically generated file.\n" 00146 "// Do not edit.\n" 00147 "//\n\n"; 00148 00149 cout << "{\n "; 00150 00151 const int iMax = (1 << 16); 00152 00153 for (int i = 0; i < iMax; i++) 00154 { 00155 cout << "{0x" << setfill ('0') << setw (8) << halfToFloat (i) << "}, "; 00156 00157 if (i % 4 == 3) 00158 { 00159 cout << "\n"; 00160 00161 if (i < iMax - 1) 00162 cout << " "; 00163 } 00164 } 00165 00166 cout << "};\n"; 00167 return 0; 00168 }