s_modf.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "SDL_internal.h"
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunPro, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. /*
  13. * modf(double x, double *iptr)
  14. * return fraction part of x, and return x's integral part in *iptr.
  15. * Method:
  16. * Bit twiddling.
  17. *
  18. * Exception:
  19. * No exception.
  20. */
  21. #include "math_libm.h"
  22. #include "math_private.h"
  23. static const double one = 1.0;
  24. double modf(double x, double *iptr)
  25. {
  26. int32_t i0,i1,_j0;
  27. u_int32_t i;
  28. EXTRACT_WORDS(i0,i1,x);
  29. _j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */
  30. if(_j0<20) { /* integer part in high x */
  31. if(_j0<0) { /* |x|<1 */
  32. INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
  33. return x;
  34. } else {
  35. i = (0x000fffff)>>_j0;
  36. if(((i0&i)|i1)==0) { /* x is integral */
  37. *iptr = x;
  38. INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */
  39. return x;
  40. } else {
  41. INSERT_WORDS(*iptr,i0&(~i),0);
  42. return x - *iptr;
  43. }
  44. }
  45. } else if (_j0>51) { /* no fraction part */
  46. *iptr = x*one;
  47. /* We must handle NaNs separately. */
  48. if (_j0 == 0x400 && ((i0 & 0xfffff) | i1))
  49. return x*one;
  50. INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */
  51. return x;
  52. } else { /* fraction part in low x */
  53. i = ((u_int32_t)(0xffffffff))>>(_j0-20);
  54. if((i1&i)==0) { /* x is integral */
  55. *iptr = x;
  56. INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */
  57. return x;
  58. } else {
  59. INSERT_WORDS(*iptr,i0,i1&(~i));
  60. return x - *iptr;
  61. }
  62. }
  63. }
  64. libm_hidden_def(modf)