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