fp_log.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. fp_log.c: floating-point math routines for the Linux-m68k
  3. floating point emulator.
  4. Copyright (c) 1998-1999 David Huggins-Daines / Roman Zippel.
  5. I hereby give permission, free of charge, to copy, modify, and
  6. redistribute this software, in source or binary form, provided that
  7. the above copyright notice and the following disclaimer are included
  8. in all such copies.
  9. THIS SOFTWARE IS PROVIDED "AS IS", WITH ABSOLUTELY NO WARRANTY, REAL
  10. OR IMPLIED.
  11. */
  12. #include "fp_arith.h"
  13. #include "fp_emu.h"
  14. #include "fp_log.h"
  15. static const struct fp_ext fp_one = {
  16. .exp = 0x3fff,
  17. };
  18. struct fp_ext *fp_fsqrt(struct fp_ext *dest, struct fp_ext *src)
  19. {
  20. struct fp_ext tmp, src2;
  21. int i, exp;
  22. dprint(PINSTR, "fsqrt\n");
  23. fp_monadic_check(dest, src);
  24. if (IS_ZERO(dest))
  25. return dest;
  26. if (dest->sign) {
  27. fp_set_nan(dest);
  28. return dest;
  29. }
  30. if (IS_INF(dest))
  31. return dest;
  32. /*
  33. * sqrt(m) * 2^(p) , if e = 2*p
  34. * sqrt(m*2^e) =
  35. * sqrt(2*m) * 2^(p) , if e = 2*p + 1
  36. *
  37. * So we use the last bit of the exponent to decide whether to
  38. * use the m or 2*m.
  39. *
  40. * Since only the fractional part of the mantissa is stored and
  41. * the integer part is assumed to be one, we place a 1 or 2 into
  42. * the fixed point representation.
  43. */
  44. exp = dest->exp;
  45. dest->exp = 0x3FFF;
  46. if (!(exp & 1)) /* lowest bit of exponent is set */
  47. dest->exp++;
  48. fp_copy_ext(&src2, dest);
  49. /*
  50. * The taylor row around a for sqrt(x) is:
  51. * sqrt(x) = sqrt(a) + 1/(2*sqrt(a))*(x-a) + R
  52. * With a=1 this gives:
  53. * sqrt(x) = 1 + 1/2*(x-1)
  54. * = 1/2*(1+x)
  55. */
  56. /* It is safe to cast away the constness, as fp_one is normalized */
  57. fp_fadd(dest, (struct fp_ext *)&fp_one);
  58. dest->exp--; /* * 1/2 */
  59. /*
  60. * We now apply the newton rule to the function
  61. * f(x) := x^2 - r
  62. * which has a null point on x = sqrt(r).
  63. *
  64. * It gives:
  65. * x' := x - f(x)/f'(x)
  66. * = x - (x^2 -r)/(2*x)
  67. * = x - (x - r/x)/2
  68. * = (2*x - x + r/x)/2
  69. * = (x + r/x)/2
  70. */
  71. for (i = 0; i < 9; i++) {
  72. fp_copy_ext(&tmp, &src2);
  73. fp_fdiv(&tmp, dest);
  74. fp_fadd(dest, &tmp);
  75. dest->exp--;
  76. }
  77. dest->exp += (exp - 0x3FFF) / 2;
  78. return dest;
  79. }
  80. struct fp_ext *fp_fetoxm1(struct fp_ext *dest, struct fp_ext *src)
  81. {
  82. uprint("fetoxm1\n");
  83. fp_monadic_check(dest, src);
  84. return dest;
  85. }
  86. struct fp_ext *fp_fetox(struct fp_ext *dest, struct fp_ext *src)
  87. {
  88. uprint("fetox\n");
  89. fp_monadic_check(dest, src);
  90. return dest;
  91. }
  92. struct fp_ext *fp_ftwotox(struct fp_ext *dest, struct fp_ext *src)
  93. {
  94. uprint("ftwotox\n");
  95. fp_monadic_check(dest, src);
  96. return dest;
  97. }
  98. struct fp_ext *fp_ftentox(struct fp_ext *dest, struct fp_ext *src)
  99. {
  100. uprint("ftentox\n");
  101. fp_monadic_check(dest, src);
  102. return dest;
  103. }
  104. struct fp_ext *fp_flogn(struct fp_ext *dest, struct fp_ext *src)
  105. {
  106. uprint("flogn\n");
  107. fp_monadic_check(dest, src);
  108. return dest;
  109. }
  110. struct fp_ext *fp_flognp1(struct fp_ext *dest, struct fp_ext *src)
  111. {
  112. uprint("flognp1\n");
  113. fp_monadic_check(dest, src);
  114. return dest;
  115. }
  116. struct fp_ext *fp_flog10(struct fp_ext *dest, struct fp_ext *src)
  117. {
  118. uprint("flog10\n");
  119. fp_monadic_check(dest, src);
  120. return dest;
  121. }
  122. struct fp_ext *fp_flog2(struct fp_ext *dest, struct fp_ext *src)
  123. {
  124. uprint("flog2\n");
  125. fp_monadic_check(dest, src);
  126. return dest;
  127. }
  128. struct fp_ext *fp_fgetexp(struct fp_ext *dest, struct fp_ext *src)
  129. {
  130. dprint(PINSTR, "fgetexp\n");
  131. fp_monadic_check(dest, src);
  132. if (IS_INF(dest)) {
  133. fp_set_nan(dest);
  134. return dest;
  135. }
  136. if (IS_ZERO(dest))
  137. return dest;
  138. fp_conv_long2ext(dest, (int)dest->exp - 0x3FFF);
  139. fp_normalize_ext(dest);
  140. return dest;
  141. }
  142. struct fp_ext *fp_fgetman(struct fp_ext *dest, struct fp_ext *src)
  143. {
  144. dprint(PINSTR, "fgetman\n");
  145. fp_monadic_check(dest, src);
  146. if (IS_ZERO(dest))
  147. return dest;
  148. if (IS_INF(dest))
  149. return dest;
  150. dest->exp = 0x3FFF;
  151. return dest;
  152. }