e90cf03786803e1f24d5bd64a6b2bbb4f2925c65
[binutils-gdb.git] / gdb / target-float.c
1 /* Floating point routines for GDB, the GNU debugger.
2
3 Copyright (C) 2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "dfp.h"
22 #include "doublest.h"
23 #include "gdbtypes.h"
24 #include "floatformat.h"
25 #include "target-float.h"
26
27
28 /* Typed floating-point routines. These routines operate on floating-point
29 values in target format, represented by a byte buffer interpreted as a
30 "struct type", which may be either a binary or decimal floating-point
31 type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT). */
32
33 /* Return whether the byte-stream ADDR holds a valid value of
34 floating-point type TYPE. */
35 bool
36 target_float_is_valid (const gdb_byte *addr, const struct type *type)
37 {
38 if (TYPE_CODE (type) == TYPE_CODE_FLT)
39 return floatformat_is_valid (floatformat_from_type (type), addr);
40
41 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
42 return true;
43
44 gdb_assert_not_reached ("unexpected type code");
45 }
46
47 /* Return whether the byte-stream ADDR, interpreted as floating-point
48 type TYPE, is numerically equal to zero (of either sign). */
49 bool
50 target_float_is_zero (const gdb_byte *addr, const struct type *type)
51 {
52 if (TYPE_CODE (type) == TYPE_CODE_FLT)
53 return (floatformat_classify (floatformat_from_type (type), addr)
54 == float_zero);
55
56 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
57 return decimal_is_zero (addr, TYPE_LENGTH (type),
58 gdbarch_byte_order (get_type_arch (type)));
59
60 gdb_assert_not_reached ("unexpected type code");
61 }
62
63 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
64 to a string, optionally using the print format FORMAT. */
65 std::string
66 target_float_to_string (const gdb_byte *addr, const struct type *type,
67 const char *format)
68 {
69 if (TYPE_CODE (type) == TYPE_CODE_FLT)
70 return floatformat_to_string (floatformat_from_type (type), addr, format);
71
72 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
73 return decimal_to_string (addr, TYPE_LENGTH (type),
74 gdbarch_byte_order (get_type_arch (type)),
75 format);
76
77 gdb_assert_not_reached ("unexpected type code");
78 }
79
80 /* Parse string STRING into a target floating-number of type TYPE and
81 store it as byte-stream ADDR. Return whether parsing succeeded. */
82 bool
83 target_float_from_string (gdb_byte *addr, const struct type *type,
84 const std::string &string)
85 {
86 /* Ensure possible padding bytes in the target buffer are zeroed out. */
87 memset (addr, 0, TYPE_LENGTH (type));
88
89 if (TYPE_CODE (type) == TYPE_CODE_FLT)
90 return floatformat_from_string (floatformat_from_type (type), addr,
91 string);
92
93 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
94 return decimal_from_string (addr, TYPE_LENGTH (type),
95 gdbarch_byte_order (get_type_arch (type)),
96 string);
97
98 gdb_assert_not_reached ("unexpected type code");
99 }