source: trunk/src/half.h @ 869

Last change on this file since 869 was 869, checked in by sam, 8 years ago

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

  • Property svn:keywords set to Id
File size: 1.5 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11//
12// The Half class
13// --------------
14//
15
16#if !defined __LOL_HALF_H__
17#define __LOL_HALF_H__
18
19#include <stdint.h>
20
21namespace lol
22{
23
24class half
25{
26private:
27    uint16_t m_bits;
28
29public:
30    inline half() { }
31
32    inline half(float f)
33    {
34        *this = makefast(f);
35    }
36
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 */
67    static half makeslow(float f);
68    static half makefast(float f);
69    static inline half makebits(uint16_t x)
70    {
71        half ret;
72        ret.m_bits = x;
73        return ret;
74    }
75};
76
77} /* namespace lol */
78
79#endif // __LOL_HALF_H__
80
Note: See TracBrowser for help on using the repository browser.