ldouble_wrappers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. #if defined __NO_LONG_DOUBLE_MATH
  116. # define int_WRAPPER_C99(func) /* not needed */
  117. # else
  118. # define int_WRAPPER_C99(func) \
  119. int func##l(long double x) \
  120. { \
  121. return func((double) x); \
  122. } \
  123. libm_hidden_def(func##l)
  124. #endif
  125. /* Implement the following, as defined by SuSv3 */
  126. #if 0
  127. long double acoshl(long double);
  128. long double acosl(long double);
  129. long double asinhl(long double);
  130. long double asinl(long double);
  131. long double atan2l(long double, long double);
  132. long double atanhl(long double);
  133. long double atanl(long double);
  134. long double cargl(long double complex);
  135. long double cbrtl(long double);
  136. long double ceill(long double);
  137. long double copysignl(long double, long double);
  138. long double coshl(long double);
  139. long double cosl(long double);
  140. long double erfcl(long double);
  141. long double erfl(long double);
  142. long double exp2l(long double);
  143. long double expl(long double);
  144. long double expm1l(long double);
  145. long double fabsl(long double);
  146. long double fdiml(long double, long double);
  147. long double floorl(long double);
  148. long double fmal(long double, long double, long double);
  149. long double fmaxl(long double, long double);
  150. long double fminl(long double, long double);
  151. long double fmodl(long double, long double);
  152. long double frexpl(long double value, int *);
  153. long double hypotl(long double, long double);
  154. int ilogbl(long double);
  155. long double ldexpl(long double, int);
  156. long double lgammal(long double);
  157. long long llrintl(long double);
  158. long long llroundl(long double);
  159. long double log10l(long double);
  160. long double log1pl(long double);
  161. long double log2l(long double);
  162. long double logbl(long double);
  163. long double logl(long double);
  164. long lrintl(long double);
  165. long lroundl(long double);
  166. long double modfl(long double, long double *);
  167. long double nearbyintl(long double);
  168. long double nextafterl(long double, long double);
  169. long double nexttowardl(long double, long double);
  170. long double powl(long double, long double);
  171. long double remainderl(long double, long double);
  172. long double remquol(long double, long double, int *);
  173. long double rintl(long double);
  174. long double roundl(long double);
  175. long double scalblnl(long double, long);
  176. long double scalbnl(long double, int);
  177. long double sinhl(long double);
  178. long double sinl(long double);
  179. long double sqrtl(long double);
  180. long double tanhl(long double);
  181. long double tanl(long double);
  182. long double tgammal(long double);
  183. long double truncl(long double);
  184. #endif
  185. #ifdef L_acoshl
  186. WRAPPER1(acosh)
  187. #endif
  188. #ifdef L_acosl
  189. WRAPPER1(acos)
  190. #endif
  191. #ifdef L_asinhl
  192. WRAPPER1(asinh)
  193. #endif
  194. #ifdef L_asinl
  195. WRAPPER1(asin)
  196. #endif
  197. #ifdef L_atan2l
  198. long double atan2l (long double x, long double y)
  199. {
  200. return (long double) atan2( (double)x, (double)y );
  201. }
  202. #endif
  203. #ifdef L_atanhl
  204. WRAPPER1(atanh)
  205. #endif
  206. #ifdef L_atanl
  207. WRAPPER1(atan)
  208. #endif
  209. #ifdef L_cargl
  210. long double cargl (long double complex x)
  211. {
  212. return (long double) carg( (double complex)x );
  213. }
  214. #endif
  215. #ifdef L_cbrtl
  216. WRAPPER1(cbrt)
  217. #endif
  218. #ifdef L_ceill
  219. WRAPPER1(ceil)
  220. #endif
  221. #ifdef L_copysignl
  222. long double copysignl (long double x, long double y)
  223. {
  224. return (long double) copysign( (double)x, (double)y );
  225. }
  226. #endif
  227. #ifdef L_coshl
  228. WRAPPER1(cosh)
  229. #endif
  230. #ifdef L_cosl
  231. WRAPPER1(cos)
  232. #endif
  233. #ifdef L_erfcl
  234. WRAPPER1(erfc)
  235. #endif
  236. #ifdef L_erfl
  237. WRAPPER1(erf)
  238. #endif
  239. #ifdef L_exp2l
  240. WRAPPER1(exp2)
  241. #endif
  242. #ifdef L_expl
  243. WRAPPER1(exp)
  244. #endif
  245. #ifdef L_expm1l
  246. WRAPPER1(expm1)
  247. #endif
  248. #ifdef L_fabsl
  249. WRAPPER1(fabs)
  250. #endif
  251. #ifdef L_fdiml
  252. long double fdiml (long double x, long double y)
  253. {
  254. return (long double) fdim( (double)x, (double)y );
  255. }
  256. #endif
  257. #ifdef L_floorl
  258. WRAPPER1(floor)
  259. #endif
  260. #ifdef L_fmal
  261. long double fmal (long double x, long double y, long double z)
  262. {
  263. return (long double) fma( (double)x, (double)y, (double)z );
  264. }
  265. #endif
  266. #ifdef L_fmaxl
  267. long double fmaxl (long double x, long double y)
  268. {
  269. return (long double) fmax( (double)x, (double)y );
  270. }
  271. #endif
  272. #ifdef L_fminl
  273. long double fminl (long double x, long double y)
  274. {
  275. return (long double) fmin( (double)x, (double)y );
  276. }
  277. #endif
  278. #ifdef L_fmodl
  279. long double fmodl (long double x, long double y)
  280. {
  281. return (long double) fmod( (double)x, (double)y );
  282. }
  283. #endif
  284. #ifdef L_frexpl
  285. long double frexpl (long double x, int *exp)
  286. {
  287. return (long double) frexp( (double)x, exp );
  288. }
  289. #endif
  290. #ifdef L_gammal
  291. /* WRAPPER1(gamma) won't work, tries to call __GI_xxx,
  292. * and gamma() hasn't got one. */
  293. long double gammal(long double x)
  294. {
  295. return (long double) gamma((double) x);
  296. }
  297. #endif
  298. #ifdef L_hypotl
  299. long double hypotl (long double x, long double y)
  300. {
  301. return (long double) hypot( (double)x, (double)y );
  302. }
  303. #endif
  304. #ifdef L_ilogbl
  305. int_WRAPPER1(ilogb)
  306. #endif
  307. #ifdef L_ldexpl
  308. long double ldexpl (long double x, int exp)
  309. {
  310. return (long double) ldexp( (double)x, exp );
  311. }
  312. #endif
  313. #ifdef L_lgammal
  314. WRAPPER1(lgamma)
  315. #endif
  316. #ifdef L_llrintl
  317. long_long_WRAPPER1(llrint)
  318. #endif
  319. #ifdef L_llroundl
  320. long_long_WRAPPER1(llround)
  321. #endif
  322. #ifdef L_log10l
  323. WRAPPER1(log10)
  324. #endif
  325. #ifdef L_log1pl
  326. WRAPPER1(log1p)
  327. #endif
  328. #ifdef L_log2l
  329. /* WRAPPER1(log2) won't work */
  330. long double log2l(long double x)
  331. {
  332. return (long double) log2((double)x);
  333. }
  334. #endif
  335. #ifdef L_logbl
  336. WRAPPER1(logb)
  337. #endif
  338. #ifdef L_logl
  339. WRAPPER1(log)
  340. #endif
  341. #ifdef L_lrintl
  342. long_WRAPPER1(lrint)
  343. #endif
  344. #ifdef L_lroundl
  345. long_WRAPPER1(lround)
  346. #endif
  347. #ifdef L_modfl
  348. long double modfl (long double x, long double *iptr)
  349. {
  350. double y, result;
  351. result = modf ( x, &y );
  352. *iptr = (long double)y;
  353. return (long double) result;
  354. }
  355. #endif
  356. #ifdef L_nearbyintl
  357. WRAPPER1(nearbyint)
  358. #endif
  359. #ifdef L_nextafterl
  360. long double nextafterl (long double x, long double y)
  361. {
  362. return (long double) nextafter( (double)x, (double)y );
  363. }
  364. #endif
  365. /* Disabled in Makefile.in */
  366. #if 0 /* def L_nexttowardl */
  367. long double nexttowardl (long double x, long double y)
  368. {
  369. return (long double) nexttoward( (double)x, (double)y );
  370. }
  371. libm_hidden_def(nexttowardl)
  372. #endif
  373. #ifdef L_powl
  374. long double powl (long double x, long double y)
  375. {
  376. return (long double) pow( (double)x, (double)y );
  377. }
  378. #endif
  379. #ifdef L_remainderl
  380. long double remainderl (long double x, long double y)
  381. {
  382. return (long double) remainder( (double)x, (double)y );
  383. }
  384. #endif
  385. #ifdef L_remquol
  386. long double remquol (long double x, long double y, int *quo)
  387. {
  388. return (long double) remquo( (double)x, (double)y, quo );
  389. }
  390. #endif
  391. #ifdef L_rintl
  392. WRAPPER1(rint)
  393. #endif
  394. #ifdef L_roundl
  395. WRAPPER1(round)
  396. #endif
  397. #ifdef L_scalblnl
  398. long double scalblnl (long double x, long exp)
  399. {
  400. return (long double) scalbln( (double)x, exp );
  401. }
  402. #endif
  403. #ifdef L_scalbnl
  404. long double scalbnl (long double x, int exp)
  405. {
  406. return (long double) scalbn( (double)x, exp );
  407. }
  408. #endif
  409. /* scalb is an obsolete function */
  410. #ifdef L_sinhl
  411. WRAPPER1(sinh)
  412. #endif
  413. #ifdef L_sinl
  414. WRAPPER1(sin)
  415. #endif
  416. #ifdef L_sqrtl
  417. WRAPPER1(sqrt)
  418. #endif
  419. #ifdef L_tanhl
  420. WRAPPER1(tanh)
  421. #endif
  422. #ifdef L_tanl
  423. WRAPPER1(tan)
  424. #endif
  425. #ifdef L_tgammal
  426. WRAPPER1(tgamma)
  427. #endif
  428. #ifdef L_truncl
  429. WRAPPER1(trunc)
  430. #endif
  431. #ifdef L_significandl
  432. /* WRAPPER1(significand) won't work, tries to call __GI_xxx,
  433. * and significand() hasn't got one. */
  434. long double significandl(long double x)
  435. {
  436. return (long double) significand((double) x);
  437. }
  438. #endif
  439. #ifdef __DO_C99_MATH__
  440. #ifdef L___fpclassifyl
  441. int_WRAPPER1(__fpclassify)
  442. libm_hidden_def(__fpclassifyl)
  443. #endif
  444. #ifdef L___finitel
  445. int_WRAPPER1(__finite)
  446. libm_hidden_def(__finitel)
  447. #endif
  448. #ifdef L___signbitl
  449. int_WRAPPER1(__signbit)
  450. libm_hidden_def(__signbitl)
  451. #endif
  452. #ifdef L___isnanl
  453. int_WRAPPER1(__isnan)
  454. libm_hidden_def(__isnanl)
  455. #endif
  456. #ifdef L___isinfl
  457. int_WRAPPER1(__isinf)
  458. libm_hidden_def(__isinfl)
  459. #endif
  460. #endif