| 1 | /* |
| 2 | * Copyright (c) 2002-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_LICENSE_HEADER_START@ |
| 5 | * |
| 6 | * The contents of this file constitute Original Code as defined in and |
| 7 | * are subject to the Apple Public Source License Version 1.1 (the |
| 8 | * "License"). You may not use this file except in compliance with the |
| 9 | * License. Please obtain a copy of the License at |
| 10 | * http://www.apple.com/publicsource and read it before using this file. |
| 11 | * |
| 12 | * This Original Code and all software distributed under the License are |
| 13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
| 17 | * License for the specific language governing rights and limitations |
| 18 | * under the License. |
| 19 | * |
| 20 | * @APPLE_LICENSE_HEADER_END@ |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #ifndef __MATH_H__ |
| 25 | #define __MATH_H__ |
| 26 | |
| 27 | #ifndef __MATH__ |
| 28 | #define __MATH__ |
| 29 | #endif |
| 30 | |
| 31 | #include <sys/cdefs.h> |
| 32 | #include <Availability.h> |
| 33 | |
| 34 | #if __has_include(<realtime_safety/realtime_safety.h>) |
| 35 | #include <realtime_safety/realtime_safety.h> |
| 36 | REALTIME_SAFE_BEGIN |
| 37 | #endif |
| 38 | |
| 39 | __BEGIN_DECLS |
| 40 | |
| 41 | /****************************************************************************** |
| 42 | * Floating point data types * |
| 43 | ******************************************************************************/ |
| 44 | |
| 45 | /* Define float_t and double_t per C standard, ISO/IEC 9899:2011 7.12 2, |
| 46 | taking advantage of GCC's __FLT_EVAL_METHOD__ (which a compiler may |
| 47 | define anytime and GCC does) that shadows FLT_EVAL_METHOD (which a |
| 48 | compiler must define only in float.h). */ |
| 49 | #if __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == -1 || __FLT_EVAL_METHOD__ == 16 |
| 50 | typedef float float_t; |
| 51 | typedef double double_t; |
| 52 | #elif __FLT_EVAL_METHOD__ == 1 |
| 53 | typedef double float_t; |
| 54 | typedef double double_t; |
| 55 | #elif __FLT_EVAL_METHOD__ == 2 |
| 56 | typedef long double float_t; |
| 57 | typedef long double double_t; |
| 58 | #else /* __FLT_EVAL_METHOD__ */ |
| 59 | # error "Unsupported value of __FLT_EVAL_METHOD__." |
| 60 | #endif /* __FLT_EVAL_METHOD__ */ |
| 61 | |
| 62 | #if defined(__GNUC__) |
| 63 | # define HUGE_VAL __builtin_huge_val() |
| 64 | # define HUGE_VALF __builtin_huge_valf() |
| 65 | # define HUGE_VALL __builtin_huge_vall() |
| 66 | # define NAN __builtin_nanf("0x7fc00000") |
| 67 | #else |
| 68 | # define HUGE_VAL 1e500 |
| 69 | # define HUGE_VALF 1e50f |
| 70 | # define HUGE_VALL 1e5000L |
| 71 | # define NAN __nan() |
| 72 | #endif |
| 73 | |
| 74 | #define INFINITY HUGE_VALF |
| 75 | |
| 76 | /****************************************************************************** |
| 77 | * Taxonomy of floating point data types * |
| 78 | ******************************************************************************/ |
| 79 | |
| 80 | #define FP_NAN 1 |
| 81 | #define FP_INFINITE 2 |
| 82 | #define FP_ZERO 3 |
| 83 | #define FP_NORMAL 4 |
| 84 | #define FP_SUBNORMAL 5 |
| 85 | #define FP_SUPERNORMAL 6 /* legacy PowerPC support; this is otherwise unused */ |
| 86 | |
| 87 | #if defined __arm64__ || defined __aarch64__ || defined __ARM_VFPV4__ |
| 88 | /* On these architectures, fma(), fmaf( ), and fmal( ) are generally about as |
| 89 | fast as (or faster than) separate multiply and add of the same operands. */ |
| 90 | # define FP_FAST_FMA 1 |
| 91 | # define FP_FAST_FMAF 1 |
| 92 | # define FP_FAST_FMAL 1 |
| 93 | #elif (defined __i386__ || defined __x86_64__) && (defined __FMA__ || defined __AVX512F__) |
| 94 | /* When targeting the FMA ISA extension, fma() and fmaf( ) are generally |
| 95 | about as fast as (or faster than) separate multiply and add of the same |
| 96 | operands, but fmal( ) may be more costly. */ |
| 97 | # define FP_FAST_FMA 1 |
| 98 | # define FP_FAST_FMAF 1 |
| 99 | # undef FP_FAST_FMAL |
| 100 | #else |
| 101 | /* On these architectures, fma( ), fmaf( ), and fmal( ) function calls are |
| 102 | significantly more costly than separate multiply and add operations. */ |
| 103 | # undef FP_FAST_FMA |
| 104 | # undef FP_FAST_FMAF |
| 105 | # undef FP_FAST_FMAL |
| 106 | #endif |
| 107 | |
| 108 | /* The values returned by `ilogb' for 0 and NaN respectively. */ |
| 109 | #define FP_ILOGB0 (-2147483647 - 1) |
| 110 | #define FP_ILOGBNAN (-2147483647 - 1) |
| 111 | |
| 112 | /* Bitmasks for the math_errhandling macro. */ |
| 113 | #define MATH_ERRNO 1 /* errno set by math functions. */ |
| 114 | #define MATH_ERREXCEPT 2 /* Exceptions raised by math functions. */ |
| 115 | |
| 116 | #define math_errhandling (__math_errhandling()) |
| 117 | extern int __math_errhandling(void); |
| 118 | |
| 119 | /****************************************************************************** |
| 120 | * * |
| 121 | * Inquiry macros * |
| 122 | * * |
| 123 | * fpclassify Returns one of the FP_* values. * |
| 124 | * isnormal Non-zero if and only if the argument x is normalized. * |
| 125 | * isfinite Non-zero if and only if the argument x is finite. * |
| 126 | * isnan Non-zero if and only if the argument x is a NaN. * |
| 127 | * signbit Non-zero if and only if the sign of the argument x is * |
| 128 | * negative. This includes, NaNs, infinities and zeros. * |
| 129 | * * |
| 130 | ******************************************************************************/ |
| 131 | |
| 132 | #if !defined(__cplusplus) || !defined(__has_feature) || !__has_feature(modules) |
| 133 | /* libc++'s math.h comes before this header in the search order. It |
| 134 | * will include this header first and then undef several of these |
| 135 | * macros. That doesn't work when the two headers are in different |
| 136 | * clang modules. The only way to make that work is to not declare |
| 137 | * the macros here, and let libc++ handle the declarations. |
| 138 | */ |
| 139 | #define fpclassify(x) \ |
| 140 | ( sizeof(x) == sizeof(float) ? __fpclassifyf((float)(x)) \ |
| 141 | : sizeof(x) == sizeof(double) ? __fpclassifyd((double)(x)) \ |
| 142 | : __fpclassifyl((long double)(x))) |
| 143 | #endif /* !defined(__cplusplus) && !__has_feature(modules) */ |
| 144 | |
| 145 | extern int __fpclassifyf(float); |
| 146 | extern int __fpclassifyd(double); |
| 147 | extern int __fpclassifyl(long double); |
| 148 | |
| 149 | #if (defined(__GNUC__) && 0 == __FINITE_MATH_ONLY__) |
| 150 | /* These inline functions may fail to return expected results if unsafe |
| 151 | math optimizations like those enabled by -ffast-math are turned on. |
| 152 | Thus, (somewhat surprisingly) you only get the fast inline |
| 153 | implementations if such compiler options are NOT enabled. This is |
| 154 | because the inline functions require the compiler to be adhering to |
| 155 | the standard in order to work properly; -ffast-math, among other |
| 156 | things, implies that NaNs don't happen, which allows the compiler to |
| 157 | optimize away checks like x != x, which might lead to things like |
| 158 | isnan(NaN) returning false. |
| 159 | |
| 160 | Thus, if you compile with -ffast-math, actual function calls are |
| 161 | generated for these utilities. */ |
| 162 | |
| 163 | #if !defined(__cplusplus) || !defined(__has_feature) || !__has_feature(modules) |
| 164 | #define isnormal(x) \ |
| 165 | ( sizeof(x) == sizeof(float) ? __inline_isnormalf((float)(x)) \ |
| 166 | : sizeof(x) == sizeof(double) ? __inline_isnormald((double)(x)) \ |
| 167 | : __inline_isnormall((long double)(x))) |
| 168 | |
| 169 | #define isfinite(x) \ |
| 170 | ( sizeof(x) == sizeof(float) ? __inline_isfinitef((float)(x)) \ |
| 171 | : sizeof(x) == sizeof(double) ? __inline_isfinited((double)(x)) \ |
| 172 | : __inline_isfinitel((long double)(x))) |
| 173 | |
| 174 | #define isinf(x) \ |
| 175 | ( sizeof(x) == sizeof(float) ? __inline_isinff((float)(x)) \ |
| 176 | : sizeof(x) == sizeof(double) ? __inline_isinfd((double)(x)) \ |
| 177 | : __inline_isinfl((long double)(x))) |
| 178 | |
| 179 | #define isnan(x) \ |
| 180 | ( sizeof(x) == sizeof(float) ? __inline_isnanf((float)(x)) \ |
| 181 | : sizeof(x) == sizeof(double) ? __inline_isnand((double)(x)) \ |
| 182 | : __inline_isnanl((long double)(x))) |
| 183 | |
| 184 | #define signbit(x) \ |
| 185 | ( sizeof(x) == sizeof(float) ? __inline_signbitf((float)(x)) \ |
| 186 | : sizeof(x) == sizeof(double) ? __inline_signbitd((double)(x)) \ |
| 187 | : __inline_signbitl((long double)(x))) |
| 188 | #endif /* !defined(__cplusplus) && !__has_feature(modules) */ |
| 189 | |
| 190 | __header_always_inline int __inline_isfinitef(float); |
| 191 | __header_always_inline int __inline_isfinited(double); |
| 192 | __header_always_inline int __inline_isfinitel(long double); |
| 193 | __header_always_inline int __inline_isinff(float); |
| 194 | __header_always_inline int __inline_isinfd(double); |
| 195 | __header_always_inline int __inline_isinfl(long double); |
| 196 | __header_always_inline int __inline_isnanf(float); |
| 197 | __header_always_inline int __inline_isnand(double); |
| 198 | __header_always_inline int __inline_isnanl(long double); |
| 199 | __header_always_inline int __inline_isnormalf(float); |
| 200 | __header_always_inline int __inline_isnormald(double); |
| 201 | __header_always_inline int __inline_isnormall(long double); |
| 202 | __header_always_inline int __inline_signbitf(float); |
| 203 | __header_always_inline int __inline_signbitd(double); |
| 204 | __header_always_inline int __inline_signbitl(long double); |
| 205 | |
| 206 | __header_always_inline int __inline_isfinitef(float __x) { |
| 207 | return __x == __x && __builtin_fabsf(__x) != __builtin_inff(); |
| 208 | } |
| 209 | __header_always_inline int __inline_isfinited(double __x) { |
| 210 | return __x == __x && __builtin_fabs(__x) != __builtin_inf(); |
| 211 | } |
| 212 | __header_always_inline int __inline_isfinitel(long double __x) { |
| 213 | return __x == __x && __builtin_fabsl(__x) != __builtin_infl(); |
| 214 | } |
| 215 | __header_always_inline int __inline_isinff(float __x) { |
| 216 | return __builtin_fabsf(__x) == __builtin_inff(); |
| 217 | } |
| 218 | __header_always_inline int __inline_isinfd(double __x) { |
| 219 | return __builtin_fabs(__x) == __builtin_inf(); |
| 220 | } |
| 221 | __header_always_inline int __inline_isinfl(long double __x) { |
| 222 | return __builtin_fabsl(__x) == __builtin_infl(); |
| 223 | } |
| 224 | __header_always_inline int __inline_isnanf(float __x) { |
| 225 | return __x != __x; |
| 226 | } |
| 227 | __header_always_inline int __inline_isnand(double __x) { |
| 228 | return __x != __x; |
| 229 | } |
| 230 | __header_always_inline int __inline_isnanl(long double __x) { |
| 231 | return __x != __x; |
| 232 | } |
| 233 | __header_always_inline int __inline_signbitf(float __x) { |
| 234 | union { float __f; unsigned int __u; } __u; |
| 235 | __u.__f = __x; |
| 236 | return (int)(__u.__u >> 31); |
| 237 | } |
| 238 | __header_always_inline int __inline_signbitd(double __x) { |
| 239 | union { double __f; unsigned long long __u; } __u; |
| 240 | __u.__f = __x; |
| 241 | return (int)(__u.__u >> 63); |
| 242 | } |
| 243 | #if defined __i386__ || defined __x86_64__ |
| 244 | __header_always_inline int __inline_signbitl(long double __x) { |
| 245 | union { |
| 246 | long double __ld; |
| 247 | struct{ unsigned long long __m; unsigned short __sexp; } __p; |
| 248 | } __u; |
| 249 | __u.__ld = __x; |
| 250 | return (int)(__u.__p.__sexp >> 15); |
| 251 | } |
| 252 | #else |
| 253 | __header_always_inline int __inline_signbitl(long double __x) { |
| 254 | union { long double __f; unsigned long long __u;} __u; |
| 255 | __u.__f = __x; |
| 256 | return (int)(__u.__u >> 63); |
| 257 | } |
| 258 | #endif |
| 259 | __header_always_inline int __inline_isnormalf(float __x) { |
| 260 | return __inline_isfinitef(__x) && __builtin_fabsf(__x) >= __FLT_MIN__; |
| 261 | } |
| 262 | __header_always_inline int __inline_isnormald(double __x) { |
| 263 | return __inline_isfinited(__x) && __builtin_fabs(__x) >= __DBL_MIN__; |
| 264 | } |
| 265 | __header_always_inline int __inline_isnormall(long double __x) { |
| 266 | return __inline_isfinitel(__x) && __builtin_fabsl(__x) >= __LDBL_MIN__; |
| 267 | } |
| 268 | |
| 269 | #else /* defined(__GNUC__) && 0 == __FINITE_MATH_ONLY__ */ |
| 270 | |
| 271 | /* Implementations making function calls to fall back on when -ffast-math |
| 272 | or similar is specified. These are not available in iOS versions prior |
| 273 | to 6.0. If you need them, you must target that version or later. */ |
| 274 | |
| 275 | #if !defined(__cplusplus) || !defined(__has_feature) || !__has_feature(modules) |
| 276 | /* libc++'s math.h comes before this header in the search order. It |
| 277 | * will include this header first and then undef several of these |
| 278 | * macros. That doesn't work when the two headers are in different |
| 279 | * clang modules. The only way to make that work is to not declare |
| 280 | * the macros here, and let libc++ handle the declarations. |
| 281 | */ |
| 282 | #define isnormal(x) \ |
| 283 | ( sizeof(x) == sizeof(float) ? __isnormalf((float)(x)) \ |
| 284 | : sizeof(x) == sizeof(double) ? __isnormald((double)(x)) \ |
| 285 | : __isnormall((long double)(x))) |
| 286 | |
| 287 | #define isfinite(x) \ |
| 288 | ( sizeof(x) == sizeof(float) ? __isfinitef((float)(x)) \ |
| 289 | : sizeof(x) == sizeof(double) ? __isfinited((double)(x)) \ |
| 290 | : __isfinitel((long double)(x))) |
| 291 | |
| 292 | #define isinf(x) \ |
| 293 | ( sizeof(x) == sizeof(float) ? __isinff((float)(x)) \ |
| 294 | : sizeof(x) == sizeof(double) ? __isinfd((double)(x)) \ |
| 295 | : __isinfl((long double)(x))) |
| 296 | |
| 297 | #define isnan(x) \ |
| 298 | ( sizeof(x) == sizeof(float) ? __isnanf((float)(x)) \ |
| 299 | : sizeof(x) == sizeof(double) ? __isnand((double)(x)) \ |
| 300 | : __isnanl((long double)(x))) |
| 301 | |
| 302 | #define signbit(x) \ |
| 303 | ( sizeof(x) == sizeof(float) ? __signbitf((float)(x)) \ |
| 304 | : sizeof(x) == sizeof(double) ? __signbitd((double)(x)) \ |
| 305 | : __signbitl((long double)(x))) |
| 306 | #endif /* !defined(__cplusplus) && !__has_feature(modules) */ |
| 307 | |
| 308 | extern int __isnormalf(float); |
| 309 | extern int __isnormald(double); |
| 310 | extern int __isnormall(long double); |
| 311 | extern int __isfinitef(float); |
| 312 | extern int __isfinited(double); |
| 313 | extern int __isfinitel(long double); |
| 314 | extern int __isinff(float); |
| 315 | extern int __isinfd(double); |
| 316 | extern int __isinfl(long double); |
| 317 | extern int __isnanf(float); |
| 318 | extern int __isnand(double); |
| 319 | extern int __isnanl(long double); |
| 320 | extern int __signbitf(float); |
| 321 | extern int __signbitd(double); |
| 322 | extern int __signbitl(long double); |
| 323 | |
| 324 | #endif /* defined(__GNUC__) && 0 == __FINITE_MATH_ONLY__ */ |
| 325 | |
| 326 | /****************************************************************************** |
| 327 | * * |
| 328 | * Math Functions * |
| 329 | * * |
| 330 | ******************************************************************************/ |
| 331 | |
| 332 | extern float acosf(float); |
| 333 | extern double acos(double); |
| 334 | extern long double acosl(long double); |
| 335 | |
| 336 | extern float asinf(float); |
| 337 | extern double asin(double); |
| 338 | extern long double asinl(long double); |
| 339 | |
| 340 | extern float atanf(float); |
| 341 | extern double atan(double); |
| 342 | extern long double atanl(long double); |
| 343 | |
| 344 | extern float atan2f(float, float); |
| 345 | extern double atan2(double, double); |
| 346 | extern long double atan2l(long double, long double); |
| 347 | |
| 348 | extern float cosf(float); |
| 349 | extern double cos(double); |
| 350 | extern long double cosl(long double); |
| 351 | |
| 352 | extern float sinf(float); |
| 353 | extern double sin(double); |
| 354 | extern long double sinl(long double); |
| 355 | |
| 356 | extern float tanf(float); |
| 357 | extern double tan(double); |
| 358 | extern long double tanl(long double); |
| 359 | |
| 360 | extern float acoshf(float); |
| 361 | extern double acosh(double); |
| 362 | extern long double acoshl(long double); |
| 363 | |
| 364 | extern float asinhf(float); |
| 365 | extern double asinh(double); |
| 366 | extern long double asinhl(long double); |
| 367 | |
| 368 | extern float atanhf(float); |
| 369 | extern double atanh(double); |
| 370 | extern long double atanhl(long double); |
| 371 | |
| 372 | extern float coshf(float); |
| 373 | extern double cosh(double); |
| 374 | extern long double coshl(long double); |
| 375 | |
| 376 | extern float sinhf(float); |
| 377 | extern double sinh(double); |
| 378 | extern long double sinhl(long double); |
| 379 | |
| 380 | extern float tanhf(float); |
| 381 | extern double tanh(double); |
| 382 | extern long double tanhl(long double); |
| 383 | |
| 384 | extern float expf(float); |
| 385 | extern double exp(double); |
| 386 | extern long double expl(long double); |
| 387 | |
| 388 | extern float exp2f(float); |
| 389 | extern double exp2(double); |
| 390 | extern long double exp2l(long double); |
| 391 | |
| 392 | extern float expm1f(float); |
| 393 | extern double expm1(double); |
| 394 | extern long double expm1l(long double); |
| 395 | |
| 396 | extern float logf(float); |
| 397 | extern double log(double); |
| 398 | extern long double logl(long double); |
| 399 | |
| 400 | extern float log10f(float); |
| 401 | extern double log10(double); |
| 402 | extern long double log10l(long double); |
| 403 | |
| 404 | extern float log2f(float); |
| 405 | extern double log2(double); |
| 406 | extern long double log2l(long double); |
| 407 | |
| 408 | extern float log1pf(float); |
| 409 | extern double log1p(double); |
| 410 | extern long double log1pl(long double); |
| 411 | |
| 412 | extern float logbf(float); |
| 413 | extern double logb(double); |
| 414 | extern long double logbl(long double); |
| 415 | |
| 416 | extern float modff(float, float *); |
| 417 | extern double modf(double, double *); |
| 418 | extern long double modfl(long double, long double *); |
| 419 | |
| 420 | extern float ldexpf(float, int); |
| 421 | extern double ldexp(double, int); |
| 422 | extern long double ldexpl(long double, int); |
| 423 | |
| 424 | extern float frexpf(float, int *); |
| 425 | extern double frexp(double, int *); |
| 426 | extern long double frexpl(long double, int *); |
| 427 | |
| 428 | extern int ilogbf(float); |
| 429 | extern int ilogb(double); |
| 430 | extern int ilogbl(long double); |
| 431 | |
| 432 | extern float scalbnf(float, int); |
| 433 | extern double scalbn(double, int); |
| 434 | extern long double scalbnl(long double, int); |
| 435 | |
| 436 | extern float scalblnf(float, long int); |
| 437 | extern double scalbln(double, long int); |
| 438 | extern long double scalblnl(long double, long int); |
| 439 | |
| 440 | extern float fabsf(float); |
| 441 | extern double fabs(double); |
| 442 | extern long double fabsl(long double); |
| 443 | |
| 444 | extern float cbrtf(float); |
| 445 | extern double cbrt(double); |
| 446 | extern long double cbrtl(long double); |
| 447 | |
| 448 | extern float hypotf(float, float); |
| 449 | extern double hypot(double, double); |
| 450 | extern long double hypotl(long double, long double); |
| 451 | |
| 452 | extern float powf(float, float); |
| 453 | extern double pow(double, double); |
| 454 | extern long double powl(long double, long double); |
| 455 | |
| 456 | extern float sqrtf(float); |
| 457 | extern double sqrt(double); |
| 458 | extern long double sqrtl(long double); |
| 459 | |
| 460 | extern float erff(float); |
| 461 | extern double erf(double); |
| 462 | extern long double erfl(long double); |
| 463 | |
| 464 | extern float erfcf(float); |
| 465 | extern double erfc(double); |
| 466 | extern long double erfcl(long double); |
| 467 | |
| 468 | /*	lgammaf, lgamma, and lgammal are not thread-safe. The thread-safe |
| 469 | variants lgammaf_r, lgamma_r, and lgammal_r are made available if |
| 470 | you define the _REENTRANT symbol before including <math.h> */ |
| 471 | extern float lgammaf(float); |
| 472 | extern double lgamma(double); |
| 473 | extern long double lgammal(long double); |
| 474 | |
| 475 | extern float tgammaf(float); |
| 476 | extern double tgamma(double); |
| 477 | extern long double tgammal(long double); |
| 478 | |
| 479 | extern float ceilf(float); |
| 480 | extern double ceil(double); |
| 481 | extern long double ceill(long double); |
| 482 | |
| 483 | extern float floorf(float); |
| 484 | extern double floor(double); |
| 485 | extern long double floorl(long double); |
| 486 | |
| 487 | extern float nearbyintf(float); |
| 488 | extern double nearbyint(double); |
| 489 | extern long double nearbyintl(long double); |
| 490 | |
| 491 | extern float rintf(float); |
| 492 | extern double rint(double); |
| 493 | extern long double rintl(long double); |
| 494 | |
| 495 | extern long int lrintf(float); |
| 496 | extern long int lrint(double); |
| 497 | extern long int lrintl(long double); |
| 498 | |
| 499 | extern float roundf(float); |
| 500 | extern double round(double); |
| 501 | extern long double roundl(long double); |
| 502 | |
| 503 | extern long int lroundf(float); |
| 504 | extern long int lround(double); |
| 505 | extern long int lroundl(long double); |
| 506 | |
| 507 | /* long long is not part of C90. Make sure you are passing -std=c99 or |
| 508 | -std=gnu99 or higher if you need these functions returning long longs */ |
| 509 | #if !(__DARWIN_NO_LONG_LONG) |
| 510 | extern long long int llrintf(float); |
| 511 | extern long long int llrint(double); |
| 512 | extern long long int llrintl(long double); |
| 513 | |
| 514 | extern long long int llroundf(float); |
| 515 | extern long long int llround(double); |
| 516 | extern long long int llroundl(long double); |
| 517 | #endif /* !(__DARWIN_NO_LONG_LONG) */ |
| 518 | |
| 519 | extern float truncf(float); |
| 520 | extern double trunc(double); |
| 521 | extern long double truncl(long double); |
| 522 | |
| 523 | extern float fmodf(float, float); |
| 524 | extern double fmod(double, double); |
| 525 | extern long double fmodl(long double, long double); |
| 526 | |
| 527 | extern float remainderf(float, float); |
| 528 | extern double remainder(double, double); |
| 529 | extern long double remainderl(long double, long double); |
| 530 | |
| 531 | extern float remquof(float, float, int *); |
| 532 | extern double remquo(double, double, int *); |
| 533 | extern long double remquol(long double, long double, int *); |
| 534 | |
| 535 | extern float copysignf(float, float); |
| 536 | extern double copysign(double, double); |
| 537 | extern long double copysignl(long double, long double); |
| 538 | |
| 539 | extern float nanf(const char *); |
| 540 | extern double nan(const char *); |
| 541 | extern long double nanl(const char *); |
| 542 | |
| 543 | extern float nextafterf(float, float); |
| 544 | extern double nextafter(double, double); |
| 545 | extern long double nextafterl(long double, long double); |
| 546 | |
| 547 | extern double nexttoward(double, long double); |
| 548 | extern float nexttowardf(float, long double); |
| 549 | extern long double nexttowardl(long double, long double); |
| 550 | |
| 551 | extern float fdimf(float, float); |
| 552 | extern double fdim(double, double); |
| 553 | extern long double fdiml(long double, long double); |
| 554 | |
| 555 | extern float fmaxf(float, float); |
| 556 | extern double fmax(double, double); |
| 557 | extern long double fmaxl(long double, long double); |
| 558 | |
| 559 | extern float fminf(float, float); |
| 560 | extern double fmin(double, double); |
| 561 | extern long double fminl(long double, long double); |
| 562 | |
| 563 | extern float fmaf(float, float, float); |
| 564 | extern double fma(double, double, double); |
| 565 | extern long double fmal(long double, long double, long double); |
| 566 | |
| 567 | #if !defined(__cplusplus) || !defined(__has_feature) || !__has_feature(modules) |
| 568 | /* libc++'s math.h comes before this header in the search order. It |
| 569 | * will include this header first and then undef several of these |
| 570 | * macros. That doesn't work when the two headers are in different |
| 571 | * clang modules. The only way to make that work is to not declare |
| 572 | * the macros here, and let libc++ handle the declarations. |
| 573 | */ |
| 574 | #define isgreater(x, y) __builtin_isgreater((x),(y)) |
| 575 | #define isgreaterequal(x, y) __builtin_isgreaterequal((x),(y)) |
| 576 | #define isless(x, y) __builtin_isless((x),(y)) |
| 577 | #define islessequal(x, y) __builtin_islessequal((x),(y)) |
| 578 | #define islessgreater(x, y) __builtin_islessgreater((x),(y)) |
| 579 | #define isunordered(x, y) __builtin_isunordered((x),(y)) |
| 580 | #endif /* !defined(__cplusplus) && !__has_feature(modules) */ |
| 581 | |
| 582 | #if defined __i386__ || defined __x86_64__ |
| 583 | /* Deprecated functions; use the INFINITY and NAN macros instead. */ |
| 584 | extern float __inff(void) |
| 585 | __API_DEPRECATED("use `(float)INFINITY` instead", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos); |
| 586 | extern double __inf(void) |
| 587 | __API_DEPRECATED("use `INFINITY` instead", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos); |
| 588 | extern long double __infl(void) |
| 589 | __API_DEPRECATED("use `(long double)INFINITY` instead", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos); |
| 590 | extern float __nan(void) |
| 591 | __API_DEPRECATED("use `NAN` instead", macos(10.0, 10.14)) __API_UNAVAILABLE(ios, watchos, tvos); |
| 592 | #endif |
| 593 | |
| 594 | /****************************************************************************** |
| 595 | * Reentrant variants of lgamma[fl] * |
| 596 | ******************************************************************************/ |
| 597 | |
| 598 | #if defined(_REENTRANT) || defined(__swift__) |
| 599 | /* Reentrant variants of the lgamma[fl] functions. */ |
| 600 | extern float lgammaf_r(float, int *) __API_AVAILABLE(macos(10.6), ios(3.1)); |
| 601 | extern double lgamma_r(double, int *) __API_AVAILABLE(macos(10.6), ios(3.1)); |
| 602 | extern long double lgammal_r(long double, int *) __API_AVAILABLE(macos(10.6), ios(3.1)); |
| 603 | #endif /* _REENTRANT || __swift__ */ |
| 604 | |
| 605 | /****************************************************************************** |
| 606 | * Apple extensions to the C standard * |
| 607 | ******************************************************************************/ |
| 608 | |
| 609 | /* Because these functions are not specified by any relevant standard, they |
| 610 | are prefixed with __, which places them in the implementor's namespace, so |
| 611 | they should not conflict with any developer or third-party code. If they |
| 612 | are added to a relevant standard in the future, un-prefixed names may be |
| 613 | added to the library and they may be moved out of this section of the |
| 614 | header. |
| 615 | |
| 616 | Because these functions are non-standard, they may not be available on non- |
| 617 | Apple platforms. */ |
| 618 | |
| 619 | /* __exp10(x) returns 10**x. Edge cases match those of exp( ) and exp2( ). */ |
| 620 | extern float __exp10f(float) __API_AVAILABLE(macos(10.9), ios(7.0)); |
| 621 | extern double __exp10(double) __API_AVAILABLE(macos(10.9), ios(7.0)); |
| 622 | |
| 623 | /* __sincos(x,sinp,cosp) computes the sine and cosine of x with a single |
| 624 | function call, storing the sine in the memory pointed to by sinp, and |
| 625 | the cosine in the memory pointed to by cosp. Edge cases match those of |
| 626 | separate calls to sin( ) and cos( ). */ |
| 627 | __header_always_inline void __sincosf(float __x, float *__sinp, float *__cosp); |
| 628 | __header_always_inline void __sincos(double __x, double *__sinp, double *__cosp); |
| 629 | |
| 630 | /* __sinpi(x) returns the sine of pi times x; __cospi(x) and __tanpi(x) return |
| 631 | the cosine and tangent, respectively. These functions can produce a more |
| 632 | accurate answer than expressions of the form sin(M_PI * x) because they |
| 633 | avoid any loss of precision that results from rounding the result of the |
| 634 | multiplication M_PI * x. They may also be significantly more efficient in |
| 635 | some cases because the argument reduction for these functions is easier |
| 636 | to compute. Consult the man pages for edge case details. */ |
| 637 | extern float __cospif(float) __API_AVAILABLE(macos(10.9), ios(7.0)); |
| 638 | extern double __cospi(double) __API_AVAILABLE(macos(10.9), ios(7.0)); |
| 639 | extern float __sinpif(float) __API_AVAILABLE(macos(10.9), ios(7.0)); |
| 640 | extern double __sinpi(double) __API_AVAILABLE(macos(10.9), ios(7.0)); |
| 641 | extern float __tanpif(float) __API_AVAILABLE(macos(10.9), ios(7.0)); |
| 642 | extern double __tanpi(double) __API_AVAILABLE(macos(10.9), ios(7.0)); |
| 643 | |
| 644 | /* half precision math functions */ |
| 645 | extern _Float16 __fabsf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 646 | extern _Float16 __hypotf16(_Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 647 | extern _Float16 __sqrtf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 648 | extern _Float16 __ceilf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 649 | extern _Float16 __floorf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 650 | extern _Float16 __rintf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 651 | extern _Float16 __roundf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 652 | extern _Float16 __truncf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 653 | extern _Float16 __copysignf16(_Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 654 | extern _Float16 __nextafterf16(_Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 655 | extern _Float16 __fmaxf16(_Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 656 | extern _Float16 __fminf16(_Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 657 | extern _Float16 __fmaf16(_Float16, _Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0)); |
| 658 | |
| 659 | #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED < 1090) || \ |
| 660 | (defined __IPHONE_OS_VERSION_MIN_REQUIRED && __IPHONE_OS_VERSION_MIN_REQUIRED < 70000) |
| 661 | /* __sincos and __sincosf were introduced in OSX 10.9 and iOS 7.0. When |
| 662 | targeting an older system, we simply split them up into discrete calls |
| 663 | to sin( ) and cos( ). */ |
| 664 | __header_always_inline void __sincosf(float __x, float *__sinp, float *__cosp) { |
| 665 | *__sinp = sinf(__x); |
| 666 | *__cosp = cosf(__x); |
| 667 | } |
| 668 | |
| 669 | __header_always_inline void __sincos(double __x, double *__sinp, double *__cosp) { |
| 670 | *__sinp = sin(__x); |
| 671 | *__cosp = cos(__x); |
| 672 | } |
| 673 | #else |
| 674 | /* __sincospi(x,sinp,cosp) computes the sine and cosine of pi times x with a |
| 675 | single function call, storing the sine in the memory pointed to by sinp, |
| 676 | and the cosine in the memory pointed to by cosp. Edge cases match those |
| 677 | of separate calls to __sinpi( ) and __cospi( ), and are documented in the |
| 678 | man pages. |
| 679 | |
| 680 | These functions were introduced in OSX 10.9 and iOS 7.0. Because they are |
| 681 | implemented as header inlines, weak-linking does not function as normal, |
| 682 | and they are simply hidden when targeting earlier OS versions. */ |
| 683 | __header_always_inline void __sincospif(float __x, float *__sinp, float *__cosp); |
| 684 | __header_always_inline void __sincospi(double __x, double *__sinp, double *__cosp); |
| 685 | |
| 686 | /* Implementation details of __sincos and __sincospi allowing them to return |
| 687 | two results while allowing the compiler to optimize away unnecessary load- |
| 688 | store traffic. Although these interfaces are exposed in the math.h header |
| 689 | to allow compilers to generate better code, users should call __sincos[f] |
| 690 | and __sincospi[f] instead and allow the compiler to emit these calls. */ |
| 691 | struct __float2 { float __sinval; float __cosval; }; |
| 692 | struct __double2 { double __sinval; double __cosval; }; |
| 693 | |
| 694 | extern struct __float2 __sincosf_stret(float); |
| 695 | extern struct __double2 __sincos_stret(double); |
| 696 | extern struct __float2 __sincospif_stret(float); |
| 697 | extern struct __double2 __sincospi_stret(double); |
| 698 | |
| 699 | __header_always_inline void __sincosf(float __x, float *__sinp, float *__cosp) { |
| 700 | const struct __float2 __stret = __sincosf_stret(__x); |
| 701 | *__sinp = __stret.__sinval; *__cosp = __stret.__cosval; |
| 702 | } |
| 703 | |
| 704 | __header_always_inline void __sincos(double __x, double *__sinp, double *__cosp) { |
| 705 | const struct __double2 __stret = __sincos_stret(__x); |
| 706 | *__sinp = __stret.__sinval; *__cosp = __stret.__cosval; |
| 707 | } |
| 708 | |
| 709 | __header_always_inline void __sincospif(float __x, float *__sinp, float *__cosp) { |
| 710 | const struct __float2 __stret = __sincospif_stret(__x); |
| 711 | *__sinp = __stret.__sinval; *__cosp = __stret.__cosval; |
| 712 | } |
| 713 | |
| 714 | __header_always_inline void __sincospi(double __x, double *__sinp, double *__cosp) { |
| 715 | const struct __double2 __stret = __sincospi_stret(__x); |
| 716 | *__sinp = __stret.__sinval; *__cosp = __stret.__cosval; |
| 717 | } |
| 718 | #endif |
| 719 | |
| 720 | /****************************************************************************** |
| 721 | * POSIX/UNIX extensions to the C standard * |
| 722 | ******************************************************************************/ |
| 723 | |
| 724 | #if __DARWIN_C_LEVEL >= 199506L |
| 725 | extern double j0(double) __API_AVAILABLE(macos(10.0), ios(3.2)); |
| 726 | extern double j1(double) __API_AVAILABLE(macos(10.0), ios(3.2)); |
| 727 | extern double jn(int, double) __API_AVAILABLE(macos(10.0), ios(3.2)); |
| 728 | extern double y0(double) __API_AVAILABLE(macos(10.0), ios(3.2)); |
| 729 | extern double y1(double) __API_AVAILABLE(macos(10.0), ios(3.2)); |
| 730 | extern double yn(int, double) __API_AVAILABLE(macos(10.0), ios(3.2)); |
| 731 | extern double scalb(double, double); |
| 732 | extern int signgam; |
| 733 | |
| 734 | /* Even though these might be more useful as long doubles, POSIX requires |
| 735 | that they be double-precision literals. */ |
| 736 | #define M_E 2.71828182845904523536028747135266250 /* e */ |
| 737 | #define M_LOG2E 1.44269504088896340735992468100189214 /* log2(e) */ |
| 738 | #define M_LOG10E 0.434294481903251827651128918916605082 /* log10(e) */ |
| 739 | #define M_LN2 0.693147180559945309417232121458176568 /* loge(2) */ |
| 740 | #define M_LN10 2.30258509299404568401799145468436421 /* loge(10) */ |
| 741 | #define M_PI 3.14159265358979323846264338327950288 /* pi */ |
| 742 | #define M_PI_2 1.57079632679489661923132169163975144 /* pi/2 */ |
| 743 | #define M_PI_4 0.785398163397448309615660845819875721 /* pi/4 */ |
| 744 | #define M_1_PI 0.318309886183790671537767526745028724 /* 1/pi */ |
| 745 | #define M_2_PI 0.636619772367581343075535053490057448 /* 2/pi */ |
| 746 | #define M_2_SQRTPI 1.12837916709551257389615890312154517 /* 2/sqrt(pi) */ |
| 747 | #define M_SQRT2 1.41421356237309504880168872420969808 /* sqrt(2) */ |
| 748 | #define M_SQRT1_2 0.707106781186547524400844362104849039 /* 1/sqrt(2) */ |
| 749 | |
| 750 | #define MAXFLOAT 0x1.fffffep+127f |
| 751 | #endif /* __DARWIN_C_LEVEL >= 199506L */ |
| 752 | |
| 753 | /* Long-double versions of M_E, etc for convenience on Intel where long- |
| 754 | double is not the same as double. Define __MATH_LONG_DOUBLE_CONSTANTS |
| 755 | to make these constants available. */ |
| 756 | #if defined __MATH_LONG_DOUBLE_CONSTANTS |
| 757 | #define M_El 0xa.df85458a2bb4a9bp-2L |
| 758 | #define M_LOG2El 0xb.8aa3b295c17f0bcp-3L |
| 759 | #define M_LOG10El 0xd.e5bd8a937287195p-5L |
| 760 | #define M_LN2l 0xb.17217f7d1cf79acp-4L |
| 761 | #define M_LN10l 0x9.35d8dddaaa8ac17p-2L |
| 762 | #define M_PIl 0xc.90fdaa22168c235p-2L |
| 763 | #define M_PI_2l 0xc.90fdaa22168c235p-3L |
| 764 | #define M_PI_4l 0xc.90fdaa22168c235p-4L |
| 765 | #define M_1_PIl 0xa.2f9836e4e44152ap-5L |
| 766 | #define M_2_PIl 0xa.2f9836e4e44152ap-4L |
| 767 | #define M_2_SQRTPIl 0x9.06eba8214db688dp-3L |
| 768 | #define M_SQRT2l 0xb.504f333f9de6484p-3L |
| 769 | #define M_SQRT1_2l 0xb.504f333f9de6484p-4L |
| 770 | #endif /* defined __MATH_LONG_DOUBLE_CONSTANTS */ |
| 771 | |
| 772 | /****************************************************************************** |
| 773 | * Legacy BSD extensions to the C standard * |
| 774 | ******************************************************************************/ |
| 775 | |
| 776 | #if __DARWIN_C_LEVEL >= __DARWIN_C_FULL |
| 777 | #define FP_SNAN		FP_NAN |
| 778 | #define FP_QNAN		FP_NAN |
| 779 | #define	HUGE		MAXFLOAT |
| 780 | #define X_TLOSS		1.41484755040568800000e+16 |
| 781 | #define	DOMAIN		1 |
| 782 | #define	SING		2 |
| 783 | #define	OVERFLOW	3 |
| 784 | #define	UNDERFLOW	4 |
| 785 | #define	TLOSS		5 |
| 786 | #define	PLOSS		6 |
| 787 | |
| 788 | #if defined __i386__ || defined __x86_64__ |
| 789 | /* Legacy BSD API; use the C99 `lrint( )` function instead. */ |
| 790 | extern long int rinttol(double) |
| 791 | __API_DEPRECATED_WITH_REPLACEMENT("lrint", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos); |
| 792 | /* Legacy BSD API; use the C99 `lround( )` function instead. */ |
| 793 | extern long int roundtol(double) |
| 794 | __API_DEPRECATED_WITH_REPLACEMENT("lround", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos); |
| 795 | /* Legacy BSD API; use the C99 `remainder( )` function instead. */ |
| 796 | extern double drem(double, double) |
| 797 | __API_DEPRECATED_WITH_REPLACEMENT("remainder", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos); |
| 798 | /* Legacy BSD API; use the C99 `isfinite( )` macro instead. */ |
| 799 | extern int finite(double) |
| 800 | __API_DEPRECATED("Use `isfinite((double)x)` instead.", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos); |
| 801 | /* Legacy BSD API; use the C99 `tgamma( )` function instead. */ |
| 802 | extern double gamma(double) |
| 803 | __API_DEPRECATED_WITH_REPLACEMENT("tgamma", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos); |
| 804 | /* Legacy BSD API; use `2*frexp( )` or `scalbn(x, -ilogb(x))` instead. */ |
| 805 | extern double significand(double) |
| 806 | __API_DEPRECATED("Use `2*frexp( )` or `scalbn(x, -ilogb(x))` instead.", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos); |
| 807 | #endif |
| 808 | |
| 809 | #if !defined __cplusplus |
| 810 | struct exception { |
| 811 | int type; |
| 812 | char *name; |
| 813 | double arg1; |
| 814 | double arg2; |
| 815 | double retval; |
| 816 | }; |
| 817 | |
| 818 | #endif /* !defined __cplusplus */ |
| 819 | #endif /* __DARWIN_C_LEVEL >= __DARWIN_C_FULL */ |
| 820 | |
| 821 | __END_DECLS |
| 822 | |
| 823 | #if __has_include(<realtime_safety/realtime_safety.h>) |
| 824 | REALTIME_SAFE_END |
| 825 | #endif |
| 826 | |
| 827 | #endif /* __MATH_H__ */ |
| 828 | |