Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

osg::Real16 Class Reference

#include <OSGReal16.h>

List of all members.

Public Member Functions

 Real16 ()
 Real16 (float f)
 operator float () const
Real16 operator- () const
Real16operator= (Real16 h)
Real16operator= (float f)
Real16operator+= (Real16 h)
Real16operator+= (float f)
Real16operator-= (Real16 h)
Real16operator-= (float f)
Real16operator *= (Real16 h)
Real16operator *= (float f)
Real16operator/= (Real16 h)
Real16operator/= (float f)
Real16 round (unsigned int n) const
bool isFinite () const
bool isNormalized () const
bool isDenormalized () const
bool isZero () const
bool isNan () const
bool isInfinity () const
bool isNegative () const
unsigned short bits () const
void setBits (unsigned short bits)

Static Public Member Functions

static Real16 posInf ()
static Real16 negInf ()
static Real16 qNan ()
static Real16 sNan ()

Static Private Member Functions

static short convert (int i)
static float overflow ()
static bool selftest ()

Private Attributes

unsigned short _h

Static Private Attributes

static const uif _toFloat [1<< 16]
static const unsigned short _eLut [1<< 9]
static const bool _itWorks

Classes

union  uif


Detailed Description

Definition at line 94 of file OSGReal16.h.


Constructor & Destructor Documentation

osg::Real16::Real16  )  [inline]
 

Definition at line 421 of file OSGReal16.h.

Referenced by operator *=(), operator+=(), operator-=(), operator/=(), and operator=().

00422 {
00423     // no initialization
00424 }

osg::Real16::Real16 float  f  )  [inline]
 

Definition at line 432 of file OSGReal16.h.

References _eLut, _h, convert(), osg::Real16::uif::f, and osg::Real16::uif::i.

00433 {
00434     if (f == 0)
00435     {
00436     //
00437     // Common special case - zero.
00438     // For speed, we don't preserve the zero's sign.
00439     //
00440 
00441     _h = 0;
00442     }
00443     else
00444     {
00445     //
00446     // We extract the combined sign and exponent, e, from our
00447     // floating-point number, f.  Then we convert e to the sign
00448     // and exponent of the half number via a table lookup.
00449     //
00450     // For the most common case, where a normalized half is produced,
00451     // the table lookup returns a non-zero value; in this case, all
00452     // we have to do, is round f's significand to 10 bits and combine
00453     // the result with e.
00454     //
00455     // For all other cases (overflow, zeroes, denormalized numbers
00456     // resulting from underflow, infinities and NANs), the table
00457     // lookup returns zero, and we call a longer, non-inline function
00458     // to do the float-to-half conversion.
00459     //
00460 
00461     uif x;
00462 
00463     x.f = f;
00464 
00465     register int e = (x.i >> 23) & 0x000001ff;
00466 
00467     e = _eLut[e];
00468 
00469     if (e)
00470     {
00471         //
00472         // Simple case - round the significand and
00473         // combine it with the sign and exponent.
00474         //
00475 
00476         _h = e + (((x.i & 0x007fffff) + 0x00001000) >> 13);
00477     }
00478     else
00479     {
00480         //
00481         // Difficult case - call a function.
00482         //
00483 
00484         _h = convert (x.i);
00485     }
00486     }
00487 }


Member Function Documentation

osg::Real16::operator float  )  const [inline]
 

Definition at line 495 of file OSGReal16.h.

References _h, _toFloat, and osg::Real16::uif::f.

00496 {
00497     return _toFloat[_h].f;
00498 }

Real16 osg::Real16::operator-  )  const [inline]
 

Definition at line 565 of file OSGReal16.h.

References _h.

00566 {
00567     Real16 h;
00568     h._h = _h ^ 0x8000;
00569     return h;
00570 }

Real16 & osg::Real16::operator= Real16  h  )  [inline]
 

Definition at line 574 of file OSGReal16.h.

References _h.

00575 {
00576     _h = h._h;
00577     return *this;
00578 }

Real16 & osg::Real16::operator= float  f  )  [inline]
 

Definition at line 582 of file OSGReal16.h.

References Real16().

00583 {
00584     *this = Real16 (f);
00585     return *this;
00586 }

Real16 & osg::Real16::operator+= Real16  h  )  [inline]
 

