BFloat16-math.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #pragma once
  2. #include <c10/util/BFloat16.h>
  3. #include <c10/util/Half.h>
  4. C10_CLANG_DIAGNOSTIC_PUSH()
  5. #if C10_CLANG_HAS_WARNING("-Wimplicit-float-conversion")
  6. C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-float-conversion")
  7. #endif
  8. namespace c10 {
  9. template <typename T>
  10. struct is_reduced_floating_point
  11. : std::integral_constant<
  12. bool,
  13. std::is_same_v<T, c10::Half> || std::is_same_v<T, c10::BFloat16>> {};
  14. template <typename T>
  15. constexpr bool is_reduced_floating_point_v =
  16. is_reduced_floating_point<T>::value;
  17. } // namespace c10
  18. namespace std {
  19. #if !defined(FBCODE_CAFFE2) && !defined(C10_NODEPRECATED)
  20. using c10::is_reduced_floating_point;
  21. using c10::is_reduced_floating_point_v;
  22. #endif // !defined(FBCODE_CAFFE2) && !defined(C10_NODEPRECATED)
  23. template <
  24. typename T,
  25. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  26. inline T acos(T a) {
  27. return std::acos(float(a));
  28. }
  29. template <
  30. typename T,
  31. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  32. inline T asin(T a) {
  33. return std::asin(float(a));
  34. }
  35. template <
  36. typename T,
  37. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  38. inline T atan(T a) {
  39. return std::atan(float(a));
  40. }
  41. template <
  42. typename T,
  43. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  44. inline T atanh(T a) {
  45. return std::atanh(float(a));
  46. }
  47. template <
  48. typename T,
  49. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  50. inline T erf(T a) {
  51. return std::erf(float(a));
  52. }
  53. template <
  54. typename T,
  55. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  56. inline T erfc(T a) {
  57. return std::erfc(float(a));
  58. }
  59. template <
  60. typename T,
  61. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  62. inline T exp(T a) {
  63. return std::exp(float(a));
  64. }
  65. template <
  66. typename T,
  67. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  68. inline T expm1(T a) {
  69. return std::expm1(float(a));
  70. }
  71. template <
  72. typename T,
  73. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  74. inline bool isfinite(T a) {
  75. return std::isfinite(float(a));
  76. }
  77. template <
  78. typename T,
  79. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  80. inline T log(T a) {
  81. return std::log(float(a));
  82. }
  83. template <
  84. typename T,
  85. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  86. inline T log10(T a) {
  87. return std::log10(float(a));
  88. }
  89. template <
  90. typename T,
  91. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  92. inline T log1p(T a) {
  93. return std::log1p(float(a));
  94. }
  95. template <
  96. typename T,
  97. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  98. inline T log2(T a) {
  99. return std::log2(float(a));
  100. }
  101. template <
  102. typename T,
  103. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  104. inline T ceil(T a) {
  105. return std::ceil(float(a));
  106. }
  107. template <
  108. typename T,
  109. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  110. inline T cos(T a) {
  111. return std::cos(float(a));
  112. }
  113. template <
  114. typename T,
  115. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  116. inline T floor(T a) {
  117. return std::floor(float(a));
  118. }
  119. template <
  120. typename T,
  121. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  122. inline T nearbyint(T a) {
  123. return std::nearbyint(float(a));
  124. }
  125. template <
  126. typename T,
  127. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  128. inline T sin(T a) {
  129. return std::sin(float(a));
  130. }
  131. template <
  132. typename T,
  133. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  134. inline T tan(T a) {
  135. return std::tan(float(a));
  136. }
  137. template <
  138. typename T,
  139. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  140. inline T sinh(T a) {
  141. return std::sinh(float(a));
  142. }
  143. template <
  144. typename T,
  145. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  146. inline T cosh(T a) {
  147. return std::cosh(float(a));
  148. }
  149. template <
  150. typename T,
  151. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  152. inline T tanh(T a) {
  153. return std::tanh(float(a));
  154. }
  155. template <
  156. typename T,
  157. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  158. inline T trunc(T a) {
  159. return std::trunc(float(a));
  160. }
  161. template <
  162. typename T,
  163. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  164. inline T lgamma(T a) {
  165. return std::lgamma(float(a));
  166. }
  167. template <
  168. typename T,
  169. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  170. inline T sqrt(T a) {
  171. return std::sqrt(float(a));
  172. }
  173. template <
  174. typename T,
  175. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  176. inline T rsqrt(T a) {
  177. return 1.0 / std::sqrt(float(a));
  178. }
  179. template <
  180. typename T,
  181. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  182. inline T abs(T a) {
  183. return std::abs(float(a));
  184. }
  185. #if defined(_MSC_VER) && defined(__CUDACC__)
  186. template <
  187. typename T,
  188. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  189. inline T pow(T a, double b) {
  190. return std::pow(float(a), float(b));
  191. }
  192. #else
  193. template <
  194. typename T,
  195. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  196. inline T pow(T a, double b) {
  197. return std::pow(float(a), b);
  198. }
  199. #endif
  200. template <
  201. typename T,
  202. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  203. inline T pow(T a, T b) {
  204. return std::pow(float(a), float(b));
  205. }
  206. template <
  207. typename T,
  208. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  209. inline T fmod(T a, T b) {
  210. return std::fmod(float(a), float(b));
  211. }
  212. /*
  213. The following function is inspired from the implementation in `musl`
  214. Link to License: https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
  215. ----------------------------------------------------------------------
  216. Copyright © 2005-2020 Rich Felker, et al.
  217. Permission is hereby granted, free of charge, to any person obtaining
  218. a copy of this software and associated documentation files (the
  219. "Software"), to deal in the Software without restriction, including
  220. without limitation the rights to use, copy, modify, merge, publish,
  221. distribute, sublicense, and/or sell copies of the Software, and to
  222. permit persons to whom the Software is furnished to do so, subject to
  223. the following conditions:
  224. The above copyright notice and this permission notice shall be
  225. included in all copies or substantial portions of the Software.
  226. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  227. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  228. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  229. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  230. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  231. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  232. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  233. ----------------------------------------------------------------------
  234. */
  235. template <
  236. typename T,
  237. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  238. C10_HOST_DEVICE inline T nextafter(T from, T to) {
  239. // Reference:
  240. // https://git.musl-libc.org/cgit/musl/tree/src/math/nextafter.c
  241. using int_repr_t = uint16_t;
  242. constexpr uint8_t bits = 16;
  243. union {
  244. T f;
  245. int_repr_t i;
  246. } ufrom = {from}, uto = {to};
  247. // get a mask to get the sign bit i.e. MSB
  248. int_repr_t sign_mask = int_repr_t{1} << (bits - 1);
  249. // short-circuit: if either is NaN, return NaN
  250. if (from != from || to != to) {
  251. return from + to;
  252. }
  253. // short-circuit: if they are exactly the same.
  254. if (ufrom.i == uto.i) {
  255. return from;
  256. }
  257. // mask the sign-bit to zero i.e. positive
  258. // equivalent to abs(x)
  259. int_repr_t abs_from = ufrom.i & ~sign_mask;
  260. int_repr_t abs_to = uto.i & ~sign_mask;
  261. if (abs_from == 0) {
  262. // if both are zero but with different sign,
  263. // preserve the sign of `to`.
  264. if (abs_to == 0) {
  265. return to;
  266. }
  267. // smallest subnormal with sign of `to`.
  268. ufrom.i = (uto.i & sign_mask) | int_repr_t{1};
  269. return ufrom.f;
  270. }
  271. // if abs(from) > abs(to) or sign(from) != sign(to)
  272. if (abs_from > abs_to || ((ufrom.i ^ uto.i) & sign_mask)) {
  273. ufrom.i--;
  274. } else {
  275. ufrom.i++;
  276. }
  277. return ufrom.f;
  278. }
  279. } // namespace std
  280. C10_CLANG_DIAGNOSTIC_POP()