Subversion Repositories Kolibri OS

Rev

Rev 5222 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* atof_ieee.c - turn a Flonum into an IEEE floating point number
  2.    Copyright (C) 1987-2015 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 3, 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 the Free
  18.    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  19.    02110-1301, USA.  */
  20.  
  21. #include "as.h"
  22.  
  23. /* Flonums returned here.  */
  24. extern FLONUM_TYPE generic_floating_point_number;
  25.  
  26. extern const char EXP_CHARS[];
  27. /* Precision in LittleNums.  */
  28. /* Don't count the gap in the m68k extended precision format.  */
  29. #define MAX_PRECISION  5
  30. #define F_PRECISION    2
  31. #define D_PRECISION    4
  32. #define X_PRECISION    5
  33. #define P_PRECISION    5
  34.  
  35. /* Length in LittleNums of guard bits.  */
  36. #define GUARD          2
  37.  
  38. #ifndef TC_LARGEST_EXPONENT_IS_NORMAL
  39. #define TC_LARGEST_EXPONENT_IS_NORMAL(PRECISION) 0
  40. #endif
  41.  
  42. static const unsigned long mask[] =
  43. {
  44.   0x00000000,
  45.   0x00000001,
  46.   0x00000003,
  47.   0x00000007,
  48.   0x0000000f,
  49.   0x0000001f,
  50.   0x0000003f,
  51.   0x0000007f,
  52.   0x000000ff,
  53.   0x000001ff,
  54.   0x000003ff,
  55.   0x000007ff,
  56.   0x00000fff,
  57.   0x00001fff,
  58.   0x00003fff,
  59.   0x00007fff,
  60.   0x0000ffff,
  61.   0x0001ffff,
  62.   0x0003ffff,
  63.   0x0007ffff,
  64.   0x000fffff,
  65.   0x001fffff,
  66.   0x003fffff,
  67.   0x007fffff,
  68.   0x00ffffff,
  69.   0x01ffffff,
  70.   0x03ffffff,
  71.   0x07ffffff,
  72.   0x0fffffff,
  73.   0x1fffffff,
  74.   0x3fffffff,
  75.   0x7fffffff,
  76.   0xffffffff,
  77. };
  78. static int bits_left_in_littlenum;
  79. static int littlenums_left;
  80. static LITTLENUM_TYPE *littlenum_pointer;
  81.  
  82. static int
  83. next_bits (int number_of_bits)
  84. {
  85.   int return_value;
  86.  
  87.   if (!littlenums_left)
  88.     return 0;
  89.  
  90.   if (number_of_bits >= bits_left_in_littlenum)
  91.     {
  92.       return_value = mask[bits_left_in_littlenum] & *littlenum_pointer;
  93.       number_of_bits -= bits_left_in_littlenum;
  94.       return_value <<= number_of_bits;
  95.  
  96.       if (--littlenums_left)
  97.         {
  98.           bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
  99.           --littlenum_pointer;
  100.           return_value |=
  101.             (*littlenum_pointer >> bits_left_in_littlenum)
  102.             & mask[number_of_bits];
  103.         }
  104.     }
  105.   else
  106.     {
  107.       bits_left_in_littlenum -= number_of_bits;
  108.       return_value =
  109.         mask[number_of_bits] & (*littlenum_pointer >> bits_left_in_littlenum);
  110.     }
  111.   return return_value;
  112. }
  113.  
  114. /* Num had better be less than LITTLENUM_NUMBER_OF_BITS.  */
  115.  
  116. static void
  117. unget_bits (int num)
  118. {
  119.   if (!littlenums_left)
  120.     {
  121.       ++littlenum_pointer;
  122.       ++littlenums_left;
  123.       bits_left_in_littlenum = num;
  124.     }
  125.   else if (bits_left_in_littlenum + num > LITTLENUM_NUMBER_OF_BITS)
  126.     {
  127.       bits_left_in_littlenum =
  128.         num - (LITTLENUM_NUMBER_OF_BITS - bits_left_in_littlenum);
  129.       ++littlenum_pointer;
  130.       ++littlenums_left;
  131.     }
  132.   else
  133.     bits_left_in_littlenum += num;
  134. }
  135.  
  136. static void
  137. make_invalid_floating_point_number (LITTLENUM_TYPE *words)
  138. {
  139.   as_bad (_("cannot create floating-point number"));
  140.   /* Zero the leftmost bit.  */
  141.   words[0] = (LITTLENUM_TYPE) ((unsigned) -1) >> 1;
  142.   words[1] = (LITTLENUM_TYPE) -1;
  143.   words[2] = (LITTLENUM_TYPE) -1;
  144.   words[3] = (LITTLENUM_TYPE) -1;
  145.   words[4] = (LITTLENUM_TYPE) -1;
  146.   words[5] = (LITTLENUM_TYPE) -1;
  147. }
  148. /* Warning: This returns 16-bit LITTLENUMs.  It is up to the caller to
  149.    figure out any alignment problems and to conspire for the
  150.    bytes/word to be emitted in the right order.  Bigendians beware!  */
  151.  
  152. /* Note that atof-ieee always has X and P precisions enabled.  it is up
  153.    to md_atof to filter them out if the target machine does not support
  154.    them.  */
  155.  
  156. /* Returns pointer past text consumed.  */
  157.  
  158. char *
  159. atof_ieee (char *str,                   /* Text to convert to binary.  */
  160.            int what_kind,               /* 'd', 'f', 'x', 'p'.  */
  161.            LITTLENUM_TYPE *words)       /* Build the binary here.  */
  162. {
  163.   /* Extra bits for zeroed low-order bits.
  164.      The 1st MAX_PRECISION are zeroed, the last contain flonum bits.  */
  165.   static LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD];
  166.   char *return_value;
  167.   /* Number of 16-bit words in the format.  */
  168.   int precision;
  169.   long exponent_bits;
  170.   FLONUM_TYPE save_gen_flonum;
  171.  
  172.   /* We have to save the generic_floating_point_number because it
  173.      contains storage allocation about the array of LITTLENUMs where
  174.      the value is actually stored.  We will allocate our own array of
  175.      littlenums below, but have to restore the global one on exit.  */
  176.   save_gen_flonum = generic_floating_point_number;
  177.  
  178.   return_value = str;
  179.   generic_floating_point_number.low = bits + MAX_PRECISION;
  180.   generic_floating_point_number.high = NULL;
  181.   generic_floating_point_number.leader = NULL;
  182.   generic_floating_point_number.exponent = 0;
  183.   generic_floating_point_number.sign = '\0';
  184.  
  185.   /* Use more LittleNums than seems necessary: the highest flonum may
  186.      have 15 leading 0 bits, so could be useless.  */
  187.  
  188.   memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION);
  189.  
  190.   switch (what_kind)
  191.     {
  192.     case 'f':
  193.     case 'F':
  194.     case 's':
  195.     case 'S':
  196.       precision = F_PRECISION;
  197.       exponent_bits = 8;
  198.       break;
  199.  
  200.     case 'd':
  201.     case 'D':
  202.     case 'r':
  203.     case 'R':
  204.       precision = D_PRECISION;
  205.       exponent_bits = 11;
  206.       break;
  207.  
  208.     case 'x':
  209.     case 'X':
  210.     case 'e':
  211.     case 'E':
  212.       precision = X_PRECISION;
  213.       exponent_bits = 15;
  214.       break;
  215.  
  216.     case 'p':
  217.     case 'P':
  218.       precision = P_PRECISION;
  219.       exponent_bits = -1;
  220.       break;
  221.  
  222.     default:
  223.       make_invalid_floating_point_number (words);
  224.       return (NULL);
  225.     }
  226.  
  227.   generic_floating_point_number.high
  228.     = generic_floating_point_number.low + precision - 1 + GUARD;
  229.  
  230.   if (atof_generic (&return_value, ".", EXP_CHARS,
  231.                     &generic_floating_point_number))
  232.     {
  233.       make_invalid_floating_point_number (words);
  234.       return NULL;
  235.     }
  236.   gen_to_words (words, precision, exponent_bits);
  237.  
  238.   /* Restore the generic_floating_point_number's storage alloc (and
  239.      everything else).  */
  240.   generic_floating_point_number = save_gen_flonum;
  241.  
  242.   return return_value;
  243. }
  244.  
  245. /* Turn generic_floating_point_number into a real float/double/extended.  */
  246.  
  247. int
  248. gen_to_words (LITTLENUM_TYPE *words, int precision, long exponent_bits)
  249. {
  250.   int return_value = 0;
  251.  
  252.   long exponent_1;
  253.   long exponent_2;
  254.   long exponent_3;
  255.   long exponent_4;
  256.   int exponent_skippage;
  257.   LITTLENUM_TYPE word1;
  258.   LITTLENUM_TYPE *lp;
  259.   LITTLENUM_TYPE *words_end;
  260.  
  261.   words_end = words + precision;
  262. #ifdef TC_M68K
  263.   if (precision == X_PRECISION)
  264.     /* On the m68k the extended precision format has a gap of 16 bits
  265.        between the exponent and the mantissa.  */
  266.     words_end++;
  267. #endif
  268.  
  269.   if (generic_floating_point_number.low > generic_floating_point_number.leader)
  270.     {
  271.       /* 0.0e0 seen.  */
  272.       if (generic_floating_point_number.sign == '+')
  273.         words[0] = 0x0000;
  274.       else
  275.         words[0] = 0x8000;
  276.       memset (&words[1], '\0',
  277.               (words_end - words - 1) * sizeof (LITTLENUM_TYPE));
  278.       return return_value;
  279.     }
  280.  
  281.   /* NaN:  Do the right thing.  */
  282.   if (generic_floating_point_number.sign == 0)
  283.     {
  284.       if (TC_LARGEST_EXPONENT_IS_NORMAL (precision))
  285.         as_warn (_("NaNs are not supported by this target\n"));
  286.       if (precision == F_PRECISION)
  287.         {
  288.           words[0] = 0x7fff;
  289.           words[1] = 0xffff;
  290.         }
  291.       else if (precision == X_PRECISION)
  292.         {
  293. #ifdef TC_M68K
  294.           words[0] = 0x7fff;
  295.           words[1] = 0;
  296.           words[2] = 0xffff;
  297.           words[3] = 0xffff;
  298.           words[4] = 0xffff;
  299.           words[5] = 0xffff;
  300. #else /* ! TC_M68K  */
  301. #ifdef TC_I386
  302.           words[0] = 0xffff;
  303.           words[1] = 0xc000;
  304.           words[2] = 0;
  305.           words[3] = 0;
  306.           words[4] = 0;
  307. #else /* ! TC_I386  */
  308.           abort ();
  309. #endif /* ! TC_I386  */
  310. #endif /* ! TC_M68K  */
  311.         }
  312.       else
  313.         {
  314.           words[0] = 0x7fff;
  315.           words[1] = 0xffff;
  316.           words[2] = 0xffff;
  317.           words[3] = 0xffff;
  318.         }
  319.       return return_value;
  320.     }
  321.   else if (generic_floating_point_number.sign == 'P')
  322.     {
  323.       if (TC_LARGEST_EXPONENT_IS_NORMAL (precision))
  324.         as_warn (_("Infinities are not supported by this target\n"));
  325.  
  326.       /* +INF:  Do the right thing.  */
  327.       if (precision == F_PRECISION)
  328.         {
  329.           words[0] = 0x7f80;
  330.           words[1] = 0;
  331.         }
  332.       else if (precision == X_PRECISION)
  333.         {
  334. #ifdef TC_M68K
  335.           words[0] = 0x7fff;
  336.           words[1] = 0;
  337.           words[2] = 0;
  338.           words[3] = 0;
  339.           words[4] = 0;
  340.           words[5] = 0;
  341. #else /* ! TC_M68K  */
  342. #ifdef TC_I386
  343.           words[0] = 0x7fff;
  344.           words[1] = 0x8000;
  345.           words[2] = 0;
  346.           words[3] = 0;
  347.           words[4] = 0;
  348. #else /* ! TC_I386  */
  349.           abort ();
  350. #endif /* ! TC_I386  */
  351. #endif /* ! TC_M68K  */
  352.         }
  353.       else
  354.         {
  355.           words[0] = 0x7ff0;
  356.           words[1] = 0;
  357.           words[2] = 0;
  358.           words[3] = 0;
  359.         }
  360.       return return_value;
  361.     }
  362.   else if (generic_floating_point_number.sign == 'N')
  363.     {
  364.       if (TC_LARGEST_EXPONENT_IS_NORMAL (precision))
  365.         as_warn (_("Infinities are not supported by this target\n"));
  366.  
  367.       /* Negative INF.  */
  368.       if (precision == F_PRECISION)
  369.         {
  370.           words[0] = 0xff80;
  371.           words[1] = 0x0;
  372.         }
  373.       else if (precision == X_PRECISION)
  374.         {
  375. #ifdef TC_M68K
  376.           words[0] = 0xffff;
  377.           words[1] = 0;
  378.           words[2] = 0;
  379.           words[3] = 0;
  380.           words[4] = 0;
  381.           words[5] = 0;
  382. #else /* ! TC_M68K  */
  383. #ifdef TC_I386
  384.           words[0] = 0xffff;
  385.           words[1] = 0x8000;
  386.           words[2] = 0;
  387.           words[3] = 0;
  388.           words[4] = 0;
  389. #else /* ! TC_I386  */
  390.           abort ();
  391. #endif /* ! TC_I386  */
  392. #endif /* ! TC_M68K  */
  393.         }
  394.       else
  395.         {
  396.           words[0] = 0xfff0;
  397.           words[1] = 0x0;
  398.           words[2] = 0x0;
  399.           words[3] = 0x0;
  400.         }
  401.       return return_value;
  402.     }
  403.  
  404.   /* The floating point formats we support have:
  405.      Bit 15 is sign bit.
  406.      Bits 14:n are excess-whatever exponent.
  407.      Bits n-1:0 (if any) are most significant bits of fraction.
  408.      Bits 15:0 of the next word(s) are the next most significant bits.
  409.  
  410.      So we need: number of bits of exponent, number of bits of
  411.      mantissa.  */
  412.   bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  413.   littlenum_pointer = generic_floating_point_number.leader;
  414.   littlenums_left = (1
  415.                      + generic_floating_point_number.leader
  416.                      - generic_floating_point_number.low);
  417.  
  418.   /* Seek (and forget) 1st significant bit.  */
  419.   for (exponent_skippage = 0; !next_bits (1); ++exponent_skippage);
  420.   exponent_1 = (generic_floating_point_number.exponent
  421.                 + generic_floating_point_number.leader
  422.                 + 1
  423.                 - generic_floating_point_number.low);
  424.  
  425.   /* Radix LITTLENUM_RADIX, point just higher than
  426.      generic_floating_point_number.leader.  */
  427.   exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  428.  
  429.   /* Radix 2.  */
  430.   exponent_3 = exponent_2 - exponent_skippage;
  431.  
  432.   /* Forget leading zeros, forget 1st bit.  */
  433.   exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
  434.  
  435.   /* Offset exponent.  */
  436.   lp = words;
  437.  
  438.   /* Word 1.  Sign, exponent and perhaps high bits.  */
  439.   word1 = ((generic_floating_point_number.sign == '+')
  440.            ? 0
  441.            : (1 << (LITTLENUM_NUMBER_OF_BITS - 1)));
  442.  
  443.   /* Assume 2's complement integers.  */
  444.   if (exponent_4 <= 0)
  445.     {
  446.       int prec_bits;
  447.       int num_bits;
  448.  
  449.       unget_bits (1);
  450.       num_bits = -exponent_4;
  451.       prec_bits =
  452.         LITTLENUM_NUMBER_OF_BITS * precision - (exponent_bits + 1 + num_bits);
  453. #ifdef TC_I386
  454.       if (precision == X_PRECISION && exponent_bits == 15)
  455.         {
  456.           /* On the i386 a denormalized extended precision float is
  457.              shifted down by one, effectively decreasing the exponent
  458.              bias by one.  */
  459.           prec_bits -= 1;
  460.           num_bits += 1;
  461.         }
  462. #endif
  463.  
  464.       if (num_bits >= LITTLENUM_NUMBER_OF_BITS - exponent_bits)
  465.         {
  466.           /* Bigger than one littlenum.  */
  467.           num_bits -= (LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits;
  468.           *lp++ = word1;
  469.           if (num_bits + exponent_bits + 1
  470.               > precision * LITTLENUM_NUMBER_OF_BITS)
  471.             {
  472.               /* Exponent overflow.  */
  473.               make_invalid_floating_point_number (words);
  474.               return return_value;
  475.             }
  476. #ifdef TC_M68K
  477.           if (precision == X_PRECISION && exponent_bits == 15)
  478.             *lp++ = 0;
  479. #endif
  480.           while (num_bits >= LITTLENUM_NUMBER_OF_BITS)
  481.             {
  482.               num_bits -= LITTLENUM_NUMBER_OF_BITS;
  483.               *lp++ = 0;
  484.             }
  485.           if (num_bits)
  486.             *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - (num_bits));
  487.         }
  488.       else
  489.         {
  490.           if (precision == X_PRECISION && exponent_bits == 15)
  491.             {
  492.               *lp++ = word1;
  493. #ifdef TC_M68K
  494.               *lp++ = 0;
  495. #endif
  496.               *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - num_bits);
  497.             }
  498.           else
  499.             {
  500.               word1 |= next_bits ((LITTLENUM_NUMBER_OF_BITS - 1)
  501.                                   - (exponent_bits + num_bits));
  502.               *lp++ = word1;
  503.             }
  504.         }
  505.       while (lp < words_end)
  506.         *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
  507.  
  508.       /* Round the mantissa up, but don't change the number.  */
  509.       if (next_bits (1))
  510.         {
  511.           --lp;
  512.           if (prec_bits >= LITTLENUM_NUMBER_OF_BITS)
  513.             {
  514.               int n = 0;
  515.               int tmp_bits;
  516.  
  517.               n = 0;
  518.               tmp_bits = prec_bits;
  519.               while (tmp_bits > LITTLENUM_NUMBER_OF_BITS)
  520.                 {
  521.                   if (lp[n] != (LITTLENUM_TYPE) - 1)
  522.                     break;
  523.                   --n;
  524.                   tmp_bits -= LITTLENUM_NUMBER_OF_BITS;
  525.                 }
  526.               if (tmp_bits > LITTLENUM_NUMBER_OF_BITS
  527.                   || (lp[n] & mask[tmp_bits]) != mask[tmp_bits]
  528.                   || (prec_bits != (precision * LITTLENUM_NUMBER_OF_BITS
  529.                                     - exponent_bits - 1)
  530. #ifdef TC_I386
  531.                       /* An extended precision float with only the integer
  532.                          bit set would be invalid.  That must be converted
  533.                          to the smallest normalized number.  */
  534.                       && !(precision == X_PRECISION
  535.                            && prec_bits == (precision * LITTLENUM_NUMBER_OF_BITS
  536.                                             - exponent_bits - 2))
  537. #endif
  538.                       ))
  539.                 {
  540.                   unsigned long carry;
  541.  
  542.                   for (carry = 1; carry && (lp >= words); lp--)
  543.                     {
  544.                       carry = *lp + carry;
  545.                       *lp = carry;
  546.                       carry >>= LITTLENUM_NUMBER_OF_BITS;
  547.                     }
  548.                 }
  549.               else
  550.                 {
  551.                   /* This is an overflow of the denormal numbers.  We
  552.                      need to forget what we have produced, and instead
  553.                      generate the smallest normalized number.  */
  554.                   lp = words;
  555.                   word1 = ((generic_floating_point_number.sign == '+')
  556.                            ? 0
  557.                            : (1 << (LITTLENUM_NUMBER_OF_BITS - 1)));
  558.                   word1 |= (1
  559.                             << ((LITTLENUM_NUMBER_OF_BITS - 1)
  560.                                 - exponent_bits));
  561.                   *lp++ = word1;
  562. #ifdef TC_I386
  563.                   /* Set the integer bit in the extended precision format.
  564.                      This cannot happen on the m68k where the mantissa
  565.                      just overflows into the integer bit above.  */
  566.                   if (precision == X_PRECISION)
  567.                     *lp++ = 1 << (LITTLENUM_NUMBER_OF_BITS - 1);
  568. #endif
  569.                   while (lp < words_end)
  570.                     *lp++ = 0;
  571.                 }
  572.             }
  573.           else
  574.             *lp += 1;
  575.         }
  576.  
  577.       return return_value;
  578.     }
  579.   else if ((unsigned long) exponent_4 > mask[exponent_bits]
  580.            || (! TC_LARGEST_EXPONENT_IS_NORMAL (precision)
  581.                && (unsigned long) exponent_4 == mask[exponent_bits]))
  582.     {
  583.       /* Exponent overflow.  Lose immediately.  */
  584.  
  585.       /* We leave return_value alone: admit we read the
  586.          number, but return a floating exception
  587.          because we can't encode the number.  */
  588.       make_invalid_floating_point_number (words);
  589.       return return_value;
  590.     }
  591.   else
  592.     {
  593.       word1 |= (exponent_4 << ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits))
  594.         | next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits);
  595.     }
  596.  
  597.   *lp++ = word1;
  598.  
  599.   /* X_PRECISION is special: on the 68k, it has 16 bits of zero in the
  600.      middle.  Either way, it is then followed by a 1 bit.  */
  601.   if (exponent_bits == 15 && precision == X_PRECISION)
  602.     {
  603. #ifdef TC_M68K
  604.       *lp++ = 0;
  605. #endif
  606.       *lp++ = (1 << (LITTLENUM_NUMBER_OF_BITS - 1)
  607.                | next_bits (LITTLENUM_NUMBER_OF_BITS - 1));
  608.     }
  609.  
  610.   /* The rest of the words are just mantissa bits.  */
  611.   while (lp < words_end)
  612.     *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
  613.  
  614.   if (next_bits (1))
  615.     {
  616.       unsigned long carry;
  617.       /* Since the NEXT bit is a 1, round UP the mantissa.
  618.          The cunning design of these hidden-1 floats permits
  619.          us to let the mantissa overflow into the exponent, and
  620.          it 'does the right thing'. However, we lose if the
  621.          highest-order bit of the lowest-order word flips.
  622.          Is that clear?  */
  623.  
  624.       /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  625.          Please allow at least 1 more bit in carry than is in a LITTLENUM.
  626.          We need that extra bit to hold a carry during a LITTLENUM carry
  627.          propagation. Another extra bit (kept 0) will assure us that we
  628.          don't get a sticky sign bit after shifting right, and that
  629.          permits us to propagate the carry without any masking of bits.
  630.          #endif */
  631.       for (carry = 1, lp--; carry; lp--)
  632.         {
  633.           carry = *lp + carry;
  634.           *lp = carry;
  635.           carry >>= LITTLENUM_NUMBER_OF_BITS;
  636.           if (lp == words)
  637.             break;
  638.         }
  639.       if (precision == X_PRECISION && exponent_bits == 15)
  640.         {
  641.           /* Extended precision numbers have an explicit integer bit
  642.              that we may have to restore.  */
  643.           if (lp == words)
  644.             {
  645. #ifdef TC_M68K
  646.               /* On the m68k there is a gap of 16 bits.  We must
  647.                  explicitly propagate the carry into the exponent.  */
  648.               words[0] += words[1];
  649.               words[1] = 0;
  650.               lp++;
  651. #endif
  652.               /* Put back the integer bit.  */
  653.               lp[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS - 1);
  654.             }
  655.         }
  656.       if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)))
  657.         {
  658.           /* We leave return_value alone: admit we read the number,
  659.              but return a floating exception because we can't encode
  660.              the number.  */
  661.           *words &= ~(1 << (LITTLENUM_NUMBER_OF_BITS - 1));
  662.         }
  663.     }
  664.   return return_value;
  665. }
  666.  
  667. #ifdef TEST
  668. char *
  669. print_gen (gen)
  670.      FLONUM_TYPE *gen;
  671. {
  672.   FLONUM_TYPE f;
  673.   LITTLENUM_TYPE arr[10];
  674.   double dv;
  675.   float fv;
  676.   static char sbuf[40];
  677.  
  678.   if (gen)
  679.     {
  680.       f = generic_floating_point_number;
  681.       generic_floating_point_number = *gen;
  682.     }
  683.   gen_to_words (&arr[0], 4, 11);
  684.   memcpy (&dv, &arr[0], sizeof (double));
  685.   sprintf (sbuf, "%x %x %x %x %.14G   ", arr[0], arr[1], arr[2], arr[3], dv);
  686.   gen_to_words (&arr[0], 2, 8);
  687.   memcpy (&fv, &arr[0], sizeof (float));
  688.   sprintf (sbuf + strlen (sbuf), "%x %x %.12g\n", arr[0], arr[1], fv);
  689.  
  690.   if (gen)
  691.     generic_floating_point_number = f;
  692.  
  693.   return (sbuf);
  694. }
  695. #endif
  696.  
  697. extern const char FLT_CHARS[];
  698. #define MAX_LITTLENUMS 6
  699.  
  700. /* This is a utility function called from various tc-*.c files.  It
  701.    is here in order to reduce code duplication.
  702.  
  703.    Turn a string at input_line_pointer into a floating point constant
  704.    of type TYPE (a character found in the FLT_CHARS macro), and store
  705.    it as LITTLENUMS in the bytes buffer LITP.  The number of chars
  706.    emitted is stored in *SIZEP.  BIG_WORDIAN is TRUE if the littlenums
  707.    should be emitted most significant littlenum first.
  708.  
  709.    An error message is returned, or a NULL pointer if everything went OK.  */
  710.  
  711. char *
  712. ieee_md_atof (int type,
  713.               char *litP,
  714.               int *sizeP,
  715.               bfd_boolean big_wordian)
  716. {
  717.   LITTLENUM_TYPE words[MAX_LITTLENUMS];
  718.   LITTLENUM_TYPE *wordP;
  719.   char *t;
  720.   int prec = 0;
  721.  
  722.   if (strchr (FLT_CHARS, type) != NULL)
  723.     {
  724.       switch (type)
  725.         {
  726.         case 'f':
  727.         case 'F':
  728.         case 's':
  729.         case 'S':
  730.           prec = F_PRECISION;
  731.           break;
  732.  
  733.         case 'd':
  734.         case 'D':
  735.         case 'r':
  736.         case 'R':
  737.           prec = D_PRECISION;
  738.           break;
  739.  
  740.         case 't':
  741.         case 'T':
  742.           prec = X_PRECISION;
  743.           type = 'x';           /* This is what atof_ieee() understands.  */
  744.           break;
  745.  
  746.         case 'x':
  747.         case 'X':
  748.         case 'p':
  749.         case 'P':
  750. #ifdef TC_M68K
  751.           /* Note: on the m68k there is a gap of 16 bits (one littlenum)
  752.              between the exponent and mantissa.  Hence the precision is
  753.              6 and not 5.  */
  754.           prec = P_PRECISION + 1;
  755. #else
  756.           prec = P_PRECISION;
  757. #endif
  758.           break;
  759.  
  760.         default:
  761.           break;
  762.         }
  763.     }
  764.   /* The 'f' and 'd' types are always recognised, even if the target has
  765.      not put them into the FLT_CHARS macro.  This is because the 'f' type
  766.      can come from the .dc.s, .dcb.s, .float or .single pseudo-ops and the
  767.      'd' type from the .dc.d, .dbc.d or .double pseudo-ops.
  768.  
  769.      The 'x' type is not implicitly recongised however, even though it can
  770.      be generated by the .dc.x and .dbc.x pseudo-ops because not all targets
  771.      can support floating point values that big.  ie the target has to
  772.      explicitly allow them by putting them into FLT_CHARS.  */
  773.   else if (type == 'f')
  774.     prec = F_PRECISION;
  775.   else if (type == 'd')
  776.     prec = D_PRECISION;
  777.  
  778.   if (prec == 0)
  779.     {
  780.       *sizeP = 0;
  781.       return _("Unrecognized or unsupported floating point constant");
  782.     }
  783.  
  784.   gas_assert (prec <= MAX_LITTLENUMS);
  785.  
  786.   t = atof_ieee (input_line_pointer, type, words);
  787.   if (t)
  788.     input_line_pointer = t;
  789.  
  790.   *sizeP = prec * sizeof (LITTLENUM_TYPE);
  791.  
  792.   if (big_wordian)
  793.     {
  794.       for (wordP = words; prec --;)
  795.         {
  796.           md_number_to_chars (litP, (valueT) (* wordP ++), sizeof (LITTLENUM_TYPE));
  797.           litP += sizeof (LITTLENUM_TYPE);
  798.         }
  799.     }
  800.   else
  801.     {
  802.       for (wordP = words + prec; prec --;)
  803.         {
  804.           md_number_to_chars (litP, (valueT) (* -- wordP), sizeof (LITTLENUM_TYPE));
  805.           litP += sizeof (LITTLENUM_TYPE);
  806.         }
  807.     }
  808.  
  809.   return NULL;
  810. }
  811.