ibm-ldouble.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* 128-bit long double support routines for Darwin.
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* Implementations of floating-point long double basic arithmetic
  20. functions called by the IBM C compiler when generating code for
  21. PowerPC platforms. In particular, the following functions are
  22. implemented: __gcc_qadd, __gcc_qsub, __gcc_qmul, and __gcc_qdiv.
  23. Double-double algorithms are based on the paper "Doubled-Precision
  24. IEEE Standard 754 Floating-Point Arithmetic" by W. Kahan, February 26,
  25. 1987. An alternative published reference is "Software for
  26. Doubled-Precision Floating-Point Computations", by Seppo Linnainmaa,
  27. ACM TOMS vol 7 no 3, September 1981, pages 272-283. */
  28. /* Each long double is made up of two IEEE doubles. The value of the
  29. long double is the sum of the values of the two parts. The most
  30. significant part is required to be the value of the long double
  31. rounded to the nearest double, as specified by IEEE. For Inf
  32. values, the least significant part is required to be one of +0.0 or
  33. -0.0. No other requirements are made; so, for example, 1.0 may be
  34. represented as (1.0, +0.0) or (1.0, -0.0), and the low part of a
  35. NaN is don't-care.
  36. This code currently assumes the most significant double is in
  37. the lower numbered register or lower addressed memory. */
  38. #if (defined (__MACH__) || defined (__powerpc__) || defined (_AIX)) \
  39. && !defined (__rtems__) \
  40. && (defined (__LONG_DOUBLE_128__) || defined (__FLOAT128_TYPE__))
  41. #define fabs(x) __builtin_fabs(x)
  42. #define isless(x, y) __builtin_isless (x, y)
  43. #define inf() __builtin_inf()
  44. #define unlikely(x) __builtin_expect ((x), 0)
  45. #define nonfinite(a) unlikely (! isless (fabs (a), inf ()))
  46. /* If we have __float128/_Float128, use __ibm128 instead of long double. On
  47. other systems, use long double, because __ibm128 might not have been
  48. created. */
  49. #ifdef __FLOAT128__
  50. #define IBM128_TYPE __ibm128
  51. #else
  52. #define IBM128_TYPE long double
  53. #endif
  54. /* Define ALIASNAME as a strong alias for NAME. */
  55. # define strong_alias(name, aliasname) _strong_alias(name, aliasname)
  56. # define _strong_alias(name, aliasname) \
  57. extern __typeof (name) aliasname __attribute__ ((alias (#name)));
  58. /* All these routines actually take two long doubles as parameters,
  59. but GCC currently generates poor code when a union is used to turn
  60. a long double into a pair of doubles. */
  61. IBM128_TYPE __gcc_qadd (double, double, double, double);
  62. IBM128_TYPE __gcc_qsub (double, double, double, double);
  63. IBM128_TYPE __gcc_qmul (double, double, double, double);
  64. IBM128_TYPE __gcc_qdiv (double, double, double, double);
  65. #if defined __ELF__ && defined SHARED \
  66. && (defined __powerpc64__ || !(defined __linux__ || defined __gnu_hurd__))
  67. /* Provide definitions of the old symbol names to satisfy apps and
  68. shared libs built against an older libgcc. To access the _xlq
  69. symbols an explicit version reference is needed, so these won't
  70. satisfy an unadorned reference like _xlqadd. If dot symbols are
  71. not needed, the assembler will remove the aliases from the symbol
  72. table. */
  73. __asm__ (".symver __gcc_qadd,_xlqadd@GCC_3.4\n\t"
  74. ".symver __gcc_qsub,_xlqsub@GCC_3.4\n\t"
  75. ".symver __gcc_qmul,_xlqmul@GCC_3.4\n\t"
  76. ".symver __gcc_qdiv,_xlqdiv@GCC_3.4\n\t"
  77. ".symver .__gcc_qadd,._xlqadd@GCC_3.4\n\t"
  78. ".symver .__gcc_qsub,._xlqsub@GCC_3.4\n\t"
  79. ".symver .__gcc_qmul,._xlqmul@GCC_3.4\n\t"
  80. ".symver .__gcc_qdiv,._xlqdiv@GCC_3.4");
  81. #endif
  82. /* Combine two 'double' values into one 'IBM128_TYPE' and return the result. */
  83. static inline IBM128_TYPE
  84. pack_ldouble (double dh, double dl)
  85. {
  86. #if defined (__LONG_DOUBLE_128__) && defined (__LONG_DOUBLE_IBM128__) \
  87. && !(defined (_SOFT_FLOAT) || defined (__NO_FPRS__))
  88. return __builtin_pack_longdouble (dh, dl);
  89. #else
  90. union
  91. {
  92. IBM128_TYPE ldval;
  93. double dval[2];
  94. } x;
  95. x.dval[0] = dh;
  96. x.dval[1] = dl;
  97. return x.ldval;
  98. #endif
  99. }
  100. /* Add two 'IBM128_TYPE' values and return the result. */
  101. static inline IBM128_TYPE
  102. ldouble_qadd_internal (double a, double aa, double c, double cc)
  103. {
  104. double xh, xl, z, q, zz;
  105. z = a + c;
  106. if (nonfinite (z))
  107. {
  108. if (fabs (z) != inf())
  109. return z;
  110. z = cc + aa + c + a;
  111. if (nonfinite (z))
  112. return z;
  113. xh = z; /* Will always be DBL_MAX. */
  114. zz = aa + cc;
  115. if (fabs(a) > fabs(c))
  116. xl = a - z + c + zz;
  117. else
  118. xl = c - z + a + zz;
  119. }
  120. else
  121. {
  122. q = a - z;
  123. zz = q + c + (a - (q + z)) + aa + cc;
  124. /* Keep -0 result. */
  125. if (zz == 0.0)
  126. return z;
  127. xh = z + zz;
  128. if (nonfinite (xh))
  129. return xh;
  130. xl = z - xh + zz;
  131. }
  132. return pack_ldouble (xh, xl);
  133. }
  134. IBM128_TYPE
  135. __gcc_qadd (double a, double aa, double c, double cc)
  136. {
  137. return ldouble_qadd_internal (a, aa, c, cc);
  138. }
  139. IBM128_TYPE
  140. __gcc_qsub (double a, double aa, double c, double cc)
  141. {
  142. return ldouble_qadd_internal (a, aa, -c, -cc);
  143. }
  144. #ifdef __NO_FPRS__
  145. static double fmsub (double, double, double);
  146. #endif
  147. IBM128_TYPE
  148. __gcc_qmul (double a, double b, double c, double d)
  149. {
  150. double xh, xl, t, tau, u, v, w;
  151. t = a * c; /* Highest order double term. */
  152. if (unlikely (t == 0) /* Preserve -0. */
  153. || nonfinite (t))
  154. return t;
  155. /* Sum terms of two highest orders. */
  156. /* Use fused multiply-add to get low part of a * c. */
  157. #ifndef __NO_FPRS__
  158. asm ("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t));
  159. #else
  160. tau = fmsub (a, c, t);
  161. #endif
  162. v = a*d;
  163. w = b*c;
  164. tau += v + w; /* Add in other second-order terms. */
  165. u = t + tau;
  166. /* Construct IBM128_TYPE result. */
  167. if (nonfinite (u))
  168. return u;
  169. xh = u;
  170. xl = (t - u) + tau;
  171. return pack_ldouble (xh, xl);
  172. }
  173. IBM128_TYPE
  174. __gcc_qdiv (double a, double b, double c, double d)
  175. {
  176. double xh, xl, s, sigma, t, tau, u, v, w;
  177. t = a / c; /* highest order double term */
  178. if (unlikely (t == 0) /* Preserve -0. */
  179. || nonfinite (t))
  180. return t;
  181. /* Finite nonzero result requires corrections to the highest order
  182. term. These corrections require the low part of c * t to be
  183. exactly represented in double. */
  184. if (fabs (a) <= 0x1p-969)
  185. {
  186. a *= 0x1p106;
  187. b *= 0x1p106;
  188. c *= 0x1p106;
  189. d *= 0x1p106;
  190. }
  191. s = c * t; /* (s,sigma) = c*t exactly. */
  192. w = -(-b + d * t); /* Written to get fnmsub for speed, but not
  193. numerically necessary. */
  194. /* Use fused multiply-add to get low part of c * t. */
  195. #ifndef __NO_FPRS__
  196. asm ("fmsub %0,%1,%2,%3" : "=f"(sigma) : "f"(c), "f"(t), "f"(s));
  197. #else
  198. sigma = fmsub (c, t, s);
  199. #endif
  200. v = a - s;
  201. tau = ((v-sigma)+w)/c; /* Correction to t. */
  202. u = t + tau;
  203. /* Construct IBM128_TYPE result. */
  204. if (nonfinite (u))
  205. return u;
  206. xh = u;
  207. xl = (t - u) + tau;
  208. return pack_ldouble (xh, xl);
  209. }
  210. #if defined (_SOFT_DOUBLE) && defined (__LONG_DOUBLE_128__)
  211. IBM128_TYPE __gcc_qneg (double, double);
  212. int __gcc_qeq (double, double, double, double);
  213. int __gcc_qne (double, double, double, double);
  214. int __gcc_qge (double, double, double, double);
  215. int __gcc_qle (double, double, double, double);
  216. IBM128_TYPE __gcc_stoq (float);
  217. IBM128_TYPE __gcc_dtoq (double);
  218. float __gcc_qtos (double, double);
  219. double __gcc_qtod (double, double);
  220. int __gcc_qtoi (double, double);
  221. unsigned int __gcc_qtou (double, double);
  222. IBM128_TYPE __gcc_itoq (int);
  223. IBM128_TYPE __gcc_utoq (unsigned int);
  224. extern int __eqdf2 (double, double);
  225. extern int __ledf2 (double, double);
  226. extern int __gedf2 (double, double);
  227. /* Negate 'IBM128_TYPE' value and return the result. */
  228. IBM128_TYPE
  229. __gcc_qneg (double a, double aa)
  230. {
  231. return pack_ldouble (-a, -aa);
  232. }
  233. /* Compare two 'IBM128_TYPE' values for equality. */
  234. int
  235. __gcc_qeq (double a, double aa, double c, double cc)
  236. {
  237. if (__eqdf2 (a, c) == 0)
  238. return __eqdf2 (aa, cc);
  239. return 1;
  240. }
  241. strong_alias (__gcc_qeq, __gcc_qne);
  242. /* Compare two 'IBM128_TYPE' values for less than or equal. */
  243. int
  244. __gcc_qle (double a, double aa, double c, double cc)
  245. {
  246. if (__eqdf2 (a, c) == 0)
  247. return __ledf2 (aa, cc);
  248. return __ledf2 (a, c);
  249. }
  250. strong_alias (__gcc_qle, __gcc_qlt);
  251. /* Compare two 'IBM128_TYPE' values for greater than or equal. */
  252. int
  253. __gcc_qge (double a, double aa, double c, double cc)
  254. {
  255. if (__eqdf2 (a, c) == 0)
  256. return __gedf2 (aa, cc);
  257. return __gedf2 (a, c);
  258. }
  259. strong_alias (__gcc_qge, __gcc_qgt);
  260. /* Convert single to IBM128_TYPE. */
  261. IBM128_TYPE
  262. __gcc_stoq (float a)
  263. {
  264. return pack_ldouble ((double) a, 0.0);
  265. }
  266. /* Convert double to IBM128_TYPE. */
  267. IBM128_TYPE
  268. __gcc_dtoq (double a)
  269. {
  270. return pack_ldouble (a, 0.0);
  271. }
  272. /* Convert IBM128_TYPE to single. */
  273. float
  274. __gcc_qtos (double a, double aa __attribute__ ((__unused__)))
  275. {
  276. return (float) a;
  277. }
  278. /* Convert IBM128_TYPE to double. */
  279. double
  280. __gcc_qtod (double a, double aa __attribute__ ((__unused__)))
  281. {
  282. return a;
  283. }
  284. /* Convert IBM128_TYPE to int. */
  285. int
  286. __gcc_qtoi (double a, double aa)
  287. {
  288. double z = a + aa;
  289. return (int) z;
  290. }
  291. /* Convert IBM128_TYPE to unsigned int. */
  292. unsigned int
  293. __gcc_qtou (double a, double aa)
  294. {
  295. double z = a + aa;
  296. return (unsigned int) z;
  297. }
  298. /* Convert int to IBM128_TYPE. */
  299. IBM128_TYPE
  300. __gcc_itoq (int a)
  301. {
  302. return __gcc_dtoq ((double) a);
  303. }
  304. /* Convert unsigned int to IBM128_TYPE. */
  305. IBM128_TYPE
  306. __gcc_utoq (unsigned int a)
  307. {
  308. return __gcc_dtoq ((double) a);
  309. }
  310. #endif
  311. #ifdef __NO_FPRS__
  312. int __gcc_qunord (double, double, double, double);
  313. extern int __eqdf2 (double, double);
  314. extern int __unorddf2 (double, double);
  315. /* Compare two 'IBM128_TYPE' values for unordered. */
  316. int
  317. __gcc_qunord (double a, double aa, double c, double cc)
  318. {
  319. if (__eqdf2 (a, c) == 0)
  320. return __unorddf2 (aa, cc);
  321. return __unorddf2 (a, c);
  322. }
  323. #include "soft-fp/soft-fp.h"
  324. #include "soft-fp/double.h"
  325. #include "soft-fp/quad.h"
  326. /* Compute floating point multiply-subtract with higher (quad) precision. */
  327. static double
  328. fmsub (double a, double b, double c)
  329. {
  330. FP_DECL_EX;
  331. FP_DECL_D(A);
  332. FP_DECL_D(B);
  333. FP_DECL_D(C);
  334. FP_DECL_Q(X);
  335. FP_DECL_Q(Y);
  336. FP_DECL_Q(Z);
  337. FP_DECL_Q(U);
  338. FP_DECL_Q(V);
  339. FP_DECL_D(R);
  340. double r;
  341. IBM128_TYPE u, x, y, z;
  342. FP_INIT_ROUNDMODE;
  343. FP_UNPACK_RAW_D (A, a);
  344. FP_UNPACK_RAW_D (B, b);
  345. FP_UNPACK_RAW_D (C, c);
  346. /* Extend double to quad. */
  347. #if _FP_W_TYPE_SIZE < 64
  348. FP_EXTEND(Q,D,4,2,X,A);
  349. FP_EXTEND(Q,D,4,2,Y,B);
  350. FP_EXTEND(Q,D,4,2,Z,C);
  351. #else
  352. FP_EXTEND(Q,D,2,1,X,A);
  353. FP_EXTEND(Q,D,2,1,Y,B);
  354. FP_EXTEND(Q,D,2,1,Z,C);
  355. #endif
  356. FP_PACK_RAW_Q(x,X);
  357. FP_PACK_RAW_Q(y,Y);
  358. FP_PACK_RAW_Q(z,Z);
  359. FP_HANDLE_EXCEPTIONS;
  360. /* Multiply. */
  361. FP_INIT_ROUNDMODE;
  362. FP_UNPACK_Q(X,x);
  363. FP_UNPACK_Q(Y,y);
  364. FP_MUL_Q(U,X,Y);
  365. FP_PACK_Q(u,U);
  366. FP_HANDLE_EXCEPTIONS;
  367. /* Subtract. */
  368. FP_INIT_ROUNDMODE;
  369. FP_UNPACK_SEMIRAW_Q(U,u);
  370. FP_UNPACK_SEMIRAW_Q(Z,z);
  371. FP_SUB_Q(V,U,Z);
  372. /* Truncate quad to double. */
  373. #if _FP_W_TYPE_SIZE < 64
  374. V_f[3] &= 0x0007ffff;
  375. FP_TRUNC(D,Q,2,4,R,V);
  376. #else
  377. V_f1 &= 0x0007ffffffffffffL;
  378. FP_TRUNC(D,Q,1,2,R,V);
  379. #endif
  380. FP_PACK_SEMIRAW_D(r,R);
  381. FP_HANDLE_EXCEPTIONS;
  382. return r;
  383. }
  384. #endif
  385. #endif