![]() |
Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members
|
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,2010 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 #ifndef FXHALF_H 00022 #define FXHALF_H 00023 00024 00025 // Some important numbers 00026 #define HALF_MIN 5.9604644775391E-08 // Smallest half number 00027 #define HALF_MAX 65504.0 // Largest half number 00028 #define HALF_EPSILON 0.00097656 // Smallest number where (1+e) != 1 00029 00030 00031 namespace FX { 00032 00033 00034 /// Half float (16-bit float) 00035 class FXAPI FXhalf { 00036 private: 00037 FXushort v; 00038 private: 00039 static const FXushort fhb[512]; 00040 static const FXuchar fhs[512]; 00041 static const FXuint hfm[2048]; 00042 static const FXuint hfe[64]; 00043 static const FXushort hfw[64]; 00044 public: 00045 FXhalf(){} 00046 00047 // Initialize half with half 00048 FXhalf(const FXhalf& h):v(h.v){} 00049 00050 // Initialize half with float 00051 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]); } 00052 00053 // Convert half to float 00054 operator FXfloat() const { union { FXuint u; FXfloat f; } r; r.u=hfm[hfw[v>>10]+(v&0x3ff)]+hfe[v>>10]; return r.f; } 00055 00056 // Test for zero 00057 FXbool operator!() const { return v==0; } 00058 00059 // Unary 00060 FXhalf operator+() const { return *this; } 00061 FXhalf operator-() const { FXhalf h; h.v=v^0x8000; return h; } 00062 00063 // Equality 00064 FXbool operator==(FXhalf h) const { return v==h.v; } 00065 FXbool operator!=(FXhalf h) const { return v!=h.v; } 00066 00067 // Assignment 00068 FXhalf& operator=(FXhalf h){ v=h.v; return *this; } 00069 FXhalf& operator=(FXfloat f){ *this=FXhalf(f); return *this; } 00070 00071 // Assignment operators 00072 FXhalf& operator+=(FXhalf h){ *this=FXhalf(FXfloat(*this)+FXfloat(h)); return *this; } 00073 FXhalf& operator+=(FXfloat f){ *this=FXhalf(FXfloat(*this)+f); return *this; } 00074 00075 FXhalf& operator-=(FXhalf h){ *this=FXhalf(FXfloat(*this)-FXfloat(h)); return *this; } 00076 FXhalf& operator-=(FXfloat f){ *this=FXhalf(FXfloat(*this)-f); return *this; } 00077 00078 FXhalf& operator*=(FXhalf h){ *this=FXhalf(FXfloat(*this)*FXfloat(h)); return *this; } 00079 FXhalf& operator*=(FXfloat f){ *this=FXhalf(FXfloat(*this)*f); return *this; } 00080 00081 FXhalf& operator/=(FXhalf h){ *this=FXhalf(FXfloat(*this)/FXfloat(h)); return *this; } 00082 FXhalf& operator/=(FXfloat f){ *this=FXhalf(FXfloat(*this)/f); return *this; } 00083 }; 00084 00085 00086 } 00087 00088 #endif
|
|