| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <AnKi/Util/StdTypes.h>
- namespace anki {
- /// @addtogroup util_other
- /// @{
- /// Half float
- class F16
- {
- /// @name Friends with F32
- /// @{
- friend F32 operator+(const F32 f, const F16 h)
- {
- return f + h.toF32();
- }
- friend F32 operator-(const F32 f, const F16 h)
- {
- return f - h.toF32();
- }
- friend F32 operator*(const F32 f, const F16 h)
- {
- return f * h.toF32();
- }
- friend F32 operator/(const F32 f, const F16 h)
- {
- return f / h.toF32();
- }
- /// @}
- /// @name Friends with F64
- /// @{
- friend F64 operator+(const F64 f, const F16 h)
- {
- return f + h.toF32();
- }
- friend F64 operator-(const F64 f, const F16 h)
- {
- return f - h.toF32();
- }
- friend F64 operator*(const F64 f, const F16 h)
- {
- return f * h.toF32();
- }
- friend F64 operator/(const F64 f, const F16 h)
- {
- return f / h.toF32();
- }
- /// @}
- public:
- /// @name Constructors
- /// @{
- F16()
- {
- static_assert(sizeof(F16) == 2, "Incorrect size");
- }
- F16(const F16& b)
- {
- m_data = b.m_data;
- }
- explicit F16(const F64 f)
- {
- *this = toF16(F32(f));
- }
- explicit F16(const F32 f)
- {
- *this = toF16(f);
- }
- explicit F16(const U16 ui)
- {
- m_data = ui;
- }
- /// @}
- /// @name Operators with same type
- /// @{
- F16& operator=(const F16 b)
- {
- m_data = b.m_data;
- return *this;
- }
- F16 operator+(const F16 b) const
- {
- return toF16(toF32() + b.toF32());
- }
- F16& operator+=(const F16 b)
- {
- *this = toF16(toF32() + b.toF32());
- return *this;
- }
- F16 operator-(const F16 b) const
- {
- return toF16(toF32() - b.toF32());
- }
- F16& operator-=(const F16 b)
- {
- *this = toF16(toF32() - b.toF32());
- return *this;
- }
- F16 operator*(const F16 b) const
- {
- return toF16(toF32() * b.toF32());
- }
- F16& operator*=(const F16 b)
- {
- *this = toF16(toF32() * b.toF32());
- return *this;
- }
- F16 operator/(const F16 b) const
- {
- return toF16(toF32() / b.toF32());
- }
- F16& operator/=(const F16 b)
- {
- *this = toF16(toF32() / b.toF32());
- return *this;
- }
- Bool operator==(const F16 b) const
- {
- return m_data == b.m_data;
- }
- Bool operator!=(const F16 b) const
- {
- return m_data != b.m_data;
- }
- /// @}
- /// @name Operators with F32
- /// @{
- F32 operator+(const F32 b) const
- {
- return toF32() + b;
- }
- F32 operator-(const F32 b) const
- {
- return toF32() - b;
- }
- F32 operator*(const F32 b) const
- {
- return toF32() * b;
- }
- F32 operator/(const F32 b) const
- {
- return toF32() / b;
- }
- /// @}
- /// @name Operators with F64
- /// @{
- F64 operator+(const F64 b) const
- {
- return toF32() + b;
- }
- F64 operator-(const F64 b) const
- {
- return toF32() - b;
- }
- F64 operator*(const F64 b) const
- {
- return toF32() * b;
- }
- F64 operator/(const F64 b) const
- {
- return toF32() / b;
- }
- /// @}
- /// @name Other
- /// @{
- F32 toF32() const
- {
- return toF32(*this);
- }
- U16 toU16() const
- {
- return m_data;
- }
- /// @}
- private:
- U16 m_data;
- static F32 toF32(F16 h);
- static F16 toF16(F32 f);
- };
- /// @}
- } // end namespace anki
|