home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / gnu / gcc / bug / 3111 < prev    next >
Encoding:
Text File  |  1993-01-05  |  18.9 KB  |  1,072 lines

  1. Newsgroups: gnu.gcc.bug
  2. Path: sparky!uunet!cis.ohio-state.edu!kikka.hut.fi!apm
  3. From: apm@kikka.hut.fi (Antti Miettinen)
  4. Subject: gcc-2.3.3, libg++-2.3, IRIX 4.0.1, cout << 123.456
  5. Message-ID: <9301050141.AA02144@kikka.hut.fi>
  6. Sender: gnulists@ai.mit.edu
  7. Organization: GNUs Not Usenet
  8. Distribution: gnu
  9. Date: Tue, 5 Jan 1993 05:41:46 GMT
  10. Approved: bug-gcc@prep.ai.mit.edu
  11. Lines: 1059
  12.  
  13. I encountered the following problem while compiling libg++-2.3 with
  14. gcc-2.3.3 on an Indigo with IRIX 4.0.1. It may be a problem in libg++
  15. but it may also be something in gcc as libg++ behaves differently when
  16. it is compiled with different optimization level.
  17.  
  18. I configured gcc and libg++ with mips-sgi-irix4 and used flags -g -O2
  19. for all compilations. Installation gave no errors but `make check' of
  20. libg++ found a difference in tFile results. It seems that floats and
  21. doubles are output incorrectly with certain values.
  22.  
  23. Here's the piece of make output:
  24.  
  25. >./tFile < ./tFile.inp > tFile.out 2>&1
  26. >diff -c tFile.out ./tFile.exp
  27. >*** tFile.out    Sun Jan  3 05:22:36 1993
  28. >--- ./tFile.exp    Tue Nov  3 08:31:35 1992
  29. >***************
  30. >*** 4,11 ****
  31. >  enter three integers (short, int, long):first  = 123 via dec =      123
  32. >  second = 4567 via form = 4567 = 010727 via cout.form = 4567 = 0x11d7
  33. >  third  = 89012 via hex = 15bb4
  34. >! enter a float then a double:first  = <3.456
  35. >! second = -0.00<
  36. >  enter 5 characters separated with spaces:first  = 1
  37. >  rest   =  2 3 4 5
  38. >  
  39. >--- 4,11 ----
  40. >  enter three integers (short, int, long):first  = 123 via dec =      123
  41. >  second = 4567 via form = 4567 = 010727 via cout.form = 4567 = 0x11d7
  42. >  third  = 89012 via hex = 15bb4
  43. >! enter a float then a double:first  = 123.456
  44. >! second = -0.012
  45. >  enter 5 characters separated with spaces:first  = 1
  46. >  rest   =  2 3 4 5
  47.  
  48. I built libg++ with -g -O1 and after that tFile passed (but make check
  49. failed in tformat) so this may be a bug in gcc. The problem seems to
  50. be function dtoa() in file `floatconv.C'. When it is compiled with -O2
  51. it gives funny results. The following is at least a bit stripped down
  52. (-E plus some editing) test program:
  53.  
  54. ----------------------------------------------------------------
  55. #include <malloc.h>
  56.  
  57. struct Bigint {
  58.   struct Bigint *next;
  59.   int k, maxwds, sign, wds;
  60.   unsigned long x[1];
  61. };
  62.  
  63. typedef struct Bigint Bigint;
  64.  
  65. static Bigint *freelist[15 +1];
  66.  
  67. static Bigint *
  68. Balloc (int k)
  69. {
  70.   int x;
  71.   Bigint *rv;
  72.  
  73.   if (rv = freelist[k]) {
  74.     freelist[k] = rv->next;
  75.   }
  76.   else {
  77.     x = 1 << k;
  78.     rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(long));
  79.     rv->k = k;
  80.     rv->maxwds = x;
  81.   }
  82.   rv->sign = rv->wds = 0;
  83.   return rv;
  84. }
  85.  
  86. static void
  87. Bfree (Bigint *v)
  88. {
  89.   if (v) {
  90.     v->next = freelist[v->k];
  91.     freelist[v->k] = v;
  92.   }
  93. }
  94.  
  95. static int HIWORD = -1, LOWORD;
  96.  
  97. static void test_endianness()
  98. {
  99.     union doubleword {
  100.     double d;
  101.     unsigned long u[2];
  102.     } dw;
  103.     dw.d = 10;
  104.     if (dw.u[0] != 0)  
  105.     HIWORD=0, LOWORD=1;
  106.     else
  107.     HIWORD=1, LOWORD=0;
  108. }
  109.  
  110. static int
  111. hi0bits (register unsigned long x)
  112. {
  113.   register int k = 0;
  114.  
  115.   if (!(x & 0xffff0000)) {
  116.     k = 16;
  117.     x <<= 16;
  118.   }
  119.   if (!(x & 0xff000000)) {
  120.     k += 8;
  121.     x <<= 8;
  122.   }
  123.   if (!(x & 0xf0000000)) {
  124.     k += 4;
  125.     x <<= 4;
  126.   }
  127.   if (!(x & 0xc0000000)) {
  128.     k += 2;
  129.     x <<= 2;
  130.   }
  131.   if (!(x & 0x80000000)) {
  132.     k++;
  133.     if (!(x & 0x40000000))
  134.       return 32;
  135.   }
  136.   return k;
  137. }
  138.  
  139. static int
  140. lo0bits (unsigned long *y)
  141. {
  142.   register int k;
  143.   register unsigned long x = *y;
  144.  
  145.   if (x & 7) {
  146.     if (x & 1)
  147.       return 0;
  148.     if (x & 2) {
  149.       *y = x >> 1;
  150.       return 1;
  151.     }
  152.     *y = x >> 2;
  153.     return 2;
  154.   }
  155.   k = 0;
  156.   if (!(x & 0xffff)) {
  157.     k = 16;
  158.     x >>= 16;
  159.   }
  160.   if (!(x & 0xff)) {
  161.     k += 8;
  162.     x >>= 8;
  163.   }
  164.   if (!(x & 0xf)) {
  165.     k += 4;
  166.     x >>= 4;
  167.   }
  168.   if (!(x & 0x3)) {
  169.     k += 2;
  170.     x >>= 2;
  171.   }
  172.   if (!(x & 1)) {
  173.     k++;
  174.     x >>= 1;
  175.     if (!x & 1)
  176.       return 32;
  177.   }
  178.   *y = x;
  179.   return k;
  180. }
  181.  
  182. static Bigint *
  183. d2b (double d, int *e, int *bits)
  184. {
  185.   Bigint *b;
  186.   int de, i, k;
  187.   unsigned long *x, y, z;
  188.  
  189.   b = Balloc(1);
  190.  
  191.   x = b->x;
  192.  
  193.   z = ((unsigned long *)&d)[HIWORD]   &  0xfffff ;
  194.   ((unsigned long *)&d)[HIWORD]   &= 0x7fffffff;        
  195.  
  196.   if (de = (int)(((unsigned long *)&d)[HIWORD]   >>  20 ))
  197.     z |=    0x100000 ;
  198.  
  199.   if (y = ((unsigned long *)&d)[LOWORD]  ) {
  200.     if (k = lo0bits(&y)) {
  201.       x[0] = y | z << 32 - k;
  202.       z >>= k;
  203.     }
  204.     else
  205.       x[0] = y;
  206.     i = b->wds = (x[1] = z) ? 2 : 1;
  207.   }
  208.   else {
  209.     k = lo0bits(&z);
  210.     x[0] = z;
  211.     i = b->wds = 1;
  212.     k += 32;
  213.   }
  214.  
  215.   if (de) {
  216.     *e = de - 1023  - (53 -1) + k;
  217.     *bits = 53  - k;
  218.   }
  219.   else {
  220.     *e = de - 1023  - (53 -1) + 1 + k;
  221.     *bits = 32*i - hi0bits(x[i-1]);
  222.   }
  223.  
  224.   return b;
  225. }
  226.  
  227. static Bigint *
  228. multadd (Bigint *b, int m, int a)        
  229. {
  230.   int i, wds;
  231.   unsigned long *x, y;
  232.   
  233.   unsigned long xi, z;
  234.   
  235.   Bigint *b1;
  236.   
  237.   wds = b->wds;
  238.   x = b->x;
  239.   i = 0;
  240.   do {
  241.     xi = *x;
  242.     y = (xi & 0xffff) * m + a;
  243.     z = (xi >> 16) * m + (y >> 16);
  244.     a = (int)(z >> 16);
  245.     *x++ = (z << 16) + (y & 0xffff);
  246.   }
  247.   while(++i < wds);
  248.   if (a) {
  249.     if (wds >= b->maxwds) {
  250.       b1 = Balloc(b->k+1);
  251.       memcpy((char *)&b1->sign, (char *)& b->sign,
  252.          b->wds*sizeof(long) + 2*sizeof(int)) ;
  253.       Bfree(b);
  254.       b = b1;
  255.     }
  256.     b->x[wds++] = a;
  257.     b->wds = wds;
  258.   }
  259.   return b;
  260. }
  261.  
  262. static Bigint *
  263. mult (Bigint *a, Bigint *b)
  264. {
  265.   Bigint *c;
  266.   int k, wa, wb, wc;
  267.   unsigned long carry, y, z;
  268.   unsigned long *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  269.  
  270.   unsigned long z2;
  271.  
  272.   if (a->wds < b->wds) {
  273.     c = a;
  274.     a = b;
  275.     b = c;
  276.   }
  277.   k = a->k;
  278.   wa = a->wds;
  279.   wb = b->wds;
  280.   wc = wa + wb;
  281.   if (wc > a->maxwds)
  282.     k++;
  283.   c = Balloc(k);
  284.   for(x = c->x, xa = x + wc; x < xa; x++)
  285.     *x = 0;
  286.   xa = a->x;
  287.   xae = xa + wa;
  288.   xb = b->x;
  289.   xbe = xb + wb;
  290.   xc0 = c->x;
  291.  
  292.   for(; xb < xbe; xb++, xc0++) {
  293.     if (y = *xb & 0xffff) {
  294.       x = xa;
  295.       xc = xc0;
  296.       carry = 0;
  297.       do {
  298.     z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  299.     carry = z >> 16;
  300.     z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  301.     carry = z2 >> 16;
  302.     (*xc++ =  z2 << 16 |  z & 0xffff) ;
  303.       }
  304.       while(x < xae);
  305.       *xc = carry;
  306.     }
  307.     if (y = *xb >> 16) {
  308.       x = xa;
  309.       xc = xc0;
  310.       carry = 0;
  311.       z2 = *xc;
  312.       do {
  313.     z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  314.     carry = z >> 16;
  315.     (*xc++ =  z << 16 |  z2 & 0xffff) ;
  316.     z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  317.     carry = z2 >> 16;
  318.       }
  319.       while(x < xae);
  320.       *xc = z2;
  321.     }
  322.   }
  323.  
  324.   for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
  325.   c->wds = wc;
  326.   return c;
  327. }
  328.  
  329. static Bigint *p5s;
  330.  
  331. static Bigint *
  332. i2b (int i)
  333. {
  334.   Bigint *b;
  335.  
  336.   b = Balloc(1);
  337.   b->x[0] = i;
  338.   b->wds = 1;
  339.   return b;
  340. }
  341.  
  342. static Bigint *
  343. pow5mult (Bigint *b, int k)
  344. {
  345.   Bigint *b1, *p5, *p51;
  346.   int i;
  347.   static int p05[3] = { 5, 25, 125 };
  348.   
  349.   if (i = k & 3)
  350.     b = multadd(b, p05[i-1], 0);
  351.   
  352.   if (!(k >>= 2))
  353.     return b;
  354.   if (!(p5 = p5s)) {
  355.     p5 = p5s = i2b(625);
  356.     p5->next = 0;
  357.   }
  358.   for(;;) {
  359.     if (k & 1) {
  360.       b1 = mult(b, p5);
  361.       Bfree(b);
  362.       b = b1;
  363.     }
  364.     if (!(k >>= 1))
  365.       break;
  366.     if (!(p51 = p5->next)) {
  367.       p51 = p5->next = mult(p5,p5);
  368.       p51->next = 0;
  369.     }
  370.     p5 = p51;
  371.   }
  372.   return b;
  373. }
  374.  
  375. static Bigint *
  376. lshift (Bigint *b, int k)
  377. {
  378.   int i, k1, n, n1;
  379.   Bigint *b1;
  380.   unsigned long *x, *x1, *xe, z;
  381.  
  382.   n = k >> 5;
  383.   k1 = b->k;
  384.   n1 = n + b->wds + 1;
  385.   for(i = b->maxwds; n1 > i; i <<= 1)
  386.     k1++;
  387.   b1 = Balloc(k1);
  388.   x1 = b1->x;
  389.   for(i = 0; i < n; i++)
  390.     *x1++ = 0;
  391.   x = b->x;
  392.   xe = x + b->wds;
  393.  
  394.   if (k &= 0x1f) {
  395.     k1 = 32 - k;
  396.     z = 0;
  397.     do {
  398.       *x1++ = *x << k | z;
  399.       z = *x++ >> k1;
  400.     }
  401.     while(x < xe);
  402.     if (*x1 = z)
  403.       ++n1;
  404.   }
  405.   else do
  406.     *x1++ = *x++;
  407.   while(x < xe);
  408.   b1->wds = n1 - 1;
  409.   Bfree(b);
  410.   return b1;
  411. }
  412.  
  413. static int
  414. cmp (Bigint *a, Bigint *b)
  415. {
  416.   unsigned long *xa, *xa0, *xb, *xb0;
  417.   int i, j;
  418.   
  419.   i = a->wds;
  420.   j = b->wds;
  421.   
  422.   if (i -= j)
  423.     return i;
  424.   xa0 = a->x;
  425.   xa = xa0 + j;
  426.   xb0 = b->x;
  427.   xb = xb0 + j;
  428.   for(;;) {
  429.     if (*--xa != *--xb)
  430.       return *xa < *xb ? -1 : 1;
  431.     if (xa <= xa0)
  432.       break;
  433.   }
  434.   return 0;
  435. }
  436.  
  437. static int quorem (Bigint *b, Bigint *S)
  438. {
  439.   int n;
  440.   long borrow, y;
  441.   unsigned long carry, q, ys;
  442.   unsigned long *bx, *bxe, *sx, *sxe;
  443.   
  444.   long z;
  445.   unsigned long si, zs;
  446.    
  447.   n = S->wds;
  448.  
  449.   if (b->wds < n)
  450.     return 0;
  451.   sx = S->x;
  452.   sxe = sx + --n;
  453.   bx = b->x;
  454.   bxe = bx + n;
  455.   q = *bxe / (*sxe + 1);   
  456.   
  457.   if (q) {
  458.     borrow = 0;
  459.     carry = 0;
  460.     do {
  461.       si = *sx++;
  462.       ys = (si & 0xffff) * q + carry;
  463.       zs = (si >> 16) * q + (ys >> 16);
  464.       carry = zs >> 16;
  465.       y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
  466.       borrow = y >> 16;
  467.       ;
  468.       z = (*bx >> 16) - (zs & 0xffff) + borrow;
  469.       borrow = z >> 16;
  470.       ;
  471.       (*bx++ =  z << 16 |  y & 0xffff) ;
  472.     }
  473.     while(sx <= sxe);
  474.     if (!*bxe) {
  475.       bx = b->x;
  476.       while(--bxe > bx && !*bxe)
  477.     --n;
  478.       b->wds = n;
  479.     }
  480.   }
  481.   if (cmp(b, S) >= 0) {
  482.     q++;
  483.     borrow = 0;
  484.     carry = 0;
  485.     bx = b->x;
  486.     sx = S->x;
  487.     do {
  488.       si = *sx++;
  489.       ys = (si & 0xffff) + carry;
  490.       zs = (si >> 16) + (ys >> 16);
  491.       carry = zs >> 16;
  492.       y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
  493.       borrow = y >> 16;
  494.       ;
  495.       z = (*bx >> 16) - (zs & 0xffff) + borrow;
  496.       borrow = z >> 16;
  497.       ;
  498.       (*bx++ =  z << 16 |  y & 0xffff) ;
  499.     }
  500.     while(sx <= sxe);
  501.     bx = b->x;
  502.     bxe = bx + n;
  503.     if (!*bxe) {
  504.       while(--bxe > bx && !*bxe)
  505.     --n;
  506.       b->wds = n;
  507.     }
  508.   }
  509.   return q;
  510. }
  511.  
  512. static Bigint *
  513. diff (Bigint *a, Bigint *b)
  514. {
  515.   Bigint *c;
  516.   int i, wa, wb;
  517.   long borrow, y;  
  518.   unsigned long *xa, *xae, *xb, *xbe, *xc;
  519.   
  520.   long z;
  521.   
  522.   i = cmp(a,b);
  523.   if (!i) {
  524.     c = Balloc(0);
  525.     c->wds = 1;
  526.     c->x[0] = 0;
  527.     return c;
  528.   }
  529.   if (i < 0) {
  530.     c = a;
  531.     a = b;
  532.     b = c;
  533.     i = 1;
  534.   }
  535.   else
  536.     i = 0;
  537.   c = Balloc(a->k);
  538.   c->sign = i;
  539.   wa = a->wds;
  540.   xa = a->x;
  541.   xae = xa + wa;
  542.   wb = b->wds;
  543.   xb = b->x;
  544.   xbe = xb + wb;
  545.   xc = c->x;
  546.   borrow = 0;
  547.   
  548.   do {
  549.     y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
  550.     borrow = y >> 16;
  551.     ;
  552.     z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
  553.     borrow = z >> 16;
  554.     ;
  555.     (*xc++ =  z << 16 |  y & 0xffff) ;
  556.   }
  557.   while(xb < xbe);
  558.   while(xa < xae) {
  559.     y = (*xa & 0xffff) + borrow;
  560.     borrow = y >> 16;
  561.     ;
  562.     z = (*xa++ >> 16) + borrow;
  563.     borrow = z >> 16;
  564.     ;
  565.     (*xc++ =  z << 16 |  y & 0xffff) ;
  566.   }
  567.   
  568.   while(!*--xc)
  569.     wa--;
  570.   c->wds = wa;
  571.   return c;
  572. }
  573.  
  574. static double tens[] = {
  575.   1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  576.   1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  577.   1e20, 1e21, 1e22,
  578. };
  579.  
  580. static double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  581.  
  582. static char *
  583. dtoa (double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
  584. {
  585.   int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1;
  586.   int j, j1, k, k0, k_check, leftright, m2, m5, s2, s5;
  587.   int spec_case, try_quick;
  588.   long L;
  589.   
  590.   int denorm;
  591.   unsigned long x;
  592.   
  593.   Bigint *b, *b1, *delta, *mlo, *mhi, *S;
  594.   double d2, ds, eps;
  595.   char *s, *s0;
  596.   static Bigint *result;
  597.   static int result_k;
  598.   
  599.   if (HIWORD<0) test_endianness(); ;
  600.   if (result) {
  601.     result->k = result_k;
  602.     result->maxwds = 1 << result_k;
  603.     Bfree(result);
  604.     result = 0;
  605.   }
  606.   
  607.   if (((unsigned long *)&d)[HIWORD]  & 0x80000000 ) {
  608.     *sign = 1;
  609.     ((unsigned long *)&d)[HIWORD]  &= ~0x80000000 ;   
  610.   }
  611.   else
  612.     *sign = 0;
  613.   
  614.   if ((((unsigned long *)&d)[HIWORD]  &  0x7ff00000 ) ==  0x7ff00000 )    
  615.     {
  616.       *decpt = 9999;
  617.       s = !((unsigned long *)&d)[LOWORD]  && !(((unsigned long *)&d)[HIWORD]
  618.                            & 0xfffff) ? "Infinity" : "NaN";
  619.       if (rve)
  620.     *rve = s[3] ? s + 8 :
  621.       s + 3;
  622.       return s;
  623.     }
  624.   
  625.   if (!d) {
  626.     *decpt = 1;
  627.     s = "0";
  628.     if (rve)
  629.       *rve = s + 1;
  630.     return s;
  631.   }
  632.   
  633.   b = d2b(d, &be, &bbits);
  634.  
  635.   if (i = (int)(((unsigned long *)&d)[HIWORD]  >> 20  & ( 0x7ff00000 >>20 ))) {
  636.     d2 = d;
  637.     ((unsigned long *)&d2)[HIWORD]  &= 0xfffff ;
  638.     ((unsigned long *)&d2)[HIWORD]  |= 0x3ff00000 ;
  639.     i -= 1023 ;
  640.     denorm = 0;
  641.   }
  642.   else {
  643.     i = bbits + be + (1023  + (53 -1) - 1);
  644.     x = i > 32  ? ((unsigned long *)&d)[HIWORD]  << 64 - i
  645.       | ((unsigned long *)&d)[LOWORD]  >> i - 32
  646.       : ((unsigned long *)&d)[LOWORD]  << 32 - i;
  647.     d2 = x;
  648.     ((unsigned long *)&d2)[HIWORD]  -= 31*   0x100000 ;  
  649.     i -= (1023  + (53 -1) - 1) + 1;
  650.     denorm = 1;
  651.   }
  652.   
  653.   ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
  654.   k = (int)ds;
  655.   if (ds < 0. && ds != k)
  656.     k--;     
  657.   k_check = 1;
  658.   if (k >= 0 && k <= 22 ) {
  659.     if (d < tens[k])
  660.       k--;
  661.     k_check = 0;
  662.   }
  663.   j = bbits - i - 1;
  664.   if (j >= 0) {
  665.     b2 = 0;
  666.     s2 = j;
  667.   }
  668.   else {
  669.     b2 = -j;
  670.     s2 = 0;
  671.   }
  672.   if (k >= 0) {
  673.     b5 = 0;
  674.     s5 = k;
  675.     s2 += k;
  676.   }
  677.   else {
  678.     b2 -= k;
  679.     b5 = -k;
  680.     s5 = 0;
  681.   }
  682.   if (mode < 0 || mode > 9)
  683.     mode = 0;
  684.   try_quick = 1;
  685.   if (mode > 5) {
  686.     mode -= 4;
  687.     try_quick = 0;
  688.   }
  689.   leftright = 1;
  690.   switch(mode) {
  691.   case 0:
  692.   case 1:
  693.     ilim = ilim1 = -1;
  694.     i = 18;
  695.     ndigits = 0;
  696.     break;
  697.   case 2:
  698.     leftright = 0;
  699.   case 4:
  700.     if (ndigits <= 0)
  701.       ndigits = 1;
  702.     ilim = ilim1 = i = ndigits;
  703.     break;
  704.   case 3:
  705.     leftright = 0;
  706.   case 5:
  707.     i = ndigits + k + 1;
  708.     ilim = i;
  709.     ilim1 = i - 1;
  710.     if (i <= 0)
  711.       i = 1;
  712.   }
  713.   j = sizeof(unsigned long);
  714.   for(result_k = 0; sizeof(Bigint) - sizeof(unsigned long) + j < i;
  715.       j <<= 1) result_k++;
  716.   result = Balloc(result_k);
  717.   s = s0 = (char *)result;
  718.   
  719.   if (ilim >= 0 && ilim <= 14  && try_quick) {
  720.     i = 0;
  721.     d2 = d;
  722.     k0 = k;
  723.     ilim0 = ilim;
  724.     ieps = 2;  
  725.     if (k > 0) {
  726.       ds = tens[k&0xf];
  727.       j = k >> 4;
  728.       if (j & 0x10 ) {
  729.     j &= 0x10  - 1;
  730.     d /= bigtens[5 -1];
  731.     ieps++;
  732.       }
  733.       for(; j; j >>= 1, i++)
  734.     if (j & 1) {
  735.       ieps++;
  736.       ds *= bigtens[i];
  737.     }
  738.       d /= ds;
  739.     }
  740.     else if (j1 = -k) {
  741.       d *= tens[j1 & 0xf];
  742.       for(j = j1 >> 4; j; j >>= 1, i++)
  743.     if (j & 1) {
  744.       ieps++;
  745.       d *= bigtens[i];
  746.     }
  747.     }
  748.     if (k_check && d < 1. && ilim > 0) {
  749.       if (ilim1 <= 0)
  750.     goto fast_failed;
  751.       ilim = ilim1;
  752.       k--;
  753.       d *= 10.;
  754.       ieps++;
  755.     }
  756.     eps = ieps*d + 7.;
  757.     ((unsigned long *)&eps)[HIWORD]  -= (53 -1)*   0x100000 ;
  758.     if (ilim == 0) {
  759.       S = mhi = 0;
  760.       d -= 5.;
  761.       if (d > eps)
  762.     goto one_digit;
  763.       if (d < -eps)
  764.     goto no_digits;
  765.       goto fast_failed;
  766.     }
  767.     
  768.     if (leftright) {
  769.       eps = 0.5/tens[ilim-1] - eps;
  770.       for(i = 0;;) {
  771.     L = (long)d;
  772.     d -= L;
  773.     *s++ = '0' + (int)L;
  774.     if (d < eps)
  775.       goto ret1;
  776.     if (1. - d < eps)
  777.       goto bump_up;
  778.     if (++i >= ilim)
  779.       break;
  780.     eps *= 10.;
  781.     d *= 10.;
  782.       }
  783.     }
  784.     else {
  785.       eps *= tens[ilim-1];
  786.       for(i = 1;; i++, d *= 10.) {
  787.     L = (long)d;
  788.     d -= L;
  789.     *s++ = '0' + (int)L;
  790.     if (i == ilim) {
  791.       if (d > 0.5 + eps)
  792.         goto bump_up;
  793.       else if (d < 0.5 - eps) {
  794.         while(*--s == '0');
  795.         s++;
  796.         goto ret1;
  797.       }
  798.       break;
  799.     }
  800.       }
  801.       
  802.     }
  803.     
  804.   fast_failed:
  805.     s = s0;
  806.     d = d2;
  807.     k = k0;
  808.     ilim = ilim0;
  809.   }
  810.   
  811.   if (be >= 0 && k <= 14 ) {
  812.     
  813.     ds = tens[k];
  814.     if (ndigits < 0 && ilim <= 0) {
  815.       S = mhi = 0;
  816.       if (ilim < 0 || d <= 5*ds)
  817.     goto no_digits;
  818.       goto one_digit;
  819.     }
  820.     for(i = 1;; i++) {
  821.       L = (long)(d / ds);
  822.       d -= L*ds;
  823.       
  824.       *s++ = '0' + (int)L;
  825.       if (i == ilim) {
  826.     d += d;
  827.     if (d > ds || d == ds && L & 1) {
  828.     bump_up:
  829.       while(*--s == '9')
  830.         if (s == s0) {
  831.           k++;
  832.           *s = '0';
  833.           break;
  834.         }
  835.       ++*s++;
  836.     }
  837.     break;
  838.       }
  839.       if (!(d *= 10.))
  840.     break;
  841.     }
  842.     goto ret1;
  843.   }
  844.   
  845.   m2 = b2;
  846.   m5 = b5;
  847.   mhi = mlo = 0;
  848.   if (leftright) {
  849.     if (mode < 2) {
  850.       i = denorm ? be + (1023  + (53 -1) - 1 + 1) :
  851.       1 + 53  - bbits;
  852.     }
  853.     else {
  854.       j = ilim - 1;
  855.       if (m5 >= j)
  856.     m5 -= j;
  857.       else {
  858.     s5 += j -= m5;
  859.     b5 += j;
  860.     m5 = 0;
  861.       }
  862.       if ((i = ilim) < 0) {
  863.     m2 -= i;
  864.     i = 0;
  865.       }
  866.     }
  867.     b2 += i;
  868.     s2 += i;
  869.     mhi = i2b(1);
  870.   }
  871.   if (m2 > 0 && s2 > 0) {
  872.     i = m2 < s2 ? m2 : s2;
  873.     b2 -= i;
  874.     m2 -= i;
  875.     s2 -= i;
  876.   }
  877.   if (b5 > 0) {
  878.     if (leftright) {
  879.       if (m5 > 0) {
  880.     mhi = pow5mult(mhi, m5);
  881.     b1 = mult(mhi, b);
  882.     Bfree(b);
  883.     b = b1;
  884.       }
  885.       if (j = b5 - m5)
  886.     b = pow5mult(b, j);
  887.     }
  888.     else
  889.       b = pow5mult(b, b5);
  890.   }
  891.   S = i2b(1);
  892.   if (s5 > 0)
  893.     S = pow5mult(S, s5);
  894.   
  895.   if (mode < 2) {
  896.     if (!((unsigned long *)&d)[LOWORD]
  897.     && !(((unsigned long *)&d)[HIWORD]  &  0xfffff )
  898.     && ((unsigned long *)&d)[HIWORD]  &  0x7ff00000 ) {
  899.       b2 += 1 ;
  900.       s2 += 1 ;
  901.       spec_case = 1;
  902.     }
  903.     else
  904.       spec_case = 0;
  905.   }
  906.   
  907.   if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f)
  908.     i = 32 - i;
  909.  
  910.   if (i > 4) {
  911.     i -= 4;
  912.     b2 += i;
  913.     m2 += i;
  914.     s2 += i;
  915.   }
  916.   else if (i < 4) {
  917.     i += 28;
  918.     b2 += i;
  919.     m2 += i;
  920.     s2 += i;
  921.   }
  922.   if (b2 > 0)
  923.     b = lshift(b, b2);
  924.   if (s2 > 0)
  925.     S = lshift(S, s2);
  926.   if (k_check) {
  927.     if (cmp(b,S) < 0) {
  928.       k--;
  929.       b = multadd(b, 10, 0);   
  930.       if (leftright)
  931.     mhi = multadd(mhi, 10, 0);
  932.       ilim = ilim1;
  933.     }
  934.   }
  935.   if (ilim <= 0 && mode > 2) {
  936.     if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
  937.       
  938.     no_digits:
  939.       k = -1 - ndigits;
  940.       goto ret;
  941.     }
  942.   one_digit:
  943.     *s++ = '1';
  944.     k++;
  945.     goto ret;
  946.   }
  947.   if (leftright) {
  948.     if (m2 > 0)
  949.       mhi = lshift(mhi, m2);
  950.     
  951.     mlo = mhi;
  952.     if (spec_case) {
  953.       mhi = Balloc(mhi->k);
  954.       memcpy((char *)&mhi->sign, (char *)& mlo->sign,
  955.          mlo->wds*sizeof(long) + 2*sizeof(int)) ;
  956.       mhi = lshift(mhi, 1 );
  957.     }
  958.     
  959.     for(i = 1;;i++) {
  960.       dig = quorem(b,S) + '0';
  961.  
  962.       j = cmp(b, mlo);
  963.       delta = diff(S, mhi);
  964.       j1 = delta->sign ? 1 : cmp(b, delta);
  965.       Bfree(delta);
  966.       
  967.       if (j1 == 0 && !mode && !(((unsigned long *)&d)[LOWORD]  & 1)) {
  968.     if (dig == '9')
  969.       goto round_9_up;
  970.     if (j > 0)
  971.       dig++;
  972.     *s++ = dig;
  973.     goto ret;
  974.       }
  975.       
  976.       if (j < 0 || j == 0 && !mode
  977.       
  978.       && !(((unsigned long *)&d)[LOWORD]  & 1)
  979.       
  980.       ) {
  981.     if (j1 > 0) {
  982.       b = lshift(b, 1);
  983.       j1 = cmp(b, S);
  984.       if ((j1 > 0 || j1 == 0 && dig & 1)
  985.           && dig++ == '9')
  986.         goto round_9_up;
  987.     }
  988.     *s++ = dig;
  989.     goto ret;
  990.       }
  991.       if (j1 > 0) {
  992.     if (dig == '9') {  
  993.     round_9_up:
  994.       *s++ = '9';
  995.       goto roundoff;
  996.     }
  997.     *s++ = dig + 1;
  998.     goto ret;
  999.       }
  1000.       *s++ = dig;
  1001.       if (i == ilim)
  1002.     break;
  1003.       b = multadd(b, 10, 0);
  1004.       if (mlo == mhi)
  1005.     mlo = mhi = multadd(mhi, 10, 0);
  1006.       else {
  1007.     mlo = multadd(mlo, 10, 0);
  1008.     mhi = multadd(mhi, 10, 0);
  1009.       }
  1010.     }
  1011.   }
  1012.   else
  1013.     for(i = 1;; i++) {
  1014.       *s++ = dig = quorem(b,S) + '0';
  1015.       if (i >= ilim)
  1016.     break;
  1017.       b = multadd(b, 10, 0);
  1018.     }
  1019.   
  1020.   
  1021.   
  1022.   b = lshift(b, 1);
  1023.   j = cmp(b, S);
  1024.   if (j > 0 || j == 0 && dig & 1) {
  1025.   roundoff:
  1026.     while(*--s == '9')
  1027.       if (s == s0) {
  1028.     k++;
  1029.     *s++ = '1';
  1030.     goto ret;
  1031.       }
  1032.     ++*s++;
  1033.   }
  1034.   else {
  1035.     while(*--s == '0');
  1036.     s++;
  1037.   }
  1038.  ret:
  1039.   Bfree(S);
  1040.   if (mhi) {
  1041.     if (mlo && mlo != mhi)
  1042.       Bfree(mlo);
  1043.     Bfree(mhi);
  1044.   }
  1045.  ret1:
  1046.   Bfree(b);
  1047.   *s = 0;
  1048.   *decpt = k + 1;
  1049.   if (rve)
  1050.     *rve = s;
  1051.   return s0;
  1052. }
  1053.  
  1054. #include <stdio.h>
  1055.  
  1056. main()
  1057. {
  1058.   int decpt, sign;
  1059.   char *rve;
  1060.  
  1061.   printf ("%s\n", dtoa(123.456, 2, 6, &decpt, &sign, &rve));
  1062. }
  1063. ----------------------------------------------------------------
  1064.  
  1065. When compiled with -O2 -fno-schedule-insns it outputs `123456'. When
  1066. compiled with -O -fschedule-insns it outputs `<3456'.
  1067.  
  1068. With gcc-2.2.2 the program outputs `123456' when compiled with -O2.
  1069.  
  1070.  
  1071.  
  1072.