Home | History | Annotate | Line # | Download | only in gdb
target-float.c revision 1.1.1.2
      1      1.1  christos /* Floating point routines for GDB, the GNU debugger.
      2      1.1  christos 
      3  1.1.1.2  christos    Copyright (C) 2017-2020 Free Software Foundation, Inc.
      4      1.1  christos 
      5      1.1  christos    This file is part of GDB.
      6      1.1  christos 
      7      1.1  christos    This program is free software; you can redistribute it and/or modify
      8      1.1  christos    it under the terms of the GNU General Public License as published by
      9      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10      1.1  christos    (at your option) any later version.
     11      1.1  christos 
     12      1.1  christos    This program is distributed in the hope that it will be useful,
     13      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15      1.1  christos    GNU General Public License for more details.
     16      1.1  christos 
     17      1.1  christos    You should have received a copy of the GNU General Public License
     18      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19      1.1  christos 
     20      1.1  christos #include "defs.h"
     21      1.1  christos #include "gdbtypes.h"
     22      1.1  christos #include "floatformat.h"
     23      1.1  christos #include "target-float.h"
     24  1.1.1.2  christos #include "gdbarch.h"
     25      1.1  christos 
     26      1.1  christos /* Target floating-point operations.
     27      1.1  christos 
     28      1.1  christos    We provide multiple implementations of those operations, which differ
     29      1.1  christos    by the host-side intermediate format they perform computations in.
     30      1.1  christos 
     31      1.1  christos    Those multiple implementations all derive from the following abstract
     32      1.1  christos    base class, which specifies the set of operations to be implemented.  */
     33      1.1  christos 
     34      1.1  christos class target_float_ops
     35      1.1  christos {
     36      1.1  christos public:
     37      1.1  christos   virtual std::string to_string (const gdb_byte *addr, const struct type *type,
     38      1.1  christos 				 const char *format) const = 0;
     39      1.1  christos   virtual bool from_string (gdb_byte *addr, const struct type *type,
     40      1.1  christos 			    const std::string &string) const = 0;
     41      1.1  christos 
     42      1.1  christos   virtual LONGEST to_longest (const gdb_byte *addr,
     43      1.1  christos 			      const struct type *type) const = 0;
     44      1.1  christos   virtual void from_longest (gdb_byte *addr, const struct type *type,
     45      1.1  christos 			     LONGEST val) const = 0;
     46      1.1  christos   virtual void from_ulongest (gdb_byte *addr, const struct type *type,
     47      1.1  christos 			      ULONGEST val) const = 0;
     48      1.1  christos   virtual double to_host_double (const gdb_byte *addr,
     49      1.1  christos 				 const struct type *type) const = 0;
     50      1.1  christos   virtual void from_host_double (gdb_byte *addr, const struct type *type,
     51      1.1  christos 				 double val) const = 0;
     52      1.1  christos   virtual void convert (const gdb_byte *from, const struct type *from_type,
     53      1.1  christos 			gdb_byte *to, const struct type *to_type) const = 0;
     54      1.1  christos 
     55      1.1  christos   virtual void binop (enum exp_opcode opcode,
     56      1.1  christos 		      const gdb_byte *x, const struct type *type_x,
     57      1.1  christos 		      const gdb_byte *y, const struct type *type_y,
     58      1.1  christos 		      gdb_byte *res, const struct type *type_res) const = 0;
     59      1.1  christos   virtual int compare (const gdb_byte *x, const struct type *type_x,
     60      1.1  christos 		       const gdb_byte *y, const struct type *type_y) const = 0;
     61      1.1  christos };
     62      1.1  christos 
     63      1.1  christos 
     64      1.1  christos /* Helper routines operating on binary floating-point data.  */
     65      1.1  christos 
     66      1.1  christos #include <cmath>
     67      1.1  christos #include <limits>
     68      1.1  christos 
     69      1.1  christos /* Different kinds of floatformat numbers recognized by
     70      1.1  christos    floatformat_classify.  To avoid portability issues, we use local
     71      1.1  christos    values instead of the C99 macros (FP_NAN et cetera).  */
     72      1.1  christos enum float_kind {
     73      1.1  christos   float_nan,
     74      1.1  christos   float_infinite,
     75      1.1  christos   float_zero,
     76      1.1  christos   float_normal,
     77      1.1  christos   float_subnormal
     78      1.1  christos };
     79      1.1  christos 
     80      1.1  christos /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
     81      1.1  christos    going to bother with trying to muck around with whether it is defined in
     82      1.1  christos    a system header, what we do if not, etc.  */
     83      1.1  christos #define FLOATFORMAT_CHAR_BIT 8
     84      1.1  christos 
     85      1.1  christos /* The number of bytes that the largest floating-point type that we
     86      1.1  christos    can convert to doublest will need.  */
     87      1.1  christos #define FLOATFORMAT_LARGEST_BYTES 16
     88      1.1  christos 
     89      1.1  christos /* Return the floatformat's total size in host bytes.  */
     90      1.1  christos static size_t
     91      1.1  christos floatformat_totalsize_bytes (const struct floatformat *fmt)
     92      1.1  christos {
     93      1.1  christos   return ((fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1)
     94      1.1  christos 	  / FLOATFORMAT_CHAR_BIT);
     95      1.1  christos }
     96      1.1  christos 
     97      1.1  christos /* Return the precision of the floating point format FMT.  */
     98      1.1  christos static int
     99      1.1  christos floatformat_precision (const struct floatformat *fmt)
    100      1.1  christos {
    101      1.1  christos   /* Assume the precision of and IBM long double is twice the precision
    102      1.1  christos      of the underlying double.  This matches what GCC does.  */
    103      1.1  christos   if (fmt->split_half)
    104      1.1  christos     return 2 * floatformat_precision (fmt->split_half);
    105      1.1  christos 
    106      1.1  christos   /* Otherwise, the precision is the size of mantissa in bits,
    107      1.1  christos      including the implicit bit if present.  */
    108      1.1  christos   int prec = fmt->man_len;
    109      1.1  christos   if (fmt->intbit == floatformat_intbit_no)
    110      1.1  christos     prec++;
    111      1.1  christos 
    112      1.1  christos   return prec;
    113      1.1  christos }
    114      1.1  christos 
    115      1.1  christos /* Normalize the byte order of FROM into TO.  If no normalization is
    116      1.1  christos    needed then FMT->byteorder is returned and TO is not changed;
    117      1.1  christos    otherwise the format of the normalized form in TO is returned.  */
    118      1.1  christos static enum floatformat_byteorders
    119      1.1  christos floatformat_normalize_byteorder (const struct floatformat *fmt,
    120      1.1  christos 				 const void *from, void *to)
    121      1.1  christos {
    122      1.1  christos   const unsigned char *swapin;
    123      1.1  christos   unsigned char *swapout;
    124      1.1  christos   int words;
    125      1.1  christos 
    126      1.1  christos   if (fmt->byteorder == floatformat_little
    127      1.1  christos       || fmt->byteorder == floatformat_big)
    128      1.1  christos     return fmt->byteorder;
    129      1.1  christos 
    130      1.1  christos   words = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
    131      1.1  christos   words >>= 2;
    132      1.1  christos 
    133      1.1  christos   swapout = (unsigned char *)to;
    134      1.1  christos   swapin = (const unsigned char *)from;
    135      1.1  christos 
    136      1.1  christos   if (fmt->byteorder == floatformat_vax)
    137      1.1  christos     {
    138      1.1  christos       while (words-- > 0)
    139      1.1  christos 	{
    140      1.1  christos 	  *swapout++ = swapin[1];
    141      1.1  christos 	  *swapout++ = swapin[0];
    142      1.1  christos 	  *swapout++ = swapin[3];
    143      1.1  christos 	  *swapout++ = swapin[2];
    144      1.1  christos 	  swapin += 4;
    145      1.1  christos 	}
    146      1.1  christos       /* This may look weird, since VAX is little-endian, but it is
    147      1.1  christos 	 easier to translate to big-endian than to little-endian.  */
    148      1.1  christos       return floatformat_big;
    149      1.1  christos     }
    150      1.1  christos   else
    151      1.1  christos     {
    152      1.1  christos       gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword);
    153      1.1  christos 
    154      1.1  christos       while (words-- > 0)
    155      1.1  christos 	{
    156      1.1  christos 	  *swapout++ = swapin[3];
    157      1.1  christos 	  *swapout++ = swapin[2];
    158      1.1  christos 	  *swapout++ = swapin[1];
    159      1.1  christos 	  *swapout++ = swapin[0];
    160      1.1  christos 	  swapin += 4;
    161      1.1  christos 	}
    162      1.1  christos       return floatformat_big;
    163      1.1  christos     }
    164      1.1  christos }
    165      1.1  christos 
    166      1.1  christos /* Extract a field which starts at START and is LEN bytes long.  DATA and
    167      1.1  christos    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
    168      1.1  christos static unsigned long
    169      1.1  christos get_field (const bfd_byte *data, enum floatformat_byteorders order,
    170      1.1  christos 	   unsigned int total_len, unsigned int start, unsigned int len)
    171      1.1  christos {
    172      1.1  christos   unsigned long result;
    173      1.1  christos   unsigned int cur_byte;
    174      1.1  christos   int cur_bitshift;
    175      1.1  christos 
    176      1.1  christos   /* Caller must byte-swap words before calling this routine.  */
    177      1.1  christos   gdb_assert (order == floatformat_little || order == floatformat_big);
    178      1.1  christos 
    179      1.1  christos   /* Start at the least significant part of the field.  */
    180      1.1  christos   if (order == floatformat_little)
    181      1.1  christos     {
    182      1.1  christos       /* We start counting from the other end (i.e, from the high bytes
    183      1.1  christos 	 rather than the low bytes).  As such, we need to be concerned
    184      1.1  christos 	 with what happens if bit 0 doesn't start on a byte boundary.
    185      1.1  christos 	 I.e, we need to properly handle the case where total_len is
    186      1.1  christos 	 not evenly divisible by 8.  So we compute ``excess'' which
    187      1.1  christos 	 represents the number of bits from the end of our starting
    188      1.1  christos 	 byte needed to get to bit 0.  */
    189      1.1  christos       int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
    190      1.1  christos 
    191      1.1  christos       cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
    192      1.1  christos                  - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
    193      1.1  christos       cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
    194      1.1  christos                      - FLOATFORMAT_CHAR_BIT;
    195      1.1  christos     }
    196      1.1  christos   else
    197      1.1  christos     {
    198      1.1  christos       cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
    199      1.1  christos       cur_bitshift =
    200      1.1  christos 	((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
    201      1.1  christos     }
    202      1.1  christos   if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
    203      1.1  christos     result = *(data + cur_byte) >> (-cur_bitshift);
    204      1.1  christos   else
    205      1.1  christos     result = 0;
    206      1.1  christos   cur_bitshift += FLOATFORMAT_CHAR_BIT;
    207      1.1  christos   if (order == floatformat_little)
    208      1.1  christos     ++cur_byte;
    209      1.1  christos   else
    210      1.1  christos     --cur_byte;
    211      1.1  christos 
    212      1.1  christos   /* Move towards the most significant part of the field.  */
    213      1.1  christos   while (cur_bitshift < len)
    214      1.1  christos     {
    215      1.1  christos       result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
    216      1.1  christos       cur_bitshift += FLOATFORMAT_CHAR_BIT;
    217      1.1  christos       switch (order)
    218      1.1  christos 	{
    219      1.1  christos 	case floatformat_little:
    220      1.1  christos 	  ++cur_byte;
    221      1.1  christos 	  break;
    222      1.1  christos 	case floatformat_big:
    223      1.1  christos 	  --cur_byte;
    224      1.1  christos 	  break;
    225      1.1  christos 	}
    226      1.1  christos     }
    227      1.1  christos   if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT)
    228      1.1  christos     /* Mask out bits which are not part of the field.  */
    229      1.1  christos     result &= ((1UL << len) - 1);
    230      1.1  christos   return result;
    231      1.1  christos }
    232      1.1  christos 
    233      1.1  christos /* Set a field which starts at START and is LEN bytes long.  DATA and
    234      1.1  christos    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
    235      1.1  christos static void
    236      1.1  christos put_field (unsigned char *data, enum floatformat_byteorders order,
    237      1.1  christos 	   unsigned int total_len, unsigned int start, unsigned int len,
    238      1.1  christos 	   unsigned long stuff_to_put)
    239      1.1  christos {
    240      1.1  christos   unsigned int cur_byte;
    241      1.1  christos   int cur_bitshift;
    242      1.1  christos 
    243      1.1  christos   /* Caller must byte-swap words before calling this routine.  */
    244      1.1  christos   gdb_assert (order == floatformat_little || order == floatformat_big);
    245      1.1  christos 
    246      1.1  christos   /* Start at the least significant part of the field.  */
    247      1.1  christos   if (order == floatformat_little)
    248      1.1  christos     {
    249      1.1  christos       int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
    250      1.1  christos 
    251      1.1  christos       cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
    252      1.1  christos                  - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
    253      1.1  christos       cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
    254      1.1  christos                      - FLOATFORMAT_CHAR_BIT;
    255      1.1  christos     }
    256      1.1  christos   else
    257      1.1  christos     {
    258      1.1  christos       cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
    259      1.1  christos       cur_bitshift =
    260      1.1  christos 	((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
    261      1.1  christos     }
    262      1.1  christos   if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
    263      1.1  christos     {
    264      1.1  christos       *(data + cur_byte) &=
    265      1.1  christos 	~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
    266      1.1  christos 	  << (-cur_bitshift));
    267      1.1  christos       *(data + cur_byte) |=
    268      1.1  christos 	(stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
    269      1.1  christos     }
    270      1.1  christos   cur_bitshift += FLOATFORMAT_CHAR_BIT;
    271      1.1  christos   if (order == floatformat_little)
    272      1.1  christos     ++cur_byte;
    273      1.1  christos   else
    274      1.1  christos     --cur_byte;
    275      1.1  christos 
    276      1.1  christos   /* Move towards the most significant part of the field.  */
    277      1.1  christos   while (cur_bitshift < len)
    278      1.1  christos     {
    279      1.1  christos       if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
    280      1.1  christos 	{
    281      1.1  christos 	  /* This is the last byte.  */
    282      1.1  christos 	  *(data + cur_byte) &=
    283      1.1  christos 	    ~((1 << (len - cur_bitshift)) - 1);
    284      1.1  christos 	  *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
    285      1.1  christos 	}
    286      1.1  christos       else
    287      1.1  christos 	*(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
    288      1.1  christos 			      & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
    289      1.1  christos       cur_bitshift += FLOATFORMAT_CHAR_BIT;
    290      1.1  christos       if (order == floatformat_little)
    291      1.1  christos 	++cur_byte;
    292      1.1  christos       else
    293      1.1  christos 	--cur_byte;
    294      1.1  christos     }
    295      1.1  christos }
    296      1.1  christos 
    297      1.1  christos /* Check if VAL (which is assumed to be a floating point number whose
    298      1.1  christos    format is described by FMT) is negative.  */
    299      1.1  christos static int
    300      1.1  christos floatformat_is_negative (const struct floatformat *fmt,
    301      1.1  christos 			 const bfd_byte *uval)
    302      1.1  christos {
    303      1.1  christos   enum floatformat_byteorders order;
    304      1.1  christos   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
    305      1.1  christos 
    306      1.1  christos   gdb_assert (fmt != NULL);
    307      1.1  christos   gdb_assert (fmt->totalsize
    308      1.1  christos 	      <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
    309      1.1  christos 
    310      1.1  christos   /* An IBM long double (a two element array of double) always takes the
    311      1.1  christos      sign of the first double.  */
    312      1.1  christos   if (fmt->split_half)
    313      1.1  christos     fmt = fmt->split_half;
    314      1.1  christos 
    315      1.1  christos   order = floatformat_normalize_byteorder (fmt, uval, newfrom);
    316      1.1  christos 
    317      1.1  christos   if (order != fmt->byteorder)
    318      1.1  christos     uval = newfrom;
    319      1.1  christos 
    320      1.1  christos   return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1);
    321      1.1  christos }
    322      1.1  christos 
    323      1.1  christos /* Check if VAL is "not a number" (NaN) for FMT.  */
    324      1.1  christos static enum float_kind
    325      1.1  christos floatformat_classify (const struct floatformat *fmt,
    326      1.1  christos 		      const bfd_byte *uval)
    327      1.1  christos {
    328      1.1  christos   long exponent;
    329      1.1  christos   unsigned long mant;
    330      1.1  christos   unsigned int mant_bits, mant_off;
    331      1.1  christos   int mant_bits_left;
    332      1.1  christos   enum floatformat_byteorders order;
    333      1.1  christos   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
    334      1.1  christos   int mant_zero;
    335      1.1  christos 
    336      1.1  christos   gdb_assert (fmt != NULL);
    337      1.1  christos   gdb_assert (fmt->totalsize
    338      1.1  christos 	      <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
    339      1.1  christos 
    340      1.1  christos   /* An IBM long double (a two element array of double) can be classified
    341      1.1  christos      by looking at the first double.  inf and nan are specified as
    342      1.1  christos      ignoring the second double.  zero and subnormal will always have
    343      1.1  christos      the second double 0.0 if the long double is correctly rounded.  */
    344      1.1  christos   if (fmt->split_half)
    345      1.1  christos     fmt = fmt->split_half;
    346      1.1  christos 
    347      1.1  christos   order = floatformat_normalize_byteorder (fmt, uval, newfrom);
    348      1.1  christos 
    349      1.1  christos   if (order != fmt->byteorder)
    350      1.1  christos     uval = newfrom;
    351      1.1  christos 
    352      1.1  christos   exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start,
    353      1.1  christos 			fmt->exp_len);
    354      1.1  christos 
    355      1.1  christos   mant_bits_left = fmt->man_len;
    356      1.1  christos   mant_off = fmt->man_start;
    357      1.1  christos 
    358      1.1  christos   mant_zero = 1;
    359      1.1  christos   while (mant_bits_left > 0)
    360      1.1  christos     {
    361      1.1  christos       mant_bits = std::min (mant_bits_left, 32);
    362      1.1  christos 
    363      1.1  christos       mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
    364      1.1  christos 
    365      1.1  christos       /* If there is an explicit integer bit, mask it off.  */
    366      1.1  christos       if (mant_off == fmt->man_start
    367      1.1  christos 	  && fmt->intbit == floatformat_intbit_yes)
    368      1.1  christos 	mant &= ~(1 << (mant_bits - 1));
    369      1.1  christos 
    370      1.1  christos       if (mant)
    371      1.1  christos 	{
    372      1.1  christos 	  mant_zero = 0;
    373      1.1  christos 	  break;
    374      1.1  christos 	}
    375      1.1  christos 
    376      1.1  christos       mant_off += mant_bits;
    377      1.1  christos       mant_bits_left -= mant_bits;
    378      1.1  christos     }
    379      1.1  christos 
    380      1.1  christos   /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
    381      1.1  christos      supported.  */
    382      1.1  christos   if (! fmt->exp_nan)
    383      1.1  christos     {
    384      1.1  christos       if (mant_zero)
    385      1.1  christos 	return float_zero;
    386      1.1  christos       else
    387      1.1  christos 	return float_normal;
    388      1.1  christos     }
    389      1.1  christos 
    390      1.1  christos   if (exponent == 0)
    391      1.1  christos     {
    392      1.1  christos       if (mant_zero)
    393      1.1  christos 	return float_zero;
    394      1.1  christos       else
    395      1.1  christos 	return float_subnormal;
    396      1.1  christos     }
    397      1.1  christos 
    398      1.1  christos   if (exponent == fmt->exp_nan)
    399      1.1  christos     {
    400      1.1  christos       if (mant_zero)
    401      1.1  christos 	return float_infinite;
    402      1.1  christos       else
    403      1.1  christos 	return float_nan;
    404      1.1  christos     }
    405      1.1  christos 
    406      1.1  christos   return float_normal;
    407      1.1  christos }
    408      1.1  christos 
    409      1.1  christos /* Convert the mantissa of VAL (which is assumed to be a floating
    410      1.1  christos    point number whose format is described by FMT) into a hexadecimal
    411      1.1  christos    and store it in a static string.  Return a pointer to that string.  */
    412      1.1  christos static const char *
    413      1.1  christos floatformat_mantissa (const struct floatformat *fmt,
    414      1.1  christos 		      const bfd_byte *val)
    415      1.1  christos {
    416      1.1  christos   unsigned char *uval = (unsigned char *) val;
    417      1.1  christos   unsigned long mant;
    418      1.1  christos   unsigned int mant_bits, mant_off;
    419      1.1  christos   int mant_bits_left;
    420      1.1  christos   static char res[50];
    421      1.1  christos   char buf[9];
    422      1.1  christos   int len;
    423      1.1  christos   enum floatformat_byteorders order;
    424      1.1  christos   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
    425      1.1  christos 
    426      1.1  christos   gdb_assert (fmt != NULL);
    427      1.1  christos   gdb_assert (fmt->totalsize
    428      1.1  christos 	      <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
    429      1.1  christos 
    430      1.1  christos   /* For IBM long double (a two element array of double), return the
    431      1.1  christos      mantissa of the first double.  The problem with returning the
    432      1.1  christos      actual mantissa from both doubles is that there can be an
    433      1.1  christos      arbitrary number of implied 0's or 1's between the mantissas
    434      1.1  christos      of the first and second double.  In any case, this function
    435      1.1  christos      is only used for dumping out nans, and a nan is specified to
    436      1.1  christos      ignore the value in the second double.  */
    437      1.1  christos   if (fmt->split_half)
    438      1.1  christos     fmt = fmt->split_half;
    439      1.1  christos 
    440      1.1  christos   order = floatformat_normalize_byteorder (fmt, uval, newfrom);
    441      1.1  christos 
    442      1.1  christos   if (order != fmt->byteorder)
    443      1.1  christos     uval = newfrom;
    444      1.1  christos 
    445      1.1  christos   if (! fmt->exp_nan)
    446      1.1  christos     return 0;
    447      1.1  christos 
    448      1.1  christos   /* Make sure we have enough room to store the mantissa.  */
    449      1.1  christos   gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2);
    450      1.1  christos 
    451      1.1  christos   mant_off = fmt->man_start;
    452      1.1  christos   mant_bits_left = fmt->man_len;
    453      1.1  christos   mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32;
    454      1.1  christos 
    455      1.1  christos   mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
    456      1.1  christos 
    457      1.1  christos   len = xsnprintf (res, sizeof res, "%lx", mant);
    458      1.1  christos 
    459      1.1  christos   mant_off += mant_bits;
    460      1.1  christos   mant_bits_left -= mant_bits;
    461      1.1  christos 
    462      1.1  christos   while (mant_bits_left > 0)
    463      1.1  christos     {
    464      1.1  christos       mant = get_field (uval, order, fmt->totalsize, mant_off, 32);
    465      1.1  christos 
    466      1.1  christos       xsnprintf (buf, sizeof buf, "%08lx", mant);
    467      1.1  christos       gdb_assert (len + strlen (buf) <= sizeof res);
    468      1.1  christos       strcat (res, buf);
    469      1.1  christos 
    470      1.1  christos       mant_off += 32;
    471      1.1  christos       mant_bits_left -= 32;
    472      1.1  christos     }
    473      1.1  christos 
    474      1.1  christos   return res;
    475      1.1  christos }
    476      1.1  christos 
    477      1.1  christos /* Convert printf format string FORMAT to the otherwise equivalent string
    478      1.1  christos    which may be used to print a host floating-point number using the length
    479      1.1  christos    modifier LENGTH (which may be 0 if none is needed).  If FORMAT is null,
    480      1.1  christos    return a format appropriate to print the full precision of a target
    481      1.1  christos    floating-point number of format FMT.  */
    482      1.1  christos static std::string
    483      1.1  christos floatformat_printf_format (const struct floatformat *fmt,
    484      1.1  christos                            const char *format, char length)
    485      1.1  christos {
    486      1.1  christos   std::string host_format;
    487      1.1  christos   char conversion;
    488      1.1  christos 
    489      1.1  christos   if (format == nullptr)
    490      1.1  christos     {
    491      1.1  christos       /* If no format was specified, print the number using a format string
    492      1.1  christos 	 where the precision is set to the DECIMAL_DIG value for the given
    493      1.1  christos 	 floating-point format.  This value is computed as
    494      1.1  christos 
    495      1.1  christos 		ceil(1 + p * log10(b)),
    496      1.1  christos 
    497      1.1  christos 	 where p is the precision of the floating-point format in bits, and
    498      1.1  christos 	 b is the base (which is always 2 for the formats we support).  */
    499      1.1  christos       const double log10_2 = .30102999566398119521;
    500      1.1  christos       double d_decimal_dig = 1 + floatformat_precision (fmt) * log10_2;
    501      1.1  christos       int decimal_dig = d_decimal_dig;
    502      1.1  christos       if (decimal_dig < d_decimal_dig)
    503      1.1  christos 	decimal_dig++;
    504      1.1  christos 
    505      1.1  christos       host_format = string_printf ("%%.%d", decimal_dig);
    506      1.1  christos       conversion = 'g';
    507      1.1  christos     }
    508      1.1  christos   else
    509      1.1  christos     {
    510      1.1  christos       /* Use the specified format, stripping out the conversion character
    511      1.1  christos          and length modifier, if present.  */
    512      1.1  christos       size_t len = strlen (format);
    513      1.1  christos       gdb_assert (len > 1);
    514      1.1  christos       conversion = format[--len];
    515      1.1  christos       gdb_assert (conversion == 'e' || conversion == 'f' || conversion == 'g'
    516      1.1  christos 		  || conversion == 'E' || conversion == 'G');
    517      1.1  christos       if (format[len - 1] == 'L')
    518      1.1  christos 	len--;
    519      1.1  christos 
    520      1.1  christos       host_format = std::string (format, len);
    521      1.1  christos     }
    522      1.1  christos 
    523      1.1  christos   /* Add the length modifier and conversion character appropriate for
    524      1.1  christos      handling the appropriate host floating-point type.  */
    525      1.1  christos   if (length)
    526      1.1  christos     host_format += length;
    527      1.1  christos   host_format += conversion;
    528      1.1  christos 
    529      1.1  christos   return host_format;
    530      1.1  christos }
    531      1.1  christos 
    532      1.1  christos /* Implementation of target_float_ops using the host floating-point type T
    533      1.1  christos    as intermediate type.  */
    534      1.1  christos 
    535      1.1  christos template<typename T> class host_float_ops : public target_float_ops
    536      1.1  christos {
    537      1.1  christos public:
    538      1.1  christos   std::string to_string (const gdb_byte *addr, const struct type *type,
    539      1.1  christos 			 const char *format) const override;
    540      1.1  christos   bool from_string (gdb_byte *addr, const struct type *type,
    541      1.1  christos 		    const std::string &string) const override;
    542      1.1  christos 
    543      1.1  christos   LONGEST to_longest (const gdb_byte *addr,
    544      1.1  christos 		      const struct type *type) const override;
    545      1.1  christos   void from_longest (gdb_byte *addr, const struct type *type,
    546      1.1  christos 		     LONGEST val) const override;
    547      1.1  christos   void from_ulongest (gdb_byte *addr, const struct type *type,
    548      1.1  christos 		      ULONGEST val) const override;
    549      1.1  christos   double to_host_double (const gdb_byte *addr,
    550      1.1  christos 			 const struct type *type) const override;
    551      1.1  christos   void from_host_double (gdb_byte *addr, const struct type *type,
    552      1.1  christos 			 double val) const override;
    553      1.1  christos   void convert (const gdb_byte *from, const struct type *from_type,
    554      1.1  christos 		gdb_byte *to, const struct type *to_type) const override;
    555      1.1  christos 
    556      1.1  christos   void binop (enum exp_opcode opcode,
    557      1.1  christos 	      const gdb_byte *x, const struct type *type_x,
    558      1.1  christos 	      const gdb_byte *y, const struct type *type_y,
    559      1.1  christos 	      gdb_byte *res, const struct type *type_res) const override;
    560      1.1  christos   int compare (const gdb_byte *x, const struct type *type_x,
    561      1.1  christos 	       const gdb_byte *y, const struct type *type_y) const override;
    562      1.1  christos 
    563      1.1  christos private:
    564      1.1  christos   void from_target (const struct floatformat *fmt,
    565      1.1  christos 		    const gdb_byte *from, T *to) const;
    566      1.1  christos   void from_target (const struct type *type,
    567      1.1  christos 		    const gdb_byte *from, T *to) const;
    568      1.1  christos 
    569      1.1  christos   void to_target (const struct type *type,
    570      1.1  christos 		  const T *from, gdb_byte *to) const;
    571      1.1  christos   void to_target (const struct floatformat *fmt,
    572      1.1  christos 		  const T *from, gdb_byte *to) const;
    573      1.1  christos };
    574      1.1  christos 
    575      1.1  christos 
    576      1.1  christos /* Convert TO/FROM target to the host floating-point format T.
    577      1.1  christos 
    578      1.1  christos    If the host and target formats agree, we just copy the raw data
    579      1.1  christos    into the appropriate type of variable and return, letting the host
    580      1.1  christos    increase precision as necessary.  Otherwise, we call the conversion
    581      1.1  christos    routine and let it do the dirty work.  Note that even if the target
    582      1.1  christos    and host floating-point formats match, the length of the types
    583      1.1  christos    might still be different, so the conversion routines must make sure
    584      1.1  christos    to not overrun any buffers.  For example, on x86, long double is
    585      1.1  christos    the 80-bit extended precision type on both 32-bit and 64-bit ABIs,
    586      1.1  christos    but by default it is stored as 12 bytes on 32-bit, and 16 bytes on
    587      1.1  christos    64-bit, for alignment reasons.  See comment in store_typed_floating
    588      1.1  christos    for a discussion about zeroing out remaining bytes in the target
    589      1.1  christos    buffer.  */
    590      1.1  christos 
    591      1.1  christos static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT;
    592      1.1  christos static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT;
    593      1.1  christos static const struct floatformat *host_long_double_format
    594      1.1  christos   = GDB_HOST_LONG_DOUBLE_FORMAT;
    595      1.1  christos 
    596      1.1  christos /* Convert target floating-point value at FROM in format FMT to host
    597      1.1  christos    floating-point format of type T.  */
    598      1.1  christos template<typename T> void
    599      1.1  christos host_float_ops<T>::from_target (const struct floatformat *fmt,
    600      1.1  christos 				const gdb_byte *from, T *to) const
    601      1.1  christos {
    602      1.1  christos   gdb_assert (fmt != NULL);
    603      1.1  christos 
    604      1.1  christos   if (fmt == host_float_format)
    605      1.1  christos     {
    606      1.1  christos       float val = 0;
    607      1.1  christos 
    608      1.1  christos       memcpy (&val, from, floatformat_totalsize_bytes (fmt));
    609      1.1  christos       *to = val;
    610      1.1  christos       return;
    611      1.1  christos     }
    612      1.1  christos   else if (fmt == host_double_format)
    613      1.1  christos     {
    614      1.1  christos       double val = 0;
    615      1.1  christos 
    616      1.1  christos       memcpy (&val, from, floatformat_totalsize_bytes (fmt));
    617      1.1  christos       *to = val;
    618      1.1  christos       return;
    619      1.1  christos     }
    620      1.1  christos   else if (fmt == host_long_double_format)
    621      1.1  christos     {
    622      1.1  christos       long double val = 0;
    623      1.1  christos 
    624      1.1  christos       memcpy (&val, from, floatformat_totalsize_bytes (fmt));
    625      1.1  christos       *to = val;
    626      1.1  christos       return;
    627      1.1  christos     }
    628      1.1  christos 
    629      1.1  christos   unsigned char *ufrom = (unsigned char *) from;
    630      1.1  christos   long exponent;
    631      1.1  christos   unsigned long mant;
    632      1.1  christos   unsigned int mant_bits, mant_off;
    633      1.1  christos   int mant_bits_left;
    634      1.1  christos   int special_exponent;		/* It's a NaN, denorm or zero.  */
    635      1.1  christos   enum floatformat_byteorders order;
    636      1.1  christos   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
    637      1.1  christos   enum float_kind kind;
    638      1.1  christos 
    639      1.1  christos   gdb_assert (fmt->totalsize
    640      1.1  christos 	      <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
    641      1.1  christos 
    642      1.1  christos   /* For non-numbers, reuse libiberty's logic to find the correct
    643      1.1  christos      format.  We do not lose any precision in this case by passing
    644      1.1  christos      through a double.  */
    645      1.1  christos   kind = floatformat_classify (fmt, (const bfd_byte *) from);
    646      1.1  christos   if (kind == float_infinite || kind == float_nan)
    647      1.1  christos     {
    648      1.1  christos       double dto;
    649      1.1  christos 
    650  1.1.1.2  christos       floatformat_to_double	/* ARI: floatformat_to_double */
    651  1.1.1.2  christos 	(fmt->split_half ? fmt->split_half : fmt, from, &dto);
    652      1.1  christos       *to = (T) dto;
    653      1.1  christos       return;
    654      1.1  christos     }
    655      1.1  christos 
    656      1.1  christos   order = floatformat_normalize_byteorder (fmt, ufrom, newfrom);
    657      1.1  christos 
    658      1.1  christos   if (order != fmt->byteorder)
    659      1.1  christos     ufrom = newfrom;
    660      1.1  christos 
    661      1.1  christos   if (fmt->split_half)
    662      1.1  christos     {
    663      1.1  christos       T dtop, dbot;
    664      1.1  christos 
    665      1.1  christos       from_target (fmt->split_half, ufrom, &dtop);
    666      1.1  christos       /* Preserve the sign of 0, which is the sign of the top
    667      1.1  christos 	 half.  */
    668      1.1  christos       if (dtop == 0.0)
    669      1.1  christos 	{
    670      1.1  christos 	  *to = dtop;
    671      1.1  christos 	  return;
    672      1.1  christos 	}
    673      1.1  christos       from_target (fmt->split_half,
    674      1.1  christos 		   ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, &dbot);
    675      1.1  christos       *to = dtop + dbot;
    676      1.1  christos       return;
    677      1.1  christos     }
    678      1.1  christos 
    679      1.1  christos   exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start,
    680      1.1  christos 			fmt->exp_len);
    681      1.1  christos   /* Note that if exponent indicates a NaN, we can't really do anything useful
    682      1.1  christos      (not knowing if the host has NaN's, or how to build one).  So it will
    683      1.1  christos      end up as an infinity or something close; that is OK.  */
    684      1.1  christos 
    685      1.1  christos   mant_bits_left = fmt->man_len;
    686      1.1  christos   mant_off = fmt->man_start;
    687      1.1  christos   T dto = 0.0;
    688      1.1  christos 
    689      1.1  christos   special_exponent = exponent == 0 || exponent == fmt->exp_nan;
    690      1.1  christos 
    691      1.1  christos   /* Don't bias NaNs.  Use minimum exponent for denorms.  For
    692      1.1  christos      simplicity, we don't check for zero as the exponent doesn't matter.
    693      1.1  christos      Note the cast to int; exp_bias is unsigned, so it's important to
    694      1.1  christos      make sure the operation is done in signed arithmetic.  */
    695      1.1  christos   if (!special_exponent)
    696      1.1  christos     exponent -= fmt->exp_bias;
    697      1.1  christos   else if (exponent == 0)
    698      1.1  christos     exponent = 1 - fmt->exp_bias;
    699      1.1  christos 
    700      1.1  christos   /* Build the result algebraically.  Might go infinite, underflow, etc;
    701      1.1  christos      who cares.  */
    702      1.1  christos 
    703      1.1  christos   /* If this format uses a hidden bit, explicitly add it in now.  Otherwise,
    704      1.1  christos      increment the exponent by one to account for the integer bit.  */
    705      1.1  christos 
    706      1.1  christos   if (!special_exponent)
    707      1.1  christos     {
    708      1.1  christos       if (fmt->intbit == floatformat_intbit_no)
    709      1.1  christos 	dto = ldexp (1.0, exponent);
    710      1.1  christos       else
    711      1.1  christos 	exponent++;
    712      1.1  christos     }
    713      1.1  christos 
    714      1.1  christos   while (mant_bits_left > 0)
    715      1.1  christos     {
    716      1.1  christos       mant_bits = std::min (mant_bits_left, 32);
    717      1.1  christos 
    718      1.1  christos       mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits);
    719      1.1  christos 
    720      1.1  christos       dto += ldexp ((T) mant, exponent - mant_bits);
    721      1.1  christos       exponent -= mant_bits;
    722      1.1  christos       mant_off += mant_bits;
    723      1.1  christos       mant_bits_left -= mant_bits;
    724      1.1  christos     }
    725      1.1  christos 
    726      1.1  christos   /* Negate it if negative.  */
    727      1.1  christos   if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1))
    728      1.1  christos     dto = -dto;
    729      1.1  christos   *to = dto;
    730      1.1  christos }
    731      1.1  christos 
    732      1.1  christos template<typename T> void
    733      1.1  christos host_float_ops<T>::from_target (const struct type *type,
    734      1.1  christos 				const gdb_byte *from, T *to) const
    735      1.1  christos {
    736      1.1  christos   from_target (floatformat_from_type (type), from, to);
    737      1.1  christos }
    738      1.1  christos 
    739      1.1  christos /* Convert host floating-point value of type T to target floating-point
    740      1.1  christos    value in format FMT and store at TO.  */
    741      1.1  christos template<typename T> void
    742      1.1  christos host_float_ops<T>::to_target (const struct floatformat *fmt,
    743      1.1  christos 			      const T *from, gdb_byte *to) const
    744      1.1  christos {
    745      1.1  christos   gdb_assert (fmt != NULL);
    746      1.1  christos 
    747      1.1  christos   if (fmt == host_float_format)
    748      1.1  christos     {
    749      1.1  christos       float val = *from;
    750      1.1  christos 
    751      1.1  christos       memcpy (to, &val, floatformat_totalsize_bytes (fmt));
    752      1.1  christos       return;
    753      1.1  christos     }
    754      1.1  christos   else if (fmt == host_double_format)
    755      1.1  christos     {
    756      1.1  christos       double val = *from;
    757      1.1  christos 
    758      1.1  christos       memcpy (to, &val, floatformat_totalsize_bytes (fmt));
    759      1.1  christos       return;
    760      1.1  christos     }
    761      1.1  christos   else if (fmt == host_long_double_format)
    762      1.1  christos     {
    763      1.1  christos       long double val = *from;
    764      1.1  christos 
    765      1.1  christos       memcpy (to, &val, floatformat_totalsize_bytes (fmt));
    766      1.1  christos       return;
    767      1.1  christos     }
    768      1.1  christos 
    769      1.1  christos   T dfrom;
    770      1.1  christos   int exponent;
    771      1.1  christos   T mant;
    772      1.1  christos   unsigned int mant_bits, mant_off;
    773      1.1  christos   int mant_bits_left;
    774      1.1  christos   unsigned char *uto = (unsigned char *) to;
    775      1.1  christos   enum floatformat_byteorders order = fmt->byteorder;
    776      1.1  christos   unsigned char newto[FLOATFORMAT_LARGEST_BYTES];
    777      1.1  christos 
    778      1.1  christos   if (order != floatformat_little)
    779      1.1  christos     order = floatformat_big;
    780      1.1  christos 
    781      1.1  christos   if (order != fmt->byteorder)
    782      1.1  christos     uto = newto;
    783      1.1  christos 
    784      1.1  christos   memcpy (&dfrom, from, sizeof (dfrom));
    785      1.1  christos   memset (uto, 0, floatformat_totalsize_bytes (fmt));
    786      1.1  christos 
    787      1.1  christos   if (fmt->split_half)
    788      1.1  christos     {
    789      1.1  christos       /* Use static volatile to ensure that any excess precision is
    790      1.1  christos 	 removed via storing in memory, and so the top half really is
    791      1.1  christos 	 the result of converting to double.  */
    792      1.1  christos       static volatile double dtop, dbot;
    793      1.1  christos       T dtopnv, dbotnv;
    794      1.1  christos 
    795      1.1  christos       dtop = (double) dfrom;
    796      1.1  christos       /* If the rounded top half is Inf, the bottom must be 0 not NaN
    797      1.1  christos 	 or Inf.  */
    798      1.1  christos       if (dtop + dtop == dtop && dtop != 0.0)
    799      1.1  christos 	dbot = 0.0;
    800      1.1  christos       else
    801      1.1  christos 	dbot = (double) (dfrom - (T) dtop);
    802      1.1  christos       dtopnv = dtop;
    803      1.1  christos       dbotnv = dbot;
    804      1.1  christos       to_target (fmt->split_half, &dtopnv, uto);
    805      1.1  christos       to_target (fmt->split_half, &dbotnv,
    806      1.1  christos 		 uto + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2);
    807      1.1  christos       return;
    808      1.1  christos     }
    809      1.1  christos 
    810      1.1  christos   if (dfrom == 0)
    811      1.1  christos     goto finalize_byteorder;	/* Result is zero */
    812      1.1  christos   if (dfrom != dfrom)		/* Result is NaN */
    813      1.1  christos     {
    814      1.1  christos       /* From is NaN */
    815      1.1  christos       put_field (uto, order, fmt->totalsize, fmt->exp_start,
    816      1.1  christos 		 fmt->exp_len, fmt->exp_nan);
    817      1.1  christos       /* Be sure it's not infinity, but NaN value is irrel.  */
    818      1.1  christos       put_field (uto, order, fmt->totalsize, fmt->man_start,
    819      1.1  christos 		 fmt->man_len, 1);
    820      1.1  christos       goto finalize_byteorder;
    821      1.1  christos     }
    822      1.1  christos 
    823      1.1  christos   /* If negative, set the sign bit.  */
    824      1.1  christos   if (dfrom < 0)
    825      1.1  christos     {
    826      1.1  christos       put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1);
    827      1.1  christos       dfrom = -dfrom;
    828      1.1  christos     }
    829      1.1  christos 
    830      1.1  christos   if (dfrom + dfrom == dfrom && dfrom != 0.0)	/* Result is Infinity.  */
    831      1.1  christos     {
    832      1.1  christos       /* Infinity exponent is same as NaN's.  */
    833      1.1  christos       put_field (uto, order, fmt->totalsize, fmt->exp_start,
    834      1.1  christos 		 fmt->exp_len, fmt->exp_nan);
    835      1.1  christos       /* Infinity mantissa is all zeroes.  */
    836      1.1  christos       put_field (uto, order, fmt->totalsize, fmt->man_start,
    837      1.1  christos 		 fmt->man_len, 0);
    838      1.1  christos       goto finalize_byteorder;
    839      1.1  christos     }
    840      1.1  christos 
    841      1.1  christos   mant = frexp (dfrom, &exponent);
    842      1.1  christos 
    843      1.1  christos   if (exponent + fmt->exp_bias <= 0)
    844      1.1  christos     {
    845      1.1  christos       /* The value is too small to be expressed in the destination
    846      1.1  christos 	 type (not enough bits in the exponent.  Treat as 0.  */
    847      1.1  christos       put_field (uto, order, fmt->totalsize, fmt->exp_start,
    848      1.1  christos 		 fmt->exp_len, 0);
    849      1.1  christos       put_field (uto, order, fmt->totalsize, fmt->man_start,
    850      1.1  christos 		 fmt->man_len, 0);
    851      1.1  christos       goto finalize_byteorder;
    852      1.1  christos     }
    853      1.1  christos 
    854      1.1  christos   if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
    855      1.1  christos     {
    856      1.1  christos       /* The value is too large to fit into the destination.
    857      1.1  christos 	 Treat as infinity.  */
    858      1.1  christos       put_field (uto, order, fmt->totalsize, fmt->exp_start,
    859      1.1  christos 		 fmt->exp_len, fmt->exp_nan);
    860      1.1  christos       put_field (uto, order, fmt->totalsize, fmt->man_start,
    861      1.1  christos 		 fmt->man_len, 0);
    862      1.1  christos       goto finalize_byteorder;
    863      1.1  christos     }
    864      1.1  christos 
    865      1.1  christos   put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
    866      1.1  christos 	     exponent + fmt->exp_bias - 1);
    867      1.1  christos 
    868      1.1  christos   mant_bits_left = fmt->man_len;
    869      1.1  christos   mant_off = fmt->man_start;
    870      1.1  christos   while (mant_bits_left > 0)
    871      1.1  christos     {
    872      1.1  christos       unsigned long mant_long;
    873      1.1  christos 
    874      1.1  christos       mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
    875      1.1  christos 
    876      1.1  christos       mant *= 4294967296.0;
    877      1.1  christos       mant_long = ((unsigned long) mant) & 0xffffffffL;
    878      1.1  christos       mant -= mant_long;
    879      1.1  christos 
    880      1.1  christos       /* If the integer bit is implicit, then we need to discard it.
    881      1.1  christos          If we are discarding a zero, we should be (but are not) creating
    882      1.1  christos          a denormalized number which means adjusting the exponent
    883      1.1  christos          (I think).  */
    884      1.1  christos       if (mant_bits_left == fmt->man_len
    885      1.1  christos 	  && fmt->intbit == floatformat_intbit_no)
    886      1.1  christos 	{
    887      1.1  christos 	  mant_long <<= 1;
    888      1.1  christos 	  mant_long &= 0xffffffffL;
    889      1.1  christos           /* If we are processing the top 32 mantissa bits of a doublest
    890      1.1  christos              so as to convert to a float value with implied integer bit,
    891      1.1  christos              we will only be putting 31 of those 32 bits into the
    892      1.1  christos              final value due to the discarding of the top bit.  In the
    893      1.1  christos              case of a small float value where the number of mantissa
    894      1.1  christos              bits is less than 32, discarding the top bit does not alter
    895      1.1  christos              the number of bits we will be adding to the result.  */
    896      1.1  christos           if (mant_bits == 32)
    897      1.1  christos             mant_bits -= 1;
    898      1.1  christos 	}
    899      1.1  christos 
    900      1.1  christos       if (mant_bits < 32)
    901      1.1  christos 	{
    902      1.1  christos 	  /* The bits we want are in the most significant MANT_BITS bits of
    903      1.1  christos 	     mant_long.  Move them to the least significant.  */
    904      1.1  christos 	  mant_long >>= 32 - mant_bits;
    905      1.1  christos 	}
    906      1.1  christos 
    907      1.1  christos       put_field (uto, order, fmt->totalsize,
    908      1.1  christos 		 mant_off, mant_bits, mant_long);
    909      1.1  christos       mant_off += mant_bits;
    910      1.1  christos       mant_bits_left -= mant_bits;
    911      1.1  christos     }
    912      1.1  christos 
    913      1.1  christos  finalize_byteorder:
    914      1.1  christos   /* Do we need to byte-swap the words in the result?  */
    915      1.1  christos   if (order != fmt->byteorder)
    916      1.1  christos     floatformat_normalize_byteorder (fmt, newto, to);
    917      1.1  christos }
    918      1.1  christos 
    919      1.1  christos template<typename T> void
    920      1.1  christos host_float_ops<T>::to_target (const struct type *type,
    921      1.1  christos 			      const T *from, gdb_byte *to) const
    922      1.1  christos {
    923      1.1  christos   /* Ensure possible padding bytes in the target buffer are zeroed out.  */
    924      1.1  christos   memset (to, 0, TYPE_LENGTH (type));
    925      1.1  christos 
    926      1.1  christos   to_target (floatformat_from_type (type), from, to);
    927      1.1  christos }
    928      1.1  christos 
    929      1.1  christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
    930      1.1  christos    to a string, optionally using the print format FORMAT.  */
    931      1.1  christos template<typename T> struct printf_length_modifier
    932      1.1  christos {
    933      1.1  christos   static constexpr char value = 0;
    934      1.1  christos };
    935      1.1  christos template<> struct printf_length_modifier<long double>
    936      1.1  christos {
    937      1.1  christos   static constexpr char value = 'L';
    938      1.1  christos };
    939      1.1  christos template<typename T> std::string
    940      1.1  christos host_float_ops<T>::to_string (const gdb_byte *addr, const struct type *type,
    941      1.1  christos 			      const char *format) const
    942      1.1  christos {
    943      1.1  christos   /* Determine the format string to use on the host side.  */
    944      1.1  christos   constexpr char length = printf_length_modifier<T>::value;
    945      1.1  christos   const struct floatformat *fmt = floatformat_from_type (type);
    946      1.1  christos   std::string host_format = floatformat_printf_format (fmt, format, length);
    947      1.1  christos 
    948      1.1  christos   T host_float;
    949      1.1  christos   from_target (type, addr, &host_float);
    950      1.1  christos 
    951      1.1  christos   DIAGNOSTIC_PUSH
    952      1.1  christos   DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
    953      1.1  christos   return string_printf (host_format.c_str (), host_float);
    954      1.1  christos   DIAGNOSTIC_POP
    955      1.1  christos }
    956      1.1  christos 
    957      1.1  christos /* Parse string IN into a target floating-number of type TYPE and
    958      1.1  christos    store it as byte-stream ADDR.  Return whether parsing succeeded.  */
    959      1.1  christos template<typename T> struct scanf_length_modifier
    960      1.1  christos {
    961      1.1  christos   static constexpr char value = 0;
    962      1.1  christos };
    963      1.1  christos template<> struct scanf_length_modifier<double>
    964      1.1  christos {
    965      1.1  christos   static constexpr char value = 'l';
    966      1.1  christos };
    967      1.1  christos template<> struct scanf_length_modifier<long double>
    968      1.1  christos {
    969      1.1  christos   static constexpr char value = 'L';
    970      1.1  christos };
    971      1.1  christos template<typename T> bool
    972      1.1  christos host_float_ops<T>::from_string (gdb_byte *addr, const struct type *type,
    973      1.1  christos 				const std::string &in) const
    974      1.1  christos {
    975      1.1  christos   T host_float;
    976      1.1  christos   int n, num;
    977      1.1  christos 
    978      1.1  christos   std::string scan_format = "%";
    979      1.1  christos   if (scanf_length_modifier<T>::value)
    980      1.1  christos     scan_format += scanf_length_modifier<T>::value;
    981      1.1  christos   scan_format += "g%n";
    982      1.1  christos 
    983      1.1  christos   DIAGNOSTIC_PUSH
    984      1.1  christos   DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
    985      1.1  christos   num = sscanf (in.c_str (), scan_format.c_str(), &host_float, &n);
    986      1.1  christos   DIAGNOSTIC_POP
    987      1.1  christos 
    988      1.1  christos   /* The sscanf man page suggests not making any assumptions on the effect
    989      1.1  christos      of %n on the result, so we don't.
    990      1.1  christos      That is why we simply test num == 0.  */
    991      1.1  christos   if (num == 0)
    992      1.1  christos     return false;
    993      1.1  christos 
    994      1.1  christos   /* We only accept the whole string.  */
    995      1.1  christos   if (in[n])
    996      1.1  christos     return false;
    997      1.1  christos 
    998      1.1  christos   to_target (type, &host_float, addr);
    999      1.1  christos   return true;
   1000      1.1  christos }
   1001      1.1  christos 
   1002      1.1  christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
   1003      1.1  christos    to an integer value (rounding towards zero).  */
   1004      1.1  christos template<typename T> LONGEST
   1005      1.1  christos host_float_ops<T>::to_longest (const gdb_byte *addr,
   1006      1.1  christos 			       const struct type *type) const
   1007      1.1  christos {
   1008      1.1  christos   T host_float;
   1009      1.1  christos   from_target (type, addr, &host_float);
   1010  1.1.1.2  christos   T min_possible_range = static_cast<T>(std::numeric_limits<LONGEST>::min());
   1011  1.1.1.2  christos   T max_possible_range = -min_possible_range;
   1012  1.1.1.2  christos   /* host_float can be converted to an integer as long as it's in
   1013  1.1.1.2  christos      the range [min_possible_range, max_possible_range). If not, it is either
   1014  1.1.1.2  christos      too large, or too small, or is NaN; in this case return the maximum or
   1015  1.1.1.2  christos      minimum possible value.  */
   1016  1.1.1.2  christos   if (host_float < max_possible_range && host_float >= min_possible_range)
   1017  1.1.1.2  christos     return static_cast<LONGEST> (host_float);
   1018  1.1.1.2  christos   if (host_float < min_possible_range)
   1019      1.1  christos     return std::numeric_limits<LONGEST>::min();
   1020  1.1.1.2  christos   /* This line will be executed if host_float is NaN.  */
   1021  1.1.1.2  christos   return std::numeric_limits<LONGEST>::max();
   1022      1.1  christos }
   1023      1.1  christos 
   1024      1.1  christos /* Convert signed integer VAL to a target floating-number of type TYPE
   1025      1.1  christos    and store it as byte-stream ADDR.  */
   1026      1.1  christos template<typename T> void
   1027      1.1  christos host_float_ops<T>::from_longest (gdb_byte *addr, const struct type *type,
   1028      1.1  christos 				 LONGEST val) const
   1029      1.1  christos {
   1030      1.1  christos   T host_float = (T) val;
   1031      1.1  christos   to_target (type, &host_float, addr);
   1032      1.1  christos }
   1033      1.1  christos 
   1034      1.1  christos /* Convert unsigned integer VAL to a target floating-number of type TYPE
   1035      1.1  christos    and store it as byte-stream ADDR.  */
   1036      1.1  christos template<typename T> void
   1037      1.1  christos host_float_ops<T>::from_ulongest (gdb_byte *addr, const struct type *type,
   1038      1.1  christos 				  ULONGEST val) const
   1039      1.1  christos {
   1040      1.1  christos   T host_float = (T) val;
   1041      1.1  christos   to_target (type, &host_float, addr);
   1042      1.1  christos }
   1043      1.1  christos 
   1044      1.1  christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
   1045      1.1  christos    to a floating-point value in the host "double" format.  */
   1046      1.1  christos template<typename T> double
   1047      1.1  christos host_float_ops<T>::to_host_double (const gdb_byte *addr,
   1048      1.1  christos 				   const struct type *type) const
   1049      1.1  christos {
   1050      1.1  christos   T host_float;
   1051      1.1  christos   from_target (type, addr, &host_float);
   1052      1.1  christos   return (double) host_float;
   1053      1.1  christos }
   1054      1.1  christos 
   1055      1.1  christos /* Convert floating-point value VAL in the host "double" format to a target
   1056      1.1  christos    floating-number of type TYPE and store it as byte-stream ADDR.  */
   1057      1.1  christos template<typename T> void
   1058      1.1  christos host_float_ops<T>::from_host_double (gdb_byte *addr, const struct type *type,
   1059      1.1  christos 				     double val) const
   1060      1.1  christos {
   1061      1.1  christos   T host_float = (T) val;
   1062      1.1  christos   to_target (type, &host_float, addr);
   1063      1.1  christos }
   1064      1.1  christos 
   1065      1.1  christos /* Convert a floating-point number of type FROM_TYPE from the target
   1066      1.1  christos    byte-stream FROM to a floating-point number of type TO_TYPE, and
   1067      1.1  christos    store it to the target byte-stream TO.  */
   1068      1.1  christos template<typename T> void
   1069      1.1  christos host_float_ops<T>::convert (const gdb_byte *from,
   1070      1.1  christos 			    const struct type *from_type,
   1071      1.1  christos 			    gdb_byte *to,
   1072      1.1  christos 			    const struct type *to_type) const
   1073      1.1  christos {
   1074      1.1  christos   T host_float;
   1075      1.1  christos   from_target (from_type, from, &host_float);
   1076      1.1  christos   to_target (to_type, &host_float, to);
   1077      1.1  christos }
   1078      1.1  christos 
   1079      1.1  christos /* Perform the binary operation indicated by OPCODE, using as operands the
   1080      1.1  christos    target byte streams X and Y, interpreted as floating-point numbers of
   1081      1.1  christos    types TYPE_X and TYPE_Y, respectively.  Convert the result to format
   1082      1.1  christos    TYPE_RES and store it into the byte-stream RES.  */
   1083      1.1  christos template<typename T> void
   1084      1.1  christos host_float_ops<T>::binop (enum exp_opcode op,
   1085      1.1  christos 			  const gdb_byte *x, const struct type *type_x,
   1086      1.1  christos 			  const gdb_byte *y, const struct type *type_y,
   1087      1.1  christos 			  gdb_byte *res, const struct type *type_res) const
   1088      1.1  christos {
   1089      1.1  christos   T v1, v2, v = 0;
   1090      1.1  christos 
   1091      1.1  christos   from_target (type_x, x, &v1);
   1092      1.1  christos   from_target (type_y, y, &v2);
   1093      1.1  christos 
   1094      1.1  christos   switch (op)
   1095      1.1  christos     {
   1096      1.1  christos       case BINOP_ADD:
   1097      1.1  christos 	v = v1 + v2;
   1098      1.1  christos 	break;
   1099      1.1  christos 
   1100      1.1  christos       case BINOP_SUB:
   1101      1.1  christos 	v = v1 - v2;
   1102      1.1  christos 	break;
   1103      1.1  christos 
   1104      1.1  christos       case BINOP_MUL:
   1105      1.1  christos 	v = v1 * v2;
   1106      1.1  christos 	break;
   1107      1.1  christos 
   1108      1.1  christos       case BINOP_DIV:
   1109      1.1  christos 	v = v1 / v2;
   1110      1.1  christos 	break;
   1111      1.1  christos 
   1112      1.1  christos       case BINOP_EXP:
   1113      1.1  christos 	errno = 0;
   1114      1.1  christos 	v = pow (v1, v2);
   1115      1.1  christos 	if (errno)
   1116      1.1  christos 	  error (_("Cannot perform exponentiation: %s"),
   1117      1.1  christos 		 safe_strerror (errno));
   1118      1.1  christos 	break;
   1119      1.1  christos 
   1120      1.1  christos       case BINOP_MIN:
   1121      1.1  christos 	v = v1 < v2 ? v1 : v2;
   1122      1.1  christos 	break;
   1123      1.1  christos 
   1124      1.1  christos       case BINOP_MAX:
   1125      1.1  christos 	v = v1 > v2 ? v1 : v2;
   1126      1.1  christos 	break;
   1127      1.1  christos 
   1128      1.1  christos       default:
   1129      1.1  christos 	error (_("Integer-only operation on floating point number."));
   1130      1.1  christos 	break;
   1131      1.1  christos     }
   1132      1.1  christos 
   1133      1.1  christos   to_target (type_res, &v, res);
   1134      1.1  christos }
   1135      1.1  christos 
   1136      1.1  christos /* Compare the two target byte streams X and Y, interpreted as floating-point
   1137      1.1  christos    numbers of types TYPE_X and TYPE_Y, respectively.  Return zero if X and Y
   1138      1.1  christos    are equal, -1 if X is less than Y, and 1 otherwise.  */
   1139      1.1  christos template<typename T> int
   1140      1.1  christos host_float_ops<T>::compare (const gdb_byte *x, const struct type *type_x,
   1141      1.1  christos 			    const gdb_byte *y, const struct type *type_y) const
   1142      1.1  christos {
   1143      1.1  christos   T v1, v2;
   1144      1.1  christos 
   1145      1.1  christos   from_target (type_x, x, &v1);
   1146      1.1  christos   from_target (type_y, y, &v2);
   1147      1.1  christos 
   1148      1.1  christos   if (v1 == v2)
   1149      1.1  christos     return 0;
   1150      1.1  christos   if (v1 < v2)
   1151      1.1  christos     return -1;
   1152      1.1  christos   return 1;
   1153      1.1  christos }
   1154      1.1  christos 
   1155      1.1  christos 
   1156      1.1  christos /* Implementation of target_float_ops using the MPFR library
   1157      1.1  christos    mpfr_t as intermediate type.  */
   1158      1.1  christos 
   1159      1.1  christos #ifdef HAVE_LIBMPFR
   1160      1.1  christos 
   1161      1.1  christos #define MPFR_USE_INTMAX_T
   1162      1.1  christos 
   1163      1.1  christos #include <mpfr.h>
   1164      1.1  christos 
   1165      1.1  christos class mpfr_float_ops : public target_float_ops
   1166      1.1  christos {
   1167      1.1  christos public:
   1168      1.1  christos   std::string to_string (const gdb_byte *addr, const struct type *type,
   1169      1.1  christos 			 const char *format) const override;
   1170      1.1  christos   bool from_string (gdb_byte *addr, const struct type *type,
   1171      1.1  christos 		    const std::string &string) const override;
   1172      1.1  christos 
   1173      1.1  christos   LONGEST to_longest (const gdb_byte *addr,
   1174      1.1  christos 		      const struct type *type) const override;
   1175      1.1  christos   void from_longest (gdb_byte *addr, const struct type *type,
   1176      1.1  christos 		     LONGEST val) const override;
   1177      1.1  christos   void from_ulongest (gdb_byte *addr, const struct type *type,
   1178      1.1  christos 		      ULONGEST val) const override;
   1179      1.1  christos   double to_host_double (const gdb_byte *addr,
   1180      1.1  christos 			 const struct type *type) const override;
   1181      1.1  christos   void from_host_double (gdb_byte *addr, const struct type *type,
   1182      1.1  christos 			 double val) const override;
   1183      1.1  christos   void convert (const gdb_byte *from, const struct type *from_type,
   1184      1.1  christos 		gdb_byte *to, const struct type *to_type) const override;
   1185      1.1  christos 
   1186      1.1  christos   void binop (enum exp_opcode opcode,
   1187      1.1  christos 	      const gdb_byte *x, const struct type *type_x,
   1188      1.1  christos 	      const gdb_byte *y, const struct type *type_y,
   1189      1.1  christos 	      gdb_byte *res, const struct type *type_res) const override;
   1190      1.1  christos   int compare (const gdb_byte *x, const struct type *type_x,
   1191      1.1  christos 	       const gdb_byte *y, const struct type *type_y) const override;
   1192      1.1  christos 
   1193      1.1  christos private:
   1194      1.1  christos   /* Local wrapper class to handle mpfr_t initalization and cleanup.  */
   1195      1.1  christos   class gdb_mpfr
   1196      1.1  christos   {
   1197      1.1  christos   public:
   1198      1.1  christos     mpfr_t val;
   1199      1.1  christos 
   1200      1.1  christos     gdb_mpfr (const struct type *type)
   1201      1.1  christos     {
   1202      1.1  christos       const struct floatformat *fmt = floatformat_from_type (type);
   1203      1.1  christos       mpfr_init2 (val, floatformat_precision (fmt));
   1204      1.1  christos     }
   1205      1.1  christos 
   1206      1.1  christos     gdb_mpfr (const gdb_mpfr &source)
   1207      1.1  christos     {
   1208      1.1  christos       mpfr_init2 (val, mpfr_get_prec (source.val));
   1209      1.1  christos     }
   1210      1.1  christos 
   1211      1.1  christos     ~gdb_mpfr ()
   1212      1.1  christos     {
   1213      1.1  christos       mpfr_clear (val);
   1214      1.1  christos     }
   1215      1.1  christos   };
   1216      1.1  christos 
   1217      1.1  christos   void from_target (const struct floatformat *fmt,
   1218      1.1  christos 		const gdb_byte *from, gdb_mpfr &to) const;
   1219      1.1  christos   void from_target (const struct type *type,
   1220      1.1  christos 		const gdb_byte *from, gdb_mpfr &to) const;
   1221      1.1  christos 
   1222      1.1  christos   void to_target (const struct type *type,
   1223      1.1  christos 		  const gdb_mpfr &from, gdb_byte *to) const;
   1224      1.1  christos   void to_target (const struct floatformat *fmt,
   1225      1.1  christos 		  const gdb_mpfr &from, gdb_byte *to) const;
   1226      1.1  christos };
   1227      1.1  christos 
   1228      1.1  christos 
   1229      1.1  christos /* Convert TO/FROM target floating-point format to mpfr_t.  */
   1230      1.1  christos 
   1231      1.1  christos void
   1232      1.1  christos mpfr_float_ops::from_target (const struct floatformat *fmt,
   1233      1.1  christos 			     const gdb_byte *orig_from, gdb_mpfr &to) const
   1234      1.1  christos {
   1235      1.1  christos   const gdb_byte *from = orig_from;
   1236      1.1  christos   mpfr_exp_t exponent;
   1237      1.1  christos   unsigned long mant;
   1238      1.1  christos   unsigned int mant_bits, mant_off;
   1239      1.1  christos   int mant_bits_left;
   1240      1.1  christos   int special_exponent;		/* It's a NaN, denorm or zero.  */
   1241      1.1  christos   enum floatformat_byteorders order;
   1242      1.1  christos   unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
   1243      1.1  christos   enum float_kind kind;
   1244      1.1  christos 
   1245      1.1  christos   gdb_assert (fmt->totalsize
   1246      1.1  christos 	      <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
   1247      1.1  christos 
   1248      1.1  christos   /* Handle non-numbers.  */
   1249      1.1  christos   kind = floatformat_classify (fmt, from);
   1250      1.1  christos   if (kind == float_infinite)
   1251      1.1  christos     {
   1252      1.1  christos       mpfr_set_inf (to.val, floatformat_is_negative (fmt, from) ? -1 : 1);
   1253      1.1  christos       return;
   1254      1.1  christos     }
   1255      1.1  christos   if (kind == float_nan)
   1256      1.1  christos     {
   1257      1.1  christos       mpfr_set_nan (to.val);
   1258      1.1  christos       return;
   1259      1.1  christos     }
   1260      1.1  christos 
   1261      1.1  christos   order = floatformat_normalize_byteorder (fmt, from, newfrom);
   1262      1.1  christos 
   1263      1.1  christos   if (order != fmt->byteorder)
   1264      1.1  christos     from = newfrom;
   1265      1.1  christos 
   1266      1.1  christos   if (fmt->split_half)
   1267      1.1  christos     {
   1268      1.1  christos       gdb_mpfr top (to), bot (to);
   1269      1.1  christos 
   1270      1.1  christos       from_target (fmt->split_half, from, top);
   1271      1.1  christos       /* Preserve the sign of 0, which is the sign of the top half.  */
   1272      1.1  christos       if (mpfr_zero_p (top.val))
   1273      1.1  christos 	{
   1274      1.1  christos 	  mpfr_set (to.val, top.val, MPFR_RNDN);
   1275      1.1  christos 	  return;
   1276      1.1  christos 	}
   1277      1.1  christos       from_target (fmt->split_half,
   1278      1.1  christos 	       from + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, bot);
   1279      1.1  christos       mpfr_add (to.val, top.val, bot.val, MPFR_RNDN);
   1280      1.1  christos       return;
   1281      1.1  christos     }
   1282      1.1  christos 
   1283      1.1  christos   exponent = get_field (from, order, fmt->totalsize, fmt->exp_start,
   1284      1.1  christos 			fmt->exp_len);
   1285      1.1  christos   /* Note that if exponent indicates a NaN, we can't really do anything useful
   1286      1.1  christos      (not knowing if the host has NaN's, or how to build one).  So it will
   1287      1.1  christos      end up as an infinity or something close; that is OK.  */
   1288      1.1  christos 
   1289      1.1  christos   mant_bits_left = fmt->man_len;
   1290      1.1  christos   mant_off = fmt->man_start;
   1291      1.1  christos   mpfr_set_zero (to.val, 0);
   1292      1.1  christos 
   1293      1.1  christos   special_exponent = exponent == 0 || exponent == fmt->exp_nan;
   1294      1.1  christos 
   1295      1.1  christos   /* Don't bias NaNs.  Use minimum exponent for denorms.  For
   1296      1.1  christos      simplicity, we don't check for zero as the exponent doesn't matter.
   1297      1.1  christos      Note the cast to int; exp_bias is unsigned, so it's important to
   1298      1.1  christos      make sure the operation is done in signed arithmetic.  */
   1299      1.1  christos   if (!special_exponent)
   1300      1.1  christos     exponent -= fmt->exp_bias;
   1301      1.1  christos   else if (exponent == 0)
   1302      1.1  christos     exponent = 1 - fmt->exp_bias;
   1303      1.1  christos 
   1304      1.1  christos   /* Build the result algebraically.  Might go infinite, underflow, etc;
   1305      1.1  christos      who cares.  */
   1306      1.1  christos 
   1307      1.1  christos   /* If this format uses a hidden bit, explicitly add it in now.  Otherwise,
   1308      1.1  christos      increment the exponent by one to account for the integer bit.  */
   1309      1.1  christos 
   1310      1.1  christos   if (!special_exponent)
   1311      1.1  christos     {
   1312      1.1  christos       if (fmt->intbit == floatformat_intbit_no)
   1313      1.1  christos 	mpfr_set_ui_2exp (to.val, 1, exponent, MPFR_RNDN);
   1314      1.1  christos       else
   1315      1.1  christos 	exponent++;
   1316      1.1  christos     }
   1317      1.1  christos 
   1318      1.1  christos   gdb_mpfr tmp (to);
   1319      1.1  christos 
   1320      1.1  christos   while (mant_bits_left > 0)
   1321      1.1  christos     {
   1322      1.1  christos       mant_bits = std::min (mant_bits_left, 32);
   1323      1.1  christos 
   1324      1.1  christos       mant = get_field (from, order, fmt->totalsize, mant_off, mant_bits);
   1325      1.1  christos 
   1326      1.1  christos       mpfr_set_ui (tmp.val, mant, MPFR_RNDN);
   1327      1.1  christos       mpfr_mul_2si (tmp.val, tmp.val, exponent - mant_bits, MPFR_RNDN);
   1328      1.1  christos       mpfr_add (to.val, to.val, tmp.val, MPFR_RNDN);
   1329      1.1  christos       exponent -= mant_bits;
   1330      1.1  christos       mant_off += mant_bits;
   1331      1.1  christos       mant_bits_left -= mant_bits;
   1332      1.1  christos     }
   1333      1.1  christos 
   1334      1.1  christos   /* Negate it if negative.  */
   1335      1.1  christos   if (get_field (from, order, fmt->totalsize, fmt->sign_start, 1))
   1336      1.1  christos     mpfr_neg (to.val, to.val, MPFR_RNDN);
   1337      1.1  christos }
   1338      1.1  christos 
   1339      1.1  christos void
   1340      1.1  christos mpfr_float_ops::from_target (const struct type *type,
   1341      1.1  christos 			     const gdb_byte *from, gdb_mpfr &to) const
   1342      1.1  christos {
   1343      1.1  christos   from_target (floatformat_from_type (type), from, to);
   1344      1.1  christos }
   1345      1.1  christos 
   1346      1.1  christos void
   1347      1.1  christos mpfr_float_ops::to_target (const struct floatformat *fmt,
   1348      1.1  christos 			   const gdb_mpfr &from, gdb_byte *orig_to) const
   1349      1.1  christos {
   1350      1.1  christos   unsigned char *to = orig_to;
   1351      1.1  christos   mpfr_exp_t exponent;
   1352      1.1  christos   unsigned int mant_bits, mant_off;
   1353      1.1  christos   int mant_bits_left;
   1354      1.1  christos   enum floatformat_byteorders order = fmt->byteorder;
   1355      1.1  christos   unsigned char newto[FLOATFORMAT_LARGEST_BYTES];
   1356      1.1  christos 
   1357      1.1  christos   if (order != floatformat_little)
   1358      1.1  christos     order = floatformat_big;
   1359      1.1  christos 
   1360      1.1  christos   if (order != fmt->byteorder)
   1361      1.1  christos     to = newto;
   1362      1.1  christos 
   1363      1.1  christos   memset (to, 0, floatformat_totalsize_bytes (fmt));
   1364      1.1  christos 
   1365      1.1  christos   if (fmt->split_half)
   1366      1.1  christos     {
   1367      1.1  christos       gdb_mpfr top (from), bot (from);
   1368      1.1  christos 
   1369      1.1  christos       mpfr_set (top.val, from.val, MPFR_RNDN);
   1370      1.1  christos       /* If the rounded top half is Inf, the bottom must be 0 not NaN
   1371      1.1  christos 	 or Inf.  */
   1372      1.1  christos       if (mpfr_inf_p (top.val))
   1373      1.1  christos 	mpfr_set_zero (bot.val, 0);
   1374      1.1  christos       else
   1375      1.1  christos 	mpfr_sub (bot.val, from.val, top.val, MPFR_RNDN);
   1376      1.1  christos 
   1377      1.1  christos       to_target (fmt->split_half, top, to);
   1378      1.1  christos       to_target (fmt->split_half, bot,
   1379      1.1  christos 		 to + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2);
   1380      1.1  christos       return;
   1381      1.1  christos     }
   1382      1.1  christos 
   1383      1.1  christos   gdb_mpfr tmp (from);
   1384      1.1  christos 
   1385      1.1  christos   if (mpfr_zero_p (from.val))
   1386      1.1  christos     goto finalize_byteorder;	/* Result is zero */
   1387      1.1  christos 
   1388      1.1  christos   mpfr_set (tmp.val, from.val, MPFR_RNDN);
   1389      1.1  christos 
   1390      1.1  christos   if (mpfr_nan_p (tmp.val))	/* Result is NaN */
   1391      1.1  christos     {
   1392      1.1  christos       /* From is NaN */
   1393      1.1  christos       put_field (to, order, fmt->totalsize, fmt->exp_start,
   1394      1.1  christos 		 fmt->exp_len, fmt->exp_nan);
   1395      1.1  christos       /* Be sure it's not infinity, but NaN value is irrel.  */
   1396      1.1  christos       put_field (to, order, fmt->totalsize, fmt->man_start,
   1397      1.1  christos 		 fmt->man_len, 1);
   1398      1.1  christos       goto finalize_byteorder;
   1399      1.1  christos     }
   1400      1.1  christos 
   1401      1.1  christos   /* If negative, set the sign bit.  */
   1402      1.1  christos   if (mpfr_sgn (tmp.val) < 0)
   1403      1.1  christos     {
   1404      1.1  christos       put_field (to, order, fmt->totalsize, fmt->sign_start, 1, 1);
   1405      1.1  christos       mpfr_neg (tmp.val, tmp.val, MPFR_RNDN);
   1406      1.1  christos     }
   1407      1.1  christos 
   1408      1.1  christos   if (mpfr_inf_p (tmp.val))		/* Result is Infinity.  */
   1409      1.1  christos     {
   1410      1.1  christos       /* Infinity exponent is same as NaN's.  */
   1411      1.1  christos       put_field (to, order, fmt->totalsize, fmt->exp_start,
   1412      1.1  christos 		 fmt->exp_len, fmt->exp_nan);
   1413      1.1  christos       /* Infinity mantissa is all zeroes.  */
   1414      1.1  christos       put_field (to, order, fmt->totalsize, fmt->man_start,
   1415      1.1  christos 		 fmt->man_len, 0);
   1416      1.1  christos       goto finalize_byteorder;
   1417      1.1  christos     }
   1418      1.1  christos 
   1419      1.1  christos   mpfr_frexp (&exponent, tmp.val, tmp.val, MPFR_RNDN);
   1420      1.1  christos 
   1421      1.1  christos   if (exponent + fmt->exp_bias <= 0)
   1422      1.1  christos     {
   1423      1.1  christos       /* The value is too small to be expressed in the destination
   1424      1.1  christos 	 type (not enough bits in the exponent.  Treat as 0.  */
   1425      1.1  christos       put_field (to, order, fmt->totalsize, fmt->exp_start,
   1426      1.1  christos 		 fmt->exp_len, 0);
   1427      1.1  christos       put_field (to, order, fmt->totalsize, fmt->man_start,
   1428      1.1  christos 		 fmt->man_len, 0);
   1429      1.1  christos       goto finalize_byteorder;
   1430      1.1  christos     }
   1431      1.1  christos 
   1432      1.1  christos   if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
   1433      1.1  christos     {
   1434      1.1  christos       /* The value is too large to fit into the destination.
   1435      1.1  christos 	 Treat as infinity.  */
   1436      1.1  christos       put_field (to, order, fmt->totalsize, fmt->exp_start,
   1437      1.1  christos 		 fmt->exp_len, fmt->exp_nan);
   1438      1.1  christos       put_field (to, order, fmt->totalsize, fmt->man_start,
   1439      1.1  christos 		 fmt->man_len, 0);
   1440      1.1  christos       goto finalize_byteorder;
   1441      1.1  christos     }
   1442      1.1  christos 
   1443      1.1  christos   put_field (to, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
   1444      1.1  christos 	     exponent + fmt->exp_bias - 1);
   1445      1.1  christos 
   1446      1.1  christos   mant_bits_left = fmt->man_len;
   1447      1.1  christos   mant_off = fmt->man_start;
   1448      1.1  christos   while (mant_bits_left > 0)
   1449      1.1  christos     {
   1450      1.1  christos       unsigned long mant_long;
   1451      1.1  christos 
   1452      1.1  christos       mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
   1453      1.1  christos 
   1454      1.1  christos       mpfr_mul_2ui (tmp.val, tmp.val, 32, MPFR_RNDN);
   1455      1.1  christos       mant_long = mpfr_get_ui (tmp.val, MPFR_RNDZ) & 0xffffffffL;
   1456      1.1  christos       mpfr_sub_ui (tmp.val, tmp.val, mant_long, MPFR_RNDZ);
   1457      1.1  christos 
   1458      1.1  christos       /* If the integer bit is implicit, then we need to discard it.
   1459      1.1  christos          If we are discarding a zero, we should be (but are not) creating
   1460      1.1  christos          a denormalized number which means adjusting the exponent
   1461      1.1  christos          (I think).  */
   1462      1.1  christos       if (mant_bits_left == fmt->man_len
   1463      1.1  christos 	  && fmt->intbit == floatformat_intbit_no)
   1464      1.1  christos 	{
   1465      1.1  christos 	  mant_long <<= 1;
   1466      1.1  christos 	  mant_long &= 0xffffffffL;
   1467      1.1  christos           /* If we are processing the top 32 mantissa bits of a doublest
   1468      1.1  christos              so as to convert to a float value with implied integer bit,
   1469      1.1  christos              we will only be putting 31 of those 32 bits into the
   1470      1.1  christos              final value due to the discarding of the top bit.  In the
   1471      1.1  christos              case of a small float value where the number of mantissa
   1472      1.1  christos              bits is less than 32, discarding the top bit does not alter
   1473      1.1  christos              the number of bits we will be adding to the result.  */
   1474      1.1  christos           if (mant_bits == 32)
   1475      1.1  christos             mant_bits -= 1;
   1476      1.1  christos 	}
   1477      1.1  christos 
   1478      1.1  christos       if (mant_bits < 32)
   1479      1.1  christos 	{
   1480      1.1  christos 	  /* The bits we want are in the most significant MANT_BITS bits of
   1481      1.1  christos 	     mant_long.  Move them to the least significant.  */
   1482      1.1  christos 	  mant_long >>= 32 - mant_bits;
   1483      1.1  christos 	}
   1484      1.1  christos 
   1485      1.1  christos       put_field (to, order, fmt->totalsize,
   1486      1.1  christos 		 mant_off, mant_bits, mant_long);
   1487      1.1  christos       mant_off += mant_bits;
   1488      1.1  christos       mant_bits_left -= mant_bits;
   1489      1.1  christos     }
   1490      1.1  christos 
   1491      1.1  christos  finalize_byteorder:
   1492      1.1  christos   /* Do we need to byte-swap the words in the result?  */
   1493      1.1  christos   if (order != fmt->byteorder)
   1494      1.1  christos     floatformat_normalize_byteorder (fmt, newto, orig_to);
   1495      1.1  christos }
   1496      1.1  christos 
   1497      1.1  christos void
   1498      1.1  christos mpfr_float_ops::to_target (const struct type *type,
   1499      1.1  christos 			   const gdb_mpfr &from, gdb_byte *to) const
   1500      1.1  christos {
   1501      1.1  christos   /* Ensure possible padding bytes in the target buffer are zeroed out.  */
   1502      1.1  christos   memset (to, 0, TYPE_LENGTH (type));
   1503      1.1  christos 
   1504      1.1  christos   to_target (floatformat_from_type (type), from, to);
   1505      1.1  christos }
   1506      1.1  christos 
   1507      1.1  christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
   1508      1.1  christos    to a string, optionally using the print format FORMAT.  */
   1509      1.1  christos std::string
   1510      1.1  christos mpfr_float_ops::to_string (const gdb_byte *addr,
   1511      1.1  christos 			   const struct type *type,
   1512      1.1  christos 			   const char *format) const
   1513      1.1  christos {
   1514      1.1  christos   const struct floatformat *fmt = floatformat_from_type (type);
   1515      1.1  christos 
   1516      1.1  christos   /* Unless we need to adhere to a specific format, provide special
   1517      1.1  christos      output for certain cases.  */
   1518      1.1  christos   if (format == nullptr)
   1519      1.1  christos     {
   1520      1.1  christos       /* Detect invalid representations.  */
   1521      1.1  christos       if (!floatformat_is_valid (fmt, addr))
   1522      1.1  christos 	return "<invalid float value>";
   1523      1.1  christos 
   1524      1.1  christos       /* Handle NaN and Inf.  */
   1525      1.1  christos       enum float_kind kind = floatformat_classify (fmt, addr);
   1526      1.1  christos       if (kind == float_nan)
   1527      1.1  christos 	{
   1528      1.1  christos 	  const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
   1529      1.1  christos 	  const char *mantissa = floatformat_mantissa (fmt, addr);
   1530      1.1  christos 	  return string_printf ("%snan(0x%s)", sign, mantissa);
   1531      1.1  christos 	}
   1532      1.1  christos       else if (kind == float_infinite)
   1533      1.1  christos 	{
   1534      1.1  christos 	  const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
   1535      1.1  christos 	  return string_printf ("%sinf", sign);
   1536      1.1  christos 	}
   1537      1.1  christos     }
   1538      1.1  christos 
   1539      1.1  christos   /* Determine the format string to use on the host side.  */
   1540      1.1  christos   std::string host_format = floatformat_printf_format (fmt, format, 'R');
   1541      1.1  christos 
   1542      1.1  christos   gdb_mpfr tmp (type);
   1543      1.1  christos   from_target (type, addr, tmp);
   1544      1.1  christos 
   1545      1.1  christos   int size = mpfr_snprintf (NULL, 0, host_format.c_str (), tmp.val);
   1546      1.1  christos   std::string str (size, '\0');
   1547      1.1  christos   mpfr_sprintf (&str[0], host_format.c_str (), tmp.val);
   1548      1.1  christos 
   1549      1.1  christos   return str;
   1550      1.1  christos }
   1551      1.1  christos 
   1552      1.1  christos /* Parse string STRING into a target floating-number of type TYPE and
   1553      1.1  christos    store it as byte-stream ADDR.  Return whether parsing succeeded.  */
   1554      1.1  christos bool
   1555      1.1  christos mpfr_float_ops::from_string (gdb_byte *addr,
   1556      1.1  christos 			     const struct type *type,
   1557      1.1  christos 			     const std::string &in) const
   1558      1.1  christos {
   1559      1.1  christos   gdb_mpfr tmp (type);
   1560      1.1  christos 
   1561      1.1  christos   char *endptr;
   1562      1.1  christos   mpfr_strtofr (tmp.val, in.c_str (), &endptr, 0, MPFR_RNDN);
   1563      1.1  christos 
   1564      1.1  christos   /* We only accept the whole string.  */
   1565      1.1  christos   if (*endptr)
   1566      1.1  christos     return false;
   1567      1.1  christos 
   1568      1.1  christos   to_target (type, tmp, addr);
   1569      1.1  christos   return true;
   1570      1.1  christos }
   1571      1.1  christos 
   1572      1.1  christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
   1573      1.1  christos    to an integer value (rounding towards zero).  */
   1574      1.1  christos LONGEST
   1575      1.1  christos mpfr_float_ops::to_longest (const gdb_byte *addr,
   1576      1.1  christos 			    const struct type *type) const
   1577      1.1  christos {
   1578      1.1  christos   gdb_mpfr tmp (type);
   1579      1.1  christos   from_target (type, addr, tmp);
   1580      1.1  christos   return mpfr_get_sj (tmp.val, MPFR_RNDZ);
   1581      1.1  christos }
   1582      1.1  christos 
   1583      1.1  christos /* Convert signed integer VAL to a target floating-number of type TYPE
   1584      1.1  christos    and store it as byte-stream ADDR.  */
   1585      1.1  christos void
   1586      1.1  christos mpfr_float_ops::from_longest (gdb_byte *addr,
   1587      1.1  christos 			      const struct type *type,
   1588      1.1  christos 			      LONGEST val) const
   1589      1.1  christos {
   1590      1.1  christos   gdb_mpfr tmp (type);
   1591      1.1  christos   mpfr_set_sj (tmp.val, val, MPFR_RNDN);
   1592      1.1  christos   to_target (type, tmp, addr);
   1593      1.1  christos }
   1594      1.1  christos 
   1595      1.1  christos /* Convert unsigned integer VAL to a target floating-number of type TYPE
   1596      1.1  christos    and store it as byte-stream ADDR.  */
   1597      1.1  christos void
   1598      1.1  christos mpfr_float_ops::from_ulongest (gdb_byte *addr,
   1599      1.1  christos 			       const struct type *type,
   1600      1.1  christos 			       ULONGEST val) const
   1601      1.1  christos {
   1602      1.1  christos   gdb_mpfr tmp (type);
   1603      1.1  christos   mpfr_set_uj (tmp.val, val, MPFR_RNDN);
   1604      1.1  christos   to_target (type, tmp, addr);
   1605      1.1  christos }
   1606      1.1  christos 
   1607      1.1  christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
   1608      1.1  christos    to a floating-point value in the host "double" format.  */
   1609      1.1  christos double
   1610      1.1  christos mpfr_float_ops::to_host_double (const gdb_byte *addr,
   1611      1.1  christos 				const struct type *type) const
   1612      1.1  christos {
   1613      1.1  christos   gdb_mpfr tmp (type);
   1614      1.1  christos   from_target (type, addr, tmp);
   1615      1.1  christos   return mpfr_get_d (tmp.val, MPFR_RNDN);
   1616      1.1  christos }
   1617      1.1  christos 
   1618      1.1  christos /* Convert floating-point value VAL in the host "double" format to a target
   1619      1.1  christos    floating-number of type TYPE and store it as byte-stream ADDR.  */
   1620      1.1  christos void
   1621      1.1  christos mpfr_float_ops::from_host_double (gdb_byte *addr,
   1622      1.1  christos 				  const struct type *type,
   1623      1.1  christos 				  double val) const
   1624      1.1  christos {
   1625      1.1  christos   gdb_mpfr tmp (type);
   1626      1.1  christos   mpfr_set_d (tmp.val, val, MPFR_RNDN);
   1627      1.1  christos   to_target (type, tmp, addr);
   1628      1.1  christos }
   1629      1.1  christos 
   1630      1.1  christos /* Convert a floating-point number of type FROM_TYPE from the target
   1631      1.1  christos    byte-stream FROM to a floating-point number of type TO_TYPE, and
   1632      1.1  christos    store it to the target byte-stream TO.  */
   1633      1.1  christos void
   1634      1.1  christos mpfr_float_ops::convert (const gdb_byte *from,
   1635      1.1  christos 			 const struct type *from_type,
   1636      1.1  christos 			 gdb_byte *to,
   1637      1.1  christos 			 const struct type *to_type) const
   1638      1.1  christos {
   1639      1.1  christos   gdb_mpfr from_tmp (from_type), to_tmp (to_type);
   1640      1.1  christos   from_target (from_type, from, from_tmp);
   1641      1.1  christos   mpfr_set (to_tmp.val, from_tmp.val, MPFR_RNDN);
   1642      1.1  christos   to_target (to_type, to_tmp, to);
   1643      1.1  christos }
   1644      1.1  christos 
   1645      1.1  christos /* Perform the binary operation indicated by OPCODE, using as operands the
   1646      1.1  christos    target byte streams X and Y, interpreted as floating-point numbers of
   1647      1.1  christos    types TYPE_X and TYPE_Y, respectively.  Convert the result to type
   1648      1.1  christos    TYPE_RES and store it into the byte-stream RES.  */
   1649      1.1  christos void
   1650      1.1  christos mpfr_float_ops::binop (enum exp_opcode op,
   1651      1.1  christos 		       const gdb_byte *x, const struct type *type_x,
   1652      1.1  christos 		       const gdb_byte *y, const struct type *type_y,
   1653      1.1  christos 		       gdb_byte *res, const struct type *type_res) const
   1654      1.1  christos {
   1655      1.1  christos   gdb_mpfr x_tmp (type_x), y_tmp (type_y), tmp (type_res);
   1656      1.1  christos 
   1657      1.1  christos   from_target (type_x, x, x_tmp);
   1658      1.1  christos   from_target (type_y, y, y_tmp);
   1659      1.1  christos 
   1660      1.1  christos   switch (op)
   1661      1.1  christos     {
   1662      1.1  christos       case BINOP_ADD:
   1663      1.1  christos 	mpfr_add (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
   1664      1.1  christos 	break;
   1665      1.1  christos 
   1666      1.1  christos       case BINOP_SUB:
   1667      1.1  christos 	mpfr_sub (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
   1668      1.1  christos 	break;
   1669      1.1  christos 
   1670      1.1  christos       case BINOP_MUL:
   1671      1.1  christos 	mpfr_mul (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
   1672      1.1  christos 	break;
   1673      1.1  christos 
   1674      1.1  christos       case BINOP_DIV:
   1675      1.1  christos 	mpfr_div (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
   1676      1.1  christos 	break;
   1677      1.1  christos 
   1678      1.1  christos       case BINOP_EXP:
   1679      1.1  christos 	mpfr_pow (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
   1680      1.1  christos 	break;
   1681      1.1  christos 
   1682      1.1  christos       case BINOP_MIN:
   1683      1.1  christos 	mpfr_min (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
   1684      1.1  christos 	break;
   1685      1.1  christos 
   1686      1.1  christos       case BINOP_MAX:
   1687      1.1  christos 	mpfr_max (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
   1688      1.1  christos 	break;
   1689      1.1  christos 
   1690      1.1  christos       default:
   1691      1.1  christos 	error (_("Integer-only operation on floating point number."));
   1692      1.1  christos 	break;
   1693      1.1  christos     }
   1694      1.1  christos 
   1695      1.1  christos   to_target (type_res, tmp, res);
   1696      1.1  christos }
   1697      1.1  christos 
   1698      1.1  christos /* Compare the two target byte streams X and Y, interpreted as floating-point
   1699      1.1  christos    numbers of types TYPE_X and TYPE_Y, respectively.  Return zero if X and Y
   1700      1.1  christos    are equal, -1 if X is less than Y, and 1 otherwise.  */
   1701      1.1  christos int
   1702      1.1  christos mpfr_float_ops::compare (const gdb_byte *x, const struct type *type_x,
   1703      1.1  christos 			 const gdb_byte *y, const struct type *type_y) const
   1704      1.1  christos {
   1705      1.1  christos   gdb_mpfr x_tmp (type_x), y_tmp (type_y);
   1706      1.1  christos 
   1707      1.1  christos   from_target (type_x, x, x_tmp);
   1708      1.1  christos   from_target (type_y, y, y_tmp);
   1709      1.1  christos 
   1710      1.1  christos   if (mpfr_equal_p (x_tmp.val, y_tmp.val))
   1711      1.1  christos     return 0;
   1712      1.1  christos   else if (mpfr_less_p (x_tmp.val, y_tmp.val))
   1713      1.1  christos     return -1;
   1714      1.1  christos   else
   1715      1.1  christos     return 1;
   1716      1.1  christos }
   1717      1.1  christos 
   1718      1.1  christos #endif
   1719      1.1  christos 
   1720      1.1  christos 
   1721      1.1  christos /* Helper routines operating on decimal floating-point data.  */
   1722      1.1  christos 
   1723      1.1  christos /* Decimal floating point is one of the extension to IEEE 754, which is
   1724      1.1  christos    described in http://grouper.ieee.org/groups/754/revision.html and
   1725      1.1  christos    http://www2.hursley.ibm.com/decimal/.  It completes binary floating
   1726      1.1  christos    point by representing floating point more exactly.  */
   1727      1.1  christos 
   1728      1.1  christos /* The order of the following headers is important for making sure
   1729      1.1  christos    decNumber structure is large enough to hold decimal128 digits.  */
   1730      1.1  christos 
   1731      1.1  christos #include "dpd/decimal128.h"
   1732      1.1  christos #include "dpd/decimal64.h"
   1733      1.1  christos #include "dpd/decimal32.h"
   1734      1.1  christos 
   1735      1.1  christos /* When using decimal128, this is the maximum string length + 1
   1736      1.1  christos    (value comes from libdecnumber's DECIMAL128_String constant).  */
   1737      1.1  christos #define MAX_DECIMAL_STRING  43
   1738      1.1  christos 
   1739      1.1  christos /* In GDB, we are using an array of gdb_byte to represent decimal values.
   1740      1.1  christos    They are stored in host byte order.  This routine does the conversion if
   1741      1.1  christos    the target byte order is different.  */
   1742      1.1  christos static void
   1743      1.1  christos match_endianness (const gdb_byte *from, const struct type *type, gdb_byte *to)
   1744      1.1  christos {
   1745  1.1.1.2  christos   gdb_assert (type->code () == TYPE_CODE_DECFLOAT);
   1746      1.1  christos 
   1747      1.1  christos   int len = TYPE_LENGTH (type);
   1748      1.1  christos   int i;
   1749      1.1  christos 
   1750      1.1  christos #if WORDS_BIGENDIAN
   1751      1.1  christos #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE
   1752      1.1  christos #else
   1753      1.1  christos #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG
   1754      1.1  christos #endif
   1755      1.1  christos 
   1756  1.1.1.2  christos   if (type_byte_order (type) == OPPOSITE_BYTE_ORDER)
   1757      1.1  christos     for (i = 0; i < len; i++)
   1758      1.1  christos       to[i] = from[len - i - 1];
   1759      1.1  christos   else
   1760      1.1  christos     for (i = 0; i < len; i++)
   1761      1.1  christos       to[i] = from[i];
   1762      1.1  christos 
   1763      1.1  christos   return;
   1764      1.1  christos }
   1765      1.1  christos 
   1766      1.1  christos /* Helper function to get the appropriate libdecnumber context for each size
   1767      1.1  christos    of decimal float.  */
   1768      1.1  christos static void
   1769      1.1  christos set_decnumber_context (decContext *ctx, const struct type *type)
   1770      1.1  christos {
   1771  1.1.1.2  christos   gdb_assert (type->code () == TYPE_CODE_DECFLOAT);
   1772      1.1  christos 
   1773      1.1  christos   switch (TYPE_LENGTH (type))
   1774      1.1  christos     {
   1775      1.1  christos       case 4:
   1776      1.1  christos 	decContextDefault (ctx, DEC_INIT_DECIMAL32);
   1777      1.1  christos 	break;
   1778      1.1  christos       case 8:
   1779      1.1  christos 	decContextDefault (ctx, DEC_INIT_DECIMAL64);
   1780      1.1  christos 	break;
   1781      1.1  christos       case 16:
   1782      1.1  christos 	decContextDefault (ctx, DEC_INIT_DECIMAL128);
   1783      1.1  christos 	break;
   1784      1.1  christos     }
   1785      1.1  christos 
   1786      1.1  christos   ctx->traps = 0;
   1787      1.1  christos }
   1788      1.1  christos 
   1789      1.1  christos /* Check for errors signaled in the decimal context structure.  */
   1790      1.1  christos static void
   1791      1.1  christos decimal_check_errors (decContext *ctx)
   1792      1.1  christos {
   1793      1.1  christos   /* An error here could be a division by zero, an overflow, an underflow or
   1794      1.1  christos      an invalid operation (from the DEC_Errors constant in decContext.h).
   1795      1.1  christos      Since GDB doesn't complain about division by zero, overflow or underflow
   1796      1.1  christos      errors for binary floating, we won't complain about them for decimal
   1797      1.1  christos      floating either.  */
   1798      1.1  christos   if (ctx->status & DEC_IEEE_854_Invalid_operation)
   1799      1.1  christos     {
   1800      1.1  christos       /* Leave only the error bits in the status flags.  */
   1801      1.1  christos       ctx->status &= DEC_IEEE_854_Invalid_operation;
   1802      1.1  christos       error (_("Cannot perform operation: %s"),
   1803      1.1  christos 	     decContextStatusToString (ctx));
   1804      1.1  christos     }
   1805      1.1  christos }
   1806      1.1  christos 
   1807      1.1  christos /* Helper function to convert from libdecnumber's appropriate representation
   1808      1.1  christos    for computation to each size of decimal float.  */
   1809      1.1  christos static void
   1810      1.1  christos decimal_from_number (const decNumber *from,
   1811      1.1  christos                      gdb_byte *to, const struct type *type)
   1812      1.1  christos {
   1813      1.1  christos   gdb_byte dec[16];
   1814      1.1  christos 
   1815      1.1  christos   decContext set;
   1816      1.1  christos 
   1817      1.1  christos   set_decnumber_context (&set, type);
   1818      1.1  christos 
   1819      1.1  christos   switch (TYPE_LENGTH (type))
   1820      1.1  christos     {
   1821      1.1  christos       case 4:
   1822      1.1  christos 	decimal32FromNumber ((decimal32 *) dec, from, &set);
   1823      1.1  christos 	break;
   1824      1.1  christos       case 8:
   1825      1.1  christos 	decimal64FromNumber ((decimal64 *) dec, from, &set);
   1826      1.1  christos 	break;
   1827      1.1  christos       case 16:
   1828      1.1  christos 	decimal128FromNumber ((decimal128 *) dec, from, &set);
   1829      1.1  christos 	break;
   1830      1.1  christos       default:
   1831      1.1  christos 	error (_("Unknown decimal floating point type."));
   1832      1.1  christos 	break;
   1833      1.1  christos     }
   1834      1.1  christos 
   1835      1.1  christos   match_endianness (dec, type, to);
   1836      1.1  christos }
   1837      1.1  christos 
   1838      1.1  christos /* Helper function to convert each size of decimal float to libdecnumber's
   1839      1.1  christos    appropriate representation for computation.  */
   1840      1.1  christos static void
   1841      1.1  christos decimal_to_number (const gdb_byte *addr, const struct type *type,
   1842      1.1  christos                    decNumber *to)
   1843      1.1  christos {
   1844      1.1  christos   gdb_byte dec[16];
   1845      1.1  christos   match_endianness (addr, type, dec);
   1846      1.1  christos 
   1847      1.1  christos   switch (TYPE_LENGTH (type))
   1848      1.1  christos     {
   1849      1.1  christos       case 4:
   1850      1.1  christos 	decimal32ToNumber ((decimal32 *) dec, to);
   1851      1.1  christos 	break;
   1852      1.1  christos       case 8:
   1853      1.1  christos 	decimal64ToNumber ((decimal64 *) dec, to);
   1854      1.1  christos 	break;
   1855      1.1  christos       case 16:
   1856      1.1  christos 	decimal128ToNumber ((decimal128 *) dec, to);
   1857      1.1  christos 	break;
   1858      1.1  christos       default:
   1859      1.1  christos 	error (_("Unknown decimal floating point type."));
   1860      1.1  christos 	break;
   1861      1.1  christos     }
   1862      1.1  christos }
   1863      1.1  christos 
   1864      1.1  christos /* Returns true if ADDR (which is of type TYPE) is the number zero.  */
   1865      1.1  christos static bool
   1866      1.1  christos decimal_is_zero (const gdb_byte *addr, const struct type *type)
   1867      1.1  christos {
   1868      1.1  christos   decNumber number;
   1869      1.1  christos 
   1870      1.1  christos   decimal_to_number (addr, type, &number);
   1871      1.1  christos 
   1872      1.1  christos   return decNumberIsZero (&number);
   1873      1.1  christos }
   1874      1.1  christos 
   1875      1.1  christos 
   1876      1.1  christos /* Implementation of target_float_ops using the libdecnumber decNumber type
   1877      1.1  christos    as intermediate format.  */
   1878      1.1  christos 
   1879      1.1  christos class decimal_float_ops : public target_float_ops
   1880      1.1  christos {
   1881      1.1  christos public:
   1882      1.1  christos   std::string to_string (const gdb_byte *addr, const struct type *type,
   1883      1.1  christos 			 const char *format) const override;
   1884      1.1  christos   bool from_string (gdb_byte *addr, const struct type *type,
   1885      1.1  christos 		    const std::string &string) const override;
   1886      1.1  christos 
   1887      1.1  christos   LONGEST to_longest (const gdb_byte *addr,
   1888      1.1  christos 		      const struct type *type) const override;
   1889      1.1  christos   void from_longest (gdb_byte *addr, const struct type *type,
   1890      1.1  christos 		     LONGEST val) const override;
   1891      1.1  christos   void from_ulongest (gdb_byte *addr, const struct type *type,
   1892      1.1  christos 		      ULONGEST val) const override;
   1893      1.1  christos   double to_host_double (const gdb_byte *addr,
   1894      1.1  christos 			 const struct type *type) const override
   1895      1.1  christos   {
   1896      1.1  christos     /* We don't support conversions between target decimal floating-point
   1897      1.1  christos        types and the host double type.  */
   1898      1.1  christos     gdb_assert_not_reached ("invalid operation on decimal float");
   1899      1.1  christos   }
   1900      1.1  christos   void from_host_double (gdb_byte *addr, const struct type *type,
   1901      1.1  christos 			 double val) const override
   1902      1.1  christos   {
   1903      1.1  christos     /* We don't support conversions between target decimal floating-point
   1904      1.1  christos        types and the host double type.  */
   1905      1.1  christos     gdb_assert_not_reached ("invalid operation on decimal float");
   1906      1.1  christos   }
   1907      1.1  christos   void convert (const gdb_byte *from, const struct type *from_type,
   1908      1.1  christos 		gdb_byte *to, const struct type *to_type) const override;
   1909      1.1  christos 
   1910      1.1  christos   void binop (enum exp_opcode opcode,
   1911      1.1  christos 	      const gdb_byte *x, const struct type *type_x,
   1912      1.1  christos 	      const gdb_byte *y, const struct type *type_y,
   1913      1.1  christos 	      gdb_byte *res, const struct type *type_res) const override;
   1914      1.1  christos   int compare (const gdb_byte *x, const struct type *type_x,
   1915      1.1  christos 	       const gdb_byte *y, const struct type *type_y) const override;
   1916      1.1  christos };
   1917      1.1  christos 
   1918      1.1  christos /* Convert decimal type to its string representation.  LEN is the length
   1919      1.1  christos    of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and
   1920      1.1  christos    16 bytes for decimal128.  */
   1921      1.1  christos std::string
   1922      1.1  christos decimal_float_ops::to_string (const gdb_byte *addr, const struct type *type,
   1923      1.1  christos 			      const char *format = nullptr) const
   1924      1.1  christos {
   1925      1.1  christos   gdb_byte dec[16];
   1926      1.1  christos 
   1927      1.1  christos   match_endianness (addr, type, dec);
   1928      1.1  christos 
   1929      1.1  christos   if (format != nullptr)
   1930      1.1  christos     {
   1931      1.1  christos       /* We don't handle format strings (yet).  If the host printf supports
   1932      1.1  christos 	 decimal floating point types, just use this.  Otherwise, fall back
   1933      1.1  christos 	 to printing the number while ignoring the format string.  */
   1934      1.1  christos #if defined (PRINTF_HAS_DECFLOAT)
   1935      1.1  christos       /* FIXME: This makes unwarranted assumptions about the host ABI!  */
   1936      1.1  christos       return string_printf (format, dec);
   1937      1.1  christos #endif
   1938      1.1  christos     }
   1939      1.1  christos 
   1940      1.1  christos   std::string result;
   1941      1.1  christos   result.resize (MAX_DECIMAL_STRING);
   1942      1.1  christos 
   1943      1.1  christos   switch (TYPE_LENGTH (type))
   1944      1.1  christos     {
   1945      1.1  christos       case 4:
   1946      1.1  christos 	decimal32ToString ((decimal32 *) dec, &result[0]);
   1947      1.1  christos 	break;
   1948      1.1  christos       case 8:
   1949      1.1  christos 	decimal64ToString ((decimal64 *) dec, &result[0]);
   1950      1.1  christos 	break;
   1951      1.1  christos       case 16:
   1952      1.1  christos 	decimal128ToString ((decimal128 *) dec, &result[0]);
   1953      1.1  christos 	break;
   1954      1.1  christos       default:
   1955      1.1  christos 	error (_("Unknown decimal floating point type."));
   1956      1.1  christos 	break;
   1957      1.1  christos     }
   1958      1.1  christos 
   1959      1.1  christos   return result;
   1960      1.1  christos }
   1961      1.1  christos 
   1962      1.1  christos /* Convert the string form of a decimal value to its decimal representation.
   1963      1.1  christos    LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for
   1964      1.1  christos    decimal64 and 16 bytes for decimal128.  */
   1965      1.1  christos bool
   1966      1.1  christos decimal_float_ops::from_string (gdb_byte *addr, const struct type *type,
   1967      1.1  christos 				const std::string &string) const
   1968      1.1  christos {
   1969      1.1  christos   decContext set;
   1970      1.1  christos   gdb_byte dec[16];
   1971      1.1  christos 
   1972      1.1  christos   set_decnumber_context (&set, type);
   1973      1.1  christos 
   1974      1.1  christos   switch (TYPE_LENGTH (type))
   1975      1.1  christos     {
   1976      1.1  christos       case 4:
   1977      1.1  christos 	decimal32FromString ((decimal32 *) dec, string.c_str (), &set);
   1978      1.1  christos 	break;
   1979      1.1  christos       case 8:
   1980      1.1  christos 	decimal64FromString ((decimal64 *) dec, string.c_str (), &set);
   1981      1.1  christos 	break;
   1982      1.1  christos       case 16:
   1983      1.1  christos 	decimal128FromString ((decimal128 *) dec, string.c_str (), &set);
   1984      1.1  christos 	break;
   1985      1.1  christos       default:
   1986      1.1  christos 	error (_("Unknown decimal floating point type."));
   1987      1.1  christos 	break;
   1988      1.1  christos     }
   1989      1.1  christos 
   1990      1.1  christos   match_endianness (dec, type, addr);
   1991      1.1  christos 
   1992      1.1  christos   /* Check for errors in the DFP operation.  */
   1993      1.1  christos   decimal_check_errors (&set);
   1994      1.1  christos 
   1995      1.1  christos   return true;
   1996      1.1  christos }
   1997      1.1  christos 
   1998      1.1  christos /* Converts a LONGEST to a decimal float of specified LEN bytes.  */
   1999      1.1  christos void
   2000      1.1  christos decimal_float_ops::from_longest (gdb_byte *addr, const struct type *type,
   2001      1.1  christos 				 LONGEST from) const
   2002      1.1  christos {
   2003      1.1  christos   decNumber number;
   2004      1.1  christos 
   2005      1.1  christos   if ((int32_t) from != from)
   2006      1.1  christos     /* libdecnumber can convert only 32-bit integers.  */
   2007      1.1  christos     error (_("Conversion of large integer to a "
   2008      1.1  christos 	     "decimal floating type is not supported."));
   2009      1.1  christos 
   2010      1.1  christos   decNumberFromInt32 (&number, (int32_t) from);
   2011      1.1  christos 
   2012      1.1  christos   decimal_from_number (&number, addr, type);
   2013      1.1  christos }
   2014      1.1  christos 
   2015      1.1  christos /* Converts a ULONGEST to a decimal float of specified LEN bytes.  */
   2016      1.1  christos void
   2017      1.1  christos decimal_float_ops::from_ulongest (gdb_byte *addr, const struct type *type,
   2018      1.1  christos 				  ULONGEST from) const
   2019      1.1  christos {
   2020      1.1  christos   decNumber number;
   2021      1.1  christos 
   2022      1.1  christos   if ((uint32_t) from != from)
   2023      1.1  christos     /* libdecnumber can convert only 32-bit integers.  */
   2024      1.1  christos     error (_("Conversion of large integer to a "
   2025      1.1  christos 	     "decimal floating type is not supported."));
   2026      1.1  christos 
   2027      1.1  christos   decNumberFromUInt32 (&number, (uint32_t) from);
   2028      1.1  christos 
   2029      1.1  christos   decimal_from_number (&number, addr, type);
   2030      1.1  christos }
   2031      1.1  christos 
   2032      1.1  christos /* Converts a decimal float of LEN bytes to a LONGEST.  */
   2033      1.1  christos LONGEST
   2034      1.1  christos decimal_float_ops::to_longest (const gdb_byte *addr,
   2035      1.1  christos                                const struct type *type) const
   2036      1.1  christos {
   2037      1.1  christos   /* libdecnumber has a function to convert from decimal to integer, but
   2038      1.1  christos      it doesn't work when the decimal number has a fractional part.  */
   2039      1.1  christos   std::string str = to_string (addr, type);
   2040      1.1  christos   return strtoll (str.c_str (), NULL, 10);
   2041      1.1  christos }
   2042      1.1  christos 
   2043      1.1  christos /* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y
   2044      1.1  christos    and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
   2045      1.1  christos    RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT.  */
   2046      1.1  christos void
   2047      1.1  christos decimal_float_ops::binop (enum exp_opcode op,
   2048      1.1  christos 			  const gdb_byte *x, const struct type *type_x,
   2049      1.1  christos 			  const gdb_byte *y, const struct type *type_y,
   2050      1.1  christos 			  gdb_byte *res, const struct type *type_res) const
   2051      1.1  christos {
   2052      1.1  christos   decContext set;
   2053      1.1  christos   decNumber number1, number2, number3;
   2054      1.1  christos 
   2055      1.1  christos   decimal_to_number (x, type_x, &number1);
   2056      1.1  christos   decimal_to_number (y, type_y, &number2);
   2057      1.1  christos 
   2058      1.1  christos   set_decnumber_context (&set, type_res);
   2059      1.1  christos 
   2060      1.1  christos   switch (op)
   2061      1.1  christos     {
   2062      1.1  christos       case BINOP_ADD:
   2063      1.1  christos 	decNumberAdd (&number3, &number1, &number2, &set);
   2064      1.1  christos 	break;
   2065      1.1  christos       case BINOP_SUB:
   2066      1.1  christos 	decNumberSubtract (&number3, &number1, &number2, &set);
   2067      1.1  christos 	break;
   2068      1.1  christos       case BINOP_MUL:
   2069      1.1  christos 	decNumberMultiply (&number3, &number1, &number2, &set);
   2070      1.1  christos 	break;
   2071      1.1  christos       case BINOP_DIV:
   2072      1.1  christos 	decNumberDivide (&number3, &number1, &number2, &set);
   2073      1.1  christos 	break;
   2074      1.1  christos       case BINOP_EXP:
   2075      1.1  christos 	decNumberPower (&number3, &number1, &number2, &set);
   2076      1.1  christos 	break;
   2077      1.1  christos      default:
   2078      1.1  christos 	error (_("Operation not valid for decimal floating point number."));
   2079      1.1  christos 	break;
   2080      1.1  christos     }
   2081      1.1  christos 
   2082      1.1  christos   /* Check for errors in the DFP operation.  */
   2083      1.1  christos   decimal_check_errors (&set);
   2084      1.1  christos 
   2085      1.1  christos   decimal_from_number (&number3, res, type_res);
   2086      1.1  christos }
   2087      1.1  christos 
   2088      1.1  christos /* Compares two numbers numerically.  If X is less than Y then the return value
   2089      1.1  christos    will be -1.  If they are equal, then the return value will be 0.  If X is
   2090      1.1  christos    greater than the Y then the return value will be 1.  */
   2091      1.1  christos int
   2092      1.1  christos decimal_float_ops::compare (const gdb_byte *x, const struct type *type_x,
   2093      1.1  christos 			    const gdb_byte *y, const struct type *type_y) const
   2094      1.1  christos {
   2095      1.1  christos   decNumber number1, number2, result;
   2096      1.1  christos   decContext set;
   2097      1.1  christos   const struct type *type_result;
   2098      1.1  christos 
   2099      1.1  christos   decimal_to_number (x, type_x, &number1);
   2100      1.1  christos   decimal_to_number (y, type_y, &number2);
   2101      1.1  christos 
   2102      1.1  christos   /* Perform the comparison in the larger of the two sizes.  */
   2103      1.1  christos   type_result = TYPE_LENGTH (type_x) > TYPE_LENGTH (type_y) ? type_x : type_y;
   2104      1.1  christos   set_decnumber_context (&set, type_result);
   2105      1.1  christos 
   2106      1.1  christos   decNumberCompare (&result, &number1, &number2, &set);
   2107      1.1  christos 
   2108      1.1  christos   /* Check for errors in the DFP operation.  */
   2109      1.1  christos   decimal_check_errors (&set);
   2110      1.1  christos 
   2111      1.1  christos   if (decNumberIsNaN (&result))
   2112      1.1  christos     error (_("Comparison with an invalid number (NaN)."));
   2113      1.1  christos   else if (decNumberIsZero (&result))
   2114      1.1  christos     return 0;
   2115      1.1  christos   else if (decNumberIsNegative (&result))
   2116      1.1  christos     return -1;
   2117      1.1  christos   else
   2118      1.1  christos     return 1;
   2119      1.1  christos }
   2120      1.1  christos 
   2121      1.1  christos /* Convert a decimal value from a decimal type with LEN_FROM bytes to a
   2122      1.1  christos    decimal type with LEN_TO bytes.  */
   2123      1.1  christos void
   2124      1.1  christos decimal_float_ops::convert (const gdb_byte *from, const struct type *from_type,
   2125      1.1  christos 			    gdb_byte *to, const struct type *to_type) const
   2126      1.1  christos {
   2127      1.1  christos   decNumber number;
   2128      1.1  christos 
   2129      1.1  christos   decimal_to_number (from, from_type, &number);
   2130      1.1  christos   decimal_from_number (&number, to, to_type);
   2131      1.1  christos }
   2132      1.1  christos 
   2133      1.1  christos 
   2134      1.1  christos /* Typed floating-point routines.  These routines operate on floating-point
   2135      1.1  christos    values in target format, represented by a byte buffer interpreted as a
   2136      1.1  christos    "struct type", which may be either a binary or decimal floating-point
   2137      1.1  christos    type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT).  */
   2138      1.1  christos 
   2139      1.1  christos /* Return whether TYPE1 and TYPE2 are of the same category (binary or
   2140      1.1  christos    decimal floating-point).  */
   2141      1.1  christos static bool
   2142      1.1  christos target_float_same_category_p (const struct type *type1,
   2143      1.1  christos 			      const struct type *type2)
   2144      1.1  christos {
   2145  1.1.1.2  christos   return type1->code () == type2->code ();
   2146      1.1  christos }
   2147      1.1  christos 
   2148      1.1  christos /* Return whether TYPE1 and TYPE2 use the same floating-point format.  */
   2149      1.1  christos static bool
   2150      1.1  christos target_float_same_format_p (const struct type *type1,
   2151      1.1  christos 			    const struct type *type2)
   2152      1.1  christos {
   2153      1.1  christos   if (!target_float_same_category_p (type1, type2))
   2154      1.1  christos     return false;
   2155      1.1  christos 
   2156  1.1.1.2  christos   switch (type1->code ())
   2157      1.1  christos     {
   2158      1.1  christos       case TYPE_CODE_FLT:
   2159      1.1  christos 	return floatformat_from_type (type1) == floatformat_from_type (type2);
   2160      1.1  christos 
   2161      1.1  christos       case TYPE_CODE_DECFLOAT:
   2162      1.1  christos 	return (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
   2163  1.1.1.2  christos 		&& (type_byte_order (type1)
   2164  1.1.1.2  christos 		    == type_byte_order (type2)));
   2165      1.1  christos 
   2166      1.1  christos       default:
   2167      1.1  christos 	gdb_assert_not_reached ("unexpected type code");
   2168      1.1  christos     }
   2169      1.1  christos }
   2170      1.1  christos 
   2171      1.1  christos /* Return the size (without padding) of the target floating-point
   2172      1.1  christos    format used by TYPE.  */
   2173      1.1  christos static int
   2174      1.1  christos target_float_format_length (const struct type *type)
   2175      1.1  christos {
   2176  1.1.1.2  christos   switch (type->code ())
   2177      1.1  christos     {
   2178      1.1  christos       case TYPE_CODE_FLT:
   2179      1.1  christos 	return floatformat_totalsize_bytes (floatformat_from_type (type));
   2180      1.1  christos 
   2181      1.1  christos       case TYPE_CODE_DECFLOAT:
   2182      1.1  christos 	return TYPE_LENGTH (type);
   2183      1.1  christos 
   2184      1.1  christos       default:
   2185      1.1  christos 	gdb_assert_not_reached ("unexpected type code");
   2186      1.1  christos     }
   2187      1.1  christos }
   2188      1.1  christos 
   2189      1.1  christos /* Identifiers of available host-side intermediate formats.  These must
   2190      1.1  christos    be sorted so the that the more "general" kinds come later.  */
   2191      1.1  christos enum target_float_ops_kind
   2192      1.1  christos {
   2193      1.1  christos   /* Target binary floating-point formats that match a host format.  */
   2194      1.1  christos   host_float = 0,
   2195      1.1  christos   host_double,
   2196      1.1  christos   host_long_double,
   2197      1.1  christos   /* Any other target binary floating-point format.  */
   2198      1.1  christos   binary,
   2199      1.1  christos   /* Any target decimal floating-point format.  */
   2200      1.1  christos   decimal
   2201      1.1  christos };
   2202      1.1  christos 
   2203      1.1  christos /* Given a target type TYPE, choose the best host-side intermediate format
   2204      1.1  christos    to perform operations on TYPE in.  */
   2205      1.1  christos static enum target_float_ops_kind
   2206      1.1  christos get_target_float_ops_kind (const struct type *type)
   2207      1.1  christos {
   2208  1.1.1.2  christos   switch (type->code ())
   2209      1.1  christos     {
   2210      1.1  christos       case TYPE_CODE_FLT:
   2211      1.1  christos         {
   2212      1.1  christos 	  const struct floatformat *fmt = floatformat_from_type (type);
   2213      1.1  christos 
   2214      1.1  christos 	  /* Binary floating-point formats matching a host format.  */
   2215      1.1  christos 	  if (fmt == host_float_format)
   2216      1.1  christos 	    return target_float_ops_kind::host_float;
   2217      1.1  christos 	  if (fmt == host_double_format)
   2218      1.1  christos 	    return target_float_ops_kind::host_double;
   2219      1.1  christos 	  if (fmt == host_long_double_format)
   2220      1.1  christos 	    return target_float_ops_kind::host_long_double;
   2221      1.1  christos 
   2222      1.1  christos 	  /* Any other binary floating-point format.  */
   2223      1.1  christos 	  return target_float_ops_kind::binary;
   2224      1.1  christos 	}
   2225      1.1  christos 
   2226      1.1  christos       case TYPE_CODE_DECFLOAT:
   2227      1.1  christos 	{
   2228      1.1  christos 	  /* Any decimal floating-point format.  */
   2229      1.1  christos 	  return target_float_ops_kind::decimal;
   2230      1.1  christos 	}
   2231      1.1  christos 
   2232      1.1  christos       default:
   2233      1.1  christos 	gdb_assert_not_reached ("unexpected type code");
   2234      1.1  christos     }
   2235      1.1  christos }
   2236      1.1  christos 
   2237      1.1  christos /* Return target_float_ops to peform operations for KIND.  */
   2238      1.1  christos static const target_float_ops *
   2239      1.1  christos get_target_float_ops (enum target_float_ops_kind kind)
   2240      1.1  christos {
   2241      1.1  christos   switch (kind)
   2242      1.1  christos     {
   2243      1.1  christos       /* If the type format matches one of the host floating-point
   2244      1.1  christos 	 types, use that type as intermediate format.  */
   2245      1.1  christos       case target_float_ops_kind::host_float:
   2246      1.1  christos         {
   2247      1.1  christos 	  static host_float_ops<float> host_float_ops_float;
   2248      1.1  christos 	  return &host_float_ops_float;
   2249      1.1  christos 	}
   2250      1.1  christos 
   2251      1.1  christos       case target_float_ops_kind::host_double:
   2252      1.1  christos         {
   2253      1.1  christos 	  static host_float_ops<double> host_float_ops_double;
   2254      1.1  christos 	  return &host_float_ops_double;
   2255      1.1  christos 	}
   2256      1.1  christos 
   2257      1.1  christos       case target_float_ops_kind::host_long_double:
   2258      1.1  christos         {
   2259      1.1  christos 	  static host_float_ops<long double> host_float_ops_long_double;
   2260      1.1  christos 	  return &host_float_ops_long_double;
   2261      1.1  christos 	}
   2262      1.1  christos 
   2263      1.1  christos       /* For binary floating-point formats that do not match any host format,
   2264      1.1  christos          use mpfr_t as intermediate format to provide precise target-floating
   2265  1.1.1.2  christos          point emulation.  However, if the MPFR library is not available,
   2266      1.1  christos          use the largest host floating-point type as intermediate format.  */
   2267      1.1  christos       case target_float_ops_kind::binary:
   2268      1.1  christos         {
   2269      1.1  christos #ifdef HAVE_LIBMPFR
   2270      1.1  christos 	  static mpfr_float_ops binary_float_ops;
   2271      1.1  christos #else
   2272      1.1  christos 	  static host_float_ops<long double> binary_float_ops;
   2273      1.1  christos #endif
   2274      1.1  christos 	  return &binary_float_ops;
   2275      1.1  christos 	}
   2276      1.1  christos 
   2277      1.1  christos       /* For decimal floating-point types, always use the libdecnumber
   2278      1.1  christos 	 decNumber type as intermediate format.  */
   2279      1.1  christos       case target_float_ops_kind::decimal:
   2280      1.1  christos 	{
   2281      1.1  christos 	  static decimal_float_ops decimal_float_ops;
   2282      1.1  christos 	  return &decimal_float_ops;
   2283      1.1  christos 	}
   2284      1.1  christos 
   2285      1.1  christos       default:
   2286      1.1  christos 	gdb_assert_not_reached ("unexpected target_float_ops_kind");
   2287      1.1  christos     }
   2288      1.1  christos }
   2289      1.1  christos 
   2290      1.1  christos /* Given a target type TYPE, determine the best host-side intermediate format
   2291      1.1  christos    to perform operations on TYPE in.  */
   2292      1.1  christos static const target_float_ops *
   2293      1.1  christos get_target_float_ops (const struct type *type)
   2294      1.1  christos {
   2295      1.1  christos   enum target_float_ops_kind kind = get_target_float_ops_kind (type);
   2296      1.1  christos   return get_target_float_ops (kind);
   2297      1.1  christos }
   2298      1.1  christos 
   2299      1.1  christos /* The same for operations involving two target types TYPE1 and TYPE2.  */
   2300      1.1  christos static const target_float_ops *
   2301      1.1  christos get_target_float_ops (const struct type *type1, const struct type *type2)
   2302      1.1  christos {
   2303  1.1.1.2  christos   gdb_assert (type1->code () == type2->code ());
   2304      1.1  christos 
   2305      1.1  christos   enum target_float_ops_kind kind1 = get_target_float_ops_kind (type1);
   2306      1.1  christos   enum target_float_ops_kind kind2 = get_target_float_ops_kind (type2);
   2307      1.1  christos 
   2308      1.1  christos   /* Given the way the kinds are sorted, we simply choose the larger one;
   2309      1.1  christos      this will be able to hold values of either type.  */
   2310      1.1  christos   return get_target_float_ops (std::max (kind1, kind2));
   2311      1.1  christos }
   2312      1.1  christos 
   2313      1.1  christos /* Return whether the byte-stream ADDR holds a valid value of
   2314      1.1  christos    floating-point type TYPE.  */
   2315      1.1  christos bool
   2316      1.1  christos target_float_is_valid (const gdb_byte *addr, const struct type *type)
   2317      1.1  christos {
   2318  1.1.1.2  christos   if (type->code () == TYPE_CODE_FLT)
   2319      1.1  christos     return floatformat_is_valid (floatformat_from_type (type), addr);
   2320      1.1  christos 
   2321  1.1.1.2  christos   if (type->code () == TYPE_CODE_DECFLOAT)
   2322      1.1  christos     return true;
   2323      1.1  christos 
   2324      1.1  christos   gdb_assert_not_reached ("unexpected type code");
   2325      1.1  christos }
   2326      1.1  christos 
   2327      1.1  christos /* Return whether the byte-stream ADDR, interpreted as floating-point
   2328      1.1  christos    type TYPE, is numerically equal to zero (of either sign).  */
   2329      1.1  christos bool
   2330      1.1  christos target_float_is_zero (const gdb_byte *addr, const struct type *type)
   2331      1.1  christos {
   2332  1.1.1.2  christos   if (type->code () == TYPE_CODE_FLT)
   2333      1.1  christos     return (floatformat_classify (floatformat_from_type (type), addr)
   2334      1.1  christos 	    == float_zero);
   2335      1.1  christos 
   2336  1.1.1.2  christos   if (type->code () == TYPE_CODE_DECFLOAT)
   2337      1.1  christos     return decimal_is_zero (addr, type);
   2338      1.1  christos 
   2339      1.1  christos   gdb_assert_not_reached ("unexpected type code");
   2340      1.1  christos }
   2341      1.1  christos 
   2342      1.1  christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
   2343      1.1  christos    to a string, optionally using the print format FORMAT.  */
   2344      1.1  christos std::string
   2345      1.1  christos target_float_to_string (const gdb_byte *addr, const struct type *type,
   2346      1.1  christos 			const char *format)
   2347      1.1  christos {
   2348      1.1  christos   /* Unless we need to adhere to a specific format, provide special
   2349      1.1  christos      output for special cases of binary floating-point numbers.  */
   2350  1.1.1.2  christos   if (format == nullptr && type->code () == TYPE_CODE_FLT)
   2351      1.1  christos     {
   2352      1.1  christos       const struct floatformat *fmt = floatformat_from_type (type);
   2353      1.1  christos 
   2354      1.1  christos       /* Detect invalid representations.  */
   2355      1.1  christos       if (!floatformat_is_valid (fmt, addr))
   2356      1.1  christos 	return "<invalid float value>";
   2357      1.1  christos 
   2358      1.1  christos       /* Handle NaN and Inf.  */
   2359      1.1  christos       enum float_kind kind = floatformat_classify (fmt, addr);
   2360      1.1  christos       if (kind == float_nan)
   2361      1.1  christos 	{
   2362      1.1  christos 	  const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
   2363      1.1  christos 	  const char *mantissa = floatformat_mantissa (fmt, addr);
   2364      1.1  christos 	  return string_printf ("%snan(0x%s)", sign, mantissa);
   2365      1.1  christos 	}
   2366      1.1  christos       else if (kind == float_infinite)
   2367      1.1  christos 	{
   2368      1.1  christos 	  const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
   2369      1.1  christos 	  return string_printf ("%sinf", sign);
   2370      1.1  christos 	}
   2371      1.1  christos     }
   2372      1.1  christos 
   2373      1.1  christos   const target_float_ops *ops = get_target_float_ops (type);
   2374      1.1  christos   return ops->to_string (addr, type, format);
   2375      1.1  christos }
   2376      1.1  christos 
   2377      1.1  christos /* Parse string STRING into a target floating-number of type TYPE and
   2378      1.1  christos    store it as byte-stream ADDR.  Return whether parsing succeeded.  */
   2379      1.1  christos bool
   2380      1.1  christos target_float_from_string (gdb_byte *addr, const struct type *type,
   2381      1.1  christos 			  const std::string &string)
   2382      1.1  christos {
   2383      1.1  christos   const target_float_ops *ops = get_target_float_ops (type);
   2384      1.1  christos   return ops->from_string (addr, type, string);
   2385      1.1  christos }
   2386      1.1  christos 
   2387      1.1  christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
   2388      1.1  christos    to an integer value (rounding towards zero).  */
   2389      1.1  christos LONGEST
   2390      1.1  christos target_float_to_longest (const gdb_byte *addr, const struct type *type)
   2391      1.1  christos {
   2392      1.1  christos   const target_float_ops *ops = get_target_float_ops (type);
   2393      1.1  christos   return ops->to_longest (addr, type);
   2394      1.1  christos }
   2395      1.1  christos 
   2396      1.1  christos /* Convert signed integer VAL to a target floating-number of type TYPE
   2397      1.1  christos    and store it as byte-stream ADDR.  */
   2398      1.1  christos void
   2399      1.1  christos target_float_from_longest (gdb_byte *addr, const struct type *type,
   2400      1.1  christos 			   LONGEST val)
   2401      1.1  christos {
   2402      1.1  christos   const target_float_ops *ops = get_target_float_ops (type);
   2403      1.1  christos   ops->from_longest (addr, type, val);
   2404      1.1  christos }
   2405      1.1  christos 
   2406      1.1  christos /* Convert unsigned integer VAL to a target floating-number of type TYPE
   2407      1.1  christos    and store it as byte-stream ADDR.  */
   2408      1.1  christos void
   2409      1.1  christos target_float_from_ulongest (gdb_byte *addr, const struct type *type,
   2410      1.1  christos 			    ULONGEST val)
   2411      1.1  christos {
   2412      1.1  christos   const target_float_ops *ops = get_target_float_ops (type);
   2413      1.1  christos   ops->from_ulongest (addr, type, val);
   2414      1.1  christos }
   2415      1.1  christos 
   2416      1.1  christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
   2417      1.1  christos    to a floating-point value in the host "double" format.  */
   2418      1.1  christos double
   2419      1.1  christos target_float_to_host_double (const gdb_byte *addr,
   2420      1.1  christos 			     const struct type *type)
   2421      1.1  christos {
   2422      1.1  christos   const target_float_ops *ops = get_target_float_ops (type);
   2423      1.1  christos   return ops->to_host_double (addr, type);
   2424      1.1  christos }
   2425      1.1  christos 
   2426      1.1  christos /* Convert floating-point value VAL in the host "double" format to a target
   2427      1.1  christos    floating-number of type TYPE and store it as byte-stream ADDR.  */
   2428      1.1  christos void
   2429      1.1  christos target_float_from_host_double (gdb_byte *addr, const struct type *type,
   2430      1.1  christos 			       double val)
   2431      1.1  christos {
   2432      1.1  christos   const target_float_ops *ops = get_target_float_ops (type);
   2433      1.1  christos   ops->from_host_double (addr, type, val);
   2434      1.1  christos }
   2435      1.1  christos 
   2436      1.1  christos /* Convert a floating-point number of type FROM_TYPE from the target
   2437      1.1  christos    byte-stream FROM to a floating-point number of type TO_TYPE, and
   2438      1.1  christos    store it to the target byte-stream TO.  */
   2439      1.1  christos void
   2440      1.1  christos target_float_convert (const gdb_byte *from, const struct type *from_type,
   2441      1.1  christos 		      gdb_byte *to, const struct type *to_type)
   2442      1.1  christos {
   2443      1.1  christos   /* We cannot directly convert between binary and decimal floating-point
   2444      1.1  christos      types, so go via an intermediary string.  */
   2445      1.1  christos   if (!target_float_same_category_p (from_type, to_type))
   2446      1.1  christos     {
   2447      1.1  christos       std::string str = target_float_to_string (from, from_type);
   2448      1.1  christos       target_float_from_string (to, to_type, str);
   2449      1.1  christos       return;
   2450      1.1  christos     }
   2451      1.1  christos 
   2452      1.1  christos   /* Convert between two different formats in the same category.  */
   2453      1.1  christos   if (!target_float_same_format_p (from_type, to_type))
   2454      1.1  christos   {
   2455      1.1  christos     const target_float_ops *ops = get_target_float_ops (from_type, to_type);
   2456      1.1  christos     ops->convert (from, from_type, to, to_type);
   2457      1.1  christos     return;
   2458      1.1  christos   }
   2459      1.1  christos 
   2460      1.1  christos   /* The floating-point formats match, so we simply copy the data, ensuring
   2461      1.1  christos      possible padding bytes in the target buffer are zeroed out.  */
   2462      1.1  christos   memset (to, 0, TYPE_LENGTH (to_type));
   2463      1.1  christos   memcpy (to, from, target_float_format_length (to_type));
   2464      1.1  christos }
   2465      1.1  christos 
   2466      1.1  christos /* Perform the binary operation indicated by OPCODE, using as operands the
   2467      1.1  christos    target byte streams X and Y, interpreted as floating-point numbers of
   2468      1.1  christos    types TYPE_X and TYPE_Y, respectively.  Convert the result to type
   2469      1.1  christos    TYPE_RES and store it into the byte-stream RES.
   2470      1.1  christos 
   2471      1.1  christos    The three types must either be all binary floating-point types, or else
   2472      1.1  christos    all decimal floating-point types.  Binary and decimal floating-point
   2473      1.1  christos    types cannot be mixed within a single operation.  */
   2474      1.1  christos void
   2475      1.1  christos target_float_binop (enum exp_opcode opcode,
   2476      1.1  christos 		    const gdb_byte *x, const struct type *type_x,
   2477      1.1  christos 		    const gdb_byte *y, const struct type *type_y,
   2478      1.1  christos 		    gdb_byte *res, const struct type *type_res)
   2479      1.1  christos {
   2480      1.1  christos   gdb_assert (target_float_same_category_p (type_x, type_res));
   2481      1.1  christos   gdb_assert (target_float_same_category_p (type_y, type_res));
   2482      1.1  christos 
   2483      1.1  christos   const target_float_ops *ops = get_target_float_ops (type_x, type_y);
   2484      1.1  christos   ops->binop (opcode, x, type_x, y, type_y, res, type_res);
   2485      1.1  christos }
   2486      1.1  christos 
   2487      1.1  christos /* Compare the two target byte streams X and Y, interpreted as floating-point
   2488      1.1  christos    numbers of types TYPE_X and TYPE_Y, respectively.  Return zero if X and Y
   2489      1.1  christos    are equal, -1 if X is less than Y, and 1 otherwise.
   2490      1.1  christos 
   2491      1.1  christos    The two types must either both be binary floating-point types, or else
   2492      1.1  christos    both be decimal floating-point types.  Binary and decimal floating-point
   2493      1.1  christos    types cannot compared directly against each other.  */
   2494      1.1  christos int
   2495      1.1  christos target_float_compare (const gdb_byte *x, const struct type *type_x,
   2496      1.1  christos 		      const gdb_byte *y, const struct type *type_y)
   2497      1.1  christos {
   2498      1.1  christos   gdb_assert (target_float_same_category_p (type_x, type_y));
   2499      1.1  christos 
   2500      1.1  christos   const target_float_ops *ops = get_target_float_ops (type_x, type_y);
   2501      1.1  christos   return ops->compare (x, type_x, y, type_y);
   2502      1.1  christos }
   2503      1.1  christos 
   2504