GDB (API)
/home/stan/gdb/src/gdb/doublest.c
Go to the documentation of this file.
00001 /* Floating point routines for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 /* Support for converting target fp numbers into host DOUBLEST format.  */
00021 
00022 /* XXX - This code should really be in libiberty/floatformat.c,
00023    however configuration issues with libiberty made this very
00024    difficult to do in the available time.  */
00025 
00026 #include "defs.h"
00027 #include "doublest.h"
00028 #include "floatformat.h"
00029 #include "gdb_assert.h"
00030 #include "gdb_string.h"
00031 #include "gdbtypes.h"
00032 #include <math.h>               /* ldexp */
00033 
00034 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
00035    going to bother with trying to muck around with whether it is defined in
00036    a system header, what we do if not, etc.  */
00037 #define FLOATFORMAT_CHAR_BIT 8
00038 
00039 /* The number of bytes that the largest floating-point type that we
00040    can convert to doublest will need.  */
00041 #define FLOATFORMAT_LARGEST_BYTES 16
00042 
00043 /* Extract a field which starts at START and is LEN bytes long.  DATA and
00044    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
00045 static unsigned long
00046 get_field (const bfd_byte *data, enum floatformat_byteorders order,
00047            unsigned int total_len, unsigned int start, unsigned int len)
00048 {
00049   unsigned long result;
00050   unsigned int cur_byte;
00051   int cur_bitshift;
00052 
00053   /* Caller must byte-swap words before calling this routine.  */
00054   gdb_assert (order == floatformat_little || order == floatformat_big);
00055 
00056   /* Start at the least significant part of the field.  */
00057   if (order == floatformat_little)
00058     {
00059       /* We start counting from the other end (i.e, from the high bytes
00060          rather than the low bytes).  As such, we need to be concerned
00061          with what happens if bit 0 doesn't start on a byte boundary.
00062          I.e, we need to properly handle the case where total_len is
00063          not evenly divisible by 8.  So we compute ``excess'' which
00064          represents the number of bits from the end of our starting
00065          byte needed to get to bit 0.  */
00066       int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
00067 
00068       cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) 
00069                  - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
00070       cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) 
00071                      - FLOATFORMAT_CHAR_BIT;
00072     }
00073   else
00074     {
00075       cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
00076       cur_bitshift =
00077         ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
00078     }
00079   if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
00080     result = *(data + cur_byte) >> (-cur_bitshift);
00081   else
00082     result = 0;
00083   cur_bitshift += FLOATFORMAT_CHAR_BIT;
00084   if (order == floatformat_little)
00085     ++cur_byte;
00086   else
00087     --cur_byte;
00088 
00089   /* Move towards the most significant part of the field.  */
00090   while (cur_bitshift < len)
00091     {
00092       result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
00093       cur_bitshift += FLOATFORMAT_CHAR_BIT;
00094       switch (order)
00095         {
00096         case floatformat_little:
00097           ++cur_byte;
00098           break;
00099         case floatformat_big:
00100           --cur_byte;
00101           break;
00102         }
00103     }
00104   if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT)
00105     /* Mask out bits which are not part of the field.  */
00106     result &= ((1UL << len) - 1);
00107   return result;
00108 }
00109 
00110 /* Normalize the byte order of FROM into TO.  If no normalization is
00111    needed then FMT->byteorder is returned and TO is not changed;
00112    otherwise the format of the normalized form in TO is returned.  */
00113 
00114 static enum floatformat_byteorders
00115 floatformat_normalize_byteorder (const struct floatformat *fmt,
00116                                  const void *from, void *to)
00117 {
00118   const unsigned char *swapin;
00119   unsigned char *swapout;
00120   int words;
00121   
00122   if (fmt->byteorder == floatformat_little
00123       || fmt->byteorder == floatformat_big)
00124     return fmt->byteorder;
00125 
00126   words = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
00127   words >>= 2;
00128 
00129   swapout = (unsigned char *)to;
00130   swapin = (const unsigned char *)from;
00131 
00132   if (fmt->byteorder == floatformat_vax)
00133     {
00134       while (words-- > 0)
00135         {
00136           *swapout++ = swapin[1];
00137           *swapout++ = swapin[0];
00138           *swapout++ = swapin[3];
00139           *swapout++ = swapin[2];
00140           swapin += 4;
00141         }
00142       /* This may look weird, since VAX is little-endian, but it is
00143          easier to translate to big-endian than to little-endian.  */
00144       return floatformat_big;
00145     }
00146   else
00147     {
00148       gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword);
00149 
00150       while (words-- > 0)
00151         {
00152           *swapout++ = swapin[3];
00153           *swapout++ = swapin[2];
00154           *swapout++ = swapin[1];
00155           *swapout++ = swapin[0];
00156           swapin += 4;
00157         }
00158       return floatformat_big;
00159     }
00160 }
00161   
00162 /* Convert from FMT to a DOUBLEST.
00163    FROM is the address of the extended float.
00164    Store the DOUBLEST in *TO.  */
00165 
00166 static void
00167 convert_floatformat_to_doublest (const struct floatformat *fmt,
00168                                  const void *from,
00169                                  DOUBLEST *to)
00170 {
00171   unsigned char *ufrom = (unsigned char *) from;
00172   DOUBLEST dto;
00173   long exponent;
00174   unsigned long mant;
00175   unsigned int mant_bits, mant_off;
00176   int mant_bits_left;
00177   int special_exponent;         /* It's a NaN, denorm or zero.  */
00178   enum floatformat_byteorders order;
00179   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
00180   enum float_kind kind;
00181   
00182   gdb_assert (fmt->totalsize
00183               <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
00184 
00185   /* For non-numbers, reuse libiberty's logic to find the correct
00186      format.  We do not lose any precision in this case by passing
00187      through a double.  */
00188   kind = floatformat_classify (fmt, from);
00189   if (kind == float_infinite || kind == float_nan)
00190     {
00191       double dto;
00192 
00193       floatformat_to_double (fmt->split_half ? fmt->split_half : fmt,
00194                              from, &dto);
00195       *to = (DOUBLEST) dto;
00196       return;
00197     }
00198 
00199   order = floatformat_normalize_byteorder (fmt, ufrom, newfrom);
00200 
00201   if (order != fmt->byteorder)
00202     ufrom = newfrom;
00203 
00204   if (fmt->split_half)
00205     {
00206       DOUBLEST dtop, dbot;
00207 
00208       floatformat_to_doublest (fmt->split_half, ufrom, &dtop);
00209       /* Preserve the sign of 0, which is the sign of the top
00210          half.  */
00211       if (dtop == 0.0)
00212         {
00213           *to = dtop;
00214           return;
00215         }
00216       floatformat_to_doublest (fmt->split_half,
00217                              ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2,
00218                              &dbot);
00219       *to = dtop + dbot;
00220       return;
00221     }
00222 
00223   exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start,
00224                         fmt->exp_len);
00225   /* Note that if exponent indicates a NaN, we can't really do anything useful
00226      (not knowing if the host has NaN's, or how to build one).  So it will
00227      end up as an infinity or something close; that is OK.  */
00228 
00229   mant_bits_left = fmt->man_len;
00230   mant_off = fmt->man_start;
00231   dto = 0.0;
00232 
00233   special_exponent = exponent == 0 || exponent == fmt->exp_nan;
00234 
00235   /* Don't bias NaNs.  Use minimum exponent for denorms.  For
00236      simplicity, we don't check for zero as the exponent doesn't matter.
00237      Note the cast to int; exp_bias is unsigned, so it's important to
00238      make sure the operation is done in signed arithmetic.  */
00239   if (!special_exponent)
00240     exponent -= fmt->exp_bias;
00241   else if (exponent == 0)
00242     exponent = 1 - fmt->exp_bias;
00243 
00244   /* Build the result algebraically.  Might go infinite, underflow, etc;
00245      who cares.  */
00246 
00247 /* If this format uses a hidden bit, explicitly add it in now.  Otherwise,
00248    increment the exponent by one to account for the integer bit.  */
00249 
00250   if (!special_exponent)
00251     {
00252       if (fmt->intbit == floatformat_intbit_no)
00253         dto = ldexp (1.0, exponent);
00254       else
00255         exponent++;
00256     }
00257 
00258   while (mant_bits_left > 0)
00259     {
00260       mant_bits = min (mant_bits_left, 32);
00261 
00262       mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits);
00263 
00264       dto += ldexp ((double) mant, exponent - mant_bits);
00265       exponent -= mant_bits;
00266       mant_off += mant_bits;
00267       mant_bits_left -= mant_bits;
00268     }
00269 
00270   /* Negate it if negative.  */
00271   if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1))
00272     dto = -dto;
00273   *to = dto;
00274 }
00275 
00276 /* Set a field which starts at START and is LEN bytes long.  DATA and
00277    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
00278 static void
00279 put_field (unsigned char *data, enum floatformat_byteorders order,
00280            unsigned int total_len, unsigned int start, unsigned int len,
00281            unsigned long stuff_to_put)
00282 {
00283   unsigned int cur_byte;
00284   int cur_bitshift;
00285 
00286   /* Caller must byte-swap words before calling this routine.  */
00287   gdb_assert (order == floatformat_little || order == floatformat_big);
00288 
00289   /* Start at the least significant part of the field.  */
00290   if (order == floatformat_little)
00291     {
00292       int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
00293 
00294       cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) 
00295                  - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
00296       cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) 
00297                      - FLOATFORMAT_CHAR_BIT;
00298     }
00299   else
00300     {
00301       cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
00302       cur_bitshift =
00303         ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
00304     }
00305   if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
00306     {
00307       *(data + cur_byte) &=
00308         ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
00309           << (-cur_bitshift));
00310       *(data + cur_byte) |=
00311         (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
00312     }
00313   cur_bitshift += FLOATFORMAT_CHAR_BIT;
00314   if (order == floatformat_little)
00315     ++cur_byte;
00316   else
00317     --cur_byte;
00318 
00319   /* Move towards the most significant part of the field.  */
00320   while (cur_bitshift < len)
00321     {
00322       if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
00323         {
00324           /* This is the last byte.  */
00325           *(data + cur_byte) &=
00326             ~((1 << (len - cur_bitshift)) - 1);
00327           *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
00328         }
00329       else
00330         *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
00331                               & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
00332       cur_bitshift += FLOATFORMAT_CHAR_BIT;
00333       if (order == floatformat_little)
00334         ++cur_byte;
00335       else
00336         --cur_byte;
00337     }
00338 }
00339 
00340 /* The converse: convert the DOUBLEST *FROM to an extended float and
00341    store where TO points.  Neither FROM nor TO have any alignment
00342    restrictions.  */
00343 
00344 static void
00345 convert_doublest_to_floatformat (CONST struct floatformat *fmt,
00346                                  const DOUBLEST *from, void *to)
00347 {
00348   DOUBLEST dfrom;
00349   int exponent;
00350   DOUBLEST mant;
00351   unsigned int mant_bits, mant_off;
00352   int mant_bits_left;
00353   unsigned char *uto = (unsigned char *) to;
00354   enum floatformat_byteorders order = fmt->byteorder;
00355   unsigned char newto[FLOATFORMAT_LARGEST_BYTES];
00356 
00357   if (order != floatformat_little)
00358     order = floatformat_big;
00359 
00360   if (order != fmt->byteorder)
00361     uto = newto;
00362 
00363   memcpy (&dfrom, from, sizeof (dfrom));
00364   memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1) 
00365                     / FLOATFORMAT_CHAR_BIT);
00366 
00367   if (fmt->split_half)
00368     {
00369       /* Use static volatile to ensure that any excess precision is
00370          removed via storing in memory, and so the top half really is
00371          the result of converting to double.  */
00372       static volatile double dtop, dbot;
00373       DOUBLEST dtopnv, dbotnv;
00374 
00375       dtop = (double) dfrom;
00376       /* If the rounded top half is Inf, the bottom must be 0 not NaN
00377          or Inf.  */
00378       if (dtop + dtop == dtop && dtop != 0.0)
00379         dbot = 0.0;
00380       else
00381         dbot = (double) (dfrom - (DOUBLEST) dtop);
00382       dtopnv = dtop;
00383       dbotnv = dbot;
00384       floatformat_from_doublest (fmt->split_half, &dtopnv, uto);
00385       floatformat_from_doublest (fmt->split_half, &dbotnv,
00386                                (uto
00387                                 + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2));
00388       return;
00389     }
00390 
00391   if (dfrom == 0)
00392     return;                     /* Result is zero */
00393   if (dfrom != dfrom)           /* Result is NaN */
00394     {
00395       /* From is NaN */
00396       put_field (uto, order, fmt->totalsize, fmt->exp_start,
00397                  fmt->exp_len, fmt->exp_nan);
00398       /* Be sure it's not infinity, but NaN value is irrel.  */
00399       put_field (uto, order, fmt->totalsize, fmt->man_start,
00400                  fmt->man_len, 1);
00401       goto finalize_byteorder;
00402     }
00403 
00404   /* If negative, set the sign bit.  */
00405   if (dfrom < 0)
00406     {
00407       put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1);
00408       dfrom = -dfrom;
00409     }
00410 
00411   if (dfrom + dfrom == dfrom && dfrom != 0.0)   /* Result is Infinity.  */
00412     {
00413       /* Infinity exponent is same as NaN's.  */
00414       put_field (uto, order, fmt->totalsize, fmt->exp_start,
00415                  fmt->exp_len, fmt->exp_nan);
00416       /* Infinity mantissa is all zeroes.  */
00417       put_field (uto, order, fmt->totalsize, fmt->man_start,
00418                  fmt->man_len, 0);
00419       goto finalize_byteorder;
00420     }
00421 
00422 #ifdef HAVE_LONG_DOUBLE
00423   mant = frexpl (dfrom, &exponent);
00424 #else
00425   mant = frexp (dfrom, &exponent);
00426 #endif
00427 
00428   if (exponent + fmt->exp_bias <= 0)
00429     {
00430       /* The value is too small to be expressed in the destination
00431          type (not enough bits in the exponent.  Treat as 0.  */
00432       put_field (uto, order, fmt->totalsize, fmt->exp_start,
00433                  fmt->exp_len, 0);
00434       put_field (uto, order, fmt->totalsize, fmt->man_start,
00435                  fmt->man_len, 0);
00436       goto finalize_byteorder;
00437     }
00438 
00439   if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
00440     {
00441       /* The value is too large to fit into the destination.
00442          Treat as infinity.  */
00443       put_field (uto, order, fmt->totalsize, fmt->exp_start,
00444                  fmt->exp_len, fmt->exp_nan);
00445       put_field (uto, order, fmt->totalsize, fmt->man_start,
00446                  fmt->man_len, 0);
00447       goto finalize_byteorder;
00448     }
00449 
00450   put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
00451              exponent + fmt->exp_bias - 1);
00452 
00453   mant_bits_left = fmt->man_len;
00454   mant_off = fmt->man_start;
00455   while (mant_bits_left > 0)
00456     {
00457       unsigned long mant_long;
00458 
00459       mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
00460 
00461       mant *= 4294967296.0;
00462       mant_long = ((unsigned long) mant) & 0xffffffffL;
00463       mant -= mant_long;
00464 
00465       /* If the integer bit is implicit, then we need to discard it.
00466          If we are discarding a zero, we should be (but are not) creating
00467          a denormalized number which means adjusting the exponent
00468          (I think).  */
00469       if (mant_bits_left == fmt->man_len
00470           && fmt->intbit == floatformat_intbit_no)
00471         {
00472           mant_long <<= 1;
00473           mant_long &= 0xffffffffL;
00474           /* If we are processing the top 32 mantissa bits of a doublest
00475              so as to convert to a float value with implied integer bit,
00476              we will only be putting 31 of those 32 bits into the
00477              final value due to the discarding of the top bit.  In the 
00478              case of a small float value where the number of mantissa 
00479              bits is less than 32, discarding the top bit does not alter
00480              the number of bits we will be adding to the result.  */
00481           if (mant_bits == 32)
00482             mant_bits -= 1;
00483         }
00484 
00485       if (mant_bits < 32)
00486         {
00487           /* The bits we want are in the most significant MANT_BITS bits of
00488              mant_long.  Move them to the least significant.  */
00489           mant_long >>= 32 - mant_bits;
00490         }
00491 
00492       put_field (uto, order, fmt->totalsize,
00493                  mant_off, mant_bits, mant_long);
00494       mant_off += mant_bits;
00495       mant_bits_left -= mant_bits;
00496     }
00497 
00498  finalize_byteorder:
00499   /* Do we need to byte-swap the words in the result?  */
00500   if (order != fmt->byteorder)
00501     floatformat_normalize_byteorder (fmt, newto, to);
00502 }
00503 
00504 /* Check if VAL (which is assumed to be a floating point number whose
00505    format is described by FMT) is negative.  */
00506 
00507 int
00508 floatformat_is_negative (const struct floatformat *fmt,
00509                          const bfd_byte *uval)
00510 {
00511   enum floatformat_byteorders order;
00512   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
00513   
00514   gdb_assert (fmt != NULL);
00515   gdb_assert (fmt->totalsize
00516               <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
00517 
00518   /* An IBM long double (a two element array of double) always takes the
00519      sign of the first double.  */
00520   if (fmt->split_half)
00521     fmt = fmt->split_half;
00522 
00523   order = floatformat_normalize_byteorder (fmt, uval, newfrom);
00524 
00525   if (order != fmt->byteorder)
00526     uval = newfrom;
00527 
00528   return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1);
00529 }
00530 
00531 /* Check if VAL is "not a number" (NaN) for FMT.  */
00532 
00533 enum float_kind
00534 floatformat_classify (const struct floatformat *fmt,
00535                       const bfd_byte *uval)
00536 {
00537   long exponent;
00538   unsigned long mant;
00539   unsigned int mant_bits, mant_off;
00540   int mant_bits_left;
00541   enum floatformat_byteorders order;
00542   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
00543   int mant_zero;
00544   
00545   gdb_assert (fmt != NULL);
00546   gdb_assert (fmt->totalsize
00547               <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
00548 
00549   /* An IBM long double (a two element array of double) can be classified
00550      by looking at the first double.  inf and nan are specified as
00551      ignoring the second double.  zero and subnormal will always have
00552      the second double 0.0 if the long double is correctly rounded.  */
00553   if (fmt->split_half)
00554     fmt = fmt->split_half;
00555 
00556   order = floatformat_normalize_byteorder (fmt, uval, newfrom);
00557 
00558   if (order != fmt->byteorder)
00559     uval = newfrom;
00560 
00561   exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start,
00562                         fmt->exp_len);
00563 
00564   mant_bits_left = fmt->man_len;
00565   mant_off = fmt->man_start;
00566 
00567   mant_zero = 1;
00568   while (mant_bits_left > 0)
00569     {
00570       mant_bits = min (mant_bits_left, 32);
00571 
00572       mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
00573 
00574       /* If there is an explicit integer bit, mask it off.  */
00575       if (mant_off == fmt->man_start
00576           && fmt->intbit == floatformat_intbit_yes)
00577         mant &= ~(1 << (mant_bits - 1));
00578 
00579       if (mant)
00580         {
00581           mant_zero = 0;
00582           break;
00583         }
00584 
00585       mant_off += mant_bits;
00586       mant_bits_left -= mant_bits;
00587     }
00588 
00589   /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
00590      supported.  */
00591   if (! fmt->exp_nan)
00592     {
00593       if (mant_zero)
00594         return float_zero;
00595       else
00596         return float_normal;
00597     }
00598 
00599   if (exponent == 0 && !mant_zero)
00600     return float_subnormal;
00601 
00602   if (exponent == fmt->exp_nan)
00603     {
00604       if (mant_zero)
00605         return float_infinite;
00606       else
00607         return float_nan;
00608     }
00609 
00610   if (mant_zero)
00611     return float_zero;
00612 
00613   return float_normal;
00614 }
00615 
00616 /* Convert the mantissa of VAL (which is assumed to be a floating
00617    point number whose format is described by FMT) into a hexadecimal
00618    and store it in a static string.  Return a pointer to that string.  */
00619 
00620 const char *
00621 floatformat_mantissa (const struct floatformat *fmt,
00622                       const bfd_byte *val)
00623 {
00624   unsigned char *uval = (unsigned char *) val;
00625   unsigned long mant;
00626   unsigned int mant_bits, mant_off;
00627   int mant_bits_left;
00628   static char res[50];
00629   char buf[9];
00630   int len;
00631   enum floatformat_byteorders order;
00632   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
00633   
00634   gdb_assert (fmt != NULL);
00635   gdb_assert (fmt->totalsize
00636               <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
00637 
00638   /* For IBM long double (a two element array of double), return the
00639      mantissa of the first double.  The problem with returning the
00640      actual mantissa from both doubles is that there can be an
00641      arbitrary number of implied 0's or 1's between the mantissas
00642      of the first and second double.  In any case, this function
00643      is only used for dumping out nans, and a nan is specified to
00644      ignore the value in the second double.  */
00645   if (fmt->split_half)
00646     fmt = fmt->split_half;
00647 
00648   order = floatformat_normalize_byteorder (fmt, uval, newfrom);
00649 
00650   if (order != fmt->byteorder)
00651     uval = newfrom;
00652 
00653   if (! fmt->exp_nan)
00654     return 0;
00655 
00656   /* Make sure we have enough room to store the mantissa.  */
00657   gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2);
00658 
00659   mant_off = fmt->man_start;
00660   mant_bits_left = fmt->man_len;
00661   mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32;
00662 
00663   mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
00664 
00665   len = xsnprintf (res, sizeof res, "%lx", mant);
00666 
00667   mant_off += mant_bits;
00668   mant_bits_left -= mant_bits;
00669 
00670   while (mant_bits_left > 0)
00671     {
00672       mant = get_field (uval, order, fmt->totalsize, mant_off, 32);
00673 
00674       xsnprintf (buf, sizeof buf, "%08lx", mant);
00675       gdb_assert (len + strlen (buf) <= sizeof res);
00676       strcat (res, buf);
00677 
00678       mant_off += 32;
00679       mant_bits_left -= 32;
00680     }
00681 
00682   return res;
00683 }
00684 
00685 
00686 /* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
00687 
00688    If the host and target formats agree, we just copy the raw data
00689    into the appropriate type of variable and return, letting the host
00690    increase precision as necessary.  Otherwise, we call the conversion
00691    routine and let it do the dirty work.  */
00692 
00693 static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT;
00694 static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT;
00695 static const struct floatformat *host_long_double_format
00696   = GDB_HOST_LONG_DOUBLE_FORMAT;
00697 
00698 void
00699 floatformat_to_doublest (const struct floatformat *fmt,
00700                          const void *in, DOUBLEST *out)
00701 {
00702   gdb_assert (fmt != NULL);
00703   if (fmt == host_float_format)
00704     {
00705       float val;
00706 
00707       memcpy (&val, in, sizeof (val));
00708       *out = val;
00709     }
00710   else if (fmt == host_double_format)
00711     {
00712       double val;
00713 
00714       memcpy (&val, in, sizeof (val));
00715       *out = val;
00716     }
00717   else if (fmt == host_long_double_format)
00718     {
00719       long double val;
00720 
00721       memcpy (&val, in, sizeof (val));
00722       *out = val;
00723     }
00724   else
00725     convert_floatformat_to_doublest (fmt, in, out);
00726 }
00727 
00728 void
00729 floatformat_from_doublest (const struct floatformat *fmt,
00730                            const DOUBLEST *in, void *out)
00731 {
00732   gdb_assert (fmt != NULL);
00733   if (fmt == host_float_format)
00734     {
00735       float val = *in;
00736 
00737       memcpy (out, &val, sizeof (val));
00738     }
00739   else if (fmt == host_double_format)
00740     {
00741       double val = *in;
00742 
00743       memcpy (out, &val, sizeof (val));
00744     }
00745   else if (fmt == host_long_double_format)
00746     {
00747       long double val = *in;
00748 
00749       memcpy (out, &val, sizeof (val));
00750     }
00751   else
00752     convert_doublest_to_floatformat (fmt, in, out);
00753 }
00754 
00755 
00756 /* Return a floating-point format for a floating-point variable of
00757    length LEN.  If no suitable floating-point format is found, an
00758    error is thrown.
00759 
00760    We need this functionality since information about the
00761    floating-point format of a type is not always available to GDB; the
00762    debug information typically only tells us the size of a
00763    floating-point type.
00764 
00765    FIXME: kettenis/2001-10-28: In many places, particularly in
00766    target-dependent code, the format of floating-point types is known,
00767    but not passed on by GDB.  This should be fixed.  */
00768 
00769 static const struct floatformat *
00770 floatformat_from_length (struct gdbarch *gdbarch, int len)
00771 {
00772   const struct floatformat *format;
00773 
00774   if (len * TARGET_CHAR_BIT == gdbarch_half_bit (gdbarch))
00775     format = gdbarch_half_format (gdbarch)
00776                [gdbarch_byte_order (gdbarch)];
00777   else if (len * TARGET_CHAR_BIT == gdbarch_float_bit (gdbarch))
00778     format = gdbarch_float_format (gdbarch)
00779                [gdbarch_byte_order (gdbarch)];
00780   else if (len * TARGET_CHAR_BIT == gdbarch_double_bit (gdbarch))
00781     format = gdbarch_double_format (gdbarch)
00782                [gdbarch_byte_order (gdbarch)];
00783   else if (len * TARGET_CHAR_BIT == gdbarch_long_double_bit (gdbarch))
00784     format = gdbarch_long_double_format (gdbarch)
00785                [gdbarch_byte_order (gdbarch)];
00786   /* On i386 the 'long double' type takes 96 bits,
00787      while the real number of used bits is only 80,
00788      both in processor and in memory.
00789      The code below accepts the real bit size.  */ 
00790   else if ((gdbarch_long_double_format (gdbarch) != NULL)
00791            && (len * TARGET_CHAR_BIT
00792                == gdbarch_long_double_format (gdbarch)[0]->totalsize))
00793     format = gdbarch_long_double_format (gdbarch)
00794                [gdbarch_byte_order (gdbarch)];
00795   else
00796     format = NULL;
00797   if (format == NULL)
00798     error (_("Unrecognized %d-bit floating-point type."),
00799            len * TARGET_CHAR_BIT);
00800   return format;
00801 }
00802 
00803 const struct floatformat *
00804 floatformat_from_type (const struct type *type)
00805 {
00806   struct gdbarch *gdbarch = get_type_arch (type);
00807 
00808   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
00809   if (TYPE_FLOATFORMAT (type) != NULL)
00810     return TYPE_FLOATFORMAT (type)[gdbarch_byte_order (gdbarch)];
00811   else
00812     return floatformat_from_length (gdbarch, TYPE_LENGTH (type));
00813 }
00814 
00815 /* Extract a floating-point number of type TYPE from a target-order
00816    byte-stream at ADDR.  Returns the value as type DOUBLEST.  */
00817 
00818 DOUBLEST
00819 extract_typed_floating (const void *addr, const struct type *type)
00820 {
00821   const struct floatformat *fmt = floatformat_from_type (type);
00822   DOUBLEST retval;
00823 
00824   floatformat_to_doublest (fmt, addr, &retval);
00825   return retval;
00826 }
00827 
00828 /* Store VAL as a floating-point number of type TYPE to a target-order
00829    byte-stream at ADDR.  */
00830 
00831 void
00832 store_typed_floating (void *addr, const struct type *type, DOUBLEST val)
00833 {
00834   const struct floatformat *fmt = floatformat_from_type (type);
00835 
00836   /* FIXME: kettenis/2001-10-28: It is debatable whether we should
00837      zero out any remaining bytes in the target buffer when TYPE is
00838      longer than the actual underlying floating-point format.  Perhaps
00839      we should store a fixed bitpattern in those remaining bytes,
00840      instead of zero, or perhaps we shouldn't touch those remaining
00841      bytes at all.
00842 
00843      NOTE: cagney/2001-10-28: With the way things currently work, it
00844      isn't a good idea to leave the end bits undefined.  This is
00845      because GDB writes out the entire sizeof(<floating>) bits of the
00846      floating-point type even though the value might only be stored
00847      in, and the target processor may only refer to, the first N <
00848      TYPE_LENGTH (type) bits.  If the end of the buffer wasn't
00849      initialized, GDB would write undefined data to the target.  An
00850      errant program, refering to that undefined data, would then
00851      become non-deterministic.
00852 
00853      See also the function convert_typed_floating below.  */
00854   memset (addr, 0, TYPE_LENGTH (type));
00855 
00856   floatformat_from_doublest (fmt, &val, addr);
00857 }
00858 
00859 /* Convert a floating-point number of type FROM_TYPE from a
00860    target-order byte-stream at FROM to a floating-point number of type
00861    TO_TYPE, and store it to a target-order byte-stream at TO.  */
00862 
00863 void
00864 convert_typed_floating (const void *from, const struct type *from_type,
00865                         void *to, const struct type *to_type)
00866 {
00867   const struct floatformat *from_fmt = floatformat_from_type (from_type);
00868   const struct floatformat *to_fmt = floatformat_from_type (to_type);
00869 
00870   if (from_fmt == NULL || to_fmt == NULL)
00871     {
00872       /* If we don't know the floating-point format of FROM_TYPE or
00873          TO_TYPE, there's not much we can do.  We might make the
00874          assumption that if the length of FROM_TYPE and TO_TYPE match,
00875          their floating-point format would match too, but that
00876          assumption might be wrong on targets that support
00877          floating-point types that only differ in endianness for
00878          example.  So we warn instead, and zero out the target buffer.  */
00879       warning (_("Can't convert floating-point number to desired type."));
00880       memset (to, 0, TYPE_LENGTH (to_type));
00881     }
00882   else if (from_fmt == to_fmt)
00883     {
00884       /* We're in business.  The floating-point format of FROM_TYPE
00885          and TO_TYPE match.  However, even though the floating-point
00886          format matches, the length of the type might still be
00887          different.  Make sure we don't overrun any buffers.  See
00888          comment in store_typed_floating for a discussion about
00889          zeroing out remaining bytes in the target buffer.  */
00890       memset (to, 0, TYPE_LENGTH (to_type));
00891       memcpy (to, from, min (TYPE_LENGTH (from_type), TYPE_LENGTH (to_type)));
00892     }
00893   else
00894     {
00895       /* The floating-point types don't match.  The best we can do
00896          (apart from simulating the target FPU) is converting to the
00897          widest floating-point type supported by the host, and then
00898          again to the desired type.  */
00899       DOUBLEST d;
00900 
00901       floatformat_to_doublest (from_fmt, from, &d);
00902       floatformat_from_doublest (to_fmt, &d, to);
00903     }
00904 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines