gethex.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /****************************************************************
  2. The author of this software is David M. Gay.
  3. Copyright (C) 1998 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. #ifdef USE_LOCALE
  27. #include "locale.h"
  28. #endif
  29. int
  30. #ifdef KR_headers
  31. gethex(sp, fpi, exp, bp, sign)
  32. CONST char **sp; FPI *fpi; Long *exp; Bigint **bp; int sign;
  33. #else
  34. gethex( CONST char **sp, FPI *fpi, Long *exp, Bigint **bp, int sign)
  35. #endif
  36. {
  37. Bigint *b;
  38. CONST unsigned char *decpt, *s0, *s, *s1;
  39. int big, esign, havedig, irv, j, k, n, n0, nbits, up, zret;
  40. ULong L, lostbits, *x;
  41. Long e, e1;
  42. #ifdef USE_LOCALE
  43. int i;
  44. #ifdef NO_LOCALE_CACHE
  45. const unsigned char *decimalpoint = (unsigned char*)localeconv()->decimal_point;
  46. #else
  47. const unsigned char *decimalpoint;
  48. static unsigned char *decimalpoint_cache;
  49. if (!(s0 = decimalpoint_cache)) {
  50. s0 = (unsigned char*)localeconv()->decimal_point;
  51. if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) {
  52. strlcpy(decimalpoint_cache, s0, strlen(s0) + 1);
  53. s0 = decimalpoint_cache;
  54. }
  55. }
  56. decimalpoint = s0;
  57. #endif
  58. #endif
  59. if (!hexdig['0'])
  60. hexdig_init_D2A();
  61. *bp = 0;
  62. havedig = 0;
  63. s0 = *(CONST unsigned char **)sp + 2;
  64. while(s0[havedig] == '0')
  65. havedig++;
  66. s0 += havedig;
  67. s = s0;
  68. decpt = 0;
  69. zret = 0;
  70. e = 0;
  71. if (hexdig[*s])
  72. havedig++;
  73. else {
  74. zret = 1;
  75. #ifdef USE_LOCALE
  76. for(i = 0; decimalpoint[i]; ++i) {
  77. if (s[i] != decimalpoint[i])
  78. goto pcheck;
  79. }
  80. decpt = s += i;
  81. #else
  82. if (*s != '.')
  83. goto pcheck;
  84. decpt = ++s;
  85. #endif
  86. if (!hexdig[*s])
  87. goto pcheck;
  88. while(*s == '0')
  89. s++;
  90. if (hexdig[*s])
  91. zret = 0;
  92. havedig = 1;
  93. s0 = s;
  94. }
  95. while(hexdig[*s])
  96. s++;
  97. #ifdef USE_LOCALE
  98. if (*s == *decimalpoint && !decpt) {
  99. for(i = 1; decimalpoint[i]; ++i) {
  100. if (s[i] != decimalpoint[i])
  101. goto pcheck;
  102. }
  103. decpt = s += i;
  104. #else
  105. if (*s == '.' && !decpt) {
  106. decpt = ++s;
  107. #endif
  108. while(hexdig[*s])
  109. s++;
  110. }/*}*/
  111. if (decpt)
  112. e = -(((Long)(s-decpt)) << 2);
  113. pcheck:
  114. s1 = s;
  115. big = esign = 0;
  116. switch(*s) {
  117. case 'p':
  118. case 'P':
  119. switch(*++s) {
  120. case '-':
  121. esign = 1;
  122. /* no break */
  123. case '+':
  124. s++;
  125. }
  126. if ((n = hexdig[*s]) == 0 || n > 0x19) {
  127. s = s1;
  128. break;
  129. }
  130. e1 = n - 0x10;
  131. while((n = hexdig[*++s]) !=0 && n <= 0x19) {
  132. if (e1 & 0xf8000000)
  133. big = 1;
  134. e1 = 10*e1 + n - 0x10;
  135. }
  136. if (esign)
  137. e1 = -e1;
  138. e += e1;
  139. }
  140. *sp = (char*)s;
  141. if (!havedig)
  142. *sp = (char*)s0 - 1;
  143. if (zret)
  144. return STRTOG_Zero;
  145. if (big) {
  146. if (esign) {
  147. switch(fpi->rounding) {
  148. case FPI_Round_up:
  149. if (sign)
  150. break;
  151. goto ret_tiny;
  152. case FPI_Round_down:
  153. if (!sign)
  154. break;
  155. goto ret_tiny;
  156. }
  157. goto retz;
  158. ret_tiny:
  159. b = Balloc(0);
  160. if (b == NULL)
  161. return (STRTOG_NoMemory);
  162. b->wds = 1;
  163. b->x[0] = 1;
  164. goto dret;
  165. }
  166. switch(fpi->rounding) {
  167. case FPI_Round_near:
  168. goto ovfl1;
  169. case FPI_Round_up:
  170. if (!sign)
  171. goto ovfl1;
  172. goto ret_big;
  173. case FPI_Round_down:
  174. if (sign)
  175. goto ovfl1;
  176. goto ret_big;
  177. }
  178. ret_big:
  179. nbits = fpi->nbits;
  180. n0 = n = nbits >> kshift;
  181. if (nbits & kmask)
  182. ++n;
  183. for(j = n, k = 0; j >>= 1; ++k);
  184. *bp = b = Balloc(k);
  185. if (*bp == NULL)
  186. return (STRTOG_NoMemory);
  187. b->wds = n;
  188. for(j = 0; j < n0; ++j)
  189. b->x[j] = ALL_ON;
  190. if (n > n0)
  191. b->x[j] = ULbits >> (ULbits - (nbits & kmask));
  192. *exp = fpi->emin;
  193. return STRTOG_Normal | STRTOG_Inexlo;
  194. }
  195. n = s1 - s0 - 1;
  196. for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)
  197. k++;
  198. b = Balloc(k);
  199. if (b == NULL)
  200. return (STRTOG_NoMemory);
  201. x = b->x;
  202. n = 0;
  203. L = 0;
  204. #ifdef USE_LOCALE
  205. for(i = 0; decimalpoint[i+1]; ++i);
  206. #endif
  207. while(s1 > s0) {
  208. #ifdef USE_LOCALE
  209. if (*--s1 == decimalpoint[i]) {
  210. s1 -= i;
  211. continue;
  212. }
  213. #else
  214. if (*--s1 == '.')
  215. continue;
  216. #endif
  217. if (n == ULbits) {
  218. *x++ = L;
  219. L = 0;
  220. n = 0;
  221. }
  222. L |= (hexdig[*s1] & 0x0f) << n;
  223. n += 4;
  224. }
  225. *x++ = L;
  226. b->wds = n = x - b->x;
  227. n = ULbits*n - hi0bits(L);
  228. nbits = fpi->nbits;
  229. lostbits = 0;
  230. x = b->x;
  231. if (n > nbits) {
  232. n -= nbits;
  233. if (any_on(b,n)) {
  234. lostbits = 1;
  235. k = n - 1;
  236. if (x[k>>kshift] & 1 << (k & kmask)) {
  237. lostbits = 2;
  238. if (k > 0 && any_on(b,k))
  239. lostbits = 3;
  240. }
  241. }
  242. rshift(b, n);
  243. e += n;
  244. }
  245. else if (n < nbits) {
  246. n = nbits - n;
  247. b = lshift(b, n);
  248. if (b == NULL)
  249. return (STRTOG_NoMemory);
  250. e -= n;
  251. x = b->x;
  252. }
  253. if (e > fpi->emax) {
  254. ovfl:
  255. Bfree(b);
  256. ovfl1:
  257. #ifndef NO_ERRNO
  258. errno = ERANGE;
  259. #endif
  260. return STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi;
  261. }
  262. irv = STRTOG_Normal;
  263. if (e < fpi->emin) {
  264. irv = STRTOG_Denormal;
  265. n = fpi->emin - e;
  266. if (n >= nbits) {
  267. switch (fpi->rounding) {
  268. case FPI_Round_near:
  269. if (n == nbits && (n < 2 || any_on(b,n-1)))
  270. goto one_bit;
  271. break;
  272. case FPI_Round_up:
  273. if (!sign)
  274. goto one_bit;
  275. break;
  276. case FPI_Round_down:
  277. if (sign) {
  278. one_bit:
  279. x[0] = b->wds = 1;
  280. dret:
  281. *bp = b;
  282. *exp = fpi->emin;
  283. #ifndef NO_ERRNO
  284. errno = ERANGE;
  285. #endif
  286. return STRTOG_Denormal | STRTOG_Inexhi
  287. | STRTOG_Underflow;
  288. }
  289. }
  290. Bfree(b);
  291. retz:
  292. #ifndef NO_ERRNO
  293. errno = ERANGE;
  294. #endif
  295. return STRTOG_Zero | STRTOG_Inexlo | STRTOG_Underflow;
  296. }
  297. k = n - 1;
  298. if (lostbits)
  299. lostbits = 1;
  300. else if (k > 0)
  301. lostbits = any_on(b,k);
  302. if (x[k>>kshift] & 1 << (k & kmask))
  303. lostbits |= 2;
  304. nbits -= n;
  305. rshift(b,n);
  306. e = fpi->emin;
  307. }
  308. if (lostbits) {
  309. up = 0;
  310. switch(fpi->rounding) {
  311. case FPI_Round_zero:
  312. break;
  313. case FPI_Round_near:
  314. if (lostbits & 2
  315. && (lostbits | x[0]) & 1)
  316. up = 1;
  317. break;
  318. case FPI_Round_up:
  319. up = 1 - sign;
  320. break;
  321. case FPI_Round_down:
  322. up = sign;
  323. }
  324. if (up) {
  325. k = b->wds;
  326. b = increment(b);
  327. if (b == NULL)
  328. return (STRTOG_NoMemory);
  329. x = b->x;
  330. if (irv == STRTOG_Denormal) {
  331. if (nbits == fpi->nbits - 1
  332. && x[nbits >> kshift] & 1 << (nbits & kmask))
  333. irv = STRTOG_Normal;
  334. }
  335. else if (b->wds > k
  336. || ((n = nbits & kmask) !=0
  337. && hi0bits(x[k-1]) < 32-n)) {
  338. rshift(b,1);
  339. if (++e > fpi->emax)
  340. goto ovfl;
  341. }
  342. irv |= STRTOG_Inexhi;
  343. }
  344. else
  345. irv |= STRTOG_Inexlo;
  346. }
  347. *bp = b;
  348. *exp = e;
  349. return irv;
  350. }