2012-12-20 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libdecnumber / bid / decimal128.c
blobdb30bd39d6dae375bdd1bea1845480a12cf5f1cc
1 /* Copyright (C) 2007, 2009
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 3, 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 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #define decimal128FromString __dpd128FromString
26 #define decimal128ToString __dpd128ToString
27 #define decimal128ToEngString __dpd128ToEngString
28 #define decimal128FromNumber __dpd128FromNumber
29 #define decimal128ToNumber __dpd128ToNumber
31 #include "dpd/decimal128.c"
33 #undef decimal128FromString
34 #undef decimal128ToString
35 #undef decimal128ToEngString
36 #undef decimal128FromNumber
37 #undef decimal128ToNumber
39 #include "bid-dpd.h"
41 #ifdef IN_LIBGCC2
42 #define decimal128FromString __decimal128FromString
43 #define decimal128ToString __decimal128ToString
44 #define decimal128ToEngString __decimal128ToEngString
45 #define decimal128FromNumber __decimal128FromNumber
46 #define decimal128ToNumber __decimal128ToNumber
47 #endif
49 decimal128 *decimal128FromString (decimal128 *, const char *, decContext *);
50 char *decimal128ToString (const decimal128 *, char *);
51 char *decimal128ToEngString (const decimal128 *, char *);
52 decimal128 *decimal128FromNumber (decimal128 *, const decNumber *, decContext *);
53 decNumber *decimal128ToNumber (const decimal128 *, decNumber *);
55 void __host_to_ieee_128 (_Decimal128 in, decimal128 *out);
56 void __ieee_to_host_128 (decimal128 in, _Decimal128 *out);
58 decimal128 *
59 decimal128FromNumber (decimal128 *d128, const decNumber *dn,
60 decContext *set)
62 /* decimal128 and _Decimal128 are different types. */
63 union
65 _Decimal128 _Dec;
66 decimal128 dec;
67 } u;
69 __dpd128FromNumber (d128, dn, set);
71 /* __dpd128FromNumber returns in big endian. But _dpd_to_bid128 takes
72 host endian. */
73 __ieee_to_host_128 (*d128, &u._Dec);
75 /* Convert DPD to BID. */
76 _dpd_to_bid128 (&u._Dec, &u._Dec);
78 /* dfp.c is in bid endian. */
79 __host_to_ieee_128 (u._Dec, &u.dec);
81 /* d128 is returned as a pointer to _Decimal128 here. */
82 *d128 = u.dec;
84 return d128;
87 decNumber *
88 decimal128ToNumber (const decimal128 *bid128, decNumber *dn)
90 /* decimal128 and _Decimal128 are different types. */
91 union
93 _Decimal128 _Dec;
94 decimal128 dec;
95 } u;
97 /* bid128 is a pointer to _Decimal128 in bid endian. But _bid_to_dpd128
98 takes host endian. */
99 __ieee_to_host_128 (*bid128, &u._Dec);
101 /* Convert BID to DPD. */
102 _bid_to_dpd128 (&u._Dec, &u._Dec);
104 /* __dpd128ToNumber is in bid endian. */
105 __host_to_ieee_128 (u._Dec, &u.dec);
107 return __dpd128ToNumber (&u.dec, dn);
110 char *
111 decimal128ToString (const decimal128 *d128, char *string)
113 decNumber dn; /* work */
114 decimal128ToNumber (d128, &dn);
115 decNumberToString (&dn, string);
116 return string;
119 char *
120 decimal128ToEngString (const decimal128 *d128, char *string)
122 decNumber dn; /* work */
123 decimal128ToNumber (d128, &dn);
124 decNumberToEngString (&dn, string);
125 return string;
128 decimal128 *
129 decimal128FromString (decimal128 *result, const char *string,
130 decContext *set)
132 decContext dc; /* work */
133 decNumber dn; /* .. */
135 decContextDefault (&dc, DEC_INIT_DECIMAL128); /* no traps, please */
136 dc.round = set->round; /* use supplied rounding */
138 decNumberFromString (&dn, string, &dc); /* will round if needed */
139 decimal128FromNumber (result, &dn, &dc);
140 if (dc.status != 0)
141 { /* something happened */
142 decContextSetStatus (set, dc.status); /* .. pass it on */
144 return result;