2
0

F16.h 3.1 KB

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