math.h 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  1. /* Declarations for math functions.
  2. Copyright (C) 1991-2021 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /*
  16. * ISO C99 Standard: 7.12 Mathematics <math.h>
  17. */
  18. #ifndef _MATH_H
  19. #define _MATH_H 1
  20. #define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
  21. #include <bits/libc-header-start.h>
  22. #if defined log && defined __GNUC__
  23. # warning A macro called log was already defined when <math.h> was included.
  24. # warning This will cause compilation problems.
  25. #endif
  26. __BEGIN_DECLS
  27. /* Get definitions of __intmax_t and __uintmax_t. */
  28. #include <bits/types.h>
  29. /* Get machine-dependent vector math functions declarations. */
  30. #include <bits/math-vector.h>
  31. /* Gather machine dependent type support. */
  32. #include <bits/floatn.h>
  33. /* Value returned on overflow. With IEEE 754 floating point, this is
  34. +Infinity, otherwise the largest representable positive value. */
  35. #if __GNUC_PREREQ (3, 3)
  36. # define HUGE_VAL (__builtin_huge_val ())
  37. #else
  38. /* This may provoke compiler warnings, and may not be rounded to
  39. +Infinity in all IEEE 754 rounding modes, but is the best that can
  40. be done in ISO C while remaining a constant expression. 10,000 is
  41. greater than the maximum (decimal) exponent for all supported
  42. floating-point formats and widths. */
  43. # define HUGE_VAL 1e10000
  44. #endif
  45. #ifdef __USE_ISOC99
  46. # if __GNUC_PREREQ (3, 3)
  47. # define HUGE_VALF (__builtin_huge_valf ())
  48. # define HUGE_VALL (__builtin_huge_vall ())
  49. # else
  50. # define HUGE_VALF 1e10000f
  51. # define HUGE_VALL 1e10000L
  52. # endif
  53. #endif
  54. #if __HAVE_FLOAT16 && __GLIBC_USE (IEC_60559_TYPES_EXT)
  55. # define HUGE_VAL_F16 (__builtin_huge_valf16 ())
  56. #endif
  57. #if __HAVE_FLOAT32 && __GLIBC_USE (IEC_60559_TYPES_EXT)
  58. # define HUGE_VAL_F32 (__builtin_huge_valf32 ())
  59. #endif
  60. #if __HAVE_FLOAT64 && __GLIBC_USE (IEC_60559_TYPES_EXT)
  61. # define HUGE_VAL_F64 (__builtin_huge_valf64 ())
  62. #endif
  63. #if __HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
  64. # define HUGE_VAL_F128 (__builtin_huge_valf128 ())
  65. #endif
  66. #if __HAVE_FLOAT32X && __GLIBC_USE (IEC_60559_TYPES_EXT)
  67. # define HUGE_VAL_F32X (__builtin_huge_valf32x ())
  68. #endif
  69. #if __HAVE_FLOAT64X && __GLIBC_USE (IEC_60559_TYPES_EXT)
  70. # define HUGE_VAL_F64X (__builtin_huge_valf64x ())
  71. #endif
  72. #if __HAVE_FLOAT128X && __GLIBC_USE (IEC_60559_TYPES_EXT)
  73. # define HUGE_VAL_F128X (__builtin_huge_valf128x ())
  74. #endif
  75. #ifdef __USE_ISOC99
  76. /* IEEE positive infinity. */
  77. # if __GNUC_PREREQ (3, 3)
  78. # define INFINITY (__builtin_inff ())
  79. # else
  80. # define INFINITY HUGE_VALF
  81. # endif
  82. /* IEEE Not A Number. */
  83. # if __GNUC_PREREQ (3, 3)
  84. # define NAN (__builtin_nanf (""))
  85. # else
  86. /* This will raise an "invalid" exception outside static initializers,
  87. but is the best that can be done in ISO C while remaining a
  88. constant expression. */
  89. # define NAN (0.0f / 0.0f)
  90. # endif
  91. #endif /* __USE_ISOC99 */
  92. #if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
  93. /* Signaling NaN macros, if supported. */
  94. # if __GNUC_PREREQ (3, 3)
  95. # define SNANF (__builtin_nansf (""))
  96. # define SNAN (__builtin_nans (""))
  97. # define SNANL (__builtin_nansl (""))
  98. # endif
  99. #endif
  100. #if __HAVE_FLOAT16 && __GLIBC_USE (IEC_60559_TYPES_EXT)
  101. # define SNANF16 (__builtin_nansf16 (""))
  102. #endif
  103. #if __HAVE_FLOAT32 && __GLIBC_USE (IEC_60559_TYPES_EXT)
  104. # define SNANF32 (__builtin_nansf32 (""))
  105. #endif
  106. #if __HAVE_FLOAT64 && __GLIBC_USE (IEC_60559_TYPES_EXT)
  107. # define SNANF64 (__builtin_nansf64 (""))
  108. #endif
  109. #if __HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
  110. # define SNANF128 (__builtin_nansf128 (""))
  111. #endif
  112. #if __HAVE_FLOAT32X && __GLIBC_USE (IEC_60559_TYPES_EXT)
  113. # define SNANF32X (__builtin_nansf32x (""))
  114. #endif
  115. #if __HAVE_FLOAT64X && __GLIBC_USE (IEC_60559_TYPES_EXT)
  116. # define SNANF64X (__builtin_nansf64x (""))
  117. #endif
  118. #if __HAVE_FLOAT128X && __GLIBC_USE (IEC_60559_TYPES_EXT)
  119. # define SNANF128X (__builtin_nansf128x (""))
  120. #endif
  121. /* Get __GLIBC_FLT_EVAL_METHOD. */
  122. #include <bits/flt-eval-method.h>
  123. #ifdef __USE_ISOC99
  124. /* Define the following typedefs.
  125. float_t floating-point type at least as wide as `float' used
  126. to evaluate `float' expressions
  127. double_t floating-point type at least as wide as `double' used
  128. to evaluate `double' expressions
  129. */
  130. # if __GLIBC_FLT_EVAL_METHOD == 0 || __GLIBC_FLT_EVAL_METHOD == 16
  131. typedef float float_t;
  132. typedef double double_t;
  133. # elif __GLIBC_FLT_EVAL_METHOD == 1
  134. typedef double float_t;
  135. typedef double double_t;
  136. # elif __GLIBC_FLT_EVAL_METHOD == 2
  137. typedef long double float_t;
  138. typedef long double double_t;
  139. # elif __GLIBC_FLT_EVAL_METHOD == 32
  140. typedef _Float32 float_t;
  141. typedef double double_t;
  142. # elif __GLIBC_FLT_EVAL_METHOD == 33
  143. typedef _Float32x float_t;
  144. typedef _Float32x double_t;
  145. # elif __GLIBC_FLT_EVAL_METHOD == 64
  146. typedef _Float64 float_t;
  147. typedef _Float64 double_t;
  148. # elif __GLIBC_FLT_EVAL_METHOD == 65
  149. typedef _Float64x float_t;
  150. typedef _Float64x double_t;
  151. # elif __GLIBC_FLT_EVAL_METHOD == 128
  152. typedef _Float128 float_t;
  153. typedef _Float128 double_t;
  154. # elif __GLIBC_FLT_EVAL_METHOD == 129
  155. typedef _Float128x float_t;
  156. typedef _Float128x double_t;
  157. # else
  158. # error "Unknown __GLIBC_FLT_EVAL_METHOD"
  159. # endif
  160. #endif
  161. /* Define macros for the return values of ilogb and llogb, based on
  162. __FP_LOGB0_IS_MIN and __FP_LOGBNAN_IS_MIN.
  163. FP_ILOGB0 Expands to a value returned by `ilogb (0.0)'.
  164. FP_ILOGBNAN Expands to a value returned by `ilogb (NAN)'.
  165. FP_LLOGB0 Expands to a value returned by `llogb (0.0)'.
  166. FP_LLOGBNAN Expands to a value returned by `llogb (NAN)'.
  167. */
  168. #include <bits/fp-logb.h>
  169. #ifdef __USE_ISOC99
  170. # if __FP_LOGB0_IS_MIN
  171. # define FP_ILOGB0 (-2147483647 - 1)
  172. # else
  173. # define FP_ILOGB0 (-2147483647)
  174. # endif
  175. # if __FP_LOGBNAN_IS_MIN
  176. # define FP_ILOGBNAN (-2147483647 - 1)
  177. # else
  178. # define FP_ILOGBNAN 2147483647
  179. # endif
  180. #endif
  181. #if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
  182. # if __WORDSIZE == 32
  183. # define __FP_LONG_MAX 0x7fffffffL
  184. # else
  185. # define __FP_LONG_MAX 0x7fffffffffffffffL
  186. # endif
  187. # if __FP_LOGB0_IS_MIN
  188. # define FP_LLOGB0 (-__FP_LONG_MAX - 1)
  189. # else
  190. # define FP_LLOGB0 (-__FP_LONG_MAX)
  191. # endif
  192. # if __FP_LOGBNAN_IS_MIN
  193. # define FP_LLOGBNAN (-__FP_LONG_MAX - 1)
  194. # else
  195. # define FP_LLOGBNAN __FP_LONG_MAX
  196. # endif
  197. #endif
  198. /* Get the architecture specific values describing the floating-point
  199. evaluation. The following symbols will get defined:
  200. FP_FAST_FMA
  201. FP_FAST_FMAF
  202. FP_FAST_FMAL
  203. If defined it indicates that the `fma' function
  204. generally executes about as fast as a multiply and an add.
  205. This macro is defined only iff the `fma' function is
  206. implemented directly with a hardware multiply-add instructions.
  207. */
  208. #include <bits/fp-fast.h>
  209. #if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
  210. /* Rounding direction macros for fromfp functions. */
  211. enum
  212. {
  213. FP_INT_UPWARD =
  214. # define FP_INT_UPWARD 0
  215. FP_INT_UPWARD,
  216. FP_INT_DOWNWARD =
  217. # define FP_INT_DOWNWARD 1
  218. FP_INT_DOWNWARD,
  219. FP_INT_TOWARDZERO =
  220. # define FP_INT_TOWARDZERO 2
  221. FP_INT_TOWARDZERO,
  222. FP_INT_TONEARESTFROMZERO =
  223. # define FP_INT_TONEARESTFROMZERO 3
  224. FP_INT_TONEARESTFROMZERO,
  225. FP_INT_TONEAREST =
  226. # define FP_INT_TONEAREST 4
  227. FP_INT_TONEAREST,
  228. };
  229. #endif
  230. /* The file <bits/mathcalls.h> contains the prototypes for all the
  231. actual math functions. These macros are used for those prototypes,
  232. so we can easily declare each function as both `name' and `__name',
  233. and can declare the float versions `namef' and `__namef'. */
  234. #define __SIMD_DECL(function) __CONCAT (__DECL_SIMD_, function)
  235. #define __MATHCALL_VEC(function, suffix, args) \
  236. __SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
  237. __MATHCALL (function, suffix, args)
  238. #define __MATHDECL_VEC(type, function,suffix, args) \
  239. __SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
  240. __MATHDECL(type, function,suffix, args)
  241. #define __MATHCALL(function,suffix, args) \
  242. __MATHDECL (_Mdouble_,function,suffix, args)
  243. #define __MATHDECL(type, function,suffix, args) \
  244. __MATHDECL_1(type, function,suffix, args); \
  245. __MATHDECL_1(type, __CONCAT(__,function),suffix, args)
  246. #define __MATHCALLX(function,suffix, args, attrib) \
  247. __MATHDECLX (_Mdouble_,function,suffix, args, attrib)
  248. #define __MATHDECLX(type, function,suffix, args, attrib) \
  249. __MATHDECL_1(type, function,suffix, args) __attribute__ (attrib); \
  250. __MATHDECL_1(type, __CONCAT(__,function),suffix, args) __attribute__ (attrib)
  251. #define __MATHDECL_1_IMPL(type, function, suffix, args) \
  252. extern type __MATH_PRECNAME(function,suffix) args __THROW
  253. #define __MATHDECL_1(type, function, suffix, args) \
  254. __MATHDECL_1_IMPL(type, function, suffix, args)
  255. /* Ignore the alias by default. The alias is only useful with
  256. redirections. */
  257. #define __MATHDECL_ALIAS(type, function, suffix, args, alias) \
  258. __MATHDECL_1(type, function, suffix, args)
  259. #define __MATHREDIR(type, function, suffix, args, to) \
  260. extern type __REDIRECT_NTH (__MATH_PRECNAME (function, suffix), args, to)
  261. #define _Mdouble_ double
  262. #define __MATH_PRECNAME(name,r) __CONCAT(name,r)
  263. #define __MATH_DECLARING_DOUBLE 1
  264. #define __MATH_DECLARING_FLOATN 0
  265. #include <bits/mathcalls-helper-functions.h>
  266. #include <bits/mathcalls.h>
  267. #undef _Mdouble_
  268. #undef __MATH_PRECNAME
  269. #undef __MATH_DECLARING_DOUBLE
  270. #undef __MATH_DECLARING_FLOATN
  271. #ifdef __USE_ISOC99
  272. /* Include the file of declarations again, this time using `float'
  273. instead of `double' and appending f to each function name. */
  274. # define _Mdouble_ float
  275. # define __MATH_PRECNAME(name,r) name##f##r
  276. # define __MATH_DECLARING_DOUBLE 0
  277. # define __MATH_DECLARING_FLOATN 0
  278. # include <bits/mathcalls-helper-functions.h>
  279. # include <bits/mathcalls.h>
  280. # undef _Mdouble_
  281. # undef __MATH_PRECNAME
  282. # undef __MATH_DECLARING_DOUBLE
  283. # undef __MATH_DECLARING_FLOATN
  284. # if !(defined __NO_LONG_DOUBLE_MATH && defined _LIBC) \
  285. || defined __LDBL_COMPAT \
  286. || defined _LIBC_TEST
  287. # ifdef __LDBL_COMPAT
  288. # ifdef __USE_ISOC99
  289. extern float __nldbl_nexttowardf (float __x, long double __y)
  290. __THROW __attribute__ ((__const__));
  291. # ifdef __REDIRECT_NTH
  292. extern float __REDIRECT_NTH (nexttowardf, (float __x, long double __y),
  293. __nldbl_nexttowardf)
  294. __attribute__ ((__const__));
  295. extern double __REDIRECT_NTH (nexttoward, (double __x, long double __y),
  296. nextafter) __attribute__ ((__const__));
  297. extern long double __REDIRECT_NTH (nexttowardl,
  298. (long double __x, long double __y),
  299. nextafter) __attribute__ ((__const__));
  300. # endif
  301. # endif
  302. # undef __MATHDECL_1
  303. # define __MATHDECL_1(type, function,suffix, args) \
  304. __MATHREDIR(type, function, suffix, args, __CONCAT(function,suffix))
  305. # elif __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
  306. # ifdef __REDIRECT_NTH
  307. # ifdef __USE_ISOC99
  308. extern float __REDIRECT_NTH (nexttowardf, (float __x, long double __y),
  309. __nexttowardf_to_ieee128)
  310. __attribute__ ((__const__));
  311. extern double __REDIRECT_NTH (nexttoward, (double __x, long double __y),
  312. __nexttoward_to_ieee128)
  313. __attribute__ ((__const__));
  314. #define __dremieee128 __remainderieee128
  315. #define __gammaieee128 __lgammaieee128
  316. # endif
  317. # endif
  318. # undef __MATHDECL_1
  319. # undef __MATHDECL_ALIAS
  320. # define __REDIRTO(function, suffix) \
  321. __ ## function ## ieee128 ## suffix
  322. # define __REDIRTO_ALT(function, suffix) \
  323. __ ## function ## f128 ## suffix
  324. # define __MATHDECL_1(type, function, suffix, args) \
  325. __MATHREDIR (type, function, suffix, args, __REDIRTO (function, suffix))
  326. # define __MATHDECL_ALIAS(type, function, suffix, args, alias) \
  327. __MATHREDIR (type, function, suffix, args, __REDIRTO_ALT (alias, suffix))
  328. # endif
  329. /* Include the file of declarations again, this time using `long double'
  330. instead of `double' and appending l to each function name. */
  331. # define _Mdouble_ long double
  332. # define __MATH_PRECNAME(name,r) name##l##r
  333. # define __MATH_DECLARING_DOUBLE 0
  334. # define __MATH_DECLARING_FLOATN 0
  335. # define __MATH_DECLARE_LDOUBLE 1
  336. # include <bits/mathcalls-helper-functions.h>
  337. # include <bits/mathcalls.h>
  338. # undef _Mdouble_
  339. # undef __MATH_PRECNAME
  340. # undef __MATH_DECLARING_DOUBLE
  341. # undef __MATH_DECLARING_FLOATN
  342. # if defined __LDBL_COMPAT \
  343. || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
  344. # undef __REDIRTO
  345. # undef __REDIRTO_ALT
  346. # undef __MATHDECL_1
  347. # undef __MATHDECL_ALIAS
  348. # define __MATHDECL_1(type, function, suffix, args) \
  349. __MATHDECL_1_IMPL(type, function, suffix, args)
  350. # define __MATHDECL_ALIAS(type, function, suffix, args, alias) \
  351. __MATHDECL_1(type, function, suffix, args)
  352. # endif
  353. # endif /* !(__NO_LONG_DOUBLE_MATH && _LIBC) || __LDBL_COMPAT */
  354. #endif /* Use ISO C99. */
  355. /* Include the file of declarations for _FloatN and _FloatNx
  356. types. */
  357. #if __HAVE_DISTINCT_FLOAT16 || (__HAVE_FLOAT16 && !defined _LIBC)
  358. # define _Mdouble_ _Float16
  359. # define __MATH_PRECNAME(name,r) name##f16##r
  360. # define __MATH_DECLARING_DOUBLE 0
  361. # define __MATH_DECLARING_FLOATN 1
  362. # if __HAVE_DISTINCT_FLOAT16
  363. # include <bits/mathcalls-helper-functions.h>
  364. # endif
  365. # if __GLIBC_USE (IEC_60559_TYPES_EXT)
  366. # include <bits/mathcalls.h>
  367. # endif
  368. # undef _Mdouble_
  369. # undef __MATH_PRECNAME
  370. # undef __MATH_DECLARING_DOUBLE
  371. # undef __MATH_DECLARING_FLOATN
  372. #endif /* __HAVE_DISTINCT_FLOAT16 || (__HAVE_FLOAT16 && !_LIBC). */
  373. #if __HAVE_DISTINCT_FLOAT32 || (__HAVE_FLOAT32 && !defined _LIBC)
  374. # define _Mdouble_ _Float32
  375. # define __MATH_PRECNAME(name,r) name##f32##r
  376. # define __MATH_DECLARING_DOUBLE 0
  377. # define __MATH_DECLARING_FLOATN 1
  378. # if __HAVE_DISTINCT_FLOAT32
  379. # include <bits/mathcalls-helper-functions.h>
  380. # endif
  381. # if __GLIBC_USE (IEC_60559_TYPES_EXT)
  382. # include <bits/mathcalls.h>
  383. # endif
  384. # undef _Mdouble_
  385. # undef __MATH_PRECNAME
  386. # undef __MATH_DECLARING_DOUBLE
  387. # undef __MATH_DECLARING_FLOATN
  388. #endif /* __HAVE_DISTINCT_FLOAT32 || (__HAVE_FLOAT32 && !_LIBC). */
  389. #if __HAVE_DISTINCT_FLOAT64 || (__HAVE_FLOAT64 && !defined _LIBC)
  390. # define _Mdouble_ _Float64
  391. # define __MATH_PRECNAME(name,r) name##f64##r
  392. # define __MATH_DECLARING_DOUBLE 0
  393. # define __MATH_DECLARING_FLOATN 1
  394. # if __HAVE_DISTINCT_FLOAT64
  395. # include <bits/mathcalls-helper-functions.h>
  396. # endif
  397. # if __GLIBC_USE (IEC_60559_TYPES_EXT)
  398. # include <bits/mathcalls.h>
  399. # endif
  400. # undef _Mdouble_
  401. # undef __MATH_PRECNAME
  402. # undef __MATH_DECLARING_DOUBLE
  403. # undef __MATH_DECLARING_FLOATN
  404. #endif /* __HAVE_DISTINCT_FLOAT64 || (__HAVE_FLOAT64 && !_LIBC). */
  405. #if __HAVE_DISTINCT_FLOAT128 || (__HAVE_FLOAT128 && !defined _LIBC)
  406. # define _Mdouble_ _Float128
  407. # define __MATH_PRECNAME(name,r) name##f128##r
  408. # define __MATH_DECLARING_DOUBLE 0
  409. # define __MATH_DECLARING_FLOATN 1
  410. # if __HAVE_DISTINCT_FLOAT128
  411. # include <bits/mathcalls-helper-functions.h>
  412. # endif
  413. # if __GLIBC_USE (IEC_60559_TYPES_EXT)
  414. # include <bits/mathcalls.h>
  415. # endif
  416. # undef _Mdouble_
  417. # undef __MATH_PRECNAME
  418. # undef __MATH_DECLARING_DOUBLE
  419. # undef __MATH_DECLARING_FLOATN
  420. #endif /* __HAVE_DISTINCT_FLOAT128 || (__HAVE_FLOAT128 && !_LIBC). */
  421. #if __HAVE_DISTINCT_FLOAT32X || (__HAVE_FLOAT32X && !defined _LIBC)
  422. # define _Mdouble_ _Float32x
  423. # define __MATH_PRECNAME(name,r) name##f32x##r
  424. # define __MATH_DECLARING_DOUBLE 0
  425. # define __MATH_DECLARING_FLOATN 1
  426. # if __HAVE_DISTINCT_FLOAT32X
  427. # include <bits/mathcalls-helper-functions.h>
  428. # endif
  429. # if __GLIBC_USE (IEC_60559_TYPES_EXT)
  430. # include <bits/mathcalls.h>
  431. # endif
  432. # undef _Mdouble_
  433. # undef __MATH_PRECNAME
  434. # undef __MATH_DECLARING_DOUBLE
  435. # undef __MATH_DECLARING_FLOATN
  436. #endif /* __HAVE_DISTINCT_FLOAT32X || (__HAVE_FLOAT32X && !_LIBC). */
  437. #if __HAVE_DISTINCT_FLOAT64X || (__HAVE_FLOAT64X && !defined _LIBC)
  438. # define _Mdouble_ _Float64x
  439. # define __MATH_PRECNAME(name,r) name##f64x##r
  440. # define __MATH_DECLARING_DOUBLE 0
  441. # define __MATH_DECLARING_FLOATN 1
  442. # if __HAVE_DISTINCT_FLOAT64X
  443. # include <bits/mathcalls-helper-functions.h>
  444. # endif
  445. # if __GLIBC_USE (IEC_60559_TYPES_EXT)
  446. # include <bits/mathcalls.h>
  447. # endif
  448. # undef _Mdouble_
  449. # undef __MATH_PRECNAME
  450. # undef __MATH_DECLARING_DOUBLE
  451. # undef __MATH_DECLARING_FLOATN
  452. #endif /* __HAVE_DISTINCT_FLOAT64X || (__HAVE_FLOAT64X && !_LIBC). */
  453. #if __HAVE_DISTINCT_FLOAT128X || (__HAVE_FLOAT128X && !defined _LIBC)
  454. # define _Mdouble_ _Float128x
  455. # define __MATH_PRECNAME(name,r) name##f128x##r
  456. # define __MATH_DECLARING_DOUBLE 0
  457. # define __MATH_DECLARING_FLOATN 1
  458. # if __HAVE_DISTINCT_FLOAT128X
  459. # include <bits/mathcalls-helper-functions.h>
  460. # endif
  461. # if __GLIBC_USE (IEC_60559_TYPES_EXT)
  462. # include <bits/mathcalls.h>
  463. # endif
  464. # undef _Mdouble_
  465. # undef __MATH_PRECNAME
  466. # undef __MATH_DECLARING_DOUBLE
  467. # undef __MATH_DECLARING_FLOATN
  468. #endif /* __HAVE_DISTINCT_FLOAT128X || (__HAVE_FLOAT128X && !_LIBC). */
  469. #undef __MATHDECL_1_IMPL
  470. #undef __MATHDECL_1
  471. #undef __MATHDECL_ALIAS
  472. #undef __MATHDECL
  473. #undef __MATHCALL
  474. /* Declare functions returning a narrower type. */
  475. #define __MATHCALL_NARROW_ARGS_1 (_Marg_ __x)
  476. #define __MATHCALL_NARROW_ARGS_2 (_Marg_ __x, _Marg_ __y)
  477. #define __MATHCALL_NARROW_ARGS_3 (_Marg_ __x, _Marg_ __y, _Marg_ __z)
  478. #define __MATHCALL_NARROW_NORMAL(func, nargs) \
  479. extern _Mret_ func __MATHCALL_NARROW_ARGS_ ## nargs __THROW
  480. #define __MATHCALL_NARROW_REDIR(func, redir, nargs) \
  481. extern _Mret_ __REDIRECT_NTH (func, __MATHCALL_NARROW_ARGS_ ## nargs, \
  482. redir)
  483. #define __MATHCALL_NARROW(func, redir, nargs) \
  484. __MATHCALL_NARROW_NORMAL (func, nargs)
  485. #if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
  486. # define _Mret_ float
  487. # define _Marg_ double
  488. # define __MATHCALL_NAME(name) f ## name
  489. # include <bits/mathcalls-narrow.h>
  490. # undef _Mret_
  491. # undef _Marg_
  492. # undef __MATHCALL_NAME
  493. # define _Mret_ float
  494. # define _Marg_ long double
  495. # define __MATHCALL_NAME(name) f ## name ## l
  496. # ifdef __LDBL_COMPAT
  497. # define __MATHCALL_REDIR_NAME(name) f ## name
  498. # undef __MATHCALL_NARROW
  499. # define __MATHCALL_NARROW(func, redir, nargs) \
  500. __MATHCALL_NARROW_REDIR (func, redir, nargs)
  501. # elif __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
  502. # define __MATHCALL_REDIR_NAME(name) __ ## f32 ## name ## ieee128
  503. # undef __MATHCALL_NARROW
  504. # define __MATHCALL_NARROW(func, redir, nargs) \
  505. __MATHCALL_NARROW_REDIR (func, redir, nargs)
  506. # endif
  507. # include <bits/mathcalls-narrow.h>
  508. # undef _Mret_
  509. # undef _Marg_
  510. # undef __MATHCALL_NAME
  511. # if defined __LDBL_COMPAT \
  512. || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
  513. # undef __MATHCALL_REDIR_NAME
  514. # undef __MATHCALL_NARROW
  515. # define __MATHCALL_NARROW(func, redir, nargs) \
  516. __MATHCALL_NARROW_NORMAL (func, nargs)
  517. # endif
  518. # define _Mret_ double
  519. # define _Marg_ long double
  520. # define __MATHCALL_NAME(name) d ## name ## l
  521. # ifdef __LDBL_COMPAT
  522. # define __MATHCALL_REDIR_NAME(name) __nldbl_d ## name ## l
  523. # undef __MATHCALL_NARROW
  524. # define __MATHCALL_NARROW(func, redir, nargs) \
  525. __MATHCALL_NARROW_REDIR (func, redir, nargs)
  526. # elif __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
  527. # define __MATHCALL_REDIR_NAME(name) __ ## f64 ## name ## ieee128
  528. # undef __MATHCALL_NARROW
  529. # define __MATHCALL_NARROW(func, redir, nargs) \
  530. __MATHCALL_NARROW_REDIR (func, redir, nargs)
  531. # endif
  532. # include <bits/mathcalls-narrow.h>
  533. # undef _Mret_
  534. # undef _Marg_
  535. # undef __MATHCALL_NAME
  536. # if defined __LDBL_COMPAT \
  537. || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
  538. # undef __MATHCALL_REDIR_NAME
  539. # undef __MATHCALL_NARROW
  540. # define __MATHCALL_NARROW(func, redir, nargs) \
  541. __MATHCALL_NARROW_NORMAL (func, nargs)
  542. # endif
  543. #endif
  544. #if __GLIBC_USE (IEC_60559_TYPES_EXT)
  545. # if __HAVE_FLOAT16 && __HAVE_FLOAT32
  546. # define _Mret_ _Float16
  547. # define _Marg_ _Float32
  548. # define __MATHCALL_NAME(name) f16 ## name ## f32
  549. # include <bits/mathcalls-narrow.h>
  550. # undef _Mret_
  551. # undef _Marg_
  552. # undef __MATHCALL_NAME
  553. # endif
  554. # if __HAVE_FLOAT16 && __HAVE_FLOAT32X
  555. # define _Mret_ _Float16
  556. # define _Marg_ _Float32x
  557. # define __MATHCALL_NAME(name) f16 ## name ## f32x
  558. # include <bits/mathcalls-narrow.h>
  559. # undef _Mret_
  560. # undef _Marg_
  561. # undef __MATHCALL_NAME
  562. # endif
  563. # if __HAVE_FLOAT16 && __HAVE_FLOAT64
  564. # define _Mret_ _Float16
  565. # define _Marg_ _Float64
  566. # define __MATHCALL_NAME(name) f16 ## name ## f64
  567. # include <bits/mathcalls-narrow.h>
  568. # undef _Mret_
  569. # undef _Marg_
  570. # undef __MATHCALL_NAME
  571. # endif
  572. # if __HAVE_FLOAT16 && __HAVE_FLOAT64X
  573. # define _Mret_ _Float16
  574. # define _Marg_ _Float64x
  575. # define __MATHCALL_NAME(name) f16 ## name ## f64x
  576. # include <bits/mathcalls-narrow.h>
  577. # undef _Mret_
  578. # undef _Marg_
  579. # undef __MATHCALL_NAME
  580. # endif
  581. # if __HAVE_FLOAT16 && __HAVE_FLOAT128
  582. # define _Mret_ _Float16
  583. # define _Marg_ _Float128
  584. # define __MATHCALL_NAME(name) f16 ## name ## f128
  585. # include <bits/mathcalls-narrow.h>
  586. # undef _Mret_
  587. # undef _Marg_
  588. # undef __MATHCALL_NAME
  589. # endif
  590. # if __HAVE_FLOAT16 && __HAVE_FLOAT128X
  591. # define _Mret_ _Float16
  592. # define _Marg_ _Float128x
  593. # define __MATHCALL_NAME(name) f16 ## name ## f128x
  594. # include <bits/mathcalls-narrow.h>
  595. # undef _Mret_
  596. # undef _Marg_
  597. # undef __MATHCALL_NAME
  598. # endif
  599. # if __HAVE_FLOAT32 && __HAVE_FLOAT32X
  600. # define _Mret_ _Float32
  601. # define _Marg_ _Float32x
  602. # define __MATHCALL_NAME(name) f32 ## name ## f32x
  603. # include <bits/mathcalls-narrow.h>
  604. # undef _Mret_
  605. # undef _Marg_
  606. # undef __MATHCALL_NAME
  607. # endif
  608. # if __HAVE_FLOAT32 && __HAVE_FLOAT64
  609. # define _Mret_ _Float32
  610. # define _Marg_ _Float64
  611. # define __MATHCALL_NAME(name) f32 ## name ## f64
  612. # include <bits/mathcalls-narrow.h>
  613. # undef _Mret_
  614. # undef _Marg_
  615. # undef __MATHCALL_NAME
  616. # endif
  617. # if __HAVE_FLOAT32 && __HAVE_FLOAT64X
  618. # define _Mret_ _Float32
  619. # define _Marg_ _Float64x
  620. # define __MATHCALL_NAME(name) f32 ## name ## f64x
  621. # include <bits/mathcalls-narrow.h>
  622. # undef _Mret_
  623. # undef _Marg_
  624. # undef __MATHCALL_NAME
  625. # endif
  626. # if __HAVE_FLOAT32 && __HAVE_FLOAT128
  627. # define _Mret_ _Float32
  628. # define _Marg_ _Float128
  629. # define __MATHCALL_NAME(name) f32 ## name ## f128
  630. # include <bits/mathcalls-narrow.h>
  631. # undef _Mret_
  632. # undef _Marg_
  633. # undef __MATHCALL_NAME
  634. # endif
  635. # if __HAVE_FLOAT32 && __HAVE_FLOAT128X
  636. # define _Mret_ _Float32
  637. # define _Marg_ _Float128x
  638. # define __MATHCALL_NAME(name) f32 ## name ## f128x
  639. # include <bits/mathcalls-narrow.h>
  640. # undef _Mret_
  641. # undef _Marg_
  642. # undef __MATHCALL_NAME
  643. # endif
  644. # if __HAVE_FLOAT32X && __HAVE_FLOAT64
  645. # define _Mret_ _Float32x
  646. # define _Marg_ _Float64
  647. # define __MATHCALL_NAME(name) f32x ## name ## f64
  648. # include <bits/mathcalls-narrow.h>
  649. # undef _Mret_
  650. # undef _Marg_
  651. # undef __MATHCALL_NAME
  652. # endif
  653. # if __HAVE_FLOAT32X && __HAVE_FLOAT64X
  654. # define _Mret_ _Float32x
  655. # define _Marg_ _Float64x
  656. # define __MATHCALL_NAME(name) f32x ## name ## f64x
  657. # include <bits/mathcalls-narrow.h>
  658. # undef _Mret_
  659. # undef _Marg_
  660. # undef __MATHCALL_NAME
  661. # endif
  662. # if __HAVE_FLOAT32X && __HAVE_FLOAT128
  663. # define _Mret_ _Float32x
  664. # define _Marg_ _Float128
  665. # define __MATHCALL_NAME(name) f32x ## name ## f128
  666. # include <bits/mathcalls-narrow.h>
  667. # undef _Mret_
  668. # undef _Marg_
  669. # undef __MATHCALL_NAME
  670. # endif
  671. # if __HAVE_FLOAT32X && __HAVE_FLOAT128X
  672. # define _Mret_ _Float32x
  673. # define _Marg_ _Float128x
  674. # define __MATHCALL_NAME(name) f32x ## name ## f128x
  675. # include <bits/mathcalls-narrow.h>
  676. # undef _Mret_
  677. # undef _Marg_
  678. # undef __MATHCALL_NAME
  679. # endif
  680. # if __HAVE_FLOAT64 && __HAVE_FLOAT64X
  681. # define _Mret_ _Float64
  682. # define _Marg_ _Float64x
  683. # define __MATHCALL_NAME(name) f64 ## name ## f64x
  684. # include <bits/mathcalls-narrow.h>
  685. # undef _Mret_
  686. # undef _Marg_
  687. # undef __MATHCALL_NAME
  688. # endif
  689. # if __HAVE_FLOAT64 && __HAVE_FLOAT128
  690. # define _Mret_ _Float64
  691. # define _Marg_ _Float128
  692. # define __MATHCALL_NAME(name) f64 ## name ## f128
  693. # include <bits/mathcalls-narrow.h>
  694. # undef _Mret_
  695. # undef _Marg_
  696. # undef __MATHCALL_NAME
  697. # endif
  698. # if __HAVE_FLOAT64 && __HAVE_FLOAT128X
  699. # define _Mret_ _Float64
  700. # define _Marg_ _Float128x
  701. # define __MATHCALL_NAME(name) f64 ## name ## f128x
  702. # include <bits/mathcalls-narrow.h>
  703. # undef _Mret_
  704. # undef _Marg_
  705. # undef __MATHCALL_NAME
  706. # endif
  707. # if __HAVE_FLOAT64X && __HAVE_FLOAT128
  708. # define _Mret_ _Float64x
  709. # define _Marg_ _Float128
  710. # define __MATHCALL_NAME(name) f64x ## name ## f128
  711. # include <bits/mathcalls-narrow.h>
  712. # undef _Mret_
  713. # undef _Marg_
  714. # undef __MATHCALL_NAME
  715. # endif
  716. # if __HAVE_FLOAT64X && __HAVE_FLOAT128X
  717. # define _Mret_ _Float64x
  718. # define _Marg_ _Float128x
  719. # define __MATHCALL_NAME(name) f64x ## name ## f128x
  720. # include <bits/mathcalls-narrow.h>
  721. # undef _Mret_
  722. # undef _Marg_
  723. # undef __MATHCALL_NAME
  724. # endif
  725. # if __HAVE_FLOAT128 && __HAVE_FLOAT128X
  726. # define _Mret_ _Float128
  727. # define _Marg_ _Float128x
  728. # define __MATHCALL_NAME(name) f128 ## name ## f128x
  729. # include <bits/mathcalls-narrow.h>
  730. # undef _Mret_
  731. # undef _Marg_
  732. # undef __MATHCALL_NAME
  733. # endif
  734. #endif
  735. #undef __MATHCALL_NARROW_ARGS_1
  736. #undef __MATHCALL_NARROW_ARGS_2
  737. #undef __MATHCALL_NARROW_ARGS_3
  738. #undef __MATHCALL_NARROW_NORMAL
  739. #undef __MATHCALL_NARROW_REDIR
  740. #undef __MATHCALL_NARROW
  741. #if defined __USE_MISC || defined __USE_XOPEN
  742. /* This variable is used by `gamma' and `lgamma'. */
  743. extern int signgam;
  744. #endif
  745. #if (__HAVE_DISTINCT_FLOAT16 \
  746. || __HAVE_DISTINCT_FLOAT32 \
  747. || __HAVE_DISTINCT_FLOAT64 \
  748. || __HAVE_DISTINCT_FLOAT32X \
  749. || __HAVE_DISTINCT_FLOAT64X \
  750. || __HAVE_DISTINCT_FLOAT128X)
  751. # error "Unsupported _FloatN or _FloatNx types for <math.h>."
  752. #endif
  753. /* Depending on the type of TG_ARG, call an appropriately suffixed
  754. version of FUNC with arguments (including parentheses) ARGS.
  755. Suffixed functions may not exist for long double if it has the same
  756. format as double, or for other types with the same format as float,
  757. double or long double. The behavior is undefined if the argument
  758. does not have a real floating type. The definition may use a
  759. conditional expression, so all suffixed versions of FUNC must
  760. return the same type (FUNC may include a cast if necessary rather
  761. than being a single identifier). */
  762. #ifdef __NO_LONG_DOUBLE_MATH
  763. # if __HAVE_DISTINCT_FLOAT128
  764. # error "Distinct _Float128 without distinct long double not supported."
  765. # endif
  766. # define __MATH_TG(TG_ARG, FUNC, ARGS) \
  767. (sizeof (TG_ARG) == sizeof (float) ? FUNC ## f ARGS : FUNC ARGS)
  768. #elif __HAVE_DISTINCT_FLOAT128
  769. # if __HAVE_GENERIC_SELECTION
  770. # if __HAVE_FLOATN_NOT_TYPEDEF && __HAVE_FLOAT32
  771. # define __MATH_TG_F32(FUNC, ARGS) _Float32: FUNC ## f ARGS,
  772. # else
  773. # define __MATH_TG_F32(FUNC, ARGS)
  774. # endif
  775. # if __HAVE_FLOATN_NOT_TYPEDEF && __HAVE_FLOAT64X
  776. # if __HAVE_FLOAT64X_LONG_DOUBLE
  777. # define __MATH_TG_F64X(FUNC, ARGS) _Float64x: FUNC ## l ARGS,
  778. # else
  779. # define __MATH_TG_F64X(FUNC, ARGS) _Float64x: FUNC ## f128 ARGS,
  780. # endif
  781. # else
  782. # define __MATH_TG_F64X(FUNC, ARGS)
  783. # endif
  784. # define __MATH_TG(TG_ARG, FUNC, ARGS) \
  785. _Generic ((TG_ARG), \
  786. float: FUNC ## f ARGS, \
  787. __MATH_TG_F32 (FUNC, ARGS) \
  788. default: FUNC ARGS, \
  789. long double: FUNC ## l ARGS, \
  790. __MATH_TG_F64X (FUNC, ARGS) \
  791. _Float128: FUNC ## f128 ARGS)
  792. # else
  793. # if __HAVE_FLOATN_NOT_TYPEDEF
  794. # error "Non-typedef _FloatN but no _Generic."
  795. # endif
  796. # define __MATH_TG(TG_ARG, FUNC, ARGS) \
  797. __builtin_choose_expr \
  798. (__builtin_types_compatible_p (__typeof (TG_ARG), float), \
  799. FUNC ## f ARGS, \
  800. __builtin_choose_expr \
  801. (__builtin_types_compatible_p (__typeof (TG_ARG), double), \
  802. FUNC ARGS, \
  803. __builtin_choose_expr \
  804. (__builtin_types_compatible_p (__typeof (TG_ARG), long double), \
  805. FUNC ## l ARGS, \
  806. FUNC ## f128 ARGS)))
  807. # endif
  808. #else
  809. # define __MATH_TG(TG_ARG, FUNC, ARGS) \
  810. (sizeof (TG_ARG) == sizeof (float) \
  811. ? FUNC ## f ARGS \
  812. : sizeof (TG_ARG) == sizeof (double) \
  813. ? FUNC ARGS \
  814. : FUNC ## l ARGS)
  815. #endif
  816. /* ISO C99 defines some generic macros which work on any data type. */
  817. #ifdef __USE_ISOC99
  818. /* All floating-point numbers can be put in one of these categories. */
  819. enum
  820. {
  821. FP_NAN =
  822. # define FP_NAN 0
  823. FP_NAN,
  824. FP_INFINITE =
  825. # define FP_INFINITE 1
  826. FP_INFINITE,
  827. FP_ZERO =
  828. # define FP_ZERO 2
  829. FP_ZERO,
  830. FP_SUBNORMAL =
  831. # define FP_SUBNORMAL 3
  832. FP_SUBNORMAL,
  833. FP_NORMAL =
  834. # define FP_NORMAL 4
  835. FP_NORMAL
  836. };
  837. /* GCC bug 66462 means we cannot use the math builtins with -fsignaling-nan,
  838. so disable builtins if this is enabled. When fixed in a newer GCC,
  839. the __SUPPORT_SNAN__ check may be skipped for those versions. */
  840. /* Return number of classification appropriate for X. */
  841. # if ((__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \
  842. || __glibc_clang_prereq (2,8)) \
  843. && (!defined __OPTIMIZE_SIZE__ || defined __cplusplus)
  844. /* The check for __cplusplus allows the use of the builtin, even
  845. when optimization for size is on. This is provided for
  846. libstdc++, only to let its configure test work when it is built
  847. with -Os. No further use of this definition of fpclassify is
  848. expected in C++ mode, since libstdc++ provides its own version
  849. of fpclassify in cmath (which undefines fpclassify). */
  850. # define fpclassify(x) __builtin_fpclassify (FP_NAN, FP_INFINITE, \
  851. FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
  852. # else
  853. # define fpclassify(x) __MATH_TG ((x), __fpclassify, (x))
  854. # endif
  855. /* Return nonzero value if sign of X is negative. */
  856. # if __GNUC_PREREQ (6,0) || __glibc_clang_prereq (3,3)
  857. # define signbit(x) __builtin_signbit (x)
  858. # elif defined __cplusplus
  859. /* In C++ mode, __MATH_TG cannot be used, because it relies on
  860. __builtin_types_compatible_p, which is a C-only builtin.
  861. The check for __cplusplus allows the use of the builtin instead of
  862. __MATH_TG. This is provided for libstdc++, only to let its configure
  863. test work. No further use of this definition of signbit is expected
  864. in C++ mode, since libstdc++ provides its own version of signbit
  865. in cmath (which undefines signbit). */
  866. # define signbit(x) __builtin_signbitl (x)
  867. # elif __GNUC_PREREQ (4,0)
  868. # define signbit(x) __MATH_TG ((x), __builtin_signbit, (x))
  869. # else
  870. # define signbit(x) __MATH_TG ((x), __signbit, (x))
  871. # endif
  872. /* Return nonzero value if X is not +-Inf or NaN. */
  873. # if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \
  874. || __glibc_clang_prereq (2,8)
  875. # define isfinite(x) __builtin_isfinite (x)
  876. # else
  877. # define isfinite(x) __MATH_TG ((x), __finite, (x))
  878. # endif
  879. /* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN. */
  880. # if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \
  881. || __glibc_clang_prereq (2,8)
  882. # define isnormal(x) __builtin_isnormal (x)
  883. # else
  884. # define isnormal(x) (fpclassify (x) == FP_NORMAL)
  885. # endif
  886. /* Return nonzero value if X is a NaN. We could use `fpclassify' but
  887. we already have this functions `__isnan' and it is faster. */
  888. # if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \
  889. || __glibc_clang_prereq (2,8)
  890. # define isnan(x) __builtin_isnan (x)
  891. # else
  892. # define isnan(x) __MATH_TG ((x), __isnan, (x))
  893. # endif
  894. /* Return nonzero value if X is positive or negative infinity. */
  895. # if __HAVE_DISTINCT_FLOAT128 && !__GNUC_PREREQ (7,0) \
  896. && !defined __SUPPORT_SNAN__ && !defined __cplusplus
  897. /* Since __builtin_isinf_sign is broken for float128 before GCC 7.0,
  898. use the helper function, __isinff128, with older compilers. This is
  899. only provided for C mode, because in C++ mode, GCC has no support
  900. for __builtin_types_compatible_p (and when in C++ mode, this macro is
  901. not used anyway, because libstdc++ headers undefine it). */
  902. # define isinf(x) \
  903. (__builtin_types_compatible_p (__typeof (x), _Float128) \
  904. ? __isinff128 (x) : __builtin_isinf_sign (x))
  905. # elif (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \
  906. || __glibc_clang_prereq (3,7)
  907. # define isinf(x) __builtin_isinf_sign (x)
  908. # else
  909. # define isinf(x) __MATH_TG ((x), __isinf, (x))
  910. # endif
  911. /* Bitmasks for the math_errhandling macro. */
  912. # define MATH_ERRNO 1 /* errno set by math functions. */
  913. # define MATH_ERREXCEPT 2 /* Exceptions raised by math functions. */
  914. /* By default all math functions support both errno and exception handling
  915. (except for soft floating point implementations which may only support
  916. errno handling). If errno handling is disabled, exceptions are still
  917. supported by GLIBC. Set math_errhandling to 0 with -ffast-math (this is
  918. nonconforming but it is more useful than leaving it undefined). */
  919. # ifdef __FAST_MATH__
  920. # define math_errhandling 0
  921. # elif defined __NO_MATH_ERRNO__
  922. # define math_errhandling (MATH_ERREXCEPT)
  923. # else
  924. # define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
  925. # endif
  926. #endif /* Use ISO C99. */
  927. #if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
  928. # include <bits/iscanonical.h>
  929. /* Return nonzero value if X is a signaling NaN. */
  930. # ifndef __cplusplus
  931. # define issignaling(x) __MATH_TG ((x), __issignaling, (x))
  932. # else
  933. /* In C++ mode, __MATH_TG cannot be used, because it relies on
  934. __builtin_types_compatible_p, which is a C-only builtin. On the
  935. other hand, overloading provides the means to distinguish between
  936. the floating-point types. The overloading resolution will match
  937. the correct parameter (regardless of type qualifiers (i.e.: const
  938. and volatile)). */
  939. extern "C++" {
  940. inline int issignaling (float __val) { return __issignalingf (__val); }
  941. inline int issignaling (double __val) { return __issignaling (__val); }
  942. inline int
  943. issignaling (long double __val)
  944. {
  945. # ifdef __NO_LONG_DOUBLE_MATH
  946. return __issignaling (__val);
  947. # else
  948. return __issignalingl (__val);
  949. # endif
  950. }
  951. # if __HAVE_FLOAT128_UNLIKE_LDBL
  952. /* When using an IEEE 128-bit long double, _Float128 is defined as long double
  953. in C++. */
  954. inline int issignaling (_Float128 __val) { return __issignalingf128 (__val); }
  955. # endif
  956. } /* extern C++ */
  957. # endif
  958. /* Return nonzero value if X is subnormal. */
  959. # define issubnormal(x) (fpclassify (x) == FP_SUBNORMAL)
  960. /* Return nonzero value if X is zero. */
  961. # ifndef __cplusplus
  962. # ifdef __SUPPORT_SNAN__
  963. # define iszero(x) (fpclassify (x) == FP_ZERO)
  964. # else
  965. # define iszero(x) (((__typeof (x)) (x)) == 0)
  966. # endif
  967. # else /* __cplusplus */
  968. extern "C++" {
  969. # ifdef __SUPPORT_SNAN__
  970. inline int
  971. iszero (float __val)
  972. {
  973. return __fpclassifyf (__val) == FP_ZERO;
  974. }
  975. inline int
  976. iszero (double __val)
  977. {
  978. return __fpclassify (__val) == FP_ZERO;
  979. }
  980. inline int
  981. iszero (long double __val)
  982. {
  983. # ifdef __NO_LONG_DOUBLE_MATH
  984. return __fpclassify (__val) == FP_ZERO;
  985. # else
  986. return __fpclassifyl (__val) == FP_ZERO;
  987. # endif
  988. }
  989. # if __HAVE_FLOAT128_UNLIKE_LDBL
  990. /* When using an IEEE 128-bit long double, _Float128 is defined as long double
  991. in C++. */
  992. inline int
  993. iszero (_Float128 __val)
  994. {
  995. return __fpclassifyf128 (__val) == FP_ZERO;
  996. }
  997. # endif
  998. # else
  999. template <class __T> inline bool
  1000. iszero (__T __val)
  1001. {
  1002. return __val == 0;
  1003. }
  1004. # endif
  1005. } /* extern C++ */
  1006. # endif /* __cplusplus */
  1007. #endif /* Use IEC_60559_BFP_EXT. */
  1008. #ifdef __USE_XOPEN
  1009. /* X/Open wants another strange constant. */
  1010. # define MAXFLOAT 3.40282347e+38F
  1011. #endif
  1012. /* Some useful constants. */
  1013. #if defined __USE_MISC || defined __USE_XOPEN
  1014. # define M_E 2.7182818284590452354 /* e */
  1015. # define M_LOG2E 1.4426950408889634074 /* log_2 e */
  1016. # define M_LOG10E 0.43429448190325182765 /* log_10 e */
  1017. # define M_LN2 0.69314718055994530942 /* log_e 2 */
  1018. # define M_LN10 2.30258509299404568402 /* log_e 10 */
  1019. # define M_PI 3.14159265358979323846 /* pi */
  1020. # define M_PI_2 1.57079632679489661923 /* pi/2 */
  1021. # define M_PI_4 0.78539816339744830962 /* pi/4 */
  1022. # define M_1_PI 0.31830988618379067154 /* 1/pi */
  1023. # define M_2_PI 0.63661977236758134308 /* 2/pi */
  1024. # define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
  1025. # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  1026. # define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
  1027. #endif
  1028. /* The above constants are not adequate for computation using `long double's.
  1029. Therefore we provide as an extension constants with similar names as a
  1030. GNU extension. Provide enough digits for the 128-bit IEEE quad. */
  1031. #ifdef __USE_GNU
  1032. # define M_El 2.718281828459045235360287471352662498L /* e */
  1033. # define M_LOG2El 1.442695040888963407359924681001892137L /* log_2 e */
  1034. # define M_LOG10El 0.434294481903251827651128918916605082L /* log_10 e */
  1035. # define M_LN2l 0.693147180559945309417232121458176568L /* log_e 2 */
  1036. # define M_LN10l 2.302585092994045684017991454684364208L /* log_e 10 */
  1037. # define M_PIl 3.141592653589793238462643383279502884L /* pi */
  1038. # define M_PI_2l 1.570796326794896619231321691639751442L /* pi/2 */
  1039. # define M_PI_4l 0.785398163397448309615660845819875721L /* pi/4 */
  1040. # define M_1_PIl 0.318309886183790671537767526745028724L /* 1/pi */
  1041. # define M_2_PIl 0.636619772367581343075535053490057448L /* 2/pi */
  1042. # define M_2_SQRTPIl 1.128379167095512573896158903121545172L /* 2/sqrt(pi) */
  1043. # define M_SQRT2l 1.414213562373095048801688724209698079L /* sqrt(2) */
  1044. # define M_SQRT1_2l 0.707106781186547524400844362104849039L /* 1/sqrt(2) */
  1045. #endif
  1046. #if __HAVE_FLOAT16 && defined __USE_GNU
  1047. # define M_Ef16 __f16 (2.718281828459045235360287471352662498) /* e */
  1048. # define M_LOG2Ef16 __f16 (1.442695040888963407359924681001892137) /* log_2 e */
  1049. # define M_LOG10Ef16 __f16 (0.434294481903251827651128918916605082) /* log_10 e */
  1050. # define M_LN2f16 __f16 (0.693147180559945309417232121458176568) /* log_e 2 */
  1051. # define M_LN10f16 __f16 (2.302585092994045684017991454684364208) /* log_e 10 */
  1052. # define M_PIf16 __f16 (3.141592653589793238462643383279502884) /* pi */
  1053. # define M_PI_2f16 __f16 (1.570796326794896619231321691639751442) /* pi/2 */
  1054. # define M_PI_4f16 __f16 (0.785398163397448309615660845819875721) /* pi/4 */
  1055. # define M_1_PIf16 __f16 (0.318309886183790671537767526745028724) /* 1/pi */
  1056. # define M_2_PIf16 __f16 (0.636619772367581343075535053490057448) /* 2/pi */
  1057. # define M_2_SQRTPIf16 __f16 (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
  1058. # define M_SQRT2f16 __f16 (1.414213562373095048801688724209698079) /* sqrt(2) */
  1059. # define M_SQRT1_2f16 __f16 (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
  1060. #endif
  1061. #if __HAVE_FLOAT32 && defined __USE_GNU
  1062. # define M_Ef32 __f32 (2.718281828459045235360287471352662498) /* e */
  1063. # define M_LOG2Ef32 __f32 (1.442695040888963407359924681001892137) /* log_2 e */
  1064. # define M_LOG10Ef32 __f32 (0.434294481903251827651128918916605082) /* log_10 e */
  1065. # define M_LN2f32 __f32 (0.693147180559945309417232121458176568) /* log_e 2 */
  1066. # define M_LN10f32 __f32 (2.302585092994045684017991454684364208) /* log_e 10 */
  1067. # define M_PIf32 __f32 (3.141592653589793238462643383279502884) /* pi */
  1068. # define M_PI_2f32 __f32 (1.570796326794896619231321691639751442) /* pi/2 */
  1069. # define M_PI_4f32 __f32 (0.785398163397448309615660845819875721) /* pi/4 */
  1070. # define M_1_PIf32 __f32 (0.318309886183790671537767526745028724) /* 1/pi */
  1071. # define M_2_PIf32 __f32 (0.636619772367581343075535053490057448) /* 2/pi */
  1072. # define M_2_SQRTPIf32 __f32 (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
  1073. # define M_SQRT2f32 __f32 (1.414213562373095048801688724209698079) /* sqrt(2) */
  1074. # define M_SQRT1_2f32 __f32 (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
  1075. #endif
  1076. #if __HAVE_FLOAT64 && defined __USE_GNU
  1077. # define M_Ef64 __f64 (2.718281828459045235360287471352662498) /* e */
  1078. # define M_LOG2Ef64 __f64 (1.442695040888963407359924681001892137) /* log_2 e */
  1079. # define M_LOG10Ef64 __f64 (0.434294481903251827651128918916605082) /* log_10 e */
  1080. # define M_LN2f64 __f64 (0.693147180559945309417232121458176568) /* log_e 2 */
  1081. # define M_LN10f64 __f64 (2.302585092994045684017991454684364208) /* log_e 10 */
  1082. # define M_PIf64 __f64 (3.141592653589793238462643383279502884) /* pi */
  1083. # define M_PI_2f64 __f64 (1.570796326794896619231321691639751442) /* pi/2 */
  1084. # define M_PI_4f64 __f64 (0.785398163397448309615660845819875721) /* pi/4 */
  1085. # define M_1_PIf64 __f64 (0.318309886183790671537767526745028724) /* 1/pi */
  1086. # define M_2_PIf64 __f64 (0.636619772367581343075535053490057448) /* 2/pi */
  1087. # define M_2_SQRTPIf64 __f64 (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
  1088. # define M_SQRT2f64 __f64 (1.414213562373095048801688724209698079) /* sqrt(2) */
  1089. # define M_SQRT1_2f64 __f64 (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
  1090. #endif
  1091. #if __HAVE_FLOAT128 && defined __USE_GNU
  1092. # define M_Ef128 __f128 (2.718281828459045235360287471352662498) /* e */
  1093. # define M_LOG2Ef128 __f128 (1.442695040888963407359924681001892137) /* log_2 e */
  1094. # define M_LOG10Ef128 __f128 (0.434294481903251827651128918916605082) /* log_10 e */
  1095. # define M_LN2f128 __f128 (0.693147180559945309417232121458176568) /* log_e 2 */
  1096. # define M_LN10f128 __f128 (2.302585092994045684017991454684364208) /* log_e 10 */
  1097. # define M_PIf128 __f128 (3.141592653589793238462643383279502884) /* pi */
  1098. # define M_PI_2f128 __f128 (1.570796326794896619231321691639751442) /* pi/2 */
  1099. # define M_PI_4f128 __f128 (0.785398163397448309615660845819875721) /* pi/4 */
  1100. # define M_1_PIf128 __f128 (0.318309886183790671537767526745028724) /* 1/pi */
  1101. # define M_2_PIf128 __f128 (0.636619772367581343075535053490057448) /* 2/pi */
  1102. # define M_2_SQRTPIf128 __f128 (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
  1103. # define M_SQRT2f128 __f128 (1.414213562373095048801688724209698079) /* sqrt(2) */
  1104. # define M_SQRT1_2f128 __f128 (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
  1105. #endif
  1106. #if __HAVE_FLOAT32X && defined __USE_GNU
  1107. # define M_Ef32x __f32x (2.718281828459045235360287471352662498) /* e */
  1108. # define M_LOG2Ef32x __f32x (1.442695040888963407359924681001892137) /* log_2 e */
  1109. # define M_LOG10Ef32x __f32x (0.434294481903251827651128918916605082) /* log_10 e */
  1110. # define M_LN2f32x __f32x (0.693147180559945309417232121458176568) /* log_e 2 */
  1111. # define M_LN10f32x __f32x (2.302585092994045684017991454684364208) /* log_e 10 */
  1112. # define M_PIf32x __f32x (3.141592653589793238462643383279502884) /* pi */
  1113. # define M_PI_2f32x __f32x (1.570796326794896619231321691639751442) /* pi/2 */
  1114. # define M_PI_4f32x __f32x (0.785398163397448309615660845819875721) /* pi/4 */
  1115. # define M_1_PIf32x __f32x (0.318309886183790671537767526745028724) /* 1/pi */
  1116. # define M_2_PIf32x __f32x (0.636619772367581343075535053490057448) /* 2/pi */
  1117. # define M_2_SQRTPIf32x __f32x (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
  1118. # define M_SQRT2f32x __f32x (1.414213562373095048801688724209698079) /* sqrt(2) */
  1119. # define M_SQRT1_2f32x __f32x (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
  1120. #endif
  1121. #if __HAVE_FLOAT64X && defined __USE_GNU
  1122. # define M_Ef64x __f64x (2.718281828459045235360287471352662498) /* e */
  1123. # define M_LOG2Ef64x __f64x (1.442695040888963407359924681001892137) /* log_2 e */
  1124. # define M_LOG10Ef64x __f64x (0.434294481903251827651128918916605082) /* log_10 e */
  1125. # define M_LN2f64x __f64x (0.693147180559945309417232121458176568) /* log_e 2 */
  1126. # define M_LN10f64x __f64x (2.302585092994045684017991454684364208) /* log_e 10 */
  1127. # define M_PIf64x __f64x (3.141592653589793238462643383279502884) /* pi */
  1128. # define M_PI_2f64x __f64x (1.570796326794896619231321691639751442) /* pi/2 */
  1129. # define M_PI_4f64x __f64x (0.785398163397448309615660845819875721) /* pi/4 */
  1130. # define M_1_PIf64x __f64x (0.318309886183790671537767526745028724) /* 1/pi */
  1131. # define M_2_PIf64x __f64x (0.636619772367581343075535053490057448) /* 2/pi */
  1132. # define M_2_SQRTPIf64x __f64x (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
  1133. # define M_SQRT2f64x __f64x (1.414213562373095048801688724209698079) /* sqrt(2) */
  1134. # define M_SQRT1_2f64x __f64x (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
  1135. #endif
  1136. #if __HAVE_FLOAT128X && defined __USE_GNU
  1137. # error "M_* values needed for _Float128x"
  1138. #endif
  1139. #ifdef __USE_ISOC99
  1140. # if __GNUC_PREREQ (3, 1)
  1141. /* ISO C99 defines some macros to compare number while taking care for
  1142. unordered numbers. Many FPUs provide special instructions to support
  1143. these operations. Generic support in GCC for these as builtins went
  1144. in 2.97, but not all cpus added their patterns until 3.1. Therefore
  1145. we enable the builtins from 3.1 onwards and use a generic implementation
  1146. othwerwise. */
  1147. # define isgreater(x, y) __builtin_isgreater(x, y)
  1148. # define isgreaterequal(x, y) __builtin_isgreaterequal(x, y)
  1149. # define isless(x, y) __builtin_isless(x, y)
  1150. # define islessequal(x, y) __builtin_islessequal(x, y)
  1151. # define islessgreater(x, y) __builtin_islessgreater(x, y)
  1152. # define isunordered(x, y) __builtin_isunordered(x, y)
  1153. # else
  1154. # define isgreater(x, y) \
  1155. (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
  1156. !isunordered (__x, __y) && __x > __y; }))
  1157. # define isgreaterequal(x, y) \
  1158. (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
  1159. !isunordered (__x, __y) && __x >= __y; }))
  1160. # define isless(x, y) \
  1161. (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
  1162. !isunordered (__x, __y) && __x < __y; }))
  1163. # define islessequal(x, y) \
  1164. (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
  1165. !isunordered (__x, __y) && __x <= __y; }))
  1166. # define islessgreater(x, y) \
  1167. (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
  1168. !isunordered (__x, __y) && __x != __y; }))
  1169. /* isunordered must always check both operands first for signaling NaNs. */
  1170. # define isunordered(x, y) \
  1171. (__extension__ ({ __typeof__ (x) __u = (x); __typeof__ (y) __v = (y); \
  1172. __u != __v && (__u != __u || __v != __v); }))
  1173. # endif
  1174. #endif
  1175. #if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
  1176. /* An expression whose type has the widest of the evaluation formats
  1177. of X and Y (which are of floating-point types). */
  1178. # if __FLT_EVAL_METHOD__ == 2 || __FLT_EVAL_METHOD__ > 64
  1179. # define __MATH_EVAL_FMT2(x, y) ((x) + (y) + 0.0L)
  1180. # elif __FLT_EVAL_METHOD__ == 1 || __FLT_EVAL_METHOD__ > 32
  1181. # define __MATH_EVAL_FMT2(x, y) ((x) + (y) + 0.0)
  1182. # elif __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 32
  1183. # define __MATH_EVAL_FMT2(x, y) ((x) + (y) + 0.0f)
  1184. # else
  1185. # define __MATH_EVAL_FMT2(x, y) ((x) + (y))
  1186. # endif
  1187. /* Return X == Y but raising "invalid" and setting errno if X or Y is
  1188. a NaN. */
  1189. # if !defined __cplusplus || (__cplusplus < 201103L && !defined __GNUC__)
  1190. # define iseqsig(x, y) \
  1191. __MATH_TG (__MATH_EVAL_FMT2 (x, y), __iseqsig, ((x), (y)))
  1192. # else
  1193. /* In C++ mode, __MATH_TG cannot be used, because it relies on
  1194. __builtin_types_compatible_p, which is a C-only builtin. Moreover,
  1195. the comparison macros from ISO C take two floating-point arguments,
  1196. which need not have the same type. Choosing what underlying function
  1197. to call requires evaluating the formats of the arguments, then
  1198. selecting which is wider. The macro __MATH_EVAL_FMT2 provides this
  1199. information, however, only the type of the macro expansion is
  1200. relevant (actually evaluating the expression would be incorrect).
  1201. Thus, the type is used as a template parameter for __iseqsig_type,
  1202. which calls the appropriate underlying function. */
  1203. extern "C++" {
  1204. template<typename> struct __iseqsig_type;
  1205. template<> struct __iseqsig_type<float>
  1206. {
  1207. static int __call (float __x, float __y) throw ()
  1208. {
  1209. return __iseqsigf (__x, __y);
  1210. }
  1211. };
  1212. template<> struct __iseqsig_type<double>
  1213. {
  1214. static int __call (double __x, double __y) throw ()
  1215. {
  1216. return __iseqsig (__x, __y);
  1217. }
  1218. };
  1219. template<> struct __iseqsig_type<long double>
  1220. {
  1221. static int __call (long double __x, long double __y) throw ()
  1222. {
  1223. # ifndef __NO_LONG_DOUBLE_MATH
  1224. return __iseqsigl (__x, __y);
  1225. # else
  1226. return __iseqsig (__x, __y);
  1227. # endif
  1228. }
  1229. };
  1230. # if __HAVE_FLOAT128_UNLIKE_LDBL
  1231. /* When using an IEEE 128-bit long double, _Float128 is defined as long double
  1232. in C++. */
  1233. template<> struct __iseqsig_type<_Float128>
  1234. {
  1235. static int __call (_Float128 __x, _Float128 __y) throw ()
  1236. {
  1237. return __iseqsigf128 (__x, __y);
  1238. }
  1239. };
  1240. # endif
  1241. template<typename _T1, typename _T2>
  1242. inline int
  1243. iseqsig (_T1 __x, _T2 __y) throw ()
  1244. {
  1245. # if __cplusplus >= 201103L
  1246. typedef decltype (__MATH_EVAL_FMT2 (__x, __y)) _T3;
  1247. # else
  1248. typedef __typeof (__MATH_EVAL_FMT2 (__x, __y)) _T3;
  1249. # endif
  1250. return __iseqsig_type<_T3>::__call (__x, __y);
  1251. }
  1252. } /* extern "C++" */
  1253. # endif /* __cplusplus */
  1254. #endif
  1255. __END_DECLS
  1256. #endif /* math.h */