s_modf.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #if !defined(__ppc__)
  2. /* @(#)s_modf.c 5.1 93/09/24 */
  3. /*
  4. * ====================================================
  5. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * Developed at SunPro, a Sun Microsystems, Inc. business.
  8. * Permission to use, copy, modify, and distribute this
  9. * software is freely granted, provided that this notice
  10. * is preserved.
  11. * ====================================================
  12. */
  13. #if defined(LIBM_SCCS) && !defined(lint)
  14. static char rcsid[] = "$NetBSD: s_modf.c,v 1.8 1995/05/10 20:47:55 jtc Exp $";
  15. #endif
  16. /*
  17. * modf(double x, double *iptr)
  18. * return fraction part of x, and return x's integral part in *iptr.
  19. * Method:
  20. * Bit twiddling.
  21. *
  22. * Exception:
  23. * No exception.
  24. */
  25. #include "math.h"
  26. #include "math_private.h"
  27. #ifdef __STDC__
  28. static const double one = 1.0;
  29. #else
  30. static double one = 1.0;
  31. #endif
  32. #ifdef __STDC__
  33. double modf(double x, double *iptr)
  34. #else
  35. double modf(x, iptr)
  36. double x,*iptr;
  37. #endif
  38. {
  39. int32_t i0,i1,j0;
  40. u_int32_t i;
  41. EXTRACT_WORDS(i0,i1,x);
  42. j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */
  43. if(j0<20) { /* integer part in high x */
  44. if(j0<0) { /* |x|<1 */
  45. INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
  46. return x;
  47. } else {
  48. i = (0x000fffff)>>j0;
  49. if(((i0&i)|i1)==0) { /* x is integral */
  50. u_int32_t high;
  51. *iptr = x;
  52. GET_HIGH_WORD(high,x);
  53. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  54. return x;
  55. } else {
  56. INSERT_WORDS(*iptr,i0&(~i),0);
  57. return x - *iptr;
  58. }
  59. }
  60. } else if (j0>51) { /* no fraction part */
  61. u_int32_t high;
  62. *iptr = x*one;
  63. GET_HIGH_WORD(high,x);
  64. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  65. return x;
  66. } else { /* fraction part in low x */
  67. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  68. if((i1&i)==0) { /* x is integral */
  69. u_int32_t high;
  70. *iptr = x;
  71. GET_HIGH_WORD(high,x);
  72. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  73. return x;
  74. } else {
  75. INSERT_WORDS(*iptr,i0,i1&(~i));
  76. return x - *iptr;
  77. }
  78. }
  79. }
  80. #endif /* !__ppc__ */