s_logbl.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 <float.h>
  13. #include <limits.h>
  14. #include <openlibm.h>
  15. #include "math_private.h"
  16. #include "fpmath.h"
  17. DLLEXPORT long double
  18. logbl(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) { /* x == 0 */
  26. u.bits.sign = 1;
  27. return (1.0L / u.e);
  28. }
  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 ((long double)(LDBL_MIN_EXP - b - 1));
  43. }
  44. if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1) /* normal */
  45. return ((long double)(u.bits.exp - LDBL_MAX_EXP + 1));
  46. else /* +/- inf or nan */
  47. return (x * x);
  48. }