2 Floating-point Math functions and macros.
4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 * ====================================================
14 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
16 * Developed at SunPro, a Sun Microsystems, Inc. business.
17 * Permission to use, copy, modify, and distribute this
18 * software is freely granted, provided that this notice
20 * ====================================================
22 NetBSD: math.h,v 1.44 2006/03/25 16:41:11 xtraeme Exp
28 #include <sys/EfiCdefs.h>
29 #include <sys/featuretest.h>
32 @brief These are forward references to unions and macros used internaly
33 by the implementation of the math functions and macros.
36 unsigned char __dummy
[sizeof(float)];
41 unsigned char __dummy
[sizeof(double)];
45 union __long_double_u
{
46 unsigned char __dummy
[sizeof(long double)];
50 #include <machine/math.h> /* may use __float_u, __double_u, or __long_double_u */
52 #ifdef __HAVE_LONG_DOUBLE
53 #define __fpmacro_unary_floating(__name, __arg0) \
55 ((sizeof (__arg0) == sizeof (float)) \
56 ? __ ## __name ## f (__arg0) \
57 : (sizeof (__arg0) == sizeof (double)) \
58 ? __ ## __name ## d (__arg0) \
59 : __ ## __name ## l (__arg0))
61 #define __fpmacro_unary_floating(__name, __arg0) \
63 ((sizeof (__arg0) == sizeof (float)) \
64 ? __ ## __name ## f (__arg0) \
65 : __ ## __name ## d (__arg0))
66 #endif /* __HAVE_LONG_DOUBLE */
68 extern const union __double_u __infinity
;
69 extern const union __float_u __infinityf
;
70 extern const union __long_double_u __infinityl
;
72 /* C99 7.12.3.1 int fpclassify(real-floating x) */
73 #define fpclassify(__x) __fpmacro_unary_floating(fpclassify, __x)
75 /* C99 7.12.3.3 int isinf(real-floating x) */
77 #define isinf(__x) __isinf(__x)
79 #define isinf(__x) __fpmacro_unary_floating(isinf, __x)
82 /* C99 7.12.3.4 int isnan(real-floating x) */
84 #define isnan(__x) __isnan(__x)
86 #define isnan(__x) __fpmacro_unary_floating(isnan, __x)
90 /*#############################################################
95 Double, float, and long double versions, respectively, of HUGE_VAL.
97 #define HUGE_VAL __infinity.__val
98 #define HUGE_VALF __infinityf.__val
99 #define HUGE_VALL __infinityl.__val
106 /** Compute the principal value of the arc cosine of Arg.
108 @param[in] Arg The value to compute the arc cosine of.
110 @return The computed value of the arc cosine of Arg in the interval [0,pi] radians.
111 If Arg is not in the interval [-1,+1], errno is set to EDOM.
113 double acos(double Arg
);
115 /** Compute the principal value of the arc sine of Arg.
117 @param[in] Arg The value to compute the arc sine of.
119 @return The computed value of the arc sine of Arg in the interval [-pi/2,+pi/2] radians.
120 If Arg is not in the interval [-1,+1], errno is set to EDOM.
122 double asin(double Arg
);
124 /** Compute the principal value of the arc tangent of Arg.
126 @param[in] Arg The value to compute the arc tangent of.
128 @return The computed value of the arc tangent of Arg in the interval [-pi/2,+pi/2] radians.
130 double atan(double Arg
);
132 /** Compute the value of the arc tangent of (Num / Denom).
133 The sign of both arguments is used to determine the quadrant of the return value.
135 @param[in] Num The numerator of the value to compute the arc tangent of.
136 @param[in] Denom The denominator of the value to compute the arc tangent of.
138 @return The computed value of the arc tangent of (Num / Denom) in the interval [-pi,+pi] radians.
140 double atan2(double Num
, double Denom
);
142 /** Compute the value of the cosine of Arg, measured in radians.
144 @param[in] Arg The value to compute the cosine of.
146 @return The computed value of the cosine of Arg.
148 double cos(double Arg
);
150 /** Compute the value of the sine of Arg.
152 @param[in] Arg The value to compute the sine of.
154 @return The computed value of the sine of Arg.
156 double sin(double Arg
);
158 /** Compute the value of the tangent of Arg.
160 @param[in] Arg The value to compute the tangent of.
162 @return The computed value of the tangent of Arg.
164 double tan(double Arg
);
167 /** Compute the value of the hyperbolic cosine of Arg.
169 @param[in] Arg The value to compute the hyperbolic cosine of.
171 @return The computed value of the hyperbolic cosine of Arg.
172 If the magnitude of Arg is too large, errno is set to ERANGE.
174 double cosh(double Arg
);
176 /** Compute the value of the hyperbolic sine of Arg.
178 @param[in] Arg The value to compute the hyperbolic sine of.
180 @return The computed value of the hyperbolic sine of Arg.
181 If the magnitude of Arg is too large, errno is set to ERANGE.
183 double sinh(double Arg
);
185 /** Compute the value of the hyperbolic tangent of Arg.
187 @param[in] Arg The value to compute the hyperbolic tangent of.
189 @return The computed value of the hyperbolic tangent of Arg.
191 double tanh(double Arg
);
194 /** Compute the base-e exponential of Arg.
196 @param[in] Arg The value to compute the base-e exponential of.
198 @return The computed value of e**Arg.
199 If the magnitude of Arg is too large, errno is set to ERANGE.
201 double exp(double Arg
);
203 /** Break a floating-point number into a normalized fraction and an integral power of 2.
205 @param[in] Value The floating-point value to be broken down.
206 @param[out] Exp A pointer to an integer object to receive the integral power of 2 exponent.
208 @return The frexp function returns a value R, such that Value == R**Exp.
209 If Value is zero, both parts of the result are zero.
211 double frexp(double Value
, int *Exp
);
213 /** Multiply a floating-point number, Value, by an integral power of 2, Exp.
215 @param[in] Value The floating-point value to be multiplied.
216 @param[out] Exp The integral power of 2 to multiply Value by.
218 @return The ldexp function returns a value R, such that R = Value x 2**Exp.
219 If a range error occurs, errno will be set to ERANGE.
221 double ldexp(double Value
, int Exp
);
223 /** Compute the natural logarithm of Arg.
225 @param[in] Arg The value to compute the natural logarithm of.
227 @return The log function returns log base-e of Arg. If Arg is negative, errno is set to EDOM.
228 Otherwise, errno will be set to ERANGE if a range error occurs.
230 double log(double Arg
);
232 /** Compute the common (base-10) logarithm of Arg.
234 @param[in] Arg The value to compute the common logarithm of.
236 @return The log10 function returns log base-10 of Arg. If Arg is negative, errno is set to EDOM.
237 Otherwise, errno will be set to ERANGE if Arg is 0.
239 double log10(double Arg
);
241 /** Compute the base-2 logarithm of Arg.
243 @param[in] Arg The value to compute the base-2 logarithm of.
245 @return The log function returns log base-2 of Arg. If Arg is negative, errno is set to EDOM.
246 Otherwise, errno will be set to ERANGE if Arg is 0.
248 double log2(double Arg
);
250 /** Break Value into integral and fractional parts, each of which has the same type and sign
251 as Value. Store the integral part in the object pointed to by Integ and return the
254 @param[in] Value The value to compute the arc cosine of.
255 @param[out] Integ Pointer to where the integral component is to be stored.
257 @return The fractional part of Value is returned directly while the integral part is
258 returned in the location pointed to by Integ.
260 double modf(double Value
, double *Integ
);
262 /** Compute Value raised to the power Exp.
264 @param[in] Value The value to be raised.
265 @param[in] Exp The power Value is to be raised to.
267 @return The pow function returns Value**Exp. If an error occurs, errno will be set as follows:
268 - EDOM: Value is finite and negative and Exp is finite and not an integer.
269 - EDOM: Both Value and Exp are zero.
270 - EDOM: Value is zero and Exp is less than zero.
272 double pow(double Value
, double Exp
);
274 /** Compute the non-negative square root of Arg.
276 @param[in] Arg The value to compute the square root of.
278 @return The square root of Arg. If Arg is less than zero, errno is set to EDOM.
280 double sqrt(double Arg
);
283 /** Compute the smallest integer value not less than Arg.
285 @param[in] Arg The value to compute the ceiling of.
287 @return The ceiling of Arg expressed as a floating-point number.
289 double ceil(double Arg
);
291 /** Compute the absolute value of Arg.
293 @param[in] Arg The value to compute the absolute value of.
295 @return The absolute value of Arg.
297 double fabs(double Arg
);
299 /** Compute the largest integer value not greater than Arg.
301 @param[in] Arg The value to compute the floor of.
303 @return The largest integer value not greater than Arg, expressed as a floating-point number.
305 double floor(double);
307 /** Compute the floating-point remainder of A1 / A2.
309 @param[in] A1 The dividend.
310 @param[in] A2 The divisor.
312 @return The remainder of A1 / A2 with the same sign as A1. If A2 is zero, the fmod function
315 double fmod(double A1
, double A2
);
319 double expm1(double);
322 C99, Posix, or NetBSD functions that are not part of the C95 specification.
325 * Functions callable from C, intended to support IEEE arithmetic.
327 double copysign(double, double);
328 double scalbn(double, int);
331 * Library implementation
333 int __fpclassifyf(float);
334 int __fpclassifyd(double);
336 int __isinfd(double);
338 int __isnand(double);
340 #ifdef __HAVE_LONG_DOUBLE
341 int __fpclassifyl(long double);
342 int __isinfl(long double);
343 int __isnanl(long double);
344 #endif /* __HAVE_LONG_DOUBLE */
350 Extensions provided by NetBSD but not required by the C95 standard.
354 enum fdversion
{fdlibm_ieee
= -1, fdlibm_svid
, fdlibm_xopen
, fdlibm_posix
};
356 #define _LIB_VERSION_TYPE enum fdversion
357 #define _LIB_VERSION _fdlib_version
359 /** If global variable _LIB_VERSION is not desirable, one may
360 * change the following to be a constant by:
361 * #define _LIB_VERSION_TYPE const enum version
362 * In that case, after one initializes the value _LIB_VERSION (see
363 * s_lib_version.c) during compile time, it cannot be modified
364 * in the middle of a program
366 extern _LIB_VERSION_TYPE _LIB_VERSION
;
368 #define _IEEE_ fdlibm_ieee
369 #define _SVID_ fdlibm_svid
370 #define _XOPEN_ fdlibm_xopen
371 #define _POSIX_ fdlibm_posix
383 #define HUGE MAXFLOAT
385 /** set X_TLOSS = pi*2**52 **/
386 #define X_TLOSS 1.41484755040568800000e+16
396 /* 7.12#4 INFINITY */
398 #define INFINITY __INFINITY /**< float constant which overflows */
400 #define INFINITY HUGE_VALF /**< positive infinity */
401 #endif /* __INFINITY */
403 /* 7.12#5 NAN: a quiet NaN, if supported */
405 extern const union __float_u __nanf
;
406 #define NAN __nanf.__val
407 #endif /* __HAVE_NANF */
410 C99 7.12#6 Number classification macros represent mutually exclusive kinds of floating-point
413 #define FP_INFINITE 0x00
415 #define FP_NORMAL 0x02
416 #define FP_SUBNORMAL 0x03
418 /* NetBSD extensions */
419 #define _FP_LOMD 0x80 /**< range for machine-specific classes */
420 #define _FP_HIMD 0xff
424 * Constants ala XOPEN/SVID.
426 #define M_E 2.7182818284590452354 /**< e */
427 #define M_LOG2E 1.4426950408889634074 /**< log 2e */
428 #define M_LOG10E 0.43429448190325182765 /**< log 10e */
429 #define M_LN2 0.69314718055994530942 /**< log e2 */
430 #define M_LN10 2.30258509299404568402 /**< log e10 */
431 #define M_PI 3.14159265358979323846 /**< pi */
432 #define M_PI_2 1.57079632679489661923 /**< pi/2 */
433 #define M_PI_4 0.78539816339744830962 /**< pi/4 */
434 #define M_1_PI 0.31830988618379067154 /**< 1/pi */
435 #define M_2_PI 0.63661977236758134308 /**< 2/pi */
436 #define M_2_SQRTPI 1.12837916709551257390 /**< 2/sqrt(pi) */
437 #define M_SQRT2 1.41421356237309504880 /**< sqrt(2) */
438 #define M_SQRT1_2 0.70710678118654752440 /**< 1/sqrt(2) */
439 #define MAXFLOAT ((float)3.40282346638528860e+38)
442 #endif /* _MATH_H_ */