Commit Graph

12 Commits

Author SHA1 Message Date
Joel Brobecker e2882c8578 Update copyright year range in all GDB files
gdb/ChangeLog:

        Update copyright year range in all GDB files
2018-01-02 07:38:06 +04:00
Ulrich Weigand f2f9e7ecc1 [MPFR] Fix regression on 32-bit host systems
When converting parts of the mantissa to MPFR, we need to make sure to do
an *unsigned* conversion.  Since we convert at most 32 bits at a time,
stored in an unsigned long, this doesn't matter on systems where "long"
is larger than 32 bits.  But on systems where it is 32 bits, we can get
conversion errors.

gdb/ChangeLog
2017-12-11  Ulrich Weigand  <uweigand@de.ibm.com>

	* target-float.c (mpfr_float_ops::from_target): Use mpfr_set_ui
	instead of mpfr_set_si to convert mantissa bits.
2017-12-11 15:26:26 +01:00
John Baldwin 7505954411 Define MPFR_USE_INTMAX_T so that mpfr.h assumes intmax_t is available.
mpfr.h uses a non-portable test to guess if intmax_t is available and
if API functions using intmax_t should be exposed.  Define
MPFR_USE_INTMAX_T to override the non-portable test and always expose
these functions.  This fixes the build on platforms where the test
guesses incorrectly.

gdb/ChangeLog:

	* target-float.c [HAVE_LIBMPFR]: Define MPFR_USE_INTMAX_T.
2017-11-29 16:28:01 -08:00
Ulrich Weigand 2400729ecf Target FP: Make use of MPFR if available
This second patch introduces mfpr_float_ops, an new implementation
of target_float_ops.  This implements precise emulation of target
floating-point formats using the MPFR library.  This is then used
to perform operations on types that do not match any host type.

Note that use of MPFR is still not required.  The patch adds
a configure option --with-mpfr similar to --with-expat.  If use of
MPFR is disabled via the option or MPFR is not available, code will
fall back to current behavior.  This means that operations on types
that do not match any host type will be implemented on the host
long double type instead.

A new test case verifies that we can correctly print the largest
__float128 value now.

gdb/ChangeLog:
2017-11-22  Ulrich Weigand  <uweigand@de.ibm.com>

	* NEWS: Document use of GNU MPFR.
	* README: Likewise.

	* Makefile.in (LIBMPFR): Add define.
	(CLIBS): Add $(LIBMPFR).
	* configure.ac: Add --with-mpfr configure option.
	* configure: Regenerate.
	* config.in: Regenerate.

	* target-float.c [HAVE_LIBMPFR]: Include <mpfr.h>.
	(class mpfr_float_ops): New type.
	(mpfr_float_ops::from_target): Two new overloaded functions.
	(mpfr_float_ops::to_target): Likewise.
	(mpfr_float_ops::to_string): New function.
	(mpfr_float_ops::from_string): Likewise.
	(mpfr_float_ops::to_longest): Likewise.
	(mpfr_float_ops::from_longest): Likewise.
	(mpfr_float_ops::from_ulongest): Likewise.
	(mpfr_float_ops::to_host_double): Likewise.
	(mpfr_float_ops::from_host_double): Likewise.
	(mpfr_float_ops::convert): Likewise.
	(mpfr_float_ops::binop): Likewise.
	(mpfr_float_ops::compare): Likewise.
	(get_target_float_ops): Use mpfr_float_ops if available.

gdb/doc/ChangeLog:
2017-11-22  Ulrich Weigand  <uweigand@de.ibm.com>

	* gdb.texinfo (Requirements): Document use of GNU MPFR.

gdb/testsuite/ChangeLog:
2017-11-22  Ulrich Weigand  <uweigand@de.ibm.com>

	* gdb.base/float128.c (large128): New variable.
	* gdb.base/float128.exp: Add test to print largest __float128 value.
2017-11-22 13:53:43 +01:00
Ulrich Weigand 7a26362d36 Target FP: Refactor use of host floating-point arithmetic
Prepare for using MPFR to implement floating-point arithmetic by
refactoring the way host floating-point arithmetic is currently used.

In particular, fix the following two problems that cause different
(and incorrect) results due to using host arithmetic:

