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