logf.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_logf.c */
  2. /*
  3. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  4. */
  5. /*
  6. * ====================================================
  7. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  8. *
  9. * Developed at SunPro, a Sun Microsystems, Inc. business.
  10. * Permission to use, copy, modify, and distribute this
  11. * software is freely granted, provided that this notice
  12. * is preserved.
  13. * ====================================================
  14. */
  15. #include "libm.h"
  16. static const float
  17. ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
  18. ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
  19. two25 = 3.355443200e+07, /* 0x4c000000 */
  20. /* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
  21. Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */
  22. Lg2 = 0xccce13.0p-25, /* 0.40000972152 */
  23. Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */
  24. Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */
  25. float logf(float x)
  26. {
  27. float hfsq,f,s,z,R,w,t1,t2,dk;
  28. int32_t k,ix,i,j;
  29. GET_FLOAT_WORD(ix, x);
  30. k = 0;
  31. if (ix < 0x00800000) { /* x < 2**-126 */
  32. if ((ix & 0x7fffffff) == 0)
  33. return -two25/0.0f; /* log(+-0)=-inf */
  34. if (ix < 0)
  35. return (x-x)/0.0f; /* log(-#) = NaN */
  36. /* subnormal number, scale up x */
  37. k -= 25;
  38. x *= two25;
  39. GET_FLOAT_WORD(ix, x);
  40. }
  41. if (ix >= 0x7f800000)
  42. return x+x;
  43. k += (ix>>23) - 127;
  44. ix &= 0x007fffff;
  45. i = (ix + (0x95f64<<3)) & 0x800000;
  46. SET_FLOAT_WORD(x, ix|(i^0x3f800000)); /* normalize x or x/2 */
  47. k += i>>23;
  48. f = x - 1.0f;
  49. if ((0x007fffff & (0x8000 + ix)) < 0xc000) { /* -2**-9 <= f < 2**-9 */
  50. if (f == 0.0f) {
  51. if (k == 0)
  52. return 0.0f;
  53. dk = (float)k;
  54. return dk*ln2_hi + dk*ln2_lo;
  55. }
  56. R = f*f*(0.5f - 0.33333333333333333f*f);
  57. if (k == 0)
  58. return f-R;
  59. dk = (float)k;
  60. return dk*ln2_hi - ((R-dk*ln2_lo)-f);
  61. }
  62. s = f/(2.0f + f);
  63. dk = (float)k;
  64. z = s*s;
  65. i = ix-(0x6147a<<3);
  66. w = z*z;
  67. j = (0x6b851<<3)-ix;
  68. t1= w*(Lg2+w*Lg4);
  69. t2= z*(Lg1+w*Lg3);
  70. i |= j;
  71. R = t2 + t1;
  72. if (i > 0) {
  73. hfsq = 0.5f * f * f;
  74. if (k == 0)
  75. return f - (hfsq-s*(hfsq+R));
  76. return dk*ln2_hi - ((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f);
  77. } else {
  78. if (k == 0)
  79. return f - s*(f-R);
  80. return dk*ln2_hi - ((s*(f-R)-dk*ln2_lo)-f);
  81. }
  82. }