libm_log.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2019 Actions Semi Co., Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /* poriting from newlib */
  7. #ifndef __UVISION_VERSION
  8. #include <stdint.h>
  9. typedef union {
  10. double value;
  11. struct {
  12. uint32_t lsw;
  13. uint32_t msw;
  14. } parts;
  15. } ieee_double_shape_type;
  16. /* Get two 32 bit ints from a double. */
  17. #define EXTRACT_WORDS(ix0,ix1,d) \
  18. do { \
  19. ieee_double_shape_type ew_u; \
  20. ew_u.value = (d); \
  21. (ix0) = ew_u.parts.msw; \
  22. (ix1) = ew_u.parts.lsw; \
  23. } while (0)
  24. /* Get the more significant 32 bit int from a double. */
  25. #define GET_HIGH_WORD(i,d) \
  26. do { \
  27. ieee_double_shape_type gh_u; \
  28. gh_u.value = (d); \
  29. (i) = gh_u.parts.msw; \
  30. } while (0)
  31. /* Set the more significant 32 bits of a double from an int. */
  32. #define SET_HIGH_WORD(d,v) \
  33. do { \
  34. ieee_double_shape_type sh_u; \
  35. sh_u.value = (d); \
  36. sh_u.parts.msw = (v); \
  37. (d) = sh_u.value; \
  38. } while (0)
  39. /* __ieee754_log(x)
  40. * Return the logrithm of x
  41. *
  42. * Method :
  43. * 1. Argument Reduction: find k and f such that
  44. * x = 2^k * (1+f),
  45. * where sqrt(2)/2 < 1+f < sqrt(2) .
  46. *
  47. * 2. Approximation of log(1+f).
  48. * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
  49. * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
  50. * = 2s + s*R
  51. * We use a special Reme algorithm on [0,0.1716] to generate
  52. * a polynomial of degree 14 to approximate R The maximum error
  53. * of this polynomial approximation is bounded by 2**-58.45. In
  54. * other words,
  55. * 2 4 6 8 10 12 14
  56. * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
  57. * (the values of Lg1 to Lg7 are listed in the program)
  58. * and
  59. * | 2 14 | -58.45
  60. * | Lg1*s +...+Lg7*s - R(z) | <= 2
  61. * | |
  62. * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
  63. * In order to guarantee error in log below 1ulp, we compute log
  64. * by
  65. * log(1+f) = f - s*(f - R) (if f is not too large)
  66. * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
  67. *
  68. * 3. Finally, log(x) = k*ln2 + log(1+f).
  69. * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
  70. * Here ln2 is split into two floating point number:
  71. * ln2_hi + ln2_lo,
  72. * where n*ln2_hi is always exact for |n| < 2000.
  73. *
  74. * Special cases:
  75. * log(x) is NaN with signal if x < 0 (including -INF) ;
  76. * log(+INF) is +INF; log(0) is -INF with signal;
  77. * log(NaN) is that NaN with no signal.
  78. *
  79. * Accuracy:
  80. * according to an error analysis, the error is always less than
  81. * 1 ulp (unit in the last place).
  82. *
  83. * Constants:
  84. * The hexadecimal values are the intended ones for the following
  85. * constants. The decimal values may be used, provided that the
  86. * compiler will convert from decimal to binary accurately enough
  87. * to produce the hexadecimal values shown.
  88. */
  89. static const double
  90. ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
  91. ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
  92. two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
  93. Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
  94. Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
  95. Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
  96. Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
  97. Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
  98. Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
  99. Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
  100. static const double zero = 0.0;
  101. static double __ieee754_log(double x)
  102. {
  103. double hfsq,f,s,z,R,w,t1,t2,dk;
  104. int32_t k,hx,i,j;
  105. uint32_t lx;
  106. EXTRACT_WORDS(hx,lx,x);
  107. k=0;
  108. if (hx < 0x00100000) { /* x < 2**-1022 */
  109. if (((hx&0x7fffffff)|lx)==0)
  110. return -two54/zero; /* log(+-0)=-inf */
  111. if (hx<0) return (x-x)/zero; /* log(-#) = NaN */
  112. k -= 54; x *= two54; /* subnormal number, scale up x */
  113. GET_HIGH_WORD(hx,x);
  114. }
  115. if (hx >= 0x7ff00000) return x+x;
  116. k += (hx>>20)-1023;
  117. hx &= 0x000fffff;
  118. i = (hx+0x95f64)&0x100000;
  119. SET_HIGH_WORD(x,hx|(i^0x3ff00000)); /* normalize x or x/2 */
  120. k += (i>>20);
  121. f = x-1.0;
  122. if((0x000fffff&(2+hx))<3) { /* |f| < 2**-20 */
  123. if(f==zero) { if(k==0) return zero; else {dk=(double)k;
  124. return dk*ln2_hi+dk*ln2_lo;}}
  125. R = f*f*(0.5-0.33333333333333333*f);
  126. if(k==0) return f-R; else {dk=(double)k;
  127. return dk*ln2_hi-((R-dk*ln2_lo)-f);}
  128. }
  129. s = f/(2.0+f);
  130. dk = (double)k;
  131. z = s*s;
  132. i = hx-0x6147a;
  133. w = z*z;
  134. j = 0x6b851-hx;
  135. t1= w*(Lg2+w*(Lg4+w*Lg6));
  136. t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
  137. i |= j;
  138. R = t2+t1;
  139. if(i>0) {
  140. hfsq=0.5*f*f;
  141. if(k==0) return f-(hfsq-s*(hfsq+R)); else
  142. return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f);
  143. } else {
  144. if(k==0) return f-s*(f-R); else
  145. return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f);
  146. }
  147. }
  148. /*
  149. FUNCTION
  150. <<log>>, <<logf>>---natural logarithms
  151. INDEX
  152. log
  153. INDEX
  154. logf
  155. SYNOPSIS
  156. #include <math.h>
  157. double log(double <[x]>);
  158. float logf(float <[x]>);
  159. DESCRIPTION
  160. Return the natural logarithm of <[x]>, that is, its logarithm base e
  161. (where e is the base of the natural system of logarithms, 2.71828@dots{}).
  162. <<log>> and <<logf>> are identical save for the return and argument types.
  163. RETURNS
  164. Normally, returns the calculated value. When <[x]> is zero, the
  165. returned value is <<-HUGE_VAL>> and <<errno>> is set to <<ERANGE>>.
  166. When <[x]> is negative, the returned value is NaN (not a number) and
  167. <<errno>> is set to <<EDOM>>.
  168. PORTABILITY
  169. <<log>> is ANSI. <<logf>> is an extension.
  170. */
  171. double log(double x)
  172. {
  173. return __ieee754_log(x);
  174. }
  175. #endif