Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXhalf.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                     H a l f - F l o a t   S u p p o r t                       *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2008,2009 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 *********************************************************************************
00021 * $Id: FXhalf.h,v 1.11 2009/01/14 11:37:25 fox Exp $                            *
00022 ********************************************************************************/
00023 #ifndef FXHALF_H
00024 #define FXHALF_H
00025 
00026 
00027 // Some important numbers
00028 #define HALF_MIN        5.9604644775391E-08     // Smallest half number
00029 #define HALF_MAX        65504.0                 // Largest half number
00030 #define HALF_EPSILON    0.00097656              // Smallest number where (1+e) != 1
00031 
00032 
00033 namespace FX {
00034 
00035 
00036 /// Half float (16-bit float)
00037 class FXAPI FXhalf {
00038 private:
00039   FXushort v;
00040 private:
00041   static const FXushort fhb[512];
00042   static const FXuchar  fhs[512];
00043   static const FXuint   hfm[2048];
00044   static const FXuint   hfe[64];
00045   static const FXushort hfw[64];
00046 public:
00047   FXhalf(){}
00048 
00049   // Initialize half with half
00050   FXhalf(const FXhalf& h):v(h.v){}
00051 
00052   // Initialize half with float
00053   FXhalf(FXfloat f){ union { FXuint u; FXfloat f; } r; r.f=f; v=fhb[(r.u>>23)&0x1ff]+((r.u&0x007fffff)>>fhs[(r.u>>23)&0x1ff]); }
00054 
00055   // Convert half to float
00056   operator FXfloat() const { union { FXuint u; FXfloat f; } r; r.u=hfm[hfw[v>>10]+(v&0x3ff)]+hfe[v>>10]; return r.f; }
00057 
00058   // Test for zero
00059   FXbool operator!() const { return v==0; }
00060 
00061   // Unary
00062   FXhalf operator+() const { return *this; }
00063   FXhalf operator-() const { FXhalf h; h.v=v^0x8000; return h; }
00064 
00065   // Equality
00066   FXbool operator==(FXhalf h) const { return v==h.v; }
00067   FXbool operator!=(FXhalf h) const { return v!=h.v; }
00068 
00069   // Assignment
00070   FXhalf& operator=(FXhalf h){ v=h.v; return *this; }
00071   FXhalf& operator=(FXfloat f){ *this=FXhalf(f); return *this; }
00072 
00073   // Assignment operators
00074   FXhalf& operator+=(FXhalf h){ *this=FXhalf(FXfloat(*this)+FXfloat(h)); return *this; }
00075   FXhalf& operator+=(FXfloat f){ *this=FXhalf(FXfloat(*this)+f); return *this; }
00076 
00077   FXhalf& operator-=(FXhalf h){ *this=FXhalf(FXfloat(*this)-FXfloat(h)); return *this; }
00078   FXhalf& operator-=(FXfloat f){ *this=FXhalf(FXfloat(*this)-f); return *this; }
00079 
00080   FXhalf& operator*=(FXhalf h){ *this=FXhalf(FXfloat(*this)*FXfloat(h)); return *this; }
00081   FXhalf& operator*=(FXfloat f){ *this=FXhalf(FXfloat(*this)*f); return *this; }
00082 
00083   FXhalf& operator/=(FXhalf h){ *this=FXhalf(FXfloat(*this)/FXfloat(h)); return *this; }
00084   FXhalf& operator/=(FXfloat f){ *this=FXhalf(FXfloat(*this)/f); return *this; }
00085   };
00086 
00087 
00088 }
00089 
00090 #endif

Copyright © 1997-2009 Jeroen van der Zijp