- Current processing always uses host "long double", and then converts
  back to the actual target format.  This may introduce rounding errors.

- Conversion of FP values to LONGEST simply does a host C++ type cast.
  However the result of such a cast is undefined if the source value
  is outside the representable range.  MPFR always has defined behavior
  here (returns the minimum or maximum representable value).

To fix the first issue, I've now created not just one set of routines
using host FP arithmetic (on long double), but instead three different
sets of routines, one each for host float, double, and long double.
Operations can then be performed in the desired type directly, avoiding
the extra rounding step.  Using C++ templates, the three sets can all
share the same source code without duplication.

To fix the second issue, I'm simply enforcing the same conversion rule
(which makes sense anyway) when converting out-of-range values from
FP to LONGEST.

To contain the code complexity with the variety of options now possible,
I've created a new class "target_float_ops".  There are a total of five
separate implementations of this:

  host_float_ops<float>        Implemented via host FP in given type
  host_float_ops<double>
  host_float_ops<long double>
  mpfr_float_ops               Implemented via MPFR if available
  decimal_float_ops            Implemented via libdecnumber

Note instead of using the DOUBLEST define, this always just uses the
"long double" data type.  But since we now require C++11 anyway, this
type must in any case be avaialble unconditionally.

Most target floating-point operations simply dispatch to a (virtual)
member routine of this class.  Which implementation to choose is
determined from the target types involved, and whether they match
some host type or not.  E.g. any operation on a single type that
matches a host type is performed in that type.  Operations involving
two types that both match host types are performed in the larger one
(according to C/C++ implicit conversion rules).  Operations that
involve any type that does not match a host type are performed using
MPFR.  (And of course operations involving decimal FP are performed
using libdecnumber.)

This first patch implements the refactoring of target-float.c as
described above, introduing the host_float_ops and decimal_float_ops
classes, and using them.  Use of MPFR is introduced in the second patch.
A bit of special-case handling code is moved around to as to avoid
code duplication between host_float_ops and mpfr_float_ops.

Note that due to the changes mentioned above, I've had to update (fix)
the floating-point register values tested in the gdb.arch/vsx-regs.exp
test case.  (The new values now work both with host arithmetic and MPFR.)

