ldouble_wrappers.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Wrapper functions implementing all the long double math functions
  4. * defined by SuSv3 by actually calling the double version of
  5. * each function and then casting the result back to a long double
  6. * to return to the user.
  7. *
  8. * Copyright (C) 2005 by Erik Andersen <andersen@uclibc.org>
  9. *
  10. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  11. */
  12. #include <features.h>
  13. /* Prevent math.h from defining colliding inlines */
  14. #undef __USE_EXTERN_INLINES
  15. #include "math.h"
  16. #include <complex.h>
  17. #define WRAPPER1(func) \
  18. long double func##l(long double x) \
  19. { \
  20. return (long double) func((double) x); \
  21. }
  22. #define int_WRAPPER1(func) \
  23. int func##l(long double x) \
  24. { \
  25. return func((double) x); \
  26. }
  27. #define long_WRAPPER1(func) \
  28. long func##l(long double x) \
  29. { \
  30. return func((double) x); \
  31. }
  32. #define long_long_WRAPPER1(func) \
  33. long long func##l(long double x) \
  34. { \
  35. return func((double) x); \
  36. }
  37. #if defined __i386__ && defined __OPTIMIZE__
  38. # undef WRAPPER1
  39. # undef int_WRAPPER1
  40. # undef long_WRAPPER1
  41. # undef long_long_WRAPPER1
  42. /* gcc 4.3.1 generates really ugly code with redundant pair of store/load:
  43. * sub $0x10,%esp
  44. * fldt 0x14(%esp)
  45. * fstpl 0x8(%esp)
  46. * fldl 0x8(%esp) <-- ??
  47. * fstpl (%esp) <-- ??
  48. * call function
  49. * add $0x10,%esp
  50. * ret
  51. * I can hope newer gcc will eliminate that. However, I don't think
  52. * it will be smart enough to reuse argument stack space and use
  53. * jump instead of call. Let's do it by hand.
  54. * The asm below loads long double x into st(0), then stores it back
  55. * to the same location, but as a double. At this point, stack looks
  56. * exactly as "double func(double)" expects it to be.
  57. * The return value is returned in st(0) per ABI in both cases (returning
  58. * a long double or returning a double). So we can simply jump to func.
  59. * Using __GI_func in jump to make optimized intra-library jump.
  60. * gcc will still generate a useless "ret" after asm. Oh well...
  61. */
  62. # define WRAPPER1(func) \
  63. long double func##l(long double x) \
  64. { \
  65. long double st_top; \
  66. __asm ( \
  67. " fldt %1\n" \
  68. " fstpl %1\n" \
  69. " jmp " __stringify(__GI_##func) "\n" \
  70. : "=t" (st_top) \
  71. : "m" (x) \
  72. ); \
  73. return st_top; \
  74. }
  75. # define int_WRAPPER1(func) \
  76. int func##l(long double x) \
  77. { \
  78. int ret; \
  79. __asm ( \
  80. " fldt %1\n" \
  81. " fstpl %1\n" \
  82. " jmp " __stringify(__GI_##func) "\n" \
  83. : "=a" (ret) \
  84. : "m" (x) \
  85. ); \
  86. return ret; \
  87. }
  88. # define long_WRAPPER1(func) \
  89. long func##l(long double x) \
  90. { \
  91. long ret; \
  92. __asm ( \
  93. " fldt %1\n" \
  94. " fstpl %1\n" \
  95. " jmp " __stringify(__GI_##func) "\n" \
  96. : "=a" (ret) \
  97. : "m" (x) \
  98. ); \
  99. return ret; \
  100. }
  101. # define long_long_WRAPPER1(func) \
  102. long long func##l(long double x) \
  103. { \
  104. long long ret; \
  105. __asm ( \
  106. " fldt %1\n" \
  107. " fstpl %1\n" \
  108. " jmp " __stringify(__GI_##func) "\n" \
  109. : "=A" (ret) \
  110. : "m" (x) \
  111. ); \
  112. return ret; \
  113. }
  114. #endif /* __i386__ && __OPTIMIZE__ */
  115. /* Implement the following, as defined by SuSv3 */
  116. #if 0
  117. long double acoshl(long double);
  118. long double acosl(long double);
  119. long double asinhl(long double);
  120. long double asinl(long double);
  121. long double atan2l(long double, long double);
  122. long double atanhl(long double);
  123. long double atanl(long double);
  124. long double cargl(long double complex);
  125. long double cbrtl(long double);
  126. long double ceill(long double);
  127. long double copysignl(long double, long double);
  128. long double coshl(long double);
  129. long double cosl(long double);
  130. long double erfcl(long double);
  131. long double erfl(long double);
  132. long double exp2l(long double);
  133. long double expl(long double);
  134. long double expm1l(long double);
  135. long double fabsl(long double);
  136. long double fdiml(long double, long double);
  137. long double floorl(long double);
  138. long double fmal(long double, long double, long double);
  139. long double fmaxl(long double, long double);
  140. long double fminl(long double, long double);
  141. long double fmodl(long double, long double);
  142. long double frexpl(long double value, int *);
  143. long double hypotl(long double, long double);
  144. int ilogbl(long double);
  145. long double ldexpl(long double, int);
  146. long double lgammal(long double);
  147. long long llrintl(long double);
  148. long long llroundl(long double);
  149. long double log10l(long double);
  150. long double log1pl(long double);
  151. long double log2l(long double);
  152. long double logbl(long double);
  153. long double logl(long double);
  154. long lrintl(long double);
  155. long lroundl(long double);
  156. long double modfl(long double, long double *);
  157. long double nearbyintl(long double);
  158. long double nextafterl(long double, long double);
  159. long double nexttowardl(long double, long double);
  160. long double powl(long double, long double);
  161. long double remainderl(long double, long double);
  162. long double remquol(long double, long double, int *);
  163. long double rintl(long double);
  164. long double roundl(long double);
  165. long double scalblnl(long double, long);
  166. long double scalbnl(long double, int);
  167. long double sinhl(long double);
  168. long double sinl(long double);
  169. long double sqrtl(long double);
  170. long double tanhl(long double);
  171. long double tanl(long double);
  172. long double tgammal(long double);
  173. long double truncl(long double);
  174. #endif
  175. #ifdef L_acoshl
  176. WRAPPER1(acosh)
  177. #endif
  178. #ifdef L_acosl
  179. WRAPPER1(acos)
  180. #endif
  181. #ifdef L_asinhl
  182. WRAPPER1(asinh)
  183. #endif
  184. #ifdef L_asinl
  185. WRAPPER1(asin)
  186. #endif
  187. #ifdef L_atan2l
  188. long double atan2l (long double x, long double y)
  189. {
  190. return (long double) atan2( (double)x, (double)y );
  191. }
  192. #endif
  193. #ifdef L_atanhl
  194. WRAPPER1(atanh)
  195. #endif
  196. #ifdef L_atanl
  197. WRAPPER1(atan)
  198. #endif
  199. #ifdef L_cargl
  200. long double cargl (long double complex x)
  201. {
  202. return (long double) carg( (double complex)x );
  203. }
  204. #endif
  205. #ifdef L_cbrtl
  206. WRAPPER1(cbrt)
  207. #endif
  208. #ifdef L_ceill
  209. WRAPPER1(ceil)
  210. #endif
  211. #ifdef L_copysignl
  212. long double copysignl (long double x, long double y)
  213. {
  214. return (long double) copysign( (double)x, (double)y );
  215. }
  216. #endif
  217. #ifdef L_coshl
  218. WRAPPER1(cosh)
  219. #endif
  220. #ifdef L_cosl
  221. WRAPPER1(cos)
  222. #endif
  223. #ifdef L_erfcl
  224. WRAPPER1(erfc)
  225. #endif
  226. #ifdef L_erfl
  227. WRAPPER1(erf)
  228. #endif
  229. #ifdef L_exp2l
  230. WRAPPER1(exp2)
  231. #endif
  232. #ifdef L_expl
  233. WRAPPER1(exp)
  234. #endif
  235. #ifdef L_expm1l
  236. WRAPPER1(expm1)
  237. #endif
  238. #ifdef L_fabsl
  239. WRAPPER1(fabs)
  240. #endif
  241. #ifdef L_fdiml
  242. long double fdiml (long double x, long double y)
  243. {
  244. return (long double) fdim( (double)x, (double)y );
  245. }
  246. #endif
  247. #ifdef L_floorl
  248. WRAPPER1(floor)
  249. #endif
  250. #ifdef L_fmal
  251. long double fmal (long double x, long double y, long double z)
  252. {
  253. return (long double) fma( (double)x, (double)y, (double)z );
  254. }
  255. #endif
  256. #ifdef L_fmaxl
  257. long double fmaxl (long double x, long double y)
  258. {
  259. return (long double) fmax( (double)x, (double)y );
  260. }
  261. #endif
  262. #ifdef L_fminl
  263. long double fminl (long double x, long double y)
  264. {
  265. return (long double) fmin( (double)x, (double)y );
  266. }
  267. #endif
  268. #ifdef L_fmodl
  269. long double fmodl (long double x, long double y)
  270. {
  271. return (long double) fmod( (double)x, (double)y );
  272. }
  273. #endif
  274. #ifdef L_frexpl
  275. long double frexpl (long double x, int *exp)
  276. {
  277. return (long double) frexp( (double)x, exp );
  278. }
  279. #endif
  280. #ifdef L_gammal
  281. /* WRAPPER1(gamma) won't work, tries to call __GI_xxx,
  282. * and gamma() hasn't got one. */
  283. long double gammal(long double x)
  284. {
  285. return (long double) gamma((double) x);
  286. }
  287. #endif
  288. #ifdef L_hypotl
  289. long double hypotl (long double x, long double y)
  290. {
  291. return (long double) hypot( (double)x, (double)y );
  292. }
  293. #endif
  294. #ifdef L_ilogbl
  295. int_WRAPPER1(ilogb)
  296. #endif
  297. #ifdef L_ldexpl
  298. long double ldexpl (long double x, int exp)
  299. {
  300. return (long double) ldexp( (double)x, exp );
  301. }
  302. #endif
  303. #ifdef L_lgammal
  304. WRAPPER1(lgamma)
  305. #endif
  306. #ifdef L_llrintl
  307. long_long_WRAPPER1(llrint)
  308. #endif
  309. #ifdef L_llroundl
  310. long_long_WRAPPER1(llround)
  311. #endif
  312. #ifdef L_log10l
  313. WRAPPER1(log10)
  314. #endif
  315. #ifdef L_log1pl
  316. WRAPPER1(log1p)
  317. #endif
  318. #ifdef L_log2l
  319. /* WRAPPER1(log2) won't work */
  320. long double log2l(long double x)
  321. {
  322. return (long double) log2((double)x);
  323. }
  324. #endif
  325. #ifdef L_logbl
  326. WRAPPER1(logb)
  327. #endif
  328. #ifdef L_logl
  329. WRAPPER1(log)
  330. #endif
  331. #ifdef L_lrintl
  332. long_WRAPPER1(lrint)
  333. #endif
  334. #ifdef L_lroundl
  335. long_WRAPPER1(lround)
  336. #endif
  337. #ifdef L_modfl
  338. long double modfl (long double x, long double *iptr)
  339. {
  340. double y, result;
  341. result = modf ( x, &y );
  342. *iptr = (long double)y;
  343. return (long double) result;
  344. }
  345. #endif
  346. #ifdef L_nearbyintl
  347. WRAPPER1(nearbyint)
  348. #endif
  349. #ifdef L_nextafterl
  350. long double nextafterl (long double x, long double y)
  351. {
  352. return (long double) nextafter( (double)x, (double)y );
  353. }
  354. #endif
  355. /* Disabled in Makefile.in */
  356. #if 0 /* def L_nexttowardl */
  357. long double nexttowardl (long double x, long double y)
  358. {
  359. return (long double) nexttoward( (double)x, (double)y );
  360. }
  361. libm_hidden_def(nexttowardl)
  362. #endif
  363. #ifdef L_powl
  364. long double powl (long double x, long double y)
  365. {
  366. return (long double) pow( (double)x, (double)y );
  367. }
  368. #endif
  369. #ifdef L_remainderl
  370. long double remainderl (long double x, long double y)
  371. {
  372. return (long double) remainder( (double)x, (double)y );
  373. }
  374. #endif
  375. #ifdef L_remquol
  376. long double remquol (long double x, long double y, int *quo)
  377. {
  378. return (long double) remquo( (double)x, (double)y, quo );
  379. }
  380. #endif
  381. #ifdef L_rintl
  382. WRAPPER1(rint)
  383. #endif
  384. #ifdef L_roundl
  385. WRAPPER1(round)
  386. #endif
  387. #ifdef L_scalblnl
  388. long double scalblnl (long double x, long exp)
  389. {
  390. return (long double) scalbln( (double)x, exp );
  391. }
  392. #endif
  393. #ifdef L_scalbnl
  394. long double scalbnl (long double x, int exp)
  395. {
  396. return (long double) scalbn( (double)x, exp );
  397. }
  398. #endif
  399. /* scalb is an obsolete function */
  400. #ifdef L_sinhl
  401. WRAPPER1(sinh)
  402. #endif
  403. #ifdef L_sinl
  404. WRAPPER1(sin)
  405. #endif
  406. #ifdef L_sqrtl
  407. WRAPPER1(sqrt)
  408. #endif
  409. #ifdef L_tanhl
  410. WRAPPER1(tanh)
  411. #endif
  412. #ifdef L_tanl
  413. WRAPPER1(tan)
  414. #endif
  415. #ifdef L_tgammal
  416. WRAPPER1(tgamma)
  417. #endif
  418. #ifdef L_truncl
  419. WRAPPER1(trunc)
  420. #endif
  421. #ifdef L_significandl
  422. /* WRAPPER1(significand) won't work, tries to call __GI_xxx,
  423. * and significand() hasn't got one. */
  424. long double significandl(long double x)
  425. {
  426. return (long double) significand((double) x);
  427. }
  428. #endif
  429. #ifdef __DO_C99_MATH__
  430. #ifdef L___fpclassifyl
  431. int_WRAPPER1(__fpclassify)
  432. libm_hidden_def(__fpclassifyl)
  433. #endif
  434. #ifdef L___finitel
  435. int_WRAPPER1(__finite)
  436. libm_hidden_def(__finitel)
  437. #endif
  438. #ifdef L___signbitl
  439. int_WRAPPER1(__signbit)
  440. libm_hidden_def(__signbitl)
  441. #endif
  442. #ifdef L___isnanl
  443. int_WRAPPER1(__isnan)
  444. libm_hidden_def(__isnanl)
  445. #endif
  446. #ifdef L___isinfl
  447. int_WRAPPER1(__isinf)
  448. libm_hidden_def(__isinfl)
  449. #endif
  450. #endif