s_modf.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifdef __STDC__
  32. double modf(double x, double *iptr)
  33. #else
  34. double modf(x, iptr)
  35. double x,*iptr;
  36. #endif
  37. {
  38. int32_t i0,i1,j0;
  39. u_int32_t i;
  40. EXTRACT_WORDS(i0,i1,x);
  41. j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */
  42. if(j0<20) { /* integer part in high x */
  43. if(j0<0) { /* |x|<1 */
  44. INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
  45. return x;
  46. } else {
  47. i = (0x000fffff)>>j0;
  48. if(((i0&i)|i1)==0) { /* x is integral */
  49. u_int32_t high;
  50. *iptr = x;
  51. GET_HIGH_WORD(high,x);
  52. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  53. return x;
  54. } else {
  55. INSERT_WORDS(*iptr,i0&(~i),0);
  56. return x - *iptr;
  57. }
  58. }
  59. } else if (j0>51) { /* no fraction part */
  60. u_int32_t high;
  61. *iptr = x*one;
  62. GET_HIGH_WORD(high,x);
  63. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  64. return x;
  65. } else { /* fraction part in low x */
  66. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  67. if((i1&i)==0) { /* x is integral */
  68. u_int32_t high;
  69. *iptr = x;
  70. GET_HIGH_WORD(high,x);
  71. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  72. return x;
  73. } else {
  74. INSERT_WORDS(*iptr,i0,i1&(~i));
  75. return x - *iptr;
  76. }
  77. }
  78. }
  79. libm_hidden_def(modf)