gdb/ChangeLog:
2017-11-22  Ulrich Weigand  <uweigand@de.ibm.com>

	* target-float.c: Do not include <math.h>.
	Include <cmath> and <limits>.
	(DOUBLEST): Do not define.
	(class target_float_ops): New type.
	(class host_float_ops): New templated type.
	(class decimal_float_ops): New type.

	(floatformat_to_doublest): Rename to ...
	(host_float_ops<T>::from_target): ... this.  Use template type T
	instead of DOUBLEST.  Use C++ math routines.  Update recursive calls.
	(host_float_ops<T>::from_target): New overload using a type argument.
	(floatformat_from_doublest): Rename to ...
	(host_float_ops<T>::to_target): ... this.  Use template type T
	instead of DOUBLEST.  Use C++ math routines.  Update recursive calls.
	(host_float_ops<T>::to_target): New overload using a type argument.
	(floatformat_printf_format): New function.
	(struct printf_length_modifier): New templated type.
	(floatformat_to_string): Rename to ...
	(host_float_ops<T>::to_string): ... this.  Use type instead of
	floatformat argument.  Use floatformat_printf_format and
	printf_length_modifier.  Remove special handling of invalid numbers,
	infinities and NaN (moved to target_float_to_string).
	(struct scanf_length_modifier): New templated type.
	(floatformat_from_string): Rename to ...
	(host_float_ops<T>::from_string): ... this.  Use type instead of
	floatformat argument.  Use scanf_length_modifier.
	(floatformat_to_longest): Rename to ...
	(host_float_ops<T>::to_longest): ... this.  Use type instead of
	floatformat argument.  Handle out-of-range values deterministically.
	(floatformat_from_longest): Rename to ...
	(host_float_ops<T>::from_longest): ... this.  Use type instead of
	floatformat argument.
	(floatformat_from_ulongest): Rename to ...
	(host_float_ops<T>::from_ulongest): ... this.  Use type instead of
	floatformat argument.
	(floatformat_to_host_double): Rename to ...
	(host_float_ops<T>::to_host_double): ... this.  Use type instead of
	floatformat argument.
	(floatformat_from_host_double): Rename to ...
	(host_float_ops<T>::from_host_double): ... this.  Use type instead of
	floatformat argument.
	(floatformat_convert): Rename to ...
	(host_float_ops<T>::convert): ... this.  Use type instead of
	floatformat arguments.  Remove handling of no-op conversions.
	(floatformat_binop): Rename to ...
	(host_float_ops<T>::binop): ... this.  Use type instead of
	floatformat arguments.
	(floatformat_compare): Rename to ...
	(host_float_ops<T>::compare): ... this.  Use type instead of
	floatformat arguments.

	(match_endianness): Use type instead of length/byte_order arguments.
	(set_decnumber_context): Likewise.
	(decimal_from_number): Likewise.  Update calls.
	(decimal_to_number): Likewise.
	(decimal_is_zero): Likewise.  Update calls.  Move to earlier in file.
	(decimal_float_ops::to_host_double): New dummy function.
	(decimal_float_ops::from_host_double): Likewise.
	(decimal_to_string): Rename to ...
	(decimal_float_ops::to_string): ... this.  Use type instead of
	length/byte_order arguments.  Update calls.
	(decimal_from_string): Rename to ...
	(decimal_float_ops::from_string): ... this.  Use type instead of
	length/byte_order arguments.  Update calls.
	(decimal_from_longest): Rename to ...
	(decimal_float_ops::from_longest): ... this.  Use type instead of
	length/byte_order arguments.  Update calls.
	(decimal_from_ulongest): Rename to ...
	(decimal_float_ops::from_ulongest): ... this.  Use type instead of
	length/byte_order arguments.  Update calls.
	(decimal_to_longest): Rename to ...
	(decimal_float_ops::to_longest): ... this.  Use type instead of
	length/byte_order arguments.  Update calls.
	(decimal_binop): Rename to ...
	(decimal_float_ops::binop): ... this.  Use type instead of
	length/byte_order arguments.  Update calls.
	(decimal_compare): Rename to ...
	(decimal_float_ops::compare): ... this.  Use type instead of
	length/byte_order arguments.  Update calls.
	(decimal_convert): Rename to ...
	(decimal_float_ops::convert): ... this.  Use type instead of
	length/byte_order arguments.  Update calls.

	(target_float_same_category_p): New function.
	(target_float_same_format_p): Likewise.
	(target_float_format_length): Likewise.
	(enum target_float_ops_kind): New type.
	(get_target_float_ops_kind): New function.
	(get_target_float_ops): Three new overloaded functions.

	(target_float_is_zero): Update call.
	(target_float_to_string): Add special handling of invalid numbers,
	infinities and NaN (moved from floatformat_to_string).  Use
	target_float_ops callback.
	(target_float_from_string): Use target_float_ops callback.
	(target_float_to_longest): Likewise.
	(target_float_from_longest): Likewise.
	(target_float_from_ulongest): Likewise.
	(target_float_to_host_double): Likewise.
	(target_float_from_host_double): Likewise.
	(target_float_convert): Add special case for no-op conversions.
	Use target_float_ops callback.
	(target_float_binop): Use target_float_ops callback.
	(target_float_compare): Likewise.

gdb/testsuite/ChangeLog:
2017-11-22  Ulrich Weigand  <uweigand@de.ibm.com>

	* gdb.arch/vsx-regs.exp: Update register content checks.
2017-11-22 13:51:49 +01:00
Ulrich Weigand d723696126 Refactor endian handling in DFP routines
This patch moves endian conversion into the decimal_from_number and
decimal_to_number routines, and removes it from all their callers,
making the code simpler overall.  No functional change.

gdb/ChangeLog:
2017-11-16  Ulrich Weigand  <uweigand@de.ibm.com>

	* target-float.c (decimal_from_number): Add byte_order argument and
	call match_endianness.  Error if unknown floating-point type.
	(decimal_to_number): Add byte_order argument and call match_endianness.
	(decimal_from_longest): Update call.  Do not call match_endianness.
	(decimal_from_ulongest): Likewise.
	(decimal_binop): Likewise.
	(decimal_is_zero): Likewise.
	(decimal_compare): Likewise.
	(decimal_convert): Likewise.
