smisc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /****************************************************************
  2. The author of this software is David M. Gay.
  3. Copyright (C) 1998, 1999 by Lucent Technologies
  4. All Rights Reserved
  5. Permission to use, copy, modify, and distribute this software and
  6. its documentation for any purpose and without fee is hereby
  7. granted, provided that the above copyright notice appear in all
  8. copies and that both that the copyright notice and this
  9. permission notice and warranty disclaimer appear in supporting
  10. documentation, and that the name of Lucent or any of its entities
  11. not be used in advertising or publicity pertaining to
  12. distribution of the software without specific, written prior
  13. permission.
  14. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
  17. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  19. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  21. THIS SOFTWARE.
  22. ****************************************************************/
  23. /* Please send bug reports to David M. Gay (dmg at acm dot org,
  24. * with " at " changed at "@" and " dot " changed to "."). */
  25. #include "gdtoaimp.h"
  26. Bigint *
  27. s2b
  28. #ifdef KR_headers
  29. (s, nd0, nd, y9, dplen) CONST char *s; int dplen, nd0, nd; ULong y9;
  30. #else
  31. (CONST char *s, int nd0, int nd, ULong y9, int dplen)
  32. #endif
  33. {
  34. Bigint *b;
  35. int i, k;
  36. Long x, y;
  37. x = (nd + 8) / 9;
  38. for(k = 0, y = 1; x > y; y <<= 1, k++) ;
  39. #ifdef Pack_32
  40. b = Balloc(k);
  41. if (b == NULL)
  42. return (NULL);
  43. b->x[0] = y9;
  44. b->wds = 1;
  45. #else
  46. b = Balloc(k+1);
  47. if (b == NULL)
  48. return (NULL);
  49. b->x[0] = y9 & 0xffff;
  50. b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
  51. #endif
  52. i = 9;
  53. if (9 < nd0) {
  54. s += 9;
  55. do {
  56. b = multadd(b, 10, *s++ - '0');
  57. if (b == NULL)
  58. return (NULL);
  59. } while(++i < nd0);
  60. s += dplen;
  61. }
  62. else
  63. s += dplen + 9;
  64. for(; i < nd; i++) {
  65. b = multadd(b, 10, *s++ - '0');
  66. if (b == NULL)
  67. return (NULL);
  68. }
  69. return b;
  70. }
  71. double
  72. ratio
  73. #ifdef KR_headers
  74. (a, b) Bigint *a, *b;
  75. #else
  76. (Bigint *a, Bigint *b)
  77. #endif
  78. {
  79. U da, db;
  80. int k, ka, kb;
  81. dval(&da) = b2d(a, &ka);
  82. dval(&db) = b2d(b, &kb);
  83. k = ka - kb + ULbits*(a->wds - b->wds);
  84. #ifdef IBM
  85. if (k > 0) {
  86. word0(&da) += (k >> 2)*Exp_msk1;
  87. if (k &= 3)
  88. dval(&da) *= 1 << k;
  89. }
  90. else {
  91. k = -k;
  92. word0(&db) += (k >> 2)*Exp_msk1;
  93. if (k &= 3)
  94. dval(&db) *= 1 << k;
  95. }
  96. #else
  97. if (k > 0)
  98. word0(&da) += k*Exp_msk1;
  99. else {
  100. k = -k;
  101. word0(&db) += k*Exp_msk1;
  102. }
  103. #endif
  104. return dval(&da) / dval(&db);
  105. }
  106. #ifdef INFNAN_CHECK
  107. int
  108. match
  109. #ifdef KR_headers
  110. (sp, t) char **sp, *t;
  111. #else
  112. (CONST char **sp, char *t)
  113. #endif
  114. {
  115. int c, d;
  116. CONST char *s = *sp;
  117. while( (d = *t++) !=0) {
  118. if ((c = *++s) >= 'A' && c <= 'Z')
  119. c += 'a' - 'A';
  120. if (c != d)
  121. return 0;
  122. }
  123. *sp = s + 1;
  124. return 1;
  125. }
  126. #endif /* INFNAN_CHECK */
  127. void
  128. #ifdef KR_headers
  129. copybits(c, n, b) ULong *c; int n; Bigint *b;
  130. #else
  131. copybits(ULong *c, int n, Bigint *b)
  132. #endif
  133. {
  134. ULong *ce, *x, *xe;
  135. #ifdef Pack_16
  136. int nw, nw1;
  137. #endif
  138. ce = c + ((n-1) >> kshift) + 1;
  139. x = b->x;
  140. #ifdef Pack_32
  141. xe = x + b->wds;
  142. while(x < xe)
  143. *c++ = *x++;
  144. #else
  145. nw = b->wds;
  146. nw1 = nw & 1;
  147. for(xe = x + (nw - nw1); x < xe; x += 2)
  148. Storeinc(c, x[1], x[0]);
  149. if (nw1)
  150. *c++ = *x;
  151. #endif
  152. while(c < ce)
  153. *c++ = 0;
  154. }
  155. ULong
  156. #ifdef KR_headers
  157. any_on(b, k) Bigint *b; int k;
  158. #else
  159. any_on(Bigint *b, int k)
  160. #endif
  161. {
  162. int n, nwds;
  163. ULong *x, *x0, x1, x2;
  164. x = b->x;
  165. nwds = b->wds;
  166. n = k >> kshift;
  167. if (n > nwds)
  168. n = nwds;
  169. else if (n < nwds && (k &= kmask)) {
  170. x1 = x2 = x[n];
  171. x1 >>= k;
  172. x1 <<= k;
  173. if (x1 != x2)
  174. return 1;
  175. }
  176. x0 = x;
  177. x += n;
  178. while(x > x0)
  179. if (*--x)
  180. return 1;
  181. return 0;
  182. }