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