Definition at line 590 of file OSGReal16.h.

References Real16().

00591 {
00592     *this = Real16 (float (*this) + float (h));
00593     return *this;
00594 }

Real16 & osg::Real16::operator+= float  f  )  [inline]
 

Definition at line 598 of file OSGReal16.h.

References Real16().

00599 {
00600     *this = Real16 (float (*this) + f);
00601     return *this;
00602 }

Real16 & osg::Real16::operator-= Real16  h  )  [inline]
 

Definition at line 606 of file OSGReal16.h.

References Real16().

00607 {
00608     *this = Real16 (float (*this) - float (h));
00609     return *this;
00610 }

Real16 & osg::Real16::operator-= float  f  )  [inline]
 

Definition at line 614 of file OSGReal16.h.

References Real16().

00615 {
00616     *this = Real16 (float (*this) - f);
00617     return *this;
00618 }

Real16 & osg::Real16::operator *= Real16  h  )  [inline]
 

Definition at line 622 of file OSGReal16.h.

References Real16().

00623 {
00624     *this = Real16 (float (*this) * float (h));
00625     return *this;
00626 }

Real16 & osg::Real16::operator *= float  f  )  [inline]
 

Definition at line 630 of file OSGReal16.h.

References Real16().

00631 {
00632     *this = Real16 (float (*this) * f);
00633     return *this;
00634 }

Real16 & osg::Real16::operator/= Real16  h  )  [inline]
 

Definition at line 638 of file OSGReal16.h.

References Real16().

00639 {
00640     *this = Real16 (float (*this) / float (h));
00641     return *this;
00642 }

Real16 & osg::Real16::operator/= float  f  )  [inline]
 

Definition at line 646 of file OSGReal16.h.

References Real16().

00647 {
00648     *this = Real16 (float (*this) / f);
00649     return *this;
00650 }

Real16 osg::Real16::round unsigned int  n  )  const [inline]
 

Definition at line 506 of file OSGReal16.h.

References _h.

00507 {
00508     //
00509     // Parameter check.
00510     //
00511 
00512     if (n >= 10)
00513     return *this;
00514 
00515     //
00516     // Disassemble h into the sign, s,
00517     // and the combined exponent and significand, e.
00518     //
00519 
00520     unsigned short s = _h & 0x8000;
00521     unsigned short e = _h & 0x7fff;
00522 
00523     //
00524     // Round the exponent and significand to the nearest value
00525     // where ones occur only in the (10-n) most significant bits.
00526     // Note that the exponent adjusts automatically if rounding
00527     // up causes the significand to overflow.
00528     //
00529 
00530     e >>= 9 - n;
00531     e  += e & 1;
00532     e <<= 9 - n;
00533 
00534     //
00535     // Check for exponent overflow.
00536     //
00537 
00538     if (e >= 0x7c00)
00539     {
00540     //
00541     // Overflow occurred -- truncate instead of rounding.
00542     //
00543 
00544     e = _h;
00545     e >>= 10 - n;
00546     e <<= 10 - n;
00547     }
00548 
00549     //
00550     // Put the original sign bit back.
00551     //
00552 
00553     Real16 h;
00554     h._h = s | e;
00555 
00556     return h;
00557 }

bool osg::Real16::isFinite  )  const [inline]
 

Definition at line 654 of file OSGReal16.h.

References _h.

00655 {
00656     unsigned short e = (_h >> 10) & 0x001f;
00657     return e < 31;
00658 }

bool osg::Real16::isNormalized  )  const [inline]
 

Definition at line 662 of file OSGReal16.h.

References _h.

00663 {
00664     unsigned short e = (_h >> 10) & 0x001f;
00665     return e > 0 && e < 31;
00666 }

bool osg::Real16::isDenormalized  )  const [inline]
 

Definition at line 670 of file OSGReal16.h.

References _h.

00671 {
00672     unsigned short e = (_h >> 10) & 0x001f;
00673     unsigned short m =  _h & 0x3ff;
00674     return e == 0 && m != 0;
00675 }

bool osg::Real16::isZero  )  const [inline]
 

Definition at line 679 of file OSGReal16.h.

References _h.

00680 {
00681     return (_h & 0x7fff) == 0;
00682 }

bool osg::Real16::isNan  )  const [inline]
 

