///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////3334353637//---------------------------------------------------------------------------38//39// toFloat40//41// A program to generate the lookup table for half-to-float42// conversion needed by class half.43// The program loops over all 65536 possible half numbers,44// converts each of them to a float, and prints the result.45//46//---------------------------------------------------------------------------474849#include <iostream>50#include <iomanip>5152using namespace std;5354//---------------------------------------------------55// Interpret an unsigned short bit pattern as a half,56// and convert that half to the corresponding float's57// bit pattern.58//---------------------------------------------------5960unsigned int61halfToFloat (unsigned short y)62{6364int s = (y >> 15) & 0x00000001;65int e = (y >> 10) & 0x0000001f;66int m = y & 0x000003ff;6768if (e == 0)69{70if (m == 0)71{72//73// Plus or minus zero74//7576return s << 31;77}78else79{80//81// Denormalized number -- renormalize it82//8384while (!(m & 0x00000400))85{86m <<= 1;87e -= 1;88}8990e += 1;91m &= ~0x00000400;92}93}94else if (e == 31)95{96if (m == 0)97{98//99// Positive or negative infinity100//101102return (s << 31) | 0x7f800000;103}104else105{106//107// Nan -- preserve sign and significand bits108//109110return (s << 31) | 0x7f800000 | (m << 13);111}112}113114//115// Normalized number116//117118e = e + (127 - 15);119m = m << 13;120121//122// Assemble s, e and m.123//124125return (s << 31) | (e << 23) | m;126}127128129//---------------------------------------------130// Main - prints the half-to-float lookup table131//---------------------------------------------132133int134main ()135{136cout.precision (9);137cout.setf (ios_base::hex, ios_base::basefield);138139cout << "//\n"140"// This is an automatically generated file.\n"141"// Do not edit.\n"142"//\n\n";143144cout << "{\n ";145146const int iMax = (1 << 16);147148for (int i = 0; i < iMax; i++)149{150cout << "{0x" << setfill ('0') << setw (8) << halfToFloat (i) << "}, ";151152if (i % 4 == 3)153{154cout << "\n";155156if (i < iMax - 1)157cout << " ";158}159}160161cout << "};\n";162return 0;163}164165166