s_frexp.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* @(#)s_frexp.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_frexp.c,v 1.9 1995/05/10 20:47:24 jtc Exp $";
  14. #endif
  15. /*
  16. * for non-zero x
  17. * x = frexp(arg,&exp);
  18. * return a double fp quantity x such that 0.5 <= |x| <1.0
  19. * and the corresponding binary exponent "exp". That is
  20. * arg = x*2^exp.
  21. * If arg is inf, 0.0, or NaN, then frexp(arg,&exp) returns arg
  22. * with *exp=0.
  23. */
  24. #include "math.h"
  25. #include "math_private.h"
  26. #ifdef __STDC__
  27. static const double
  28. #else
  29. static double
  30. #endif
  31. two54 = 1.80143985094819840000e+16; /* 0x43500000, 0x00000000 */
  32. libm_hidden_proto(frexp)
  33. #ifdef __STDC__
  34. double frexp(double x, int *eptr)
  35. #else
  36. double frexp(x, eptr)
  37. double x; int *eptr;
  38. #endif
  39. {
  40. int32_t hx, ix, lx;
  41. EXTRACT_WORDS(hx,lx,x);
  42. ix = 0x7fffffff&hx;
  43. *eptr = 0;
  44. if(ix>=0x7ff00000||((ix|lx)==0)) return x; /* 0,inf,nan */
  45. if (ix<0x00100000) { /* subnormal */
  46. x *= two54;
  47. GET_HIGH_WORD(hx,x);
  48. ix = hx&0x7fffffff;
  49. *eptr = -54;
  50. }
  51. *eptr += (ix>>20)-1022;
  52. hx = (hx&0x800fffff)|0x3fe00000;
  53. SET_HIGH_WORD(x,hx);
  54. return x;
  55. }
  56. libm_hidden_def(frexp)