* src-release (GDB_SUPPORT_DIRS): Add libdecnumber.
[deliverable/binutils-gdb.git] / libdecnumber / bid / decimal64.c
CommitLineData
f5bc1778
DJ
1/* Copyright (C) 2007
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 2, 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
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING. If not, write to the Free
18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA. */
20
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. */
27
28#define decimal64FromString __dpd64FromString
29#define decimal64ToString __dpd64ToString
30#define decimal64ToEngString __dpd64ToEngString
31#define decimal64FromNumber __dpd64FromNumber
32#define decimal64ToNumber __dpd64ToNumber
33
34#include "dpd/decimal64.c"
35
36#undef decimal64FromString
37#undef decimal64ToString
38#undef decimal64ToEngString
39#undef decimal64FromNumber
40#undef decimal64ToNumber
41
42#include "bid-dpd.h"
43
44#ifdef IN_LIBGCC2
45#define decimal64FromString __decimal64FromString
46#define decimal64ToString __decimal64ToString
47#define decimal64ToEngString __decimal64ToEngString
48#define decimal64FromNumber __decimal64FromNumber
49#define decimal64ToNumber __decimal64ToNumber
50#endif
51
52decimal64 *decimal64FromString (decimal64 *, const char *, decContext *);
53char *decimal64ToString (const decimal64 *, char *);
54char *decimal64ToEngString (const decimal64 *, char *);
55decimal64 *decimal64FromNumber (decimal64 *, const decNumber *, decContext *);
56decNumber *decimal64ToNumber (const decimal64 *, decNumber *);
57
58void __host_to_ieee_64 (_Decimal64 in, decimal64 *out);
59void __ieee_to_host_64 (decimal64 in, _Decimal64 *out);
60
61decimal64 *
62decimal64FromNumber (decimal64 *d64, const decNumber *dn,
63 decContext *set)
64{
65 /* decimal64 and _Decimal64 are different types. */
66 union
67 {
68 _Decimal64 _Dec;
69 decimal64 dec;
70 } u;
71
72 __dpd64FromNumber (d64, dn, set);
73
74 /* __dpd64FromNumber returns in big endian. But _dpd_to_bid64 takes
75 host endian. */
76 __ieee_to_host_64 (*d64, &u._Dec);
77
78 /* Convert DPD to BID. */
79 _dpd_to_bid64 (&u._Dec, &u._Dec);
80
81 /* dfp.c is in bid endian. */
82 __host_to_ieee_64 (u._Dec, &u.dec);
83
84 /* d64 is returned as a pointer to _Decimal64 here. */
85 *d64 = u.dec;
86
87 return d64;
88}
89
90decNumber *
91decimal64ToNumber (const decimal64 *bid64, decNumber *dn)
92{
93 /* decimal64 and _Decimal64 are different types. */
94 union
95 {
96 _Decimal64 _Dec;
97 decimal64 dec;
98 } u;
99
100 /* bid64 is a pointer to _Decimal64 in bid endian. But _bid_to_dpd64
101 takes host endian. */
102 __ieee_to_host_64 (*bid64, &u._Dec);
103
104 /* Convert BID to DPD. */
105 _bid_to_dpd64 (&u._Dec, &u._Dec);
106
107 /* __dpd64ToNumber is in bid endian. */
108 __host_to_ieee_64 (u._Dec, &u.dec);
109
110 return __dpd64ToNumber (&u.dec, dn);
111}
112
113char *
114decimal64ToString (const decimal64 *d64, char *string)
115{
116 decNumber dn; /* work */
117 decimal64ToNumber (d64, &dn);
118 decNumberToString (&dn, string);
119 return string;
120}
121
122char *
123decimal64ToEngString (const decimal64 *d64, char *string)
124{
125 decNumber dn; /* work */
126 decimal64ToNumber (d64, &dn);
127 decNumberToEngString (&dn, string);
128 return string;
129}
130
131decimal64 *
132decimal64FromString (decimal64 *result, const char *string,
133 decContext *set)
134{
135 decContext dc; /* work */
136 decNumber dn; /* .. */
137
138 decContextDefault (&dc, DEC_INIT_DECIMAL64); /* no traps, please */
139 dc.round = set->round; /* use supplied rounding */
140
141 decNumberFromString (&dn, string, &dc); /* will round if needed */
142 decimal64FromNumber (result, &dn, &dc);
143 if (dc.status != 0)
144 { /* something happened */
145 decContextSetStatus (set, dc.status); /* .. pass it on */
146 }
147 return result;
148}
This page took 0.036242 seconds and 4 git commands to generate.