OSDN Git Service

d08430dc60ce1072b01603d453a4347baccf1996
[uclinux-h8/uClibc.git] / libm / powerpc / classic / s_nearbyint.c
1 #include <limits.h>
2 #include <math.h>
3
4 /*******************************************************************************
5 *                                                                              *
6 *     The function nearbyint rounds its double argument to integral value      *
7 *     according to the current rounding direction and returns the result in    *
8 *     double format.  This function does not signal inexact.                   *
9 *                                                                              *
10 ********************************************************************************
11 *                                                                              *
12 *     This function calls fabs and copysign.                                     *
13 *                                                                              *
14 *******************************************************************************/
15
16 static const double twoTo52      = 4503599627370496.0;
17
18 libm_hidden_proto(nearbyint)
19 double nearbyint ( double x )
20       {
21         double y;
22         double OldEnvironment;
23
24         y = twoTo52;
25
26         __asm__ ("mffs %0" : "=f" (OldEnvironment));    /* get the environement */
27
28       if ( fabs ( x ) >= y )                          /* huge case is exact */
29             return x;
30       if ( x < 0 ) y = -y;                                   /* negative case */
31       y = ( x + y ) - y;                                    /* force rounding */
32       if ( y == 0.0 )                        /* zero results mirror sign of x */
33             y = copysign ( y, x );
34 //      restore old flags
35         __asm__ ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment ));
36       return ( y );
37         }
38 libm_hidden_def(nearbyint)