Blame IlmImf/b44ExpLogTable.cpp

Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
//
Packit 0d464f
// Copyright (c) 2006, Industrial Light & Magic, a division of Lucas
Packit 0d464f
// Digital Ltd. LLC
Packit 0d464f
// 
Packit 0d464f
// All rights reserved.
Packit 0d464f
// 
Packit 0d464f
// Redistribution and use in source and binary forms, with or without
Packit 0d464f
// modification, are permitted provided that the following conditions are
Packit 0d464f
// met:
Packit 0d464f
// *       Redistributions of source code must retain the above copyright
Packit 0d464f
// notice, this list of conditions and the following disclaimer.
Packit 0d464f
// *       Redistributions in binary form must reproduce the above
Packit 0d464f
// copyright notice, this list of conditions and the following disclaimer
Packit 0d464f
// in the documentation and/or other materials provided with the
Packit 0d464f
// distribution.
Packit 0d464f
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 0d464f
// its contributors may be used to endorse or promote products derived
Packit 0d464f
// from this software without specific prior written permission. 
Packit 0d464f
// 
Packit 0d464f
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 0d464f
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 0d464f
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 0d464f
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 0d464f
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 0d464f
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 0d464f
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 0d464f
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 0d464f
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 0d464f
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 0d464f
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 0d464f
//
Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
Packit 0d464f
//---------------------------------------------------------------------------
Packit 0d464f
//
Packit 0d464f
//	b44ExpLogTable
Packit 0d464f
//
Packit 0d464f
//	A program to generate lookup tables for
Packit 0d464f
//
Packit 0d464f
//		y = exp (x / 8)
Packit 0d464f
//
Packit 0d464f
//	and
Packit 0d464f
//		x = 8 * log (x);
Packit 0d464f
//
Packit 0d464f
//	where x and y are 16-bit floating-point numbers
Packit 0d464f
//
Packit 0d464f
//	The tables are used by class B44Compressor.
Packit 0d464f
//
Packit 0d464f
//---------------------------------------------------------------------------
Packit 0d464f
Packit 0d464f
#include <half.h>
Packit 0d464f
#include <math.h>
Packit 0d464f
#include <iostream>
Packit 0d464f
#include <iomanip>
Packit 0d464f
Packit 0d464f
using namespace std;
Packit 0d464f
Packit 0d464f
//---------------------------------------------
Packit 0d464f
// Main - prints the half-to-float lookup table
Packit 0d464f
//---------------------------------------------
Packit 0d464f
Packit 0d464f
int
Packit 0d464f
main ()
Packit 0d464f
{
Packit 0d464f
#ifndef HAVE_IOS_BASE
Packit 0d464f
    cout.setf (ios::hex, ios::basefield);
Packit 0d464f
#else
Packit 0d464f
    cout.setf (ios_base::hex, ios_base::basefield);
Packit 0d464f
#endif
Packit 0d464f
Packit 0d464f
    cout << "//\n"
Packit 0d464f
	    "// This is an automatically generated file.\n"
Packit 0d464f
	    "// Do not edit.\n"
Packit 0d464f
	    "//\n\n";
Packit 0d464f
Packit 0d464f
    const int iMax = (1 << 16);
Packit 0d464f
Packit 0d464f
    cout << "const unsigned short expTable[] =\n"
Packit 0d464f
	    "{\n"
Packit 0d464f
	    "    ";
Packit 0d464f
Packit 0d464f
    for (int i = 0; i < iMax; i++)
Packit 0d464f
    {
Packit 0d464f
	half h;
Packit 0d464f
	h.setBits (i);
Packit 0d464f
Packit 0d464f
	if (!h.isFinite())
Packit 0d464f
	    h = 0;
Packit 0d464f
	else if (h >= 8 * log (HALF_MAX))
Packit 0d464f
	    h = HALF_MAX;
Packit 0d464f
	else
Packit 0d464f
	    h = exp (h / 8);
Packit 0d464f
Packit 0d464f
	cout << "0x" << setfill ('0') << setw (4) << h.bits() << ", ";
Packit 0d464f
Packit 0d464f
	if (i % 8 == 7)
Packit 0d464f
	{
Packit 0d464f
	    cout << "\n";
Packit 0d464f
Packit 0d464f
	    if (i < iMax - 1)
Packit 0d464f
		cout << "    ";
Packit 0d464f
	}
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    cout << "};\n\n";
Packit 0d464f
Packit 0d464f
    cout << "const unsigned short logTable[] =\n"
Packit 0d464f
	    "{\n"
Packit 0d464f
	    "    ";
Packit 0d464f
Packit 0d464f
    for (int i = 0; i < iMax; i++)
Packit 0d464f
    {
Packit 0d464f
	half h;
Packit 0d464f
	h.setBits (i);
Packit 0d464f
Packit 0d464f
	if (!h.isFinite() || h < 0)
Packit 0d464f
	    h = 0;
Packit 0d464f
	else
Packit 0d464f
	    h = 8 * log (h);
Packit 0d464f
Packit 0d464f
	cout << "0x" << setfill ('0') << setw (4) << h.bits() << ", ";
Packit 0d464f
Packit 0d464f
	if (i % 8 == 7)
Packit 0d464f
	{
Packit 0d464f
	    cout << "\n";
Packit 0d464f
Packit 0d464f
	    if (i < iMax - 1)
Packit 0d464f
		cout << "    ";
Packit 0d464f
	}
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    cout << "};\n";
Packit 0d464f
Packit 0d464f
    return 0;
Packit 0d464f
}