extended.h 12 KB

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