s_logb.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* @(#)s_logb.c 5.1 93/09/24 */
  2. /*
  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_logb.c,v 1.12 2008/02/08 01:22:13 bde Exp $");
  14. /*
  15. * double logb(x)
  16. * IEEE 754 logb. Included to pass IEEE test suite. Not recommend.
  17. * Use ilogb instead.
  18. */
  19. #include <float.h>
  20. #include "openlibm.h"
  21. #include "math_private.h"
  22. static const double
  23. two54 = 1.80143985094819840000e+16; /* 43500000 00000000 */
  24. double
  25. logb(double x)
  26. {
  27. int32_t lx,ix;
  28. EXTRACT_WORDS(ix,lx,x);
  29. ix &= 0x7fffffff; /* high |x| */
  30. if((ix|lx)==0) return -1.0/fabs(x);
  31. if(ix>=0x7ff00000) return x*x;
  32. if(ix<0x00100000) {
  33. x *= two54; /* convert subnormal x to normal */
  34. GET_HIGH_WORD(ix,x);
  35. ix &= 0x7fffffff;
  36. return (double) ((ix>>20)-1023-54);
  37. } else
  38. return (double) ((ix>>20)-1023);
  39. }
  40. #if (LDBL_MANT_DIG == 53)
  41. __weak_reference(logb, logbl);
  42. #endif