1/* $NetBSD: math.h,v 1.65 2018/06/24 23:55:29 christos Exp $ */
2
3/*
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 *
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
10 * is preserved.
11 * ====================================================
12 */
13
14/*
15 * @(#)fdlibm.h 5.1 93/09/24
16 */
17
18#ifndef _MATH_H_
19#define _MATH_H_
20
21#include <sys/cdefs.h>
22#include <sys/featuretest.h>
23
24union __float_u {
25 unsigned char __dummy[sizeof(float)];
26 float __val;
27};
28
29union __double_u {
30 unsigned char __dummy[sizeof(double)];
31 double __val;
32};
33
34union __long_double_u {
35 unsigned char __dummy[sizeof(long double)];
36 long double __val;
37};
38
39#include <machine/math.h> /* may use __float_u, __double_u,
40 or __long_double_u */
41#include <limits.h> /* for INT_{MIN,MAX} */
42
43#if ((_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE))
44# if defined(__FLT_EVAL_METHOD__) && (__FLT_EVAL_METHOD__ - 0) == 0
45typedef double double_t;
46typedef float float_t;
47# elif (__FLT_EVAL_METHOD__ - 0) == 1
48typedef double double_t;
49typedef double float_t;
50# elif (__FLT_EVAL_METHOD__ - 0) == 2
51typedef long double double_t;
52typedef long double float_t;
53# endif
54#endif
55
56#ifdef __HAVE_LONG_DOUBLE
57#define __fpmacro_unary_floating(__name, __arg0) \
58 /* LINTED */ \
59 ((sizeof (__arg0) == sizeof (float)) \
60 ? __ ## __name ## f (__arg0) \
61 : (sizeof (__arg0) == sizeof (double)) \
62 ? __ ## __name ## d (__arg0) \
63 : __ ## __name ## l (__arg0))
64#else
65#define __fpmacro_unary_floating(__name, __arg0) \
66 /* LINTED */ \
67 ((sizeof (__arg0) == sizeof (float)) \
68 ? __ ## __name ## f (__arg0) \
69 : __ ## __name ## d (__arg0))
70#endif /* __HAVE_LONG_DOUBLE */
71
72/*
73 * ANSI/POSIX
74 */
75/* 7.12#3 HUGE_VAL, HUGELF, HUGE_VALL */
76#if __GNUC_PREREQ__(3, 3)
77#define HUGE_VAL __builtin_huge_val()
78#else
79extern const union __double_u __infinity;
80#define HUGE_VAL __infinity.__val
81#endif
82
83/*
84 * ISO C99
85 */
86#if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \
87 !defined(_XOPEN_SOURCE) || \
88 ((__STDC_VERSION__ - 0) >= 199901L) || \
89 ((_POSIX_C_SOURCE - 0) >= 200112L) || \
90 ((_XOPEN_SOURCE - 0) >= 600) || \
91 defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE)
92/* 7.12#3 HUGE_VAL, HUGELF, HUGE_VALL */
93#if __GNUC_PREREQ__(3, 3)
94#define HUGE_VALF __builtin_huge_valf()
95#define HUGE_VALL __builtin_huge_vall()
96#else
97extern const union __float_u __infinityf;
98#define HUGE_VALF __infinityf.__val
99
100extern const union __long_double_u __infinityl;
101#define HUGE_VALL __infinityl.__val
102#endif
103
104/* 7.12#4 INFINITY */
105#if defined(__INFINITY)
106#define INFINITY __INFINITY /* float constant which overflows */
107#elif __GNUC_PREREQ__(3, 3)
108#define INFINITY __builtin_inff()
109#else
110#define INFINITY HUGE_VALF /* positive infinity */
111#endif /* __INFINITY */
112
113/* 7.12#5 NAN: a quiet NaN, if supported */
114#ifdef __HAVE_NANF
115#if __GNUC_PREREQ__(3,3)
116#define NAN __builtin_nanf("")
117#else
118extern const union __float_u __nanf;
119#define NAN __nanf.__val
120#endif
121#endif /* __HAVE_NANF */
122
123/* 7.12#6 number classification macros */
124#define FP_INFINITE 0x00
125#define FP_NAN 0x01
126#define FP_NORMAL 0x02
127#define FP_SUBNORMAL 0x03
128#define FP_ZERO 0x04
129/* NetBSD extensions */
130#define _FP_LOMD 0x80 /* range for machine-specific classes */
131#define _FP_HIMD 0xff
132
133#define FP_ILOGB0 INT_MIN
134#define FP_ILOGBNAN INT_MAX
135
136#endif /* !_ANSI_SOURCE && ... */
137
138/*
139 * XOPEN/SVID
140 */
141#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
142#define M_E 2.7182818284590452354 /* e */
143#define M_LOG2E 1.4426950408889634074 /* log 2e */
144#define M_LOG10E 0.43429448190325182765 /* log 10e */
145#define M_LN2 0.69314718055994530942 /* log e2 */
146#define M_LN10 2.30258509299404568402 /* log e10 */
147#define M_PI 3.14159265358979323846 /* pi */
148#define M_PI_2 1.57079632679489661923 /* pi/2 */
149#define M_PI_4 0.78539816339744830962 /* pi/4 */
150#define M_1_PI 0.31830988618379067154 /* 1/pi */
151#define M_2_PI 0.63661977236758134308 /* 2/pi */
152#define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
153#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
154#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
155
156#define MAXFLOAT ((float)3.40282346638528860e+38)
157extern int signgam;
158#endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
159
160#if defined(_NETBSD_SOURCE)
161enum fdversion {fdlibm_ieee = -1, fdlibm_svid, fdlibm_xopen, fdlibm_posix};
162
163#define _LIB_VERSION_TYPE enum fdversion
164#define _LIB_VERSION _fdlib_version
165
166/* if global variable _LIB_VERSION is not desirable, one may
167 * change the following to be a constant by:
168 * #define _LIB_VERSION_TYPE const enum version
169 * In that case, after one initializes the value _LIB_VERSION (see
170 * s_lib_version.c) during compile time, it cannot be modified
171 * in the middle of a program
172 */
173extern _LIB_VERSION_TYPE _LIB_VERSION;
174
175#define _IEEE_ fdlibm_ieee
176#define _SVID_ fdlibm_svid
177#define _XOPEN_ fdlibm_xopen
178#define _POSIX_ fdlibm_posix
179
180#ifndef __cplusplus
181struct exception {
182 int type;
183 const char *name;
184 double arg1;
185 double arg2;
186 double retval;
187};
188#endif
189
190#define HUGE MAXFLOAT
191
192/*
193 * set X_TLOSS = pi*2**52, which is possibly defined in <values.h>
194 * (one may replace the following line by "#include <values.h>")
195 */
196
197#define X_TLOSS 1.41484755040568800000e+16
198
199#define DOMAIN 1
200#define SING 2
201#define OVERFLOW 3
202#define UNDERFLOW 4
203#define TLOSS 5
204#define PLOSS 6
205
206#endif /* _NETBSD_SOURCE */
207
208__BEGIN_DECLS
209/*
210 * ANSI/POSIX
211 */
212double acos(double);
213double asin(double);
214double atan(double);
215double atan2(double, double);
216double cos(double);
217double sin(double);
218double tan(double);
219
220double cosh(double);
221double sinh(double);
222double tanh(double);
223
224double exp(double);
225double exp2(double);
226double frexp(double, int *);
227double ldexp(double, int);
228double log(double);
229double log2(double);
230double log10(double);
231double modf(double, double *);
232
233double pow(double, double);
234double sqrt(double);
235
236double ceil(double);
237double fabs(double);
238double floor(double);
239double fmod(double, double);
240
241#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
242double erf(double);
243double erfc(double);
244double gamma(double);
245double hypot(double, double);
246int finite(double);
247double j0(double);
248double j1(double);
249double jn(int, double);
250double lgamma(double);
251double y0(double);
252double y1(double);
253double yn(int, double);
254
255#if (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
256double scalb(double, double);
257#endif /* (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)*/
258#endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
259
260/*
261 * ISO C99
262 */
263#if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \
264 !defined(_XOPEN_SOURCE) || \
265 ((__STDC_VERSION__ - 0) >= 199901L) || \
266 ((_POSIX_C_SOURCE - 0) >= 200809L) || \
267 ((_XOPEN_SOURCE - 0) >= 500) || \
268 defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE)
269double acosh(double);
270double asinh(double);
271double atanh(double);
272double cbrt(double);
273double expm1(double);
274int ilogb(double);
275double log1p(double);
276double logb(double);
277double nextafter(double, double);
278double remainder(double, double);
279double rint(double);
280#endif
281
282#if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \
283 !defined(_XOPEN_SOURCE) || \
284 ((__STDC_VERSION__ - 0) >= 199901L) || \
285 ((_POSIX_C_SOURCE - 0) >= 200112L) || \
286 ((_XOPEN_SOURCE - 0) >= 600) || \
287 defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE)
288/* 7.12.3.1 int fpclassify(real-floating x) */
289#define fpclassify(__x) __fpmacro_unary_floating(fpclassify, __x)
290
291/* 7.12.3.2 int isfinite(real-floating x) */
292#define isfinite(__x) __fpmacro_unary_floating(isfinite, __x)
293
294/* 7.12.3.5 int isnormal(real-floating x) */
295#define isnormal(__x) (fpclassify(__x) == FP_NORMAL)
296
297/* 7.12.3.6 int signbit(real-floating x) */
298#define signbit(__x) __fpmacro_unary_floating(signbit, __x)
299
300/* 7.12.4 trigonometric */
301
302float acosf(float);
303float asinf(float);
304float atanf(float);
305float atan2f(float, float);
306float cosf(float);
307float sinf(float);
308float tanf(float);
309
310long double acosl(long double);
311long double asinl(long double);
312long double atanl(long double);
313long double atan2l(long double, long double);
314long double cosl(long double);
315long double sinl(long double);
316long double tanl(long double);
317
318/* 7.12.5 hyperbolic */
319
320float acoshf(float);
321float asinhf(float);
322float atanhf(float);
323float coshf(float);
324float sinhf(float);
325float tanhf(float);
326long double acoshl(long double);
327long double asinhl(long double);
328long double atanhl(long double);
329long double coshl(long double);
330long double sinhl(long double);
331long double tanhl(long double);
332
333/* 7.12.6 exp / log */
334
335float expf(float);
336float exp2f(float);
337float expm1f(float);
338float frexpf(float, int *);
339int ilogbf(float);
340float ldexpf(float, int);
341float logf(float);
342float log2f(float);
343float log10f(float);
344float log1pf(float);
345float logbf(float);
346float modff(float, float *);
347float scalbnf(float, int);
348float scalblnf(float, long);
349
350long double expl(long double);
351long double exp2l(long double);
352long double expm1l(long double);
353long double frexpl(long double, int *);
354int ilogbl(long double);
355long double ldexpl(long double, int);
356long double logl(long double);
357long double log2l(long double);
358long double log10l(long double);
359long double log1pl(long double);
360long double logbl(long double);
361long double modfl(long double, long double *);
362long double scalbnl(long double, int);
363long double scalblnl(long double, long);
364
365
366/* 7.12.7 power / absolute */
367
368float cbrtf(float);
369float fabsf(float);
370float hypotf(float, float);
371float powf(float, float);
372float sqrtf(float);
373long double cbrtl(long double);
374long double fabsl(long double);
375long double hypotl(long double, long double);
376long double powl(long double, long double);
377long double sqrtl(long double);
378
379/* 7.12.8 error / gamma */
380
381double tgamma(double);
382float erff(float);
383float erfcf(float);
384float lgammaf(float);
385float tgammaf(float);
386long double erfl(long double);
387long double erfcl(long double);
388long double lgammal(long double);
389long double tgammal(long double);
390
391/* 7.12.9 nearest integer */
392
393/* LONGLONG */
394long long int llrint(double);
395long int lround(double);
396/* LONGLONG */
397long long int llround(double);
398long int lrint(double);
399double round(double);
400double trunc(double);
401
402float ceilf(float);
403float floorf(float);
404/* LONGLONG */
405long long int llrintf(float);
406long int lroundf(float);
407/* LONGLONG */
408long long int llroundf(float);
409long int lrintf(float);
410float rintf(float);
411float roundf(float);
412float truncf(float);
413long double ceill(long double);
414long double floorl(long double);
415/* LONGLONG */
416long long int llrintl(long double);
417long int lroundl(long double);
418/* LONGLONG */
419long long int llroundl(long double);
420long int lrintl(long double);
421long double rintl(long double);
422long double roundl(long double);
423long double truncl(long double);
424
425/* 7.12.10 remainder */
426
427float fmodf(float, float);
428float remainderf(float, float);
429long double fmodl(long double, long double);
430long double remainderl(long double, long double);
431
432/* 7.12.10.3 The remquo functions */
433double remquo(double, double, int *);
434float remquof(float, float, int *);
435long double remquol(long double, long double, int *);
436
437/* 7.12.11 manipulation */
438
439double nan(const char *);
440double nearbyint(double);
441double nexttoward(double, long double);
442float copysignf(float, float);
443float nanf(const char *);
444float nearbyintf(float);
445float nextafterf(float, float);
446float nexttowardf(float, long double);
447long double copysignl(long double, long double);
448long double nanl(const char *);
449long double nearbyintl(long double);
450long double nextafterl(long double, long double);
451long double nexttowardl(long double, long double);
452
453/* 7.12.14 comparison */
454
455#define isunordered(x, y) (isnan(x) || isnan(y))
456#define isgreater(x, y) (!isunordered((x), (y)) && (x) > (y))
457#define isgreaterequal(x, y) (!isunordered((x), (y)) && (x) >= (y))
458#define isless(x, y) (!isunordered((x), (y)) && (x) < (y))
459#define islessequal(x, y) (!isunordered((x), (y)) && (x) <= (y))
460#define islessgreater(x, y) (!isunordered((x), (y)) && \
461 ((x) > (y) || (y) > (x)))
462double fdim(double, double);
463double fma(double, double, double);
464double fmax(double, double);
465double fmin(double, double);
466float fdimf(float, float);
467float fmaf(float, float, float);
468float fmaxf(float, float);
469float fminf(float, float);
470long double fdiml(long double, long double);
471long double fmal(long double, long double, long double);
472long double fmaxl(long double, long double);
473long double fminl(long double, long double);
474
475#endif /* !_ANSI_SOURCE && ... */
476
477#if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) || \
478 !defined(_XOPEN_SOURCE) || \
479 ((__STDC_VERSION__ - 0) >= 199901L) || \
480 ((_POSIX_C_SOURCE - 0) >= 200112L) || \
481 defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE)
482/* 7.12.3.3 int isinf(real-floating x) */
483#if defined(__isinf) || defined(__HAVE_INLINE___ISINF)
484#define isinf(__x) __isinf(__x)
485#else
486#define isinf(__x) __fpmacro_unary_floating(isinf, __x)
487#endif
488
489/* 7.12.3.4 int isnan(real-floating x) */
490#if defined(__isnan) || defined(__HAVE_INLINE___ISNAN)
491#define isnan(__x) __isnan(__x)
492#else
493#define isnan(__x) __fpmacro_unary_floating(isnan, __x)
494#endif
495#endif /* !_ANSI_SOURCE && ... */
496
497#if defined(_NETBSD_SOURCE)
498#ifndef __cplusplus
499int matherr(struct exception *);
500#endif
501
502/*
503 * IEEE Test Vector
504 */
505double significand(double);
506
507/*
508 * Functions callable from C, intended to support IEEE arithmetic.
509 */
510double copysign(double, double);
511double scalbn(double, int);
512double scalbln(double, long);
513
514/*
515 * BSD math library entry points
516 */
517double drem(double, double);
518
519#endif /* _NETBSD_SOURCE */
520
521#if defined(_NETBSD_SOURCE) || defined(_REENTRANT)
522/*
523 * Reentrant version of gamma & lgamma; passes signgam back by reference
524 * as the second argument; user must allocate space for signgam.
525 */
526double gamma_r(double, int *);
527double lgamma_r(double, int *);
528#endif /* _NETBSD_SOURCE || _REENTRANT */
529
530
531#if defined(_NETBSD_SOURCE)
532
533/* float versions of ANSI/POSIX functions */
534
535float gammaf(float);
536int isinff(float);
537int isnanf(float);
538int finitef(float);
539float j0f(float);
540float j1f(float);
541float jnf(int, float);
542float y0f(float);
543float y1f(float);
544float ynf(int, float);
545
546float scalbf(float, float);
547
548/*
549 * float version of IEEE Test Vector
550 */
551float significandf(float);
552
553/*
554 * float versions of BSD math library entry points
555 */
556float dremf(float, float);
557#endif /* _NETBSD_SOURCE */
558
559#if defined(_NETBSD_SOURCE) || defined(_REENTRANT)
560/*
561 * Float versions of reentrant version of gamma & lgamma; passes
562 * signgam back by reference as the second argument; user must
563 * allocate space for signgam.
564 */
565float gammaf_r(float, int *);
566float lgammaf_r(float, int *);
567#endif /* !... || _REENTRANT */
568
569/*
570 * Library implementation
571 */
572int __fpclassifyf(float);
573int __fpclassifyd(double);
574int __isfinitef(float);
575int __isfinited(double);
576int __isinff(float);
577int __isinfd(double);
578int __isnanf(float);
579int __isnand(double);
580int __signbitf(float);
581int __signbitd(double);
582
583#ifdef __HAVE_LONG_DOUBLE
584int __fpclassifyl(long double);
585int __isfinitel(long double);
586int __isinfl(long double);
587int __isnanl(long double);
588int __signbitl(long double);
589#endif
590
591__END_DECLS
592
593#endif /* _MATH_H_ */
594