s_ilogbl.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_math.h>
  17. #include "fpmath.h"
  18. #include "math_private.h"
  19. OLM_DLLEXPORT int
  20. ilogbl(long double x)
  21. {
  22. union IEEEl2bits u;
  23. unsigned long m;
  24. int b;
  25. u.e = x;
  26. if (u.bits.exp == 0) {
  27. if ((u.bits.manl | u.bits.manh) == 0)
  28. return (FP_ILOGB0);
  29. /* denormalized */
  30. if (u.bits.manh == 0) {
  31. m = 1lu << (LDBL_MANL_SIZE - 1);
  32. for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
  33. b++;
  34. } else {
  35. m = 1lu << (LDBL_MANH_SIZE - 1);
  36. for (b = 0; !(u.bits.manh & m); m >>= 1)
  37. b++;
  38. }
  39. #ifdef LDBL_IMPLICIT_NBIT
  40. b++;
  41. #endif
  42. return (LDBL_MIN_EXP - b - 1);
  43. } else if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)
  44. return (u.bits.exp - LDBL_MAX_EXP + 1);
  45. else if (u.bits.manl != 0 || u.bits.manh != 0)
  46. return (FP_ILOGBNAN);
  47. else
  48. return (INT_MAX);
  49. }