2
0

F16.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #ifndef ANKI_MATH_F16_H
  2. #define ANKI_MATH_F16_H
  3. #include "anki/math/CommonIncludes.h"
  4. namespace anki {
  5. /// @addtogroup Math
  6. /// @{
  7. /// Half float
  8. class F16
  9. {
  10. /// @name Friends
  11. /// @{
  12. friend F32 operator+(const F32 f, const F16 h);
  13. friend F32 operator-(const F32 f, const F16 h);
  14. friend F32 operator*(const F32 f, const F16 h);
  15. friend F32 operator/(const F32 f, const F16 h);
  16. /// @}
  17. public:
  18. /// @name Constructors
  19. /// @{
  20. explicit F16()
  21. {}
  22. F16(const F16& b)
  23. {
  24. data = b.data;
  25. }
  26. explicit F16(const F32 f)
  27. {
  28. *this = toF16(f);
  29. }
  30. explicit F16(const U16 ui)
  31. {
  32. data = ui;
  33. }
  34. /// @}
  35. /// @name Operators with same type
  36. /// @{
  37. F16& operator=(const F16 b)
  38. {
  39. data = b.data;
  40. return *this;
  41. }
  42. F16 operator+(const F16 b) const
  43. {
  44. return toF16(toF32() + b.toF32());
  45. }
  46. F16& operator+=(const F16 b)
  47. {
  48. *this = toF16(toF32() + b.toF32());
  49. return *this;
  50. }
  51. F16 operator-(const F16 b) const
  52. {
  53. return toF16(toF32() - b.toF32());
  54. }
  55. F16& operator-=(const F16 b)
  56. {
  57. *this = toF16(toF32() - b.toF32());
  58. return *this;
  59. }
  60. F16 operator*(const F16 b) const
  61. {
  62. return toF16(toF32() * b.toF32());
  63. }
  64. F16& operator*=(const F16 b)
  65. {
  66. *this = toF16(toF32() * b.toF32());
  67. return *this;
  68. }
  69. F16 operator/(const F16 b) const
  70. {
  71. return toF16(toF32() / b.toF32());
  72. }
  73. F16& operator/=(const F16 b)
  74. {
  75. *this = toF16(toF32() / b.toF32());
  76. return *this;
  77. }
  78. Bool operator==(const F16 b) const
  79. {
  80. return data == b.data;
  81. }
  82. Bool operator!=(const F16 b) const
  83. {
  84. return data != b.data;
  85. }
  86. /// @}
  87. /// @name Operators with F32
  88. /// @{
  89. F16& operator=(const F32 b)
  90. {
  91. *this = toF16(b);
  92. return *this;
  93. }
  94. F32 operator+(const F32 b) const
  95. {
  96. return toF32() + b;
  97. }
  98. F16& operator+=(const F32 b)
  99. {
  100. *this = toF16(toF32() + b);
  101. return *this;
  102. }
  103. F32 operator-(const F32 b) const
  104. {
  105. return toF32() - b;
  106. }
  107. F16& operator-=(const F32 b)
  108. {
  109. *this = toF16(toF32() - b);
  110. return *this;
  111. }
  112. F32 operator*(const F32 b) const
  113. {
  114. return toF32() * b;
  115. }
  116. F16& operator*=(const F32 b)
  117. {
  118. *this = toF16(toF32() * b);
  119. return *this;
  120. }
  121. F32 operator/(const F32 b) const
  122. {
  123. return toF32() / b;
  124. }
  125. F16& operator/=(const F32 b)
  126. {
  127. *this = toF16(toF32() / b);
  128. return *this;
  129. }
  130. Bool operator==(const F32 b) const
  131. {
  132. return toF32() == b;
  133. }
  134. Bool operator!=(const F32 b) const
  135. {
  136. return toF32() != b;
  137. }
  138. /// @}
  139. /// @name Other
  140. /// @{
  141. F32 toF32() const
  142. {
  143. return toF32(*this);
  144. }
  145. U16 toU16() const
  146. {
  147. return data;
  148. }
  149. /// @}
  150. private:
  151. U16 data;
  152. static F32 toF32(F16 h);
  153. static F16 toF16(F32 f);
  154. };
  155. /// @name F16 friends
  156. /// @{
  157. inline F32 operator+(const F32 f, const F16 h)
  158. {
  159. return f + h.toF32();
  160. }
  161. inline F32 operator-(const F32 f, const F16 h)
  162. {
  163. return f - h.toF32();
  164. }
  165. inline F32 operator*(const F32 f, const F16 h)
  166. {
  167. return f * h.toF32();
  168. }
  169. inline F32 operator/(const F32 f, const F16 h)
  170. {
  171. return f / h.toF32();
  172. }
  173. /// @}
  174. /// @}
  175. static_assert(sizeof(F16) == 2, "Incorrect size");
  176. } // end namespace anki
  177. #endif