etodec.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "ehead.h"
  2. void emovi(), emovo(), ecleaz(), eshdn8(), emdnorm();
  3. void todec();
  4. /*
  5. ; convert DEC double precision to e type
  6. ; double d;
  7. ; short e[NE];
  8. ; dectoe( &d, e );
  9. */
  10. void dectoe( d, e )
  11. unsigned short *d;
  12. unsigned short *e;
  13. {
  14. unsigned short y[NI];
  15. register unsigned short r, *p;
  16. ecleaz(y); /* start with a zero */
  17. p = y; /* point to our number */
  18. r = *d; /* get DEC exponent word */
  19. if( *d & (unsigned int )0x8000 )
  20. *p = 0xffff; /* fill in our sign */
  21. ++p; /* bump pointer to our exponent word */
  22. r &= 0x7fff; /* strip the sign bit */
  23. if( r == 0 ) /* answer = 0 if high order DEC word = 0 */
  24. goto done;
  25. r >>= 7; /* shift exponent word down 7 bits */
  26. r += EXONE - 0201; /* subtract DEC exponent offset */
  27. /* add our e type exponent offset */
  28. *p++ = r; /* to form our exponent */
  29. r = *d++; /* now do the high order mantissa */
  30. r &= 0177; /* strip off the DEC exponent and sign bits */
  31. r |= 0200; /* the DEC understood high order mantissa bit */
  32. *p++ = r; /* put result in our high guard word */
  33. *p++ = *d++; /* fill in the rest of our mantissa */
  34. *p++ = *d++;
  35. *p = *d;
  36. eshdn8(y); /* shift our mantissa down 8 bits */
  37. done:
  38. emovo( y, e );
  39. }
  40. /*
  41. ; convert e type to DEC double precision
  42. ; double d;
  43. ; short e[NE];
  44. ; etodec( e, &d );
  45. */
  46. #if 0
  47. static unsigned short decbit[NI] = {0,0,0,0,0,0,0200,0};
  48. void etodec( x, d )
  49. unsigned short *x, *d;
  50. {
  51. unsigned short xi[NI];
  52. register unsigned short r;
  53. int i, j;
  54. emovi( x, xi );
  55. *d = 0;
  56. if( xi[0] != 0 )
  57. *d = 0100000;
  58. r = xi[E];
  59. if( r < (EXONE - 128) )
  60. goto zout;
  61. i = xi[M+4];
  62. if( (i & 0200) != 0 )
  63. {
  64. if( (i & 0377) == 0200 )
  65. {
  66. if( (i & 0400) != 0 )
  67. {
  68. /* check all less significant bits */
  69. for( j=M+5; j<NI; j++ )
  70. {
  71. if( xi[j] != 0 )
  72. goto yesrnd;
  73. }
  74. }
  75. goto nornd;
  76. }
  77. yesrnd:
  78. eaddm( decbit, xi );
  79. r -= enormlz(xi);
  80. }
  81. nornd:
  82. r -= EXONE;
  83. r += 0201;
  84. if( r < 0 )
  85. {
  86. zout:
  87. *d++ = 0;
  88. *d++ = 0;
  89. *d++ = 0;
  90. *d++ = 0;
  91. return;
  92. }
  93. if( r >= 0377 )
  94. {
  95. *d++ = 077777;
  96. *d++ = -1;
  97. *d++ = -1;
  98. *d++ = -1;
  99. return;
  100. }
  101. r &= 0377;
  102. r <<= 7;
  103. eshup8( xi );
  104. xi[M] &= 0177;
  105. r |= xi[M];
  106. *d++ |= r;
  107. *d++ = xi[M+1];
  108. *d++ = xi[M+2];
  109. *d++ = xi[M+3];
  110. }
  111. #else
  112. extern int rndprc;
  113. void etodec( x, d )
  114. unsigned short *x, *d;
  115. {
  116. unsigned short xi[NI];
  117. long exp;
  118. int rndsav;
  119. emovi( x, xi );
  120. exp = (long )xi[E] - (EXONE - 0201); /* adjust exponent for offsets */
  121. /* round off to nearest or even */
  122. rndsav = rndprc;
  123. rndprc = 56;
  124. emdnorm( xi, 0, 0, exp, 64 );
  125. rndprc = rndsav;
  126. todec( xi, d );
  127. }
  128. void todec( x, y )
  129. unsigned short *x, *y;
  130. {
  131. unsigned short i;
  132. unsigned short *p;
  133. p = x;
  134. *y = 0;
  135. if( *p++ )
  136. *y = 0100000;
  137. i = *p++;
  138. if( i == 0 )
  139. {
  140. *y++ = 0;
  141. *y++ = 0;
  142. *y++ = 0;
  143. *y++ = 0;
  144. return;
  145. }
  146. if( i > 0377 )
  147. {
  148. *y++ |= 077777;
  149. *y++ = 0xffff;
  150. *y++ = 0xffff;
  151. *y++ = 0xffff;
  152. return;
  153. }
  154. i &= 0377;
  155. i <<= 7;
  156. eshup8( x );
  157. x[M] &= 0177;
  158. i |= x[M];
  159. *y++ |= i;
  160. *y++ = x[M+1];
  161. *y++ = x[M+2];
  162. *y++ = x[M+3];
  163. }
  164. #endif