f9befc7f285d7535974a9f2cea5af0472c95d8bd
[dragonfly.git] / lib / libm / src / math.h
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11
12 /*
13  * from: @(#)fdlibm.h 5.1 93/09/24
14  * $FreeBSD: head/lib/msun/src/math.h 251599 2013-06-10 06:04:58Z das $
15  */
16
17 #ifndef _MATH_H_
18 #define _MATH_H_
19
20 #include <sys/cdefs.h>
21 #include <sys/types.h>
22 #include <machine/limits.h>
23
24 /*
25  * ANSI/POSIX
26  */
27 extern const union __infinity_un {
28         unsigned char   __uc[8];
29         double          __ud;
30 } __infinity;
31
32 extern const union __nan_un {
33         unsigned char   __uc[sizeof(float)];
34         float           __uf;
35 } __nan;
36
37 #if __GNUC_PREREQ__(3, 3) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 800)
38 #define __MATH_BUILTIN_CONSTANTS
39 #endif
40
41 #if __GNUC_PREREQ__(3, 0) && !defined(__INTEL_COMPILER)
42 #define __MATH_BUILTIN_RELOPS
43 #endif
44
45 #ifdef __MATH_BUILTIN_CONSTANTS
46 #define HUGE_VAL        __builtin_huge_val()
47 #else
48 #define HUGE_VAL        (__infinity.__ud)
49 #endif
50
51 #if __ISO_C_VISIBLE >= 1999
52 #define FP_ILOGB0       (-INT_MAX)
53 #define FP_ILOGBNAN     INT_MAX
54
55 #ifdef __MATH_BUILTIN_CONSTANTS
56 #define HUGE_VALF       __builtin_huge_valf()
57 #define HUGE_VALL       __builtin_huge_vall()
58 #define INFINITY        __builtin_inff()
59 #define NAN             __builtin_nanf("")
60 #else
61 #define HUGE_VALF       (float)HUGE_VAL
62 #define HUGE_VALL       (long double)HUGE_VAL
63 #define INFINITY        HUGE_VALF
64 #define NAN             (__nan.__uf)
65 #endif /* __MATH_BUILTIN_CONSTANTS */
66
67 #define MATH_ERRNO      1
68 #define MATH_ERREXCEPT  2
69 #define math_errhandling        MATH_ERREXCEPT
70
71 #define FP_FAST_FMAF    1
72
73 /* Symbolic constants to classify floating point numbers. */
74 #define FP_INFINITE     0x01
75 #define FP_NAN          0x02
76 #define FP_NORMAL       0x04
77 #define FP_SUBNORMAL    0x08
78 #define FP_ZERO         0x10
79 #define fpclassify(x) \
80     ((sizeof (x) == sizeof (float)) ? __fpclassifyf(x) \
81     : (sizeof (x) == sizeof (double)) ? __fpclassifyd(x) \
82     : __fpclassifyl(x))
83
84 #define isfinite(x)                                     \
85     ((sizeof (x) == sizeof (float)) ? __isfinitef(x)    \
86     : (sizeof (x) == sizeof (double)) ? __isfinite(x)   \
87     : __isfinitel(x))
88 #define isinf(x)                                        \
89     ((sizeof (x) == sizeof (float)) ? __isinff(x)       \
90     : (sizeof (x) == sizeof (double)) ? isinf(x)        \
91     : __isinfl(x))
92 #define isnan(x)                                        \
93     ((sizeof (x) == sizeof (float)) ? __isnanf(x)       \
94     : (sizeof (x) == sizeof (double)) ? isnan(x)        \
95     : __isnanl(x))
96 #define isnormal(x)                                     \
97     ((sizeof (x) == sizeof (float)) ? __isnormalf(x)    \
98     : (sizeof (x) == sizeof (double)) ? __isnormal(x)   \
99     : __isnormall(x))
100
101 #ifdef __MATH_BUILTIN_RELOPS
102 #define isgreater(x, y)         __builtin_isgreater((x), (y))
103 #define isgreaterequal(x, y)    __builtin_isgreaterequal((x), (y))
104 #define isless(x, y)            __builtin_isless((x), (y))
105 #define islessequal(x, y)       __builtin_islessequal((x), (y))
106 #define islessgreater(x, y)     __builtin_islessgreater((x), (y))
107 #define isunordered(x, y)       __builtin_isunordered((x), (y))
108 #else
109 #define isgreater(x, y)         (!isunordered((x), (y)) && (x) > (y))
110 #define isgreaterequal(x, y)    (!isunordered((x), (y)) && (x) >= (y))
111 #define isless(x, y)            (!isunordered((x), (y)) && (x) < (y))
112 #define islessequal(x, y)       (!isunordered((x), (y)) && (x) <= (y))
113 #define islessgreater(x, y)     (!isunordered((x), (y)) && \
114                                         ((x) > (y) || (y) > (x)))
115 #define isunordered(x, y)       (isnan(x) || isnan(y))
116 #endif /* __MATH_BUILTIN_RELOPS */
117
118 #define signbit(x)                                      \
119     ((sizeof (x) == sizeof (float)) ? __signbitf(x)     \
120     : (sizeof (x) == sizeof (double)) ? __signbit(x)    \
121     : __signbitl(x))
122
123 #ifdef __LP64__
124 typedef double          double_t;
125 typedef float           float_t;
126 #else
127 typedef long double     double_t;
128 typedef long double     float_t;
129 #endif /* __LP64__ */
130 #endif /* __ISO_C_VISIBLE >= 1999 */
131
132 /*
133  * XOPEN/SVID
134  */
135 #if __BSD_VISIBLE || __XSI_VISIBLE
136 #define M_E             2.7182818284590452354   /* e */
137 #define M_LOG2E         1.4426950408889634074   /* log 2e */
138 #define M_LOG10E        0.43429448190325182765  /* log 10e */
139 #define M_LN2           0.69314718055994530942  /* log e2 */
140 #define M_LN10          2.30258509299404568402  /* log e10 */
141 #define M_PI            3.14159265358979323846  /* pi */
142 #define M_PI_2          1.57079632679489661923  /* pi/2 */
143 #define M_PI_4          0.78539816339744830962  /* pi/4 */
144 #define M_1_PI          0.31830988618379067154  /* 1/pi */
145 #define M_2_PI          0.63661977236758134308  /* 2/pi */
146 #define M_2_SQRTPI      1.12837916709551257390  /* 2/sqrt(pi) */
147 #define M_SQRT2         1.41421356237309504880  /* sqrt(2) */
148 #define M_SQRT1_2       0.70710678118654752440  /* 1/sqrt(2) */
149
150 #define MAXFLOAT        ((float)3.40282346638528860e+38)
151 extern int signgam;
152 #endif /* __BSD_VISIBLE || __XSI_VISIBLE */
153
154 #if __BSD_VISIBLE
155 #if 0
156 /* Old value from 4.4BSD-Lite math.h; this is probably better. */
157 #define HUGE            HUGE_VAL
158 #else
159 #define HUGE            MAXFLOAT
160 #endif
161 #endif /* __BSD_VISIBLE */
162
163 /*
164  * Most of these functions depend on the rounding mode and have the side
165  * effect of raising floating-point exceptions, so they are not declared
166  * as __pure2.  In C99, FENV_ACCESS affects the purity of these functions.
167  */
168 __BEGIN_DECLS
169 /*
170  * ANSI/POSIX
171  */
172 int     __fpclassifyd(double) __pure2;
173 int     __fpclassifyf(float) __pure2;
174 int     __fpclassifyl(long double) __pure2;
175 int     __isfinitef(float) __pure2;
176 int     __isfinite(double) __pure2;
177 int     __isfinitel(long double) __pure2;
178 int     __isinff(float) __pure2;
179 int     __isinfl(long double) __pure2;
180 int     __isnanf(float) __pure2;
181 int     __isnanl(long double) __pure2;
182 int     __isnormalf(float) __pure2;
183 int     __isnormal(double) __pure2;
184 int     __isnormall(long double) __pure2;
185 int     __signbit(double) __pure2;
186 int     __signbitf(float) __pure2;
187 int     __signbitl(long double) __pure2;
188
189 double  acos(double);
190 double  asin(double);
191 double  atan(double);
192 double  atan2(double, double);
193 double  cos(double);
194 double  sin(double);
195 double  tan(double);
196
197 double  cosh(double);
198 double  sinh(double);
199 double  tanh(double);
200
201 double  exp(double);
202 double  frexp(double, int *);   /* fundamentally !__pure2 */
203 double  ldexp(double, int);
204 double  log(double);
205 double  log10(double);
206 double  modf(double, double *); /* fundamentally !__pure2 */
207
208 double  pow(double, double);
209 double  sqrt(double);
210
211 double  ceil(double);
212 double  fabs(double) __pure2;
213 double  floor(double);
214 double  fmod(double, double);
215
216 /*
217  * These functions are not in C90.
218  */
219 #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE
220 double  acosh(double);
221 double  asinh(double);
222 double  atanh(double);
223 double  cbrt(double);
224 double  erf(double);
225 double  erfc(double);
226 double  exp2(double);
227 double  expm1(double);
228 double  fma(double, double, double);
229 double  hypot(double, double);
230 int     ilogb(double) __pure2;
231 int     (isinf)(double) __pure2;
232 int     (isnan)(double) __pure2;
233 double  lgamma(double);
234 long long llrint(double);
235 long long llround(double);
236 double  log1p(double);
237 double  log2(double);
238 double  logb(double);
239 long    lrint(double);
240 long    lround(double);
241 double  nan(const char *) __pure2;
242 double  nextafter(double, double);
243 double  remainder(double, double);
244 double  remquo(double, double, int *);
245 double  rint(double);
246 #endif /* __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE */
247
248 #if __BSD_VISIBLE || __XSI_VISIBLE
249 double  j0(double);
250 double  j1(double);
251 double  jn(int, double);
252 double  y0(double);
253 double  y1(double);
254 double  yn(int, double);
255
256 #if __XSI_VISIBLE <= 500 || __BSD_VISIBLE
257 double  gamma(double);
258 #endif
259
260 #if __XSI_VISIBLE <= 600 || __BSD_VISIBLE
261 double  scalb(double, double);
262 #endif
263 #endif /* __BSD_VISIBLE || __XSI_VISIBLE */
264
265 #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999
266 double  copysign(double, double) __pure2;
267 double  fdim(double, double);
268 double  fmax(double, double) __pure2;
269 double  fmin(double, double) __pure2;
270 double  nearbyint(double);
271 double  round(double);
272 double  scalbln(double, long);
273 double  scalbn(double, int);
274 double  tgamma(double);
275 double  trunc(double);
276 #endif
277
278 /*
279  * BSD math library entry points
280  */
281 #if __BSD_VISIBLE
282 double  drem(double, double);
283 int     finite(double) __pure2;
284 int     isnanf(float) __pure2;
285
286 /*
287  * Reentrant version of gamma & lgamma; passes signgam back by reference
288  * as the second argument; user must allocate space for signgam.
289  */
290 double  gamma_r(double, int *);
291 double  lgamma_r(double, int *);
292
293 /*
294  * IEEE Test Vector
295  */
296 double  significand(double);
297 #endif /* __BSD_VISIBLE */
298
299 /* float versions of ANSI/POSIX functions */
300 #if __ISO_C_VISIBLE >= 1999
301 float   acosf(float);
302 float   asinf(float);
303 float   atanf(float);
304 float   atan2f(float, float);
305 float   cosf(float);
306 float   sinf(float);
307 float   tanf(float);
308
309 float   coshf(float);
310 float   sinhf(float);
311 float   tanhf(float);
312
313 float   exp2f(float);
314 float   expf(float);
315 float   expm1f(float);
316 float   frexpf(float, int *);   /* fundamentally !__pure2 */
317 int     ilogbf(float) __pure2;
318 float   ldexpf(float, int);
319 float   log10f(float);
320 float   log1pf(float);
321 float   log2f(float);
322 float   logf(float);
323 float   modff(float, float *);  /* fundamentally !__pure2 */
324
325 float   powf(float, float);
326 float   sqrtf(float);
327
328 float   ceilf(float);
329 float   fabsf(float) __pure2;
330 float   floorf(float);
331 float   fmodf(float, float);
332 float   roundf(float);
333
334 float   erff(float);
335 float   erfcf(float);
336 float   hypotf(float, float);
337 float   lgammaf(float);
338 float   tgammaf(float);
339
340 float   acoshf(float);
341 float   asinhf(float);
342 float   atanhf(float);
343 float   cbrtf(float);
344 float   logbf(float);
345 float   copysignf(float, float) __pure2;
346 long long llrintf(float);
347 long long llroundf(float);
348 long    lrintf(float);
349 long    lroundf(float);
350 float   nanf(const char *) __pure2;
351 float   nearbyintf(float);
352 float   nextafterf(float, float);
353 float   remainderf(float, float);
354 float   remquof(float, float, int *);
355 float   rintf(float);
356 float   scalblnf(float, long);
357 float   scalbnf(float, int);
358 float   truncf(float);
359
360 float   fdimf(float, float);
361 float   fmaf(float, float, float);
362 float   fmaxf(float, float) __pure2;
363 float   fminf(float, float) __pure2;
364 #endif
365
366 /*
367  * float versions of BSD math library entry points
368  */
369 #if __BSD_VISIBLE
370 float   dremf(float, float);
371 int     finitef(float) __pure2;
372 float   gammaf(float);
373 float   j0f(float);
374 float   j1f(float);
375 float   jnf(int, float);
376 float   scalbf(float, float);
377 float   y0f(float);
378 float   y1f(float);
379 float   ynf(int, float);
380
381 /*
382  * Float versions of reentrant version of gamma & lgamma; passes
383  * signgam back by reference as the second argument; user must
384  * allocate space for signgam.
385  */
386 float   gammaf_r(float, int *);
387 float   lgammaf_r(float, int *);
388
389 /*
390  * float version of IEEE Test Vector
391  */
392 float   significandf(float);
393 #endif  /* __BSD_VISIBLE */
394
395 /*
396  * long double versions of ISO/POSIX math functions
397  */
398 #if __ISO_C_VISIBLE >= 1999
399 long double     acoshl(long double);
400 long double     acosl(long double);
401 long double     asinhl(long double);
402 long double     asinl(long double);
403 long double     atan2l(long double, long double);
404 long double     atanhl(long double);
405 long double     atanl(long double);
406 long double     cbrtl(long double);
407 long double     ceill(long double);
408 long double     copysignl(long double, long double) __pure2;
409 long double     cosl(long double);
410 long double     exp2l(long double);
411 long double     expl(long double);
412 long double     expm1l(long double);
413 long double     fabsl(long double) __pure2;
414 long double     fdiml(long double, long double);
415 long double     floorl(long double);
416 long double     fmal(long double, long double, long double);
417 long double     fmaxl(long double, long double) __pure2;
418 long double     fminl(long double, long double) __pure2;
419 long double     fmodl(long double, long double);
420 long double     frexpl(long double value, int *); /* fundamentally !__pure2 */
421 long double     hypotl(long double, long double);
422 int             ilogbl(long double) __pure2;
423 long double     ldexpl(long double, int);
424 long long       llrintl(long double);
425 long long       llroundl(long double);
426 long double     log10l(long double);
427 long double     log1pl(long double);
428 long double     log2l(long double);
429 long double     logbl(long double);
430 long double     logl(long double);
431 long            lrintl(long double);
432 long            lroundl(long double);
433 long double     modfl(long double, long double *); /* fundamentally !__pure2 */
434 long double     nanl(const char *) __pure2;
435 long double     nearbyintl(long double);
436 long double     nextafterl(long double, long double);
437 double          nexttoward(double, long double);
438 float           nexttowardf(float, long double);
439 long double     nexttowardl(long double, long double);
440 long double     remainderl(long double, long double);
441 long double     remquol(long double, long double, int *);
442 long double     rintl(long double);
443 long double     roundl(long double);
444 long double     scalblnl(long double, long);
445 long double     scalbnl(long double, int);
446 long double     sinl(long double);
447 long double     sqrtl(long double);
448 long double     tanl(long double);
449 long double     truncl(long double);
450
451 #endif /* __ISO_C_VISIBLE >= 1999 */
452 __END_DECLS
453
454 #endif /* !_MATH_H_ */
455
456 /* separate header for cmath */
457 #ifndef _MATH_EXTRA_H_
458 #if __ISO_C_VISIBLE >= 1999
459 #if _DECLARE_C99_LDBL_MATH
460
461 #define _MATH_EXTRA_H_
462
463 /*
464  * extra long double versions of math functions for C99 and cmath
465  */
466 __BEGIN_DECLS
467
468 long double     coshl(long double);
469 long double     erfcl(long double);
470 long double     erfl(long double);
471 long double     lgammal(long double);
472 long double     powl(long double, long double);
473 long double     sinhl(long double);
474 long double     tanhl(long double);
475 long double     tgammal(long double);
476
477 __END_DECLS
478
479 #endif /* !_DECLARE_C99_LDBL_MATH */
480 #endif /* __ISO_C_VISIBLE >= 1999 */
481 #endif /* !_MATH_EXTRA_H_ */