• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  This Software is provided under the Zope Public License (ZPL) Version 2.1.
3 
4  Copyright (c) 2009 - 2013 by the mingw-w64 project
5 
6  See the AUTHORS file for the list of contributors to the mingw-w64 project.
7 
8  This license has been certified as open source. It has also been designated
9  as GPL compatible by the Free Software Foundation (FSF).
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14    1. Redistributions in source code must retain the accompanying copyright
15       notice, this list of conditions, and the following disclaimer.
16    2. Redistributions in binary form must reproduce the accompanying
17       copyright notice, this list of conditions, and the following disclaimer
18       in the documentation and/or other materials provided with the
19       distribution.
20    3. Names of the copyright holders must not be used to endorse or promote
21       products derived from this software without prior written permission
22       from the copyright holders.
23    4. The right to distribute this software or to use it for any purpose does
24       not give you the right to use Servicemarks (sm) or Trademarks (tm) of
25       the copyright holders.  Use of them is covered by separate agreement
26       with the copyright holders.
27    5. If any files are modified, you must cause the modified files to carry
28       prominent notices stating that you changed the files and the date of
29       any change.
30 
31  Disclaimer
32 
33  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESSED
34  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
36  EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
37  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
39  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44 
45 #ifndef __STDC_WANT_DEC_FP__
46 #define __STDC_WANT_DEC_FP__ 1
47 #endif
48 #include <math.h>
49 #include <fenv.h>
50 #include <errno.h>
51 #include <inttypes.h>
52 
53 #ifndef _MSC_VER
54 #ifndef __DECIMAL_BID_FORMAT__
55 #error Only BID DFP supported!
56 #endif
57 
58 #if (__BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__) || (__FLOAT_WORD_ORDER__ != __ORDER_LITTLE_ENDIAN__)
59 #error Only little endian supported!
60 #endif
61 #endif /* _MSC_VER */
62 
63 /* We need to check the BID encoding format
64 
65 _Decimal32:
66 MSB-----------------------------------------------------------------LSB
67 s         | 00eeeeee                      |(0)TTTtttttttttttttttttttt |
68 [sign bit]|[ proble bits must not be "11"]|[Implicit "0" MSB]         |
69           |[ 8-bit exponent, "00" probed ]|[23-bit True Siginifcant  ]|
70 
71 If probed bits form "11" exponent is 10-bits instead of 8-bits.
72 s         | 11 00eeeeee       |(100) Ttttttttttttttttttttt |
73 [sign bit]|["11" proble bits ]|                            |
74           |[ 10-bit exponent ]|[Implicit "100" as MSB]     |
75           |                   |[21-bit True Significant   ]|
76 
77 DFP = -1^s x tttt x [ 10 ^ (eeee - (101(base10))]
78 
79 Use type0d32 to probe the 2 bits, if not 11, use type1d32, else use type2d32.
80 The 11 bits are ignored when computing exponents.
81 
82 Examples:
83 
84 s 00eeeeee (0)TTTtttttttttttttttttttt
85 s 01eeeeee (0)TTTtttttttttttttttttttt
86 s 10eeeeee (0)TTTtttttttttttttttttttt
87 s 11 01eeeeee (100)Ttttttttttttttttttttt
88 s 11 10eeeeee (100)Ttttttttttttttttttttt
89 
90 NAN types:
91 
92 s 11110 xxxxxxxxxxxxxxxxxxxxxxxxxx   +-Infinity
93 s 111110 xxxxxxxxxxxxxxxxxxxxxxxxx   quiet NaN
94 s 111111 xxxxxxxxxxxxxxxxxxxxxxxxx   signaling NaN
95 
96 _Decimal64:
97 
98 s 00eeeeeeee TTTtttttttttttttttttttt tttttttttttttttttttttttttttttt
99 s 01eeeeeeee TTTtttttttttttttttttttt tttttttttttttttttttttttttttttt
100 s 10eeeeeeee TTTtttttttttttttttttttt tttttttttttttttttttttttttttttt
101 s 11 00eeeeeeee (100)Ttttttttttttttttttttt tttttttttttttttttttttttttttttt
102 s 11 01eeeeeeee (100)Ttttttttttttttttttttt tttttttttttttttttttttttttttttt
103 s 11 10eeeeeeee (100)Ttttttttttttttttttttt tttttttttttttttttttttttttttttt
104 
105 DFP = -1^s x tttt x [ 10 ^ (eeee - (398(base10))]
106 
107 _Decimal128:
108 
109 s 00eeeeeeeeeeee TTTtttttttttttttttttttt tttttttttttttttttttttttttttttt tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
110 s 01eeeeeeeeeeee TTTtttttttttttttttttttt tttttttttttttttttttttttttttttt tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
111 s 10eeeeeeeeeeee TTTtttttttttttttttttttt tttttttttttttttttttttttttttttt tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
112 s 11 00eeeeeeeeeeee (100)Ttttttttttttttttttttt tttttttttttttttttttttttttttttt tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
113 s 11 01eeeeeeeeeeee (100)Ttttttttttttttttttttt tttttttttttttttttttttttttttttt tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
114 s 11 10eeeeeeeeeeee (100)Ttttttttttttttttttttt tttttttttttttttttttttttttttttt tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
115 
116 DFP = -1^s x tttt x [ 10 ^ (eeee - (6176(base10))]
117 
118 */
119 
120 #include <pshpack1.h>
121 #ifdef __GNUC__
122 #define structpack __attribute__((gcc_struct))
123 #else
124 #define structpack
125 #endif
126 
127 typedef struct structpack type0d32 {
128   uint64_t blob:25;
129   uint32_t flag:4;
130   uint32_t bits:2;
131   uint32_t sign:1;
132 } type0d32;
133 
134 /* case not 11 */
135 typedef struct structpack type1d32 {
136   uint64_t mantissa:23;
137   uint32_t exponent:8;
138   uint32_t sign:1;
139 } type1d32;
140 
141 /* case 11 */
142 typedef struct structpack type2d32 {
143   /* implicit (100) MSB for mantissa */
144   uint64_t mantissa:21;
145   uint32_t exponent:8;
146   uint32_t bits:2;
147   uint32_t sign:1;
148 } type2d32;
149 
150 typedef struct structpack type0d64 {
151   uint64_t blob:57;
152   uint32_t flag:4;
153   uint32_t bits:2;
154   uint32_t sign:1;
155 } type0d64;
156 
157 /* case not 11 */
158 typedef struct structpack type1d64 {;
159   uint64_t mantissa:53;
160   uint32_t exponent:10;
161   uint32_t sign:1;
162 } type1d64;
163 
164 /* case 11 */
165 typedef struct structpack type2d64 {
166   /* implicit (100) MSB for mantissa */
167   uint64_t mantissa:51;
168   uint32_t exponent:10;
169   uint32_t bits:2;
170   uint32_t sign:1;
171 } type2d64;
172 
173 typedef struct structpack type0d128 {
174   uint64_t blobL:64;
175   uint64_t blobH:57;
176   uint32_t flag:4;
177   uint32_t bits:2;
178   uint32_t sign:1;
179 } type0d128;
180 
181 /* case not 11 */
182 typedef struct structpack type1d128 {
183   uint64_t mantissaL:64;
184   uint64_t mantissaH:49;
185   uint32_t exponent:14;
186   uint32_t sign:1;
187 } type1d128;
188 
189 /* case 11 */
190 typedef struct structpack type2d128 {
191   uint64_t mantissaL:64;
192   /* implicit (100) MSB for mantissa */
193   uint64_t mantissaH:47;
194   uint32_t exponent:14;
195   uint32_t bits:2;
196   uint32_t sign:1;
197 } type2d128;
198 
199 #ifdef _MSC_VER
200 typedef type0d32 _Decimal32;
201 typedef type0d64 _Decimal64;
202 typedef type0d128 _Decimal128;
203 #endif /* _MSC_VER */
204 
205 #include <poppack.h>
206 
207 typedef union ud32 {
208   _Decimal32 d;
209   type0d32 t0;
210   type1d32 t1;
211   type2d32 t2;
212   unsigned char b[sizeof(_Decimal32)];
213 } ud32;
214 
215 typedef union ud64 {
216   _Decimal64 d;
217   type0d64 t0;
218   type1d64 t1;
219   type2d64 t2;
220   unsigned char b[sizeof(_Decimal64)];
221 } ud64;
222 
223 typedef union ud128 {
224   _Decimal128 d;
225   type0d128 t0;
226   type1d128 t1;
227   type2d128 t2;
228   unsigned char b[sizeof(_Decimal128)];
229 } ud128;
230