[ARM] PR ld/21402, only override the symbol dynamic decision on undefined weak symbol.
[deliverable/binutils-gdb.git] / gdb / dfp.c
CommitLineData
9b913628
TJB
1/* Decimal floating point support for GDB.
2
61baf725 3 Copyright (C) 2007-2017 Free Software Foundation, Inc.
9b913628
TJB
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"
4ef30785 21#include "expression.h"
4ef30785 22#include "dfp.h"
9b913628
TJB
23
24/* The order of the following headers is important for making sure
25 decNumber structure is large enough to hold decimal128 digits. */
26
27#include "dpd/decimal128.h"
28#include "dpd/decimal64.h"
29#include "dpd/decimal32.h"
30
3b4b2f16
UW
31/* When using decimal128, this is the maximum string length + 1
32 (value comes from libdecnumber's DECIMAL128_String constant). */
33#define MAX_DECIMAL_STRING 43
34
9b913628
TJB
35/* In GDB, we are using an array of gdb_byte to represent decimal values.
36 They are stored in host byte order. This routine does the conversion if
37 the target byte order is different. */
38static void
e17a4113
UW
39match_endianness (const gdb_byte *from, int len, enum bfd_endian byte_order,
40 gdb_byte *to)
9b913628
TJB
41{
42 int i;
43
44#if WORDS_BIGENDIAN
45#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE
46#else
47#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG
48#endif
49
e17a4113 50 if (byte_order == OPPOSITE_BYTE_ORDER)
9b913628
TJB
51 for (i = 0; i < len; i++)
52 to[i] = from[len - i - 1];
53 else
54 for (i = 0; i < len; i++)
55 to[i] = from[i];
56
57 return;
58}
59
4ef30785
TJB
60/* Helper function to get the appropriate libdecnumber context for each size
61 of decimal float. */
62static void
63set_decnumber_context (decContext *ctx, int len)
64{
65 switch (len)
66 {
67 case 4:
68 decContextDefault (ctx, DEC_INIT_DECIMAL32);
69 break;
70 case 8:
71 decContextDefault (ctx, DEC_INIT_DECIMAL64);
72 break;
73 case 16:
74 decContextDefault (ctx, DEC_INIT_DECIMAL128);
75 break;
76 }
77
78 ctx->traps = 0;
79}
80
81/* Check for errors signaled in the decimal context structure. */
82static void
83decimal_check_errors (decContext *ctx)
84{
85 /* An error here could be a division by zero, an overflow, an underflow or
86 an invalid operation (from the DEC_Errors constant in decContext.h).
87 Since GDB doesn't complain about division by zero, overflow or underflow
88 errors for binary floating, we won't complain about them for decimal
89 floating either. */
90 if (ctx->status & DEC_IEEE_854_Invalid_operation)
91 {
92 /* Leave only the error bits in the status flags. */
93 ctx->status &= DEC_IEEE_854_Invalid_operation;
3e43a32a
MS
94 error (_("Cannot perform operation: %s"),
95 decContextStatusToString (ctx));
4ef30785
TJB
96 }
97}
98
99/* Helper function to convert from libdecnumber's appropriate representation
100 for computation to each size of decimal float. */
101static void
102decimal_from_number (const decNumber *from, gdb_byte *to, int len)
103{
104 decContext set;
105
106 set_decnumber_context (&set, len);
107
108 switch (len)
109 {
110 case 4:
111 decimal32FromNumber ((decimal32 *) to, from, &set);
112 break;
113 case 8:
114 decimal64FromNumber ((decimal64 *) to, from, &set);
115 break;
116 case 16:
117 decimal128FromNumber ((decimal128 *) to, from, &set);
118 break;
119 }
120}
121
122/* Helper function to convert each size of decimal float to libdecnumber's
123 appropriate representation for computation. */
124static void
125decimal_to_number (const gdb_byte *from, int len, decNumber *to)
126{
127 switch (len)
128 {
129 case 4:
130 decimal32ToNumber ((decimal32 *) from, to);
131 break;
132 case 8:
133 decimal64ToNumber ((decimal64 *) from, to);
134 break;
135 case 16:
136 decimal128ToNumber ((decimal128 *) from, to);
137 break;
138 default:
b37520b6 139 error (_("Unknown decimal floating point type."));
4ef30785
TJB
140 break;
141 }
142}
143
9b913628
TJB
144/* Convert decimal type to its string representation. LEN is the length
145 of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and
146 16 bytes for decimal128. */
3b4b2f16 147std::string
e17a4113 148decimal_to_string (const gdb_byte *decbytes, int len,
3b4b2f16 149 enum bfd_endian byte_order)
9b913628
TJB
150{
151 gdb_byte dec[16];
152
e17a4113 153 match_endianness (decbytes, len, byte_order, dec);
4ef30785 154
3b4b2f16
UW
155 std::string result;
156 result.resize (MAX_DECIMAL_STRING);
157
9b913628
TJB
158 switch (len)
159 {
160 case 4:
3b4b2f16 161 decimal32ToString ((decimal32 *) dec, &result[0]);
9b913628
TJB
162 break;
163 case 8:
3b4b2f16 164 decimal64ToString ((decimal64 *) dec, &result[0]);
9b913628
TJB
165 break;
166 case 16:
3b4b2f16 167 decimal128ToString ((decimal128 *) dec, &result[0]);
9b913628
TJB
168 break;
169 default:
a4ae0ca1 170 error (_("Unknown decimal floating point type."));
9b913628
TJB
171 break;
172 }
3b4b2f16
UW
173
174 return result;
9b913628
TJB
175}
176
177/* Convert the string form of a decimal value to its decimal representation.
178 LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for
179 decimal64 and 16 bytes for decimal128. */
3b4b2f16 180bool
e17a4113 181decimal_from_string (gdb_byte *decbytes, int len, enum bfd_endian byte_order,
3b4b2f16 182 std::string string)
9b913628
TJB
183{
184 decContext set;
185 gdb_byte dec[16];
186
4ef30785
TJB
187 set_decnumber_context (&set, len);
188
9b913628
TJB
189 switch (len)
190 {
191 case 4:
3b4b2f16 192 decimal32FromString ((decimal32 *) dec, string.c_str (), &set);
9b913628
TJB
193 break;
194 case 8:
3b4b2f16 195 decimal64FromString ((decimal64 *) dec, string.c_str (), &set);
9b913628
TJB
196 break;
197 case 16:
3b4b2f16 198 decimal128FromString ((decimal128 *) dec, string.c_str (), &set);
9b913628
TJB
199 break;
200 default:
a4ae0ca1 201 error (_("Unknown decimal floating point type."));
9b913628
TJB
202 break;
203 }
204
e17a4113 205 match_endianness (dec, len, byte_order, decbytes);
9b913628 206
4ef30785
TJB
207 /* Check for errors in the DFP operation. */
208 decimal_check_errors (&set);
209
3b4b2f16 210 return true;
9b913628 211}
4ef30785 212
3b4b2f16 213/* Converts a LONGEST to a decimal float of specified LEN bytes. */
4ef30785 214void
3b4b2f16
UW
215decimal_from_longest (LONGEST from,
216 gdb_byte *to, int len, enum bfd_endian byte_order)
4ef30785 217{
4ef30785
TJB
218 gdb_byte dec[16];
219 decNumber number;
3b4b2f16
UW
220 if ((int32_t) from != from)
221 /* libdecnumber can convert only 32-bit integers. */
222 error (_("Conversion of large integer to a "
223 "decimal floating type is not supported."));
4ef30785 224
3b4b2f16 225 decNumberFromInt32 (&number, (int32_t) from);
4ef30785 226
3b4b2f16
UW
227 decimal_from_number (&number, dec, len);
228 match_endianness (dec, len, byte_order, to);
229}
230
231/* Converts a ULONGEST to a decimal float of specified LEN bytes. */
232void
233decimal_from_ulongest (ULONGEST from,
234 gdb_byte *to, int len, enum bfd_endian byte_order)
235{
236 gdb_byte dec[16];
237 decNumber number;
238
239 if ((uint32_t) from != from)
4ef30785 240 /* libdecnumber can convert only 32-bit integers. */
3e43a32a
MS
241 error (_("Conversion of large integer to a "
242 "decimal floating type is not supported."));
4ef30785 243
3b4b2f16 244 decNumberFromUInt32 (&number, (uint32_t) from);
4ef30785
TJB
245
246 decimal_from_number (&number, dec, len);
e17a4113 247 match_endianness (dec, len, byte_order, to);
4ef30785
TJB
248}
249
3b4b2f16
UW
250/* Converts a decimal float of LEN bytes to a LONGEST. */
251LONGEST
252decimal_to_longest (const gdb_byte *from, int len, enum bfd_endian byte_order)
253{
254 /* libdecnumber has a function to convert from decimal to integer, but
255 it doesn't work when the decimal number has a fractional part. */
256 std::string str = decimal_to_string (from, len, byte_order);
257 return strtoll (str.c_str (), NULL, 10);
258}
259
4ef30785
TJB
260/* Converts a value of a float type to a decimal float of
261 specified LEN bytes.
262
263 This is an ugly way to do the conversion, but libdecnumber does
264 not offer a direct way to do it. */
265void
3b4b2f16 266decimal_from_doublest (DOUBLEST from,
e17a4113 267 gdb_byte *to, int len, enum bfd_endian byte_order)
4ef30785 268{
3b4b2f16
UW
269 std::string str = string_printf ("%.30" DOUBLEST_PRINT_FORMAT, from);
270 decimal_from_string (to, len, byte_order, str);
4ef30785
TJB
271}
272
273/* Converts a decimal float of LEN bytes to a double value. */
274DOUBLEST
e17a4113 275decimal_to_doublest (const gdb_byte *from, int len, enum bfd_endian byte_order)
4ef30785 276{
4ef30785
TJB
277 /* This is an ugly way to do the conversion, but libdecnumber does
278 not offer a direct way to do it. */
3b4b2f16
UW
279 std::string str = decimal_to_string (from, len, byte_order);
280 return strtod (str.c_str (), NULL);
4ef30785
TJB
281}
282
289bd67a 283/* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y
e17a4113
UW
284 and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
285 RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */
4ef30785 286void
e17a4113
UW
287decimal_binop (enum exp_opcode op,
288 const gdb_byte *x, int len_x, enum bfd_endian byte_order_x,
289 const gdb_byte *y, int len_y, enum bfd_endian byte_order_y,
290 gdb_byte *result, int len_result,
291 enum bfd_endian byte_order_result)
4ef30785
TJB
292{
293 decContext set;
294 decNumber number1, number2, number3;
295 gdb_byte dec1[16], dec2[16], dec3[16];
296
e17a4113
UW
297 match_endianness (x, len_x, byte_order_x, dec1);
298 match_endianness (y, len_y, byte_order_y, dec2);
4ef30785 299
289bd67a
UW
300 decimal_to_number (dec1, len_x, &number1);
301 decimal_to_number (dec2, len_y, &number2);
4ef30785 302
289bd67a 303 set_decnumber_context (&set, len_result);
4ef30785
TJB
304
305 switch (op)
306 {
307 case BINOP_ADD:
308 decNumberAdd (&number3, &number1, &number2, &set);
309 break;
310 case BINOP_SUB:
311 decNumberSubtract (&number3, &number1, &number2, &set);
312 break;
313 case BINOP_MUL:
314 decNumberMultiply (&number3, &number1, &number2, &set);
315 break;
316 case BINOP_DIV:
317 decNumberDivide (&number3, &number1, &number2, &set);
318 break;
319 case BINOP_EXP:
320 decNumberPower (&number3, &number1, &number2, &set);
321 break;
322 default:
323 internal_error (__FILE__, __LINE__,
324 _("Unknown decimal floating point operation."));
325 break;
326 }
327
328 /* Check for errors in the DFP operation. */
329 decimal_check_errors (&set);
330
289bd67a 331 decimal_from_number (&number3, dec3, len_result);
4ef30785 332
e17a4113 333 match_endianness (dec3, len_result, byte_order_result, result);
4ef30785
TJB
334}
335
336/* Returns true if X (which is LEN bytes wide) is the number zero. */
337int
e17a4113 338decimal_is_zero (const gdb_byte *x, int len, enum bfd_endian byte_order)
4ef30785
TJB
339{
340 decNumber number;
341 gdb_byte dec[16];
342
e17a4113 343 match_endianness (x, len, byte_order, dec);
4ef30785
TJB
344 decimal_to_number (dec, len, &number);
345
346 return decNumberIsZero (&number);
347}
348
349/* Compares two numbers numerically. If X is less than Y then the return value
350 will be -1. If they are equal, then the return value will be 0. If X is
351 greater than the Y then the return value will be 1. */
352int
e17a4113
UW
353decimal_compare (const gdb_byte *x, int len_x, enum bfd_endian byte_order_x,
354 const gdb_byte *y, int len_y, enum bfd_endian byte_order_y)
4ef30785
TJB
355{
356 decNumber number1, number2, result;
357 decContext set;
358 gdb_byte dec1[16], dec2[16];
359 int len_result;
360
e17a4113
UW
361 match_endianness (x, len_x, byte_order_x, dec1);
362 match_endianness (y, len_y, byte_order_y, dec2);
4ef30785 363
289bd67a
UW
364 decimal_to_number (dec1, len_x, &number1);
365 decimal_to_number (dec2, len_y, &number2);
4ef30785 366
289bd67a
UW
367 /* Perform the comparison in the larger of the two sizes. */
368 len_result = len_x > len_y ? len_x : len_y;
4ef30785
TJB
369 set_decnumber_context (&set, len_result);
370
371 decNumberCompare (&result, &number1, &number2, &set);
372
373 /* Check for errors in the DFP operation. */
374 decimal_check_errors (&set);
375
376 if (decNumberIsNaN (&result))
377 error (_("Comparison with an invalid number (NaN)."));
378 else if (decNumberIsZero (&result))
379 return 0;
380 else if (decNumberIsNegative (&result))
381 return -1;
382 else
383 return 1;
384}
385
386/* Convert a decimal value from a decimal type with LEN_FROM bytes to a
387 decimal type with LEN_TO bytes. */
388void
e17a4113
UW
389decimal_convert (const gdb_byte *from, int len_from,
390 enum bfd_endian byte_order_from, gdb_byte *to, int len_to,
391 enum bfd_endian byte_order_to)
4ef30785
TJB
392{
393 decNumber number;
cd0b43b1
JM
394 gdb_byte dec[16];
395
e17a4113 396 match_endianness (from, len_from, byte_order_from, dec);
cd0b43b1
JM
397
398 decimal_to_number (dec, len_from, &number);
399 decimal_from_number (&number, dec, len_to);
4ef30785 400
e17a4113 401 match_endianness (dec, len_to, byte_order_to, to);
4ef30785 402}
This page took 0.697727 seconds and 4 git commands to generate.