hexnan.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /****************************************************************
  2. The author of this software is David M. Gay.
  3. Copyright (C) 2000 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. static void
  27. #ifdef KR_headers
  28. L_shift(x, x1, i) ULong *x; ULong *x1; int i;
  29. #else
  30. L_shift(ULong *x, ULong *x1, int i)
  31. #endif
  32. {
  33. int j;
  34. i = 8 - i;
  35. i <<= 2;
  36. j = ULbits - i;
  37. do {
  38. *x |= x[1] << j;
  39. x[1] >>= i;
  40. } while(++x < x1);
  41. }
  42. int
  43. #ifdef KR_headers
  44. hexnan(sp, fpi, x0)
  45. CONST char **sp; FPI *fpi; ULong *x0;
  46. #else
  47. hexnan( CONST char **sp, FPI *fpi, ULong *x0)
  48. #endif
  49. {
  50. ULong c, h, *x, *x1, *xe;
  51. CONST char *s;
  52. int havedig, hd0, i, nbits;
  53. if (!hexdig['0'])
  54. hexdig_init_D2A();
  55. nbits = fpi->nbits;
  56. x = x0 + (nbits >> kshift);
  57. if (nbits & kmask)
  58. x++;
  59. *--x = 0;
  60. x1 = xe = x;
  61. havedig = hd0 = i = 0;
  62. s = *sp;
  63. /* allow optional initial 0x or 0X */
  64. while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')
  65. ++s;
  66. if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
  67. && *(CONST unsigned char*)(s+3) > ' ')
  68. s += 2;
  69. while((c = *(CONST unsigned char*)++s)) {
  70. if (!(h = hexdig[c])) {
  71. if (c <= ' ') {
  72. if (hd0 < havedig) {
  73. if (x < x1 && i < 8)
  74. L_shift(x, x1, i);
  75. if (x <= x0) {
  76. i = 8;
  77. continue;
  78. }
  79. hd0 = havedig;
  80. *--x = 0;
  81. x1 = x;
  82. i = 0;
  83. }
  84. while(*(CONST unsigned char*)(s+1) <= ' ')
  85. ++s;
  86. if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')
  87. && *(CONST unsigned char*)(s+3) > ' ')
  88. s += 2;
  89. continue;
  90. }
  91. if (/*(*/ c == ')' && havedig) {
  92. *sp = s + 1;
  93. break;
  94. }
  95. #ifndef GDTOA_NON_PEDANTIC_NANCHECK
  96. do {
  97. if (/*(*/ c == ')') {
  98. *sp = s + 1;
  99. break;
  100. }
  101. } while((c = *++s));
  102. #endif
  103. return STRTOG_NaN;
  104. }
  105. havedig++;
  106. if (++i > 8) {
  107. if (x <= x0)
  108. continue;
  109. i = 1;
  110. *--x = 0;
  111. }
  112. *x = (*x << 4) | (h & 0xf);
  113. }
  114. if (!havedig)
  115. return STRTOG_NaN;
  116. if (x < x1 && i < 8)
  117. L_shift(x, x1, i);
  118. if (x > x0) {
  119. x1 = x0;
  120. do *x1++ = *x++;
  121. while(x <= xe);
  122. do *x1++ = 0;
  123. while(x1 <= xe);
  124. }
  125. else {
  126. /* truncate high-order word if necessary */
  127. if ( (i = nbits & (ULbits-1)) !=0)
  128. *xe &= ((ULong)0xffffffff) >> (ULbits - i);
  129. }
  130. for(x1 = xe;; --x1) {
  131. if (*x1 != 0)
  132. break;
  133. if (x1 == x0) {
  134. *x1 = 1;
  135. break;
  136. }
  137. }
  138. return STRTOG_NaNbits;
  139. }