Blame Half/eLut.cpp

Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
//
Packit 8dc392
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
Packit 8dc392
// Digital Ltd. LLC
Packit 8dc392
// 
Packit 8dc392
// All rights reserved.
Packit 8dc392
// 
Packit 8dc392
// Redistribution and use in source and binary forms, with or without
Packit 8dc392
// modification, are permitted provided that the following conditions are
Packit 8dc392
// met:
Packit 8dc392
// *       Redistributions of source code must retain the above copyright
Packit 8dc392
// notice, this list of conditions and the following disclaimer.
Packit 8dc392
// *       Redistributions in binary form must reproduce the above
Packit 8dc392
// copyright notice, this list of conditions and the following disclaimer
Packit 8dc392
// in the documentation and/or other materials provided with the
Packit 8dc392
// distribution.
Packit 8dc392
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 8dc392
// its contributors may be used to endorse or promote products derived
Packit 8dc392
// from this software without specific prior written permission. 
Packit 8dc392
// 
Packit 8dc392
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 8dc392
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 8dc392
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 8dc392
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 8dc392
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 8dc392
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 8dc392
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 8dc392
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 8dc392
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 8dc392
//
Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
Packit 8dc392
Packit 8dc392
Packit 8dc392
#include <iostream>
Packit 8dc392
#include <iomanip>
Packit 8dc392
Packit 8dc392
using namespace std;
Packit 8dc392
Packit 8dc392
//-----------------------------------------------------
Packit 8dc392
// Compute a lookup table for float-to-half conversion.
Packit 8dc392
//
Packit 8dc392
// When indexed with the combined sign and exponent of
Packit 8dc392
// a float, the table either returns the combined sign
Packit 8dc392
// and exponent of the corresponding half, or zero if
Packit 8dc392
// the corresponding half may not be normalized (zero,
Packit 8dc392
// denormalized, overflow).
Packit 8dc392
//-----------------------------------------------------
Packit 8dc392
Packit 8dc392
void
Packit 8dc392
initELut (unsigned short eLut[])
Packit 8dc392
{
Packit 8dc392
    for (int i = 0; i < 0x100; i++)
Packit 8dc392
    {
Packit 8dc392
	int e = (i & 0x0ff) - (127 - 15);
Packit 8dc392
Packit 8dc392
	if (e <= 0 || e >= 30)
Packit 8dc392
	{
Packit 8dc392
	    //
Packit 8dc392
	    // Special case
Packit 8dc392
	    //
Packit 8dc392
Packit 8dc392
	    eLut[i]         = 0;
Packit 8dc392
	    eLut[i | 0x100] = 0;
Packit 8dc392
	}
Packit 8dc392
	else
Packit 8dc392
	{
Packit 8dc392
	    //
Packit 8dc392
	    // Common case - normalized half, no exponent overflow possible
Packit 8dc392
	    //
Packit 8dc392
Packit 8dc392
	    eLut[i]         =  (e << 10);
Packit 8dc392
	    eLut[i | 0x100] = ((e << 10) | 0x8000);
Packit 8dc392
	}
Packit 8dc392
    }
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
Packit 8dc392
//------------------------------------------------------------
Packit 8dc392
// Main - prints the sign-and-exponent conversion lookup table
Packit 8dc392
//------------------------------------------------------------
Packit 8dc392
Packit 8dc392
int
Packit 8dc392
main ()
Packit 8dc392
{
Packit 8dc392
    const int tableSize = 1 << 9;
Packit 8dc392
    unsigned short eLut[tableSize];
Packit 8dc392
    initELut (eLut);
Packit 8dc392
Packit 8dc392
    cout << "//\n"
Packit 8dc392
	    "// This is an automatically generated file.\n"
Packit 8dc392
	    "// Do not edit.\n"
Packit 8dc392
	    "//\n\n";
Packit 8dc392
Packit 8dc392
    cout << "{\n    ";
Packit 8dc392
Packit 8dc392
    for (int i = 0; i < tableSize; i++)
Packit 8dc392
    {
Packit 8dc392
	cout << setw (5) << eLut[i] << ", ";
Packit 8dc392
Packit 8dc392
	if (i % 8 == 7)
Packit 8dc392
	{
Packit 8dc392
	    cout << "\n";
Packit 8dc392
Packit 8dc392
	    if (i < tableSize - 1)
Packit 8dc392
		cout << "    ";
Packit 8dc392
	}
Packit 8dc392
    }
Packit 8dc392
Packit 8dc392
    cout << "};\n";
Packit 8dc392
    return 0;
Packit 8dc392
}