c99_functions.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  1. /* Implementation of various C99 functions
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU Fortran 95 runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public
  6. License as published by the Free Software Foundation; either
  7. version 3 of the License, or (at your option) any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License 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. #include "config.h"
  20. #define C99_PROTOS_H WE_DONT_WANT_PROTOS_NOW
  21. #include "libgfortran.h"
  22. /* On a C99 system "I" (with I*I = -1) should be defined in complex.h;
  23. if not, we define a fallback version here. */
  24. #ifndef I
  25. # if defined(_Imaginary_I)
  26. # define I _Imaginary_I
  27. # elif defined(_Complex_I)
  28. # define I _Complex_I
  29. # else
  30. # define I (1.0fi)
  31. # endif
  32. #endif
  33. /* Macros to get real and imaginary parts of a complex, and set
  34. a complex value. */
  35. #define REALPART(z) (__real__(z))
  36. #define IMAGPART(z) (__imag__(z))
  37. #define COMPLEX_ASSIGN(z_, r_, i_) {__real__(z_) = (r_); __imag__(z_) = (i_);}
  38. /* Prototypes are included to silence -Wstrict-prototypes
  39. -Wmissing-prototypes. */
  40. /* Wrappers for systems without the various C99 single precision Bessel
  41. functions. */
  42. #if defined(HAVE_J0) && ! defined(HAVE_J0F)
  43. #define HAVE_J0F 1
  44. float j0f (float);
  45. float
  46. j0f (float x)
  47. {
  48. return (float) j0 ((double) x);
  49. }
  50. #endif
  51. #if defined(HAVE_J1) && !defined(HAVE_J1F)
  52. #define HAVE_J1F 1
  53. float j1f (float);
  54. float j1f (float x)
  55. {
  56. return (float) j1 ((double) x);
  57. }
  58. #endif
  59. #if defined(HAVE_JN) && !defined(HAVE_JNF)
  60. #define HAVE_JNF 1
  61. float jnf (int, float);
  62. float
  63. jnf (int n, float x)
  64. {
  65. return (float) jn (n, (double) x);
  66. }
  67. #endif
  68. #if defined(HAVE_Y0) && !defined(HAVE_Y0F)
  69. #define HAVE_Y0F 1
  70. float y0f (float);
  71. float
  72. y0f (float x)
  73. {
  74. return (float) y0 ((double) x);
  75. }
  76. #endif
  77. #if defined(HAVE_Y1) && !defined(HAVE_Y1F)
  78. #define HAVE_Y1F 1
  79. float y1f (float);
  80. float
  81. y1f (float x)
  82. {
  83. return (float) y1 ((double) x);
  84. }
  85. #endif
  86. #if defined(HAVE_YN) && !defined(HAVE_YNF)
  87. #define HAVE_YNF 1
  88. float ynf (int, float);
  89. float
  90. ynf (int n, float x)
  91. {
  92. return (float) yn (n, (double) x);
  93. }
  94. #endif
  95. /* Wrappers for systems without the C99 erff() and erfcf() functions. */
  96. #if defined(HAVE_ERF) && !defined(HAVE_ERFF)
  97. #define HAVE_ERFF 1
  98. float erff (float);
  99. float
  100. erff (float x)
  101. {
  102. return (float) erf ((double) x);
  103. }
  104. #endif
  105. #if defined(HAVE_ERFC) && !defined(HAVE_ERFCF)
  106. #define HAVE_ERFCF 1
  107. float erfcf (float);
  108. float
  109. erfcf (float x)
  110. {
  111. return (float) erfc ((double) x);
  112. }
  113. #endif
  114. #ifndef HAVE_ACOSF
  115. #define HAVE_ACOSF 1
  116. float acosf (float x);
  117. float
  118. acosf (float x)
  119. {
  120. return (float) acos (x);
  121. }
  122. #endif
  123. #if HAVE_ACOSH && !HAVE_ACOSHF
  124. float acoshf (float x);
  125. float
  126. acoshf (float x)
  127. {
  128. return (float) acosh ((double) x);
  129. }
  130. #endif
  131. #ifndef HAVE_ASINF
  132. #define HAVE_ASINF 1
  133. float asinf (float x);
  134. float
  135. asinf (float x)
  136. {
  137. return (float) asin (x);
  138. }
  139. #endif
  140. #if HAVE_ASINH && !HAVE_ASINHF
  141. float asinhf (float x);
  142. float
  143. asinhf (float x)
  144. {
  145. return (float) asinh ((double) x);
  146. }
  147. #endif
  148. #ifndef HAVE_ATAN2F
  149. #define HAVE_ATAN2F 1
  150. float atan2f (float y, float x);
  151. float
  152. atan2f (float y, float x)
  153. {
  154. return (float) atan2 (y, x);
  155. }
  156. #endif
  157. #ifndef HAVE_ATANF
  158. #define HAVE_ATANF 1
  159. float atanf (float x);
  160. float
  161. atanf (float x)
  162. {
  163. return (float) atan (x);
  164. }
  165. #endif
  166. #if HAVE_ATANH && !HAVE_ATANHF
  167. float atanhf (float x);
  168. float
  169. atanhf (float x)
  170. {
  171. return (float) atanh ((double) x);
  172. }
  173. #endif
  174. #ifndef HAVE_CEILF
  175. #define HAVE_CEILF 1
  176. float ceilf (float x);
  177. float
  178. ceilf (float x)
  179. {
  180. return (float) ceil (x);
  181. }
  182. #endif
  183. #ifndef HAVE_COPYSIGNF
  184. #define HAVE_COPYSIGNF 1
  185. float copysignf (float x, float y);
  186. float
  187. copysignf (float x, float y)
  188. {
  189. return (float) copysign (x, y);
  190. }
  191. #endif
  192. #ifndef HAVE_COSF
  193. #define HAVE_COSF 1
  194. float cosf (float x);
  195. float
  196. cosf (float x)
  197. {
  198. return (float) cos (x);
  199. }
  200. #endif
  201. #ifndef HAVE_COSHF
  202. #define HAVE_COSHF 1
  203. float coshf (float x);
  204. float
  205. coshf (float x)
  206. {
  207. return (float) cosh (x);
  208. }
  209. #endif
  210. #ifndef HAVE_EXPF
  211. #define HAVE_EXPF 1
  212. float expf (float x);
  213. float
  214. expf (float x)
  215. {
  216. return (float) exp (x);
  217. }
  218. #endif
  219. #ifndef HAVE_FABSF
  220. #define HAVE_FABSF 1
  221. float fabsf (float x);
  222. float
  223. fabsf (float x)
  224. {
  225. return (float) fabs (x);
  226. }
  227. #endif
  228. #ifndef HAVE_FLOORF
  229. #define HAVE_FLOORF 1
  230. float floorf (float x);
  231. float
  232. floorf (float x)
  233. {
  234. return (float) floor (x);
  235. }
  236. #endif
  237. #ifndef HAVE_FMODF
  238. #define HAVE_FMODF 1
  239. float fmodf (float x, float y);
  240. float
  241. fmodf (float x, float y)
  242. {
  243. return (float) fmod (x, y);
  244. }
  245. #endif
  246. #ifndef HAVE_FREXPF
  247. #define HAVE_FREXPF 1
  248. float frexpf (float x, int *exp);
  249. float
  250. frexpf (float x, int *exp)
  251. {
  252. return (float) frexp (x, exp);
  253. }
  254. #endif
  255. #ifndef HAVE_HYPOTF
  256. #define HAVE_HYPOTF 1
  257. float hypotf (float x, float y);
  258. float
  259. hypotf (float x, float y)
  260. {
  261. return (float) hypot (x, y);
  262. }
  263. #endif
  264. #ifndef HAVE_LOGF
  265. #define HAVE_LOGF 1
  266. float logf (float x);
  267. float
  268. logf (float x)
  269. {
  270. return (float) log (x);
  271. }
  272. #endif
  273. #ifndef HAVE_LOG10F
  274. #define HAVE_LOG10F 1
  275. float log10f (float x);
  276. float
  277. log10f (float x)
  278. {
  279. return (float) log10 (x);
  280. }
  281. #endif
  282. #ifndef HAVE_SCALBN
  283. #define HAVE_SCALBN 1
  284. double scalbn (double x, int y);
  285. double
  286. scalbn (double x, int y)
  287. {
  288. #if (FLT_RADIX == 2) && defined(HAVE_LDEXP)
  289. return ldexp (x, y);
  290. #else
  291. return x * pow (FLT_RADIX, y);
  292. #endif
  293. }
  294. #endif
  295. #ifndef HAVE_SCALBNF
  296. #define HAVE_SCALBNF 1
  297. float scalbnf (float x, int y);
  298. float
  299. scalbnf (float x, int y)
  300. {
  301. return (float) scalbn (x, y);
  302. }
  303. #endif
  304. #ifndef HAVE_SINF
  305. #define HAVE_SINF 1
  306. float sinf (float x);
  307. float
  308. sinf (float x)
  309. {
  310. return (float) sin (x);
  311. }
  312. #endif
  313. #ifndef HAVE_SINHF
  314. #define HAVE_SINHF 1
  315. float sinhf (float x);
  316. float
  317. sinhf (float x)
  318. {
  319. return (float) sinh (x);
  320. }
  321. #endif
  322. #ifndef HAVE_SQRTF
  323. #define HAVE_SQRTF 1
  324. float sqrtf (float x);
  325. float
  326. sqrtf (float x)
  327. {
  328. return (float) sqrt (x);
  329. }
  330. #endif
  331. #ifndef HAVE_TANF
  332. #define HAVE_TANF 1
  333. float tanf (float x);
  334. float
  335. tanf (float x)
  336. {
  337. return (float) tan (x);
  338. }
  339. #endif
  340. #ifndef HAVE_TANHF
  341. #define HAVE_TANHF 1
  342. float tanhf (float x);
  343. float
  344. tanhf (float x)
  345. {
  346. return (float) tanh (x);
  347. }
  348. #endif
  349. #ifndef HAVE_TRUNC
  350. #define HAVE_TRUNC 1
  351. double trunc (double x);
  352. double
  353. trunc (double x)
  354. {
  355. if (!isfinite (x))
  356. return x;
  357. if (x < 0.0)
  358. return - floor (-x);
  359. else
  360. return floor (x);
  361. }
  362. #endif
  363. #ifndef HAVE_TRUNCF
  364. #define HAVE_TRUNCF 1
  365. float truncf (float x);
  366. float
  367. truncf (float x)
  368. {
  369. return (float) trunc (x);
  370. }
  371. #endif
  372. #ifndef HAVE_NEXTAFTERF
  373. #define HAVE_NEXTAFTERF 1
  374. /* This is a portable implementation of nextafterf that is intended to be
  375. independent of the floating point format or its in memory representation.
  376. This implementation works correctly with denormalized values. */
  377. float nextafterf (float x, float y);
  378. float
  379. nextafterf (float x, float y)
  380. {
  381. /* This variable is marked volatile to avoid excess precision problems
  382. on some platforms, including IA-32. */
  383. volatile float delta;
  384. float absx, denorm_min;
  385. if (isnan (x) || isnan (y))
  386. return x + y;
  387. if (x == y)
  388. return x;
  389. if (!isfinite (x))
  390. return x > 0 ? __FLT_MAX__ : - __FLT_MAX__;
  391. /* absx = fabsf (x); */
  392. absx = (x < 0.0) ? -x : x;
  393. /* __FLT_DENORM_MIN__ is non-zero iff the target supports denormals. */
  394. if (__FLT_DENORM_MIN__ == 0.0f)
  395. denorm_min = __FLT_MIN__;
  396. else
  397. denorm_min = __FLT_DENORM_MIN__;
  398. if (absx < __FLT_MIN__)
  399. delta = denorm_min;
  400. else
  401. {
  402. float frac;
  403. int exp;
  404. /* Discard the fraction from x. */
  405. frac = frexpf (absx, &exp);
  406. delta = scalbnf (0.5f, exp);
  407. /* Scale x by the epsilon of the representation. By rights we should
  408. have been able to combine this with scalbnf, but some targets don't
  409. get that correct with denormals. */
  410. delta *= __FLT_EPSILON__;
  411. /* If we're going to be reducing the absolute value of X, and doing so
  412. would reduce the exponent of X, then the delta to be applied is
  413. one exponent smaller. */
  414. if (frac == 0.5f && (y < x) == (x > 0))
  415. delta *= 0.5f;
  416. /* If that underflows to zero, then we're back to the minimum. */
  417. if (delta == 0.0f)
  418. delta = denorm_min;
  419. }
  420. if (y < x)
  421. delta = -delta;
  422. return x + delta;
  423. }
  424. #endif
  425. #ifndef HAVE_POWF
  426. #define HAVE_POWF 1
  427. float powf (float x, float y);
  428. float
  429. powf (float x, float y)
  430. {
  431. return (float) pow (x, y);
  432. }
  433. #endif
  434. #ifndef HAVE_ROUND
  435. #define HAVE_ROUND 1
  436. /* Round to nearest integral value. If the argument is halfway between two
  437. integral values then round away from zero. */
  438. double round (double x);
  439. double
  440. round (double x)
  441. {
  442. double t;
  443. if (!isfinite (x))
  444. return (x);
  445. if (x >= 0.0)
  446. {
  447. t = floor (x);
  448. if (t - x <= -0.5)
  449. t += 1.0;
  450. return (t);
  451. }
  452. else
  453. {
  454. t = floor (-x);
  455. if (t + x <= -0.5)
  456. t += 1.0;
  457. return (-t);
  458. }
  459. }
  460. #endif
  461. /* Algorithm by Steven G. Kargl. */
  462. #if !defined(HAVE_ROUNDL)
  463. #define HAVE_ROUNDL 1
  464. long double roundl (long double x);
  465. #if defined(HAVE_CEILL)
  466. /* Round to nearest integral value. If the argument is halfway between two
  467. integral values then round away from zero. */
  468. long double
  469. roundl (long double x)
  470. {
  471. long double t;
  472. if (!isfinite (x))
  473. return (x);
  474. if (x >= 0.0)
  475. {
  476. t = ceill (x);
  477. if (t - x > 0.5)
  478. t -= 1.0;
  479. return (t);
  480. }
  481. else
  482. {
  483. t = ceill (-x);
  484. if (t + x > 0.5)
  485. t -= 1.0;
  486. return (-t);
  487. }
  488. }
  489. #else
  490. /* Poor version of roundl for system that don't have ceill. */
  491. long double
  492. roundl (long double x)
  493. {
  494. if (x > DBL_MAX || x < -DBL_MAX)
  495. {
  496. #ifdef HAVE_NEXTAFTERL
  497. long double prechalf = nextafterl (0.5L, LDBL_MAX);
  498. #else
  499. static long double prechalf = 0.5L;
  500. #endif
  501. return (GFC_INTEGER_LARGEST) (x + (x > 0 ? prechalf : -prechalf));
  502. }
  503. else
  504. /* Use round(). */
  505. return round ((double) x);
  506. }
  507. #endif
  508. #endif
  509. #ifndef HAVE_ROUNDF
  510. #define HAVE_ROUNDF 1
  511. /* Round to nearest integral value. If the argument is halfway between two
  512. integral values then round away from zero. */
  513. float roundf (float x);
  514. float
  515. roundf (float x)
  516. {
  517. float t;
  518. if (!isfinite (x))
  519. return (x);
  520. if (x >= 0.0)
  521. {
  522. t = floorf (x);
  523. if (t - x <= -0.5)
  524. t += 1.0;
  525. return (t);
  526. }
  527. else
  528. {
  529. t = floorf (-x);
  530. if (t + x <= -0.5)
  531. t += 1.0;
  532. return (-t);
  533. }
  534. }
  535. #endif
  536. /* lround{f,,l} and llround{f,,l} functions. */
  537. #if !defined(HAVE_LROUNDF) && defined(HAVE_ROUNDF)
  538. #define HAVE_LROUNDF 1
  539. long int lroundf (float x);
  540. long int
  541. lroundf (float x)
  542. {
  543. return (long int) roundf (x);
  544. }
  545. #endif
  546. #if !defined(HAVE_LROUND) && defined(HAVE_ROUND)
  547. #define HAVE_LROUND 1
  548. long int lround (double x);
  549. long int
  550. lround (double x)
  551. {
  552. return (long int) round (x);
  553. }
  554. #endif
  555. #if !defined(HAVE_LROUNDL) && defined(HAVE_ROUNDL)
  556. #define HAVE_LROUNDL 1
  557. long int lroundl (long double x);
  558. long int
  559. lroundl (long double x)
  560. {
  561. return (long long int) roundl (x);
  562. }
  563. #endif
  564. #if !defined(HAVE_LLROUNDF) && defined(HAVE_ROUNDF)
  565. #define HAVE_LLROUNDF 1
  566. long long int llroundf (float x);
  567. long long int
  568. llroundf (float x)
  569. {
  570. return (long long int) roundf (x);
  571. }
  572. #endif
  573. #if !defined(HAVE_LLROUND) && defined(HAVE_ROUND)
  574. #define HAVE_LLROUND 1
  575. long long int llround (double x);
  576. long long int
  577. llround (double x)
  578. {
  579. return (long long int) round (x);
  580. }
  581. #endif
  582. #if !defined(HAVE_LLROUNDL) && defined(HAVE_ROUNDL)
  583. #define HAVE_LLROUNDL 1
  584. long long int llroundl (long double x);
  585. long long int
  586. llroundl (long double x)
  587. {
  588. return (long long int) roundl (x);
  589. }
  590. #endif
  591. #ifndef HAVE_LOG10L
  592. #define HAVE_LOG10L 1
  593. /* log10 function for long double variables. The version provided here
  594. reduces the argument until it fits into a double, then use log10. */
  595. long double log10l (long double x);
  596. long double
  597. log10l (long double x)
  598. {
  599. #if LDBL_MAX_EXP > DBL_MAX_EXP
  600. if (x > DBL_MAX)
  601. {
  602. double val;
  603. int p2_result = 0;
  604. if (x > 0x1p16383L) { p2_result += 16383; x /= 0x1p16383L; }
  605. if (x > 0x1p8191L) { p2_result += 8191; x /= 0x1p8191L; }
  606. if (x > 0x1p4095L) { p2_result += 4095; x /= 0x1p4095L; }
  607. if (x > 0x1p2047L) { p2_result += 2047; x /= 0x1p2047L; }
  608. if (x > 0x1p1023L) { p2_result += 1023; x /= 0x1p1023L; }
  609. val = log10 ((double) x);
  610. return (val + p2_result * .30102999566398119521373889472449302L);
  611. }
  612. #endif
  613. #if LDBL_MIN_EXP < DBL_MIN_EXP
  614. if (x < DBL_MIN)
  615. {
  616. double val;
  617. int p2_result = 0;
  618. if (x < 0x1p-16380L) { p2_result += 16380; x /= 0x1p-16380L; }
  619. if (x < 0x1p-8189L) { p2_result += 8189; x /= 0x1p-8189L; }
  620. if (x < 0x1p-4093L) { p2_result += 4093; x /= 0x1p-4093L; }
  621. if (x < 0x1p-2045L) { p2_result += 2045; x /= 0x1p-2045L; }
  622. if (x < 0x1p-1021L) { p2_result += 1021; x /= 0x1p-1021L; }
  623. val = fabs (log10 ((double) x));
  624. return (- val - p2_result * .30102999566398119521373889472449302L);
  625. }
  626. #endif
  627. return log10 (x);
  628. }
  629. #endif
  630. #ifndef HAVE_FLOORL
  631. #define HAVE_FLOORL 1
  632. long double floorl (long double x);
  633. long double
  634. floorl (long double x)
  635. {
  636. /* Zero, possibly signed. */
  637. if (x == 0)
  638. return x;
  639. /* Large magnitude. */
  640. if (x > DBL_MAX || x < (-DBL_MAX))
  641. return x;
  642. /* Small positive values. */
  643. if (x >= 0 && x < DBL_MIN)
  644. return 0;
  645. /* Small negative values. */
  646. if (x < 0 && x > (-DBL_MIN))
  647. return -1;
  648. return floor (x);
  649. }
  650. #endif
  651. #ifndef HAVE_FMODL
  652. #define HAVE_FMODL 1
  653. long double fmodl (long double x, long double y);
  654. long double
  655. fmodl (long double x, long double y)
  656. {
  657. if (y == 0.0L)
  658. return 0.0L;
  659. /* Need to check that the result has the same sign as x and magnitude
  660. less than the magnitude of y. */
  661. return x - floorl (x / y) * y;
  662. }
  663. #endif
  664. #if !defined(HAVE_CABSF)
  665. #define HAVE_CABSF 1
  666. float cabsf (float complex z);
  667. float
  668. cabsf (float complex z)
  669. {
  670. return hypotf (REALPART (z), IMAGPART (z));
  671. }
  672. #endif
  673. #if !defined(HAVE_CABS)
  674. #define HAVE_CABS 1
  675. double cabs (double complex z);
  676. double
  677. cabs (double complex z)
  678. {
  679. return hypot (REALPART (z), IMAGPART (z));
  680. }
  681. #endif
  682. #if !defined(HAVE_CABSL) && defined(HAVE_HYPOTL)
  683. #define HAVE_CABSL 1
  684. long double cabsl (long double complex z);
  685. long double
  686. cabsl (long double complex z)
  687. {
  688. return hypotl (REALPART (z), IMAGPART (z));
  689. }
  690. #endif
  691. #if !defined(HAVE_CARGF)
  692. #define HAVE_CARGF 1
  693. float cargf (float complex z);
  694. float
  695. cargf (float complex z)
  696. {
  697. return atan2f (IMAGPART (z), REALPART (z));
  698. }
  699. #endif
  700. #if !defined(HAVE_CARG)
  701. #define HAVE_CARG 1
  702. double carg (double complex z);
  703. double
  704. carg (double complex z)
  705. {
  706. return atan2 (IMAGPART (z), REALPART (z));
  707. }
  708. #endif
  709. #if !defined(HAVE_CARGL) && defined(HAVE_ATAN2L)
  710. #define HAVE_CARGL 1
  711. long double cargl (long double complex z);
  712. long double
  713. cargl (long double complex z)
  714. {
  715. return atan2l (IMAGPART (z), REALPART (z));
  716. }
  717. #endif
  718. /* exp(z) = exp(a)*(cos(b) + i sin(b)) */
  719. #if !defined(HAVE_CEXPF)
  720. #define HAVE_CEXPF 1
  721. float complex cexpf (float complex z);
  722. float complex
  723. cexpf (float complex z)
  724. {
  725. float a, b;
  726. float complex v;
  727. a = REALPART (z);
  728. b = IMAGPART (z);
  729. COMPLEX_ASSIGN (v, cosf (b), sinf (b));
  730. return expf (a) * v;
  731. }
  732. #endif
  733. #if !defined(HAVE_CEXP)
  734. #define HAVE_CEXP 1
  735. double complex cexp (double complex z);
  736. double complex
  737. cexp (double complex z)
  738. {
  739. double a, b;
  740. double complex v;
  741. a = REALPART (z);
  742. b = IMAGPART (z);
  743. COMPLEX_ASSIGN (v, cos (b), sin (b));
  744. return exp (a) * v;
  745. }
  746. #endif
  747. #if !defined(HAVE_CEXPL) && defined(HAVE_COSL) && defined(HAVE_SINL) && defined(EXPL)
  748. #define HAVE_CEXPL 1
  749. long double complex cexpl (long double complex z);
  750. long double complex
  751. cexpl (long double complex z)
  752. {
  753. long double a, b;
  754. long double complex v;
  755. a = REALPART (z);
  756. b = IMAGPART (z);
  757. COMPLEX_ASSIGN (v, cosl (b), sinl (b));
  758. return expl (a) * v;
  759. }
  760. #endif
  761. /* log(z) = log (cabs(z)) + i*carg(z) */
  762. #if !defined(HAVE_CLOGF)
  763. #define HAVE_CLOGF 1
  764. float complex clogf (float complex z);
  765. float complex
  766. clogf (float complex z)
  767. {
  768. float complex v;
  769. COMPLEX_ASSIGN (v, logf (cabsf (z)), cargf (z));
  770. return v;
  771. }
  772. #endif
  773. #if !defined(HAVE_CLOG)
  774. #define HAVE_CLOG 1
  775. double complex clog (double complex z);
  776. double complex
  777. clog (double complex z)
  778. {
  779. double complex v;
  780. COMPLEX_ASSIGN (v, log (cabs (z)), carg (z));
  781. return v;
  782. }
  783. #endif
  784. #if !defined(HAVE_CLOGL) && defined(HAVE_LOGL) && defined(HAVE_CABSL) && defined(HAVE_CARGL)
  785. #define HAVE_CLOGL 1
  786. long double complex clogl (long double complex z);
  787. long double complex
  788. clogl (long double complex z)
  789. {
  790. long double complex v;
  791. COMPLEX_ASSIGN (v, logl (cabsl (z)), cargl (z));
  792. return v;
  793. }
  794. #endif
  795. /* log10(z) = log10 (cabs(z)) + i*carg(z) */
  796. #if !defined(HAVE_CLOG10F)
  797. #define HAVE_CLOG10F 1
  798. float complex clog10f (float complex z);
  799. float complex
  800. clog10f (float complex z)
  801. {
  802. float complex v;
  803. COMPLEX_ASSIGN (v, log10f (cabsf (z)), cargf (z));
  804. return v;
  805. }
  806. #endif
  807. #if !defined(HAVE_CLOG10)
  808. #define HAVE_CLOG10 1
  809. double complex clog10 (double complex z);
  810. double complex
  811. clog10 (double complex z)
  812. {
  813. double complex v;
  814. COMPLEX_ASSIGN (v, log10 (cabs (z)), carg (z));
  815. return v;
  816. }
  817. #endif
  818. #if !defined(HAVE_CLOG10L) && defined(HAVE_LOG10L) && defined(HAVE_CABSL) && defined(HAVE_CARGL)
  819. #define HAVE_CLOG10L 1
  820. long double complex clog10l (long double complex z);
  821. long double complex
  822. clog10l (long double complex z)
  823. {
  824. long double complex v;
  825. COMPLEX_ASSIGN (v, log10l (cabsl (z)), cargl (z));
  826. return v;
  827. }
  828. #endif
  829. /* pow(base, power) = cexp (power * clog (base)) */
  830. #if !defined(HAVE_CPOWF)
  831. #define HAVE_CPOWF 1
  832. float complex cpowf (float complex base, float complex power);
  833. float complex
  834. cpowf (float complex base, float complex power)
  835. {
  836. return cexpf (power * clogf (base));
  837. }
  838. #endif
  839. #if !defined(HAVE_CPOW)
  840. #define HAVE_CPOW 1
  841. double complex cpow (double complex base, double complex power);
  842. double complex
  843. cpow (double complex base, double complex power)
  844. {
  845. return cexp (power * clog (base));
  846. }
  847. #endif
  848. #if !defined(HAVE_CPOWL) && defined(HAVE_CEXPL) && defined(HAVE_CLOGL)
  849. #define HAVE_CPOWL 1
  850. long double complex cpowl (long double complex base, long double complex power);
  851. long double complex
  852. cpowl (long double complex base, long double complex power)
  853. {
  854. return cexpl (power * clogl (base));
  855. }
  856. #endif
  857. /* sqrt(z). Algorithm pulled from glibc. */
  858. #if !defined(HAVE_CSQRTF)
  859. #define HAVE_CSQRTF 1
  860. float complex csqrtf (float complex z);
  861. float complex
  862. csqrtf (float complex z)
  863. {
  864. float re, im;
  865. float complex v;
  866. re = REALPART (z);
  867. im = IMAGPART (z);
  868. if (im == 0)
  869. {
  870. if (re < 0)
  871. {
  872. COMPLEX_ASSIGN (v, 0, copysignf (sqrtf (-re), im));
  873. }
  874. else
  875. {
  876. COMPLEX_ASSIGN (v, fabsf (sqrtf (re)), copysignf (0, im));
  877. }
  878. }
  879. else if (re == 0)
  880. {
  881. float r;
  882. r = sqrtf (0.5 * fabsf (im));
  883. COMPLEX_ASSIGN (v, r, copysignf (r, im));
  884. }
  885. else
  886. {
  887. float d, r, s;
  888. d = hypotf (re, im);
  889. /* Use the identity 2 Re res Im res = Im x
  890. to avoid cancellation error in d +/- Re x. */
  891. if (re > 0)
  892. {
  893. r = sqrtf (0.5 * d + 0.5 * re);
  894. s = (0.5 * im) / r;
  895. }
  896. else
  897. {
  898. s = sqrtf (0.5 * d - 0.5 * re);
  899. r = fabsf ((0.5 * im) / s);
  900. }
  901. COMPLEX_ASSIGN (v, r, copysignf (s, im));
  902. }
  903. return v;
  904. }
  905. #endif
  906. #if !defined(HAVE_CSQRT)
  907. #define HAVE_CSQRT 1
  908. double complex csqrt (double complex z);
  909. double complex
  910. csqrt (double complex z)
  911. {
  912. double re, im;
  913. double complex v;
  914. re = REALPART (z);
  915. im = IMAGPART (z);
  916. if (im == 0)
  917. {
  918. if (re < 0)
  919. {
  920. COMPLEX_ASSIGN (v, 0, copysign (sqrt (-re), im));
  921. }
  922. else
  923. {
  924. COMPLEX_ASSIGN (v, fabs (sqrt (re)), copysign (0, im));
  925. }
  926. }
  927. else if (re == 0)
  928. {
  929. double r;
  930. r = sqrt (0.5 * fabs (im));
  931. COMPLEX_ASSIGN (v, r, copysign (r, im));
  932. }
  933. else
  934. {
  935. double d, r, s;
  936. d = hypot (re, im);
  937. /* Use the identity 2 Re res Im res = Im x
  938. to avoid cancellation error in d +/- Re x. */
  939. if (re > 0)
  940. {
  941. r = sqrt (0.5 * d + 0.5 * re);
  942. s = (0.5 * im) / r;
  943. }
  944. else
  945. {
  946. s = sqrt (0.5 * d - 0.5 * re);
  947. r = fabs ((0.5 * im) / s);
  948. }
  949. COMPLEX_ASSIGN (v, r, copysign (s, im));
  950. }
  951. return v;
  952. }
  953. #endif
  954. #if !defined(HAVE_CSQRTL) && defined(HAVE_COPYSIGNL) && defined(HAVE_SQRTL) && defined(HAVE_FABSL) && defined(HAVE_HYPOTL)
  955. #define HAVE_CSQRTL 1
  956. long double complex csqrtl (long double complex z);
  957. long double complex
  958. csqrtl (long double complex z)
  959. {
  960. long double re, im;
  961. long double complex v;
  962. re = REALPART (z);
  963. im = IMAGPART (z);
  964. if (im == 0)
  965. {
  966. if (re < 0)
  967. {
  968. COMPLEX_ASSIGN (v, 0, copysignl (sqrtl (-re), im));
  969. }
  970. else
  971. {
  972. COMPLEX_ASSIGN (v, fabsl (sqrtl (re)), copysignl (0, im));
  973. }
  974. }
  975. else if (re == 0)
  976. {
  977. long double r;
  978. r = sqrtl (0.5 * fabsl (im));
  979. COMPLEX_ASSIGN (v, copysignl (r, im), r);
  980. }
  981. else
  982. {
  983. long double d, r, s;
  984. d = hypotl (re, im);
  985. /* Use the identity 2 Re res Im res = Im x
  986. to avoid cancellation error in d +/- Re x. */
  987. if (re > 0)
  988. {
  989. r = sqrtl (0.5 * d + 0.5 * re);
  990. s = (0.5 * im) / r;
  991. }
  992. else
  993. {
  994. s = sqrtl (0.5 * d - 0.5 * re);
  995. r = fabsl ((0.5 * im) / s);
  996. }
  997. COMPLEX_ASSIGN (v, r, copysignl (s, im));
  998. }
  999. return v;
  1000. }
  1001. #endif
  1002. /* sinh(a + i b) = sinh(a) cos(b) + i cosh(a) sin(b) */
  1003. #if !defined(HAVE_CSINHF)
  1004. #define HAVE_CSINHF 1
  1005. float complex csinhf (float complex a);
  1006. float complex
  1007. csinhf (float complex a)
  1008. {
  1009. float r, i;
  1010. float complex v;
  1011. r = REALPART (a);
  1012. i = IMAGPART (a);
  1013. COMPLEX_ASSIGN (v, sinhf (r) * cosf (i), coshf (r) * sinf (i));
  1014. return v;
  1015. }
  1016. #endif
  1017. #if !defined(HAVE_CSINH)
  1018. #define HAVE_CSINH 1
  1019. double complex csinh (double complex a);
  1020. double complex
  1021. csinh (double complex a)
  1022. {
  1023. double r, i;
  1024. double complex v;
  1025. r = REALPART (a);
  1026. i = IMAGPART (a);
  1027. COMPLEX_ASSIGN (v, sinh (r) * cos (i), cosh (r) * sin (i));
  1028. return v;
  1029. }
  1030. #endif
  1031. #if !defined(HAVE_CSINHL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
  1032. #define HAVE_CSINHL 1
  1033. long double complex csinhl (long double complex a);
  1034. long double complex
  1035. csinhl (long double complex a)
  1036. {
  1037. long double r, i;
  1038. long double complex v;
  1039. r = REALPART (a);
  1040. i = IMAGPART (a);
  1041. COMPLEX_ASSIGN (v, sinhl (r) * cosl (i), coshl (r) * sinl (i));
  1042. return v;
  1043. }
  1044. #endif
  1045. /* cosh(a + i b) = cosh(a) cos(b) + i sinh(a) sin(b) */
  1046. #if !defined(HAVE_CCOSHF)
  1047. #define HAVE_CCOSHF 1
  1048. float complex ccoshf (float complex a);
  1049. float complex
  1050. ccoshf (float complex a)
  1051. {
  1052. float r, i;
  1053. float complex v;
  1054. r = REALPART (a);
  1055. i = IMAGPART (a);
  1056. COMPLEX_ASSIGN (v, coshf (r) * cosf (i), sinhf (r) * sinf (i));
  1057. return v;
  1058. }
  1059. #endif
  1060. #if !defined(HAVE_CCOSH)
  1061. #define HAVE_CCOSH 1
  1062. double complex ccosh (double complex a);
  1063. double complex
  1064. ccosh (double complex a)
  1065. {
  1066. double r, i;
  1067. double complex v;
  1068. r = REALPART (a);
  1069. i = IMAGPART (a);
  1070. COMPLEX_ASSIGN (v, cosh (r) * cos (i), sinh (r) * sin (i));
  1071. return v;
  1072. }
  1073. #endif
  1074. #if !defined(HAVE_CCOSHL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
  1075. #define HAVE_CCOSHL 1
  1076. long double complex ccoshl (long double complex a);
  1077. long double complex
  1078. ccoshl (long double complex a)
  1079. {
  1080. long double r, i;
  1081. long double complex v;
  1082. r = REALPART (a);
  1083. i = IMAGPART (a);
  1084. COMPLEX_ASSIGN (v, coshl (r) * cosl (i), sinhl (r) * sinl (i));
  1085. return v;
  1086. }
  1087. #endif
  1088. /* tanh(a + i b) = (tanh(a) + i tan(b)) / (1 + i tanh(a) tan(b)) */
  1089. #if !defined(HAVE_CTANHF)
  1090. #define HAVE_CTANHF 1
  1091. float complex ctanhf (float complex a);
  1092. float complex
  1093. ctanhf (float complex a)
  1094. {
  1095. float rt, it;
  1096. float complex n, d;
  1097. rt = tanhf (REALPART (a));
  1098. it = tanf (IMAGPART (a));
  1099. COMPLEX_ASSIGN (n, rt, it);
  1100. COMPLEX_ASSIGN (d, 1, rt * it);
  1101. return n / d;
  1102. }
  1103. #endif
  1104. #if !defined(HAVE_CTANH)
  1105. #define HAVE_CTANH 1
  1106. double complex ctanh (double complex a);
  1107. double complex
  1108. ctanh (double complex a)
  1109. {
  1110. double rt, it;
  1111. double complex n, d;
  1112. rt = tanh (REALPART (a));
  1113. it = tan (IMAGPART (a));
  1114. COMPLEX_ASSIGN (n, rt, it);
  1115. COMPLEX_ASSIGN (d, 1, rt * it);
  1116. return n / d;
  1117. }
  1118. #endif
  1119. #if !defined(HAVE_CTANHL) && defined(HAVE_TANL) && defined(HAVE_TANHL)
  1120. #define HAVE_CTANHL 1
  1121. long double complex ctanhl (long double complex a);
  1122. long double complex
  1123. ctanhl (long double complex a)
  1124. {
  1125. long double rt, it;
  1126. long double complex n, d;
  1127. rt = tanhl (REALPART (a));
  1128. it = tanl (IMAGPART (a));
  1129. COMPLEX_ASSIGN (n, rt, it);
  1130. COMPLEX_ASSIGN (d, 1, rt * it);
  1131. return n / d;
  1132. }
  1133. #endif
  1134. /* sin(a + i b) = sin(a) cosh(b) + i cos(a) sinh(b) */
  1135. #if !defined(HAVE_CSINF)
  1136. #define HAVE_CSINF 1
  1137. float complex csinf (float complex a);
  1138. float complex
  1139. csinf (float complex a)
  1140. {
  1141. float r, i;
  1142. float complex v;
  1143. r = REALPART (a);
  1144. i = IMAGPART (a);
  1145. COMPLEX_ASSIGN (v, sinf (r) * coshf (i), cosf (r) * sinhf (i));
  1146. return v;
  1147. }
  1148. #endif
  1149. #if !defined(HAVE_CSIN)
  1150. #define HAVE_CSIN 1
  1151. double complex csin (double complex a);
  1152. double complex
  1153. csin (double complex a)
  1154. {
  1155. double r, i;
  1156. double complex v;
  1157. r = REALPART (a);
  1158. i = IMAGPART (a);
  1159. COMPLEX_ASSIGN (v, sin (r) * cosh (i), cos (r) * sinh (i));
  1160. return v;
  1161. }
  1162. #endif
  1163. #if !defined(HAVE_CSINL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
  1164. #define HAVE_CSINL 1
  1165. long double complex csinl (long double complex a);
  1166. long double complex
  1167. csinl (long double complex a)
  1168. {
  1169. long double r, i;
  1170. long double complex v;
  1171. r = REALPART (a);
  1172. i = IMAGPART (a);
  1173. COMPLEX_ASSIGN (v, sinl (r) * coshl (i), cosl (r) * sinhl (i));
  1174. return v;
  1175. }
  1176. #endif
  1177. /* cos(a + i b) = cos(a) cosh(b) - i sin(a) sinh(b) */
  1178. #if !defined(HAVE_CCOSF)
  1179. #define HAVE_CCOSF 1
  1180. float complex ccosf (float complex a);
  1181. float complex
  1182. ccosf (float complex a)
  1183. {
  1184. float r, i;
  1185. float complex v;
  1186. r = REALPART (a);
  1187. i = IMAGPART (a);
  1188. COMPLEX_ASSIGN (v, cosf (r) * coshf (i), - (sinf (r) * sinhf (i)));
  1189. return v;
  1190. }
  1191. #endif
  1192. #if !defined(HAVE_CCOS)
  1193. #define HAVE_CCOS 1
  1194. double complex ccos (double complex a);
  1195. double complex
  1196. ccos (double complex a)
  1197. {
  1198. double r, i;
  1199. double complex v;
  1200. r = REALPART (a);
  1201. i = IMAGPART (a);
  1202. COMPLEX_ASSIGN (v, cos (r) * cosh (i), - (sin (r) * sinh (i)));
  1203. return v;
  1204. }
  1205. #endif
  1206. #if !defined(HAVE_CCOSL) && defined(HAVE_COSL) && defined(HAVE_COSHL) && defined(HAVE_SINL) && defined(HAVE_SINHL)
  1207. #define HAVE_CCOSL 1
  1208. long double complex ccosl (long double complex a);
  1209. long double complex
  1210. ccosl (long double complex a)
  1211. {
  1212. long double r, i;
  1213. long double complex v;
  1214. r = REALPART (a);
  1215. i = IMAGPART (a);
  1216. COMPLEX_ASSIGN (v, cosl (r) * coshl (i), - (sinl (r) * sinhl (i)));
  1217. return v;
  1218. }
  1219. #endif
  1220. /* tan(a + i b) = (tan(a) + i tanh(b)) / (1 - i tan(a) tanh(b)) */
  1221. #if !defined(HAVE_CTANF)
  1222. #define HAVE_CTANF 1
  1223. float complex ctanf (float complex a);
  1224. float complex
  1225. ctanf (float complex a)
  1226. {
  1227. float rt, it;
  1228. float complex n, d;
  1229. rt = tanf (REALPART (a));
  1230. it = tanhf (IMAGPART (a));
  1231. COMPLEX_ASSIGN (n, rt, it);
  1232. COMPLEX_ASSIGN (d, 1, - (rt * it));
  1233. return n / d;
  1234. }
  1235. #endif
  1236. #if !defined(HAVE_CTAN)
  1237. #define HAVE_CTAN 1
  1238. double complex ctan (double complex a);
  1239. double complex
  1240. ctan (double complex a)
  1241. {
  1242. double rt, it;
  1243. double complex n, d;
  1244. rt = tan (REALPART (a));
  1245. it = tanh (IMAGPART (a));
  1246. COMPLEX_ASSIGN (n, rt, it);
  1247. COMPLEX_ASSIGN (d, 1, - (rt * it));
  1248. return n / d;
  1249. }
  1250. #endif
  1251. #if !defined(HAVE_CTANL) && defined(HAVE_TANL) && defined(HAVE_TANHL)
  1252. #define HAVE_CTANL 1
  1253. long double complex ctanl (long double complex a);
  1254. long double complex
  1255. ctanl (long double complex a)
  1256. {
  1257. long double rt, it;
  1258. long double complex n, d;
  1259. rt = tanl (REALPART (a));
  1260. it = tanhl (IMAGPART (a));
  1261. COMPLEX_ASSIGN (n, rt, it);
  1262. COMPLEX_ASSIGN (d, 1, - (rt * it));
  1263. return n / d;
  1264. }
  1265. #endif
  1266. /* Complex ASIN. Returns wrongly NaN for infinite arguments.
  1267. Algorithm taken from Abramowitz & Stegun. */
  1268. #if !defined(HAVE_CASINF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
  1269. #define HAVE_CASINF 1
  1270. complex float casinf (complex float z);
  1271. complex float
  1272. casinf (complex float z)
  1273. {
  1274. return -I*clogf (I*z + csqrtf (1.0f-z*z));
  1275. }
  1276. #endif
  1277. #if !defined(HAVE_CASIN) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
  1278. #define HAVE_CASIN 1
  1279. complex double casin (complex double z);
  1280. complex double
  1281. casin (complex double z)
  1282. {
  1283. return -I*clog (I*z + csqrt (1.0-z*z));
  1284. }
  1285. #endif
  1286. #if !defined(HAVE_CASINL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
  1287. #define HAVE_CASINL 1
  1288. complex long double casinl (complex long double z);
  1289. complex long double
  1290. casinl (complex long double z)
  1291. {
  1292. return -I*clogl (I*z + csqrtl (1.0L-z*z));
  1293. }
  1294. #endif
  1295. /* Complex ACOS. Returns wrongly NaN for infinite arguments.
  1296. Algorithm taken from Abramowitz & Stegun. */
  1297. #if !defined(HAVE_CACOSF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
  1298. #define HAVE_CACOSF 1
  1299. complex float cacosf (complex float z);
  1300. complex float
  1301. cacosf (complex float z)
  1302. {
  1303. return -I*clogf (z + I*csqrtf (1.0f-z*z));
  1304. }
  1305. #endif
  1306. #if !defined(HAVE_CACOS) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
  1307. #define HAVE_CACOS 1
  1308. complex double cacos (complex double z);
  1309. complex double
  1310. cacos (complex double z)
  1311. {
  1312. return -I*clog (z + I*csqrt (1.0-z*z));
  1313. }
  1314. #endif
  1315. #if !defined(HAVE_CACOSL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
  1316. #define HAVE_CACOSL 1
  1317. complex long double cacosl (complex long double z);
  1318. complex long double
  1319. cacosl (complex long double z)
  1320. {
  1321. return -I*clogl (z + I*csqrtl (1.0L-z*z));
  1322. }
  1323. #endif
  1324. /* Complex ATAN. Returns wrongly NaN for infinite arguments.
  1325. Algorithm taken from Abramowitz & Stegun. */
  1326. #if !defined(HAVE_CATANF) && defined(HAVE_CLOGF)
  1327. #define HAVE_CACOSF 1
  1328. complex float catanf (complex float z);
  1329. complex float
  1330. catanf (complex float z)
  1331. {
  1332. return I*clogf ((I+z)/(I-z))/2.0f;
  1333. }
  1334. #endif
  1335. #if !defined(HAVE_CATAN) && defined(HAVE_CLOG)
  1336. #define HAVE_CACOS 1
  1337. complex double catan (complex double z);
  1338. complex double
  1339. catan (complex double z)
  1340. {
  1341. return I*clog ((I+z)/(I-z))/2.0;
  1342. }
  1343. #endif
  1344. #if !defined(HAVE_CATANL) && defined(HAVE_CLOGL)
  1345. #define HAVE_CACOSL 1
  1346. complex long double catanl (complex long double z);
  1347. complex long double
  1348. catanl (complex long double z)
  1349. {
  1350. return I*clogl ((I+z)/(I-z))/2.0L;
  1351. }
  1352. #endif
  1353. /* Complex ASINH. Returns wrongly NaN for infinite arguments.
  1354. Algorithm taken from Abramowitz & Stegun. */
  1355. #if !defined(HAVE_CASINHF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
  1356. #define HAVE_CASINHF 1
  1357. complex float casinhf (complex float z);
  1358. complex float
  1359. casinhf (complex float z)
  1360. {
  1361. return clogf (z + csqrtf (z*z+1.0f));
  1362. }
  1363. #endif
  1364. #if !defined(HAVE_CASINH) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
  1365. #define HAVE_CASINH 1
  1366. complex double casinh (complex double z);
  1367. complex double
  1368. casinh (complex double z)
  1369. {
  1370. return clog (z + csqrt (z*z+1.0));
  1371. }
  1372. #endif
  1373. #if !defined(HAVE_CASINHL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
  1374. #define HAVE_CASINHL 1
  1375. complex long double casinhl (complex long double z);
  1376. complex long double
  1377. casinhl (complex long double z)
  1378. {
  1379. return clogl (z + csqrtl (z*z+1.0L));
  1380. }
  1381. #endif
  1382. /* Complex ACOSH. Returns wrongly NaN for infinite arguments.
  1383. Algorithm taken from Abramowitz & Stegun. */
  1384. #if !defined(HAVE_CACOSHF) && defined(HAVE_CLOGF) && defined(HAVE_CSQRTF)
  1385. #define HAVE_CACOSHF 1
  1386. complex float cacoshf (complex float z);
  1387. complex float
  1388. cacoshf (complex float z)
  1389. {
  1390. return clogf (z + csqrtf (z-1.0f) * csqrtf (z+1.0f));
  1391. }
  1392. #endif
  1393. #if !defined(HAVE_CACOSH) && defined(HAVE_CLOG) && defined(HAVE_CSQRT)
  1394. #define HAVE_CACOSH 1
  1395. complex double cacosh (complex double z);
  1396. complex double
  1397. cacosh (complex double z)
  1398. {
  1399. return clog (z + csqrt (z-1.0) * csqrt (z+1.0));
  1400. }
  1401. #endif
  1402. #if !defined(HAVE_CACOSHL) && defined(HAVE_CLOGL) && defined(HAVE_CSQRTL)
  1403. #define HAVE_CACOSHL 1
  1404. complex long double cacoshl (complex long double z);
  1405. complex long double
  1406. cacoshl (complex long double z)
  1407. {
  1408. return clogl (z + csqrtl (z-1.0L) * csqrtl (z+1.0L));
  1409. }
  1410. #endif
  1411. /* Complex ATANH. Returns wrongly NaN for infinite arguments.
  1412. Algorithm taken from Abramowitz & Stegun. */
  1413. #if !defined(HAVE_CATANHF) && defined(HAVE_CLOGF)
  1414. #define HAVE_CATANHF 1
  1415. complex float catanhf (complex float z);
  1416. complex float
  1417. catanhf (complex float z)
  1418. {
  1419. return clogf ((1.0f+z)/(1.0f-z))/2.0f;
  1420. }
  1421. #endif
  1422. #if !defined(HAVE_CATANH) && defined(HAVE_CLOG)
  1423. #define HAVE_CATANH 1
  1424. complex double catanh (complex double z);
  1425. complex double
  1426. catanh (complex double z)
  1427. {
  1428. return clog ((1.0+z)/(1.0-z))/2.0;
  1429. }
  1430. #endif
  1431. #if !defined(HAVE_CATANHL) && defined(HAVE_CLOGL)
  1432. #define HAVE_CATANHL 1
  1433. complex long double catanhl (complex long double z);
  1434. complex long double
  1435. catanhl (complex long double z)
  1436. {
  1437. return clogl ((1.0L+z)/(1.0L-z))/2.0L;
  1438. }
  1439. #endif
  1440. #if !defined(HAVE_TGAMMA)
  1441. #define HAVE_TGAMMA 1
  1442. double tgamma (double);
  1443. /* Fallback tgamma() function. Uses the algorithm from
  1444. http://www.netlib.org/specfun/gamma and references therein. */
  1445. #undef SQRTPI
  1446. #define SQRTPI 0.9189385332046727417803297
  1447. #undef PI
  1448. #define PI 3.1415926535897932384626434
  1449. double
  1450. tgamma (double x)
  1451. {
  1452. int i, n, parity;
  1453. double fact, res, sum, xden, xnum, y, y1, ysq, z;
  1454. static double p[8] = {
  1455. -1.71618513886549492533811e0, 2.47656508055759199108314e1,
  1456. -3.79804256470945635097577e2, 6.29331155312818442661052e2,
  1457. 8.66966202790413211295064e2, -3.14512729688483675254357e4,
  1458. -3.61444134186911729807069e4, 6.64561438202405440627855e4 };
  1459. static double q[8] = {
  1460. -3.08402300119738975254353e1, 3.15350626979604161529144e2,
  1461. -1.01515636749021914166146e3, -3.10777167157231109440444e3,
  1462. 2.25381184209801510330112e4, 4.75584627752788110767815e3,
  1463. -1.34659959864969306392456e5, -1.15132259675553483497211e5 };
  1464. static double c[7] = { -1.910444077728e-03,
  1465. 8.4171387781295e-04, -5.952379913043012e-04,
  1466. 7.93650793500350248e-04, -2.777777777777681622553e-03,
  1467. 8.333333333333333331554247e-02, 5.7083835261e-03 };
  1468. static const double xminin = 2.23e-308;
  1469. static const double xbig = 171.624;
  1470. static const double xnan = __builtin_nan ("0x0"), xinf = __builtin_inf ();
  1471. static double eps = 0;
  1472. if (eps == 0)
  1473. eps = nextafter (1., 2.) - 1.;
  1474. parity = 0;
  1475. fact = 1;
  1476. n = 0;
  1477. y = x;
  1478. if (isnan (x))
  1479. return x;
  1480. if (y <= 0)
  1481. {
  1482. y = -x;
  1483. y1 = trunc (y);
  1484. res = y - y1;
  1485. if (res != 0)
  1486. {
  1487. if (y1 != trunc (y1*0.5l)*2)
  1488. parity = 1;
  1489. fact = -PI / sin (PI*res);
  1490. y = y + 1;
  1491. }
  1492. else
  1493. return x == 0 ? copysign (xinf, x) : xnan;
  1494. }
  1495. if (y < eps)
  1496. {
  1497. if (y >= xminin)
  1498. res = 1 / y;
  1499. else
  1500. return xinf;
  1501. }
  1502. else if (y < 13)
  1503. {
  1504. y1 = y;
  1505. if (y < 1)
  1506. {
  1507. z = y;
  1508. y = y + 1;
  1509. }
  1510. else
  1511. {
  1512. n = (int)y - 1;
  1513. y = y - n;
  1514. z = y - 1;
  1515. }
  1516. xnum = 0;
  1517. xden = 1;
  1518. for (i = 0; i < 8; i++)
  1519. {
  1520. xnum = (xnum + p[i]) * z;
  1521. xden = xden * z + q[i];
  1522. }
  1523. res = xnum / xden + 1;
  1524. if (y1 < y)
  1525. res = res / y1;
  1526. else if (y1 > y)
  1527. for (i = 1; i <= n; i++)
  1528. {
  1529. res = res * y;
  1530. y = y + 1;
  1531. }
  1532. }
  1533. else
  1534. {
  1535. if (y < xbig)
  1536. {
  1537. ysq = y * y;
  1538. sum = c[6];
  1539. for (i = 0; i < 6; i++)
  1540. sum = sum / ysq + c[i];
  1541. sum = sum/y - y + SQRTPI;
  1542. sum = sum + (y - 0.5) * log (y);
  1543. res = exp (sum);
  1544. }
  1545. else
  1546. return x < 0 ? xnan : xinf;
  1547. }
  1548. if (parity)
  1549. res = -res;
  1550. if (fact != 1)
  1551. res = fact / res;
  1552. return res;
  1553. }
  1554. #endif
  1555. #if !defined(HAVE_LGAMMA)
  1556. #define HAVE_LGAMMA 1
  1557. double lgamma (double);
  1558. /* Fallback lgamma() function. Uses the algorithm from
  1559. http://www.netlib.org/specfun/algama and references therein,
  1560. except for negative arguments (where netlib would return +Inf)
  1561. where we use the following identity:
  1562. lgamma(y) = log(pi/(|y*sin(pi*y)|)) - lgamma(-y)
  1563. */
  1564. double
  1565. lgamma (double y)
  1566. {
  1567. #undef SQRTPI
  1568. #define SQRTPI 0.9189385332046727417803297
  1569. #undef PI
  1570. #define PI 3.1415926535897932384626434
  1571. #define PNT68 0.6796875
  1572. #define D1 -0.5772156649015328605195174
  1573. #define D2 0.4227843350984671393993777
  1574. #define D4 1.791759469228055000094023
  1575. static double p1[8] = {
  1576. 4.945235359296727046734888e0, 2.018112620856775083915565e2,
  1577. 2.290838373831346393026739e3, 1.131967205903380828685045e4,
  1578. 2.855724635671635335736389e4, 3.848496228443793359990269e4,
  1579. 2.637748787624195437963534e4, 7.225813979700288197698961e3 };
  1580. static double q1[8] = {
  1581. 6.748212550303777196073036e1, 1.113332393857199323513008e3,
  1582. 7.738757056935398733233834e3, 2.763987074403340708898585e4,
  1583. 5.499310206226157329794414e4, 6.161122180066002127833352e4,
  1584. 3.635127591501940507276287e4, 8.785536302431013170870835e3 };
  1585. static double p2[8] = {
  1586. 4.974607845568932035012064e0, 5.424138599891070494101986e2,
  1587. 1.550693864978364947665077e4, 1.847932904445632425417223e5,
  1588. 1.088204769468828767498470e6, 3.338152967987029735917223e6,
  1589. 5.106661678927352456275255e6, 3.074109054850539556250927e6 };
  1590. static double q2[8] = {
  1591. 1.830328399370592604055942e2, 7.765049321445005871323047e3,
  1592. 1.331903827966074194402448e5, 1.136705821321969608938755e6,
  1593. 5.267964117437946917577538e6, 1.346701454311101692290052e7,
  1594. 1.782736530353274213975932e7, 9.533095591844353613395747e6 };
  1595. static double p4[8] = {
  1596. 1.474502166059939948905062e4, 2.426813369486704502836312e6,
  1597. 1.214755574045093227939592e8, 2.663432449630976949898078e9,
  1598. 2.940378956634553899906876e10, 1.702665737765398868392998e11,
  1599. 4.926125793377430887588120e11, 5.606251856223951465078242e11 };
  1600. static double q4[8] = {
  1601. 2.690530175870899333379843e3, 6.393885654300092398984238e5,
  1602. 4.135599930241388052042842e7, 1.120872109616147941376570e9,
  1603. 1.488613728678813811542398e10, 1.016803586272438228077304e11,
  1604. 3.417476345507377132798597e11, 4.463158187419713286462081e11 };
  1605. static double c[7] = {
  1606. -1.910444077728e-03, 8.4171387781295e-04,
  1607. -5.952379913043012e-04, 7.93650793500350248e-04,
  1608. -2.777777777777681622553e-03, 8.333333333333333331554247e-02,
  1609. 5.7083835261e-03 };
  1610. static double xbig = 2.55e305, xinf = __builtin_inf (), eps = 0,
  1611. frtbig = 2.25e76;
  1612. int i;
  1613. double corr, res, xden, xm1, xm2, xm4, xnum, ysq;
  1614. if (eps == 0)
  1615. eps = __builtin_nextafter (1., 2.) - 1.;
  1616. if ((y > 0) && (y <= xbig))
  1617. {
  1618. if (y <= eps)
  1619. res = -log (y);
  1620. else if (y <= 1.5)
  1621. {
  1622. if (y < PNT68)
  1623. {
  1624. corr = -log (y);
  1625. xm1 = y;
  1626. }
  1627. else
  1628. {
  1629. corr = 0;
  1630. xm1 = (y - 0.5) - 0.5;
  1631. }
  1632. if ((y <= 0.5) || (y >= PNT68))
  1633. {
  1634. xden = 1;
  1635. xnum = 0;
  1636. for (i = 0; i < 8; i++)
  1637. {
  1638. xnum = xnum*xm1 + p1[i];
  1639. xden = xden*xm1 + q1[i];
  1640. }
  1641. res = corr + (xm1 * (D1 + xm1*(xnum/xden)));
  1642. }
  1643. else
  1644. {
  1645. xm2 = (y - 0.5) - 0.5;
  1646. xden = 1;
  1647. xnum = 0;
  1648. for (i = 0; i < 8; i++)
  1649. {
  1650. xnum = xnum*xm2 + p2[i];
  1651. xden = xden*xm2 + q2[i];
  1652. }
  1653. res = corr + xm2 * (D2 + xm2*(xnum/xden));
  1654. }
  1655. }
  1656. else if (y <= 4)
  1657. {
  1658. xm2 = y - 2;
  1659. xden = 1;
  1660. xnum = 0;
  1661. for (i = 0; i < 8; i++)
  1662. {
  1663. xnum = xnum*xm2 + p2[i];
  1664. xden = xden*xm2 + q2[i];
  1665. }
  1666. res = xm2 * (D2 + xm2*(xnum/xden));
  1667. }
  1668. else if (y <= 12)
  1669. {
  1670. xm4 = y - 4;
  1671. xden = -1;
  1672. xnum = 0;
  1673. for (i = 0; i < 8; i++)
  1674. {
  1675. xnum = xnum*xm4 + p4[i];
  1676. xden = xden*xm4 + q4[i];
  1677. }
  1678. res = D4 + xm4*(xnum/xden);
  1679. }
  1680. else
  1681. {
  1682. res = 0;
  1683. if (y <= frtbig)
  1684. {
  1685. res = c[6];
  1686. ysq = y * y;
  1687. for (i = 0; i < 6; i++)
  1688. res = res / ysq + c[i];
  1689. }
  1690. res = res/y;
  1691. corr = log (y);
  1692. res = res + SQRTPI - 0.5*corr;
  1693. res = res + y*(corr-1);
  1694. }
  1695. }
  1696. else if (y < 0 && __builtin_floor (y) != y)
  1697. {
  1698. /* lgamma(y) = log(pi/(|y*sin(pi*y)|)) - lgamma(-y)
  1699. For abs(y) very close to zero, we use a series expansion to
  1700. the first order in y to avoid overflow. */
  1701. if (y > -1.e-100)
  1702. res = -2 * log (fabs (y)) - lgamma (-y);
  1703. else
  1704. res = log (PI / fabs (y * sin (PI * y))) - lgamma (-y);
  1705. }
  1706. else
  1707. res = xinf;
  1708. return res;
  1709. }
  1710. #endif
  1711. #if defined(HAVE_TGAMMA) && !defined(HAVE_TGAMMAF)
  1712. #define HAVE_TGAMMAF 1
  1713. float tgammaf (float);
  1714. float
  1715. tgammaf (float x)
  1716. {
  1717. return (float) tgamma ((double) x);
  1718. }
  1719. #endif
  1720. #if defined(HAVE_LGAMMA) && !defined(HAVE_LGAMMAF)
  1721. #define HAVE_LGAMMAF 1
  1722. float lgammaf (float);
  1723. float
  1724. lgammaf (float x)
  1725. {
  1726. return (float) lgamma ((double) x);
  1727. }
  1728. #endif