return res;
}
+/* Return the precision of the floating point format FMT. */
+
+static int
+floatformat_precision (const struct floatformat *fmt)
+{
+ /* Assume the precision of and IBM long double is twice the precision
+ of the underlying double. This matches what GCC does. */
+ if (fmt->split_half)
+ return 2 * floatformat_precision (fmt->split_half);
+
+ /* Otherwise, the precision is the size of mantissa in bits,
+ including the implicit bit if present. */
+ int prec = fmt->man_len;
+ if (fmt->intbit == floatformat_intbit_no)
+ prec++;
+
+ return prec;
+}
+
\f
/* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
convert_doublest_to_floatformat (fmt, in, out);
}
+/* Convert the byte-stream ADDR, interpreted as floating-point format FMT,
+ to a string. */
+std::string
+floatformat_to_string (const struct floatformat *fmt,
+ const gdb_byte *in)
+{
+ /* Detect invalid representations. */
+ if (!floatformat_is_valid (fmt, in))
+ return "<invalid float value>";
+
+ /* Handle NaN and Inf. */
+ enum float_kind kind = floatformat_classify (fmt, in);
+ if (kind == float_nan)
+ {
+ const char *sign = floatformat_is_negative (fmt, in)? "-" : "";
+ const char *mantissa = floatformat_mantissa (fmt, in);
+ return string_printf ("%snan(0x%s)", sign, mantissa);
+ }
+ else if (kind == float_infinite)
+ {
+ const char *sign = floatformat_is_negative (fmt, in)? "-" : "";
+ return string_printf ("%sinf", sign);
+ }
+
+ /* Print the number using a format string where the precision is set
+ to the DECIMAL_DIG value for the given floating-point format.
+ This value is computed as
+
+ ceil(1 + p * log10(b)),
+
+ where p is the precision of the floating-point format in bits, and
+ b is the base (which is always 2 for the formats we support). */
+ const double log10_2 = .30102999566398119521;
+ double d_decimal_dig = 1 + floatformat_precision (fmt) * log10_2;
+ int decimal_dig = d_decimal_dig;
+ if (decimal_dig < d_decimal_dig)
+ decimal_dig++;
+
+ std::string host_format
+ = string_printf ("%%.%d" DOUBLEST_PRINT_FORMAT, decimal_dig);
+
+ DOUBLEST doub;
+ floatformat_to_doublest (fmt, in, &doub);
+ return string_printf (host_format.c_str (), doub);
+}
\f
/* Extract a floating-point number of type TYPE from a target-order
byte-stream at ADDR. Returns the value as type DOUBLEST. */
#include "language.h"
#include "annotate.h"
#include "valprint.h"
-#include "floatformat.h"
#include "doublest.h"
#include "dfp.h"
#include "extension.h"
}
}
-/* generic_val_print helper for TYPE_CODE_FLT. */
+/* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */
static void
generic_val_print_float (struct type *type,
}
}
-/* generic_val_print helper for TYPE_CODE_DECFLOAT. */
-
-static void
-generic_val_print_decfloat (struct type *type,
- int embedded_offset, struct ui_file *stream,
- struct value *original_value,
- const struct value_print_options *options)
-{
- struct gdbarch *gdbarch = get_type_arch (type);
- int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
-
- if (options->format)
- val_print_scalar_formatted (type, embedded_offset, original_value,
- options, 0, stream);
- else
- {
- const gdb_byte *valaddr = value_contents_for_printing (original_value);
-
- print_decimal_floating (valaddr + embedded_offset * unit_size, type,
- stream);
- }
-}
-
/* generic_val_print helper for TYPE_CODE_COMPLEX. */
static void
break;
case TYPE_CODE_FLT:
+ case TYPE_CODE_DECFLOAT:
generic_val_print_float (type, embedded_offset, stream,
original_value, options);
break;
- case TYPE_CODE_DECFLOAT:
- generic_val_print_decfloat (type, embedded_offset, stream,
- original_value, options);
- break;
-
case TYPE_CODE_VOID:
fputs_filtered (decorations->void_name, stream);
break;
return (rtnval);
}
-/* Print a floating point value of type TYPE (not always a
- TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
+/* Print a floating point value of floating-point type TYPE,
+ pointed to in GDB by VALADDR, on STREAM. */
void
print_floating (const gdb_byte *valaddr, struct type *type,
struct ui_file *stream)
{
- DOUBLEST doub;
- int inv;
- const struct floatformat *fmt = NULL;
- unsigned len = TYPE_LENGTH (type);
- enum float_kind kind;
-
- /* If it is a floating-point, check for obvious problems. */
+ std::string str;
if (TYPE_CODE (type) == TYPE_CODE_FLT)
- fmt = floatformat_from_type (type);
- if (fmt != NULL)
{
- kind = floatformat_classify (fmt, valaddr);
- if (kind == float_nan)
- {
- if (floatformat_is_negative (fmt, valaddr))
- fprintf_filtered (stream, "-");
- fprintf_filtered (stream, "nan(");
- fputs_filtered ("0x", stream);
- fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
- fprintf_filtered (stream, ")");
- return;
- }
- else if (kind == float_infinite)
- {
- if (floatformat_is_negative (fmt, valaddr))
- fputs_filtered ("-", stream);
- fputs_filtered ("inf", stream);
- return;
- }
+ const struct floatformat *fmt = floatformat_from_type (type);
+ str = floatformat_to_string (fmt, valaddr);
}
-
- /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
- isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
- needs to be used as that takes care of any necessary type
- conversions. Such conversions are of course direct to DOUBLEST
- and disregard any possible target floating point limitations.
- For instance, a u64 would be converted and displayed exactly on a
- host with 80 bit DOUBLEST but with loss of information on a host
- with 64 bit DOUBLEST. */
-
- doub = unpack_double (type, valaddr, &inv);
- if (inv)
+ else
{
- fprintf_filtered (stream, "<invalid float value>");
- return;
+ enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
+ unsigned len = TYPE_LENGTH (type);
+ str = decimal_to_string (valaddr, len, byte_order);
}
-
- /* FIXME: kettenis/2001-01-20: The following code makes too much
- assumptions about the host and target floating point format. */
-
- /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
- not necessarily be a TYPE_CODE_FLT, the below ignores that and
- instead uses the type's length to determine the precision of the
- floating-point value being printed. */
-
- if (len < sizeof (double))
- fprintf_filtered (stream, "%.9g", (double) doub);
- else if (len == sizeof (double))
- fprintf_filtered (stream, "%.17g", (double) doub);
- else
-#ifdef PRINTF_HAS_LONG_DOUBLE
- fprintf_filtered (stream, "%.35Lg", doub);
-#else
- /* This at least wins with values that are representable as
- doubles. */
- fprintf_filtered (stream, "%.17g", (double) doub);
-#endif
-}
-
-void
-print_decimal_floating (const gdb_byte *valaddr, struct type *type,
- struct ui_file *stream)
-{
- enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
- unsigned len = TYPE_LENGTH (type);
-
- std::string str = decimal_to_string (valaddr, len, byte_order);
fputs_filtered (str.c_str (), stream);
}