1/* Copyright (C) 2007-2024 Free Software Foundation, Inc.
2
3This file is part of GCC.
4
5GCC is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free
7Software Foundation; either version 3, or (at your option) any later
8version.
9
10GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or
12FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13for more details.
14
15Under Section 7 of GPL version 3, you are granted additional
16permissions described in the GCC Runtime Library Exception, version
173.1, as published by the Free Software Foundation.
18
19You should have received a copy of the GNU General Public License and
20a copy of the GCC Runtime Library Exception along with this program;
21see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22<http://www.gnu.org/licenses/>. */
23
24#define decimal64FromString __dpd64FromString
25#define decimal64ToString __dpd64ToString
26#define decimal64ToEngString __dpd64ToEngString
27#define decimal64FromNumber __dpd64FromNumber
28#define decimal64ToNumber __dpd64ToNumber
29
30#include "dpd/decimal64.c"
31
32#undef decimal64FromString
33#undef decimal64ToString
34#undef decimal64ToEngString
35#undef decimal64FromNumber
36#undef decimal64ToNumber
37
38#include "bid-dpd.h"
39
40#ifdef IN_LIBGCC2
41#define decimal64FromString __decimal64FromString
42#define decimal64ToString __decimal64ToString
43#define decimal64ToEngString __decimal64ToEngString
44#define decimal64FromNumber __decimal64FromNumber
45#define decimal64ToNumber __decimal64ToNumber
46#endif
47
48decimal64 *decimal64FromString (decimal64 *, const char *, decContext *);
49char *decimal64ToString (const decimal64 *, char *);
50char *decimal64ToEngString (const decimal64 *, char *);
51decimal64 *decimal64FromNumber (decimal64 *, const decNumber *, decContext *);
52decNumber *decimal64ToNumber (const decimal64 *, decNumber *);
53
54void __host_to_ieee_64 (_Decimal64 in, decimal64 *out);
55void __ieee_to_host_64 (decimal64 in, _Decimal64 *out);
56
57decimal64 *
58decimal64FromNumber (decimal64 *d64, const decNumber *dn,
59 decContext *set)
60{
61 /* decimal64 and _Decimal64 are different types. */
62 union
63 {
64 _Decimal64 _Dec;
65 decimal64 dec;
66 } u;
67
68 __dpd64FromNumber (d64, dn, set);
69
70 /* __dpd64FromNumber returns in big endian. But _dpd_to_bid64 takes
71 host endian. */
72 __ieee_to_host_64 (in: *d64, out: &u._Dec);
73
74 /* Convert DPD to BID. */
75 _dpd_to_bid64 (&u._Dec, &u._Dec);
76
77 /* dfp.c is in bid endian. */
78 __host_to_ieee_64 (in: u._Dec, out: &u.dec);
79
80 /* d64 is returned as a pointer to _Decimal64 here. */
81 *d64 = u.dec;
82
83 return d64;
84}
85
86decNumber *
87decimal64ToNumber (const decimal64 *bid64, decNumber *dn)
88{
89 /* decimal64 and _Decimal64 are different types. */
90 union
91 {
92 _Decimal64 _Dec;
93 decimal64 dec;
94 } u;
95
96 /* bid64 is a pointer to _Decimal64 in bid endian. But _bid_to_dpd64
97 takes host endian. */
98 __ieee_to_host_64 (in: *bid64, out: &u._Dec);
99
100 /* Convert BID to DPD. */
101 _bid_to_dpd64 (&u._Dec, &u._Dec);
102
103 /* __dpd64ToNumber is in bid endian. */
104 __host_to_ieee_64 (in: u._Dec, out: &u.dec);
105
106 return __dpd64ToNumber (d64: &u.dec, dn);
107}
108
109char *
110decimal64ToString (const decimal64 *d64, char *string)
111{
112 decNumber dn; /* work */
113 decimal64ToNumber (bid64: d64, dn: &dn);
114 decNumberToString (&dn, string);
115 return string;
116}
117
118char *
119decimal64ToEngString (const decimal64 *d64, char *string)
120{
121 decNumber dn; /* work */
122 decimal64ToNumber (bid64: d64, dn: &dn);
123 decNumberToEngString (&dn, string);
124 return string;
125}
126
127decimal64 *
128decimal64FromString (decimal64 *result, const char *string,
129 decContext *set)
130{
131 decContext dc; /* work */
132 decNumber dn; /* .. */
133
134 decContextDefault (&dc, DEC_INIT_DECIMAL64); /* no traps, please */
135 dc.round = set->round; /* use supplied rounding */
136
137 decNumberFromString (&dn, string, &dc); /* will round if needed */
138 decimal64FromNumber (d64: result, dn: &dn, set: &dc);
139 if (dc.status != 0)
140 { /* something happened */
141 decContextSetStatus (set, dc.status); /* .. pass it on */
142 }
143 return result;
144}
145

source code of libdecnumber/bid/decimal64.c