master
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>
36REALTIME_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())
117extern 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
145extern int __fpclassifyf(float);
146extern int __fpclassifyd(double);
147extern 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
308extern int __isnormalf(float);
309extern int __isnormald(double);
310extern int __isnormall(long double);
311extern int __isfinitef(float);
312extern int __isfinited(double);
313extern int __isfinitel(long double);
314extern int __isinff(float);
315extern int __isinfd(double);
316extern int __isinfl(long double);
317extern int __isnanf(float);
318extern int __isnand(double);
319extern int __isnanl(long double);
320extern int __signbitf(float);
321extern int __signbitd(double);
322extern int __signbitl(long double);
323
324#endif /* defined(__GNUC__) && 0 == __FINITE_MATH_ONLY__ */
325
326/******************************************************************************
327 * *
328 * Math Functions *
329 * *
330 ******************************************************************************/
331
332extern float acosf(float);
333extern double acos(double);
334extern long double acosl(long double);
335
336extern float asinf(float);
337extern double asin(double);
338extern long double asinl(long double);
339
340extern float atanf(float);
341extern double atan(double);
342extern long double atanl(long double);
343
344extern float atan2f(float, float);
345extern double atan2(double, double);
346extern long double atan2l(long double, long double);
347
348extern float cosf(float);
349extern double cos(double);
350extern long double cosl(long double);
351
352extern float sinf(float);
353extern double sin(double);
354extern long double sinl(long double);
355
356extern float tanf(float);
357extern double tan(double);
358extern long double tanl(long double);
359
360extern float acoshf(float);
361extern double acosh(double);
362extern long double acoshl(long double);
363
364extern float asinhf(float);
365extern double asinh(double);
366extern long double asinhl(long double);
367
368extern float atanhf(float);
369extern double atanh(double);
370extern long double atanhl(long double);
371
372extern float coshf(float);
373extern double cosh(double);
374extern long double coshl(long double);
375
376extern float sinhf(float);
377extern double sinh(double);
378extern long double sinhl(long double);
379
380extern float tanhf(float);
381extern double tanh(double);
382extern long double tanhl(long double);
383
384extern float expf(float);
385extern double exp(double);
386extern long double expl(long double);
387
388extern float exp2f(float);
389extern double exp2(double);
390extern long double exp2l(long double);
391
392extern float expm1f(float);
393extern double expm1(double);
394extern long double expm1l(long double);
395
396extern float logf(float);
397extern double log(double);
398extern long double logl(long double);
399
400extern float log10f(float);
401extern double log10(double);
402extern long double log10l(long double);
403
404extern float log2f(float);
405extern double log2(double);
406extern long double log2l(long double);
407
408extern float log1pf(float);
409extern double log1p(double);
410extern long double log1pl(long double);
411
412extern float logbf(float);
413extern double logb(double);
414extern long double logbl(long double);
415
416extern float modff(float, float *);
417extern double modf(double, double *);
418extern long double modfl(long double, long double *);
419
420extern float ldexpf(float, int);
421extern double ldexp(double, int);
422extern long double ldexpl(long double, int);
423
424extern float frexpf(float, int *);
425extern double frexp(double, int *);
426extern long double frexpl(long double, int *);
427
428extern int ilogbf(float);
429extern int ilogb(double);
430extern int ilogbl(long double);
431
432extern float scalbnf(float, int);
433extern double scalbn(double, int);
434extern long double scalbnl(long double, int);
435
436extern float scalblnf(float, long int);
437extern double scalbln(double, long int);
438extern long double scalblnl(long double, long int);
439
440extern float fabsf(float);
441extern double fabs(double);
442extern long double fabsl(long double);
443
444extern float cbrtf(float);
445extern double cbrt(double);
446extern long double cbrtl(long double);
447
448extern float hypotf(float, float);
449extern double hypot(double, double);
450extern long double hypotl(long double, long double);
451
452extern float powf(float, float);
453extern double pow(double, double);
454extern long double powl(long double, long double);
455
456extern float sqrtf(float);
457extern double sqrt(double);
458extern long double sqrtl(long double);
459
460extern float erff(float);
461extern double erf(double);
462extern long double erfl(long double);
463
464extern float erfcf(float);
465extern double erfc(double);
466extern 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> */
471extern float lgammaf(float);
472extern double lgamma(double);
473extern long double lgammal(long double);
474
475extern float tgammaf(float);
476extern double tgamma(double);
477extern long double tgammal(long double);
478
479extern float ceilf(float);
480extern double ceil(double);
481extern long double ceill(long double);
482
483extern float floorf(float);
484extern double floor(double);
485extern long double floorl(long double);
486
487extern float nearbyintf(float);
488extern double nearbyint(double);
489extern long double nearbyintl(long double);
490
491extern float rintf(float);
492extern double rint(double);
493extern long double rintl(long double);
494
495extern long int lrintf(float);
496extern long int lrint(double);
497extern long int lrintl(long double);
498
499extern float roundf(float);
500extern double round(double);
501extern long double roundl(long double);
502
503extern long int lroundf(float);
504extern long int lround(double);
505extern 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)
510extern long long int llrintf(float);
511extern long long int llrint(double);
512extern long long int llrintl(long double);
513
514extern long long int llroundf(float);
515extern long long int llround(double);
516extern long long int llroundl(long double);
517#endif /* !(__DARWIN_NO_LONG_LONG) */
518
519extern float truncf(float);
520extern double trunc(double);
521extern long double truncl(long double);
522
523extern float fmodf(float, float);
524extern double fmod(double, double);
525extern long double fmodl(long double, long double);
526
527extern float remainderf(float, float);
528extern double remainder(double, double);
529extern long double remainderl(long double, long double);
530
531extern float remquof(float, float, int *);
532extern double remquo(double, double, int *);
533extern long double remquol(long double, long double, int *);
534
535extern float copysignf(float, float);
536extern double copysign(double, double);
537extern long double copysignl(long double, long double);
538
539extern float nanf(const char *);
540extern double nan(const char *);
541extern long double nanl(const char *);
542
543extern float nextafterf(float, float);
544extern double nextafter(double, double);
545extern long double nextafterl(long double, long double);
546
547extern double nexttoward(double, long double);
548extern float nexttowardf(float, long double);
549extern long double nexttowardl(long double, long double);
550
551extern float fdimf(float, float);
552extern double fdim(double, double);
553extern long double fdiml(long double, long double);
554
555extern float fmaxf(float, float);
556extern double fmax(double, double);
557extern long double fmaxl(long double, long double);
558
559extern float fminf(float, float);
560extern double fmin(double, double);
561extern long double fminl(long double, long double);
562
563extern float fmaf(float, float, float);
564extern double fma(double, double, double);
565extern 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. */
584extern float __inff(void)
585__API_DEPRECATED("use `(float)INFINITY` instead", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos);
586extern double __inf(void)
587__API_DEPRECATED("use `INFINITY` instead", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos);
588extern long double __infl(void)
589__API_DEPRECATED("use `(long double)INFINITY` instead", macos(10.0, 10.9)) __API_UNAVAILABLE(ios, watchos, tvos);
590extern 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. */
600extern float lgammaf_r(float, int *) __API_AVAILABLE(macos(10.6), ios(3.1));
601extern double lgamma_r(double, int *) __API_AVAILABLE(macos(10.6), ios(3.1));
602extern 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( ). */
620extern float __exp10f(float) __API_AVAILABLE(macos(10.9), ios(7.0));
621extern 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. */
637extern float __cospif(float) __API_AVAILABLE(macos(10.9), ios(7.0));
638extern double __cospi(double) __API_AVAILABLE(macos(10.9), ios(7.0));
639extern float __sinpif(float) __API_AVAILABLE(macos(10.9), ios(7.0));
640extern double __sinpi(double) __API_AVAILABLE(macos(10.9), ios(7.0));
641extern float __tanpif(float) __API_AVAILABLE(macos(10.9), ios(7.0));
642extern double __tanpi(double) __API_AVAILABLE(macos(10.9), ios(7.0));
643
644/* half precision math functions */
645extern _Float16 __fabsf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
646extern _Float16 __hypotf16(_Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
647extern _Float16 __sqrtf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
648extern _Float16 __ceilf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
649extern _Float16 __floorf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
650extern _Float16 __rintf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
651extern _Float16 __roundf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
652extern _Float16 __truncf16(_Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
653extern _Float16 __copysignf16(_Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
654extern _Float16 __nextafterf16(_Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
655extern _Float16 __fmaxf16(_Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
656extern _Float16 __fminf16(_Float16, _Float16) __API_AVAILABLE(macos(15.0), ios(18.0), watchos(11.0), tvos(18.0));
657extern _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. */
691struct __float2 { float __sinval; float __cosval; };
692struct __double2 { double __sinval; double __cosval; };
693
694extern struct __float2 __sincosf_stret(float);
695extern struct __double2 __sincos_stret(double);
696extern struct __float2 __sincospif_stret(float);
697extern 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
725extern double j0(double) __API_AVAILABLE(macos(10.0), ios(3.2));
726extern double j1(double) __API_AVAILABLE(macos(10.0), ios(3.2));
727extern double jn(int, double) __API_AVAILABLE(macos(10.0), ios(3.2));
728extern double y0(double) __API_AVAILABLE(macos(10.0), ios(3.2));
729extern double y1(double) __API_AVAILABLE(macos(10.0), ios(3.2));
730extern double yn(int, double) __API_AVAILABLE(macos(10.0), ios(3.2));
731extern double scalb(double, double);
732extern 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. */
790extern 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. */
793extern 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. */
796extern 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. */
799extern 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. */
802extern 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. */
805extern 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
810struct 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>)
824REALTIME_SAFE_END
825#endif
826
827#endif /* __MATH_H__ */
828