2017-11-16 18:49:11 +01:00
Ulrich Weigand 1cfb73dbb7 Target FP: Merge doublest.c and dfp.c into target-float.c
Now that all target FP operations are performed via target-float.c,
this file remains the sole caller of functions in doublest.c and dfp.c.
Therefore, this patch merges the latter files into the former and
makes all their function static there.

gdb/ChangeLog:
2017-11-06  Ulrich Weigand  <uweigand@de.ibm.com>

	* Makefile.in (SFILES): Remove doublest.c and dfp.c.
	(HFILES_NO_SRCDIR): Remove doublest.h and dfp.h.
	(COMMON_OBS): Remove doublest.o and dfp.o.
	Do not build target-float.c (instead of doublest.c)
	with -Wformat-nonliteral.

	* doublest.c: Remove file.
	* doublest.h: Remove file.
	* dfp.c: Remove file.
	* dfp.h: Remove file.

	* target-float.c: Do not include "doublest.h" and "dfp.h".
	(DOUBLEST): Move here from doublest.h.
	(enum float_kind): Likewise.
	(FLOATFORMAT_CHAR_BIT): Likewise.
	(FLOATFORMAT_LARGEST_BYTES): Likewise.
	(floatformat_totalsize_bytes): Move here from doublest.c.  Make static.
	(floatformat_precision): Likewise.
	(floatformat_normalize_byteorder, get_field, put_field): Likewise.
	(floatformat_is_negative, floatformat_classify, floatformat_mantissa):
	Likewise.
	(host_float_format, host_double_format, host_long_double_format):
	Likewise.
	(floatformat_to_string, floatformat_from_string): Likewise.
	(floatformat_to_doublest): Likewise.  Also, inline the original
	convert_floatformat_to_doublest.
	(floatformat_from_doublest): Likewise.  Also, inline the original
	convert_floatformat_from_doublest.

	Include "dpd/decimal128.h", "dpd/decimal64.h", and "dpd/decimal32.h".
	(MAX_DECIMAL_STRING): Move here from dfp.c.
	(match_endianness): Likewise.
	(set_decnumber_context, decimal_check_errors): Likewise.
	(decimal_from_number, decimal_to_number): Likewise.
	(decimal_to_string, decimal_from_string): Likewise.  Make static.
	(decimal_from_longest, decimal_from_ulongest): Likewise.
	(decimal_to_longest): Likewise.
	(decimal_binop, decimal_is_zero, decimal_compare): Likewise.
	(decimal_convert): Likewise.
2017-11-06 16:04:03 +01:00
Ulrich Weigand 14ad931172 Target FP: Handle interfaces to scripting languages
The last remaing use for DOUBLEST is in the code that interfaces to the
scripting languages (Python and Guile).  The problem here is that we
expose interfaces to convert a GDB value to and from native values of
floating-point type in those languages, and those by definition use
the host floating-point format.

While we cannot completely eliminate conversions to/from the host
floating-point format here, we still need to get rid of the uses
of value_as_double / value_from_double, since those will go away.

This patch implements two new target-float.c routine:
 - target_float_to_host_double
 - target_float_from_host_double
which convert to/from a host "double".  Those should only ever be
used where a host "double" is mandated by an external interface.

gdb/ChangeLog:
2017-11-06  Ulrich Weigand  <uweigand@de.ibm.com>

	* target-float.c (floatformat_to_host_double): New function.
	(floatformat_from_host_double): Likewise.
	(target_float_to_host_double): Likewise.
	(target_float_from_host_double): Likewise.
	* target-float.h (target_float_to_host_double): Add prototype.
	(target_float_from_host_double): Likewise.

	* guile/scm-value.c: Include "target-float.h".
	(gdbscm_value_to_real): Use target_float_to_host_double.
	Handle integer source values via value_as_long.
	* guile/scm-math.c: Include "target-float.h".  Do not include
	"doublest.h", "dfp.h", and "expression.h".
	(vlscm_convert_typed_number): Use target_float_from_host_double.
	(vlscm_convert_number): Likewise.

	* python/py-value.c (valpy_float): Use target_float_to_host_double.
	(convert_value_from_python): Use target_float_from_host_double.
