#include <OSGReal16.h>
Public Member Functions | |
| Real16 () | |
| Real16 (float f) | |
| operator float () const | |
| Real16 | operator- () const |
| Real16 & | operator= (Real16 h) |
| Real16 & | operator= (float f) |
| Real16 & | operator+= (Real16 h) |
| Real16 & | operator+= (float f) |
| Real16 & | operator-= (Real16 h) |
| Real16 & | operator-= (float f) |
| Real16 & | operator*= (Real16 h) |
| Real16 & | operator*= (float f) |
| Real16 & | operator/= (Real16 h) |
| Real16 & | operator/= (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 |
Definition at line 94 of file OSGReal16.h.
| osg::Real16::Real16 | ( | ) | [inline] |
Definition at line 421 of file OSGReal16.h.
Referenced by operator*=(), operator+=(), operator-=(), operator/=(), operator=(), and osg::operator>>().
| 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 }
| osg::Real16::operator float | ( | ) | const [inline] |
| Real16 osg::Real16::operator- | ( | ) | const [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 }
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 }
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 }
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 }
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(), 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().
| short osg::Real16::convert | ( | int | i | ) | [static, private] |
Definition at line 16555 of file OSGReal16.cpp.
References overflow().
Referenced by Real16().
16556 { 16557 // 16558 // Our floating point number, f, is represented by the bit 16559 // pattern in integer i. Disassemble that bit pattern into 16560 // the sign, s, the exponent, e, and the significand, m. 16561 // Shift s into the position where it will go in in the 16562 // resulting half number. 16563 // Adjust e, accounting for the different exponent bias 16564 // of float and half (127 versus 15). 16565 // 16566 16567 register int s = (i >> 16) & 0x00008000; 16568 register int e = ((i >> 23) & 0x000000ff) - (127 - 15); 16569 register int m = i & 0x007fffff; 16570 16571 // 16572 // Now reassemble s, e and m into a half: 16573 // 16574 16575 if (e <= 0) 16576 { 16577 if (e < -10) 16578 { 16579 // 16580 // E is less than -10. The absolute value of f is 16581 // less than REAL16_MIN (f may be a small normalized 16582 // float, a denormalized float or a zero). 16583 // 16584 // We convert f to a half zero. 16585 // 16586 16587 return 0; 16588 } 16589 16590 // 16591 // E is between -10 and 0. F is a normalized float, 16592 // whose magnitude is less than REAL16_NRM_MIN. 16593 // 16594 // We convert f to a denormalized half. 16595 // 16596 16597 m = (m | 0x00800000) >> (1 - e); 16598 16599 // 16600 // Round to nearest, round "0.5" up. 16601 // 16602 // Rounding may cause the significand to overflow and make 16603 // our number normalized. Because of the way a half's bits 16604 // are laid out, we don't have to treat this case separately; 16605 // the code below will handle it correctly. 16606 // 16607 16608 if (m & 0x00001000) 16609 m += 0x00002000; 16610 16611 // 16612 // Assemble the half from s, e (zero) and m. 16613 // 16614 16615 return s | (m >> 13); 16616 } 16617 else if (e == 0xff - (127 - 15)) 16618 { 16619 if (m == 0) 16620 { 16621 // 16622 // F is an infinity; convert f to a half 16623 // infinity with the same sign as f. 16624 // 16625 16626 return s | 0x7c00; 16627 } 16628 else 16629 { 16630 // 16631 // F is a NAN; we produce a half NAN that preserves 16632 // the sign bit and the 10 leftmost bits of the 16633 // significand of f, with one exception: If the 10 16634 // leftmost bits are all zero, the NAN would turn 16635 // into an infinity, so we have to set at least one 16636 // bit in the significand. 16637 // 16638 16639 m >>= 13; 16640 return s | 0x7c00 | m | (m == 0); 16641 } 16642 } 16643 else 16644 { 16645 // 16646 // E is greater than zero. F is a normalized float. 16647 // We try to convert f to a normalized half. 16648 // 16649 16650 // 16651 // Round to nearest, round "0.5" up 16652 // 16653 16654 if (m & 0x00001000) 16655 { 16656 m += 0x00002000; 16657 16658 if (m & 0x00800000) 16659 { 16660 m = 0; // overflow in significand, 16661 e += 1; // adjust exponent 16662 } 16663 } 16664 16665 // 16666 // Handle exponent overflow 16667 // 16668 16669 if (e > 30) 16670 { 16671 overflow (); // Cause a hardware floating point overflow; 16672 return s | 0x7c00; // if this returns, the half becomes an 16673 } // infinity with the same sign as f. 16674 16675 // 16676 // Assemble the half from s, e and m. 16677 // 16678 16679 return s | (e << 10) | (m >> 13); 16680 } 16681 }
| float osg::Real16::overflow | ( | ) | [static, private] |
Definition at line 16538 of file OSGReal16.cpp.
Referenced by convert().
16539 { 16540 volatile float f = 1e10; 16541 16542 for (int i = 0; i < 10; i++) 16543 f *= f; // this will overflow before 16544 // the forloop terminates 16545 return f; 16546 }
| bool osg::Real16::selftest | ( | ) | [static, private] |
Definition at line 16727 of file OSGReal16.cpp.
References REAL16_MAX, REAL16_MIN, REAL16_NRM_MIN, osg::testDenormalized(), and osg::testNormalized().
16728 { 16729 testNormalized ((float) REAL16_MAX); 16730 testNormalized ((float) -REAL16_MAX); 16731 testNormalized ( 0.1f); 16732 testNormalized (-0.1f); 16733 testNormalized ( 0.5f); 16734 testNormalized (-0.5f); 16735 testNormalized ( 1.0f); 16736 testNormalized (-1.0f); 16737 testNormalized ( 2.0f); 16738 testNormalized (-2.0f); 16739 testNormalized ( 3.0f); 16740 testNormalized (-3.0f); 16741 testNormalized ( 17.0f); 16742 testNormalized (-17.0f); 16743 testNormalized ((float) REAL16_NRM_MIN); 16744 testNormalized ((float) -REAL16_NRM_MIN); 16745 testDenormalized ((float) REAL16_MIN); 16746 testDenormalized ((float) -REAL16_MIN); 16747 testDenormalized ( 0.0f); 16748 testDenormalized (-0.0f); 16749 16750 return true; 16751 }
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 osg::Real16::_toFloat [static, private] |
const unsigned short osg::Real16::_eLut [static, private] |
const bool osg::Real16::_itWorks [static, private] |
1.5.5