home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Source / GNU / cctools / as / atof-ieee.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-25  |  15.2 KB  |  576 lines

  1. /* atof_ieee.c - turn a Flonum into an IEEE floating point number
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "flonum.h"
  21. #ifdef USG
  22. #define bzero(s,n) memset(s,0,n)
  23. #define bcopy(from,to,n) memcpy((to),(from),(n))
  24. #endif
  25.  
  26. extern FLONUM_TYPE generic_floating_point_number; /* Flonums returned here. */
  27. #define NULL (0)
  28.  
  29. extern char EXP_CHARS[];
  30.                 /* Precision in LittleNums. */
  31. #define MAX_PRECISION (6)
  32. #define F_PRECISION (2)
  33. #define D_PRECISION (4)
  34. #define X_PRECISION (6)
  35. #define P_PRECISION (6)
  36.  
  37.                 /* Length in LittleNums of guard bits. */
  38. #define GUARD (2)
  39.  
  40. static unsigned long int mask [] = {
  41.   0x00000000,
  42.   0x00000001,
  43.   0x00000003,
  44.   0x00000007,
  45.   0x0000000f,
  46.   0x0000001f,
  47.   0x0000003f,
  48.   0x0000007f,
  49.   0x000000ff,
  50.   0x000001ff,
  51.   0x000003ff,
  52.   0x000007ff,
  53.   0x00000fff,
  54.   0x00001fff,
  55.   0x00003fff,
  56.   0x00007fff,
  57.   0x0000ffff,
  58.   0x0001ffff,
  59.   0x0003ffff,
  60.   0x0007ffff,
  61.   0x000fffff,
  62.   0x001fffff,
  63.   0x003fffff,
  64.   0x007fffff,
  65.   0x00ffffff,
  66.   0x01ffffff,
  67.   0x03ffffff,
  68.   0x07ffffff,
  69.   0x0fffffff,
  70.   0x1fffffff,
  71.   0x3fffffff,
  72.   0x7fffffff,
  73.   0xffffffff
  74.   };
  75.  
  76. static int bits_left_in_littlenum;
  77. static int littlenums_left;
  78. static LITTLENUM_TYPE *    littlenum_pointer;
  79.  
  80. static int
  81. next_bits (number_of_bits)
  82.      int        number_of_bits;
  83. {
  84.   int            return_value;
  85.  
  86.   if(!littlenums_left)
  87.       return 0;
  88.   if (number_of_bits >= bits_left_in_littlenum)
  89.     {
  90.       return_value  = mask [bits_left_in_littlenum] & *littlenum_pointer;
  91.       number_of_bits -= bits_left_in_littlenum;
  92.       return_value <<= number_of_bits;
  93.       if(--littlenums_left) {
  94.           bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
  95.           littlenum_pointer --;
  96.           return_value |= (*littlenum_pointer>>bits_left_in_littlenum) & mask[number_of_bits];
  97.       }
  98.     }
  99.   else
  100.     {
  101.       bits_left_in_littlenum -= number_of_bits;
  102.       return_value = mask [number_of_bits] & (*littlenum_pointer>>bits_left_in_littlenum);
  103.     }
  104.   return (return_value);
  105. }
  106.  
  107. /* Num had better be less than LITTLENUM_NUMBER_OF_BITS */
  108. static int
  109. unget_bits(num)
  110. {
  111.     if(!littlenums_left) {
  112.         ++littlenum_pointer;
  113.         ++littlenums_left;
  114.         bits_left_in_littlenum=num;
  115.     } else if(bits_left_in_littlenum+num>LITTLENUM_NUMBER_OF_BITS) {
  116.         bits_left_in_littlenum= num-(LITTLENUM_NUMBER_OF_BITS-bits_left_in_littlenum);
  117.         ++littlenum_pointer;
  118.         ++littlenums_left;
  119.     } else
  120.         bits_left_in_littlenum+=num;
  121. }
  122.  
  123. static void
  124. make_invalid_floating_point_number (words)
  125.      LITTLENUM_TYPE *    words;
  126. {
  127.     as_warn("cannot create floating-point number");
  128.     words[0]= ((unsigned)-1)>>1;    /* Zero the leftmost bit */
  129.     words[1]= -1;
  130.     words[2]= -1;
  131.     words[3]= -1;
  132.     words[4]= -1;
  133.     words[5]= -1;
  134. }
  135.  
  136. /***********************************************************************\
  137. *    Warning: this returns 16-bit LITTLENUMs. It is up to the caller    *
  138. *    to figure out any alignment problems and to conspire for the    *
  139. *    bytes/word to be emitted in the right order. Bigendians beware!    *
  140. *                                    *
  141. \***********************************************************************/
  142.  
  143. /* Note that atof-ieee always has X and P precisions enabled.  it is up
  144.    to md_atof to filter them out if the target machine does not support
  145.    them.  */
  146.  
  147. char *                /* Return pointer past text consumed. */
  148. atof_ieee (str, what_kind, words)
  149.      char *        str;    /* Text to convert to binary. */
  150.      char        what_kind; /* 'd', 'f', 'g', 'h' */
  151.      LITTLENUM_TYPE *    words;    /* Build the binary here. */
  152. {
  153.     static LITTLENUM_TYPE    bits [MAX_PRECISION + MAX_PRECISION + GUARD];
  154.                 /* Extra bits for zeroed low-order bits. */
  155.                 /* The 1st MAX_PRECISION are zeroed, */
  156.                 /* the last contain flonum bits. */
  157.     char *        return_value;
  158.     int        precision; /* Number of 16-bit words in the format. */
  159.     long int    exponent_bits;
  160.  
  161.     return_value = str;
  162.     generic_floating_point_number.low    = bits + MAX_PRECISION;
  163.     generic_floating_point_number.high    = NULL;
  164.     generic_floating_point_number.leader    = NULL;
  165.     generic_floating_point_number.exponent    = NULL;
  166.     generic_floating_point_number.sign    = '\0';
  167.  
  168.                 /* Use more LittleNums than seems */
  169.                 /* necessary: the highest flonum may have */
  170.                 /* 15 leading 0 bits, so could be useless. */
  171.  
  172.     bzero (bits, sizeof(LITTLENUM_TYPE) * MAX_PRECISION);
  173.  
  174.     switch(what_kind) {
  175.     case 'f':
  176.     case 'F':
  177.     case 's':
  178.     case 'S':
  179.         precision = F_PRECISION;
  180.         exponent_bits = 8;
  181.         break;
  182.  
  183.     case 'd':
  184.     case 'D':
  185.     case 'r':
  186.     case 'R':
  187.         precision = D_PRECISION;
  188.         exponent_bits = 11;
  189.         break;
  190.  
  191.     case 'x':
  192.     case 'X':
  193.     case 'e':
  194.     case 'E':
  195.         precision = X_PRECISION;
  196.         exponent_bits = 15;
  197.         break;
  198.  
  199.     case 'p':
  200.     case 'P':
  201.         
  202.         precision = P_PRECISION;
  203.         exponent_bits= -1;
  204.         break;
  205.  
  206.     default:
  207.         make_invalid_floating_point_number (words);
  208.         return NULL;
  209.     }
  210.  
  211.     generic_floating_point_number.high = generic_floating_point_number.low + precision - 1 + GUARD;
  212.  
  213.     if (atof_generic (& return_value, ".", EXP_CHARS, & generic_floating_point_number)) {
  214.         /* as_warn("Error converting floating point number (Exponent overflow?)"); */
  215. #ifdef NeXT
  216.         if(precision==F_PRECISION) {
  217.             words[0]=0x7f80;
  218.             words[1]=0;
  219.         } else {
  220.             words[0]=0x7ff0;
  221.             words[1]=0;
  222.             words[2]=0;
  223.             words[3]=0;
  224.         }
  225.         if(generic_floating_point_number.sign=='-')
  226.             words[0] |= 0x8000;
  227.         return return_value;
  228. #else NeXT
  229.         make_invalid_floating_point_number (words);
  230.         return NULL;
  231. #endif NeXT
  232.     }
  233.     gen_to_words(words, precision, exponent_bits);
  234.     return return_value;
  235. }
  236.  
  237. /* Turn generic_floating_point_number into a real float/double/extended */
  238. gen_to_words(words,precision,exponent_bits)
  239. LITTLENUM_TYPE *words;
  240. long int    exponent_bits;
  241. int precision;
  242. {
  243.     int return_value=0;
  244.  
  245.     long int    exponent_1;
  246.     long int    exponent_2;
  247.     long int    exponent_3;
  248.     long int    exponent_4;
  249.     int        exponent_skippage;
  250.     LITTLENUM_TYPE    word1;
  251.     LITTLENUM_TYPE *    lp;
  252.  
  253.     if (generic_floating_point_number.low > generic_floating_point_number.leader) {
  254.         /* 0.0e0 seen. */
  255.         if(generic_floating_point_number.sign=='+')
  256.             words[0]=0x0000;
  257.         else
  258.             words[0]=0x8000;
  259.         bzero (&words[1], sizeof(LITTLENUM_TYPE) * (precision-1));
  260.         return return_value;
  261.     }
  262.  
  263.     /* NaN:  Do the right thing */
  264.     if(generic_floating_point_number.sign==0) {
  265.         if(precision==F_PRECISION) {
  266.             words[0]=0x7fff;
  267.             words[1]=0xffff;
  268.         } else {
  269.             words[0]=0x7fff;
  270.             words[1]=0xffff;
  271.             words[2]=0xffff;
  272.             words[3]=0xffff;
  273.         }
  274.         return return_value;
  275.     } else if(generic_floating_point_number.sign=='P') {
  276.         /* +INF:  Do the right thing */
  277.         if(precision==F_PRECISION) {
  278.             words[0]=0x7f80;
  279.             words[1]=0;
  280.         } else {
  281.             words[0]=0x7ff0;
  282.             words[1]=0;
  283.             words[2]=0;
  284.             words[3]=0;
  285.         }
  286.         return return_value;
  287.     } else if(generic_floating_point_number.sign=='N') {
  288.         /* Negative INF */
  289.         if(precision==F_PRECISION) {
  290.             words[0]=0xff80;
  291.             words[1]=0x0;
  292.         } else {
  293.             words[0]=0xfff0;
  294.             words[1]=0x0;
  295.             words[2]=0x0;
  296.             words[3]=0x0;
  297.         }
  298.         return return_value;
  299.     }
  300.         /*
  301.          * The floating point formats we support have:
  302.          * Bit 15 is sign bit.
  303.          * Bits 14:n are excess-whatever exponent.
  304.          * Bits n-1:0 (if any) are most significant bits of fraction.
  305.          * Bits 15:0 of the next word(s) are the next most significant bits.
  306.          *
  307.          * So we need: number of bits of exponent, number of bits of
  308.          * mantissa.
  309.          */
  310.     bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  311.     littlenum_pointer = generic_floating_point_number.leader;
  312.     littlenums_left = 1+generic_floating_point_number.leader - generic_floating_point_number.low;
  313.     /* Seek (and forget) 1st significant bit */
  314.     for (exponent_skippage = 0;! next_bits(1); exponent_skippage ++)
  315.         ;
  316.     exponent_1 = generic_floating_point_number.exponent + generic_floating_point_number.leader + 1 -
  317.  generic_floating_point_number.low;
  318.     /* Radix LITTLENUM_RADIX, point just higher than generic_floating_point_number.leader. */
  319.     exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  320.     /* Radix 2. */
  321.     exponent_3 = exponent_2 - exponent_skippage;
  322.     /* Forget leading zeros, forget 1st bit. */
  323.     exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
  324.     /* Offset exponent. */
  325.  
  326.     lp = words;
  327.  
  328.     /* Word 1. Sign, exponent and perhaps high bits. */
  329.     word1 =   (generic_floating_point_number.sign == '+') ? 0 : (1<<(LITTLENUM_NUMBER_OF_BITS-1));
  330.  
  331.     /* Assume 2's complement integers. */
  332.     if(exponent_4<1 && exponent_4>=-62) {
  333.         int prec_bits;
  334.         int num_bits;
  335.  
  336.         unget_bits(1);
  337.         num_bits= -exponent_4;
  338.         prec_bits=LITTLENUM_NUMBER_OF_BITS*precision-(exponent_bits+1+num_bits);
  339.         if(precision==X_PRECISION && exponent_bits==15)
  340.             prec_bits-=LITTLENUM_NUMBER_OF_BITS+1;
  341.  
  342.         if(num_bits>=LITTLENUM_NUMBER_OF_BITS-exponent_bits) {
  343.             /* Bigger than one littlenum */
  344.             num_bits-=(LITTLENUM_NUMBER_OF_BITS-1)-exponent_bits;
  345.             *lp++=word1;
  346.             if(num_bits+exponent_bits+1>=precision*LITTLENUM_NUMBER_OF_BITS) {
  347.                 /* Exponent overflow */
  348. #ifdef NeXT
  349.                 if(precision==F_PRECISION) {
  350.                     words[0]=0x7f80;
  351.                     words[1]=0;
  352.                 } else {
  353.                     words[0]=0x7ff0;
  354.                     words[1]=0;
  355.                     words[2]=0;
  356.                     words[3]=0;
  357.                 }
  358.                 if(generic_floating_point_number.sign=='-')
  359.                     words[0] |= 0x8000;
  360.                 return return_value;
  361. #else NeXT
  362.                 make_invalid_floating_point_number(words);
  363.                 return return_value;
  364. #endif NeXT
  365.             }
  366.             if(precision==X_PRECISION && exponent_bits==15) {
  367.                 *lp++=0;
  368.                 *lp++=0;
  369.                 num_bits-=LITTLENUM_NUMBER_OF_BITS-1;
  370.             }
  371.             while(num_bits>=LITTLENUM_NUMBER_OF_BITS) {
  372.                 num_bits-=LITTLENUM_NUMBER_OF_BITS;
  373.                 *lp++=0;
  374.             }
  375.             if(num_bits)
  376.                 *lp++=next_bits(LITTLENUM_NUMBER_OF_BITS-(num_bits));
  377.         } else {
  378.             if(precision==X_PRECISION && exponent_bits==15) {
  379.                 *lp++=word1;
  380.                 *lp++=0;
  381.                 if(num_bits==LITTLENUM_NUMBER_OF_BITS) {
  382.                     *lp++=0;
  383.                     *lp++=next_bits(LITTLENUM_NUMBER_OF_BITS-1);
  384.                 } else if(num_bits==LITTLENUM_NUMBER_OF_BITS-1)
  385.                     *lp++=0;
  386.                 else
  387.                     *lp++=next_bits(LITTLENUM_NUMBER_OF_BITS-1-num_bits);
  388.                 num_bits=0;
  389.             } else {
  390.                 word1|= next_bits ((LITTLENUM_NUMBER_OF_BITS-1) - (exponent_bits+num_bits));
  391.                 *lp++=word1;
  392.             }
  393.         }
  394.         while(lp<words+precision)
  395.             *lp++=next_bits(LITTLENUM_NUMBER_OF_BITS);
  396.  
  397. #ifdef NeXT
  398.         /*
  399.          * Round the mantissa up, and let the rounding change the
  400.          * number if that happens.  Noting that the largest denorm
  401.          * rounded up will produce the correct smallest normalilized
  402.          * number.  This is not correct IEEE round to nearest as if
  403.          * the number is exactly half way between two numbers (the
  404.          * round bit is set and all lower bits are zero) the last bit
  405.          * is not set to zero.  This would require that the input
  406.          * flonum be created from the decimal string with a correct
  407.          * sticky bit for the remaining digits so that could be used
  408.          * here.  The reason the rounding is needed is so that the
  409.          * decimal version of the smallest denorm will not become 0.
  410.          */
  411. #else /* !defined(NeXT) */
  412.         /* Round the mantissa up, but don't change the number */
  413. #endif /* NeXT */
  414.         if(next_bits(1)) {
  415.             --lp;
  416.             if(prec_bits>LITTLENUM_NUMBER_OF_BITS) {
  417.                 int n = 0;
  418.                 int tmp_bits;
  419.  
  420.                 n=0;
  421.                 tmp_bits=prec_bits;
  422.                 while(tmp_bits>LITTLENUM_NUMBER_OF_BITS) {
  423.                     if(lp[n]!=(LITTLENUM_TYPE)-1)
  424.                         break;
  425.                     --n;
  426.                     tmp_bits-=LITTLENUM_NUMBER_OF_BITS;
  427.                 }
  428. #ifndef NeXT
  429.                 if(tmp_bits>LITTLENUM_NUMBER_OF_BITS ||
  430.                    (lp[n]&mask[tmp_bits])!=mask[tmp_bits])
  431. #endif NeXT
  432.                 {
  433.                     unsigned long int carry;
  434.  
  435.                     for (carry = 1; carry && (lp >= words); lp --) {
  436.                         carry = * lp + carry;
  437.                         * lp = carry;
  438.                         carry >>= LITTLENUM_NUMBER_OF_BITS;
  439.                     }
  440.                 }
  441.             }
  442.             else 
  443. #ifdef NeXT
  444.                 *lp = *lp + 1;
  445. #else /* !defined(NeXT) */
  446.             else if((*lp&mask[prec_bits])!=mask[prec_bits])
  447.                 *lp++;
  448. #endif /* NeXT */
  449.         }
  450.  
  451.         return return_value;
  452.     } else     if (exponent_4 & ~ mask [exponent_bits]) {
  453.             /*
  454.              * Exponent overflow. Lose immediately.
  455.              */
  456.  
  457.             /*
  458.              * We leave return_value alone: admit we read the
  459.              * number, but return a floating exception
  460.              * because we can't encode the number.
  461.              */
  462. #ifdef NeXT
  463.         if(precision==F_PRECISION) {
  464.             words[0]=0x7f80;
  465.             words[1]=0;
  466.         } else {
  467.             words[0]=0x7ff0;
  468.             words[1]=0;
  469.             words[2]=0;
  470.             words[3]=0;
  471.         }
  472.         if(generic_floating_point_number.sign=='-')
  473.             words[0] |= 0x8000;
  474. #else NeXT
  475.         make_invalid_floating_point_number (words);
  476. #endif NeXT
  477.         return return_value;
  478.     } else {
  479.         word1 |=  (exponent_4 << ((LITTLENUM_NUMBER_OF_BITS-1) - exponent_bits))
  480.             | next_bits ((LITTLENUM_NUMBER_OF_BITS-1) - exponent_bits);
  481.     }
  482.  
  483.     * lp ++ = word1;
  484.  
  485.     /* X_PRECISION is special: it has 16 bits of zero in the middle,
  486.        followed by a 1 bit. */
  487.     if(exponent_bits==15 && precision==X_PRECISION) {
  488.         *lp++=0;
  489.         *lp++= 1<<(LITTLENUM_NUMBER_OF_BITS)|next_bits(LITTLENUM_NUMBER_OF_BITS-1);
  490.     }
  491.  
  492.     /* The rest of the words are just mantissa bits. */
  493.     while(lp < words + precision)
  494.         *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
  495.  
  496.     if (next_bits (1)) {
  497.         unsigned long int    carry;
  498.             /*
  499.              * Since the NEXT bit is a 1, round UP the mantissa.
  500.              * The cunning design of these hidden-1 floats permits
  501.              * us to let the mantissa overflow into the exponent, and
  502.              * it 'does the right thing'. However, we lose if the
  503.              * highest-order bit of the lowest-order word flips.
  504.              * Is that clear?
  505.              */
  506.  
  507.  
  508. /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  509.     Please allow at least 1 more bit in carry than is in a LITTLENUM.
  510.     We need that extra bit to hold a carry during a LITTLENUM carry
  511.     propagation. Another extra bit (kept 0) will assure us that we
  512.     don't get a sticky sign bit after shifting right, and that
  513.     permits us to propagate the carry without any masking of bits.
  514. #endif */
  515.         for (carry = 1, lp --; carry && (lp >= words); lp --) {
  516.             carry = * lp + carry;
  517.             * lp = carry;
  518.             carry >>= LITTLENUM_NUMBER_OF_BITS;
  519.         }
  520.         if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) ) {
  521.             /* We leave return_value alone: admit we read the
  522.              * number, but return a floating exception
  523.              * because we can't encode the number.
  524.              */
  525.             *words&= ~ (1 << (LITTLENUM_NUMBER_OF_BITS - 1));
  526.             /* make_invalid_floating_point_number (words); */
  527.             /* return return_value; */
  528.         }
  529.     }
  530.     return (return_value);
  531. }
  532.  
  533. /* This routine is a real kludge.  Someone really should do it better, but
  534.    I'm too lazy, and I don't understand this stuff all too well anyway
  535.    (JF)
  536.  */
  537. void
  538. int_to_gen(x)
  539. long x;
  540. {
  541.     char buf[20];
  542.     char *bufp;
  543.  
  544.     sprintf(buf,"%ld",x);
  545.     bufp= &buf[0];
  546.     if(atof_generic(&bufp,".", EXP_CHARS, &generic_floating_point_number))
  547.         as_warn("Error converting number to floating point (Exponent overflow?)");
  548. }
  549.  
  550. #ifdef TEST
  551. char *
  552. print_gen(gen)
  553. FLONUM_TYPE *gen;
  554. {
  555.     FLONUM_TYPE f;
  556.     LITTLENUM_TYPE arr[10];
  557.     double dv;
  558.     float fv;
  559.     static char sbuf[40];
  560.  
  561.     if(gen) {
  562.         f=generic_floating_point_number;
  563.         generic_floating_point_number= *gen;
  564.     }
  565.     gen_to_words(&arr[0],4,11);
  566.     bcopy(&arr[0],&dv,sizeof(double));
  567.     sprintf(sbuf,"%x %x %x %x %.14G   ",arr[0],arr[1],arr[2],arr[3],dv);
  568.     gen_to_words(&arr[0],2,8);
  569.     bcopy(&arr[0],&fv,sizeof(float));
  570.     sprintf(sbuf+strlen(sbuf),"%x %x %.12g\n",arr[0],arr[1],fv);
  571.     if(gen)
  572.         generic_floating_point_number=f;
  573.     return sbuf;
  574. }
  575. #endif
  576.