2017-11-06 16:00:47 +01:00
Ulrich Weigand 66c02b9ed1 Target FP: Add binop and compare routines to target-float.{c,h}
This patch adds the following target floating-point routines:
 - target_float_binop
 - target_float_compare
which call the equivalent decimal_ routines to handle decimal FP,
and call helper routines that currently still go via DOUBLEST to
handle binary FP (derived from current valarith.c code).

These routines are used to handle both binary and decimal FP types
in scalar_binop, value_equal, and value_less, mostly following the
method currently used for decimal FP.  The existing value_args_as_decimal
helper is renamed to value_args_as_target_float and extended to handle
both binary and decimal types.

The unary operations value_pos and value_neg are also simplified,
the former by using a simple copy for all scalar types, the latter
by using value_binop (... BINOP_SUB) to implement negation as
subtraction from zero.

ChangeLog:
2017-11-06  Ulrich Weigand  <uweigand@de.ibm.com>

	* target-float.c: Include <math.h>.
	(floatformat_binop): New function.
	(floatformat_compare): Likewise.
	(target_float_binop): Likewise.
	(target_float_compare): Likewise.
	* target-float.h: Include "expression.h".
	(target_float_binop): Add prototype.
	(target_float_compare): Likewise.

	* valarith.c: Do not include "doublest.h" and "dfp.h".
	Include "common/byte-vector.h".
	(value_args_as_decimal): Remove, replace by ...
	(value_args_as_target_float): ... this function.  Handle both
	binary and decimal target floating-point formats.
	(scalar_binop): Handle both binary and decimal FP using
	value_args_as_target_float and target_float_binop.
	(value_equal): Handle both binary and decimal FP using
	value_args_as_target_float and target_float_compare.
	(value_less): Likewise.
	(value_pos): Handle all scalar types as simple copy.
	(value_neg): Handle all scalar types via BINOP_SUB from 0.
	* dfp.c (decimal_binop): Throw error instead of internal_error
	when called with an unsupported operation code.
2017-11-06 15:58:46 +01:00
Ulrich Weigand 50637b26f8 Target FP: Add conversion routines to target-float.{c,h}
This patch adds the following conversion routines:
 - target_float_to_longest
 - target_float_from_longest
 - target_float_from_ulongest
 - target_float_convert
which call the equivalent decimal_ routines to handle decimal FP,
and call helper routines that currently still go via DOUBLEST to
handle binary FP.

The target_float_convert routine not only handles BFP<->BFP and
DFP<->DFP conversions, but also BFP<->DFP, which are implemented
by converting to a string and back.

These helpers are used in particular to implement conversion
from and to FP in value_cast, without going through DOUBLEST there.
In order to implement this for the FP<-integer case, the
pack_long / pack_unsigned_long routines are extended to support
floating-point values as output (thereby allowing use of
value_from_[u]longest with a floating-point target type).

This latter change also allows simplification of value_one.

gdb/ChangeLog:
2017-11-06  Ulrich Weigand  <uweigand@de.ibm.com>

	* target-float.c (floatformat_to_longest): New function.
	(floatformat_from_longest, floatformat_from_ulongest): Likewise.
	(floatformat_convert): Likewise.
	(target_float_to_longest): Likewise.
	(target_float_from_longest, target_float_from_ulongest): Likewise.
	(target_float_convert): Likewise.
	* target-float.h (target_float_to_longest): Add prototype.
	(target_float_from_longest, target_float_from_ulongest): Likewise.
	(target_float_convert): Likewise.

	* value.c (unpack_long): Use target_float_to_longest.
	(pack_long): Allow FP types.  Use target_float_from_longest.
	(pack_unsigned_long): Likewise using target_float_from_ulongest.
	* valops.c: Include "target-float.h".  Do not include "dfp.h".
	(value_cast): Handle conversions to FP using target_float_convert,
	value_from_ulongest, and value_from_longest.
	(value_one): Use value_from_longest for FP types as well.
2017-11-06 15:57:31 +01:00
Ulrich Weigand f69fdf9bca Target FP: Add string routines to target-float.{c,h}
This adds target_float_to_string and target_float_from_string,
which dispatch to the corresponding floatformat_ or decimal_ routines.

