home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / astrnomy / de118i.zip / ETODEC.C < prev    next >
C/C++ Source or Header  |  1993-02-28  |  3KB  |  182 lines

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