gdb/doublest.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Floating point routines for GDB, the GNU debugger.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. /* Support for converting target fp numbers into host DOUBLEST format.  */

  15. /* XXX - This code should really be in libiberty/floatformat.c,
  16.    however configuration issues with libiberty made this very
  17.    difficult to do in the available time.  */

  18. #include "defs.h"
  19. #include "doublest.h"
  20. #include "floatformat.h"
  21. #include "gdbtypes.h"
  22. #include <math.h>                /* ldexp */

  23. /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
  24.    going to bother with trying to muck around with whether it is defined in
  25.    a system header, what we do if not, etc.  */
  26. #define FLOATFORMAT_CHAR_BIT 8

  27. /* The number of bytes that the largest floating-point type that we
  28.    can convert to doublest will need.  */
  29. #define FLOATFORMAT_LARGEST_BYTES 16

  30. /* Extract a field which starts at START and is LEN bytes long.  DATA and
  31.    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
  32. static unsigned long
  33. get_field (const bfd_byte *data, enum floatformat_byteorders order,
  34.            unsigned int total_len, unsigned int start, unsigned int len)
  35. {
  36.   unsigned long result;
  37.   unsigned int cur_byte;
  38.   int cur_bitshift;

  39.   /* Caller must byte-swap words before calling this routine.  */
  40.   gdb_assert (order == floatformat_little || order == floatformat_big);

  41.   /* Start at the least significant part of the field.  */
  42.   if (order == floatformat_little)
  43.     {
  44.       /* We start counting from the other end (i.e, from the high bytes
  45.          rather than the low bytes).  As such, we need to be concerned
  46.          with what happens if bit 0 doesn't start on a byte boundary.
  47.          I.e, we need to properly handle the case where total_len is
  48.          not evenly divisible by 8.  So we compute ``excess'' which
  49.          represents the number of bits from the end of our starting
  50.          byte needed to get to bit 0.  */
  51.       int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);

  52.       cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
  53.                  - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
  54.       cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
  55.                      - FLOATFORMAT_CHAR_BIT;
  56.     }
  57.   else
  58.     {
  59.       cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
  60.       cur_bitshift =
  61.         ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
  62.     }
  63.   if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
  64.     result = *(data + cur_byte) >> (-cur_bitshift);
  65.   else
  66.     result = 0;
  67.   cur_bitshift += FLOATFORMAT_CHAR_BIT;
  68.   if (order == floatformat_little)
  69.     ++cur_byte;
  70.   else
  71.     --cur_byte;

  72.   /* Move towards the most significant part of the field.  */
  73.   while (cur_bitshift < len)
  74.     {
  75.       result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
  76.       cur_bitshift += FLOATFORMAT_CHAR_BIT;
  77.       switch (order)
  78.         {
  79.         case floatformat_little:
  80.           ++cur_byte;
  81.           break;
  82.         case floatformat_big:
  83.           --cur_byte;
  84.           break;
  85.         }
  86.     }
  87.   if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT)
  88.     /* Mask out bits which are not part of the field.  */
  89.     result &= ((1UL << len) - 1);
  90.   return result;
  91. }

  92. /* Normalize the byte order of FROM into TO.  If no normalization is
  93.    needed then FMT->byteorder is returned and TO is not changed;
  94.    otherwise the format of the normalized form in TO is returned.  */

  95. static enum floatformat_byteorders
  96. floatformat_normalize_byteorder (const struct floatformat *fmt,
  97.                                  const void *from, void *to)
  98. {
  99.   const unsigned char *swapin;
  100.   unsigned char *swapout;
  101.   int words;

  102.   if (fmt->byteorder == floatformat_little
  103.       || fmt->byteorder == floatformat_big)
  104.     return fmt->byteorder;

  105.   words = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
  106.   words >>= 2;

  107.   swapout = (unsigned char *)to;
  108.   swapin = (const unsigned char *)from;

  109.   if (fmt->byteorder == floatformat_vax)
  110.     {
  111.       while (words-- > 0)
  112.         {
  113.           *swapout++ = swapin[1];
  114.           *swapout++ = swapin[0];
  115.           *swapout++ = swapin[3];
  116.           *swapout++ = swapin[2];
  117.           swapin += 4;
  118.         }
  119.       /* This may look weird, since VAX is little-endian, but it is
  120.          easier to translate to big-endian than to little-endian.  */
  121.       return floatformat_big;
  122.     }
  123.   else
  124.     {
  125.       gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword);

  126.       while (words-- > 0)
  127.         {
  128.           *swapout++ = swapin[3];
  129.           *swapout++ = swapin[2];
  130.           *swapout++ = swapin[1];
  131.           *swapout++ = swapin[0];
  132.           swapin += 4;
  133.         }
  134.       return floatformat_big;
  135.     }
  136. }

  137. /* Convert from FMT to a DOUBLEST.
  138.    FROM is the address of the extended float.
  139.    Store the DOUBLEST in *TO.  */

  140. static void
  141. convert_floatformat_to_doublest (const struct floatformat *fmt,
  142.                                  const void *from,
  143.                                  DOUBLEST *to)
  144. {
  145.   unsigned char *ufrom = (unsigned char *) from;
  146.   DOUBLEST dto;
  147.   long exponent;
  148.   unsigned long mant;
  149.   unsigned int mant_bits, mant_off;
  150.   int mant_bits_left;
  151.   int special_exponent;                /* It's a NaN, denorm or zero.  */
  152.   enum floatformat_byteorders order;
  153.   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
  154.   enum float_kind kind;

  155.   gdb_assert (fmt->totalsize
  156.               <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);

  157.   /* For non-numbers, reuse libiberty's logic to find the correct
  158.      format.  We do not lose any precision in this case by passing
  159.      through a double.  */
  160.   kind = floatformat_classify (fmt, from);
  161.   if (kind == float_infinite || kind == float_nan)
  162.     {
  163.       double dto;

  164.       floatformat_to_double (fmt->split_half ? fmt->split_half : fmt,
  165.                              from, &dto);
  166.       *to = (DOUBLEST) dto;
  167.       return;
  168.     }

  169.   order = floatformat_normalize_byteorder (fmt, ufrom, newfrom);

  170.   if (order != fmt->byteorder)
  171.     ufrom = newfrom;

  172.   if (fmt->split_half)
  173.     {
  174.       DOUBLEST dtop, dbot;

  175.       floatformat_to_doublest (fmt->split_half, ufrom, &dtop);
  176.       /* Preserve the sign of 0, which is the sign of the top
  177.          half.  */
  178.       if (dtop == 0.0)
  179.         {
  180.           *to = dtop;
  181.           return;
  182.         }
  183.       floatformat_to_doublest (fmt->split_half,
  184.                              ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2,
  185.                              &dbot);
  186.       *to = dtop + dbot;
  187.       return;
  188.     }

  189.   exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start,
  190.                         fmt->exp_len);
  191.   /* Note that if exponent indicates a NaN, we can't really do anything useful
  192.      (not knowing if the host has NaN's, or how to build one).  So it will
  193.      end up as an infinity or something close; that is OK.  */

  194.   mant_bits_left = fmt->man_len;
  195.   mant_off = fmt->man_start;
  196.   dto = 0.0;

  197.   special_exponent = exponent == 0 || exponent == fmt->exp_nan;

  198.   /* Don't bias NaNs.  Use minimum exponent for denorms.  For
  199.      simplicity, we don't check for zero as the exponent doesn't matter.
  200.      Note the cast to int; exp_bias is unsigned, so it's important to
  201.      make sure the operation is done in signed arithmetic.  */
  202.   if (!special_exponent)
  203.     exponent -= fmt->exp_bias;
  204.   else if (exponent == 0)
  205.     exponent = 1 - fmt->exp_bias;

  206.   /* Build the result algebraically.  Might go infinite, underflow, etc;
  207.      who cares.  */

  208. /* If this format uses a hidden bit, explicitly add it in now.  Otherwise,
  209.    increment the exponent by one to account for the integer bit.  */

  210.   if (!special_exponent)
  211.     {
  212.       if (fmt->intbit == floatformat_intbit_no)
  213.         dto = ldexp (1.0, exponent);
  214.       else
  215.         exponent++;
  216.     }

  217.   while (mant_bits_left > 0)
  218.     {
  219.       mant_bits = min (mant_bits_left, 32);

  220.       mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits);

  221.       dto += ldexp ((double) mant, exponent - mant_bits);
  222.       exponent -= mant_bits;
  223.       mant_off += mant_bits;
  224.       mant_bits_left -= mant_bits;
  225.     }

  226.   /* Negate it if negative.  */
  227.   if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1))
  228.     dto = -dto;
  229.   *to = dto;
  230. }

  231. /* Set a field which starts at START and is LEN bytes long.  DATA and
  232.    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
  233. static void
  234. put_field (unsigned char *data, enum floatformat_byteorders order,
  235.            unsigned int total_len, unsigned int start, unsigned int len,
  236.            unsigned long stuff_to_put)
  237. {
  238.   unsigned int cur_byte;
  239.   int cur_bitshift;

  240.   /* Caller must byte-swap words before calling this routine.  */
  241.   gdb_assert (order == floatformat_little || order == floatformat_big);

  242.   /* Start at the least significant part of the field.  */
  243.   if (order == floatformat_little)
  244.     {
  245.       int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);

  246.       cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
  247.                  - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
  248.       cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
  249.                      - FLOATFORMAT_CHAR_BIT;
  250.     }
  251.   else
  252.     {
  253.       cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
  254.       cur_bitshift =
  255.         ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
  256.     }
  257.   if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
  258.     {
  259.       *(data + cur_byte) &=
  260.         ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
  261.           << (-cur_bitshift));
  262.       *(data + cur_byte) |=
  263.         (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
  264.     }
  265.   cur_bitshift += FLOATFORMAT_CHAR_BIT;
  266.   if (order == floatformat_little)
  267.     ++cur_byte;
  268.   else
  269.     --cur_byte;

  270.   /* Move towards the most significant part of the field.  */
  271.   while (cur_bitshift < len)
  272.     {
  273.       if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
  274.         {
  275.           /* This is the last byte.  */
  276.           *(data + cur_byte) &=
  277.             ~((1 << (len - cur_bitshift)) - 1);
  278.           *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
  279.         }
  280.       else
  281.         *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
  282.                               & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
  283.       cur_bitshift += FLOATFORMAT_CHAR_BIT;
  284.       if (order == floatformat_little)
  285.         ++cur_byte;
  286.       else
  287.         --cur_byte;
  288.     }
  289. }

  290. /* The converse: convert the DOUBLEST *FROM to an extended float and
  291.    store where TO points.  Neither FROM nor TO have any alignment
  292.    restrictions.  */

  293. static void
  294. convert_doublest_to_floatformat (const struct floatformat *fmt,
  295.                                  const DOUBLEST *from, void *to)
  296. {
  297.   DOUBLEST dfrom;
  298.   int exponent;
  299.   DOUBLEST mant;
  300.   unsigned int mant_bits, mant_off;
  301.   int mant_bits_left;
  302.   unsigned char *uto = (unsigned char *) to;
  303.   enum floatformat_byteorders order = fmt->byteorder;
  304.   unsigned char newto[FLOATFORMAT_LARGEST_BYTES];

  305.   if (order != floatformat_little)
  306.     order = floatformat_big;

  307.   if (order != fmt->byteorder)
  308.     uto = newto;

  309.   memcpy (&dfrom, from, sizeof (dfrom));
  310.   memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1)
  311.                     / FLOATFORMAT_CHAR_BIT);

  312.   if (fmt->split_half)
  313.     {
  314.       /* Use static volatile to ensure that any excess precision is
  315.          removed via storing in memory, and so the top half really is
  316.          the result of converting to double.  */
  317.       static volatile double dtop, dbot;
  318.       DOUBLEST dtopnv, dbotnv;

  319.       dtop = (double) dfrom;
  320.       /* If the rounded top half is Inf, the bottom must be 0 not NaN
  321.          or Inf.  */
  322.       if (dtop + dtop == dtop && dtop != 0.0)
  323.         dbot = 0.0;
  324.       else
  325.         dbot = (double) (dfrom - (DOUBLEST) dtop);
  326.       dtopnv = dtop;
  327.       dbotnv = dbot;
  328.       floatformat_from_doublest (fmt->split_half, &dtopnv, uto);
  329.       floatformat_from_doublest (fmt->split_half, &dbotnv,
  330.                                (uto
  331.                                 + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2));
  332.       return;
  333.     }

  334.   if (dfrom == 0)
  335.     return;                        /* Result is zero */
  336.   if (dfrom != dfrom)                /* Result is NaN */
  337.     {
  338.       /* From is NaN */
  339.       put_field (uto, order, fmt->totalsize, fmt->exp_start,
  340.                  fmt->exp_len, fmt->exp_nan);
  341.       /* Be sure it's not infinity, but NaN value is irrel.  */
  342.       put_field (uto, order, fmt->totalsize, fmt->man_start,
  343.                  fmt->man_len, 1);
  344.       goto finalize_byteorder;
  345.     }

  346.   /* If negative, set the sign bit.  */
  347.   if (dfrom < 0)
  348.     {
  349.       put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1);
  350.       dfrom = -dfrom;
  351.     }

  352.   if (dfrom + dfrom == dfrom && dfrom != 0.0)        /* Result is Infinity.  */
  353.     {
  354.       /* Infinity exponent is same as NaN's.  */
  355.       put_field (uto, order, fmt->totalsize, fmt->exp_start,
  356.                  fmt->exp_len, fmt->exp_nan);
  357.       /* Infinity mantissa is all zeroes.  */
  358.       put_field (uto, order, fmt->totalsize, fmt->man_start,
  359.                  fmt->man_len, 0);
  360.       goto finalize_byteorder;
  361.     }

  362. #ifdef HAVE_LONG_DOUBLE
  363.   mant = frexpl (dfrom, &exponent);
  364. #else
  365.   mant = frexp (dfrom, &exponent);
  366. #endif

  367.   if (exponent + fmt->exp_bias <= 0)
  368.     {
  369.       /* The value is too small to be expressed in the destination
  370.          type (not enough bits in the exponent.  Treat as 0.  */
  371.       put_field (uto, order, fmt->totalsize, fmt->exp_start,
  372.                  fmt->exp_len, 0);
  373.       put_field (uto, order, fmt->totalsize, fmt->man_start,
  374.                  fmt->man_len, 0);
  375.       goto finalize_byteorder;
  376.     }

  377.   if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
  378.     {
  379.       /* The value is too large to fit into the destination.
  380.          Treat as infinity.  */
  381.       put_field (uto, order, fmt->totalsize, fmt->exp_start,
  382.                  fmt->exp_len, fmt->exp_nan);
  383.       put_field (uto, order, fmt->totalsize, fmt->man_start,
  384.                  fmt->man_len, 0);
  385.       goto finalize_byteorder;
  386.     }

  387.   put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
  388.              exponent + fmt->exp_bias - 1);

  389.   mant_bits_left = fmt->man_len;
  390.   mant_off = fmt->man_start;
  391.   while (mant_bits_left > 0)
  392.     {
  393.       unsigned long mant_long;

  394.       mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;

  395.       mant *= 4294967296.0;
  396.       mant_long = ((unsigned long) mant) & 0xffffffffL;
  397.       mant -= mant_long;

  398.       /* If the integer bit is implicit, then we need to discard it.
  399.          If we are discarding a zero, we should be (but are not) creating
  400.          a denormalized number which means adjusting the exponent
  401.          (I think).  */
  402.       if (mant_bits_left == fmt->man_len
  403.           && fmt->intbit == floatformat_intbit_no)
  404.         {
  405.           mant_long <<= 1;
  406.           mant_long &= 0xffffffffL;
  407.           /* If we are processing the top 32 mantissa bits of a doublest
  408.              so as to convert to a float value with implied integer bit,
  409.              we will only be putting 31 of those 32 bits into the
  410.              final value due to the discarding of the top bit.  In the
  411.              case of a small float value where the number of mantissa
  412.              bits is less than 32, discarding the top bit does not alter
  413.              the number of bits we will be adding to the result.  */
  414.           if (mant_bits == 32)
  415.             mant_bits -= 1;
  416.         }

  417.       if (mant_bits < 32)
  418.         {
  419.           /* The bits we want are in the most significant MANT_BITS bits of
  420.              mant_long.  Move them to the least significant.  */
  421.           mant_long >>= 32 - mant_bits;
  422.         }

  423.       put_field (uto, order, fmt->totalsize,
  424.                  mant_off, mant_bits, mant_long);
  425.       mant_off += mant_bits;
  426.       mant_bits_left -= mant_bits;
  427.     }

  428. finalize_byteorder:
  429.   /* Do we need to byte-swap the words in the result?  */
  430.   if (order != fmt->byteorder)
  431.     floatformat_normalize_byteorder (fmt, newto, to);
  432. }

  433. /* Check if VAL (which is assumed to be a floating point number whose
  434.    format is described by FMT) is negative.  */

  435. int
  436. floatformat_is_negative (const struct floatformat *fmt,
  437.                          const bfd_byte *uval)
  438. {
  439.   enum floatformat_byteorders order;
  440.   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];

  441.   gdb_assert (fmt != NULL);
  442.   gdb_assert (fmt->totalsize
  443.               <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);

  444.   /* An IBM long double (a two element array of double) always takes the
  445.      sign of the first double.  */
  446.   if (fmt->split_half)
  447.     fmt = fmt->split_half;

  448.   order = floatformat_normalize_byteorder (fmt, uval, newfrom);

  449.   if (order != fmt->byteorder)
  450.     uval = newfrom;

  451.   return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1);
  452. }

  453. /* Check if VAL is "not a number" (NaN) for FMT.  */

  454. enum float_kind
  455. floatformat_classify (const struct floatformat *fmt,
  456.                       const bfd_byte *uval)
  457. {
  458.   long exponent;
  459.   unsigned long mant;
  460.   unsigned int mant_bits, mant_off;
  461.   int mant_bits_left;
  462.   enum floatformat_byteorders order;
  463.   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
  464.   int mant_zero;

  465.   gdb_assert (fmt != NULL);
  466.   gdb_assert (fmt->totalsize
  467.               <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);

  468.   /* An IBM long double (a two element array of double) can be classified
  469.      by looking at the first double.  inf and nan are specified as
  470.      ignoring the second double.  zero and subnormal will always have
  471.      the second double 0.0 if the long double is correctly rounded.  */
  472.   if (fmt->split_half)
  473.     fmt = fmt->split_half;

  474.   order = floatformat_normalize_byteorder (fmt, uval, newfrom);

  475.   if (order != fmt->byteorder)
  476.     uval = newfrom;

  477.   exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start,
  478.                         fmt->exp_len);

  479.   mant_bits_left = fmt->man_len;
  480.   mant_off = fmt->man_start;

  481.   mant_zero = 1;
  482.   while (mant_bits_left > 0)
  483.     {
  484.       mant_bits = min (mant_bits_left, 32);

  485.       mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);

  486.       /* If there is an explicit integer bit, mask it off.  */
  487.       if (mant_off == fmt->man_start
  488.           && fmt->intbit == floatformat_intbit_yes)
  489.         mant &= ~(1 << (mant_bits - 1));

  490.       if (mant)
  491.         {
  492.           mant_zero = 0;
  493.           break;
  494.         }

  495.       mant_off += mant_bits;
  496.       mant_bits_left -= mant_bits;
  497.     }

  498.   /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
  499.      supported.  */
  500.   if (! fmt->exp_nan)
  501.     {
  502.       if (mant_zero)
  503.         return float_zero;
  504.       else
  505.         return float_normal;
  506.     }

  507.   if (exponent == 0 && !mant_zero)
  508.     return float_subnormal;

  509.   if (exponent == fmt->exp_nan)
  510.     {
  511.       if (mant_zero)
  512.         return float_infinite;
  513.       else
  514.         return float_nan;
  515.     }

  516.   if (mant_zero)
  517.     return float_zero;

  518.   return float_normal;
  519. }

  520. /* Convert the mantissa of VAL (which is assumed to be a floating
  521.    point number whose format is described by FMT) into a hexadecimal
  522.    and store it in a static string.  Return a pointer to that string.  */

  523. const char *
  524. floatformat_mantissa (const struct floatformat *fmt,
  525.                       const bfd_byte *val)
  526. {
  527.   unsigned char *uval = (unsigned char *) val;
  528.   unsigned long mant;
  529.   unsigned int mant_bits, mant_off;
  530.   int mant_bits_left;
  531.   static char res[50];
  532.   char buf[9];
  533.   int len;
  534.   enum floatformat_byteorders order;
  535.   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];

  536.   gdb_assert (fmt != NULL);
  537.   gdb_assert (fmt->totalsize
  538.               <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);

  539.   /* For IBM long double (a two element array of double), return the
  540.      mantissa of the first double.  The problem with returning the
  541.      actual mantissa from both doubles is that there can be an
  542.      arbitrary number of implied 0's or 1's between the mantissas
  543.      of the first and second double.  In any case, this function
  544.      is only used for dumping out nans, and a nan is specified to
  545.      ignore the value in the second double.  */
  546.   if (fmt->split_half)
  547.     fmt = fmt->split_half;

  548.   order = floatformat_normalize_byteorder (fmt, uval, newfrom);

  549.   if (order != fmt->byteorder)
  550.     uval = newfrom;

  551.   if (! fmt->exp_nan)
  552.     return 0;

  553.   /* Make sure we have enough room to store the mantissa.  */
  554.   gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2);

  555.   mant_off = fmt->man_start;
  556.   mant_bits_left = fmt->man_len;
  557.   mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32;

  558.   mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);

  559.   len = xsnprintf (res, sizeof res, "%lx", mant);

  560.   mant_off += mant_bits;
  561.   mant_bits_left -= mant_bits;

  562.   while (mant_bits_left > 0)
  563.     {
  564.       mant = get_field (uval, order, fmt->totalsize, mant_off, 32);

  565.       xsnprintf (buf, sizeof buf, "%08lx", mant);
  566.       gdb_assert (len + strlen (buf) <= sizeof res);
  567.       strcat (res, buf);

  568.       mant_off += 32;
  569.       mant_bits_left -= 32;
  570.     }

  571.   return res;
  572. }


  573. /* Convert TO/FROM target to the hosts DOUBLEST floating-point format.

  574.    If the host and target formats agree, we just copy the raw data
  575.    into the appropriate type of variable and return, letting the host
  576.    increase precision as necessary.  Otherwise, we call the conversion
  577.    routine and let it do the dirty work.  */

  578. static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT;
  579. static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT;
  580. static const struct floatformat *host_long_double_format
  581.   = GDB_HOST_LONG_DOUBLE_FORMAT;

  582. void
  583. floatformat_to_doublest (const struct floatformat *fmt,
  584.                          const void *in, DOUBLEST *out)
  585. {
  586.   gdb_assert (fmt != NULL);
  587.   if (fmt == host_float_format)
  588.     {
  589.       float val;

  590.       memcpy (&val, in, sizeof (val));
  591.       *out = val;
  592.     }
  593.   else if (fmt == host_double_format)
  594.     {
  595.       double val;

  596.       memcpy (&val, in, sizeof (val));
  597.       *out = val;
  598.     }
  599.   else if (fmt == host_long_double_format)
  600.     {
  601.       long double val;

  602.       memcpy (&val, in, sizeof (val));
  603.       *out = val;
  604.     }
  605.   else
  606.     convert_floatformat_to_doublest (fmt, in, out);
  607. }

  608. void
  609. floatformat_from_doublest (const struct floatformat *fmt,
  610.                            const DOUBLEST *in, void *out)
  611. {
  612.   gdb_assert (fmt != NULL);
  613.   if (fmt == host_float_format)
  614.     {
  615.       float val = *in;

  616.       memcpy (out, &val, sizeof (val));
  617.     }
  618.   else if (fmt == host_double_format)
  619.     {
  620.       double val = *in;

  621.       memcpy (out, &val, sizeof (val));
  622.     }
  623.   else if (fmt == host_long_double_format)
  624.     {
  625.       long double val = *in;

  626.       memcpy (out, &val, sizeof (val));
  627.     }
  628.   else
  629.     convert_doublest_to_floatformat (fmt, in, out);
  630. }


  631. /* Return a floating-point format for a floating-point variable of
  632.    length LEN.  If no suitable floating-point format is found, an
  633.    error is thrown.

  634.    We need this functionality since information about the
  635.    floating-point format of a type is not always available to GDB; the
  636.    debug information typically only tells us the size of a
  637.    floating-point type.

  638.    FIXME: kettenis/2001-10-28: In many places, particularly in
  639.    target-dependent code, the format of floating-point types is known,
  640.    but not passed on by GDB.  This should be fixed.  */

  641. static const struct floatformat *
  642. floatformat_from_length (struct gdbarch *gdbarch, int len)
  643. {
  644.   const struct floatformat *format;

  645.   if (len * TARGET_CHAR_BIT == gdbarch_half_bit (gdbarch))
  646.     format = gdbarch_half_format (gdbarch)
  647.                [gdbarch_byte_order (gdbarch)];
  648.   else if (len * TARGET_CHAR_BIT == gdbarch_float_bit (gdbarch))
  649.     format = gdbarch_float_format (gdbarch)
  650.                [gdbarch_byte_order (gdbarch)];
  651.   else if (len * TARGET_CHAR_BIT == gdbarch_double_bit (gdbarch))
  652.     format = gdbarch_double_format (gdbarch)
  653.                [gdbarch_byte_order (gdbarch)];
  654.   else if (len * TARGET_CHAR_BIT == gdbarch_long_double_bit (gdbarch))
  655.     format = gdbarch_long_double_format (gdbarch)
  656.                [gdbarch_byte_order (gdbarch)];
  657.   /* On i386 the 'long double' type takes 96 bits,
  658.      while the real number of used bits is only 80,
  659.      both in processor and in memory.
  660.      The code below accepts the real bit size.  */
  661.   else if ((gdbarch_long_double_format (gdbarch) != NULL)
  662.            && (len * TARGET_CHAR_BIT
  663.                == gdbarch_long_double_format (gdbarch)[0]->totalsize))
  664.     format = gdbarch_long_double_format (gdbarch)
  665.                [gdbarch_byte_order (gdbarch)];
  666.   else
  667.     format = NULL;
  668.   if (format == NULL)
  669.     error (_("Unrecognized %d-bit floating-point type."),
  670.            len * TARGET_CHAR_BIT);
  671.   return format;
  672. }

  673. const struct floatformat *
  674. floatformat_from_type (const struct type *type)
  675. {
  676.   struct gdbarch *gdbarch = get_type_arch (type);

  677.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
  678.   if (TYPE_FLOATFORMAT (type) != NULL)
  679.     return TYPE_FLOATFORMAT (type)[gdbarch_byte_order (gdbarch)];
  680.   else
  681.     return floatformat_from_length (gdbarch, TYPE_LENGTH (type));
  682. }

  683. /* Extract a floating-point number of type TYPE from a target-order
  684.    byte-stream at ADDR.  Returns the value as type DOUBLEST.  */

  685. DOUBLEST
  686. extract_typed_floating (const void *addr, const struct type *type)
  687. {
  688.   const struct floatformat *fmt = floatformat_from_type (type);
  689.   DOUBLEST retval;

  690.   floatformat_to_doublest (fmt, addr, &retval);
  691.   return retval;
  692. }

  693. /* Store VAL as a floating-point number of type TYPE to a target-order
  694.    byte-stream at ADDR.  */

  695. void
  696. store_typed_floating (void *addr, const struct type *type, DOUBLEST val)
  697. {
  698.   const struct floatformat *fmt = floatformat_from_type (type);

  699.   /* FIXME: kettenis/2001-10-28: It is debatable whether we should
  700.      zero out any remaining bytes in the target buffer when TYPE is
  701.      longer than the actual underlying floating-point format.  Perhaps
  702.      we should store a fixed bitpattern in those remaining bytes,
  703.      instead of zero, or perhaps we shouldn't touch those remaining
  704.      bytes at all.

  705.      NOTE: cagney/2001-10-28: With the way things currently work, it
  706.      isn't a good idea to leave the end bits undefined.  This is
  707.      because GDB writes out the entire sizeof(<floating>) bits of the
  708.      floating-point type even though the value might only be stored
  709.      in, and the target processor may only refer to, the first N <
  710.      TYPE_LENGTH (type) bits.  If the end of the buffer wasn't
  711.      initialized, GDB would write undefined data to the target.  An
  712.      errant program, refering to that undefined data, would then
  713.      become non-deterministic.

  714.      See also the function convert_typed_floating below.  */
  715.   memset (addr, 0, TYPE_LENGTH (type));

  716.   floatformat_from_doublest (fmt, &val, addr);
  717. }

  718. /* Convert a floating-point number of type FROM_TYPE from a
  719.    target-order byte-stream at FROM to a floating-point number of type
  720.    TO_TYPE, and store it to a target-order byte-stream at TO.  */

  721. void
  722. convert_typed_floating (const void *from, const struct type *from_type,
  723.                         void *to, const struct type *to_type)
  724. {
  725.   const struct floatformat *from_fmt = floatformat_from_type (from_type);
  726.   const struct floatformat *to_fmt = floatformat_from_type (to_type);

  727.   if (from_fmt == NULL || to_fmt == NULL)
  728.     {
  729.       /* If we don't know the floating-point format of FROM_TYPE or
  730.          TO_TYPE, there's not much we can do.  We might make the
  731.          assumption that if the length of FROM_TYPE and TO_TYPE match,
  732.          their floating-point format would match too, but that
  733.          assumption might be wrong on targets that support
  734.          floating-point types that only differ in endianness for
  735.          example.  So we warn instead, and zero out the target buffer.  */
  736.       warning (_("Can't convert floating-point number to desired type."));
  737.       memset (to, 0, TYPE_LENGTH (to_type));
  738.     }
  739.   else if (from_fmt == to_fmt)
  740.     {
  741.       /* We're in business.  The floating-point format of FROM_TYPE
  742.          and TO_TYPE match.  However, even though the floating-point
  743.          format matches, the length of the type might still be
  744.          different.  Make sure we don't overrun any buffers.  See
  745.          comment in store_typed_floating for a discussion about
  746.          zeroing out remaining bytes in the target buffer.  */
  747.       memset (to, 0, TYPE_LENGTH (to_type));
  748.       memcpy (to, from, min (TYPE_LENGTH (from_type), TYPE_LENGTH (to_type)));
  749.     }
  750.   else
  751.     {
  752.       /* The floating-point types don't match.  The best we can do
  753.          (apart from simulating the target FPU) is converting to the
  754.          widest floating-point type supported by the host, and then
  755.          again to the desired type.  */
  756.       DOUBLEST d;

  757.       floatformat_to_doublest (from_fmt, from, &d);
  758.       floatformat_from_doublest (to_fmt, &d, to);
  759.     }
  760. }