Changeset 869 for trunk/src/half.h


Ignore:
Timestamp:
Aug 28, 2011, 11:07:06 PM (8 years ago)
Author:
sam
Message:

core: add a half to float conversion routine and utility floating point
functions such as isnan(), isinf() etc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/half.h

    r867 r869  
    3535    }
    3636
     37    inline int isnan() const
     38    {
     39        return ((m_bits & 0x7c00u) == 0x7c00u) && (m_bits & 0x03ffu);
     40    }
     41
     42    inline int isfinite() const
     43    {
     44        return (m_bits & 0x7c00u) != 0x7c00u;
     45    }
     46
     47    inline int isinf() const
     48    {
     49        return (uint16_t)(m_bits << 1) == (0x7c00u << 1);
     50    }
     51
     52    inline int isnormal() const
     53    {
     54        return (isfinite() && (m_bits & 0x7c00u)) || ((m_bits & 0x7fffu) == 0);
     55    }
     56
     57    inline uint16_t bits()
     58    {
     59        return m_bits;
     60    }
     61
     62    /* Cast to other types */
     63    operator float() const;
     64    inline operator int() const { return (int)(float)*this; }
     65
     66    /* Factories */
    3767    static half makeslow(float f);
    3868    static half makefast(float f);
    39 
    4069    static inline half makebits(uint16_t x)
    4170    {
     
    4372        ret.m_bits = x;
    4473        return ret;
    45     }
    46 
    47     inline operator float() const
    48     {
    49         int s = m_bits & 0x8000u;
    50         int e = m_bits & 0x7c00u;
    51         int m = m_bits & 0x03ffu;
    52 
    53         union { float f; uint32_t x; } u;
    54         u.x = 0;
    55         u.x |= s << 16;
    56         u.x |= (-15 + (e >> 10) + 127) << 23;
    57         u.x |= m << 13;
    58 
    59         return u.f;
    60     }
    61 
    62     inline uint16_t bits()
    63     {
    64         return m_bits;
    6574    }
    6675};
Note: See TracChangeset for help on using the changeset viewer.