Definition at line 686 of file OSGReal16.h.

References _h.

00687 {
00688     unsigned short e = (_h >> 10) & 0x001f;
00689     unsigned short m =  _h & 0x3ff;
00690     return e == 31 && m != 0;
00691 }

bool osg::Real16::isInfinity  )  const [inline]
 

Definition at line 695 of file OSGReal16.h.

References _h.

00696 {
00697     unsigned short e = (_h >> 10) & 0x001f;
00698     unsigned short m =  _h & 0x3ff;
00699     return e == 31 && m == 0;
00700 }

bool osg::Real16::isNegative  )  const [inline]
 

Definition at line 704 of file OSGReal16.h.

References _h.

00705 {
00706     return (_h & 0x8000) != 0;
00707 }

Real16 osg::Real16::posInf  )  [inline, static]
 

Definition at line 711 of file OSGReal16.h.

References _h.

00712 {
00713     Real16 h;
00714     h._h = 0x7c00;
00715     return h;
00716 }

Real16 osg::Real16::negInf  )  [inline, static]
 

Definition at line 720 of file OSGReal16.h.

References _h.

00721 {
00722     Real16 h;
00723     h._h = 0xfc00;
00724     return h;
00725 }

Real16 osg::Real16::qNan  )  [inline, static]
 

Definition at line 729 of file OSGReal16.h.

References _h.

00730 {
00731     Real16 h;
00732     h._h = 0x7fff;
00733     return h;
00734 }

Real16 osg::Real16::sNan  )  [inline, static]
 

Definition at line 738 of file OSGReal16.h.

References _h.

00739 {
00740     Real16 h;
00741     h._h = 0x7dff;
00742     return h;
00743 }

unsigned short osg::Real16::bits  )  const [inline]
 

Definition at line 747 of file OSGReal16.h.

References _h.

Referenced by osg::printBits(), printBits(), and osg::BinaryDataHandler::putValue().

00748 {
00749     return _h;
00750 }

void osg::Real16::setBits unsigned short  bits  )  [inline]
 

Definition at line 754 of file OSGReal16.h.

References _h.

Referenced by osg::BinaryDataHandler::getValue(), and osg::BinaryDataHandler::getValues().

00755 {
00756     _h = bits;
00757 }

short Real16::convert int  i  )  [static, private]
 

Definition at line 16554 of file OSGReal16.cpp.

References overflow().

Referenced by Real16().

16555 {
16556     //
16557     // Our floating point number, f, is represented by the bit
16558     // pattern in integer i.  Disassemble that bit pattern into
16559     // the sign, s, the exponent, e, and the significand, m.
16560     // Shift s into the position where it will go in in the
16561     // resulting half number.
16562     // Adjust e, accounting for the different exponent bias
16563     // of float and half (127 versus 15).
16564     //
16565 
16566     register int s =  (i >> 16) & 0x00008000;
16567     register int e = ((i >> 23) & 0x000000ff) - (127 - 15);
16568     register int m =   i        & 0x007fffff;
16569 
16570     //
16571     // Now reassemble s, e and m into a half:
16572     //
16573 
16574     if (e <= 0)
16575     {
16576     if (e < -10)
16577     {
16578         //
16579         // E is less than -10.  The absolute value of f is
16580         // less than REAL16_MIN (f may be a small normalized
16581         // float, a denormalized float or a zero).
16582         //
16583         // We convert f to a half zero.
16584         //
16585 
16586         return 0;
16587     }
16588 
16589     //
16590     // E is between -10 and 0.  F is a normalized float,
16591     // whose magnitude is less than REAL16_NRM_MIN.
16592     //
16593     // We convert f to a denormalized half.
16594     // 
16595 
16596     m = (m | 0x00800000) >> (1 - e);
16597 
16598     //
16599     // Round to nearest, round "0.5" up.
16600     //
16601     // Rounding may cause the significand to overflow and make
16602     // our number normalized.  Because of the way a half's bits
16603     // are laid out, we don't have to treat this case separately;
16604     // the code below will handle it correctly.
16605     // 
16606 
16607     if (m &  0x00001000)
16608         m += 0x00002000;
16609 
16610     //
16611     // Assemble the half from s, e (zero) and m.
16612     //
16613 
16614     return s | (m >> 13);
16615     }
16616     else if (e == 0xff - (127 - 15))
16617     {
16618     if (m == 0)
16619     {
16620         //
16621         // F is an infinity; convert f to a half
16622         // infinity with the same sign as f.
16623         //
16624 
16625         return s | 0x7c00;
16626     }
16627     else
16628     {
16629         //
16630         // F is a NAN; we produce a half NAN that preserves
16631         // the sign bit and the 10 leftmost bits of the
16632         // significand of f, with one exception: If the 10
16633         // leftmost bits are all zero, the NAN would turn 
16634         // into an infinity, so we have to set at least one
16635         // bit in the significand.
16636         //
16637 
16638         m >>= 13;
16639         return s | 0x7c00 | m | (m == 0);
16640     }
16641     }
16642     else
16643     {
16644     //
16645     // E is greater than zero.  F is a normalized float.
16646     // We try to convert f to a normalized half.
16647     //
16648 
16649     //
16650     // Round to nearest, round "0.5" up
16651     //
16652 
16653     if (m &  0x00001000)
16654     {
16655         m += 0x00002000;
16656 
16657         if (m & 0x00800000)
16658         {
16659         m =  0;        // overflow in significand,
16660         e += 1;        // adjust exponent
16661         }
16662     }
16663 
16664     //
16665     // Handle exponent overflow
16666     //
16667 
16668     if (e > 30)
16669     {
16670         overflow ();    // Cause a hardware floating point overflow;
16671         return s | 0x7c00;    // if this returns, the half becomes an
16672     }               // infinity with the same sign as f.
16673 
16674     //
16675     // Assemble the half from s, e and m.
16676     //
16677 
16678     return s | (e << 10) | (m >> 13);
16679     }
16680 }

