| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #ifndef ANKI_MATH_F16_H
- #define ANKI_MATH_F16_H
- #include "anki/math/CommonIncludes.h"
- namespace anki {
- /// @addtogroup Math
- /// @{
- /// Half float
- class F16
- {
- /// @name Friends
- /// @{
- friend F32 operator+(const F32 f, const F16 h);
- friend F32 operator-(const F32 f, const F16 h);
- friend F32 operator*(const F32 f, const F16 h);
- friend F32 operator/(const F32 f, const F16 h);
- /// @}
- public:
- /// @name Constructors
- /// @{
- explicit F16()
- {}
- F16(const F16& b)
- {
- data = b.data;
- }
- explicit F16(const F32 f)
- {
- *this = toF16(f);
- }
- explicit F16(const U16 ui)
- {
- data = ui;
- }
- /// @}
- /// @name Operators with same type
- /// @{
- F16& operator=(const F16 b)
- {
- data = b.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 data == b.data;
- }
- Bool operator!=(const F16 b) const
- {
- return data != b.data;
- }
- /// @}
- /// @name Operators with F32
- /// @{
- F16& operator=(const F32 b)
- {
- *this = toF16(b);
- return *this;
- }
- F32 operator+(const F32 b) const
- {
- return toF32() + b;
- }
- F16& operator+=(const F32 b)
- {
- *this = toF16(toF32() + b);
- return *this;
- }
- F32 operator-(const F32 b) const
- {
- return toF32() - b;
- }
- F16& operator-=(const F32 b)
- {
- *this = toF16(toF32() - b);
- return *this;
- }
- F32 operator*(const F32 b) const
- {
- return toF32() * b;
- }
- F16& operator*=(const F32 b)
- {
- *this = toF16(toF32() * b);
- return *this;
- }
- F32 operator/(const F32 b) const
- {
- return toF32() / b;
- }
- F16& operator/=(const F32 b)
- {
- *this = toF16(toF32() / b);
- return *this;
- }
- Bool operator==(const F32 b) const
- {
- return toF32() == b;
- }
- Bool operator!=(const F32 b) const
- {
- return toF32() != b;
- }
- /// @}
- /// @name Other
- /// @{
- F32 toF32() const
- {
- return toF32(*this);
- }
- U16 toU16() const
- {
- return data;
- }
- /// @}
- private:
- U16 data;
- static F32 toF32(F16 h);
- static F16 toF16(F32 f);
- };
- /// @name F16 friends
- /// @{
- inline F32 operator+(const F32 f, const F16 h)
- {
- return f + h.toF32();
- }
- inline F32 operator-(const F32 f, const F16 h)
- {
- return f - h.toF32();
- }
- inline F32 operator*(const F32 f, const F16 h)
- {
- return f * h.toF32();
- }
- inline F32 operator/(const F32 f, const F16 h)
- {
- return f / h.toF32();
- }
- /// @}
- /// @}
- static_assert(sizeof(F16) == 2, "Incorrect size");
- } // end namespace anki
- #endif
|