fp16.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #ifndef FP16_FP16_H
  4. #define FP16_FP16_H
  5. #if defined(__cplusplus) && (__cplusplus >= 201103L)
  6. #include <cstdint>
  7. #include <cmath>
  8. #elif !defined(__OPENCL_VERSION__)
  9. #include <stdint.h>
  10. #include <math.h>
  11. #endif
  12. #ifdef _MSC_VER
  13. #include <intrin.h>
  14. #endif
  15. #include <fp16/bitcasts.h>
  16. /*
  17. * Convert a 16-bit floating-point number in IEEE half-precision format, in bit representation, to
  18. * a 32-bit floating-point number in IEEE single-precision format, in bit representation.
  19. *
  20. * @note The implementation doesn't use any floating-point operations.
  21. */
  22. static inline uint32_t fp16_ieee_to_fp32_bits(uint16_t h) {
  23. /*
  24. * Extend the half-precision floating-point number to 32 bits and shift to the upper part of the 32-bit word:
  25. * +---+-----+------------+-------------------+
  26. * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
  27. * +---+-----+------------+-------------------+
  28. * Bits 31 26-30 16-25 0-15
  29. *
  30. * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0 - zero bits.
  31. */
  32. const uint32_t w = (uint32_t) h << 16;
  33. /*
  34. * Extract the sign of the input number into the high bit of the 32-bit word:
  35. *
  36. * +---+----------------------------------+
  37. * | S |0000000 00000000 00000000 00000000|
  38. * +---+----------------------------------+
  39. * Bits 31 0-31
  40. */
  41. const uint32_t sign = w & UINT32_C(0x80000000);
  42. /*
  43. * Extract mantissa and biased exponent of the input number into the bits 0-30 of the 32-bit word:
  44. *
  45. * +---+-----+------------+-------------------+
  46. * | 0 |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
  47. * +---+-----+------------+-------------------+
  48. * Bits 30 27-31 17-26 0-16
  49. */
  50. const uint32_t nonsign = w & UINT32_C(0x7FFFFFFF);
  51. /*
  52. * Renorm shift is the number of bits to shift mantissa left to make the half-precision number normalized.
  53. * If the initial number is normalized, some of its high 6 bits (sign == 0 and 5-bit exponent) equals one.
  54. * In this case renorm_shift == 0. If the number is denormalize, renorm_shift > 0. Note that if we shift
  55. * denormalized nonsign by renorm_shift, the unit bit of mantissa will shift into exponent, turning the
  56. * biased exponent into 1, and making mantissa normalized (i.e. without leading 1).
  57. */
  58. #ifdef _MSC_VER
  59. unsigned long nonsign_bsr;
  60. _BitScanReverse(&nonsign_bsr, (unsigned long) nonsign);
  61. uint32_t renorm_shift = (uint32_t) nonsign_bsr ^ 31;
  62. #else
  63. uint32_t renorm_shift = __builtin_clz(nonsign);
  64. #endif
  65. renorm_shift = renorm_shift > 5 ? renorm_shift - 5 : 0;
  66. /*
  67. * Iff half-precision number has exponent of 15, the addition overflows it into bit 31,
  68. * and the subsequent shift turns the high 9 bits into 1. Thus
  69. * inf_nan_mask ==
  70. * 0x7F800000 if the half-precision number had exponent of 15 (i.e. was NaN or infinity)
  71. * 0x00000000 otherwise
  72. */
  73. const int32_t inf_nan_mask = ((int32_t) (nonsign + 0x04000000) >> 8) & INT32_C(0x7F800000);
  74. /*
  75. * Iff nonsign is 0, it overflows into 0xFFFFFFFF, turning bit 31 into 1. Otherwise, bit 31 remains 0.
  76. * The signed shift right by 31 broadcasts bit 31 into all bits of the zero_mask. Thus
  77. * zero_mask ==
  78. * 0xFFFFFFFF if the half-precision number was zero (+0.0h or -0.0h)
  79. * 0x00000000 otherwise
  80. */
  81. const int32_t zero_mask = (int32_t) (nonsign - 1) >> 31;
  82. /*
  83. * 1. Shift nonsign left by renorm_shift to normalize it (if the input was denormal)
  84. * 2. Shift nonsign right by 3 so the exponent (5 bits originally) becomes an 8-bit field and 10-bit mantissa
  85. * shifts into the 10 high bits of the 23-bit mantissa of IEEE single-precision number.
  86. * 3. Add 0x70 to the exponent (starting at bit 23) to compensate the different in exponent bias
  87. * (0x7F for single-precision number less 0xF for half-precision number).
  88. * 4. Subtract renorm_shift from the exponent (starting at bit 23) to account for renormalization. As renorm_shift
  89. * is less than 0x70, this can be combined with step 3.
  90. * 5. Binary OR with inf_nan_mask to turn the exponent into 0xFF if the input was NaN or infinity.
  91. * 6. Binary ANDNOT with zero_mask to turn the mantissa and exponent into zero if the input was zero.
  92. * 7. Combine with the sign of the input number.
  93. */
  94. return sign | ((((nonsign << renorm_shift >> 3) + ((0x70 - renorm_shift) << 23)) | inf_nan_mask) & ~zero_mask);
  95. }
  96. /*
  97. * Convert a 16-bit floating-point number in IEEE half-precision format, in bit representation, to
  98. * a 32-bit floating-point number in IEEE single-precision format.
  99. *
  100. * @note The implementation relies on IEEE-like (no assumption about rounding mode and no operations on denormals)
  101. * floating-point operations and bitcasts between integer and floating-point variables.
  102. */
  103. static inline float fp16_ieee_to_fp32_value(uint16_t h) {
  104. /*
  105. * Extend the half-precision floating-point number to 32 bits and shift to the upper part of the 32-bit word:
  106. * +---+-----+------------+-------------------+
  107. * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
  108. * +---+-----+------------+-------------------+
  109. * Bits 31 26-30 16-25 0-15
  110. *
  111. * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0 - zero bits.
  112. */
  113. const uint32_t w = (uint32_t) h << 16;
  114. /*
  115. * Extract the sign of the input number into the high bit of the 32-bit word:
  116. *
  117. * +---+----------------------------------+
  118. * | S |0000000 00000000 00000000 00000000|
  119. * +---+----------------------------------+
  120. * Bits 31 0-31
  121. */
  122. const uint32_t sign = w & UINT32_C(0x80000000);
  123. /*
  124. * Extract mantissa and biased exponent of the input number into the high bits of the 32-bit word:
  125. *
  126. * +-----+------------+---------------------+
  127. * |EEEEE|MM MMMM MMMM|0 0000 0000 0000 0000|
  128. * +-----+------------+---------------------+
  129. * Bits 27-31 17-26 0-16
  130. */
  131. const uint32_t two_w = w + w;
  132. /*
  133. * Shift mantissa and exponent into bits 23-28 and bits 13-22 so they become mantissa and exponent
  134. * of a single-precision floating-point number:
  135. *
  136. * S|Exponent | Mantissa
  137. * +-+---+-----+------------+----------------+
  138. * |0|000|EEEEE|MM MMMM MMMM|0 0000 0000 0000|
  139. * +-+---+-----+------------+----------------+
  140. * Bits | 23-31 | 0-22
  141. *
  142. * Next, there are some adjustments to the exponent:
  143. * - The exponent needs to be corrected by the difference in exponent bias between single-precision and half-precision
  144. * formats (0x7F - 0xF = 0x70)
  145. * - Inf and NaN values in the inputs should become Inf and NaN values after conversion to the single-precision number.
  146. * Therefore, if the biased exponent of the half-precision input was 0x1F (max possible value), the biased exponent
  147. * of the single-precision output must be 0xFF (max possible value). We do this correction in two steps:
  148. * - First, we adjust the exponent by (0xFF - 0x1F) = 0xE0 (see exp_offset below) rather than by 0x70 suggested
  149. * by the difference in the exponent bias (see above).
  150. * - Then we multiply the single-precision result of exponent adjustment by 2**(-112) to reverse the effect of
  151. * exponent adjustment by 0xE0 less the necessary exponent adjustment by 0x70 due to difference in exponent bias.
  152. * The floating-point multiplication hardware would ensure than Inf and NaN would retain their value on at least
  153. * partially IEEE754-compliant implementations.
  154. *
  155. * Note that the above operations do not handle denormal inputs (where biased exponent == 0). However, they also do not
  156. * operate on denormal inputs, and do not produce denormal results.
  157. */
  158. const uint32_t exp_offset = UINT32_C(0xE0) << 23;
  159. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
  160. const float exp_scale = 0x1.0p-112f;
  161. #else
  162. const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
  163. #endif
  164. const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
  165. /*
  166. * Convert denormalized half-precision inputs into single-precision results (always normalized).
  167. * Zero inputs are also handled here.
  168. *
  169. * In a denormalized number the biased exponent is zero, and mantissa has on-zero bits.
  170. * First, we shift mantissa into bits 0-9 of the 32-bit word.
  171. *
  172. * zeros | mantissa
  173. * +---------------------------+------------+
  174. * |0000 0000 0000 0000 0000 00|MM MMMM MMMM|
  175. * +---------------------------+------------+
  176. * Bits 10-31 0-9
  177. *
  178. * Now, remember that denormalized half-precision numbers are represented as:
  179. * FP16 = mantissa * 2**(-24).
  180. * The trick is to construct a normalized single-precision number with the same mantissa and thehalf-precision input
  181. * and with an exponent which would scale the corresponding mantissa bits to 2**(-24).
  182. * A normalized single-precision floating-point number is represented as:
  183. * FP32 = (1 + mantissa * 2**(-23)) * 2**(exponent - 127)
  184. * Therefore, when the biased exponent is 126, a unit change in the mantissa of the input denormalized half-precision
  185. * number causes a change of the constructud single-precision number by 2**(-24), i.e. the same ammount.
  186. *
  187. * The last step is to adjust the bias of the constructed single-precision number. When the input half-precision number
  188. * is zero, the constructed single-precision number has the value of
  189. * FP32 = 1 * 2**(126 - 127) = 2**(-1) = 0.5
  190. * Therefore, we need to subtract 0.5 from the constructed single-precision number to get the numerical equivalent of
  191. * the input half-precision number.
  192. */
  193. const uint32_t magic_mask = UINT32_C(126) << 23;
  194. const float magic_bias = 0.5f;
  195. const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
  196. /*
  197. * - Choose either results of conversion of input as a normalized number, or as a denormalized number, depending on the
  198. * input exponent. The variable two_w contains input exponent in bits 27-31, therefore if its smaller than 2**27, the
  199. * input is either a denormal number, or zero.
  200. * - Combine the result of conversion of exponent and mantissa with the sign of the input number.
  201. */
  202. const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
  203. const uint32_t result = sign |
  204. (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
  205. return fp32_from_bits(result);
  206. }
  207. /*
  208. * Convert a 32-bit floating-point number in IEEE single-precision format to a 16-bit floating-point number in
  209. * IEEE half-precision format, in bit representation.
  210. *
  211. * @note The implementation relies on IEEE-like (no assumption about rounding mode and no operations on denormals)
  212. * floating-point operations and bitcasts between integer and floating-point variables.
  213. */
  214. static inline uint16_t fp16_ieee_from_fp32_value(float f) {
  215. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
  216. const float scale_to_inf = 0x1.0p+112f;
  217. const float scale_to_zero = 0x1.0p-110f;
  218. #else
  219. const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
  220. const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
  221. #endif
  222. float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
  223. const uint32_t w = fp32_to_bits(f);
  224. const uint32_t shl1_w = w + w;
  225. const uint32_t sign = w & UINT32_C(0x80000000);
  226. uint32_t bias = shl1_w & UINT32_C(0xFF000000);
  227. if (bias < UINT32_C(0x71000000)) {
  228. bias = UINT32_C(0x71000000);
  229. }
  230. base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
  231. const uint32_t bits = fp32_to_bits(base);
  232. const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
  233. const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
  234. const uint32_t nonsign = exp_bits + mantissa_bits;
  235. return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
  236. }
  237. /*
  238. * Convert a 16-bit floating-point number in ARM alternative half-precision format, in bit representation, to
  239. * a 32-bit floating-point number in IEEE single-precision format, in bit representation.
  240. *
  241. * @note The implementation doesn't use any floating-point operations.
  242. */
  243. static inline uint32_t fp16_alt_to_fp32_bits(uint16_t h) {
  244. /*
  245. * Extend the half-precision floating-point number to 32 bits and shift to the upper part of the 32-bit word:
  246. * +---+-----+------------+-------------------+
  247. * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
  248. * +---+-----+------------+-------------------+
  249. * Bits 31 26-30 16-25 0-15
  250. *
  251. * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0 - zero bits.
  252. */
  253. const uint32_t w = (uint32_t) h << 16;
  254. /*
  255. * Extract the sign of the input number into the high bit of the 32-bit word:
  256. *
  257. * +---+----------------------------------+
  258. * | S |0000000 00000000 00000000 00000000|
  259. * +---+----------------------------------+
  260. * Bits 31 0-31
  261. */
  262. const uint32_t sign = w & UINT32_C(0x80000000);
  263. /*
  264. * Extract mantissa and biased exponent of the input number into the bits 0-30 of the 32-bit word:
  265. *
  266. * +---+-----+------------+-------------------+
  267. * | 0 |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
  268. * +---+-----+------------+-------------------+
  269. * Bits 30 27-31 17-26 0-16
  270. */
  271. const uint32_t nonsign = w & UINT32_C(0x7FFFFFFF);
  272. /*
  273. * Renorm shift is the number of bits to shift mantissa left to make the half-precision number normalized.
  274. * If the initial number is normalized, some of its high 6 bits (sign == 0 and 5-bit exponent) equals one.
  275. * In this case renorm_shift == 0. If the number is denormalize, renorm_shift > 0. Note that if we shift
  276. * denormalized nonsign by renorm_shift, the unit bit of mantissa will shift into exponent, turning the
  277. * biased exponent into 1, and making mantissa normalized (i.e. without leading 1).
  278. */
  279. #ifdef _MSC_VER
  280. unsigned long nonsign_bsr;
  281. _BitScanReverse(&nonsign_bsr, (unsigned long) nonsign);
  282. uint32_t renorm_shift = (uint32_t) nonsign_bsr ^ 31;
  283. #else
  284. uint32_t renorm_shift = __builtin_clz(nonsign);
  285. #endif
  286. renorm_shift = renorm_shift > 5 ? renorm_shift - 5 : 0;
  287. /*
  288. * Iff nonsign is 0, it overflows into 0xFFFFFFFF, turning bit 31 into 1. Otherwise, bit 31 remains 0.
  289. * The signed shift right by 31 broadcasts bit 31 into all bits of the zero_mask. Thus
  290. * zero_mask ==
  291. * 0xFFFFFFFF if the half-precision number was zero (+0.0h or -0.0h)
  292. * 0x00000000 otherwise
  293. */
  294. const int32_t zero_mask = (int32_t) (nonsign - 1) >> 31;
  295. /*
  296. * 1. Shift nonsign left by renorm_shift to normalize it (if the input was denormal)
  297. * 2. Shift nonsign right by 3 so the exponent (5 bits originally) becomes an 8-bit field and 10-bit mantissa
  298. * shifts into the 10 high bits of the 23-bit mantissa of IEEE single-precision number.
  299. * 3. Add 0x70 to the exponent (starting at bit 23) to compensate the different in exponent bias
  300. * (0x7F for single-precision number less 0xF for half-precision number).
  301. * 4. Subtract renorm_shift from the exponent (starting at bit 23) to account for renormalization. As renorm_shift
  302. * is less than 0x70, this can be combined with step 3.
  303. * 5. Binary ANDNOT with zero_mask to turn the mantissa and exponent into zero if the input was zero.
  304. * 6. Combine with the sign of the input number.
  305. */
  306. return sign | (((nonsign << renorm_shift >> 3) + ((0x70 - renorm_shift) << 23)) & ~zero_mask);
  307. }
  308. /*
  309. * Convert a 16-bit floating-point number in ARM alternative half-precision format, in bit representation, to
  310. * a 32-bit floating-point number in IEEE single-precision format.
  311. *
  312. * @note The implementation relies on IEEE-like (no assumption about rounding mode and no operations on denormals)
  313. * floating-point operations and bitcasts between integer and floating-point variables.
  314. */
  315. static inline float fp16_alt_to_fp32_value(uint16_t h) {
  316. /*
  317. * Extend the half-precision floating-point number to 32 bits and shift to the upper part of the 32-bit word:
  318. * +---+-----+------------+-------------------+
  319. * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
  320. * +---+-----+------------+-------------------+
  321. * Bits 31 26-30 16-25 0-15
  322. *
  323. * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0 - zero bits.
  324. */
  325. const uint32_t w = (uint32_t) h << 16;
  326. /*
  327. * Extract the sign of the input number into the high bit of the 32-bit word:
  328. *
  329. * +---+----------------------------------+
  330. * | S |0000000 00000000 00000000 00000000|
  331. * +---+----------------------------------+
  332. * Bits 31 0-31
  333. */
  334. const uint32_t sign = w & UINT32_C(0x80000000);
  335. /*
  336. * Extract mantissa and biased exponent of the input number into the high bits of the 32-bit word:
  337. *
  338. * +-----+------------+---------------------+
  339. * |EEEEE|MM MMMM MMMM|0 0000 0000 0000 0000|
  340. * +-----+------------+---------------------+
  341. * Bits 27-31 17-26 0-16
  342. */
  343. const uint32_t two_w = w + w;
  344. /*
  345. * Shift mantissa and exponent into bits 23-28 and bits 13-22 so they become mantissa and exponent
  346. * of a single-precision floating-point number:
  347. *
  348. * S|Exponent | Mantissa
  349. * +-+---+-----+------------+----------------+
  350. * |0|000|EEEEE|MM MMMM MMMM|0 0000 0000 0000|
  351. * +-+---+-----+------------+----------------+
  352. * Bits | 23-31 | 0-22
  353. *
  354. * Next, the exponent is adjusted for the difference in exponent bias between single-precision and half-precision
  355. * formats (0x7F - 0xF = 0x70). This operation never overflows or generates non-finite values, as the largest
  356. * half-precision exponent is 0x1F and after the adjustment is can not exceed 0x8F < 0xFE (largest single-precision
  357. * exponent for non-finite values).
  358. *
  359. * Note that this operation does not handle denormal inputs (where biased exponent == 0). However, they also do not
  360. * operate on denormal inputs, and do not produce denormal results.
  361. */
  362. const uint32_t exp_offset = UINT32_C(0x70) << 23;
  363. const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset);
  364. /*
  365. * Convert denormalized half-precision inputs into single-precision results (always normalized).
  366. * Zero inputs are also handled here.
  367. *
  368. * In a denormalized number the biased exponent is zero, and mantissa has on-zero bits.
  369. * First, we shift mantissa into bits 0-9 of the 32-bit word.
  370. *
  371. * zeros | mantissa
  372. * +---------------------------+------------+
  373. * |0000 0000 0000 0000 0000 00|MM MMMM MMMM|
  374. * +---------------------------+------------+
  375. * Bits 10-31 0-9
  376. *
  377. * Now, remember that denormalized half-precision numbers are represented as:
  378. * FP16 = mantissa * 2**(-24).
  379. * The trick is to construct a normalized single-precision number with the same mantissa and thehalf-precision input
  380. * and with an exponent which would scale the corresponding mantissa bits to 2**(-24).
  381. * A normalized single-precision floating-point number is represented as:
  382. * FP32 = (1 + mantissa * 2**(-23)) * 2**(exponent - 127)
  383. * Therefore, when the biased exponent is 126, a unit change in the mantissa of the input denormalized half-precision
  384. * number causes a change of the constructud single-precision number by 2**(-24), i.e. the same ammount.
  385. *
  386. * The last step is to adjust the bias of the constructed single-precision number. When the input half-precision number
  387. * is zero, the constructed single-precision number has the value of
  388. * FP32 = 1 * 2**(126 - 127) = 2**(-1) = 0.5
  389. * Therefore, we need to subtract 0.5 from the constructed single-precision number to get the numerical equivalent of
  390. * the input half-precision number.
  391. */
  392. const uint32_t magic_mask = UINT32_C(126) << 23;
  393. const float magic_bias = 0.5f;
  394. const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
  395. /*
  396. * - Choose either results of conversion of input as a normalized number, or as a denormalized number, depending on the
  397. * input exponent. The variable two_w contains input exponent in bits 27-31, therefore if its smaller than 2**27, the
  398. * input is either a denormal number, or zero.
  399. * - Combine the result of conversion of exponent and mantissa with the sign of the input number.
  400. */
  401. const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
  402. const uint32_t result = sign |
  403. (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
  404. return fp32_from_bits(result);
  405. }
  406. /*
  407. * Convert a 32-bit floating-point number in IEEE single-precision format to a 16-bit floating-point number in
  408. * ARM alternative half-precision format, in bit representation.
  409. *
  410. * @note The implementation relies on IEEE-like (no assumption about rounding mode and no operations on denormals)
  411. * floating-point operations and bitcasts between integer and floating-point variables.
  412. */
  413. static inline uint16_t fp16_alt_from_fp32_value(float f) {
  414. const uint32_t w = fp32_to_bits(f);
  415. const uint32_t sign = w & UINT32_C(0x80000000);
  416. const uint32_t shl1_w = w + w;
  417. const uint32_t shl1_max_fp16_fp32 = UINT32_C(0x8FFFC000);
  418. const uint32_t shl1_base = shl1_w > shl1_max_fp16_fp32 ? shl1_max_fp16_fp32 : shl1_w;
  419. uint32_t shl1_bias = shl1_base & UINT32_C(0xFF000000);
  420. const uint32_t exp_difference = 23 - 10;
  421. const uint32_t shl1_bias_min = (127 - 1 - exp_difference) << 24;
  422. if (shl1_bias < shl1_bias_min) {
  423. shl1_bias = shl1_bias_min;
  424. }
  425. const float bias = fp32_from_bits((shl1_bias >> 1) + ((exp_difference + 2) << 23));
  426. const float base = fp32_from_bits((shl1_base >> 1) + (2 << 23)) + bias;
  427. const uint32_t exp_f = fp32_to_bits(base) >> 13;
  428. return (sign >> 16) | ((exp_f & UINT32_C(0x00007C00)) + (fp32_to_bits(base) & UINT32_C(0x00000FFF)));
  429. }
  430. #endif /* FP16_FP16_H */
  431. #else
  432. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  433. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)