gdb/dfp.c - gdb

Functions defined

Macros defined

Source code

  1. /* Decimal floating point support for GDB.

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

  3.    This file is part of GDB.

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

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

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

  14. #include "defs.h"
  15. #include "expression.h"
  16. #include "gdbtypes.h"
  17. #include "value.h"
  18. #include "dfp.h"

  19. /* The order of the following headers is important for making sure
  20.    decNumber structure is large enough to hold decimal128 digits.  */

  21. #include "dpd/decimal128.h"
  22. #include "dpd/decimal64.h"
  23. #include "dpd/decimal32.h"

  24. /* In GDB, we are using an array of gdb_byte to represent decimal values.
  25.    They are stored in host byte order.  This routine does the conversion if
  26.    the target byte order is different.  */
  27. static void
  28. match_endianness (const gdb_byte *from, int len, enum bfd_endian byte_order,
  29.                   gdb_byte *to)
  30. {
  31.   int i;

  32. #if WORDS_BIGENDIAN
  33. #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE
  34. #else
  35. #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG
  36. #endif

  37.   if (byte_order == OPPOSITE_BYTE_ORDER)
  38.     for (i = 0; i < len; i++)
  39.       to[i] = from[len - i - 1];
  40.   else
  41.     for (i = 0; i < len; i++)
  42.       to[i] = from[i];

  43.   return;
  44. }

  45. /* Helper function to get the appropriate libdecnumber context for each size
  46.    of decimal float.  */
  47. static void
  48. set_decnumber_context (decContext *ctx, int len)
  49. {
  50.   switch (len)
  51.     {
  52.       case 4:
  53.         decContextDefault (ctx, DEC_INIT_DECIMAL32);
  54.         break;
  55.       case 8:
  56.         decContextDefault (ctx, DEC_INIT_DECIMAL64);
  57.         break;
  58.       case 16:
  59.         decContextDefault (ctx, DEC_INIT_DECIMAL128);
  60.         break;
  61.     }

  62.   ctx->traps = 0;
  63. }

  64. /* Check for errors signaled in the decimal context structure.  */
  65. static void
  66. decimal_check_errors (decContext *ctx)
  67. {
  68.   /* An error here could be a division by zero, an overflow, an underflow or
  69.      an invalid operation (from the DEC_Errors constant in decContext.h).
  70.      Since GDB doesn't complain about division by zero, overflow or underflow
  71.      errors for binary floating, we won't complain about them for decimal
  72.      floating either.  */
  73.   if (ctx->status & DEC_IEEE_854_Invalid_operation)
  74.     {
  75.       /* Leave only the error bits in the status flags.  */
  76.       ctx->status &= DEC_IEEE_854_Invalid_operation;
  77.       error (_("Cannot perform operation: %s"),
  78.              decContextStatusToString (ctx));
  79.     }
  80. }

  81. /* Helper function to convert from libdecnumber's appropriate representation
  82.    for computation to each size of decimal float.  */
  83. static void
  84. decimal_from_number (const decNumber *from, gdb_byte *to, int len)
  85. {
  86.   decContext set;

  87.   set_decnumber_context (&set, len);

  88.   switch (len)
  89.     {
  90.       case 4:
  91.         decimal32FromNumber ((decimal32 *) to, from, &set);
  92.         break;
  93.       case 8:
  94.         decimal64FromNumber ((decimal64 *) to, from, &set);
  95.         break;
  96.       case 16:
  97.         decimal128FromNumber ((decimal128 *) to, from, &set);
  98.         break;
  99.     }
  100. }

  101. /* Helper function to convert each size of decimal float to libdecnumber's
  102.    appropriate representation for computation.  */
  103. static void
  104. decimal_to_number (const gdb_byte *from, int len, decNumber *to)
  105. {
  106.   switch (len)
  107.     {
  108.       case 4:
  109.         decimal32ToNumber ((decimal32 *) from, to);
  110.         break;
  111.       case 8:
  112.         decimal64ToNumber ((decimal64 *) from, to);
  113.         break;
  114.       case 16:
  115.         decimal128ToNumber ((decimal128 *) from, to);
  116.         break;
  117.       default:
  118.         error (_("Unknown decimal floating point type."));
  119.         break;
  120.     }
  121. }

  122. /* Convert decimal type to its string representation.  LEN is the length
  123.    of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and
  124.    16 bytes for decimal128.  */
  125. void
  126. decimal_to_string (const gdb_byte *decbytes, int len,
  127.                    enum bfd_endian byte_order, char *s)
  128. {
  129.   gdb_byte dec[16];

  130.   match_endianness (decbytes, len, byte_order, dec);

  131.   switch (len)
  132.     {
  133.       case 4:
  134.         decimal32ToString ((decimal32 *) dec, s);
  135.         break;
  136.       case 8:
  137.         decimal64ToString ((decimal64 *) dec, s);
  138.         break;
  139.       case 16:
  140.         decimal128ToString ((decimal128 *) dec, s);
  141.         break;
  142.       default:
  143.         error (_("Unknown decimal floating point type."));
  144.         break;
  145.     }
  146. }

  147. /* Convert the string form of a decimal value to its decimal representation.
  148.    LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for
  149.    decimal64 and 16 bytes for decimal128.  */
  150. int
  151. decimal_from_string (gdb_byte *decbytes, int len, enum bfd_endian byte_order,
  152.                      const char *string)
  153. {
  154.   decContext set;
  155.   gdb_byte dec[16];

  156.   set_decnumber_context (&set, len);

  157.   switch (len)
  158.     {
  159.       case 4:
  160.         decimal32FromString ((decimal32 *) dec, string, &set);
  161.         break;
  162.       case 8:
  163.         decimal64FromString ((decimal64 *) dec, string, &set);
  164.         break;
  165.       case 16:
  166.         decimal128FromString ((decimal128 *) dec, string, &set);
  167.         break;
  168.       default:
  169.         error (_("Unknown decimal floating point type."));
  170.         break;
  171.     }

  172.   match_endianness (dec, len, byte_order, decbytes);

  173.   /* Check for errors in the DFP operation.  */
  174.   decimal_check_errors (&set);

  175.   return 1;
  176. }

  177. /* Converts a value of an integral type to a decimal float of
  178.    specified LEN bytes.  */
  179. void
  180. decimal_from_integral (struct value *from,
  181.                        gdb_byte *to, int len, enum bfd_endian byte_order)
  182. {
  183.   LONGEST l;
  184.   gdb_byte dec[16];
  185.   decNumber number;
  186.   struct type *type;

  187.   type = check_typedef (value_type (from));

  188.   if (TYPE_LENGTH (type) > 4)
  189.     /* libdecnumber can convert only 32-bit integers.  */
  190.     error (_("Conversion of large integer to a "
  191.              "decimal floating type is not supported."));

  192.   l = value_as_long (from);

  193.   if (TYPE_UNSIGNED (type))
  194.     decNumberFromUInt32 (&number, (unsigned int) l);
  195.   else
  196.     decNumberFromInt32 (&number, (int) l);

  197.   decimal_from_number (&number, dec, len);
  198.   match_endianness (dec, len, byte_order, to);
  199. }

  200. /* Converts a value of a float type to a decimal float of
  201.    specified LEN bytes.

  202.    This is an ugly way to do the conversion, but libdecnumber does
  203.    not offer a direct way to do it.  */
  204. void
  205. decimal_from_floating (struct value *from,
  206.                        gdb_byte *to, int len, enum bfd_endian byte_order)
  207. {
  208.   char *buffer;

  209.   buffer = xstrprintf ("%.30" DOUBLEST_PRINT_FORMAT, value_as_double (from));

  210.   decimal_from_string (to, len, byte_order, buffer);

  211.   xfree (buffer);
  212. }

  213. /* Converts a decimal float of LEN bytes to a double value.  */
  214. DOUBLEST
  215. decimal_to_doublest (const gdb_byte *from, int len, enum bfd_endian byte_order)
  216. {
  217.   char buffer[MAX_DECIMAL_STRING];

  218.   /* This is an ugly way to do the conversion, but libdecnumber does
  219.      not offer a direct way to do it.  */
  220.   decimal_to_string (from, len, byte_order, buffer);
  221.   return strtod (buffer, NULL);
  222. }

  223. /* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y
  224.    and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
  225.    RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT.  */
  226. void
  227. decimal_binop (enum exp_opcode op,
  228.                const gdb_byte *x, int len_x, enum bfd_endian byte_order_x,
  229.                const gdb_byte *y, int len_y, enum bfd_endian byte_order_y,
  230.                gdb_byte *result, int len_result,
  231.                enum bfd_endian byte_order_result)
  232. {
  233.   decContext set;
  234.   decNumber number1, number2, number3;
  235.   gdb_byte dec1[16], dec2[16], dec3[16];

  236.   match_endianness (x, len_x, byte_order_x, dec1);
  237.   match_endianness (y, len_y, byte_order_y, dec2);

  238.   decimal_to_number (dec1, len_x, &number1);
  239.   decimal_to_number (dec2, len_y, &number2);

  240.   set_decnumber_context (&set, len_result);

  241.   switch (op)
  242.     {
  243.       case BINOP_ADD:
  244.         decNumberAdd (&number3, &number1, &number2, &set);
  245.         break;
  246.       case BINOP_SUB:
  247.         decNumberSubtract (&number3, &number1, &number2, &set);
  248.         break;
  249.       case BINOP_MUL:
  250.         decNumberMultiply (&number3, &number1, &number2, &set);
  251.         break;
  252.       case BINOP_DIV:
  253.         decNumberDivide (&number3, &number1, &number2, &set);
  254.         break;
  255.       case BINOP_EXP:
  256.         decNumberPower (&number3, &number1, &number2, &set);
  257.         break;
  258.       default:
  259.         internal_error (__FILE__, __LINE__,
  260.                         _("Unknown decimal floating point operation."));
  261.         break;
  262.     }

  263.   /* Check for errors in the DFP operation.  */
  264.   decimal_check_errors (&set);

  265.   decimal_from_number (&number3, dec3, len_result);

  266.   match_endianness (dec3, len_result, byte_order_result, result);
  267. }

  268. /* Returns true if X (which is LEN bytes wide) is the number zero.  */
  269. int
  270. decimal_is_zero (const gdb_byte *x, int len, enum bfd_endian byte_order)
  271. {
  272.   decNumber number;
  273.   gdb_byte dec[16];

  274.   match_endianness (x, len, byte_order, dec);
  275.   decimal_to_number (dec, len, &number);

  276.   return decNumberIsZero (&number);
  277. }

  278. /* Compares two numbers numerically.  If X is less than Y then the return value
  279.    will be -1.  If they are equal, then the return value will be 0.  If X is
  280.    greater than the Y then the return value will be 1.  */
  281. int
  282. decimal_compare (const gdb_byte *x, int len_x, enum bfd_endian byte_order_x,
  283.                  const gdb_byte *y, int len_y, enum bfd_endian byte_order_y)
  284. {
  285.   decNumber number1, number2, result;
  286.   decContext set;
  287.   gdb_byte dec1[16], dec2[16];
  288.   int len_result;

  289.   match_endianness (x, len_x, byte_order_x, dec1);
  290.   match_endianness (y, len_y, byte_order_y, dec2);

  291.   decimal_to_number (dec1, len_x, &number1);
  292.   decimal_to_number (dec2, len_y, &number2);

  293.   /* Perform the comparison in the larger of the two sizes.  */
  294.   len_result = len_x > len_y ? len_x : len_y;
  295.   set_decnumber_context (&set, len_result);

  296.   decNumberCompare (&result, &number1, &number2, &set);

  297.   /* Check for errors in the DFP operation.  */
  298.   decimal_check_errors (&set);

  299.   if (decNumberIsNaN (&result))
  300.     error (_("Comparison with an invalid number (NaN)."));
  301.   else if (decNumberIsZero (&result))
  302.     return 0;
  303.   else if (decNumberIsNegative (&result))
  304.     return -1;
  305.   else
  306.     return 1;
  307. }

  308. /* Convert a decimal value from a decimal type with LEN_FROM bytes to a
  309.    decimal type with LEN_TO bytes.  */
  310. void
  311. decimal_convert (const gdb_byte *from, int len_from,
  312.                  enum bfd_endian byte_order_from, gdb_byte *to, int len_to,
  313.                  enum bfd_endian byte_order_to)
  314. {
  315.   decNumber number;
  316.   gdb_byte dec[16];

  317.   match_endianness (from, len_from, byte_order_from, dec);

  318.   decimal_to_number (dec, len_from, &number);
  319.   decimal_from_number (&number, dec, len_to);

  320.   match_endianness (dec, len_to, byte_order_to, to);
  321. }