opcodes/
[binutils-gdb.git] / libdecnumber / bid / decimal128.c
blob1daf5df1913db028a2a0e9587fe753edc5291c78
1 /* Copyright (C) 2007
2 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
21 /* As a special exception, if you link this library with other files,
22 some of which are compiled with GCC, to produce an executable,
23 this library does not by itself cause the resulting executable
24 to be covered by the GNU General Public License.
25 This exception does not however invalidate any other reasons why
26 the executable file might be covered by the GNU General Public License. */
28 #define decimal128FromString __dpd128FromString
29 #define decimal128ToString __dpd128ToString
30 #define decimal128ToEngString __dpd128ToEngString
31 #define decimal128FromNumber __dpd128FromNumber
32 #define decimal128ToNumber __dpd128ToNumber
34 #include "dpd/decimal128.c"
36 #undef decimal128FromString
37 #undef decimal128ToString
38 #undef decimal128ToEngString
39 #undef decimal128FromNumber
40 #undef decimal128ToNumber
42 #include "bid-dpd.h"
44 #ifdef IN_LIBGCC2
45 #define decimal128FromString __decimal128FromString
46 #define decimal128ToString __decimal128ToString
47 #define decimal128ToEngString __decimal128ToEngString
48 #define decimal128FromNumber __decimal128FromNumber
49 #define decimal128ToNumber __decimal128ToNumber
50 #endif
52 decimal128 *decimal128FromString (decimal128 *, const char *, decContext *);
53 char *decimal128ToString (const decimal128 *, char *);
54 char *decimal128ToEngString (const decimal128 *, char *);
55 decimal128 *decimal128FromNumber (decimal128 *, const decNumber *, decContext *);
56 decNumber *decimal128ToNumber (const decimal128 *, decNumber *);
58 void __host_to_ieee_128 (_Decimal128 in, decimal128 *out);
59 void __ieee_to_host_128 (decimal128 in, _Decimal128 *out);
61 decimal128 *
62 decimal128FromNumber (decimal128 *d128, const decNumber *dn,
63 decContext *set)
65 /* decimal128 and _Decimal128 are different types. */
66 union
68 _Decimal128 _Dec;
69 decimal128 dec;
70 } u;
72 __dpd128FromNumber (d128, dn, set);
74 /* __dpd128FromNumber returns in big endian. But _dpd_to_bid128 takes
75 host endian. */
76 __ieee_to_host_128 (*d128, &u._Dec);
78 /* Convert DPD to BID. */
79 _dpd_to_bid128 (&u._Dec, &u._Dec);
81 /* dfp.c is in bid endian. */
82 __host_to_ieee_128 (u._Dec, &u.dec);
84 /* d128 is returned as a pointer to _Decimal128 here. */
85 *d128 = u.dec;
87 return d128;
90 decNumber *
91 decimal128ToNumber (const decimal128 *bid128, decNumber *dn)
93 /* decimal128 and _Decimal128 are different types. */
94 union
96 _Decimal128 _Dec;
97 decimal128 dec;
98 } u;
100 /* bid128 is a pointer to _Decimal128 in bid endian. But _bid_to_dpd128
101 takes host endian. */
102 __ieee_to_host_128 (*bid128, &u._Dec);
104 /* Convert BID to DPD. */
105 _bid_to_dpd128 (&u._Dec, &u._Dec);
107 /* __dpd128ToNumber is in bid endian. */
108 __host_to_ieee_128 (u._Dec, &u.dec);
110 return __dpd128ToNumber (&u.dec, dn);
113 char *
114 decimal128ToString (const decimal128 *d128, char *string)
116 decNumber dn; /* work */
117 decimal128ToNumber (d128, &dn);
118 decNumberToString (&dn, string);
119 return string;
122 char *
123 decimal128ToEngString (const decimal128 *d128, char *string)
125 decNumber dn; /* work */
126 decimal128ToNumber (d128, &dn);
127 decNumberToEngString (&dn, string);
128 return string;
131 decimal128 *
132 decimal128FromString (decimal128 *result, const char *string,
133 decContext *set)
135 decContext dc; /* work */
136 decNumber dn; /* .. */
138 decContextDefault (&dc, DEC_INIT_DECIMAL128); /* no traps, please */
139 dc.round = set->round; /* use supplied rounding */
141 decNumberFromString (&dn, string, &dc); /* will round if needed */
142 decimal128FromNumber (result, &dn, &dc);
143 if (dc.status != 0)
144 { /* something happened */
145 decContextSetStatus (set, dc.status); /* .. pass it on */
147 return result;