elog.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* xlog.c */
  2. /* natural logarithm */
  3. /* by Stephen L. Moshier. */
  4. #include "mconf.h"
  5. #include "ehead.h"
  6. void elog( x, y )
  7. unsigned short *x, *y;
  8. {
  9. unsigned short xx[NE], z[NE], a[NE], b[NE], t[NE], qj[NE];
  10. long ex;
  11. int fex;
  12. if( x[NE-1] & (unsigned short )0x8000 )
  13. {
  14. eclear(y);
  15. mtherr( "elog", DOMAIN );
  16. return;
  17. }
  18. if( ecmp( x, ezero ) == 0 )
  19. {
  20. einfin( y );
  21. eneg(y);
  22. mtherr( "elog", SING );
  23. return;
  24. }
  25. if( ecmp( x, eone ) == 0 )
  26. {
  27. eclear( y );
  28. return;
  29. }
  30. /* range reduction: log x = log( 2**ex * m ) = ex * log2 + log m */
  31. efrexp( x, &fex, xx );
  32. /*
  33. emov(x, xx );
  34. ex = xx[NX-1] & 0x7fff;
  35. ex -= 0x3ffe;
  36. xx[NX-1] = 0x3ffe;
  37. */
  38. /* Adjust range to 1/sqrt(2), sqrt(2) */
  39. esqrt2[NE-1] -= 1;
  40. if( ecmp( xx, esqrt2 ) < 0 )
  41. {
  42. fex -= 1;
  43. emul( xx, etwo, xx );
  44. }
  45. esqrt2[NE-1] += 1;
  46. esub( eone, xx, a );
  47. if( a[NE-1] == 0 )
  48. {
  49. eclear( y );
  50. goto logdon;
  51. }
  52. eadd( eone, xx, b );
  53. ediv( b, a, y ); /* store (x-1)/(x+1) in y */
  54. emul( y, y, z );
  55. emov( eone, a );
  56. emov( eone, b );
  57. emov( eone, qj );
  58. do
  59. {
  60. eadd( etwo, qj, qj ); /* 2 * i + 1 */
  61. emul( z, a, a );
  62. ediv( qj, a, t );
  63. eadd( t, b, b );
  64. }
  65. while( ((b[NE-1] & 0x7fff) - (t[NE-1] & 0x7fff)) < NBITS );
  66. emul( b, y, y );
  67. emul( y, etwo, y );
  68. logdon:
  69. /* now add log of 2**ex */
  70. if( fex != 0 )
  71. {
  72. ex = fex;
  73. ltoe( &ex, b );
  74. emul( elog2, b, b );
  75. eadd( b, y, y );
  76. }
  77. }