libstdc++: Use if-constexpr in std::__try_use_facet [PR113099]
[official-gcc.git] / libdecnumber / bid / decimal32.c
blobc0de01709b3a42611a751ac9bf4e44bb024cdaa3
1 /* Copyright (C) 2007-2024 Free Software Foundation, Inc.
3 This file is part of GCC.
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
8 version.
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 for more details.
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
24 #define decimal32FromString __dpd32FromString
25 #define decimal32ToString __dpd32ToString
26 #define decimal32ToEngString __dpd32ToEngString
27 #define decimal32FromNumber __dpd32FromNumber
28 #define decimal32ToNumber __dpd32ToNumber
30 #include "dpd/decimal32.c"
32 #undef decimal32FromString
33 #undef decimal32ToString
34 #undef decimal32ToEngString
35 #undef decimal32FromNumber
36 #undef decimal32ToNumber
38 #include "bid-dpd.h"
40 #ifdef IN_LIBGCC2
41 #define decimal32FromString __decimal32FromString
42 #define decimal32ToString __decimal32ToString
43 #define decimal32ToEngString __decimal32ToEngString
44 #define decimal32FromNumber __decimal32FromNumber
45 #define decimal32ToNumber __decimal32ToNumber
46 #endif
48 decimal32 *decimal32FromString (decimal32 *, const char *, decContext *);
49 char *decimal32ToString (const decimal32 *, char *);
50 char *decimal32ToEngString (const decimal32 *, char *);
51 decimal32 *decimal32FromNumber (decimal32 *, const decNumber *, decContext *);
52 decNumber *decimal32ToNumber (const decimal32 *, decNumber *);
54 void __host_to_ieee_32 (_Decimal32 in, decimal32 *out);
55 void __ieee_to_host_32 (decimal32 in, _Decimal32 *out);
57 decimal32 *
58 decimal32FromNumber (decimal32 *d32, const decNumber *dn,
59 decContext *set)
61 /* decimal32 and _Decimal32 are different types. */
62 union
64 _Decimal32 _Dec;
65 decimal32 dec;
66 } u;
68 __dpd32FromNumber (d32, dn, set);
70 /* __dpd32FromNumber returns in big endian. But _dpd_to_bid32 takes
71 host endian. */
72 __ieee_to_host_32 (*d32, &u._Dec);
74 /* Convert DPD to BID. */
75 _dpd_to_bid32 (&u._Dec, &u._Dec);
77 /* dfp.c is in bid endian. */
78 __host_to_ieee_32 (u._Dec, &u.dec);
80 /* d32 is returned as a pointer to _Decimal32 here. */
81 *d32 = u.dec;
83 return d32;
86 decNumber *
87 decimal32ToNumber (const decimal32 *bid32, decNumber *dn)
89 /* decimal32 and _Decimal32 are different types. */
90 union
92 _Decimal32 _Dec;
93 decimal32 dec;
94 } u;
96 /* bid32 is a pointer to _Decimal32 in bid endian. But _bid_to_dpd32
97 takes host endian. */
98 __ieee_to_host_32 (*bid32, &u._Dec);
100 /* Convert BID to DPD. */
101 _bid_to_dpd32 (&u._Dec, &u._Dec);
103 /* __dpd32ToNumber is in bid endian. */
104 __host_to_ieee_32 (u._Dec, &u.dec);
106 return __dpd32ToNumber (&u.dec, dn);
109 char *
110 decimal32ToString (const decimal32 *d32, char *string)
112 decNumber dn; /* work */
113 decimal32ToNumber (d32, &dn);
114 decNumberToString (&dn, string);
115 return string;
118 char *
119 decimal32ToEngString (const decimal32 *d32, char *string)
121 decNumber dn; /* work */
122 decimal32ToNumber (d32, &dn);
123 decNumberToEngString (&dn, string);
124 return string;
127 decimal32 *
128 decimal32FromString (decimal32 *result, const char *string,
129 decContext *set)
131 decContext dc; /* work */
132 decNumber dn; /* .. */
134 decContextDefault (&dc, DEC_INIT_DECIMAL32); /* no traps, please */
135 dc.round = set->round; /* use supplied rounding */
137 decNumberFromString (&dn, string, &dc); /* will round if needed */
138 decimal32FromNumber (result, &dn, &dc);
139 if (dc.status != 0)
140 { /* something happened */
141 decContextSetStatus (set, dc.status); /* .. pass it on */
143 return result;