s_frexp.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /*
  12. * for non-zero x
  13. * x = frexp(arg,&exp);
  14. * return a double fp quantity x such that 0.5 <= |x| <1.0
  15. * and the corresponding binary exponent "exp". That is
  16. * arg = x*2^exp.
  17. * If arg is inf, 0.0, or NaN, then frexp(arg,&exp) returns arg
  18. * with *exp=0.
  19. */
  20. #include "math.h"
  21. #include "math_private.h"
  22. static const double
  23. two54 = 1.80143985094819840000e+16; /* 0x43500000, 0x00000000 */
  24. double frexp(double x, int *eptr)
  25. {
  26. int32_t hx, ix, lx;
  27. EXTRACT_WORDS(hx,lx,x);
  28. ix = 0x7fffffff&hx;
  29. *eptr = 0;
  30. if(ix>=0x7ff00000||((ix|lx)==0)) return x; /* 0,inf,nan */
  31. if (ix<0x00100000) { /* subnormal */
  32. x *= two54;
  33. GET_HIGH_WORD(hx,x);
  34. ix = hx&0x7fffffff;
  35. *eptr = -54;
  36. }
  37. *eptr += (ix>>20)-1022;
  38. hx = (hx&0x800fffff)|0x3fe00000;
  39. SET_HIGH_WORD(x,hx);
  40. return x;
  41. }
  42. libm_hidden_def(frexp)