Existing users of those routines are changed to use the new
target-float routines instead (most of those places already handle
both binary and decimal FP).

In addition, two other places are changes to use target_float_from_string:

- define_symbol in stabsread.c, when parsing a floating-point literal
  from stabs debug info

- gdbarch-selftest.c when initializing a target format values (to
  eliminate use of DOUBLEST there).

gdb/ChangeLog:
2017-11-06  Ulrich Weigand  <uweigand@de.ibm.com>

	* target-float.c (target_float_to_string): New function.
	(target_float_from_string): New function.
	* target-float.h (target_float_to_string): Add prototype.
	(target_float_from_string): Add prototype.

	* valprint.c: Include "target-float.h".  Do not include
	"doublest.h" and "dfp.h".
	(print_floating): Use target_float_to_string.
	* printcmd.c: Include "target-float.h".  Do not include "dfp.h".
	(printf_floating): Use target_float_to_string.
	* i387-tdep.c: Include "target-float.h".  Do not include "doublest.h".
	(print_i387_value): Use target_float_to_string.
	* mips-tdep.c: Include "target-float.h".
	(mips_print_fp_register): Use target_float_to_string.
	* sh64-tdep.c: Include "target-float.h".
	(sh64_do_fp_register): Use target_float_to_string.

	* parse.c: Include "target-float.h".  Do not include
	"doublest.h" and "dfp.h".
	(parse_float): Use target_float_from_string.
	* stabsread.c: Include "target-float.h".  Do not include "doublest.h".
	(define_symbol): Use target_float_from_string.
	* gdbarch-selftests.c: Include "target-float.h".
	(register_to_value_test): Use target_float_from_string.
2017-11-06 15:56:35 +01:00
Ulrich Weigand 701000146a Target FP: Introduce target-float.{c,h}
This patch introduces the new set of target floating-point handling routines
in target-float.{c,h}.  In the end, the intention is that this file will
contain support for all operations in target FP format, fully replacing
both the current doublest.{c,h} and dfp.{c,h}.

To begin with, this patch only adds a target_float_is_zero routine,
which handles the equivalent of decimal_is_zero for both binary and
decimal FP.  For the binary case, to avoid conversion to DOUBLEST,
this is implemented using the floatformat_classify routine.

However, it turns out that floatformat_classify actually has a bug
(it was not used to check for zero before), so this is fixed as well.

The new routine is used in both value_logical_not and valpy_nonzero.

There is one extra twist: the code previously used value_as_double
to convert to DOUBLEST and then compare against zero.  That routine
performs an extra task: it detects invalid floating-point values
and raises an error.  In any place where value_as_double is removed
in favor of some target-float.c routine, we need to replace that check.

To keep this check centralized in one place, I've added a new routine
is_floating_value, which returns a boolean determining whether a
value's type is floating point (binary or decimal), and if so, also
performs the validity check.  Since we need to check whether a value
is FP before calling any of the target-float routines anyway, this
seems a good place to add the check without much code size overhead.

In some places where we only want to check for floating-point types
and not perform a validity check (e.g. for the *output* of an operation),
we can use the new is_floating_type routine (in gdbarch) instead.

The validity check itself is done by a new target_float_is_valid
routine in target-float, encapsulating floatformat_is_valid.

ChangeLog:
2017-11-06  Ulrich Weigand  <uweigand@de.ibm.com>

	* Makefile.c (SFILES): Add target-float.c.
	(HFILES_NO_SRCDIR): Add target-float.h.
	(COMMON_OBS): Add target-float.o.
	* target-float.h: New file.
	* target-float.c: New file.

	* doublest.c (floatformat_classify): Fix detection of float_zero.

	* gdbtypes.c (is_floating_type): New function.
	* gdbtypes.h (is_floating_type): Add prototype.

	* value.c: Do not include "floatformat.h".
	(unpack_double): Use target_float_is_valid.
	(is_floating_value): New function.
	* value.h (is_floating_value): Add prototype-

	* valarith.c: Include "target-float.h".
	(value_logical_not): Use target_float_is_zero.

	* python/py-value.c: Include "target-float.h".
	(valpy_nonzero): Use target_float_is_zero.
2017-11-06 15:56:02 +01:00