s_nearbyint.c 1.6 KB

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