s_ilogb.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. /* ilogb(double x)
  12. * return the binary exponent of non-zero x
  13. * ilogb(0) = 0x80000001
  14. * ilogb(inf/NaN) = 0x7fffffff (no signal is raised)
  15. */
  16. #include "math.h"
  17. #include "math_private.h"
  18. int ilogb(double x)
  19. {
  20. int32_t hx,lx,ix;
  21. GET_HIGH_WORD(hx,x);
  22. hx &= 0x7fffffff;
  23. if(hx<0x00100000) {
  24. GET_LOW_WORD(lx,x);
  25. if((hx|lx)==0)
  26. return 0x80000001; /* ilogb(0) = 0x80000001 */
  27. else /* subnormal x */
  28. if(hx==0) {
  29. for (ix = -1043; lx>0; lx<<=1) ix -=1;
  30. } else {
  31. for (ix = -1022,hx<<=11; hx>0; hx<<=1) ix -=1;
  32. }
  33. return ix;
  34. }
  35. else if (hx<0x7ff00000) return (hx>>20)-1023;
  36. else return 0x7fffffff;
  37. }
  38. libm_hidden_def(ilogb)