strtorx.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /****************************************************************
  2. The author of this software is David M. Gay.
  3. Copyright (C) 1998, 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. #undef _0
  27. #undef _1
  28. /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
  29. #ifdef IEEE_MC68k
  30. #define _0 0
  31. #define _1 1
  32. #define _2 2
  33. #define _3 3
  34. #define _4 4
  35. #endif
  36. #ifdef IEEE_8087
  37. #define _0 4
  38. #define _1 3
  39. #define _2 2
  40. #define _3 1
  41. #define _4 0
  42. #endif
  43. void
  44. #ifdef KR_headers
  45. ULtox(L, bits, exp, k) UShort *L; ULong *bits; Long exp; int k;
  46. #else
  47. ULtox(UShort *L, ULong *bits, Long exp, int k)
  48. #endif
  49. {
  50. switch(k & STRTOG_Retmask) {
  51. case STRTOG_NoNumber:
  52. case STRTOG_Zero:
  53. L[0] = L[1] = L[2] = L[3] = L[4] = 0;
  54. break;
  55. case STRTOG_Denormal:
  56. L[_0] = 0;
  57. goto normal_bits;
  58. case STRTOG_Normal:
  59. case STRTOG_NaNbits:
  60. L[_0] = exp + 0x3fff + 63;
  61. normal_bits:
  62. L[_4] = (UShort)bits[0];
  63. L[_3] = (UShort)(bits[0] >> 16);
  64. L[_2] = (UShort)bits[1];
  65. L[_1] = (UShort)(bits[1] >> 16);
  66. break;
  67. case STRTOG_NoMemory:
  68. errno = ERANGE;
  69. /* FALLTHROUGH */
  70. case STRTOG_Infinite:
  71. L[_0] = 0x7fff;
  72. L[_1] = 0x8000;
  73. L[_2] = L[_3] = L[_4] = 0;
  74. break;
  75. case STRTOG_NaN:
  76. L[0] = ldus_QNAN0;
  77. L[1] = ldus_QNAN1;
  78. L[2] = ldus_QNAN2;
  79. L[3] = ldus_QNAN3;
  80. L[4] = ldus_QNAN4;
  81. }
  82. if (k & STRTOG_Neg)
  83. L[_0] |= 0x8000;
  84. }
  85. int
  86. #ifdef KR_headers
  87. strtorx(s, sp, rounding, L) CONST char *s; char **sp; int rounding; void *L;
  88. #else
  89. strtorx(CONST char *s, char **sp, int rounding, void *L)
  90. #endif
  91. {
  92. static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI };
  93. FPI *fpi, fpi1;
  94. ULong bits[2];
  95. Long exp;
  96. int k;
  97. fpi = &fpi0;
  98. if (rounding != FPI_Round_near) {
  99. fpi1 = fpi0;
  100. fpi1.rounding = rounding;
  101. fpi = &fpi1;
  102. }
  103. k = strtodg(s, sp, fpi, &exp, bits);
  104. ULtox((UShort*)L, bits, exp, k);
  105. return k;
  106. }