float Real16::overflow  )  [static, private]
 

Definition at line 16537 of file OSGReal16.cpp.

Referenced by convert().

16538 {
16539     volatile float f = 1e10;
16540 
16541     for (int i = 0; i < 10; i++)    
16542     f *= f;                // this will overflow before
16543                     // the for­loop terminates
16544     return f;
16545 }

bool Real16::selftest  )  [static, private]
 

Definition at line 16726 of file OSGReal16.cpp.

References REAL16_MAX, REAL16_MIN, REAL16_NRM_MIN, testDenormalized(), and testNormalized().

16727 {
16728     testNormalized   ((float)  REAL16_MAX);
16729     testNormalized   ((float) -REAL16_MAX);
16730     testNormalized   ( 0.1f);
16731     testNormalized   (-0.1f);
16732     testNormalized   ( 0.5f);
16733     testNormalized   (-0.5f);
16734     testNormalized   ( 1.0f);
16735     testNormalized   (-1.0f);
16736     testNormalized   ( 2.0f);
16737     testNormalized   (-2.0f);
16738     testNormalized   ( 3.0f);
16739     testNormalized   (-3.0f);
16740     testNormalized   ( 17.0f);
16741     testNormalized   (-17.0f);
16742     testNormalized   ((float)  REAL16_NRM_MIN);
16743     testNormalized   ((float) -REAL16_NRM_MIN);
16744     testDenormalized ((float)  REAL16_MIN);
16745     testDenormalized ((float) -REAL16_MIN);
16746     testDenormalized ( 0.0f);
16747     testDenormalized (-0.0f);
16748 
16749     return true;
16750 }


Member Data Documentation

unsigned short osg::Real16::_h [private]
 

Definition at line 221 of file OSGReal16.h.

Referenced by bits(), isDenormalized(), isFinite(), isInfinity(), isNan(), isNegative(), isNormalized(), isZero(), negInf(), operator float(), operator-(), operator=(), posInf(), qNan(), Real16(), round(), setBits(), and sNan().

const Real16::uif Real16::_toFloat [static, private]
 

Definition at line 59 of file OSGReal16.cpp.

Referenced by operator float().

const unsigned short Real16::_eLut [static, private]
 

Definition at line 16453 of file OSGReal16.cpp.

Referenced by Real16().

const bool Real16::_itWorks [static, private]
 

Initial value:

Definition at line 16526 of file OSGReal16.cpp.


The documentation for this class was generated from the following files:
Generated on Thu Aug 25 04:12:36 2005 for OpenSG by  doxygen 1.4.3