math.in.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* A GNU-like <math.h>.
  2. Copyright (C) 2002-2003, 2007-2009 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef _GL_MATH_H
  14. #if __GNUC__ >= 3
  15. @PRAGMA_SYSTEM_HEADER@
  16. #endif
  17. /* The include_next requires a split double-inclusion guard. */
  18. #@INCLUDE_NEXT_AS_FIRST_DIRECTIVE@ @NEXT_MATH_H@
  19. #ifndef _GL_MATH_H
  20. #define _GL_MATH_H
  21. /* The definition of GL_LINK_WARNING is copied here. */
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* POSIX allows platforms that don't support NAN. But all major
  26. machines in the past 15 years have supported something close to
  27. IEEE NaN, so we define this unconditionally. We also must define
  28. it on platforms like Solaris 10, where NAN is present but defined
  29. as a function pointer rather than a floating point constant. */
  30. #if !defined NAN || @REPLACE_NAN@
  31. # undef NAN
  32. /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
  33. # ifdef __DECC
  34. static float
  35. _NaN ()
  36. {
  37. static float zero = 0.0f;
  38. return zero / zero;
  39. }
  40. # define NAN (_NaN())
  41. # else
  42. # define NAN (0.0f / 0.0f)
  43. # endif
  44. #endif
  45. /* Solaris 10 defines HUGE_VAL, but as a function pointer rather
  46. than a floating point constant. */
  47. #if @REPLACE_HUGE_VAL@
  48. # undef HUGE_VAL
  49. # define HUGE_VAL (1.0 / 0.0)
  50. #endif
  51. /* Write x as
  52. x = mantissa * 2^exp
  53. where
  54. If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
  55. If x is zero: mantissa = x, exp = 0.
  56. If x is infinite or NaN: mantissa = x, exp unspecified.
  57. Store exp in *EXPPTR and return mantissa. */
  58. #if @GNULIB_FREXP@
  59. # if @REPLACE_FREXP@
  60. # define frexp rpl_frexp
  61. extern double frexp (double x, int *expptr);
  62. # endif
  63. #elif defined GNULIB_POSIXCHECK
  64. # undef frexp
  65. # define frexp(x,e) \
  66. (GL_LINK_WARNING ("frexp is unportable - " \
  67. "use gnulib module frexp for portability"), \
  68. frexp (x, e))
  69. #endif
  70. #if @GNULIB_MATHL@ || !@HAVE_DECL_ACOSL@
  71. extern long double acosl (long double x);
  72. #endif
  73. #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
  74. # undef acosl
  75. # define acosl(x) \
  76. (GL_LINK_WARNING ("acosl is unportable - " \
  77. "use gnulib module mathl for portability"), \
  78. acosl (x))
  79. #endif
  80. #if @GNULIB_MATHL@ || !@HAVE_DECL_ASINL@
  81. extern long double asinl (long double x);
  82. #endif
  83. #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
  84. # undef asinl
  85. # define asinl(x) \
  86. (GL_LINK_WARNING ("asinl is unportable - " \
  87. "use gnulib module mathl for portability"), \
  88. asinl (x))
  89. #endif
  90. #if @GNULIB_MATHL@ || !@HAVE_DECL_ATANL@
  91. extern long double atanl (long double x);
  92. #endif
  93. #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
  94. # undef atanl
  95. # define atanl(x) \
  96. (GL_LINK_WARNING ("atanl is unportable - " \
  97. "use gnulib module mathl for portability"), \
  98. atanl (x))
  99. #endif
  100. #if @GNULIB_CEILF@
  101. # if @REPLACE_CEILF@
  102. # define ceilf rpl_ceilf
  103. extern float ceilf (float x);
  104. # endif
  105. #elif defined GNULIB_POSIXCHECK
  106. # undef ceilf
  107. # define ceilf(x) \
  108. (GL_LINK_WARNING ("ceilf is unportable - " \
  109. "use gnulib module ceilf for portability"), \
  110. ceilf (x))
  111. #endif
  112. #if @GNULIB_CEILL@
  113. # if @REPLACE_CEILL@
  114. # define ceill rpl_ceill
  115. extern long double ceill (long double x);
  116. # endif
  117. #elif defined GNULIB_POSIXCHECK
  118. # undef ceill
  119. # define ceill(x) \
  120. (GL_LINK_WARNING ("ceill is unportable - " \
  121. "use gnulib module ceill for portability"), \
  122. ceill (x))
  123. #endif
  124. #if @GNULIB_MATHL@ || !@HAVE_DECL_COSL@
  125. extern long double cosl (long double x);
  126. #endif
  127. #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
  128. # undef cosl
  129. # define cosl(x) \
  130. (GL_LINK_WARNING ("cosl is unportable - " \
  131. "use gnulib module mathl for portability"), \
  132. cosl (x))
  133. #endif
  134. #if @GNULIB_MATHL@ || !@HAVE_DECL_EXPL@
  135. extern long double expl (long double x);
  136. #endif
  137. #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
  138. # undef expl
  139. # define expl(x) \
  140. (GL_LINK_WARNING ("expl is unportable - " \
  141. "use gnulib module mathl for portability"), \
  142. expl (x))
  143. #endif
  144. #if @GNULIB_FLOORF@
  145. # if @REPLACE_FLOORF@
  146. # define floorf rpl_floorf
  147. extern float floorf (float x);
  148. # endif
  149. #elif defined GNULIB_POSIXCHECK
  150. # undef floorf
  151. # define floorf(x) \
  152. (GL_LINK_WARNING ("floorf is unportable - " \
  153. "use gnulib module floorf for portability"), \
  154. floorf (x))
  155. #endif
  156. #if @GNULIB_FLOORL@
  157. # if @REPLACE_FLOORL@
  158. # define floorl rpl_floorl
  159. extern long double floorl (long double x);
  160. # endif
  161. #elif defined GNULIB_POSIXCHECK
  162. # undef floorl
  163. # define floorl(x) \
  164. (GL_LINK_WARNING ("floorl is unportable - " \
  165. "use gnulib module floorl for portability"), \
  166. floorl (x))
  167. #endif
  168. /* Write x as
  169. x = mantissa * 2^exp
  170. where
  171. If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
  172. If x is zero: mantissa = x, exp = 0.
  173. If x is infinite or NaN: mantissa = x, exp unspecified.
  174. Store exp in *EXPPTR and return mantissa. */
  175. #if @GNULIB_FREXPL@ && @REPLACE_FREXPL@
  176. # define frexpl rpl_frexpl
  177. #endif
  178. #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
  179. extern long double frexpl (long double x, int *expptr);
  180. #endif
  181. #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
  182. # undef frexpl
  183. # define frexpl(x,e) \
  184. (GL_LINK_WARNING ("frexpl is unportable - " \
  185. "use gnulib module frexpl for portability"), \
  186. frexpl (x, e))
  187. #endif
  188. /* Return x * 2^exp. */
  189. #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
  190. # define ldexpl rpl_ldexpl
  191. #endif
  192. #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
  193. extern long double ldexpl (long double x, int exp);
  194. #endif
  195. #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
  196. # undef ldexpl
  197. # define ldexpl(x,e) \
  198. (GL_LINK_WARNING ("ldexpl is unportable - " \
  199. "use gnulib module ldexpl for portability"), \
  200. ldexpl (x, e))
  201. #endif
  202. #if @GNULIB_MATHL@ || !@HAVE_DECL_LOGL@
  203. extern long double logl (long double x);
  204. #endif
  205. #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
  206. # undef logl
  207. # define logl(x) \
  208. (GL_LINK_WARNING ("logl is unportable - " \
  209. "use gnulib module mathl for portability"), \
  210. logl (x))
  211. #endif
  212. #if @GNULIB_ROUNDF@
  213. # if @REPLACE_ROUNDF@
  214. # undef roundf
  215. # define roundf rpl_roundf
  216. extern float roundf (float x);
  217. # endif
  218. #elif defined GNULIB_POSIXCHECK
  219. # undef roundf
  220. # define roundf(x) \
  221. (GL_LINK_WARNING ("roundf is unportable - " \
  222. "use gnulib module roundf for portability"), \
  223. roundf (x))
  224. #endif
  225. #if @GNULIB_ROUND@
  226. # if @REPLACE_ROUND@
  227. # undef round
  228. # define round rpl_round
  229. extern double round (double x);
  230. # endif
  231. #elif defined GNULIB_POSIXCHECK
  232. # undef round
  233. # define round(x) \
  234. (GL_LINK_WARNING ("round is unportable - " \
  235. "use gnulib module round for portability"), \
  236. round (x))
  237. #endif
  238. #if @GNULIB_ROUNDL@
  239. # if @REPLACE_ROUNDL@
  240. # undef roundl
  241. # define roundl rpl_roundl
  242. extern long double roundl (long double x);
  243. # endif
  244. #elif defined GNULIB_POSIXCHECK
  245. # undef roundl
  246. # define roundl(x) \
  247. (GL_LINK_WARNING ("roundl is unportable - " \
  248. "use gnulib module roundl for portability"), \
  249. roundl (x))
  250. #endif
  251. #if @GNULIB_MATHL@ || !@HAVE_DECL_SINL@
  252. extern long double sinl (long double x);
  253. #endif
  254. #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
  255. # undef sinl
  256. # define sinl(x) \
  257. (GL_LINK_WARNING ("sinl is unportable - " \
  258. "use gnulib module mathl for portability"), \
  259. sinl (x))
  260. #endif
  261. #if @GNULIB_MATHL@ || !@HAVE_DECL_SQRTL@
  262. extern long double sqrtl (long double x);
  263. #endif
  264. #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
  265. # undef sqrtl
  266. # define sqrtl(x) \
  267. (GL_LINK_WARNING ("sqrtl is unportable - " \
  268. "use gnulib module mathl for portability"), \
  269. sqrtl (x))
  270. #endif
  271. #if @GNULIB_MATHL@ || !@HAVE_DECL_TANL@
  272. extern long double tanl (long double x);
  273. #endif
  274. #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
  275. # undef tanl
  276. # define tanl(x) \
  277. (GL_LINK_WARNING ("tanl is unportable - " \
  278. "use gnulib module mathl for portability"), \
  279. tanl (x))
  280. #endif
  281. #if @GNULIB_TRUNCF@
  282. # if !@HAVE_DECL_TRUNCF@
  283. # define truncf rpl_truncf
  284. extern float truncf (float x);
  285. # endif
  286. #elif defined GNULIB_POSIXCHECK
  287. # undef truncf
  288. # define truncf(x) \
  289. (GL_LINK_WARNING ("truncf is unportable - " \
  290. "use gnulib module truncf for portability"), \
  291. truncf (x))
  292. #endif
  293. #if @GNULIB_TRUNC@
  294. # if !@HAVE_DECL_TRUNC@
  295. # define trunc rpl_trunc
  296. extern double trunc (double x);
  297. # endif
  298. #elif defined GNULIB_POSIXCHECK
  299. # undef trunc
  300. # define trunc(x) \
  301. (GL_LINK_WARNING ("trunc is unportable - " \
  302. "use gnulib module trunc for portability"), \
  303. trunc (x))
  304. #endif
  305. #if @GNULIB_TRUNCL@
  306. # if @REPLACE_TRUNCL@
  307. # undef truncl
  308. # define truncl rpl_truncl
  309. extern long double truncl (long double x);
  310. # endif
  311. #elif defined GNULIB_POSIXCHECK
  312. # undef truncl
  313. # define truncl(x) \
  314. (GL_LINK_WARNING ("truncl is unportable - " \
  315. "use gnulib module truncl for portability"), \
  316. truncl (x))
  317. #endif
  318. #if @GNULIB_ISFINITE@
  319. # if @REPLACE_ISFINITE@
  320. extern int gl_isfinitef (float x);
  321. extern int gl_isfinited (double x);
  322. extern int gl_isfinitel (long double x);
  323. # undef isfinite
  324. # define isfinite(x) \
  325. (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
  326. sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
  327. gl_isfinitef (x))
  328. # endif
  329. #elif defined GNULIB_POSIXCHECK
  330. /* How to override a macro? */
  331. #endif
  332. #if @GNULIB_ISINF@
  333. # if @REPLACE_ISINF@
  334. extern int gl_isinff (float x);
  335. extern int gl_isinfd (double x);
  336. extern int gl_isinfl (long double x);
  337. # undef isinf
  338. # define isinf(x) \
  339. (sizeof (x) == sizeof (long double) ? gl_isinfl (x) : \
  340. sizeof (x) == sizeof (double) ? gl_isinfd (x) : \
  341. gl_isinff (x))
  342. # endif
  343. #elif defined GNULIB_POSIXCHECK
  344. /* How to override a macro? */
  345. #endif
  346. #if @GNULIB_ISNANF@
  347. /* Test for NaN for 'float' numbers. */
  348. # if @HAVE_ISNANF@
  349. /* The original <math.h> included above provides a declaration of isnan macro
  350. or (older) isnanf function. */
  351. # include <math.h>
  352. # if __GNUC__ >= 4
  353. /* GCC 4.0 and newer provides three built-ins for isnan. */
  354. # undef isnanf
  355. # define isnanf(x) __builtin_isnanf ((float)(x))
  356. # elif defined isnan
  357. # undef isnanf
  358. # define isnanf(x) isnan ((float)(x))
  359. # endif
  360. # else
  361. /* Test whether X is a NaN. */
  362. # undef isnanf
  363. # define isnanf rpl_isnanf
  364. extern int isnanf (float x);
  365. # endif
  366. #endif
  367. #if @GNULIB_ISNAND@
  368. /* Test for NaN for 'double' numbers.
  369. This function is a gnulib extension, unlike isnan() which applied only
  370. to 'double' numbers earlier but now is a type-generic macro. */
  371. # if @HAVE_ISNAND@
  372. /* The original <math.h> included above provides a declaration of isnan macro. */
  373. # include <math.h>
  374. # if __GNUC__ >= 4
  375. /* GCC 4.0 and newer provides three built-ins for isnan. */
  376. # undef isnand
  377. # define isnand(x) __builtin_isnan ((double)(x))
  378. # else
  379. # undef isnand
  380. # define isnand(x) isnan ((double)(x))
  381. # endif
  382. # else
  383. /* Test whether X is a NaN. */
  384. # undef isnand
  385. # define isnand rpl_isnand
  386. extern int isnand (double x);
  387. # endif
  388. #endif
  389. #if @GNULIB_ISNANL@
  390. /* Test for NaN for 'long double' numbers. */
  391. # if @HAVE_ISNANL@
  392. /* The original <math.h> included above provides a declaration of isnan macro or (older) isnanl function. */
  393. # include <math.h>
  394. # if __GNUC__ >= 4
  395. /* GCC 4.0 and newer provides three built-ins for isnan. */
  396. # undef isnanl
  397. # define isnanl(x) __builtin_isnanl ((long double)(x))
  398. # elif defined isnan
  399. # undef isnanl
  400. # define isnanl(x) isnan ((long double)(x))
  401. # endif
  402. # else
  403. /* Test whether X is a NaN. */
  404. # undef isnanl
  405. # define isnanl rpl_isnanl
  406. extern int isnanl (long double x);
  407. # endif
  408. #endif
  409. /* This must come *after* the snippets for GNULIB_ISNANF and GNULIB_ISNANL! */
  410. #if @GNULIB_ISNAN@
  411. # if @REPLACE_ISNAN@
  412. /* We can't just use the isnanf macro (e.g.) as exposed by
  413. isnanf.h (e.g.) here, because those may end up being macros
  414. that recursively expand back to isnan. So use the gnulib
  415. replacements for them directly. */
  416. # if @HAVE_ISNANF@ && __GNUC__ >= 4
  417. # define gl_isnan_f(x) __builtin_isnan ((float)(x))
  418. # else
  419. extern int rpl_isnanf (float x);
  420. # define gl_isnan_f(x) rpl_isnanf (x)
  421. # endif
  422. # if @HAVE_ISNAND@ && __GNUC__ >= 4
  423. # define gl_isnan_d(x) __builtin_isnan ((double)(x))
  424. # else
  425. extern int rpl_isnand (double x);
  426. # define gl_isnan_d(x) rpl_isnand (x)
  427. # endif
  428. # if @HAVE_ISNANL@ && __GNUC__ >= 4
  429. # define gl_isnan_l(x) __builtin_isnan ((long double)(x))
  430. # else
  431. extern int rpl_isnanl (long double x);
  432. # define gl_isnan_l(x) rpl_isnanl (x)
  433. # endif
  434. # undef isnan
  435. # define isnan(x) \
  436. (sizeof (x) == sizeof (long double) ? gl_isnan_l (x) : \
  437. sizeof (x) == sizeof (double) ? gl_isnan_d (x) : \
  438. gl_isnan_f (x))
  439. # endif
  440. #elif defined GNULIB_POSIXCHECK
  441. /* How to override a macro? */
  442. #endif
  443. #if @GNULIB_SIGNBIT@
  444. # if @REPLACE_SIGNBIT_USING_GCC@
  445. # undef signbit
  446. /* GCC 4.0 and newer provides three built-ins for signbit. */
  447. # define signbit(x) \
  448. (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
  449. sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
  450. __builtin_signbitf (x))
  451. # endif
  452. # if @REPLACE_SIGNBIT@
  453. # undef signbit
  454. extern int gl_signbitf (float arg);
  455. extern int gl_signbitd (double arg);
  456. extern int gl_signbitl (long double arg);
  457. # if __GNUC__ >= 2 && !__STRICT_ANSI__
  458. # if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT && !defined gl_signbitf
  459. # define gl_signbitf_OPTIMIZED_MACRO
  460. # define gl_signbitf(arg) \
  461. ({ union { float _value; \
  462. unsigned int _word[(sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
  463. } _m; \
  464. _m._value = (arg); \
  465. (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1; \
  466. })
  467. # endif
  468. # if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT && !defined gl_signbitd
  469. # define gl_signbitd_OPTIMIZED_MACRO
  470. # define gl_signbitd(arg) \
  471. ({ union { double _value; \
  472. unsigned int _word[(sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
  473. } _m; \
  474. _m._value = (arg); \
  475. (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1; \
  476. })
  477. # endif
  478. # if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT && !defined gl_signbitl
  479. # define gl_signbitl_OPTIMIZED_MACRO
  480. # define gl_signbitl(arg) \
  481. ({ union { long double _value; \
  482. unsigned int _word[(sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
  483. } _m; \
  484. _m._value = (arg); \
  485. (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1; \
  486. })
  487. # endif
  488. # endif
  489. # define signbit(x) \
  490. (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
  491. sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
  492. gl_signbitf (x))
  493. # endif
  494. #elif defined GNULIB_POSIXCHECK
  495. /* How to override a macro? */
  496. #endif
  497. #ifdef __cplusplus
  498. }
  499. #endif
  500. #endif /* _GL_MATH_H */
  501. #endif /* _GL_MATH_H */