home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gassrc04.zoo / atof-vax.c < prev    next >
C/C++ Source or Header  |  1991-01-24  |  14KB  |  514 lines

  1. /* atof_vax.c - turn a Flonum into a VAX 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.     /* JF added these two for md_atof() */
  21. #include "as.h"
  22. #include "read.h"
  23.  
  24. #include "flonum.h"
  25.  
  26.  
  27.                 /* Precision in LittleNums. */
  28. #define MAX_PRECISION (8)
  29. #define H_PRECISION (8)
  30. #define G_PRECISION (4)
  31. #define D_PRECISION (4)
  32. #define F_PRECISION (2)
  33.  
  34.                 /* Length in LittleNums of guard bits. */
  35. #define GUARD (2)
  36.  
  37. int                /* Number of chars in flonum type 'letter'. */
  38. atof_vax_sizeof (letter)
  39.      char letter;
  40. {
  41.   int    return_value;
  42.  
  43.   /*
  44.    * Permitting uppercase letters is probably a bad idea.
  45.    * Please use only lower-cased letters in case the upper-cased
  46.    * ones become unsupported!
  47.    */
  48.   switch (letter)
  49.     {
  50.     case 'f':
  51.     case 'F':
  52.       return_value = 4;
  53.       break;
  54.  
  55.     case 'd':
  56.     case 'D':
  57.     case 'g':
  58.     case 'G':
  59.       return_value = 8;
  60.       break;
  61.  
  62.     case 'h':
  63.     case 'H':
  64.       return_value = 16;
  65.       break;
  66.  
  67.     default:
  68.       return_value = 0;
  69.       break;
  70.     }
  71.   return (return_value);
  72. }                /* atof_vax_sizeof */
  73.  
  74. static const long int mask [] = {
  75.   0x00000000,
  76.   0x00000001,
  77.   0x00000003,
  78.   0x00000007,
  79.   0x0000000f,
  80.   0x0000001f,
  81.   0x0000003f,
  82.   0x0000007f,
  83.   0x000000ff,
  84.   0x000001ff,
  85.   0x000003ff,
  86.   0x000007ff,
  87.   0x00000fff,
  88.   0x00001fff,
  89.   0x00003fff,
  90.   0x00007fff,
  91.   0x0000ffff,
  92.   0x0001ffff,
  93.   0x0003ffff,
  94.   0x0007ffff,
  95.   0x000fffff,
  96.   0x001fffff,
  97.   0x003fffff,
  98.   0x007fffff,
  99.   0x00ffffff,
  100.   0x01ffffff,
  101.   0x03ffffff,
  102.   0x07ffffff,
  103.   0x0fffffff,
  104.   0x1fffffff,
  105.   0x3fffffff,
  106.   0x7fffffff,
  107.   0xffffffff
  108.   };
  109.  
  110.  
  111. /* Shared between flonum_gen2vax and next_bits */
  112. static int        bits_left_in_littlenum;
  113. static LITTLENUM_TYPE *    littlenum_pointer;
  114. static LITTLENUM_TYPE * littlenum_end;
  115.  
  116. static int
  117. next_bits (number_of_bits)
  118.      int        number_of_bits;
  119. {
  120.   int            return_value;
  121.  
  122.   if(littlenum_pointer<littlenum_end)
  123.       return 0;
  124.   if (number_of_bits >= bits_left_in_littlenum)
  125.     {
  126.       return_value  = mask [bits_left_in_littlenum] & * littlenum_pointer;
  127.       number_of_bits -= bits_left_in_littlenum;
  128.       return_value <<= number_of_bits;
  129.       bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
  130.       littlenum_pointer --;
  131.       if(littlenum_pointer>=littlenum_end)
  132.           return_value |= ( (* littlenum_pointer) >> (bits_left_in_littlenum) ) & mask [number_of_bits];
  133.     }
  134.   else
  135.     {
  136.       bits_left_in_littlenum -= number_of_bits;
  137.       return_value = mask [number_of_bits] & ( (* littlenum_pointer) >> bits_left_in_littlenum);
  138.     }
  139.   return (return_value);
  140. }
  141.  
  142. static void
  143. make_invalid_floating_point_number (words)
  144.      LITTLENUM_TYPE *    words;
  145. {
  146.   * words = 0x8000;        /* Floating Reserved Operand Code */
  147. }
  148.  
  149. static int            /* 0 means letter is OK. */
  150. what_kind_of_float (letter, precisionP, exponent_bitsP)
  151.      char        letter;    /* In: lowercase please. What kind of float? */
  152.      int *        precisionP; /* Number of 16-bit words in the float. */
  153.      long int *        exponent_bitsP;    /* Number of exponent bits. */
  154. {
  155.   int    retval;            /* 0: OK. */
  156.  
  157.   retval = 0;
  158.   switch (letter)
  159.     {
  160.     case 'f':
  161.       * precisionP = F_PRECISION;
  162.       * exponent_bitsP = 8;
  163.       break;
  164.  
  165.     case 'd':
  166.       * precisionP = D_PRECISION;
  167.       * exponent_bitsP = 8;
  168.       break;
  169.  
  170.     case 'g':
  171.       * precisionP = G_PRECISION;
  172.       * exponent_bitsP = 11;
  173.       break;
  174.  
  175.     case 'h':
  176.       * precisionP = H_PRECISION;
  177.       * exponent_bitsP = 15;
  178.       break;
  179.  
  180.     default:
  181.       retval = 69;
  182.       break;
  183.     }
  184.   return (retval);
  185. }
  186.  
  187. /***********************************************************************\
  188. *                                    *
  189. *    Warning: this returns 16-bit LITTLENUMs, because that is    *
  190. *    what the VAX thinks in. It is up to the caller to figure    *
  191. *    out any alignment problems and to conspire for the bytes/word    *
  192. *    to be emitted in the right order. Bigendians beware!        *
  193. *                                    *
  194. \***********************************************************************/
  195.  
  196. char *                /* Return pointer past text consumed. */
  197. atof_vax (str, what_kind, words)
  198.      char *        str;    /* Text to convert to binary. */
  199.      char        what_kind; /* 'd', 'f', 'g', 'h' */
  200.      LITTLENUM_TYPE *    words;    /* Build the binary here. */
  201. {
  202.   FLONUM_TYPE        f;
  203.   LITTLENUM_TYPE    bits [MAX_PRECISION + MAX_PRECISION + GUARD];
  204.                 /* Extra bits for zeroed low-order bits. */
  205.                 /* The 1st MAX_PRECISION are zeroed, */
  206.                 /* the last contain flonum bits. */
  207.   char *        return_value;
  208.   int            precision; /* Number of 16-bit words in the format. */
  209.   long int        exponent_bits;
  210.  
  211.   return_value = str;
  212.   f . low    = bits + MAX_PRECISION;
  213.   f . high    = NULL;
  214.   f . leader    = NULL;
  215.   f . exponent    = NULL;
  216.   f . sign    = '\0';
  217.  
  218.   if (what_kind_of_float (what_kind, & precision, & exponent_bits))
  219.     {
  220.       return_value = NULL;    /* We lost. */
  221.       make_invalid_floating_point_number (words);
  222.     }
  223.   if (return_value)
  224.     {
  225.       bzero (bits, sizeof(LITTLENUM_TYPE) * MAX_PRECISION);
  226.  
  227.                 /* Use more LittleNums than seems */
  228.                 /* necessary: the highest flonum may have */
  229.                 /* 15 leading 0 bits, so could be useless. */
  230.       f . high = f . low + precision - 1 + GUARD;
  231.  
  232.       if (atof_generic (& return_value, ".", "eE", & f))
  233.     {
  234.       make_invalid_floating_point_number (words);
  235.       return_value = NULL;    /* we lost */
  236.     }
  237.       else
  238.     {
  239.       if (flonum_gen2vax (what_kind, & f, words))
  240.         {
  241.           return_value = NULL;
  242.         }
  243.     }
  244.     }
  245.   return (return_value);
  246. }
  247.  
  248. /*
  249.  * In: a flonum, a vax floating point format.
  250.  * Out: a vax floating-point bit pattern.
  251.  */
  252.  
  253. int                /* 0: OK. */
  254. flonum_gen2vax (format_letter, f, words)
  255.      char        format_letter; /* One of 'd' 'f' 'g' 'h'. */
  256.      FLONUM_TYPE *    f;
  257.      LITTLENUM_TYPE *    words;    /* Deliver answer here. */
  258. {
  259.   LITTLENUM_TYPE *    lp;
  260.   int            precision;
  261.   long int        exponent_bits;
  262.   int            return_value; /* 0 == OK. */
  263.  
  264.   return_value = what_kind_of_float (format_letter, & precision, & exponent_bits);
  265.   if (return_value != 0)
  266.     {
  267.       make_invalid_floating_point_number (words);
  268.     }
  269.   else
  270.     {
  271.       if (f -> low > f -> leader)
  272.     {
  273.       /* 0.0e0 seen. */
  274.       bzero (words, sizeof(LITTLENUM_TYPE) * precision);
  275.     }
  276.       else
  277.     {
  278.       long int        exponent_1;
  279.       long int        exponent_2;
  280.       long int        exponent_3;
  281.       long int        exponent_4;
  282.       int        exponent_skippage;
  283.       LITTLENUM_TYPE    word1;
  284.  
  285.         /* JF: Deal with new Nan, +Inf and -Inf codes */
  286.       if(f->sign!='-' && f->sign!='+') {
  287.         make_invalid_floating_point_number(words);
  288.         return return_value;
  289.       }
  290.       /*
  291.        * All vaxen floating_point formats (so far) have:
  292.        * Bit 15 is sign bit.
  293.        * Bits 14:n are excess-whatever exponent.
  294.        * Bits n-1:0 (if any) are most significant bits of fraction.
  295.        * Bits 15:0 of the next word are the next most significant bits.
  296.        * And so on for each other word.
  297.        *
  298.        * All this to be compatible with a KF11?? (Which is still faster
  299.        * than lots of vaxen I can think of, but it also has higher
  300.        * maintenance costs ... sigh).
  301.        *
  302.        * So we need: number of bits of exponent, number of bits of
  303.        * mantissa.
  304.        */
  305.       
  306. #ifdef NEVER  /******* This zeroing seems redundant - Dean 3may86 **********/
  307.       /*
  308.        * No matter how few bits we got back from the atof()
  309.        * routine, add enough zero littlenums so the rest of the
  310.        * code won't run out of "significant" bits in the mantissa.
  311.        */
  312.       {
  313.         LITTLENUM_TYPE * ltp;
  314.         for (ltp = f -> leader + 1;
  315.          ltp <= f -> low + precision;
  316.          ltp ++)
  317.           {
  318.         * ltp = 0;
  319.           }
  320.       }
  321. #endif
  322.       
  323.       bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  324.       littlenum_pointer = f -> leader;
  325.       littlenum_end = f->low;
  326.       /* Seek (and forget) 1st significant bit */
  327.       for (exponent_skippage = 0;
  328.            ! next_bits(1);
  329.            exponent_skippage ++)
  330.         {
  331.         }
  332.       exponent_1 = f -> exponent + f -> leader + 1 - f -> low;
  333.       /* Radix LITTLENUM_RADIX, point just higher than f -> leader. */
  334.       exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  335.       /* Radix 2. */
  336.       exponent_3 = exponent_2 - exponent_skippage;
  337.       /* Forget leading zeros, forget 1st bit. */
  338.       exponent_4 = exponent_3 + (1 << (exponent_bits - 1));
  339.       /* Offset exponent. */
  340.       
  341.       if (exponent_4 & ~ mask [exponent_bits])
  342.         {
  343.           /*
  344.            * Exponent overflow. Lose immediately.
  345.            */
  346.           
  347.           make_invalid_floating_point_number (words);
  348.           
  349.           /*
  350.            * We leave return_value alone: admit we read the
  351.            * number, but return a floating exception
  352.            * because we can't encode the number.
  353.            */
  354.         }
  355.       else
  356.         {
  357.           lp = words;
  358.           
  359.           /* Word 1. Sign, exponent and perhaps high bits. */
  360.           /* Assume 2's complement integers. */
  361.           word1 = ((exponent_4 & mask [exponent_bits]) << (15 - exponent_bits))
  362.         |       ((f -> sign == '+') ? 0 : 0x8000)
  363.           |    next_bits (15 - exponent_bits);
  364.           * lp ++ = word1;
  365.           
  366.           /* The rest of the words are just mantissa bits. */
  367.           for (; lp < words + precision; lp++)
  368.         {
  369.           * lp = next_bits (LITTLENUM_NUMBER_OF_BITS);
  370.         }
  371.           
  372.           if (next_bits (1))
  373.         {
  374.           /*
  375.            * Since the NEXT bit is a 1, round UP the mantissa.
  376.            * The cunning design of these hidden-1 floats permits
  377.            * us to let the mantissa overflow into the exponent, and
  378.            * it 'does the right thing'. However, we lose if the
  379.            * highest-order bit of the lowest-order word flips.
  380.            * Is that clear?
  381.            */
  382.           
  383.           unsigned long int    carry;
  384.           
  385.           /*
  386.             #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  387.             Please allow at least 1 more bit in carry than is in a LITTLENUM.
  388.             We need that extra bit to hold a carry during a LITTLENUM carry
  389.             propagation. Another extra bit (kept 0) will assure us that we
  390.             don't get a sticky sign bit after shifting right, and that
  391.             permits us to propagate the carry without any masking of bits.
  392.             #endif
  393.             */
  394.           for (carry = 1, lp --;
  395.                carry && (lp >= words);
  396.                lp --)
  397.             {
  398.               carry = * lp + carry;
  399.               * lp = carry;
  400.               carry >>= LITTLENUM_NUMBER_OF_BITS;
  401.             }
  402.           
  403.           if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) )
  404.             {
  405.               make_invalid_floating_point_number (words);
  406.               /*
  407.                * We leave return_value alone: admit we read the
  408.                * number, but return a floating exception
  409.                * because we can't encode the number.
  410.                */
  411.             }
  412.         }        /* if (we needed to round up) */
  413.         }            /* if (exponent overflow) */
  414.     }            /* if (0.0e0) */
  415.     }                /* if (float_type was OK) */
  416.   return (return_value);
  417. }
  418.  
  419.  
  420. /* JF this used to be in vax.c but this looks like a better place for it */
  421.  
  422. /*
  423.  *        md_atof()
  424.  *
  425.  * In:    input_line_pointer -> the 1st character of a floating-point
  426.  *        number.
  427.  *    1 letter denoting the type of statement that wants a
  428.  *        binary floating point number returned.
  429.  *    Address of where to build floating point literal.
  430.  *        Assumed to be 'big enough'.
  431.  *    Address of where to return size of literal (in chars).
  432.  *
  433.  * Out:    Input_line_pointer -> of next char after floating number.
  434.  *    Error message, or "".
  435.  *    Floating point literal.
  436.  *    Number of chars we used for the literal.
  437.  */
  438.  
  439. int atof_vax_sizeof();
  440.  
  441. #define MAXIMUM_NUMBER_OF_LITTLENUMS (8) /* For .hfloats. */
  442.  
  443. char *
  444. md_atof (what_statement_type, literalP, sizeP)
  445.      char    what_statement_type;
  446.      char *    literalP;
  447.      int *    sizeP;
  448. {
  449.   LITTLENUM_TYPE    words [MAXIMUM_NUMBER_OF_LITTLENUMS];
  450.   register char        kind_of_float;
  451.   register int        number_of_chars;
  452.   register LITTLENUM_TYPE * littlenum_pointer;
  453.  
  454.   switch (what_statement_type)
  455.     {
  456.     case 'F':            /* .float */
  457.     case 'f':            /* .ffloat */
  458.       kind_of_float = 'f';
  459.       break;
  460.  
  461.     case 'D':            /* .double */
  462.     case 'd':            /* .dfloat */
  463.       kind_of_float = 'd';
  464.       break;
  465.  
  466.     case 'g':            /* .gfloat */
  467.       kind_of_float = 'g';
  468.       break;
  469.  
  470.     case 'h':            /* .hfloat */
  471.       kind_of_float = 'h';
  472.       break;
  473.  
  474.     default:
  475.       kind_of_float = 0;
  476.       break;
  477.     };
  478.  
  479.   if (kind_of_float)
  480.     {
  481.       register LITTLENUM_TYPE * limit;
  482.       char * atof_vax();
  483.  
  484.       input_line_pointer = atof_vax (input_line_pointer,
  485.                      kind_of_float,
  486.                      words);
  487.       /*
  488.        * The atof_vax() builds up 16-bit numbers.
  489.        * Since the assembler may not be running on
  490.        * a little-endian machine, be very careful about
  491.        * converting words to chars.
  492.        */
  493.       number_of_chars = atof_vax_sizeof (kind_of_float);
  494.       know( number_of_chars <= MAXIMUM_NUMBER_OF_LITTLENUMS * sizeof(LITTLENUM_TYPE) );
  495.       limit = words + (number_of_chars / sizeof(LITTLENUM_TYPE));
  496.       for (littlenum_pointer = words;
  497.        littlenum_pointer < limit;
  498.        littlenum_pointer ++)
  499.     {
  500.       md_number_to_chars (literalP, * littlenum_pointer, sizeof(LITTLENUM_TYPE));
  501.       literalP += sizeof(LITTLENUM_TYPE);
  502.     };
  503.     }
  504.   else
  505.     {
  506.       number_of_chars = 0;
  507.     };
  508.  
  509.   * sizeP = number_of_chars;
  510.   return (kind_of_float ? "" : "Bad call to md_atof()");
  511. }                /* md_atof() */
  512.  
  513. /* atof_vax.c */
  514.