s_logbl.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #ifndef lint
  13. static char rcsid[] = "$FreeBSD: src/lib/msun/src/s_logbl.c,v 1.1 2007/12/17 03:53:38 das Exp $";
  14. #endif
  15. #include <float.h>
  16. #include <limits.h>
  17. #include "openlibm.h"
  18. #include "fpmath.h"
  19. long double
  20. logbl(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) { /* x == 0 */
  28. u.bits.sign = 1;
  29. return (1.0L / u.e);
  30. }
  31. /* denormalized */
  32. if (u.bits.manh == 0) {
  33. m = 1lu << (LDBL_MANL_SIZE - 1);
  34. for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
  35. b++;
  36. } else {
  37. m = 1lu << (LDBL_MANH_SIZE - 1);
  38. for (b = 0; !(u.bits.manh & m); m >>= 1)
  39. b++;
  40. }
  41. #ifdef LDBL_IMPLICIT_NBIT
  42. b++;
  43. #endif
  44. return ((long double)(LDBL_MIN_EXP - b - 1));
  45. }
  46. if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1) /* normal */
  47. return ((long double)(u.bits.exp - LDBL_MAX_EXP + 1));
  48. else /* +/- inf or nan */
  49. return (x * x);
  50. }