astcenc_mathlib_softfloat.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2011-2021 Arm Limited
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. // use this file except in compliance with the License. You may obtain a copy
  7. // of the License at:
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. // License for the specific language governing permissions and limitations
  15. // under the License.
  16. // ----------------------------------------------------------------------------
  17. /**
  18. * @brief Soft-float library for IEEE-754.
  19. */
  20. #if (ASTCENC_F16C == 0) && (ASTCENC_NEON == 0)
  21. #include "astcenc_mathlib.h"
  22. /* sized soft-float types. These are mapped to the sized integer
  23. types of C99, instead of C's floating-point types; this is because
  24. the library needs to maintain exact, bit-level control on all
  25. operations on these data types. */
  26. typedef uint16_t sf16;
  27. typedef uint32_t sf32;
  28. /******************************************
  29. helper functions and their lookup tables
  30. ******************************************/
  31. /* count leading zeros functions. Only used when the input is nonzero. */
  32. #if defined(__GNUC__) && (defined(__i386) || defined(__amd64))
  33. #elif defined(__arm__) && defined(__ARMCC_VERSION)
  34. #elif defined(__arm__) && defined(__GNUC__)
  35. #else
  36. /* table used for the slow default versions. */
  37. static const uint8_t clz_table[256] =
  38. {
  39. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  40. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  41. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  42. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  43. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  44. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  45. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  46. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  47. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  54. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  55. };
  56. #endif
  57. /*
  58. 32-bit count-leading-zeros function: use the Assembly instruction whenever possible. */
  59. static uint32_t clz32(uint32_t inp)
  60. {
  61. #if defined(__GNUC__) && (defined(__i386) || defined(__amd64))
  62. uint32_t bsr;
  63. __asm__("bsrl %1, %0": "=r"(bsr):"r"(inp | 1));
  64. return 31 - bsr;
  65. #else
  66. #if defined(__arm__) && defined(__ARMCC_VERSION)
  67. return __clz(inp); /* armcc builtin */
  68. #else
  69. #if defined(__arm__) && defined(__GNUC__)
  70. uint32_t lz;
  71. __asm__("clz %0, %1": "=r"(lz):"r"(inp));
  72. return lz;
  73. #else
  74. /* slow default version */
  75. uint32_t summa = 24;
  76. if (inp >= UINT32_C(0x10000))
  77. {
  78. inp >>= 16;
  79. summa -= 16;
  80. }
  81. if (inp >= UINT32_C(0x100))
  82. {
  83. inp >>= 8;
  84. summa -= 8;
  85. }
  86. return summa + clz_table[inp];
  87. #endif
  88. #endif
  89. #endif
  90. }
  91. /* the five rounding modes that IEEE-754r defines */
  92. typedef enum
  93. {
  94. SF_UP = 0, /* round towards positive infinity */
  95. SF_DOWN = 1, /* round towards negative infinity */
  96. SF_TOZERO = 2, /* round towards zero */
  97. SF_NEARESTEVEN = 3, /* round toward nearest value; if mid-between, round to even value */
  98. SF_NEARESTAWAY = 4 /* round toward nearest value; if mid-between, round away from zero */
  99. } roundmode;
  100. static uint32_t rtne_shift32(uint32_t inp, uint32_t shamt)
  101. {
  102. uint32_t vl1 = UINT32_C(1) << shamt;
  103. uint32_t inp2 = inp + (vl1 >> 1); /* added 0.5 ULP */
  104. uint32_t msk = (inp | UINT32_C(1)) & vl1; /* nonzero if odd. '| 1' forces it to 1 if the shamt is 0. */
  105. msk--; /* negative if even, nonnegative if odd. */
  106. inp2 -= (msk >> 31); /* subtract epsilon before shift if even. */
  107. inp2 >>= shamt;
  108. return inp2;
  109. }
  110. static uint32_t rtna_shift32(uint32_t inp, uint32_t shamt)
  111. {
  112. uint32_t vl1 = (UINT32_C(1) << shamt) >> 1;
  113. inp += vl1;
  114. inp >>= shamt;
  115. return inp;
  116. }
  117. static uint32_t rtup_shift32(uint32_t inp, uint32_t shamt)
  118. {
  119. uint32_t vl1 = UINT32_C(1) << shamt;
  120. inp += vl1;
  121. inp--;
  122. inp >>= shamt;
  123. return inp;
  124. }
  125. /* convert from FP16 to FP32. */
  126. static sf32 sf16_to_sf32(sf16 inp)
  127. {
  128. uint32_t inpx = inp;
  129. /*
  130. This table contains, for every FP16 sign/exponent value combination,
  131. the difference between the input FP16 value and the value obtained
  132. by shifting the correct FP32 result right by 13 bits.
  133. This table allows us to handle every case except denormals and NaN
  134. with just 1 table lookup, 2 shifts and 1 add.
  135. */
  136. #define WITH_MSB(a) (UINT32_C(a) | (1u << 31))
  137. static const uint32_t tbl[64] =
  138. {
  139. WITH_MSB(0x00000), 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000,
  140. 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000,
  141. 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000,
  142. 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, 0x1C000, WITH_MSB(0x38000),
  143. WITH_MSB(0x38000), 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000,
  144. 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000,
  145. 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000,
  146. 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, 0x54000, WITH_MSB(0x70000)
  147. };
  148. uint32_t res = tbl[inpx >> 10];
  149. res += inpx;
  150. /* Normal cases: MSB of 'res' not set. */
  151. if ((res & WITH_MSB(0)) == 0)
  152. {
  153. return res << 13;
  154. }
  155. /* Infinity and Zero: 10 LSB of 'res' not set. */
  156. if ((res & 0x3FF) == 0)
  157. {
  158. return res << 13;
  159. }
  160. /* NaN: the exponent field of 'inp' is non-zero. */
  161. if ((inpx & 0x7C00) != 0)
  162. {
  163. /* All NaNs are quietened. */
  164. return (res << 13) | 0x400000;
  165. }
  166. /* Denormal cases */
  167. uint32_t sign = (inpx & 0x8000) << 16;
  168. uint32_t mskval = inpx & 0x7FFF;
  169. uint32_t leadingzeroes = clz32(mskval);
  170. mskval <<= leadingzeroes;
  171. return (mskval >> 8) + ((0x85 - leadingzeroes) << 23) + sign;
  172. }
  173. /* Conversion routine that converts from FP32 to FP16. It supports denormals and all rounding modes. If a NaN is given as input, it is quietened. */
  174. static sf16 sf32_to_sf16(sf32 inp, roundmode rmode)
  175. {
  176. /* for each possible sign/exponent combination, store a case index. This gives a 512-byte table */
  177. static const uint8_t tab[512] {
  178. 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  179. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  180. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  181. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  182. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  183. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  184. 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  185. 20, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
  186. 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 40,
  187. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  188. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  189. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  190. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  191. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  192. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  193. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 50,
  194. 5, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  195. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  196. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  197. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  198. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  199. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  200. 15, 15, 15, 15, 15, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  201. 25, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  202. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 45,
  203. 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  204. 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  205. 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  206. 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  207. 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  208. 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  209. 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 55,
  210. };
  211. /* many of the cases below use a case-dependent magic constant. So we look up a magic constant before actually performing the switch. This table allows us to group cases, thereby minimizing code
  212. size. */
  213. static const uint32_t tabx[60] {
  214. UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0x8000), UINT32_C(0x80000000), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0x8000),
  215. UINT32_C(1), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0x8000), UINT32_C(0x8001), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0x8000),
  216. UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0x8000), UINT32_C(0x8000),
  217. UINT32_C(0xC8001FFF), UINT32_C(0xC8000000), UINT32_C(0xC8000000), UINT32_C(0xC8000FFF), UINT32_C(0xC8001000),
  218. UINT32_C(0x58000000), UINT32_C(0x38001FFF), UINT32_C(0x58000000), UINT32_C(0x58000FFF), UINT32_C(0x58001000),
  219. UINT32_C(0x7C00), UINT32_C(0x7BFF), UINT32_C(0x7BFF), UINT32_C(0x7C00), UINT32_C(0x7C00),
  220. UINT32_C(0xFBFF), UINT32_C(0xFC00), UINT32_C(0xFBFF), UINT32_C(0xFC00), UINT32_C(0xFC00),
  221. UINT32_C(0x90000000), UINT32_C(0x90000000), UINT32_C(0x90000000), UINT32_C(0x90000000), UINT32_C(0x90000000),
  222. UINT32_C(0x20000000), UINT32_C(0x20000000), UINT32_C(0x20000000), UINT32_C(0x20000000), UINT32_C(0x20000000)
  223. };
  224. uint32_t p;
  225. uint32_t idx = rmode + tab[inp >> 23];
  226. uint32_t vlx = tabx[idx];
  227. switch (idx)
  228. {
  229. /*
  230. Positive number which may be Infinity or NaN.
  231. We need to check whether it is NaN; if it is, quieten it by setting the top bit of the mantissa.
  232. (If we don't do this quieting, then a NaN that is distinguished only by having
  233. its low-order bits set, would be turned into an INF. */
  234. case 50:
  235. case 51:
  236. case 52:
  237. case 53:
  238. case 54:
  239. case 55:
  240. case 56:
  241. case 57:
  242. case 58:
  243. case 59:
  244. /*
  245. the input value is 0x7F800000 or 0xFF800000 if it is INF.
  246. By subtracting 1, we get 7F7FFFFF or FF7FFFFF, that is, bit 23 becomes zero.
  247. For NaNs, however, this operation will keep bit 23 with the value 1.
  248. We can then extract bit 23, and logical-OR bit 9 of the result with this
  249. bit in order to quieten the NaN (a Quiet NaN is a NaN where the top bit
  250. of the mantissa is set.)
  251. */
  252. p = (inp - 1) & UINT32_C(0x800000); /* zero if INF, nonzero if NaN. */
  253. return static_cast<sf16>(((inp + vlx) >> 13) | (p >> 14));
  254. /*
  255. positive, exponent = 0, round-mode == UP; need to check whether number actually is 0.
  256. If it is, then return 0, else return 1 (the smallest representable nonzero number)
  257. */
  258. case 0:
  259. /*
  260. -inp will set the MSB if the input number is nonzero.
  261. Thus (-inp) >> 31 will turn into 0 if the input number is 0 and 1 otherwise.
  262. */
  263. return static_cast<sf16>(static_cast<uint32_t>((-static_cast<int32_t>(inp))) >> 31);
  264. /*
  265. negative, exponent = , round-mode == DOWN, need to check whether number is
  266. actually 0. If it is, return 0x8000 ( float -0.0 )
  267. Else return the smallest negative number ( 0x8001 ) */
  268. case 6:
  269. /*
  270. in this case 'vlx' is 0x80000000. By subtracting the input value from it,
  271. we obtain a value that is 0 if the input value is in fact zero and has
  272. the MSB set if it isn't. We then right-shift the value by 31 places to
  273. get a value that is 0 if the input is -0.0 and 1 otherwise.
  274. */
  275. return static_cast<sf16>(((vlx - inp) >> 31) + UINT32_C(0x8000));
  276. /*
  277. for all other cases involving underflow/overflow, we don't need to
  278. do actual tests; we just return 'vlx'.
  279. */
  280. case 1:
  281. case 2:
  282. case 3:
  283. case 4:
  284. case 5:
  285. case 7:
  286. case 8:
  287. case 9:
  288. case 10:
  289. case 11:
  290. case 12:
  291. case 13:
  292. case 14:
  293. case 15:
  294. case 16:
  295. case 17:
  296. case 18:
  297. case 19:
  298. case 40:
  299. case 41:
  300. case 42:
  301. case 43:
  302. case 44:
  303. case 45:
  304. case 46:
  305. case 47:
  306. case 48:
  307. case 49:
  308. return static_cast<sf16>(vlx);
  309. /*
  310. for normal numbers, 'vlx' is the difference between the FP32 value of a number and the
  311. FP16 representation of the same number left-shifted by 13 places. In addition, a rounding constant is
  312. baked into 'vlx': for rounding-away-from zero, the constant is 2^13 - 1, causing roundoff away
  313. from zero. for round-to-nearest away, the constant is 2^12, causing roundoff away from zero.
  314. for round-to-nearest-even, the constant is 2^12 - 1. This causes correct round-to-nearest-even
  315. except for odd input numbers. For odd input numbers, we need to add 1 to the constant. */
  316. /* normal number, all rounding modes except round-to-nearest-even: */
  317. case 30:
  318. case 31:
  319. case 32:
  320. case 34:
  321. case 35:
  322. case 36:
  323. case 37:
  324. case 39:
  325. return static_cast<sf16>((inp + vlx) >> 13);
  326. /* normal number, round-to-nearest-even. */
  327. case 33:
  328. case 38:
  329. p = inp + vlx;
  330. p += (inp >> 13) & 1;
  331. return static_cast<sf16>(p >> 13);
  332. /*
  333. the various denormal cases. These are not expected to be common, so their performance is a bit
  334. less important. For each of these cases, we need to extract an exponent and a mantissa
  335. (including the implicit '1'!), and then right-shift the mantissa by a shift-amount that
  336. depends on the exponent. The shift must apply the correct rounding mode. 'vlx' is used to supply the
  337. sign of the resulting denormal number.
  338. */
  339. case 21:
  340. case 22:
  341. case 25:
  342. case 27:
  343. /* denormal, round towards zero. */
  344. p = 126 - ((inp >> 23) & 0xFF);
  345. return static_cast<sf16>((((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000)) >> p) | vlx);
  346. case 20:
  347. case 26:
  348. /* denormal, round away from zero. */
  349. p = 126 - ((inp >> 23) & 0xFF);
  350. return static_cast<sf16>(rtup_shift32((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000), p) | vlx);
  351. case 24:
  352. case 29:
  353. /* denormal, round to nearest-away */
  354. p = 126 - ((inp >> 23) & 0xFF);
  355. return static_cast<sf16>(rtna_shift32((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000), p) | vlx);
  356. case 23:
  357. case 28:
  358. /* denormal, round to nearest-even. */
  359. p = 126 - ((inp >> 23) & 0xFF);
  360. return static_cast<sf16>(rtne_shift32((inp & UINT32_C(0x7FFFFF)) + UINT32_C(0x800000), p) | vlx);
  361. }
  362. return 0;
  363. }
  364. /* convert from soft-float to native-float */
  365. float sf16_to_float(uint16_t p)
  366. {
  367. if32 i;
  368. i.u = sf16_to_sf32(p);
  369. return i.f;
  370. }
  371. /* convert from native-float to soft-float */
  372. uint16_t float_to_sf16(float p)
  373. {
  374. if32 i;
  375. i.f = p;
  376. return sf32_to_sf16(i.u, SF_NEARESTEVEN);
  377. }
  378. #endif