s_ilogbl.c 1.3 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 "cdefs-compat.h"
  13. //__FBSDID("$FreeBSD: src/lib/msun/src/s_ilogbl.c,v 1.2 2008/02/22 02:30:35 das Exp $");
  14. #include <float.h>
  15. #include <limits.h>
  16. #include <openlibm.h>
  17. #include "fpmath.h"
  18. int
  19. ilogbl(long double x)
  20. {
  21. union IEEEl2bits u;
  22. unsigned long m;
  23. int b;
  24. u.e = x;
  25. if (u.bits.exp == 0) {
  26. if ((u.bits.manl | u.bits.manh) == 0)
  27. return (FP_ILOGB0);
  28. /* denormalized */
  29. if (u.bits.manh == 0) {
  30. m = 1lu << (LDBL_MANL_SIZE - 1);
  31. for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
  32. b++;
  33. } else {
  34. m = 1lu << (LDBL_MANH_SIZE - 1);
  35. for (b = 0; !(u.bits.manh & m); m >>= 1)
  36. b++;
  37. }
  38. #ifdef LDBL_IMPLICIT_NBIT
  39. b++;
  40. #endif
  41. return (LDBL_MIN_EXP - b - 1);
  42. } else if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)
  43. return (u.bits.exp - LDBL_MAX_EXP + 1);
  44. else if (u.bits.manl != 0 || u.bits.manh != 0)
  45. return (FP_ILOGBNAN);
  46. else
  47. return (INT_MAX);
  48. }