s_ilogbl.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * From: @(#)s_ilogb.c 5.1 93/09/24
  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. #include <sys/cdefs.h>
  13. #include <float.h>
  14. #include <limits.h>
  15. #include "openlibm.h"
  16. #include "fpmath.h"
  17. int
  18. ilogbl(long double x)
  19. {
  20. union IEEEl2bits u;
  21. unsigned long m;
  22. int b;
  23. u.e = x;
  24. if (u.bits.exp == 0) {
  25. if ((u.bits.manl | u.bits.manh) == 0)
  26. return (FP_ILOGB0);
  27. /* denormalized */
  28. if (u.bits.manh == 0) {
  29. m = 1lu << (LDBL_MANL_SIZE - 1);
  30. for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
  31. b++;
  32. } else {
  33. m = 1lu << (LDBL_MANH_SIZE - 1);
  34. for (b = 0; !(u.bits.manh & m); m >>= 1)
  35. b++;
  36. }
  37. #ifdef LDBL_IMPLICIT_NBIT
  38. b++;
  39. #endif
  40. return (LDBL_MIN_EXP - b - 1);
  41. } else if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)
  42. return (u.bits.exp - LDBL_MAX_EXP + 1);
  43. else if (u.bits.manl != 0 || u.bits.manh != 0)
  44. return (FP_ILOGBNAN);
  45. else
  46. return (INT_MAX);
  47. }