home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / gas / config / atof-vax.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  13KB  |  511 lines

  1. /* atof_vax.c - turn a Flonum into a VAX floating point number
  2.    Copyright (C) 1987, 1992 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 2, 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  19.  
  20. #include "as.h"
  21.  
  22. /* Precision in LittleNums. */
  23. #define MAX_PRECISION (8)
  24. #define H_PRECISION (8)
  25. #define G_PRECISION (4)
  26. #define D_PRECISION (4)
  27. #define F_PRECISION (2)
  28.  
  29. /* Length in LittleNums of guard bits. */
  30. #define GUARD (2)
  31.  
  32. int flonum_gen2vax PARAMS ((int format_letter, FLONUM_TYPE * f,
  33.                 LITTLENUM_TYPE * words));
  34.  
  35. /* Number of chars in flonum type 'letter'. */
  36. int
  37. atof_vax_sizeof (letter)
  38.      char letter;
  39. {
  40.   int return_value;
  41.  
  42.   /*
  43.    * Permitting uppercase letters is probably a bad idea.
  44.    * Please use only lower-cased letters in case the upper-cased
  45.    * ones become unsupported!
  46.    */
  47.   switch (letter)
  48.     {
  49.     case 'f':
  50.     case 'F':
  51.       return_value = 4;
  52.       break;
  53.  
  54.     case 'd':
  55.     case 'D':
  56.     case 'g':
  57.     case 'G':
  58.       return_value = 8;
  59.       break;
  60.  
  61.     case 'h':
  62.     case 'H':
  63.       return_value = 16;
  64.       break;
  65.  
  66.     default:
  67.       return_value = 0;
  68.       break;
  69.     }
  70.   return (return_value);
  71. }                /* atof_vax_sizeof */
  72.  
  73. static const long mask[] =
  74. {
  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 *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 exponent_bits;
  210.  
  211.   return_value = str;
  212.   f.low = bits + MAX_PRECISION;
  213.   f.high = NULL;
  214.   f.leader = NULL;
  215.   f.exponent = 0;
  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.  
  224.   if (return_value)
  225.     {
  226.       memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION);
  227.  
  228.       /* Use more LittleNums than seems */
  229.       /* necessary: the highest flonum may have */
  230.       /* 15 leading 0 bits, so could be useless. */
  231.       f.high = f.low + precision - 1 + GUARD;
  232.  
  233.       if (atof_generic (&return_value, ".", "eE", &f))
  234.     {
  235.       make_invalid_floating_point_number (words);
  236.       return_value = NULL;    /* we lost */
  237.     }
  238.       else
  239.     {
  240.       if (flonum_gen2vax (what_kind, &f, words))
  241.         {
  242.           return_value = NULL;
  243.         }
  244.     }
  245.     }
  246.   return (return_value);
  247. }                /* atof_vax() */
  248.  
  249. /*
  250.  * In: a flonum, a vax floating point format.
  251.  * Out: a vax floating-point bit pattern.
  252.  */
  253.  
  254. int                /* 0: OK. */
  255. flonum_gen2vax (format_letter, f, words)
  256.      char format_letter;    /* One of 'd' 'f' 'g' 'h'. */
  257.      FLONUM_TYPE *f;
  258.      LITTLENUM_TYPE *words;    /* Deliver answer here. */
  259. {
  260.   LITTLENUM_TYPE *lp;
  261.   int precision;
  262.   long exponent_bits;
  263.   int return_value;        /* 0 == OK. */
  264.  
  265.   return_value = what_kind_of_float (format_letter, &precision, &exponent_bits);
  266.  
  267.   if (return_value != 0)
  268.     {
  269.       make_invalid_floating_point_number (words);
  270.     }
  271.   else
  272.     {
  273.       if (f->low > f->leader)
  274.     {
  275.       /* 0.0e0 seen. */
  276.       memset (words, '\0', sizeof (LITTLENUM_TYPE) * precision);
  277.     }
  278.       else
  279.     {
  280.       long exponent_1;
  281.       long exponent_2;
  282.       long exponent_3;
  283.       long exponent_4;
  284.       int exponent_skippage;
  285.       LITTLENUM_TYPE word1;
  286.  
  287.       /* JF: Deal with new Nan, +Inf and -Inf codes */
  288.       if (f->sign != '-' && f->sign != '+')
  289.         {
  290.           make_invalid_floating_point_number (words);
  291.           return return_value;
  292.         }
  293.       /*
  294.              * All vaxen floating_point formats (so far) have:
  295.              * Bit 15 is sign bit.
  296.              * Bits 14:n are excess-whatever exponent.
  297.              * Bits n-1:0 (if any) are most significant bits of fraction.
  298.              * Bits 15:0 of the next word are the next most significant bits.
  299.              * And so on for each other word.
  300.              *
  301.              * All this to be compatible with a KF11?? (Which is still faster
  302.              * than lots of vaxen I can think of, but it also has higher
  303.              * maintenance costs ... sigh).
  304.              *
  305.              * So we need: number of bits of exponent, number of bits of
  306.              * mantissa.
  307.              */
  308.  
  309. #ifdef NEVER            /******* This zeroing seems redundant - Dean 3may86 **********/
  310.       /*
  311.              * No matter how few bits we got back from the atof()
  312.              * routine, add enough zero littlenums so the rest of the
  313.              * code won't run out of "significant" bits in the mantissa.
  314.              */
  315.       {
  316.         LITTLENUM_TYPE *ltp;
  317.         for (ltp = f->leader + 1;
  318.          ltp <= f->low + precision;
  319.          ltp++)
  320.           {
  321.         *ltp = 0;
  322.           }
  323.       }
  324. #endif
  325.  
  326.       bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  327.       littlenum_pointer = f->leader;
  328.       littlenum_end = f->low;
  329.       /* Seek (and forget) 1st significant bit */
  330.       for (exponent_skippage = 0;
  331.            !next_bits (1);
  332.            exponent_skippage++);;
  333.  
  334.       exponent_1 = f->exponent + f->leader + 1 - f->low;
  335.       /* Radix LITTLENUM_RADIX, point just higher than f->leader. */
  336.       exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  337.       /* Radix 2. */
  338.       exponent_3 = exponent_2 - exponent_skippage;
  339.       /* Forget leading zeros, forget 1st bit. */
  340.       exponent_4 = exponent_3 + (1 << (exponent_bits - 1));
  341.       /* Offset exponent. */
  342.  
  343.       if (exponent_4 & ~mask[exponent_bits])
  344.         {
  345.           /*
  346.                  * Exponent overflow. Lose immediately.
  347.                  */
  348.  
  349.           make_invalid_floating_point_number (words);
  350.  
  351.           /*
  352.                  * We leave return_value alone: admit we read the
  353.                  * number, but return a floating exception
  354.                  * because we can't encode the number.
  355.                  */
  356.         }
  357.       else
  358.         {
  359.           lp = words;
  360.  
  361.           /* Word 1. Sign, exponent and perhaps high bits. */
  362.           /* Assume 2's complement integers. */
  363.           word1 = (((exponent_4 & mask[exponent_bits]) << (15 - exponent_bits))
  364.                | ((f->sign == '+') ? 0 : 0x8000)
  365.                | next_bits (15 - exponent_bits));
  366.           *lp++ = word1;
  367.  
  368.           /* The rest of the words are just mantissa bits. */
  369.           for (; lp < words + precision; lp++)
  370.         {
  371.           *lp = next_bits (LITTLENUM_NUMBER_OF_BITS);
  372.         }
  373.  
  374.           if (next_bits (1))
  375.         {
  376.           /*
  377.                      * Since the NEXT bit is a 1, round UP the mantissa.
  378.                      * The cunning design of these hidden-1 floats permits
  379.                      * us to let the mantissa overflow into the exponent, and
  380.                      * it 'does the right thing'. However, we lose if the
  381.                      * highest-order bit of the lowest-order word flips.
  382.                      * Is that clear?
  383.                      */
  384.  
  385.           unsigned long carry;
  386.  
  387.           /*
  388.                       #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  389.                       Please allow at least 1 more bit in carry than is in a LITTLENUM.
  390.                       We need that extra bit to hold a carry during a LITTLENUM carry
  391.                       propagation. Another extra bit (kept 0) will assure us that we
  392.                       don't get a sticky sign bit after shifting right, and that
  393.                       permits us to propagate the carry without any masking of bits.
  394.                       #endif
  395.                       */
  396.           for (carry = 1, lp--;
  397.                carry && (lp >= words);
  398.                lp--)
  399.             {
  400.               carry = *lp + carry;
  401.               *lp = carry;
  402.               carry >>= LITTLENUM_NUMBER_OF_BITS;
  403.             }
  404.  
  405.           if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)))
  406.             {
  407.               make_invalid_floating_point_number (words);
  408.               /*
  409.                          * We leave return_value alone: admit we read the
  410.                          * number, but return a floating exception
  411.                          * because we can't encode the number.
  412.                          */
  413.             }
  414.         }        /* if (we needed to round up) */
  415.         }            /* if (exponent overflow) */
  416.     }            /* if (0.0e0) */
  417.     }                /* if (float_type was OK) */
  418.   return (return_value);
  419. }                /* flonum_gen2vax() */
  420.  
  421.  
  422. /* JF this used to be in vax.c but this looks like a better place for it */
  423.  
  424. /*
  425.  *        md_atof()
  426.  *
  427.  * In:    input_line_pointer->the 1st character of a floating-point
  428.  *        number.
  429.  *    1 letter denoting the type of statement that wants a
  430.  *        binary floating point number returned.
  431.  *    Address of where to build floating point literal.
  432.  *        Assumed to be 'big enough'.
  433.  *    Address of where to return size of literal (in chars).
  434.  *
  435.  * Out:    Input_line_pointer->of next char after floating number.
  436.  *    Error message, or 0.
  437.  *    Floating point literal.
  438.  *    Number of chars we used for the literal.
  439.  */
  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 *littlenumP;
  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.  
  483.       input_line_pointer = atof_vax (input_line_pointer,
  484.                      kind_of_float,
  485.                      words);
  486.       /*
  487.        * The atof_vax() builds up 16-bit numbers.
  488.        * Since the assembler may not be running on
  489.        * a little-endian machine, be very careful about
  490.        * converting words to chars.
  491.        */
  492.       number_of_chars = atof_vax_sizeof (kind_of_float);
  493.       know (number_of_chars <= MAXIMUM_NUMBER_OF_LITTLENUMS * sizeof (LITTLENUM_TYPE));
  494.       limit = words + (number_of_chars / sizeof (LITTLENUM_TYPE));
  495.       for (littlenumP = words; littlenumP < limit; littlenumP++)
  496.     {
  497.       md_number_to_chars (literalP, *littlenumP, sizeof (LITTLENUM_TYPE));
  498.       literalP += sizeof (LITTLENUM_TYPE);
  499.     };
  500.     }
  501.   else
  502.     {
  503.       number_of_chars = 0;
  504.     };
  505.  
  506.   *sizeP = number_of_chars;
  507.   return kind_of_float ? 0 : "Bad call to md_atof()";
  508. }
  509.  
  510. /* end of atof-vax.c */
  511.