1 /** @file
2   Floating-point Math functions and macros.
3 
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.
9 
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.
12 
13  * ====================================================
14  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
15  *
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
19  * is preserved.
20  * ====================================================
21 
22     NetBSD: math.h,v 1.44 2006/03/25 16:41:11 xtraeme Exp
23     dlibm.h 5.1 93/09/24
24 **/
25 #ifndef _MATH_H_
26 #define _MATH_H_
27 
28 #include  <sys/EfiCdefs.h>
29 #include  <sys/featuretest.h>
30 
31 /** @{
32     @brief    These are forward references to unions and macros used internaly
33               by the implementation of the math functions and macros.
34 **/
35 union __float_u {
36   unsigned char __dummy[sizeof(float)];
37   float __val;
38 };
39 
40 union __double_u {
41   unsigned char __dummy[sizeof(double)];
42   double __val;
43 };
44 
45 union __long_double_u {
46   unsigned char __dummy[sizeof(long double)];
47   long double __val;
48 };
49 
50 #include <machine/math.h>   /* may use __float_u, __double_u, 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 extern const union __double_u       __infinity;
69 extern const union __float_u        __infinityf;
70 extern const union __long_double_u  __infinityl;
71 
72 /* C99 7.12.3.1 int fpclassify(real-floating x) */
73 #define fpclassify(__x) __fpmacro_unary_floating(fpclassify, __x)
74 
75 /* C99 7.12.3.3 int isinf(real-floating x) */
76 #ifdef __isinf
77   #define isinf(__x)  __isinf(__x)
78 #else
79   #define isinf(__x)  __fpmacro_unary_floating(isinf, __x)
80 #endif
81 
82 /* C99 7.12.3.4 int isnan(real-floating x) */
83 #ifdef __isnan
84   #define isnan(__x)  __isnan(__x)
85 #else
86   #define isnan(__x)  __fpmacro_unary_floating(isnan, __x)
87 #endif
88 /*@)*/
89 
90 /*#############################################################
91  * ISO C95
92  */
93 
94 /**@{
95     Double, float, and long double versions, respectively, of HUGE_VAL.
96 */
97 #define HUGE_VAL  __infinity.__val
98 #define HUGE_VALF __infinityf.__val
99 #define HUGE_VALL __infinityl.__val
100 /*@)*/
101 
102 __BEGIN_DECLS
103 /*
104  * ANSI/POSIX
105  */
106 /** Compute the principal value of the arc cosine of Arg.
107 
108     @param[in]    Arg   The value to compute the arc cosine of.
109 
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.
112 **/
113 double  acos(double Arg);
114 
115 /** Compute the principal value of the arc sine of Arg.
116 
117     @param[in]    Arg   The value to compute the arc sine of.
118 
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.
121 **/
122 double  asin(double Arg);
123 
124 /** Compute the principal value of the arc tangent of Arg.
125 
126     @param[in]    Arg   The value to compute the arc tangent of.
127 
128     @return   The computed value of the arc tangent of Arg in the interval [-pi/2,+pi/2] radians.
129 **/
130 double  atan(double Arg);
131 
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.
134 
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.
137 
138     @return   The computed value of the arc tangent of (Num / Denom) in the interval [-pi,+pi] radians.
139 **/
140 double  atan2(double Num, double Denom);
141 
142 /** Compute the value of the cosine of Arg, measured in radians.
143 
144     @param[in]    Arg   The value to compute the cosine of.
145 
146     @return   The computed value of the cosine of Arg.
147 **/
148 double  cos(double Arg);
149 
150 /** Compute the value of the sine of Arg.
151 
152     @param[in]    Arg   The value to compute the sine of.
153 
154     @return   The computed value of the sine of Arg.
155 **/
156 double  sin(double Arg);
157 
158 /** Compute the value of the tangent of Arg.
159 
160     @param[in]    Arg   The value to compute the tangent of.
161 
162     @return   The computed value of the tangent of Arg.
163 **/
164 double  tan(double Arg);
165 
166 
167 /** Compute the value of the hyperbolic cosine of Arg.
168 
169     @param[in]    Arg   The value to compute the hyperbolic cosine of.
170 
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.
173 **/
174 double  cosh(double Arg);
175 
176 /** Compute the value of the hyperbolic sine of Arg.
177 
178     @param[in]    Arg   The value to compute the hyperbolic sine of.
179 
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.
182 **/
183 double  sinh(double Arg);
184 
185 /** Compute the value of the hyperbolic tangent of Arg.
186 
187     @param[in]    Arg   The value to compute the hyperbolic tangent of.
188 
189     @return   The computed value of the hyperbolic tangent of Arg.
190 **/
191 double  tanh(double Arg);
192 
193 
194 /** Compute the base-e exponential of Arg.
195 
196     @param[in]    Arg   The value to compute the base-e exponential of.
197 
198     @return   The computed value of e**Arg.
199               If the magnitude of Arg is too large, errno is set to ERANGE.
200 **/
201 double  exp(double Arg);
202 
203 /** Break a floating-point number into a normalized fraction and an integral power of 2.
204 
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.
207 
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.
210 **/
211 double  frexp(double Value, int *Exp);
212 
213 /** Multiply a floating-point number, Value, by an integral power of 2, Exp.
214 
215     @param[in]    Value   The floating-point value to be multiplied.
216     @param[out]   Exp     The integral power of 2 to multiply Value by.
217 
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.
220 **/
221 double  ldexp(double Value, int Exp);
222 
223 /** Compute the natural logarithm of Arg.
224 
225     @param[in]    Arg   The value to compute the natural logarithm of.
226 
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.
229 **/
230 double  log(double Arg);
231 
232 /** Compute the common (base-10) logarithm of Arg.
233 
234     @param[in]    Arg   The value to compute the common logarithm of.
235 
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.
238 **/
239 double  log10(double Arg);
240 
241 /** Compute the base-2 logarithm of Arg.
242 
243     @param[in]    Arg   The value to compute the base-2 logarithm of.
244 
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.
247 **/
248 double  log2(double Arg);
249 
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
252     fractional part.
253 
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.
256 
257     @return   The fractional part of Value is returned directly while the integral part is
258               returned in the location pointed to by Integ.
259 **/
260 double  modf(double Value, double *Integ);
261 
262 /** Compute Value raised to the power Exp.
263 
264     @param[in]    Value   The value to be raised.
265     @param[in]    Exp     The power Value is to be raised to.
266 
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.
271 **/
272 double  pow(double Value, double Exp);
273 
274 /** Compute the non-negative square root of Arg.
275 
276     @param[in]    Arg   The value to compute the square root of.
277 
278     @return   The square root of Arg.  If Arg is less than zero, errno is set to EDOM.
279 **/
280 double  sqrt(double Arg);
281 
282 
283 /** Compute the smallest integer value not less than Arg.
284 
285     @param[in]    Arg   The value to compute the ceiling of.
286 
287     @return   The ceiling of Arg expressed as a floating-point number.
288 **/
289 double  ceil(double Arg);
290 
291 /** Compute the absolute value of Arg.
292 
293     @param[in]    Arg   The value to compute the absolute value of.
294 
295     @return   The absolute value of Arg.
296 **/
297 double  fabs(double Arg);
298 
299 /** Compute the largest integer value not greater than Arg.
300 
301     @param[in]    Arg   The value to compute the floor of.
302 
303     @return   The largest integer value not greater than Arg, expressed as a floating-point number.
304 **/
305 double  floor(double);
306 
307 /** Compute the floating-point remainder of A1 / A2.
308 
309     @param[in]    A1    The dividend.
310     @param[in]    A2    The divisor.
311 
312     @return   The remainder of A1 / A2 with the same sign as A1.  If A2 is zero, the fmod function
313               returns 0.
314 **/
315 double  fmod(double A1, double A2);
316 
317 
318 int finite(double);
319 double  expm1(double);
320 
321 /**@{
322     C99, Posix, or NetBSD functions that are not part of the C95 specification.
323 **/
324 /*
325  * Functions callable from C, intended to support IEEE arithmetic.
326  */
327 double  copysign(double, double);
328 double  scalbn(double, int);
329 
330 /*
331  * Library implementation
332  */
333 int __fpclassifyf(float);
334 int __fpclassifyd(double);
335 int __isinff(float);
336 int __isinfd(double);
337 int __isnanf(float);
338 int __isnand(double);
339 
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 */
345 /*@}*/
346 
347 __END_DECLS
348 
349 /**@{
350     Extensions provided by NetBSD but not required by the C95 standard.
351 **/
352 extern int signgam;
353 
354 enum fdversion {fdlibm_ieee = -1, fdlibm_svid, fdlibm_xopen, fdlibm_posix};
355 
356 #define _LIB_VERSION_TYPE enum fdversion
357 #define _LIB_VERSION _fdlib_version
358 
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
365  */
366 extern  _LIB_VERSION_TYPE  _LIB_VERSION;
367 
368 #define _IEEE_  fdlibm_ieee
369 #define _SVID_  fdlibm_svid
370 #define _XOPEN_ fdlibm_xopen
371 #define _POSIX_ fdlibm_posix
372 
373 #ifndef __cplusplus
374 struct exception {
375   int type;
376   char *name;
377   double arg1;
378   double arg2;
379   double retval;
380 };
381 #endif
382 
383 #define HUGE    MAXFLOAT
384 
385 /** set X_TLOSS = pi*2**52 **/
386 #define X_TLOSS   1.41484755040568800000e+16
387 
388 #define DOMAIN    1
389 #define SING      2
390 #define OVERFLOW  3
391 #define UNDERFLOW 4
392 #define TLOSS     5
393 #define PLOSS     6
394 /*@}*/
395 
396 /* 7.12#4 INFINITY */
397 #ifdef __INFINITY
398 #define INFINITY  __INFINITY  /**< float constant which overflows */
399 #else
400 #define INFINITY  HUGE_VALF   /**< positive infinity */
401 #endif /* __INFINITY */
402 
403 /* 7.12#5 NAN: a quiet NaN, if supported */
404 #ifdef __HAVE_NANF
405 extern const union __float_u __nanf;
406 #define NAN   __nanf.__val
407 #endif /* __HAVE_NANF */
408 
409 /**@{
410     C99 7.12#6 Number classification macros represent mutually exclusive kinds of floating-point
411     values.
412 **/
413 #define FP_INFINITE   0x00
414 #define FP_NAN        0x01
415 #define FP_NORMAL     0x02
416 #define FP_SUBNORMAL  0x03
417 #define FP_ZERO       0x04
418 /* NetBSD extensions */
419 #define _FP_LOMD      0x80    /**< range for machine-specific classes */
420 #define _FP_HIMD      0xff
421 /*@)*/
422 
423 /**@{
424  * Constants ala XOPEN/SVID.
425  */
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)
440 /*@}*/
441 
442 #endif /* _MATH_H_ */
443