F16.h 3.1 KB

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