extended.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* Software floating-point emulation.
  2. Definitions for IEEE Extended Precision.
  3. Copyright (C) 1999,2006,2007 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Jakub Jelinek (jj@ultra.linux.cz).
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. In addition to the permissions in the GNU Lesser General Public
  11. License, the Free Software Foundation gives you unlimited
  12. permission to link the compiled version of this file into
  13. combinations with other programs, and to distribute those
  14. combinations without any restriction coming from the use of this
  15. file. (The Lesser General Public License restrictions do apply in
  16. other respects; for example, they cover modification of the file,
  17. and distribution when not linked into a combine executable.)
  18. The GNU C Library is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. Lesser General Public License for more details.
  22. You should have received a copy of the GNU Lesser General Public
  23. License along with the GNU C Library; if not, write to the Free
  24. Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  25. MA 02110-1301, USA. */
  26. #if _FP_W_TYPE_SIZE < 32
  27. #error "Here's a nickel, kid. Go buy yourself a real computer."
  28. #endif
  29. #if _FP_W_TYPE_SIZE < 64
  30. #define _FP_FRACTBITS_E (4*_FP_W_TYPE_SIZE)
  31. #else
  32. #define _FP_FRACTBITS_E (2*_FP_W_TYPE_SIZE)
  33. #endif
  34. #define _FP_FRACBITS_E 64
  35. #define _FP_FRACXBITS_E (_FP_FRACTBITS_E - _FP_FRACBITS_E)
  36. #define _FP_WFRACBITS_E (_FP_WORKBITS + _FP_FRACBITS_E)
  37. #define _FP_WFRACXBITS_E (_FP_FRACTBITS_E - _FP_WFRACBITS_E)
  38. #define _FP_EXPBITS_E 15
  39. #define _FP_EXPBIAS_E 16383
  40. #define _FP_EXPMAX_E 32767
  41. #define _FP_QNANBIT_E \
  42. ((_FP_W_TYPE)1 << (_FP_FRACBITS_E-2) % _FP_W_TYPE_SIZE)
  43. #define _FP_QNANBIT_SH_E \
  44. ((_FP_W_TYPE)1 << (_FP_FRACBITS_E-2+_FP_WORKBITS) % _FP_W_TYPE_SIZE)
  45. #define _FP_IMPLBIT_E \
  46. ((_FP_W_TYPE)1 << (_FP_FRACBITS_E-1) % _FP_W_TYPE_SIZE)
  47. #define _FP_IMPLBIT_SH_E \
  48. ((_FP_W_TYPE)1 << (_FP_FRACBITS_E-1+_FP_WORKBITS) % _FP_W_TYPE_SIZE)
  49. #define _FP_OVERFLOW_E \
  50. ((_FP_W_TYPE)1 << (_FP_WFRACBITS_E % _FP_W_TYPE_SIZE))
  51. typedef float XFtype __attribute__((mode(XF)));
  52. #if _FP_W_TYPE_SIZE < 64
  53. union _FP_UNION_E
  54. {
  55. XFtype flt;
  56. struct
  57. {
  58. #if __BYTE_ORDER == __BIG_ENDIAN
  59. unsigned long pad1 : _FP_W_TYPE_SIZE;
  60. unsigned long pad2 : (_FP_W_TYPE_SIZE - 1 - _FP_EXPBITS_E);
  61. unsigned long sign : 1;
  62. unsigned long exp : _FP_EXPBITS_E;
  63. unsigned long frac1 : _FP_W_TYPE_SIZE;
  64. unsigned long frac0 : _FP_W_TYPE_SIZE;
  65. #else
  66. unsigned long frac0 : _FP_W_TYPE_SIZE;
  67. unsigned long frac1 : _FP_W_TYPE_SIZE;
  68. unsigned exp : _FP_EXPBITS_E;
  69. unsigned sign : 1;
  70. #endif /* not bigendian */
  71. } bits __attribute__((packed));
  72. };
  73. #define FP_DECL_E(X) _FP_DECL(4,X)
  74. #define FP_UNPACK_RAW_E(X, val) \
  75. do { \
  76. union _FP_UNION_E _flo; _flo.flt = (val); \
  77. \
  78. X##_f[2] = 0; X##_f[3] = 0; \
  79. X##_f[0] = _flo.bits.frac0; \
  80. X##_f[1] = _flo.bits.frac1; \
  81. X##_e = _flo.bits.exp; \
  82. X##_s = _flo.bits.sign; \
  83. } while (0)
  84. #define FP_UNPACK_RAW_EP(X, val) \
  85. do { \
  86. union _FP_UNION_E *_flo = \
  87. (union _FP_UNION_E *)(val); \
  88. \
  89. X##_f[2] = 0; X##_f[3] = 0; \
  90. X##_f[0] = _flo->bits.frac0; \
  91. X##_f[1] = _flo->bits.frac1; \
  92. X##_e = _flo->bits.exp; \
  93. X##_s = _flo->bits.sign; \
  94. } while (0)
  95. #define FP_PACK_RAW_E(val, X) \
  96. do { \
  97. union _FP_UNION_E _flo; \
  98. \
  99. if (X##_e) X##_f[1] |= _FP_IMPLBIT_E; \
  100. else X##_f[1] &= ~(_FP_IMPLBIT_E); \
  101. _flo.bits.frac0 = X##_f[0]; \
  102. _flo.bits.frac1 = X##_f[1]; \
  103. _flo.bits.exp = X##_e; \
  104. _flo.bits.sign = X##_s; \
  105. \
  106. (val) = _flo.flt; \
  107. } while (0)
  108. #define FP_PACK_RAW_EP(val, X) \
  109. do { \
  110. if (!FP_INHIBIT_RESULTS) \
  111. { \
  112. union _FP_UNION_E *_flo = \
  113. (union _FP_UNION_E *)(val); \
  114. \
  115. if (X##_e) X##_f[1] |= _FP_IMPLBIT_E; \
  116. else X##_f[1] &= ~(_FP_IMPLBIT_E); \
  117. _flo->bits.frac0 = X##_f[0]; \
  118. _flo->bits.frac1 = X##_f[1]; \
  119. _flo->bits.exp = X##_e; \
  120. _flo->bits.sign = X##_s; \
  121. } \
  122. } while (0)
  123. #define FP_UNPACK_E(X,val) \
  124. do { \
  125. FP_UNPACK_RAW_E(X,val); \
  126. _FP_UNPACK_CANONICAL(E,4,X); \
  127. } while (0)
  128. #define FP_UNPACK_EP(X,val) \
  129. do { \
  130. FP_UNPACK_RAW_EP(X,val); \
  131. _FP_UNPACK_CANONICAL(E,4,X); \
  132. } while (0)
  133. #define FP_UNPACK_SEMIRAW_E(X,val) \
  134. do { \
  135. FP_UNPACK_RAW_E(X,val); \
  136. _FP_UNPACK_SEMIRAW(E,4,X); \
  137. } while (0)
  138. #define FP_UNPACK_SEMIRAW_EP(X,val) \
  139. do { \
  140. FP_UNPACK_RAW_EP(X,val); \
  141. _FP_UNPACK_SEMIRAW(E,4,X); \
  142. } while (0)
  143. #define FP_PACK_E(val,X) \
  144. do { \
  145. _FP_PACK_CANONICAL(E,4,X); \
  146. FP_PACK_RAW_E(val,X); \
  147. } while (0)
  148. #define FP_PACK_EP(val,X) \
  149. do { \
  150. _FP_PACK_CANONICAL(E,4,X); \
  151. FP_PACK_RAW_EP(val,X); \
  152. } while (0)
  153. #define FP_PACK_SEMIRAW_E(val,X) \
  154. do { \
  155. _FP_PACK_SEMIRAW(E,4,X); \
  156. FP_PACK_RAW_E(val,X); \
  157. } while (0)
  158. #define FP_PACK_SEMIRAW_EP(val,X) \
  159. do { \
  160. _FP_PACK_SEMIRAW(E,4,X); \
  161. FP_PACK_RAW_EP(val,X); \
  162. } while (0)
  163. #define FP_ISSIGNAN_E(X) _FP_ISSIGNAN(E,4,X)
  164. #define FP_NEG_E(R,X) _FP_NEG(E,4,R,X)
  165. #define FP_ADD_E(R,X,Y) _FP_ADD(E,4,R,X,Y)
  166. #define FP_SUB_E(R,X,Y) _FP_SUB(E,4,R,X,Y)
  167. #define FP_MUL_E(R,X,Y) _FP_MUL(E,4,R,X,Y)
  168. #define FP_DIV_E(R,X,Y) _FP_DIV(E,4,R,X,Y)
  169. #define FP_SQRT_E(R,X) _FP_SQRT(E,4,R,X)
  170. /*
  171. * Square root algorithms:
  172. * We have just one right now, maybe Newton approximation
  173. * should be added for those machines where division is fast.
  174. * This has special _E version because standard _4 square
  175. * root would not work (it has to start normally with the
  176. * second word and not the first), but as we have to do it
  177. * anyway, we optimize it by doing most of the calculations
  178. * in two UWtype registers instead of four.
  179. */
  180. #define _FP_SQRT_MEAT_E(R, S, T, X, q) \
  181. do { \
  182. q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
  183. _FP_FRAC_SRL_4(X, (_FP_WORKBITS)); \
  184. while (q) \
  185. { \
  186. T##_f[1] = S##_f[1] + q; \
  187. if (T##_f[1] <= X##_f[1]) \
  188. { \
  189. S##_f[1] = T##_f[1] + q; \
  190. X##_f[1] -= T##_f[1]; \
  191. R##_f[1] += q; \
  192. } \
  193. _FP_FRAC_SLL_2(X, 1); \
  194. q >>= 1; \
  195. } \
  196. q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
  197. while (q) \
  198. { \
  199. T##_f[0] = S##_f[0] + q; \
  200. T##_f[1] = S##_f[1]; \
  201. if (T##_f[1] < X##_f[1] || \
  202. (T##_f[1] == X##_f[1] && \
  203. T##_f[0] <= X##_f[0])) \
  204. { \
  205. S##_f[0] = T##_f[0] + q; \
  206. S##_f[1] += (T##_f[0] > S##_f[0]); \
  207. _FP_FRAC_DEC_2(X, T); \
  208. R##_f[0] += q; \
  209. } \
  210. _FP_FRAC_SLL_2(X, 1); \
  211. q >>= 1; \
  212. } \
  213. _FP_FRAC_SLL_4(R, (_FP_WORKBITS)); \
  214. if (X##_f[0] | X##_f[1]) \
  215. { \
  216. if (S##_f[1] < X##_f[1] || \
  217. (S##_f[1] == X##_f[1] && \
  218. S##_f[0] < X##_f[0])) \
  219. R##_f[0] |= _FP_WORK_ROUND; \
  220. R##_f[0] |= _FP_WORK_STICKY; \
  221. } \
  222. } while (0)
  223. #define FP_CMP_E(r,X,Y,un) _FP_CMP(E,4,r,X,Y,un)
  224. #define FP_CMP_EQ_E(r,X,Y) _FP_CMP_EQ(E,4,r,X,Y)
  225. #define FP_CMP_UNORD_E(r,X,Y) _FP_CMP_UNORD(E,4,r,X,Y)
  226. #define FP_TO_INT_E(r,X,rsz,rsg) _FP_TO_INT(E,4,r,X,rsz,rsg)
  227. #define FP_FROM_INT_E(X,r,rs,rt) _FP_FROM_INT(E,4,X,r,rs,rt)
  228. #define _FP_FRAC_HIGH_E(X) (X##_f[2])
  229. #define _FP_FRAC_HIGH_RAW_E(X) (X##_f[1])
  230. #else /* not _FP_W_TYPE_SIZE < 64 */
  231. union _FP_UNION_E
  232. {
  233. XFtype flt;
  234. struct {
  235. #if __BYTE_ORDER == __BIG_ENDIAN
  236. _FP_W_TYPE pad : (_FP_W_TYPE_SIZE - 1 - _FP_EXPBITS_E);
  237. unsigned sign : 1;
  238. unsigned exp : _FP_EXPBITS_E;
  239. _FP_W_TYPE frac : _FP_W_TYPE_SIZE;
  240. #else
  241. _FP_W_TYPE frac : _FP_W_TYPE_SIZE;
  242. unsigned exp : _FP_EXPBITS_E;
  243. unsigned sign : 1;
  244. #endif
  245. } bits;
  246. };
  247. #define FP_DECL_E(X) _FP_DECL(2,X)
  248. #define FP_UNPACK_RAW_E(X, val) \
  249. do { \
  250. union _FP_UNION_E _flo; _flo.flt = (val); \
  251. \
  252. X##_f0 = _flo.bits.frac; \
  253. X##_f1 = 0; \
  254. X##_e = _flo.bits.exp; \
  255. X##_s = _flo.bits.sign; \
  256. } while (0)
  257. #define FP_UNPACK_RAW_EP(X, val) \
  258. do { \
  259. union _FP_UNION_E *_flo = \
  260. (union _FP_UNION_E *)(val); \
  261. \
  262. X##_f0 = _flo->bits.frac; \
  263. X##_f1 = 0; \
  264. X##_e = _flo->bits.exp; \
  265. X##_s = _flo->bits.sign; \
  266. } while (0)
  267. #define FP_PACK_RAW_E(val, X) \
  268. do { \
  269. union _FP_UNION_E _flo; \
  270. \
  271. if (X##_e) X##_f0 |= _FP_IMPLBIT_E; \
  272. else X##_f0 &= ~(_FP_IMPLBIT_E); \
  273. _flo.bits.frac = X##_f0; \
  274. _flo.bits.exp = X##_e; \
  275. _flo.bits.sign = X##_s; \
  276. \
  277. (val) = _flo.flt; \
  278. } while (0)
  279. #define FP_PACK_RAW_EP(fs, val, X) \
  280. do { \
  281. if (!FP_INHIBIT_RESULTS) \
  282. { \
  283. union _FP_UNION_E *_flo = \
  284. (union _FP_UNION_E *)(val); \
  285. \
  286. if (X##_e) X##_f0 |= _FP_IMPLBIT_E; \
  287. else X##_f0 &= ~(_FP_IMPLBIT_E); \
  288. _flo->bits.frac = X##_f0; \
  289. _flo->bits.exp = X##_e; \
  290. _flo->bits.sign = X##_s; \
  291. } \
  292. } while (0)
  293. #define FP_UNPACK_E(X,val) \
  294. do { \
  295. FP_UNPACK_RAW_E(X,val); \
  296. _FP_UNPACK_CANONICAL(E,2,X); \
  297. } while (0)
  298. #define FP_UNPACK_EP(X,val) \
  299. do { \
  300. FP_UNPACK_RAW_EP(X,val); \
  301. _FP_UNPACK_CANONICAL(E,2,X); \
  302. } while (0)
  303. #define FP_UNPACK_SEMIRAW_E(X,val) \
  304. do { \
  305. FP_UNPACK_RAW_E(X,val); \
  306. _FP_UNPACK_SEMIRAW(E,2,X); \
  307. } while (0)
  308. #define FP_UNPACK_SEMIRAW_EP(X,val) \
  309. do { \
  310. FP_UNPACK_RAW_EP(X,val); \
  311. _FP_UNPACK_SEMIRAW(E,2,X); \
  312. } while (0)
  313. #define FP_PACK_E(val,X) \
  314. do { \
  315. _FP_PACK_CANONICAL(E,2,X); \
  316. FP_PACK_RAW_E(val,X); \
  317. } while (0)
  318. #define FP_PACK_EP(val,X) \
  319. do { \
  320. _FP_PACK_CANONICAL(E,2,X); \
  321. FP_PACK_RAW_EP(val,X); \
  322. } while (0)
  323. #define FP_PACK_SEMIRAW_E(val,X) \
  324. do { \
  325. _FP_PACK_SEMIRAW(E,2,X); \
  326. FP_PACK_RAW_E(val,X); \
  327. } while (0)
  328. #define FP_PACK_SEMIRAW_EP(val,X) \
  329. do { \
  330. _FP_PACK_SEMIRAW(E,2,X); \
  331. FP_PACK_RAW_EP(val,X); \
  332. } while (0)
  333. #define FP_ISSIGNAN_E(X) _FP_ISSIGNAN(E,2,X)
  334. #define FP_NEG_E(R,X) _FP_NEG(E,2,R,X)
  335. #define FP_ADD_E(R,X,Y) _FP_ADD(E,2,R,X,Y)
  336. #define FP_SUB_E(R,X,Y) _FP_SUB(E,2,R,X,Y)
  337. #define FP_MUL_E(R,X,Y) _FP_MUL(E,2,R,X,Y)
  338. #define FP_DIV_E(R,X,Y) _FP_DIV(E,2,R,X,Y)
  339. #define FP_SQRT_E(R,X) _FP_SQRT(E,2,R,X)
  340. /*
  341. * Square root algorithms:
  342. * We have just one right now, maybe Newton approximation
  343. * should be added for those machines where division is fast.
  344. * We optimize it by doing most of the calculations
  345. * in one UWtype registers instead of two, although we don't
  346. * have to.
  347. */
  348. #define _FP_SQRT_MEAT_E(R, S, T, X, q) \
  349. do { \
  350. q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
  351. _FP_FRAC_SRL_2(X, (_FP_WORKBITS)); \
  352. while (q) \
  353. { \
  354. T##_f0 = S##_f0 + q; \
  355. if (T##_f0 <= X##_f0) \
  356. { \
  357. S##_f0 = T##_f0 + q; \
  358. X##_f0 -= T##_f0; \
  359. R##_f0 += q; \
  360. } \
  361. _FP_FRAC_SLL_1(X, 1); \
  362. q >>= 1; \
  363. } \
  364. _FP_FRAC_SLL_2(R, (_FP_WORKBITS)); \
  365. if (X##_f0) \
  366. { \
  367. if (S##_f0 < X##_f0) \
  368. R##_f0 |= _FP_WORK_ROUND; \
  369. R##_f0 |= _FP_WORK_STICKY; \
  370. } \
  371. } while (0)
  372. #define FP_CMP_E(r,X,Y,un) _FP_CMP(E,2,r,X,Y,un)
  373. #define FP_CMP_EQ_E(r,X,Y) _FP_CMP_EQ(E,2,r,X,Y)
  374. #define FP_CMP_UNORD_E(r,X,Y) _FP_CMP_UNORD(E,2,r,X,Y)
  375. #define FP_TO_INT_E(r,X,rsz,rsg) _FP_TO_INT(E,2,r,X,rsz,rsg)
  376. #define FP_FROM_INT_E(X,r,rs,rt) _FP_FROM_INT(E,2,X,r,rs,rt)
  377. #define _FP_FRAC_HIGH_E(X) (X##_f1)
  378. #define _FP_FRAC_HIGH_RAW_E(X) (X##_f0)
  379. #endif /* not _FP_W_TYPE_SIZE < 64 */