s_modf.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* @(#)s_modf.c 5.1 93/09/24 */
  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. #if defined(LIBM_SCCS) && !defined(lint)
  13. static char rcsid[] = "$NetBSD: s_modf.c,v 1.8 1995/05/10 20:47:55 jtc Exp $";
  14. #endif
  15. /*
  16. * modf(double x, double *iptr)
  17. * return fraction part of x, and return x's integral part in *iptr.
  18. * Method:
  19. * Bit twiddling.
  20. *
  21. * Exception:
  22. * No exception.
  23. */
  24. #include "math.h"
  25. #include "math_private.h"
  26. #ifdef __STDC__
  27. static const double one = 1.0;
  28. #else
  29. static double one = 1.0;
  30. #endif
  31. libm_hidden_proto(modf)
  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. libm_hidden_def(modf)