s_ilogb.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 x
  13. * ilogb(+-0) = FP_ILOGB0
  14. * ilogb(+-inf) = INT_MAX
  15. * ilogb(NaN) = FP_ILOGBNAN (no signal is raised)
  16. */
  17. #include "math.h"
  18. #include "math_private.h"
  19. int ilogb(double x)
  20. {
  21. int32_t hx,lx,ix;
  22. GET_HIGH_WORD(hx, x);
  23. hx &= 0x7fffffff;
  24. if (hx < 0x00100000) {
  25. GET_LOW_WORD(lx, x);
  26. if ((hx|lx)==0) /* +-0, ilogb(0) = FP_ILOGB0 */
  27. return FP_ILOGB0;
  28. /* subnormal x */
  29. ix = -1043;
  30. if (hx != 0) {
  31. ix = -1022;
  32. lx = (hx << 11);
  33. }
  34. /* each leading zero mantissa bit makes exponent smaller */
  35. for (; lx > 0; lx <<= 1)
  36. ix--;
  37. return ix;
  38. }
  39. if (hx < 0x7ff00000) /* normal x */
  40. return (hx>>20) - 1023;
  41. if (FP_ILOGBNAN != (~0U >> 1)) {
  42. GET_LOW_WORD(lx, x);
  43. if (hx == 0x7ff00000 && lx == 0) /* +-inf */
  44. return ~0U >> 1; /* = INT_MAX */
  45. }
  46. /* NAN. ilogb(NAN) = FP_ILOGBNAN */
  47. return FP_ILOGBNAN;
  48. }
  49. libm_hidden_def(ilogb)