• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GCC with patches for OS216


Commit MetaInfo

Revision2533577ff6607c4fe093acb0fb711a003a4b3eeb (tree)
Time2007-09-11 05:44:08
AuthorJanis Johnson <janis187@us.i...>
CommiterJanis Johnson

Log Message

Makefile.in (libdecnumber_a_OBJS): Remove decUtility.o

2007-09-10 Janis Johnson <janis187@us.ibm.com>
Ben Elliston <bje@au.ibm.com>

libdecnumber/
* Makefile.in (libdecnumber_a_OBJS): Remove decUtility.o
(dependencies): Add Symbols headers.
* decContext.c: Upgrade to decNumber 3.53.
* decContext.h: Ditto.
* decDPD.h: Ditto.
* decNumber.c: Ditto.
* decNumber.h: Ditto.
* decNumberLocal.h: Ditto.
* decBasic.c: New file from decNumber 3.53.
* decCommon.c: Ditto.
* decDouble.c: Ditto.
* decDouble.h: Ditto.
* decQuad.c: Ditto.
* decQuad.h: Ditto.
* decSingle.c: Ditto.
* decSingle.h: Ditto.
* decPacked.c: Ditto.
* decPacked.h: Ditto.
* dpd/decimal128.c: Upgrade to decNumber 3.53.
* dpd/decimal128.h: Ditto.
* dpd/decimal32.c: Ditto.
* dpd/decimal32.h: Ditto.
* dpd/decimal64.c: Ditto.
* dpd/decimal64.h: Ditto.
* decLibrary.c (dec_byte_swap): Remove.
* decContextSymbols.h: New file.
* decDoubleSymbols.h: New file.
* decNumberSymbols.h: New file.
* decPackedSymbols.h: New file.
* decQuadSymbols.h: New file.
* decSingleSymbols.h: New file.
* decUtility.c: Delete file.
* decUtility.h: Delete file.
* bid/decimal128Symbols.h: New file.
* bid/decimal128Local.h: New file.
* bid/decimal32Symbols.h: New file.
* bid/decimal64Symbols.h: New file.
* bid/host-ieee128.c (
swap128): Remove.
(host_to_ieee_128, ieee_to_host_128): Don't handle endianness.
* bid/host-ieee32.c (dec_type_swap): Remove.
(
host_to_ieee_32, ieee_to_host_32): Don't handle endianness.
* bid/host-ieee64.c (
swap64): Remove.
(host_to_ieee_64, ieee_to_host_64): Don't handle endianness.
* dpd/decimal32Symbols.h: New file.
* dpd/decimal64Symbols.h: New file.
* dpd/decimal128Symbols.h: New file.
* dpd/decimal128Local.h: New file.

libgcc/
* Makefile.in (dfp-filenames): Remove decUtility, add
decDouble, decPacked, decQuad, decSingle.

gcc/
* dfp.c: Include decimal128Local.h;
(dfp_byte_swap): Remove.
(encode_decimal32, decode_decimal32): Don't handle endianness.
(encode_decimal64, decode_decimal64): Ditto.
(encode_decimal128, decode_decimal128): Ditto.
* config/dfp-bit.c (host_to_ieee32, ieee_to_host_32): Ditto.
(swap64): Remove.
(host_to_ieee_64, ieee_to_host_64): Don't handle endianness.

(swap128): Remove

(host_to_ieee_128, ieee_to_host_128): Don't handle endianness.
* Makefile.in (DECNUM_H): Add decimal128Local.h.

Co-Authored-By: Ben Elliston <bje@au.ibm.com>

From-SVN: r128350

Change Summary

Incremental Difference

--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,18 @@
1+2007-09-10 Janis Johnson <janis187@us.ibm.com>
2+ Ben Elliston <bje@au.ibm.com>
3+
4+ * dfp.c: Include decimal128Local.h;
5+ (dfp_byte_swap): Remove.
6+ (encode_decimal32, decode_decimal32): Don't handle endianness.
7+ (encode_decimal64, decode_decimal64): Ditto.
8+ (encode_decimal128, decode_decimal128): Ditto.
9+ * config/dfp-bit.c (host_to_ieee32, ieee_to_host_32): Ditto.
10+ (__swap64): Remove.
11+ (host_to_ieee_64, ieee_to_host_64): Don't handle endianness.
12+ (__swap128): Remove
13+ (host_to_ieee_128, ieee_to_host_128): Don't handle endianness.
14+ * Makefile.in (DECNUM_H): Add decimal128Local.h.
15+
116 2007-09-10 David Daney <ddaney@avtrex.com>
217
318 * config/mips/mips.md (UNSPEC_MEMORY_BARRIER): New entry in
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -815,7 +815,8 @@ PREDICT_H = predict.h predict.def
815815 CPPLIB_H = $(srcdir)/../libcpp/include/line-map.h \
816816 $(srcdir)/../libcpp/include/cpplib.h
817817 DECNUM_H = $(DECNUM)/decContext.h $(DECNUM)/decDPD.h $(DECNUM)/decNumber.h \
818- $(DECNUMFMT)/decimal32.h $(DECNUMFMT)/decimal64.h $(DECNUMFMT)/decimal128.h
818+ $(DECNUMFMT)/decimal32.h $(DECNUMFMT)/decimal64.h \
819+ $(DECNUMFMT)/decimal128.h $(DECNUMFMT)/decimal128Local.h
819820 MKDEPS_H = $(srcdir)/../libcpp/include/mkdeps.h
820821 SYMTAB_H = $(srcdir)/../libcpp/include/symtab.h
821822 CPP_ID_DATA_H = $(CPPLIB_H) $(srcdir)/../libcpp/include/cpp-id-data.h
--- a/gcc/config/dfp-bit.c
+++ b/gcc/config/dfp-bit.c
@@ -70,8 +70,6 @@ typedef decNumber* (*dfp_unary_func)
7070 /* A pointer to a binary decNumber operation. */
7171 typedef decNumber* (*dfp_binary_func)
7272 (decNumber *, const decNumber *, const decNumber *, decContext *);
73-
74-extern uint32_t __dec_byte_swap (uint32_t);
7573
7674 /* Unary operations. */
7775
@@ -190,101 +188,41 @@ dfp_compare_op (dfp_binary_func op, DFP_C_TYPE arg_a, DFP_C_TYPE arg_b)
190188 void
191189 __host_to_ieee_32 (_Decimal32 in, decimal32 *out)
192190 {
193- uint32_t t;
194-
195- if (!LIBGCC2_FLOAT_WORDS_BIG_ENDIAN)
196- {
197- memcpy (&t, &in, 4);
198- t = __dec_byte_swap (t);
199- memcpy (out, &t, 4);
200- }
201- else
202- memcpy (out, &in, 4);
191+ memcpy (out, &in, 4);
203192 }
204193
205194 void
206195 __ieee_to_host_32 (decimal32 in, _Decimal32 *out)
207196 {
208- uint32_t t;
209-
210- if (!LIBGCC2_FLOAT_WORDS_BIG_ENDIAN)
211- {
212- memcpy (&t, &in, 4);
213- t = __dec_byte_swap (t);
214- memcpy (out, &t, 4);
215- }
216- else
217- memcpy (out, &in, 4);
197+ memcpy (out, &in, 4);
218198 }
219199 #endif /* L_conv_sd */
220200
221201 #if defined(L_conv_dd)
222-static void
223-__swap64 (char *src, char *dst)
224-{
225- uint32_t t1, t2;
226-
227- if (!LIBGCC2_FLOAT_WORDS_BIG_ENDIAN)
228- {
229- memcpy (&t1, src, 4);
230- memcpy (&t2, src + 4, 4);
231- t1 = __dec_byte_swap (t1);
232- t2 = __dec_byte_swap (t2);
233- memcpy (dst, &t2, 4);
234- memcpy (dst + 4, &t1, 4);
235- }
236- else
237- memcpy (dst, src, 8);
238-}
239-
240202 void
241203 __host_to_ieee_64 (_Decimal64 in, decimal64 *out)
242204 {
243- __swap64 ((char *) &in, (char *) out);
205+ memcpy (out, &in, 8);
244206 }
245207
246208 void
247209 __ieee_to_host_64 (decimal64 in, _Decimal64 *out)
248210 {
249- __swap64 ((char *) &in, (char *) out);
211+ memcpy (out, &in, 8);
250212 }
251213 #endif /* L_conv_dd */
252214
253215 #if defined(L_conv_td)
254-static void
255-__swap128 (char *src, char *dst)
256-{
257- uint32_t t1, t2, t3, t4;
258-
259- if (!LIBGCC2_FLOAT_WORDS_BIG_ENDIAN)
260- {
261- memcpy (&t1, src, 4);
262- memcpy (&t2, src + 4, 4);
263- memcpy (&t3, src + 8, 4);
264- memcpy (&t4, src + 12, 4);
265- t1 = __dec_byte_swap (t1);
266- t2 = __dec_byte_swap (t2);
267- t3 = __dec_byte_swap (t3);
268- t4 = __dec_byte_swap (t4);
269- memcpy (dst, &t4, 4);
270- memcpy (dst + 4, &t3, 4);
271- memcpy (dst + 8, &t2, 4);
272- memcpy (dst + 12, &t1, 4);
273- }
274- else
275- memcpy (dst, src, 16);
276-}
277-
278216 void
279217 __host_to_ieee_128 (_Decimal128 in, decimal128 *out)
280218 {
281- __swap128 ((char *) &in, (char *) out);
219+ memcpy (out, &in, 16);
282220 }
283221
284222 void
285223 __ieee_to_host_128 (decimal128 in, _Decimal128 *out)
286224 {
287- __swap128 ((char *) &in, (char *) out);
225+ memcpy (out, &in, 16);
288226 }
289227 #endif /* L_conv_td */
290228
--- a/gcc/dfp.c
+++ b/gcc/dfp.c
@@ -31,29 +31,11 @@ along with GCC; see the file COPYING3. If not see
3131 decNumber structure is large enough to hold decimal128 digits. */
3232
3333 #include "decimal128.h"
34+#include "decimal128Local.h"
3435 #include "decimal64.h"
3536 #include "decimal32.h"
3637 #include "decNumber.h"
3738
38-static uint32_t
39-dfp_byte_swap (uint32_t in)
40-{
41- uint32_t out = 0;
42- unsigned char *p = (unsigned char *) &out;
43- union {
44- uint32_t i;
45- unsigned char b[4];
46- } u;
47-
48- u.i = in;
49- p[0] = u.b[3];
50- p[1] = u.b[2];
51- p[2] = u.b[1];
52- p[3] = u.b[0];
53-
54- return out;
55-}
56-
5739 /* Initialize R (a real with the decimal flag set) from DN. Can
5840 utilize status passed in via CONTEXT, if a previous operation had
5941 interesting status. */
@@ -155,10 +137,7 @@ encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
155137 decimal_to_decnumber (r, &dn);
156138 decimal32FromNumber (&d32, &dn, &set);
157139
158- if (FLOAT_WORDS_BIG_ENDIAN)
159- buf[0] = *(uint32_t *) d32.bytes;
160- else
161- buf[0] = dfp_byte_swap (*(uint32_t *) d32.bytes);
140+ buf[0] = *(uint32_t *) d32.bytes;
162141 }
163142
164143 /* Decode an IEEE 754R decimal32 type into a real. */
@@ -174,10 +153,7 @@ decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
174153 decContextDefault (&set, DEC_INIT_DECIMAL128);
175154 set.traps = 0;
176155
177- if (FLOAT_WORDS_BIG_ENDIAN)
178- *((uint32_t *) d32.bytes) = (uint32_t) buf[0];
179- else
180- *((uint32_t *) d32.bytes) = dfp_byte_swap ((uint32_t) buf[0]);
156+ *((uint32_t *) d32.bytes) = (uint32_t) buf[0];
181157
182158 decimal32ToNumber (&d32, &dn);
183159 decimal_from_decnumber (r, &dn, &set);
@@ -199,16 +175,8 @@ encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
199175 decimal_to_decnumber (r, &dn);
200176 decimal64FromNumber (&d64, &dn, &set);
201177
202- if (FLOAT_WORDS_BIG_ENDIAN)
203- {
204- buf[0] = *(uint32_t *) &d64.bytes[0];
205- buf[1] = *(uint32_t *) &d64.bytes[4];
206- }
207- else
208- {
209- buf[1] = dfp_byte_swap (*(uint32_t *) &d64.bytes[0]);
210- buf[0] = dfp_byte_swap (*(uint32_t *) &d64.bytes[4]);
211- }
178+ buf[0] = *(uint32_t *) &d64.bytes[0];
179+ buf[1] = *(uint32_t *) &d64.bytes[4];
212180 }
213181
214182 /* Decode an IEEE 754R decimal64 type into a real. */
@@ -224,16 +192,8 @@ decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
224192 decContextDefault (&set, DEC_INIT_DECIMAL128);
225193 set.traps = 0;
226194
227- if (FLOAT_WORDS_BIG_ENDIAN)
228- {
229- *((uint32_t *) &d64.bytes[0]) = (uint32_t) buf[0];
230- *((uint32_t *) &d64.bytes[4]) = (uint32_t) buf[1];
231- }
232- else
233- {
234- *((uint32_t *) &d64.bytes[0]) = dfp_byte_swap ((uint32_t) buf[1]);
235- *((uint32_t *) &d64.bytes[4]) = dfp_byte_swap ((uint32_t) buf[0]);
236- }
195+ *((uint32_t *) &d64.bytes[0]) = (uint32_t) buf[0];
196+ *((uint32_t *) &d64.bytes[4]) = (uint32_t) buf[1];
237197
238198 decimal64ToNumber (&d64, &dn);
239199 decimal_from_decnumber (r, &dn, &set);
@@ -255,20 +215,10 @@ encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
255215 decimal_to_decnumber (r, &dn);
256216 decimal128FromNumber (&d128, &dn, &set);
257217
258- if (FLOAT_WORDS_BIG_ENDIAN)
259- {
260- buf[0] = *(uint32_t *) &d128.bytes[0];
261- buf[1] = *(uint32_t *) &d128.bytes[4];
262- buf[2] = *(uint32_t *) &d128.bytes[8];
263- buf[3] = *(uint32_t *) &d128.bytes[12];
264- }
265- else
266- {
267- buf[0] = dfp_byte_swap (*(uint32_t *) &d128.bytes[12]);
268- buf[1] = dfp_byte_swap (*(uint32_t *) &d128.bytes[8]);
269- buf[2] = dfp_byte_swap (*(uint32_t *) &d128.bytes[4]);
270- buf[3] = dfp_byte_swap (*(uint32_t *) &d128.bytes[0]);
271- }
218+ buf[0] = *(uint32_t *) &d128.bytes[0];
219+ buf[1] = *(uint32_t *) &d128.bytes[4];
220+ buf[2] = *(uint32_t *) &d128.bytes[8];
221+ buf[3] = *(uint32_t *) &d128.bytes[12];
272222 }
273223
274224 /* Decode an IEEE 754R decimal128 type into a real. */
@@ -284,20 +234,10 @@ decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
284234 decContextDefault (&set, DEC_INIT_DECIMAL128);
285235 set.traps = 0;
286236
287- if (FLOAT_WORDS_BIG_ENDIAN)
288- {
289- *((uint32_t *) &d128.bytes[0]) = (uint32_t) buf[0];
290- *((uint32_t *) &d128.bytes[4]) = (uint32_t) buf[1];
291- *((uint32_t *) &d128.bytes[8]) = (uint32_t) buf[2];
292- *((uint32_t *) &d128.bytes[12]) = (uint32_t) buf[3];
293- }
294- else
295- {
296- *((uint32_t *) &d128.bytes[0]) = dfp_byte_swap ((uint32_t) buf[3]);
297- *((uint32_t *) &d128.bytes[4]) = dfp_byte_swap ((uint32_t) buf[2]);
298- *((uint32_t *) &d128.bytes[8]) = dfp_byte_swap ((uint32_t) buf[1]);
299- *((uint32_t *) &d128.bytes[12]) = dfp_byte_swap ((uint32_t) buf[0]);
300- }
237+ *((uint32_t *) &d128.bytes[0]) = (uint32_t) buf[0];
238+ *((uint32_t *) &d128.bytes[4]) = (uint32_t) buf[1];
239+ *((uint32_t *) &d128.bytes[8]) = (uint32_t) buf[2];
240+ *((uint32_t *) &d128.bytes[12]) = (uint32_t) buf[3];
301241
302242 decimal128ToNumber (&d128, &dn);
303243 decimal_from_decnumber (r, &dn, &set);
--- a/libdecnumber/ChangeLog
+++ b/libdecnumber/ChangeLog
@@ -1,3 +1,54 @@
1+2007-09-10 Janis Johnson <janis187@us.ibm.com>
2+ Ben Elliston <bje@au.ibm.com>
3+
4+ * Makefile.in (libdecnumber_a_OBJS): Remove decUtility.o
5+ (dependencies): Add Symbols headers.
6+ * decContext.c: Upgrade to decNumber 3.53.
7+ * decContext.h: Ditto.
8+ * decDPD.h: Ditto.
9+ * decNumber.c: Ditto.
10+ * decNumber.h: Ditto.
11+ * decNumberLocal.h: Ditto.
12+ * decBasic.c: New file from decNumber 3.53.
13+ * decCommon.c: Ditto.
14+ * decDouble.c: Ditto.
15+ * decDouble.h: Ditto.
16+ * decQuad.c: Ditto.
17+ * decQuad.h: Ditto.
18+ * decSingle.c: Ditto.
19+ * decSingle.h: Ditto.
20+ * decPacked.c: Ditto.
21+ * decPacked.h: Ditto.
22+ * dpd/decimal128.c: Upgrade to decNumber 3.53.
23+ * dpd/decimal128.h: Ditto.
24+ * dpd/decimal32.c: Ditto.
25+ * dpd/decimal32.h: Ditto.
26+ * dpd/decimal64.c: Ditto.
27+ * dpd/decimal64.h: Ditto.
28+ * decLibrary.c (__dec_byte_swap): Remove.
29+ * decContextSymbols.h: New file.
30+ * decDoubleSymbols.h: New file.
31+ * decNumberSymbols.h: New file.
32+ * decPackedSymbols.h: New file.
33+ * decQuadSymbols.h: New file.
34+ * decSingleSymbols.h: New file.
35+ * decUtility.c: Delete file.
36+ * decUtility.h: Delete file.
37+ * bid/decimal128Symbols.h: New file.
38+ * bid/decimal128Local.h: New file.
39+ * bid/decimal32Symbols.h: New file.
40+ * bid/decimal64Symbols.h: New file.
41+ * bid/host-ieee128.c (__swap128): Remove.
42+ (__host_to_ieee_128, __ieee_to_host_128): Don't handle endianness.
43+ * bid/host-ieee32.c (__dec_type_swap): Remove.
44+ (__host_to_ieee_32, __ieee_to_host_32): Don't handle endianness.
45+ * bid/host-ieee64.c (__swap64): Remove.
46+ (__host_to_ieee_64, __ieee_to_host_64): Don't handle endianness.
47+ * dpd/decimal32Symbols.h: New file.
48+ * dpd/decimal64Symbols.h: New file.
49+ * dpd/decimal128Symbols.h: New file.
50+ * dpd/decimal128Local.h: New file.
51+
152 2007-06-18 Martin Michlmayr <tbm@cyrius.com>
253 H.J. Lu <hongjiu.lu@intel.com>
354
--- a/libdecnumber/Makefile.in
+++ b/libdecnumber/Makefile.in
@@ -56,7 +56,7 @@ INCLUDES = -I$(srcdir) -I.
5656
5757 ALL_CFLAGS = $(CFLAGS) $(WARN_CFLAGS) $(INCLUDES) $(CPPFLAGS)
5858
59-libdecnumber_a_OBJS = decNumber.o decContext.o decUtility.o \
59+libdecnumber_a_OBJS = decNumber.o decContext.o \
6060 decimal32.o decimal64.o decimal128.o
6161
6262 ifeq ($(enable_decimal_float),bid)
@@ -66,7 +66,6 @@ endif
6666
6767 libdecnumber_a_SOURCES = decContext.c decContext.h decDPD.h \
6868 decNumber.c decNumber.h decNumberLocal.h \
69- decUtility.c decUtility.h \
7069 dpd/decimal128.c dpd/decimal128.h \
7170 dpd/decimal32.c dpd/decimal32.h \
7271 dpd/decimal64.c dpd/decimal64.h \
@@ -113,19 +112,25 @@ $(srcdir)/config.in: @MAINT@ $(srcdir)/configure
113112
114113 # Dependencies.
115114
116-decContext.o: decContext.c decContext.h decNumberLocal.h
117-decNumber.o: decNumber.c decNumber.h decContext.h decNumberLocal.h
115+decContext.o: decContext.c decContext.h decNumberLocal.h \
116+ decContextSymbols.h
117+decNumber.o: decNumber.c decNumber.h decContext.h decNumberLocal.h \
118+ decNumberSymbols.h
118119 decimal32.o: $(enable_decimal_float)/decimal32.c \
119120 $(enable_decimal_float)/decimal32.h \
120- decNumber.h decContext.h decNumberLocal.h decUtility.h
121+ $(enable_decimal_float)/decimal32Symbols.h \
122+ decNumber.h decContext.h decNumberLocal.h
121123 $(COMPILE) $<
122124 decimal64.o: $(enable_decimal_float)/decimal64.c \
123125 $(enable_decimal_float)/decimal64.h \
124- decNumber.h decContext.h decNumberLocal.h decUtility.h
126+ $(enable_decimal_float)/decimal64Symbols.h \
127+ decNumber.h decContext.h decNumberLocal.h
125128 $(COMPILE) $<
126129 decimal128.o: $(enable_decimal_float)/decimal128.c \
127130 $(enable_decimal_float)/decimal128.h \
128- decNumber.h decContext.h decNumberLocal.h decUtility.h
131+ $(enable_decimal_float)/decimal128Symbols.h\
132+ $(enable_decimal_float)/decimal128Local.h\
133+ decNumber.h decContext.h decNumberLocal.h
129134 $(COMPILE) $<
130135 bid2dpd_dpd2bid.o : bid/bid2dpd_dpd2bid.c bid/bid2dpd_dpd2bid.h
131136 $(COMPILE) $<
--- /dev/null
+++ b/libdecnumber/bid/decimal128Local.h
@@ -0,0 +1 @@
1+#include "dpd/decimal128Local.h"
--- /dev/null
+++ b/libdecnumber/bid/decimal128Symbols.h
@@ -0,0 +1 @@
1+#include "dpd/decimal128Symbols.h"
--- /dev/null
+++ b/libdecnumber/bid/decimal32Symbols.h
@@ -0,0 +1 @@
1+#include "dpd/decimal32Symbols.h"
--- /dev/null
+++ b/libdecnumber/bid/decimal64Symbols.h
@@ -0,0 +1 @@
1+#include "dpd/decimal64Symbols.h"
--- a/libdecnumber/bid/host-ieee128.c
+++ b/libdecnumber/bid/host-ieee128.c
@@ -27,56 +27,22 @@ along with GCC; see the file COPYING. If not, write to the Free
2727 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2828 02110-1301, USA. */
2929
30-#include <stdio.h>
31-#include <stdlib.h>
3230 #include <string.h>
33-#include <limits.h>
3431
35-#include "config.h"
36-#include "gstdint.h"
3732 #include "bid-dpd.h"
3833 #include "decimal128.h"
3934
40-extern uint32_t __dec_byte_swap (uint32_t);
4135 void __host_to_ieee_128 (_Decimal128 in, decimal128 *out);
4236 void __ieee_to_host_128 (decimal128 in, _Decimal128 *out);
4337
44-#ifndef WORDS_BIGENDIAN
45-#define WORDS_BIGENDIAN 0
46-#endif
47-
48-static void
49-__swap128 (char *src, char *dst)
50-{
51- uint32_t t1, t2, t3, t4;
52-
53- if (!WORDS_BIGENDIAN)
54- {
55- memcpy (&t1, src, 4);
56- memcpy (&t2, src + 4, 4);
57- memcpy (&t3, src + 8, 4);
58- memcpy (&t4, src + 12, 4);
59- t1 = __dec_byte_swap (t1);
60- t2 = __dec_byte_swap (t2);
61- t3 = __dec_byte_swap (t3);
62- t4 = __dec_byte_swap (t4);
63- memcpy (dst, &t4, 4);
64- memcpy (dst + 4, &t3, 4);
65- memcpy (dst + 8, &t2, 4);
66- memcpy (dst + 12, &t1, 4);
67- }
68- else
69- memcpy (dst, src, 16);
70-}
71-
7238 void
7339 __host_to_ieee_128 (_Decimal128 in, decimal128 *out)
7440 {
75- __swap128 ((char *) &in, (char *) out);
41+ memcpy ((char *) out, (char *) &in, 16);
7642 }
7743
7844 void
7945 __ieee_to_host_128 (decimal128 in, _Decimal128 *out)
8046 {
81- __swap128 ((char *) &in, (char *) out);
47+ memcpy ((char *) out, (char *) &in, 16);
8248 }
--- a/libdecnumber/bid/host-ieee32.c
+++ b/libdecnumber/bid/host-ieee32.c
@@ -37,69 +37,21 @@ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
3737 /* The intended way to use this file is to make two copies, add `#define '
3838 to one copy, then compile both copies and add them to libgcc.a. */
3939
40-#include <stdio.h>
41-#include <stdlib.h>
4240 #include <string.h>
43-#include <limits.h>
44-
45-#include "config.h"
46-#include "gstdint.h"
4741 #include "bid-dpd.h"
4842 #include "decimal32.h"
4943
50-uint32_t __dec_byte_swap (uint32_t);
5144 void __host_to_ieee_32 (_Decimal32 in, decimal32 *out);
5245 void __ieee_to_host_32 (decimal32 in, _Decimal32 *out);
5346
54-#ifndef WORDS_BIGENDIAN
55-#define WORDS_BIGENDIAN 0
56-#endif
57-
58-uint32_t
59-__dec_byte_swap (uint32_t in)
60-{
61- uint32_t out = 0;
62- unsigned char *p = (unsigned char *) &out;
63- union {
64- uint32_t i;
65- unsigned char b[4];
66- } u;
67-
68- u.i = in;
69- p[0] = u.b[3];
70- p[1] = u.b[2];
71- p[2] = u.b[1];
72- p[3] = u.b[0];
73-
74- return out;
75-}
76-
7747 void
7848 __host_to_ieee_32 (_Decimal32 in, decimal32 *out)
7949 {
80- uint32_t t;
81-
82- if (!WORDS_BIGENDIAN)
83- {
84- memcpy (&t, &in, 4);
85- t = __dec_byte_swap (t);
86- memcpy (out, &t, 4);
87- }
88- else
89- memcpy (out, &in, 4);
50+ memcpy ((char *) out, (char *) &in, 4);
9051 }
9152
9253 void
9354 __ieee_to_host_32 (decimal32 in, _Decimal32 *out)
9455 {
95- uint32_t t;
96-
97- if (!WORDS_BIGENDIAN)
98- {
99- memcpy (&t, &in, 4);
100- t = __dec_byte_swap (t);
101- memcpy (out, &t, 4);
102- }
103- else
104- memcpy (out, &in, 4);
56+ memcpy ((char *) out, (char *) &in, 4);
10557 }
--- a/libdecnumber/bid/host-ieee64.c
+++ b/libdecnumber/bid/host-ieee64.c
@@ -37,50 +37,21 @@ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
3737 /* The intended way to use this file is to make two copies, add `#define '
3838 to one copy, then compile both copies and add them to libgcc.a. */
3939
40-#include <stdio.h>
41-#include <stdlib.h>
4240 #include <string.h>
43-#include <limits.h>
44-
45-#include "config.h"
46-#include "gstdint.h"
4741 #include "bid-dpd.h"
4842 #include "decimal64.h"
4943
50-uint32_t __dec_byte_swap (uint32_t);
5144 void __host_to_ieee_64 (_Decimal64 in, decimal64 *out);
5245 void __ieee_to_host_64 (decimal64 in, _Decimal64 *out);
5346
54-#ifndef WORDS_BIGENDIAN
55-#define WORDS_BIGENDIAN 0
56-#endif
57-
58-static void
59-__swap64 (char *src, char *dst)
60-{
61- uint32_t t1, t2;
62-
63- if (!WORDS_BIGENDIAN)
64- {
65- memcpy (&t1, src, 4);
66- memcpy (&t2, src + 4, 4);
67- t1 = __dec_byte_swap (t1);
68- t2 = __dec_byte_swap (t2);
69- memcpy (dst, &t2, 4);
70- memcpy (dst + 4, &t1, 4);
71- }
72- else
73- memcpy (dst, src, 8);
74-}
75-
7647 void
7748 __host_to_ieee_64 (_Decimal64 in, decimal64 *out)
7849 {
79- __swap64 ((char *) &in, (char *) out);
50+ memcpy ((char *) out, (char *) &in, 8);
8051 }
8152
8253 void
8354 __ieee_to_host_64 (decimal64 in, _Decimal64 *out)
8455 {
85- __swap64 ((char *) &in, (char *) out);
56+ memcpy ((char *) out, (char *) &in, 8);
8657 }
--- /dev/null
+++ b/libdecnumber/decBasic.c
@@ -0,0 +1,3769 @@
1+/* Common base code for the decNumber C Library.
2+ Copyright (C) 2007 Free Software Foundation, Inc.
3+ Contributed by IBM Corporation. Author Mike Cowlishaw.
4+
5+ This file is part of GCC.
6+
7+ GCC is free software; you can redistribute it and/or modify it under
8+ the terms of the GNU General Public License as published by the Free
9+ Software Foundation; either version 2, or (at your option) any later
10+ version.
11+
12+ In addition to the permissions in the GNU General Public License,
13+ the Free Software Foundation gives you unlimited permission to link
14+ the compiled version of this file into combinations with other
15+ programs, and to distribute those combinations without any
16+ restriction coming from the use of this file. (The General Public
17+ License restrictions do apply in other respects; for example, they
18+ cover modification of the file, and distribution when not linked
19+ into a combine executable.)
20+
21+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
23+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24+ for more details.
25+
26+ You should have received a copy of the GNU General Public License
27+ along with GCC; see the file COPYING. If not, write to the Free
28+ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29+ 02110-1301, USA. */
30+
31+/* ------------------------------------------------------------------ */
32+/* decBasic.c -- common base code for Basic decimal types */
33+/* ------------------------------------------------------------------ */
34+/* This module comprises code that is shared between decDouble and */
35+/* decQuad (but not decSingle). The main arithmetic operations are */
36+/* here (Add, Subtract, Multiply, FMA, and Division operators). */
37+/* */
38+/* Unlike decNumber, parameterization takes place at compile time */
39+/* rather than at runtime. The parameters are set in the decDouble.c */
40+/* (etc.) files, which then include this one to produce the compiled */
41+/* code. The functions here, therefore, are code shared between */
42+/* multiple formats. */
43+/* */
44+/* This must be included after decCommon.c. */
45+/* ------------------------------------------------------------------ */
46+/* Names here refer to decFloat rather than to decDouble, etc., and */
47+/* the functions are in strict alphabetical order. */
48+
49+/* The compile-time flags SINGLE, DOUBLE, and QUAD are set up in */
50+/* decCommon.c */
51+#if !defined(QUAD)
52+ #error decBasic.c must be included after decCommon.c
53+#endif
54+#if SINGLE
55+ #error Routines in decBasic.c are for decDouble and decQuad only
56+#endif
57+
58+/* Private constants */
59+#define DIVIDE 0x80000000 /* Divide operations [as flags] */
60+#define REMAINDER 0x40000000 /* .. */
61+#define DIVIDEINT 0x20000000 /* .. */
62+#define REMNEAR 0x10000000 /* .. */
63+
64+/* Private functions (local, used only by routines in this module) */
65+static decFloat *decDivide(decFloat *, const decFloat *,
66+ const decFloat *, decContext *, uInt);
67+static decFloat *decCanonical(decFloat *, const decFloat *);
68+static void decFiniteMultiply(bcdnum *, uByte *, const decFloat *,
69+ const decFloat *);
70+static decFloat *decInfinity(decFloat *, const decFloat *);
71+static decFloat *decInvalid(decFloat *, decContext *);
72+static decFloat *decNaNs(decFloat *, const decFloat *, const decFloat *,
73+ decContext *);
74+static Int decNumCompare(const decFloat *, const decFloat *, Flag);
75+static decFloat *decToIntegral(decFloat *, const decFloat *, decContext *,
76+ enum rounding, Flag);
77+static uInt decToInt32(const decFloat *, decContext *, enum rounding,
78+ Flag, Flag);
79+
80+/* ------------------------------------------------------------------ */
81+/* decCanonical -- copy a decFloat, making canonical */
82+/* */
83+/* result gets the canonicalized df */
84+/* df is the decFloat to copy and make canonical */
85+/* returns result */
86+/* */
87+/* This is exposed via decFloatCanonical for Double and Quad only. */
88+/* This works on specials, too; no error or exception is possible. */
89+/* ------------------------------------------------------------------ */
90+static decFloat * decCanonical(decFloat *result, const decFloat *df) {
91+ uInt encode, precode, dpd; /* work */
92+ uInt inword, uoff, canon; /* .. */
93+ Int n; /* counter (down) */
94+ if (df!=result) *result=*df; /* effect copy if needed */
95+ if (DFISSPECIAL(result)) {
96+ if (DFISINF(result)) return decInfinity(result, df); /* clean Infinity */
97+ /* is a NaN */
98+ DFWORD(result, 0)&=~ECONNANMASK; /* clear ECON except selector */
99+ if (DFISCCZERO(df)) return result; /* coefficient continuation is 0 */
100+ /* drop through to check payload */
101+ }
102+ /* return quickly if the coefficient continuation is canonical */
103+ { /* declare block */
104+ #if DOUBLE
105+ uInt sourhi=DFWORD(df, 0);
106+ uInt sourlo=DFWORD(df, 1);
107+ if (CANONDPDOFF(sourhi, 8)
108+ && CANONDPDTWO(sourhi, sourlo, 30)
109+ && CANONDPDOFF(sourlo, 20)
110+ && CANONDPDOFF(sourlo, 10)
111+ && CANONDPDOFF(sourlo, 0)) return result;
112+ #elif QUAD
113+ uInt sourhi=DFWORD(df, 0);
114+ uInt sourmh=DFWORD(df, 1);
115+ uInt sourml=DFWORD(df, 2);
116+ uInt sourlo=DFWORD(df, 3);
117+ if (CANONDPDOFF(sourhi, 4)
118+ && CANONDPDTWO(sourhi, sourmh, 26)
119+ && CANONDPDOFF(sourmh, 16)
120+ && CANONDPDOFF(sourmh, 6)
121+ && CANONDPDTWO(sourmh, sourml, 28)
122+ && CANONDPDOFF(sourml, 18)
123+ && CANONDPDOFF(sourml, 8)
124+ && CANONDPDTWO(sourml, sourlo, 30)
125+ && CANONDPDOFF(sourlo, 20)
126+ && CANONDPDOFF(sourlo, 10)
127+ && CANONDPDOFF(sourlo, 0)) return result;
128+ #endif
129+ } /* block */
130+
131+ /* Loop to repair a non-canonical coefficent, as needed */
132+ inword=DECWORDS-1; /* current input word */
133+ uoff=0; /* bit offset of declet */
134+ encode=DFWORD(result, inword);
135+ for (n=DECLETS-1; n>=0; n--) { /* count down declets of 10 bits */
136+ dpd=encode>>uoff;
137+ uoff+=10;
138+ if (uoff>32) { /* crossed uInt boundary */
139+ inword--;
140+ encode=DFWORD(result, inword);
141+ uoff-=32;
142+ dpd|=encode<<(10-uoff); /* get pending bits */
143+ }
144+ dpd&=0x3ff; /* clear uninteresting bits */
145+ if (dpd<0x16e) continue; /* must be canonical */
146+ canon=BIN2DPD[DPD2BIN[dpd]]; /* determine canonical declet */
147+ if (canon==dpd) continue; /* have canonical declet */
148+ /* need to replace declet */
149+ if (uoff>=10) { /* all within current word */
150+ encode&=~(0x3ff<<(uoff-10)); /* clear the 10 bits ready for replace */
151+ encode|=canon<<(uoff-10); /* insert the canonical form */
152+ DFWORD(result, inword)=encode; /* .. and save */
153+ continue;
154+ }
155+ /* straddled words */
156+ precode=DFWORD(result, inword+1); /* get previous */
157+ precode&=0xffffffff>>(10-uoff); /* clear top bits */
158+ DFWORD(result, inword+1)=precode|(canon<<(32-(10-uoff)));
159+ encode&=0xffffffff<<uoff; /* clear bottom bits */
160+ encode|=canon>>(10-uoff); /* insert canonical */
161+ DFWORD(result, inword)=encode; /* .. and save */
162+ } /* n */
163+ return result;
164+ } /* decCanonical */
165+
166+/* ------------------------------------------------------------------ */
167+/* decDivide -- divide operations */
168+/* */
169+/* result gets the result of dividing dfl by dfr: */
170+/* dfl is the first decFloat (lhs) */
171+/* dfr is the second decFloat (rhs) */
172+/* set is the context */
173+/* op is the operation selector */
174+/* returns result */
175+/* */
176+/* op is one of DIVIDE, REMAINDER, DIVIDEINT, or REMNEAR. */
177+/* ------------------------------------------------------------------ */
178+#define DIVCOUNT 0 /* 1 to instrument subtractions counter */
179+#define DIVBASE BILLION /* the base used for divide */
180+#define DIVOPLEN DECPMAX9 /* operand length ('digits' base 10**9) */
181+#define DIVACCLEN (DIVOPLEN*3) /* accumulator length (ditto) */
182+static decFloat * decDivide(decFloat *result, const decFloat *dfl,
183+ const decFloat *dfr, decContext *set, uInt op) {
184+ decFloat quotient; /* for remainders */
185+ bcdnum num; /* for final conversion */
186+ uInt acc[DIVACCLEN]; /* coefficent in base-billion .. */
187+ uInt div[DIVOPLEN]; /* divisor in base-billion .. */
188+ uInt quo[DIVOPLEN+1]; /* quotient in base-billion .. */
189+ uByte bcdacc[(DIVOPLEN+1)*9+2]; /* for quotient in BCD, +1, +1 */
190+ uInt *msua, *msud, *msuq; /* -> msu of acc, div, and quo */
191+ Int divunits, accunits; /* lengths */
192+ Int quodigits; /* digits in quotient */
193+ uInt *lsua, *lsuq; /* -> current acc and quo lsus */
194+ Int length, multiplier; /* work */
195+ uInt carry, sign; /* .. */
196+ uInt *ua, *ud, *uq; /* .. */
197+ uByte *ub; /* .. */
198+ uInt divtop; /* top unit of div adjusted for estimating */
199+ #if DIVCOUNT
200+ static uInt maxcount=0; /* worst-seen subtractions count */
201+ uInt divcount=0; /* subtractions count [this divide] */
202+ #endif
203+
204+ /* calculate sign */
205+ num.sign=(DFWORD(dfl, 0)^DFWORD(dfr, 0)) & DECFLOAT_Sign;
206+
207+ if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { /* either is special? */
208+ /* NaNs are handled as usual */
209+ if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
210+ /* one or two infinities */
211+ if (DFISINF(dfl)) {
212+ if (DFISINF(dfr)) return decInvalid(result, set); /* Two infinities bad */
213+ if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); /* as is rem */
214+ /* Infinity/x is infinite and quiet, even if x=0 */
215+ DFWORD(result, 0)=num.sign;
216+ return decInfinity(result, result);
217+ }
218+ /* must be x/Infinity -- remainders are lhs */
219+ if (op&(REMAINDER|REMNEAR)) return decCanonical(result, dfl);
220+ /* divides: return zero with correct sign and exponent depending */
221+ /* on op (Etiny for divide, 0 for divideInt) */
222+ decFloatZero(result);
223+ if (op==DIVIDEINT) DFWORD(result, 0)|=num.sign; /* add sign */
224+ else DFWORD(result, 0)=num.sign; /* zeros the exponent, too */
225+ return result;
226+ }
227+ /* next, handle zero operands (x/0 and 0/x) */
228+ if (DFISZERO(dfr)) { /* x/0 */
229+ if (DFISZERO(dfl)) { /* 0/0 is undefined */
230+ decFloatZero(result);
231+ DFWORD(result, 0)=DECFLOAT_qNaN;
232+ set->status|=DEC_Division_undefined;
233+ return result;
234+ }
235+ if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); /* bad rem */
236+ set->status|=DEC_Division_by_zero;
237+ DFWORD(result, 0)=num.sign;
238+ return decInfinity(result, result); /* x/0 -> signed Infinity */
239+ }
240+ num.exponent=GETEXPUN(dfl)-GETEXPUN(dfr); /* ideal exponent */
241+ if (DFISZERO(dfl)) { /* 0/x (x!=0) */
242+ /* if divide, result is 0 with ideal exponent; divideInt has */
243+ /* exponent=0, remainders give zero with lower exponent */
244+ if (op&DIVIDEINT) {
245+ decFloatZero(result);
246+ DFWORD(result, 0)|=num.sign; /* add sign */
247+ return result;
248+ }
249+ if (!(op&DIVIDE)) { /* a remainder */
250+ /* exponent is the minimum of the operands */
251+ num.exponent=MINI(GETEXPUN(dfl), GETEXPUN(dfr));
252+ /* if the result is zero the sign shall be sign of dfl */
253+ num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
254+ }
255+ bcdacc[0]=0;
256+ num.msd=bcdacc; /* -> 0 */
257+ num.lsd=bcdacc; /* .. */
258+ return decFinalize(result, &num, set); /* [divide may clamp exponent] */
259+ } /* 0/x */
260+ /* [here, both operands are known to be finite and non-zero] */
261+
262+ /* extract the operand coefficents into 'units' which are */
263+ /* base-billion; the lhs is high-aligned in acc and the msu of both */
264+ /* acc and div is at the right-hand end of array (offset length-1); */
265+ /* the quotient can need one more unit than the operands as digits */
266+ /* in it are not necessarily aligned neatly; further, the quotient */
267+ /* may not start accumulating until after the end of the initial */
268+ /* operand in acc if that is small (e.g., 1) so the accumulator */
269+ /* must have at least that number of units extra (at the ls end) */
270+ GETCOEFFBILL(dfl, acc+DIVACCLEN-DIVOPLEN);
271+ GETCOEFFBILL(dfr, div);
272+ /* zero the low uInts of acc */
273+ acc[0]=0;
274+ acc[1]=0;
275+ acc[2]=0;
276+ acc[3]=0;
277+ #if DOUBLE
278+ #if DIVOPLEN!=2
279+ #error Unexpected Double DIVOPLEN
280+ #endif
281+ #elif QUAD
282+ acc[4]=0;
283+ acc[5]=0;
284+ acc[6]=0;
285+ acc[7]=0;
286+ #if DIVOPLEN!=4
287+ #error Unexpected Quad DIVOPLEN
288+ #endif
289+ #endif
290+
291+ /* set msu and lsu pointers */
292+ msua=acc+DIVACCLEN-1; /* [leading zeros removed below] */
293+ msuq=quo+DIVOPLEN;
294+ /*[loop for div will terminate because operands are non-zero] */
295+ for (msud=div+DIVOPLEN-1; *msud==0;) msud--;
296+ /* the initial least-significant unit of acc is set so acc appears */
297+ /* to have the same length as div. */
298+ /* This moves one position towards the least possible for each */
299+ /* iteration */
300+ divunits=(Int)(msud-div+1); /* precalculate */
301+ lsua=msua-divunits+1; /* initial working lsu of acc */
302+ lsuq=msuq; /* and of quo */
303+
304+ /* set up the estimator for the multiplier; this is the msu of div, */
305+ /* plus two bits from the unit below (if any) rounded up by one if */
306+ /* there are any non-zero bits or units below that [the extra two */
307+ /* bits makes for a much better estimate when the top unit is small] */
308+ divtop=*msud<<2;
309+ if (divunits>1) {
310+ uInt *um=msud-1;
311+ uInt d=*um;
312+ if (d>=750000000) {divtop+=3; d-=750000000;}
313+ else if (d>=500000000) {divtop+=2; d-=500000000;}
314+ else if (d>=250000000) {divtop++; d-=250000000;}
315+ if (d) divtop++;
316+ else for (um--; um>=div; um--) if (*um) {
317+ divtop++;
318+ break;
319+ }
320+ } /* >1 unit */
321+
322+ #if DECTRACE
323+ {Int i;
324+ printf("----- div=");
325+ for (i=divunits-1; i>=0; i--) printf("%09ld ", (LI)div[i]);
326+ printf("\n");}
327+ #endif
328+
329+ /* now collect up to DECPMAX+1 digits in the quotient (this may */
330+ /* need OPLEN+1 uInts if unaligned) */
331+ quodigits=0; /* no digits yet */
332+ for (;; lsua--) { /* outer loop -- each input position */
333+ #if DECCHECK
334+ if (lsua<acc) {
335+ printf("Acc underrun...\n");
336+ break;
337+ }
338+ #endif
339+ #if DECTRACE
340+ printf("Outer: quodigits=%ld acc=", (LI)quodigits);
341+ for (ua=msua; ua>=lsua; ua--) printf("%09ld ", (LI)*ua);
342+ printf("\n");
343+ #endif
344+ *lsuq=0; /* default unit result is 0 */
345+ for (;;) { /* inner loop -- calculate quotient unit */
346+ /* strip leading zero units from acc (either there initially or */
347+ /* from subtraction below); this may strip all if exactly 0 */
348+ for (; *msua==0 && msua>=lsua;) msua--;
349+ accunits=(Int)(msua-lsua+1); /* [maybe 0] */
350+ /* subtraction is only necessary and possible if there are as */
351+ /* least as many units remaining in acc for this iteration as */
352+ /* there are in div */
353+ if (accunits<divunits) {
354+ if (accunits==0) msua++; /* restore */
355+ break;
356+ }
357+
358+ /* If acc is longer than div then subtraction is definitely */
359+ /* possible (as msu of both is non-zero), but if they are the */
360+ /* same length a comparison is needed. */
361+ /* If a subtraction is needed then a good estimate of the */
362+ /* multiplier for the subtraction is also needed in order to */
363+ /* minimise the iterations of this inner loop because the */
364+ /* subtractions needed dominate division performance. */
365+ if (accunits==divunits) {
366+ /* compare the high divunits of acc and div: */
367+ /* acc<div: this quotient unit is unchanged; subtraction */
368+ /* will be possible on the next iteration */
369+ /* acc==div: quotient gains 1, set acc=0 */
370+ /* acc>div: subtraction necessary at this position */
371+ for (ud=msud, ua=msua; ud>div; ud--, ua--) if (*ud!=*ua) break;
372+ /* [now at first mismatch or lsu] */
373+ if (*ud>*ua) break; /* next time... */
374+ if (*ud==*ua) { /* all compared equal */
375+ *lsuq+=1; /* increment result */
376+ msua=lsua; /* collapse acc units */
377+ *msua=0; /* .. to a zero */
378+ break;
379+ }
380+
381+ /* subtraction necessary; estimate multiplier [see above] */
382+ /* if both *msud and *msua are small it is cost-effective to */
383+ /* bring in part of the following units (if any) to get a */
384+ /* better estimate (assume some other non-zero in div) */
385+ #define DIVLO 1000000U
386+ #define DIVHI (DIVBASE/DIVLO)
387+ #if DECUSE64
388+ if (divunits>1) {
389+ /* there cannot be a *(msud-2) for DECDOUBLE so next is */
390+ /* an exact calculation unless DECQUAD (which needs to */
391+ /* assume bits out there if divunits>2) */
392+ uLong mul=(uLong)*msua * DIVBASE + *(msua-1);
393+ uLong div=(uLong)*msud * DIVBASE + *(msud-1);
394+ #if QUAD
395+ if (divunits>2) div++;
396+ #endif
397+ mul/=div;
398+ multiplier=(Int)mul;
399+ }
400+ else multiplier=*msua/(*msud);
401+ #else
402+ if (divunits>1 && *msua<DIVLO && *msud<DIVLO) {
403+ multiplier=(*msua*DIVHI + *(msua-1)/DIVLO)
404+ /(*msud*DIVHI + *(msud-1)/DIVLO +1);
405+ }
406+ else multiplier=(*msua<<2)/divtop;
407+ #endif
408+ }
409+ else { /* accunits>divunits */
410+ /* msud is one unit 'lower' than msua, so estimate differently */
411+ #if DECUSE64
412+ uLong mul;
413+ /* as before, bring in extra digits if possible */
414+ if (divunits>1 && *msua<DIVLO && *msud<DIVLO) {
415+ mul=((uLong)*msua * DIVHI * DIVBASE) + *(msua-1) * DIVHI
416+ + *(msua-2)/DIVLO;
417+ mul/=(*msud*DIVHI + *(msud-1)/DIVLO +1);
418+ }
419+ else if (divunits==1) {
420+ mul=(uLong)*msua * DIVBASE + *(msua-1);
421+ mul/=*msud; /* no more to the right */
422+ }
423+ else {
424+ mul=(uLong)(*msua) * (uInt)(DIVBASE<<2) + (*(msua-1)<<2);
425+ mul/=divtop; /* [divtop already allows for sticky bits] */
426+ }
427+ multiplier=(Int)mul;
428+ #else
429+ multiplier=*msua * ((DIVBASE<<2)/divtop);
430+ #endif
431+ }
432+ if (multiplier==0) multiplier=1; /* marginal case */
433+ *lsuq+=multiplier;
434+
435+ #if DIVCOUNT
436+ /* printf("Multiplier: %ld\n", (LI)multiplier); */
437+ divcount++;
438+ #endif
439+
440+ /* Carry out the subtraction acc-(div*multiplier); for each */
441+ /* unit in div, do the multiply, split to units (see */
442+ /* decFloatMultiply for the algorithm), and subtract from acc */
443+ #define DIVMAGIC 2305843009U /* 2**61/10**9 */
444+ #define DIVSHIFTA 29
445+ #define DIVSHIFTB 32
446+ carry=0;
447+ for (ud=div, ua=lsua; ud<=msud; ud++, ua++) {
448+ uInt lo, hop;
449+ #if DECUSE64
450+ uLong sub=(uLong)multiplier*(*ud)+carry;
451+ if (sub<DIVBASE) {
452+ carry=0;
453+ lo=(uInt)sub;
454+ }
455+ else {
456+ hop=(uInt)(sub>>DIVSHIFTA);
457+ carry=(uInt)(((uLong)hop*DIVMAGIC)>>DIVSHIFTB);
458+ /* the estimate is now in hi; now calculate sub-hi*10**9 */
459+ /* to get the remainder (which will be <DIVBASE)) */
460+ lo=(uInt)sub;
461+ lo-=carry*DIVBASE; /* low word of result */
462+ if (lo>=DIVBASE) {
463+ lo-=DIVBASE; /* correct by +1 */
464+ carry++;
465+ }
466+ }
467+ #else /* 32-bit */
468+ uInt hi;
469+ /* calculate multiplier*(*ud) into hi and lo */
470+ LONGMUL32HI(hi, *ud, multiplier); /* get the high word */
471+ lo=multiplier*(*ud); /* .. and the low */
472+ lo+=carry; /* add the old hi */
473+ carry=hi+(lo<carry); /* .. with any carry */
474+ if (carry || lo>=DIVBASE) { /* split is needed */
475+ hop=(carry<<3)+(lo>>DIVSHIFTA); /* hi:lo/2**29 */
476+ LONGMUL32HI(carry, hop, DIVMAGIC); /* only need the high word */
477+ /* [DIVSHIFTB is 32, so carry can be used directly] */
478+ /* the estimate is now in carry; now calculate hi:lo-est*10**9; */
479+ /* happily the top word of the result is irrelevant because it */
480+ /* will always be zero so this needs only one multiplication */
481+ lo-=(carry*DIVBASE);
482+ /* the correction here will be at most +1; do it */
483+ if (lo>=DIVBASE) {
484+ lo-=DIVBASE;
485+ carry++;
486+ }
487+ }
488+ #endif
489+ if (lo>*ua) { /* borrow needed */
490+ *ua+=DIVBASE;
491+ carry++;
492+ }
493+ *ua-=lo;
494+ } /* ud loop */
495+ if (carry) *ua-=carry; /* accdigits>divdigits [cannot borrow] */
496+ } /* inner loop */
497+
498+ /* the outer loop terminates when there is either an exact result */
499+ /* or enough digits; first update the quotient digit count and */
500+ /* pointer (if any significant digits) */
501+ #if DECTRACE
502+ if (*lsuq || quodigits) printf("*lsuq=%09ld\n", (LI)*lsuq);
503+ #endif
504+ if (quodigits) {
505+ quodigits+=9; /* had leading unit earlier */
506+ lsuq--;
507+ if (quodigits>DECPMAX+1) break; /* have enough */
508+ }
509+ else if (*lsuq) { /* first quotient digits */
510+ const uInt *pow;
511+ for (pow=DECPOWERS; *lsuq>=*pow; pow++) quodigits++;
512+ lsuq--;
513+ /* [cannot have >DECPMAX+1 on first unit] */
514+ }
515+
516+ if (*msua!=0) continue; /* not an exact result */
517+ /* acc is zero iff used all of original units and zero down to lsua */
518+ /* (must also continue to original lsu for correct quotient length) */
519+ if (lsua>acc+DIVACCLEN-DIVOPLEN) continue;
520+ for (; msua>lsua && *msua==0;) msua--;
521+ if (*msua==0 && msua==lsua) break;
522+ } /* outer loop */
523+
524+ /* all of the original operand in acc has been covered at this point */
525+ /* quotient now has at least DECPMAX+2 digits */
526+ /* *msua is now non-0 if inexact and sticky bits */
527+ /* lsuq is one below the last uint of the quotient */
528+ lsuq++; /* set -> true lsu of quo */
529+ if (*msua) *lsuq|=1; /* apply sticky bit */
530+
531+ /* quo now holds the (unrounded) quotient in base-billion; one */
532+ /* base-billion 'digit' per uInt. */
533+ #if DECTRACE
534+ printf("DivQuo:");
535+ for (uq=msuq; uq>=lsuq; uq--) printf(" %09ld", (LI)*uq);
536+ printf("\n");
537+ #endif
538+
539+ /* Now convert to BCD for rounding and cleanup, starting from the */
540+ /* most significant end [offset by one into bcdacc to leave room */
541+ /* for a possible carry digit if rounding for REMNEAR is needed] */
542+ for (uq=msuq, ub=bcdacc+1; uq>=lsuq; uq--, ub+=9) {
543+ uInt top, mid, rem; /* work */
544+ if (*uq==0) { /* no split needed */
545+ UINTAT(ub)=0; /* clear 9 BCD8s */
546+ UINTAT(ub+4)=0; /* .. */
547+ *(ub+8)=0; /* .. */
548+ continue;
549+ }
550+ /* *uq is non-zero -- split the base-billion digit into */
551+ /* hi, mid, and low three-digits */
552+ #define divsplit9 1000000 /* divisor */
553+ #define divsplit6 1000 /* divisor */
554+ /* The splitting is done by simple divides and remainders, */
555+ /* assuming the compiler will optimize these [GCC does] */
556+ top=*uq/divsplit9;
557+ rem=*uq%divsplit9;
558+ mid=rem/divsplit6;
559+ rem=rem%divsplit6;
560+ /* lay out the nine BCD digits (plus one unwanted byte) */
561+ UINTAT(ub) =UINTAT(&BIN2BCD8[top*4]);
562+ UINTAT(ub+3)=UINTAT(&BIN2BCD8[mid*4]);
563+ UINTAT(ub+6)=UINTAT(&BIN2BCD8[rem*4]);
564+ } /* BCD conversion loop */
565+ ub--; /* -> lsu */
566+
567+ /* complete the bcdnum; quodigits is correct, so the position of */
568+ /* the first non-zero is known */
569+ num.msd=bcdacc+1+(msuq-lsuq+1)*9-quodigits;
570+ num.lsd=ub;
571+
572+ /* make exponent adjustments, etc */
573+ if (lsua<acc+DIVACCLEN-DIVOPLEN) { /* used extra digits */
574+ num.exponent-=(Int)((acc+DIVACCLEN-DIVOPLEN-lsua)*9);
575+ /* if the result was exact then there may be up to 8 extra */
576+ /* trailing zeros in the overflowed quotient final unit */
577+ if (*msua==0) {
578+ for (; *ub==0;) ub--; /* drop zeros */
579+ num.exponent+=(Int)(num.lsd-ub); /* and adjust exponent */
580+ num.lsd=ub;
581+ }
582+ } /* adjustment needed */
583+
584+ #if DIVCOUNT
585+ if (divcount>maxcount) { /* new high-water nark */
586+ maxcount=divcount;
587+ printf("DivNewMaxCount: %ld\n", (LI)maxcount);
588+ }
589+ #endif
590+
591+ if (op&DIVIDE) return decFinalize(result, &num, set); /* all done */
592+
593+ /* Is DIVIDEINT or a remainder; there is more to do -- first form */
594+ /* the integer (this is done 'after the fact', unlike as in */
595+ /* decNumber, so as not to tax DIVIDE) */
596+
597+ /* The first non-zero digit will be in the first 9 digits, known */
598+ /* from quodigits and num.msd, so there is always space for DECPMAX */
599+ /* digits */
600+
601+ length=(Int)(num.lsd-num.msd+1);
602+ /*printf("Length exp: %ld %ld\n", (LI)length, (LI)num.exponent); */
603+
604+ if (length+num.exponent>DECPMAX) { /* cannot fit */
605+ decFloatZero(result);
606+ DFWORD(result, 0)=DECFLOAT_qNaN;
607+ set->status|=DEC_Division_impossible;
608+ return result;
609+ }
610+
611+ if (num.exponent>=0) { /* already an int, or need pad zeros */
612+ for (ub=num.lsd+1; ub<=num.lsd+num.exponent; ub++) *ub=0;
613+ num.lsd+=num.exponent;
614+ }
615+ else { /* too long: round or truncate needed */
616+ Int drop=-num.exponent;
617+ if (!(op&REMNEAR)) { /* simple truncate */
618+ num.lsd-=drop;
619+ if (num.lsd<num.msd) { /* truncated all */
620+ num.lsd=num.msd; /* make 0 */
621+ *num.lsd=0; /* .. [sign still relevant] */
622+ }
623+ }
624+ else { /* round to nearest even [sigh] */
625+ /* round-to-nearest, in-place; msd is at or to right of bcdacc+1 */
626+ /* (this is a special case of Quantize -- q.v. for commentary) */
627+ uByte *roundat; /* -> re-round digit */
628+ uByte reround; /* reround value */
629+ *(num.msd-1)=0; /* in case of left carry, or make 0 */
630+ if (drop<length) roundat=num.lsd-drop+1;
631+ else if (drop==length) roundat=num.msd;
632+ else roundat=num.msd-1; /* [-> 0] */
633+ reround=*roundat;
634+ for (ub=roundat+1; ub<=num.lsd; ub++) {
635+ if (*ub!=0) {
636+ reround=DECSTICKYTAB[reround];
637+ break;
638+ }
639+ } /* check stickies */
640+ if (roundat>num.msd) num.lsd=roundat-1;
641+ else {
642+ num.msd--; /* use the 0 .. */
643+ num.lsd=num.msd; /* .. at the new MSD place */
644+ }
645+ if (reround!=0) { /* discarding non-zero */
646+ uInt bump=0;
647+ /* rounding is DEC_ROUND_HALF_EVEN always */
648+ if (reround>5) bump=1; /* >0.5 goes up */
649+ else if (reround==5) /* exactly 0.5000 .. */
650+ bump=*(num.lsd) & 0x01; /* .. up iff [new] lsd is odd */
651+ if (bump!=0) { /* need increment */
652+ /* increment the coefficient; this might end up with 1000... */
653+ ub=num.lsd;
654+ for (; UINTAT(ub-3)==0x09090909; ub-=4) UINTAT(ub-3)=0;
655+ for (; *ub==9; ub--) *ub=0; /* at most 3 more */
656+ *ub+=1;
657+ if (ub<num.msd) num.msd--; /* carried */
658+ } /* bump needed */
659+ } /* reround!=0 */
660+ } /* remnear */
661+ } /* round or truncate needed */
662+ num.exponent=0; /* all paths */
663+ /*decShowNum(&num, "int"); */
664+
665+ if (op&DIVIDEINT) return decFinalize(result, &num, set); /* all done */
666+
667+ /* Have a remainder to calculate */
668+ decFinalize(&quotient, &num, set); /* lay out the integer so far */
669+ DFWORD(&quotient, 0)^=DECFLOAT_Sign; /* negate it */
670+ sign=DFWORD(dfl, 0); /* save sign of dfl */
671+ decFloatFMA(result, &quotient, dfr, dfl, set);
672+ if (!DFISZERO(result)) return result;
673+ /* if the result is zero the sign shall be sign of dfl */
674+ DFWORD(&quotient, 0)=sign; /* construct decFloat of sign */
675+ return decFloatCopySign(result, result, &quotient);
676+ } /* decDivide */
677+
678+/* ------------------------------------------------------------------ */
679+/* decFiniteMultiply -- multiply two finite decFloats */
680+/* */
681+/* num gets the result of multiplying dfl and dfr */
682+/* bcdacc .. with the coefficient in this array */
683+/* dfl is the first decFloat (lhs) */
684+/* dfr is the second decFloat (rhs) */
685+/* */
686+/* This effects the multiplication of two decFloats, both known to be */
687+/* finite, leaving the result in a bcdnum ready for decFinalize (for */
688+/* use in Multiply) or in a following addition (FMA). */
689+/* */
690+/* bcdacc must have space for at least DECPMAX9*18+1 bytes. */
691+/* No error is possible and no status is set. */
692+/* ------------------------------------------------------------------ */
693+/* This routine has two separate implementations of the core */
694+/* multiplication; both using base-billion. One uses only 32-bit */
695+/* variables (Ints and uInts) or smaller; the other uses uLongs (for */
696+/* multiplication and addition only). Both implementations cover */
697+/* both arithmetic sizes (DOUBLE and QUAD) in order to allow timing */
698+/* comparisons. In any one compilation only one implementation for */
699+/* each size can be used, and if DECUSE64 is 0 then use of the 32-bit */
700+/* version is forced. */
701+/* */
702+/* Historical note: an earlier version of this code also supported the */
703+/* 256-bit format and has been preserved. That is somewhat trickier */
704+/* during lazy carry splitting because the initial quotient estimate */
705+/* (est) can exceed 32 bits. */
706+
707+#define MULTBASE BILLION /* the base used for multiply */
708+#define MULOPLEN DECPMAX9 /* operand length ('digits' base 10**9) */
709+#define MULACCLEN (MULOPLEN*2) /* accumulator length (ditto) */
710+#define LEADZEROS (MULACCLEN*9 - DECPMAX*2) /* leading zeros always */
711+
712+/* Assertions: exponent not too large and MULACCLEN is a multiple of 4 */
713+#if DECEMAXD>9
714+ #error Exponent may overflow when doubled for Multiply
715+#endif
716+#if MULACCLEN!=(MULACCLEN/4)*4
717+ /* This assumption is used below only for initialization */
718+ #error MULACCLEN is not a multiple of 4
719+#endif
720+
721+static void decFiniteMultiply(bcdnum *num, uByte *bcdacc,
722+ const decFloat *dfl, const decFloat *dfr) {
723+ uInt bufl[MULOPLEN]; /* left coefficient (base-billion) */
724+ uInt bufr[MULOPLEN]; /* right coefficient (base-billion) */
725+ uInt *ui, *uj; /* work */
726+ uByte *ub; /* .. */
727+
728+ #if DECUSE64
729+ uLong accl[MULACCLEN]; /* lazy accumulator (base-billion+) */
730+ uLong *pl; /* work -> lazy accumulator */
731+ uInt acc[MULACCLEN]; /* coefficent in base-billion .. */
732+ #else
733+ uInt acc[MULACCLEN*2]; /* accumulator in base-billion .. */
734+ #endif
735+ uInt *pa; /* work -> accumulator */
736+ /*printf("Base10**9: OpLen=%d MulAcclen=%d\n", OPLEN, MULACCLEN); */
737+
738+ /* Calculate sign and exponent */
739+ num->sign=(DFWORD(dfl, 0)^DFWORD(dfr, 0)) & DECFLOAT_Sign;
740+ num->exponent=GETEXPUN(dfl)+GETEXPUN(dfr); /* [see assertion above] */
741+
742+ /* Extract the coefficients and prepare the accumulator */
743+ /* the coefficients of the operands are decoded into base-billion */
744+ /* numbers in uInt arrays (bufl and bufr, LSD at offset 0) of the */
745+ /* appropriate size. */
746+ GETCOEFFBILL(dfl, bufl);
747+ GETCOEFFBILL(dfr, bufr);
748+ #if DECTRACE && 0
749+ printf("CoeffbL:");
750+ for (ui=bufl+MULOPLEN-1; ui>=bufl; ui--) printf(" %08lx", (LI)*ui);
751+ printf("\n");
752+ printf("CoeffbR:");
753+ for (uj=bufr+MULOPLEN-1; uj>=bufr; uj--) printf(" %08lx", (LI)*uj);
754+ printf("\n");
755+ #endif
756+
757+ /* start the 64-bit/32-bit differing paths... */
758+#if DECUSE64
759+
760+ /* zero the accumulator */
761+ #if MULACCLEN==4
762+ accl[0]=0; accl[1]=0; accl[2]=0; accl[3]=0;
763+ #else /* use a loop */
764+ /* MULACCLEN is a multiple of four, asserted above */
765+ for (pl=accl; pl<accl+MULACCLEN; pl+=4) {
766+ *pl=0; *(pl+1)=0; *(pl+2)=0; *(pl+3)=0;/* [reduce overhead] */
767+ } /* pl */
768+ #endif
769+
770+ /* Effect the multiplication */
771+ /* The multiplcation proceeds using MFC's lazy-carry resolution */
772+ /* algorithm from decNumber. First, the multiplication is */
773+ /* effected, allowing accumulation of the partial products (which */
774+ /* are in base-billion at each column position) into 64 bits */
775+ /* without resolving back to base=billion after each addition. */
776+ /* These 64-bit numbers (which may contain up to 19 decimal digits) */
777+ /* are then split using the Clark & Cowlishaw algorithm (see below). */
778+ /* [Testing for 0 in the inner loop is not really a 'win'] */
779+ for (ui=bufr; ui<bufr+MULOPLEN; ui++) { /* over each item in rhs */
780+ if (*ui==0) continue; /* product cannot affect result */
781+ pl=accl+(ui-bufr); /* where to add the lhs */
782+ for (uj=bufl; uj<bufl+MULOPLEN; uj++, pl++) { /* over each item in lhs */
783+ /* if (*uj==0) continue; // product cannot affect result */
784+ *pl+=((uLong)*ui)*(*uj);
785+ } /* uj */
786+ } /* ui */
787+
788+ /* The 64-bit carries must now be resolved; this means that a */
789+ /* quotient/remainder has to be calculated for base-billion (1E+9). */
790+ /* For this, Clark & Cowlishaw's quotient estimation approach (also */
791+ /* used in decNumber) is needed, because 64-bit divide is generally */
792+ /* extremely slow on 32-bit machines, and may be slower than this */
793+ /* approach even on 64-bit machines. This algorithm splits X */
794+ /* using: */
795+ /* */
796+ /* magic=2**(A+B)/1E+9; // 'magic number' */
797+ /* hop=X/2**A; // high order part of X (by shift) */
798+ /* est=magic*hop/2**B // quotient estimate (may be low by 1) */
799+ /* */
800+ /* A and B are quite constrained; hop and magic must fit in 32 bits, */
801+ /* and 2**(A+B) must be as large as possible (which is 2**61 if */
802+ /* magic is to fit). Further, maxX increases with the length of */
803+ /* the operands (and hence the number of partial products */
804+ /* accumulated); maxX is OPLEN*(10**18), which is up to 19 digits. */
805+ /* */
806+ /* It can be shown that when OPLEN is 2 then the maximum error in */
807+ /* the estimated quotient is <1, but for larger maximum x the */
808+ /* maximum error is above 1 so a correction that is >1 may be */
809+ /* needed. Values of A and B are chosen to satisfy the constraints */
810+ /* just mentioned while minimizing the maximum error (and hence the */
811+ /* maximum correction), as shown in the following table: */
812+ /* */
813+ /* Type OPLEN A B maxX maxError maxCorrection */
814+ /* --------------------------------------------------------- */
815+ /* DOUBLE 2 29 32 <2*10**18 0.63 1 */
816+ /* QUAD 4 30 31 <4*10**18 1.17 2 */
817+ /* */
818+ /* In the OPLEN==2 case there is most choice, but the value for B */
819+ /* of 32 has a big advantage as then the calculation of the */
820+ /* estimate requires no shifting; the compiler can extract the high */
821+ /* word directly after multiplying magic*hop. */
822+ #define MULMAGIC 2305843009U /* 2**61/10**9 [both cases] */
823+ #if DOUBLE
824+ #define MULSHIFTA 29
825+ #define MULSHIFTB 32
826+ #elif QUAD
827+ #define MULSHIFTA 30
828+ #define MULSHIFTB 31
829+ #else
830+ #error Unexpected type
831+ #endif
832+
833+ #if DECTRACE
834+ printf("MulAccl:");
835+ for (pl=accl+MULACCLEN-1; pl>=accl; pl--)
836+ printf(" %08lx:%08lx", (LI)(*pl>>32), (LI)(*pl&0xffffffff));
837+ printf("\n");
838+ #endif
839+
840+ for (pl=accl, pa=acc; pl<accl+MULACCLEN; pl++, pa++) { /* each column position */
841+ uInt lo, hop; /* work */
842+ uInt est; /* cannot exceed 4E+9 */
843+ if (*pl>MULTBASE) {
844+ /* *pl holds a binary number which needs to be split */
845+ hop=(uInt)(*pl>>MULSHIFTA);
846+ est=(uInt)(((uLong)hop*MULMAGIC)>>MULSHIFTB);
847+ /* the estimate is now in est; now calculate hi:lo-est*10**9; */
848+ /* happily the top word of the result is irrelevant because it */
849+ /* will always be zero so this needs only one multiplication */
850+ lo=(uInt)(*pl-((uLong)est*MULTBASE)); /* low word of result */
851+ /* If QUAD, the correction here could be +2 */
852+ if (lo>=MULTBASE) {
853+ lo-=MULTBASE; /* correct by +1 */
854+ est++;
855+ #if QUAD
856+ /* may need to correct by +2 */
857+ if (lo>=MULTBASE) {
858+ lo-=MULTBASE;
859+ est++;
860+ }
861+ #endif
862+ }
863+ /* finally place lo as the new coefficient 'digit' and add est to */
864+ /* the next place up [this is safe because this path is never */
865+ /* taken on the final iteration as *pl will fit] */
866+ *pa=lo;
867+ *(pl+1)+=est;
868+ } /* *pl needed split */
869+ else { /* *pl<MULTBASE */
870+ *pa=(uInt)*pl; /* just copy across */
871+ }
872+ } /* pl loop */
873+
874+#else /* 32-bit */
875+ for (pa=acc;; pa+=4) { /* zero the accumulator */
876+ *pa=0; *(pa+1)=0; *(pa+2)=0; *(pa+3)=0; /* [reduce overhead] */
877+ if (pa==acc+MULACCLEN*2-4) break; /* multiple of 4 asserted */
878+ } /* pa */
879+
880+ /* Effect the multiplication */
881+ /* uLongs are not available (and in particular, there is no uLong */
882+ /* divide) but it is still possible to use MFC's lazy-carry */
883+ /* resolution algorithm from decNumber. First, the multiplication */
884+ /* is effected, allowing accumulation of the partial products */
885+ /* (which are in base-billion at each column position) into 64 bits */
886+ /* [with the high-order 32 bits in each position being held at */
887+ /* offset +ACCLEN from the low-order 32 bits in the accumulator]. */
888+ /* These 64-bit numbers (which may contain up to 19 decimal digits) */
889+ /* are then split using the Clark & Cowlishaw algorithm (see */
890+ /* below). */
891+ for (ui=bufr;; ui++) { /* over each item in rhs */
892+ uInt hi, lo; /* words of exact multiply result */
893+ pa=acc+(ui-bufr); /* where to add the lhs */
894+ for (uj=bufl;; uj++, pa++) { /* over each item in lhs */
895+ LONGMUL32HI(hi, *ui, *uj); /* calculate product of digits */
896+ lo=(*ui)*(*uj); /* .. */
897+ *pa+=lo; /* accumulate low bits and .. */
898+ *(pa+MULACCLEN)+=hi+(*pa<lo); /* .. high bits with any carry */
899+ if (uj==bufl+MULOPLEN-1) break;
900+ }
901+ if (ui==bufr+MULOPLEN-1) break;
902+ }
903+
904+ /* The 64-bit carries must now be resolved; this means that a */
905+ /* quotient/remainder has to be calculated for base-billion (1E+9). */
906+ /* For this, Clark & Cowlishaw's quotient estimation approach (also */
907+ /* used in decNumber) is needed, because 64-bit divide is generally */
908+ /* extremely slow on 32-bit machines. This algorithm splits X */
909+ /* using: */
910+ /* */
911+ /* magic=2**(A+B)/1E+9; // 'magic number' */
912+ /* hop=X/2**A; // high order part of X (by shift) */
913+ /* est=magic*hop/2**B // quotient estimate (may be low by 1) */
914+ /* */
915+ /* A and B are quite constrained; hop and magic must fit in 32 bits, */
916+ /* and 2**(A+B) must be as large as possible (which is 2**61 if */
917+ /* magic is to fit). Further, maxX increases with the length of */
918+ /* the operands (and hence the number of partial products */
919+ /* accumulated); maxX is OPLEN*(10**18), which is up to 19 digits. */
920+ /* */
921+ /* It can be shown that when OPLEN is 2 then the maximum error in */
922+ /* the estimated quotient is <1, but for larger maximum x the */
923+ /* maximum error is above 1 so a correction that is >1 may be */
924+ /* needed. Values of A and B are chosen to satisfy the constraints */
925+ /* just mentioned while minimizing the maximum error (and hence the */
926+ /* maximum correction), as shown in the following table: */
927+ /* */
928+ /* Type OPLEN A B maxX maxError maxCorrection */
929+ /* --------------------------------------------------------- */
930+ /* DOUBLE 2 29 32 <2*10**18 0.63 1 */
931+ /* QUAD 4 30 31 <4*10**18 1.17 2 */
932+ /* */
933+ /* In the OPLEN==2 case there is most choice, but the value for B */
934+ /* of 32 has a big advantage as then the calculation of the */
935+ /* estimate requires no shifting; the high word is simply */
936+ /* calculated from multiplying magic*hop. */
937+ #define MULMAGIC 2305843009U /* 2**61/10**9 [both cases] */
938+ #if DOUBLE
939+ #define MULSHIFTA 29
940+ #define MULSHIFTB 32
941+ #elif QUAD
942+ #define MULSHIFTA 30
943+ #define MULSHIFTB 31
944+ #else
945+ #error Unexpected type
946+ #endif
947+
948+ #if DECTRACE
949+ printf("MulHiLo:");
950+ for (pa=acc+MULACCLEN-1; pa>=acc; pa--)
951+ printf(" %08lx:%08lx", (LI)*(pa+MULACCLEN), (LI)*pa);
952+ printf("\n");
953+ #endif
954+
955+ for (pa=acc;; pa++) { /* each low uInt */
956+ uInt hi, lo; /* words of exact multiply result */
957+ uInt hop, estlo; /* work */
958+ #if QUAD
959+ uInt esthi; /* .. */
960+ #endif
961+
962+ lo=*pa;
963+ hi=*(pa+MULACCLEN); /* top 32 bits */
964+ /* hi and lo now hold a binary number which needs to be split */
965+
966+ #if DOUBLE
967+ hop=(hi<<3)+(lo>>MULSHIFTA); /* hi:lo/2**29 */
968+ LONGMUL32HI(estlo, hop, MULMAGIC);/* only need the high word */
969+ /* [MULSHIFTB is 32, so estlo can be used directly] */
970+ /* the estimate is now in estlo; now calculate hi:lo-est*10**9; */
971+ /* happily the top word of the result is irrelevant because it */
972+ /* will always be zero so this needs only one multiplication */
973+ lo-=(estlo*MULTBASE);
974+ /* esthi=0; // high word is ignored below */
975+ /* the correction here will be at most +1; do it */
976+ if (lo>=MULTBASE) {
977+ lo-=MULTBASE;
978+ estlo++;
979+ }
980+ #elif QUAD
981+ hop=(hi<<2)+(lo>>MULSHIFTA); /* hi:lo/2**30 */
982+ LONGMUL32HI(esthi, hop, MULMAGIC);/* shift will be 31 .. */
983+ estlo=hop*MULMAGIC; /* .. so low word needed */
984+ estlo=(esthi<<1)+(estlo>>MULSHIFTB); /* [just the top bit] */
985+ /* esthi=0; // high word is ignored below */
986+ lo-=(estlo*MULTBASE); /* as above */
987+ /* the correction here could be +1 or +2 */
988+ if (lo>=MULTBASE) {
989+ lo-=MULTBASE;
990+ estlo++;
991+ }
992+ if (lo>=MULTBASE) {
993+ lo-=MULTBASE;
994+ estlo++;
995+ }
996+ #else
997+ #error Unexpected type
998+ #endif
999+
1000+ /* finally place lo as the new accumulator digit and add est to */
1001+ /* the next place up; this latter add could cause a carry of 1 */
1002+ /* to the high word of the next place */
1003+ *pa=lo;
1004+ *(pa+1)+=estlo;
1005+ /* esthi is always 0 for DOUBLE and QUAD so this is skipped */
1006+ /* *(pa+1+MULACCLEN)+=esthi; */
1007+ if (*(pa+1)<estlo) *(pa+1+MULACCLEN)+=1; /* carry */
1008+ if (pa==acc+MULACCLEN-2) break; /* [MULACCLEN-1 will never need split] */
1009+ } /* pa loop */
1010+#endif
1011+
1012+ /* At this point, whether using the 64-bit or the 32-bit paths, the */
1013+ /* accumulator now holds the (unrounded) result in base-billion; */
1014+ /* one base-billion 'digit' per uInt. */
1015+ #if DECTRACE
1016+ printf("MultAcc:");
1017+ for (pa=acc+MULACCLEN-1; pa>=acc; pa--) printf(" %09ld", (LI)*pa);
1018+ printf("\n");
1019+ #endif
1020+
1021+ /* Now convert to BCD for rounding and cleanup, starting from the */
1022+ /* most significant end */
1023+ pa=acc+MULACCLEN-1;
1024+ if (*pa!=0) num->msd=bcdacc+LEADZEROS;/* drop known lead zeros */
1025+ else { /* >=1 word of leading zeros */
1026+ num->msd=bcdacc; /* known leading zeros are gone */
1027+ pa--; /* skip first word .. */
1028+ for (; *pa==0; pa--) if (pa==acc) break; /* .. and any more leading 0s */
1029+ }
1030+ for (ub=bcdacc;; pa--, ub+=9) {
1031+ if (*pa!=0) { /* split(s) needed */
1032+ uInt top, mid, rem; /* work */
1033+ /* *pa is non-zero -- split the base-billion acc digit into */
1034+ /* hi, mid, and low three-digits */
1035+ #define mulsplit9 1000000 /* divisor */
1036+ #define mulsplit6 1000 /* divisor */
1037+ /* The splitting is done by simple divides and remainders, */
1038+ /* assuming the compiler will optimize these where useful */
1039+ /* [GCC does] */
1040+ top=*pa/mulsplit9;
1041+ rem=*pa%mulsplit9;
1042+ mid=rem/mulsplit6;
1043+ rem=rem%mulsplit6;
1044+ /* lay out the nine BCD digits (plus one unwanted byte) */
1045+ UINTAT(ub) =UINTAT(&BIN2BCD8[top*4]);
1046+ UINTAT(ub+3)=UINTAT(&BIN2BCD8[mid*4]);
1047+ UINTAT(ub+6)=UINTAT(&BIN2BCD8[rem*4]);
1048+ }
1049+ else { /* *pa==0 */
1050+ UINTAT(ub)=0; /* clear 9 BCD8s */
1051+ UINTAT(ub+4)=0; /* .. */
1052+ *(ub+8)=0; /* .. */
1053+ }
1054+ if (pa==acc) break;
1055+ } /* BCD conversion loop */
1056+
1057+ num->lsd=ub+8; /* complete the bcdnum .. */
1058+
1059+ #if DECTRACE
1060+ decShowNum(num, "postmult");
1061+ decFloatShow(dfl, "dfl");
1062+ decFloatShow(dfr, "dfr");
1063+ #endif
1064+ return;
1065+ } /* decFiniteMultiply */
1066+
1067+/* ------------------------------------------------------------------ */
1068+/* decFloatAbs -- absolute value, heeding NaNs, etc. */
1069+/* */
1070+/* result gets the canonicalized df with sign 0 */
1071+/* df is the decFloat to abs */
1072+/* set is the context */
1073+/* returns result */
1074+/* */
1075+/* This has the same effect as decFloatPlus unless df is negative, */
1076+/* in which case it has the same effect as decFloatMinus. The */
1077+/* effect is also the same as decFloatCopyAbs except that NaNs are */
1078+/* handled normally (the sign of a NaN is not affected, and an sNaN */
1079+/* will signal) and the result will be canonical. */
1080+/* ------------------------------------------------------------------ */
1081+decFloat * decFloatAbs(decFloat *result, const decFloat *df,
1082+ decContext *set) {
1083+ if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
1084+ decCanonical(result, df); /* copy and check */
1085+ DFBYTE(result, 0)&=~0x80; /* zero sign bit */
1086+ return result;
1087+ } /* decFloatAbs */
1088+
1089+/* ------------------------------------------------------------------ */
1090+/* decFloatAdd -- add two decFloats */
1091+/* */
1092+/* result gets the result of adding dfl and dfr: */
1093+/* dfl is the first decFloat (lhs) */
1094+/* dfr is the second decFloat (rhs) */
1095+/* set is the context */
1096+/* returns result */
1097+/* */
1098+/* ------------------------------------------------------------------ */
1099+decFloat * decFloatAdd(decFloat *result,
1100+ const decFloat *dfl, const decFloat *dfr,
1101+ decContext *set) {
1102+ bcdnum num; /* for final conversion */
1103+ Int expl, expr; /* left and right exponents */
1104+ uInt *ui, *uj; /* work */
1105+ uByte *ub; /* .. */
1106+
1107+ uInt sourhil, sourhir; /* top words from source decFloats */
1108+ /* [valid only until specials */
1109+ /* handled or exponents decoded] */
1110+ uInt diffsign; /* non-zero if signs differ */
1111+ uInt carry; /* carry: 0 or 1 before add loop */
1112+ Int overlap; /* coefficient overlap (if full) */
1113+ /* the following buffers hold coefficients with various alignments */
1114+ /* (see commentary and diagrams below) */
1115+ uByte acc[4+2+DECPMAX*3+8];
1116+ uByte buf[4+2+DECPMAX*2];
1117+ uByte *umsd, *ulsd; /* local MSD and LSD pointers */
1118+
1119+ #if DECLITEND
1120+ #define CARRYPAT 0x01000000 /* carry=1 pattern */
1121+ #else
1122+ #define CARRYPAT 0x00000001 /* carry=1 pattern */
1123+ #endif
1124+
1125+ /* Start decoding the arguments */
1126+ /* the initial exponents are placed into the opposite Ints to */
1127+ /* that which might be expected; there are two sets of data to */
1128+ /* keep track of (each decFloat and the corresponding exponent), */
1129+ /* and this scheme means that at the swap point (after comparing */
1130+ /* exponents) only one pair of words needs to be swapped */
1131+ /* whichever path is taken (thereby minimising worst-case path) */
1132+ sourhil=DFWORD(dfl, 0); /* LHS top word */
1133+ expr=DECCOMBEXP[sourhil>>26]; /* get exponent high bits (in place) */
1134+ sourhir=DFWORD(dfr, 0); /* RHS top word */
1135+ expl=DECCOMBEXP[sourhir>>26];
1136+
1137+ diffsign=(sourhil^sourhir)&DECFLOAT_Sign;
1138+
1139+ if (EXPISSPECIAL(expl | expr)) { /* either is special? */
1140+ if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
1141+ /* one or two infinities */
1142+ /* two infinities with different signs is invalid */
1143+ if (diffsign && DFISINF(dfl) && DFISINF(dfr))
1144+ return decInvalid(result, set);
1145+ if (DFISINF(dfl)) return decInfinity(result, dfl); /* LHS is infinite */
1146+ return decInfinity(result, dfr); /* RHS must be Infinite */
1147+ }
1148+
1149+ /* Here when both arguments are finite */
1150+
1151+ /* complete exponent gathering (keeping swapped) */
1152+ expr+=GETECON(dfl)-DECBIAS; /* .. + continuation and unbias */
1153+ expl+=GETECON(dfr)-DECBIAS;
1154+ /* here expr has exponent from lhs, and vice versa */
1155+
1156+ /* now swap either exponents or argument pointers */
1157+ if (expl<=expr) {
1158+ /* original left is bigger */
1159+ Int expswap=expl;
1160+ expl=expr;
1161+ expr=expswap;
1162+ /* printf("left bigger\n"); */
1163+ }
1164+ else {
1165+ const decFloat *dfswap=dfl;
1166+ dfl=dfr;
1167+ dfr=dfswap;
1168+ /* printf("right bigger\n"); */
1169+ }
1170+ /* [here dfl and expl refer to the datum with the larger exponent, */
1171+ /* of if the exponents are equal then the original LHS argument] */
1172+
1173+ /* if lhs is zero then result will be the rhs (now known to have */
1174+ /* the smaller exponent), which also may need to be tested for zero */
1175+ /* for the weird IEEE 754 sign rules */
1176+ if (DFISZERO(dfl)) {
1177+ decCanonical(result, dfr); /* clean copy */
1178+ /* "When the sum of two operands with opposite signs is */
1179+ /* exactly zero, the sign of that sum shall be '+' in all */
1180+ /* rounding modes except round toward -Infinity, in which */
1181+ /* mode that sign shall be '-'." */
1182+ if (diffsign && DFISZERO(result)) {
1183+ DFWORD(result, 0)&=~DECFLOAT_Sign; /* assume sign 0 */
1184+ if (set->round==DEC_ROUND_FLOOR) DFWORD(result, 0)|=DECFLOAT_Sign;
1185+ }
1186+ return result;
1187+ } /* numfl is zero */
1188+ /* [here, LHS is non-zero; code below assumes that] */
1189+
1190+ /* Coefficients layout during the calculations to follow: */
1191+ /* */
1192+ /* Overlap case: */
1193+ /* +------------------------------------------------+ */
1194+ /* acc: |0000| coeffa | tail B | | */
1195+ /* +------------------------------------------------+ */
1196+ /* buf: |0000| pad0s | coeffb | | */
1197+ /* +------------------------------------------------+ */
1198+ /* */
1199+ /* Touching coefficients or gap: */
1200+ /* +------------------------------------------------+ */
1201+ /* acc: |0000| coeffa | gap | coeffb | */
1202+ /* +------------------------------------------------+ */
1203+ /* [buf not used or needed; gap clamped to Pmax] */
1204+
1205+ /* lay out lhs coefficient into accumulator; this starts at acc+4 */
1206+ /* for decDouble or acc+6 for decQuad so the LSD is word- */
1207+ /* aligned; the top word gap is there only in case a carry digit */
1208+ /* is prefixed after the add -- it does not need to be zeroed */
1209+ #if DOUBLE
1210+ #define COFF 4 /* offset into acc */
1211+ #elif QUAD
1212+ USHORTAT(acc+4)=0; /* prefix 00 */
1213+ #define COFF 6 /* offset into acc */
1214+ #endif
1215+
1216+ GETCOEFF(dfl, acc+COFF); /* decode from decFloat */
1217+ ulsd=acc+COFF+DECPMAX-1;
1218+ umsd=acc+4; /* [having this here avoids */
1219+ /* weird GCC optimizer failure] */
1220+ #if DECTRACE
1221+ {bcdnum tum;
1222+ tum.msd=umsd;
1223+ tum.lsd=ulsd;
1224+ tum.exponent=expl;
1225+ tum.sign=DFWORD(dfl, 0) & DECFLOAT_Sign;
1226+ decShowNum(&tum, "dflx");}
1227+ #endif
1228+
1229+ /* if signs differ, take ten's complement of lhs (here the */
1230+ /* coefficient is subtracted from all-nines; the 1 is added during */
1231+ /* the later add cycle -- zeros to the right do not matter because */
1232+ /* the complement of zero is zero); these are fixed-length inverts */
1233+ /* where the lsd is known to be at a 4-byte boundary (so no borrow */
1234+ /* possible) */
1235+ carry=0; /* assume no carry */
1236+ if (diffsign) {
1237+ carry=CARRYPAT; /* for +1 during add */
1238+ UINTAT(acc+ 4)=0x09090909-UINTAT(acc+ 4);
1239+ UINTAT(acc+ 8)=0x09090909-UINTAT(acc+ 8);
1240+ UINTAT(acc+12)=0x09090909-UINTAT(acc+12);
1241+ UINTAT(acc+16)=0x09090909-UINTAT(acc+16);
1242+ #if QUAD
1243+ UINTAT(acc+20)=0x09090909-UINTAT(acc+20);
1244+ UINTAT(acc+24)=0x09090909-UINTAT(acc+24);
1245+ UINTAT(acc+28)=0x09090909-UINTAT(acc+28);
1246+ UINTAT(acc+32)=0x09090909-UINTAT(acc+32);
1247+ UINTAT(acc+36)=0x09090909-UINTAT(acc+36);
1248+ #endif
1249+ } /* diffsign */
1250+
1251+ /* now process the rhs coefficient; if it cannot overlap lhs then */
1252+ /* it can be put straight into acc (with an appropriate gap, if */
1253+ /* needed) because no actual addition will be needed (except */
1254+ /* possibly to complete ten's complement) */
1255+ overlap=DECPMAX-(expl-expr);
1256+ #if DECTRACE
1257+ printf("exps: %ld %ld\n", (LI)expl, (LI)expr);
1258+ printf("Overlap=%ld carry=%08lx\n", (LI)overlap, (LI)carry);
1259+ #endif
1260+
1261+ if (overlap<=0) { /* no overlap possible */
1262+ uInt gap; /* local work */
1263+ /* since a full addition is not needed, a ten's complement */
1264+ /* calculation started above may need to be completed */
1265+ if (carry) {
1266+ for (ub=ulsd; *ub==9; ub--) *ub=0;
1267+ *ub+=1;
1268+ carry=0; /* taken care of */
1269+ }
1270+ /* up to DECPMAX-1 digits of the final result can extend down */
1271+ /* below the LSD of the lhs, so if the gap is >DECPMAX then the */
1272+ /* rhs will be simply sticky bits. In this case the gap is */
1273+ /* clamped to DECPMAX and the exponent adjusted to suit [this is */
1274+ /* safe because the lhs is non-zero]. */
1275+ gap=-overlap;
1276+ if (gap>DECPMAX) {
1277+ expr+=gap-1;
1278+ gap=DECPMAX;
1279+ }
1280+ ub=ulsd+gap+1; /* where MSD will go */
1281+ /* Fill the gap with 0s; note that there is no addition to do */
1282+ ui=&UINTAT(acc+COFF+DECPMAX); /* start of gap */
1283+ for (; ui<&UINTAT(ub); ui++) *ui=0; /* mind the gap */
1284+ if (overlap<-DECPMAX) { /* gap was > DECPMAX */
1285+ *ub=(uByte)(!DFISZERO(dfr)); /* make sticky digit */
1286+ }
1287+ else { /* need full coefficient */
1288+ GETCOEFF(dfr, ub); /* decode from decFloat */
1289+ ub+=DECPMAX-1; /* new LSD... */
1290+ }
1291+ ulsd=ub; /* save new LSD */
1292+ } /* no overlap possible */
1293+
1294+ else { /* overlap>0 */
1295+ /* coefficients overlap (perhaps completely, although also */
1296+ /* perhaps only where zeros) */
1297+ ub=buf+COFF+DECPMAX-overlap; /* where MSD will go */
1298+ /* Fill the prefix gap with 0s; 8 will cover most common */
1299+ /* unalignments, so start with direct assignments (a loop is */
1300+ /* then used for any remaining -- the loop (and the one in a */
1301+ /* moment) is not then on the critical path because the number */
1302+ /* of additions is reduced by (at least) two in this case) */
1303+ UINTAT(buf+4)=0; /* [clears decQuad 00 too] */
1304+ UINTAT(buf+8)=0;
1305+ if (ub>buf+12) {
1306+ ui=&UINTAT(buf+12); /* start of any remaining */
1307+ for (; ui<&UINTAT(ub); ui++) *ui=0; /* fill them */
1308+ }
1309+ GETCOEFF(dfr, ub); /* decode from decFloat */
1310+
1311+ /* now move tail of rhs across to main acc; again use direct */
1312+ /* assignment for 8 digits-worth */
1313+ UINTAT(acc+COFF+DECPMAX)=UINTAT(buf+COFF+DECPMAX);
1314+ UINTAT(acc+COFF+DECPMAX+4)=UINTAT(buf+COFF+DECPMAX+4);
1315+ if (buf+COFF+DECPMAX+8<ub+DECPMAX) {
1316+ uj=&UINTAT(buf+COFF+DECPMAX+8); /* source */
1317+ ui=&UINTAT(acc+COFF+DECPMAX+8); /* target */
1318+ for (; uj<&UINTAT(ub+DECPMAX); ui++, uj++) *ui=*uj;
1319+ }
1320+
1321+ ulsd=acc+(ub-buf+DECPMAX-1); /* update LSD pointer */
1322+
1323+ /* now do the add of the non-tail; this is all nicely aligned, */
1324+ /* and is over a multiple of four digits (because for Quad two */
1325+ /* two 0 digits were added on the left); words in both acc and */
1326+ /* buf (buf especially) will often be zero */
1327+ /* [byte-by-byte add, here, is about 15% slower than the by-fours] */
1328+
1329+ /* Now effect the add; this is harder on a little-endian */
1330+ /* machine as the inter-digit carry cannot use the usual BCD */
1331+ /* addition trick because the bytes are loaded in the wrong order */
1332+ /* [this loop could be unrolled, but probably scarcely worth it] */
1333+
1334+ ui=&UINTAT(acc+COFF+DECPMAX-4); /* target LSW (acc) */
1335+ uj=&UINTAT(buf+COFF+DECPMAX-4); /* source LSW (buf, to add to acc) */
1336+
1337+ #if !DECLITEND
1338+ for (; ui>=&UINTAT(acc+4); ui--, uj--) {
1339+ /* bcd8 add */
1340+ carry+=*uj; /* rhs + carry */
1341+ if (carry==0) continue; /* no-op */
1342+ carry+=*ui; /* lhs */
1343+ /* Big-endian BCD adjust (uses internal carry) */
1344+ carry+=0x76f6f6f6; /* note top nibble not all bits */
1345+ *ui=(carry & 0x0f0f0f0f) - ((carry & 0x60606060)>>4); /* BCD adjust */
1346+ carry>>=31; /* true carry was at far left */
1347+ } /* add loop */
1348+ #else
1349+ for (; ui>=&UINTAT(acc+4); ui--, uj--) {
1350+ /* bcd8 add */
1351+ carry+=*uj; /* rhs + carry */
1352+ if (carry==0) continue; /* no-op [common if unaligned] */
1353+ carry+=*ui; /* lhs */
1354+ /* Little-endian BCD adjust; inter-digit carry must be manual */
1355+ /* because the lsb from the array will be in the most-significant */
1356+ /* byte of carry */
1357+ carry+=0x76767676; /* note no inter-byte carries */
1358+ carry+=(carry & 0x80000000)>>15;
1359+ carry+=(carry & 0x00800000)>>15;
1360+ carry+=(carry & 0x00008000)>>15;
1361+ carry-=(carry & 0x60606060)>>4; /* BCD adjust back */
1362+ *ui=carry & 0x0f0f0f0f; /* clear debris and save */
1363+ /* here, final carry-out bit is at 0x00000080; move it ready */
1364+ /* for next word-add (i.e., to 0x01000000) */
1365+ carry=(carry & 0x00000080)<<17;
1366+ } /* add loop */
1367+ #endif
1368+ #if DECTRACE
1369+ {bcdnum tum;
1370+ printf("Add done, carry=%08lx, diffsign=%ld\n", (LI)carry, (LI)diffsign);
1371+ tum.msd=umsd; /* acc+4; */
1372+ tum.lsd=ulsd;
1373+ tum.exponent=0;
1374+ tum.sign=0;
1375+ decShowNum(&tum, "dfadd");}
1376+ #endif
1377+ } /* overlap possible */
1378+
1379+ /* ordering here is a little strange in order to have slowest path */
1380+ /* first in GCC asm listing */
1381+ if (diffsign) { /* subtraction */
1382+ if (!carry) { /* no carry out means RHS<LHS */
1383+ /* borrowed -- take ten's complement */
1384+ /* sign is lhs sign */
1385+ num.sign=DFWORD(dfl, 0) & DECFLOAT_Sign;
1386+
1387+ /* invert the coefficient first by fours, then add one; space */
1388+ /* at the end of the buffer ensures the by-fours is always */
1389+ /* safe, but lsd+1 must be cleared to prevent a borrow */
1390+ /* if big-endian */
1391+ #if !DECLITEND
1392+ *(ulsd+1)=0;
1393+ #endif
1394+ /* there are always at least four coefficient words */
1395+ UINTAT(umsd) =0x09090909-UINTAT(umsd);
1396+ UINTAT(umsd+4) =0x09090909-UINTAT(umsd+4);
1397+ UINTAT(umsd+8) =0x09090909-UINTAT(umsd+8);
1398+ UINTAT(umsd+12)=0x09090909-UINTAT(umsd+12);
1399+ #if DOUBLE
1400+ #define BNEXT 16
1401+ #elif QUAD
1402+ UINTAT(umsd+16)=0x09090909-UINTAT(umsd+16);
1403+ UINTAT(umsd+20)=0x09090909-UINTAT(umsd+20);
1404+ UINTAT(umsd+24)=0x09090909-UINTAT(umsd+24);
1405+ UINTAT(umsd+28)=0x09090909-UINTAT(umsd+28);
1406+ UINTAT(umsd+32)=0x09090909-UINTAT(umsd+32);
1407+ #define BNEXT 36
1408+ #endif
1409+ if (ulsd>=umsd+BNEXT) { /* unaligned */
1410+ /* eight will handle most unaligments for Double; 16 for Quad */
1411+ UINTAT(umsd+BNEXT)=0x09090909-UINTAT(umsd+BNEXT);
1412+ UINTAT(umsd+BNEXT+4)=0x09090909-UINTAT(umsd+BNEXT+4);
1413+ #if DOUBLE
1414+ #define BNEXTY (BNEXT+8)
1415+ #elif QUAD
1416+ UINTAT(umsd+BNEXT+8)=0x09090909-UINTAT(umsd+BNEXT+8);
1417+ UINTAT(umsd+BNEXT+12)=0x09090909-UINTAT(umsd+BNEXT+12);
1418+ #define BNEXTY (BNEXT+16)
1419+ #endif
1420+ if (ulsd>=umsd+BNEXTY) { /* very unaligned */
1421+ ui=&UINTAT(umsd+BNEXTY); /* -> continue */
1422+ for (;;ui++) {
1423+ *ui=0x09090909-*ui; /* invert four digits */
1424+ if (ui>=&UINTAT(ulsd-3)) break; /* all done */
1425+ }
1426+ }
1427+ }
1428+ /* complete the ten's complement by adding 1 */
1429+ for (ub=ulsd; *ub==9; ub--) *ub=0;
1430+ *ub+=1;
1431+ } /* borrowed */
1432+
1433+ else { /* carry out means RHS>=LHS */
1434+ num.sign=DFWORD(dfr, 0) & DECFLOAT_Sign;
1435+ /* all done except for the special IEEE 754 exact-zero-result */
1436+ /* rule (see above); while testing for zero, strip leading */
1437+ /* zeros (which will save decFinalize doing it) (this is in */
1438+ /* diffsign path, so carry impossible and true umsd is */
1439+ /* acc+COFF) */
1440+
1441+ /* Check the initial coefficient area using the fast macro; */
1442+ /* this will often be all that needs to be done (as on the */
1443+ /* worst-case path when the subtraction was aligned and */
1444+ /* full-length) */
1445+ if (ISCOEFFZERO(acc+COFF)) {
1446+ umsd=acc+COFF+DECPMAX-1; /* so far, so zero */
1447+ if (ulsd>umsd) { /* more to check */
1448+ umsd++; /* to align after checked area */
1449+ for (; UINTAT(umsd)==0 && umsd+3<ulsd;) umsd+=4;
1450+ for (; *umsd==0 && umsd<ulsd;) umsd++;
1451+ }
1452+ if (*umsd==0) { /* must be true zero (and diffsign) */
1453+ num.sign=0; /* assume + */
1454+ if (set->round==DEC_ROUND_FLOOR) num.sign=DECFLOAT_Sign;
1455+ }
1456+ }
1457+ /* [else was not zero, might still have leading zeros] */
1458+ } /* subtraction gave positive result */
1459+ } /* diffsign */
1460+
1461+ else { /* same-sign addition */
1462+ num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
1463+ #if DOUBLE
1464+ if (carry) { /* only possible with decDouble */
1465+ *(acc+3)=1; /* [Quad has leading 00] */
1466+ umsd=acc+3;
1467+ }
1468+ #endif
1469+ } /* same sign */
1470+
1471+ num.msd=umsd; /* set MSD .. */
1472+ num.lsd=ulsd; /* .. and LSD */
1473+ num.exponent=expr; /* set exponent to smaller */
1474+
1475+ #if DECTRACE
1476+ decFloatShow(dfl, "dfl");
1477+ decFloatShow(dfr, "dfr");
1478+ decShowNum(&num, "postadd");
1479+ #endif
1480+ return decFinalize(result, &num, set); /* round, check, and lay out */
1481+ } /* decFloatAdd */
1482+
1483+/* ------------------------------------------------------------------ */
1484+/* decFloatAnd -- logical digitwise AND of two decFloats */
1485+/* */
1486+/* result gets the result of ANDing dfl and dfr */
1487+/* dfl is the first decFloat (lhs) */
1488+/* dfr is the second decFloat (rhs) */
1489+/* set is the context */
1490+/* returns result, which will be canonical with sign=0 */
1491+/* */
1492+/* The operands must be positive, finite with exponent q=0, and */
1493+/* comprise just zeros and ones; if not, Invalid operation results. */
1494+/* ------------------------------------------------------------------ */
1495+decFloat * decFloatAnd(decFloat *result,
1496+ const decFloat *dfl, const decFloat *dfr,
1497+ decContext *set) {
1498+ if (!DFISUINT01(dfl) || !DFISUINT01(dfr)
1499+ || !DFISCC01(dfl) || !DFISCC01(dfr)) return decInvalid(result, set);
1500+ /* the operands are positive finite integers (q=0) with just 0s and 1s */
1501+ #if DOUBLE
1502+ DFWORD(result, 0)=ZEROWORD
1503+ |((DFWORD(dfl, 0) & DFWORD(dfr, 0))&0x04009124);
1504+ DFWORD(result, 1)=(DFWORD(dfl, 1) & DFWORD(dfr, 1))&0x49124491;
1505+ #elif QUAD
1506+ DFWORD(result, 0)=ZEROWORD
1507+ |((DFWORD(dfl, 0) & DFWORD(dfr, 0))&0x04000912);
1508+ DFWORD(result, 1)=(DFWORD(dfl, 1) & DFWORD(dfr, 1))&0x44912449;
1509+ DFWORD(result, 2)=(DFWORD(dfl, 2) & DFWORD(dfr, 2))&0x12449124;
1510+ DFWORD(result, 3)=(DFWORD(dfl, 3) & DFWORD(dfr, 3))&0x49124491;
1511+ #endif
1512+ return result;
1513+ } /* decFloatAnd */
1514+
1515+/* ------------------------------------------------------------------ */
1516+/* decFloatCanonical -- copy a decFloat, making canonical */
1517+/* */
1518+/* result gets the canonicalized df */
1519+/* df is the decFloat to copy and make canonical */
1520+/* returns result */
1521+/* */
1522+/* This works on specials, too; no error or exception is possible. */
1523+/* ------------------------------------------------------------------ */
1524+decFloat * decFloatCanonical(decFloat *result, const decFloat *df) {
1525+ return decCanonical(result, df);
1526+ } /* decFloatCanonical */
1527+
1528+/* ------------------------------------------------------------------ */
1529+/* decFloatClass -- return the class of a decFloat */
1530+/* */
1531+/* df is the decFloat to test */
1532+/* returns the decClass that df falls into */
1533+/* ------------------------------------------------------------------ */
1534+enum decClass decFloatClass(const decFloat *df) {
1535+ Int exp; /* exponent */
1536+ if (DFISSPECIAL(df)) {
1537+ if (DFISQNAN(df)) return DEC_CLASS_QNAN;
1538+ if (DFISSNAN(df)) return DEC_CLASS_SNAN;
1539+ /* must be an infinity */
1540+ if (DFISSIGNED(df)) return DEC_CLASS_NEG_INF;
1541+ return DEC_CLASS_POS_INF;
1542+ }
1543+ if (DFISZERO(df)) { /* quite common */
1544+ if (DFISSIGNED(df)) return DEC_CLASS_NEG_ZERO;
1545+ return DEC_CLASS_POS_ZERO;
1546+ }
1547+ /* is finite and non-zero; similar code to decFloatIsNormal, here */
1548+ /* [this could be speeded up slightly by in-lining decFloatDigits] */
1549+ exp=GETEXPUN(df) /* get unbiased exponent .. */
1550+ +decFloatDigits(df)-1; /* .. and make adjusted exponent */
1551+ if (exp>=DECEMIN) { /* is normal */
1552+ if (DFISSIGNED(df)) return DEC_CLASS_NEG_NORMAL;
1553+ return DEC_CLASS_POS_NORMAL;
1554+ }
1555+ /* is subnormal */
1556+ if (DFISSIGNED(df)) return DEC_CLASS_NEG_SUBNORMAL;
1557+ return DEC_CLASS_POS_SUBNORMAL;
1558+ } /* decFloatClass */
1559+
1560+/* ------------------------------------------------------------------ */
1561+/* decFloatClassString -- return the class of a decFloat as a string */
1562+/* */
1563+/* df is the decFloat to test */
1564+/* returns a constant string describing the class df falls into */
1565+/* ------------------------------------------------------------------ */
1566+const char *decFloatClassString(const decFloat *df) {
1567+ enum decClass eclass=decFloatClass(df);
1568+ if (eclass==DEC_CLASS_POS_NORMAL) return DEC_ClassString_PN;
1569+ if (eclass==DEC_CLASS_NEG_NORMAL) return DEC_ClassString_NN;
1570+ if (eclass==DEC_CLASS_POS_ZERO) return DEC_ClassString_PZ;
1571+ if (eclass==DEC_CLASS_NEG_ZERO) return DEC_ClassString_NZ;
1572+ if (eclass==DEC_CLASS_POS_SUBNORMAL) return DEC_ClassString_PS;
1573+ if (eclass==DEC_CLASS_NEG_SUBNORMAL) return DEC_ClassString_NS;
1574+ if (eclass==DEC_CLASS_POS_INF) return DEC_ClassString_PI;
1575+ if (eclass==DEC_CLASS_NEG_INF) return DEC_ClassString_NI;
1576+ if (eclass==DEC_CLASS_QNAN) return DEC_ClassString_QN;
1577+ if (eclass==DEC_CLASS_SNAN) return DEC_ClassString_SN;
1578+ return DEC_ClassString_UN; /* Unknown */
1579+ } /* decFloatClassString */
1580+
1581+/* ------------------------------------------------------------------ */
1582+/* decFloatCompare -- compare two decFloats; quiet NaNs allowed */
1583+/* */
1584+/* result gets the result of comparing dfl and dfr */
1585+/* dfl is the first decFloat (lhs) */
1586+/* dfr is the second decFloat (rhs) */
1587+/* set is the context */
1588+/* returns result, which may be -1, 0, 1, or NaN (Unordered) */
1589+/* ------------------------------------------------------------------ */
1590+decFloat * decFloatCompare(decFloat *result,
1591+ const decFloat *dfl, const decFloat *dfr,
1592+ decContext *set) {
1593+ Int comp; /* work */
1594+ /* NaNs are handled as usual */
1595+ if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
1596+ /* numeric comparison needed */
1597+ comp=decNumCompare(dfl, dfr, 0);
1598+ decFloatZero(result);
1599+ if (comp==0) return result;
1600+ DFBYTE(result, DECBYTES-1)=0x01; /* LSD=1 */
1601+ if (comp<0) DFBYTE(result, 0)|=0x80; /* set sign bit */
1602+ return result;
1603+ } /* decFloatCompare */
1604+
1605+/* ------------------------------------------------------------------ */
1606+/* decFloatCompareSignal -- compare two decFloats; all NaNs signal */
1607+/* */
1608+/* result gets the result of comparing dfl and dfr */
1609+/* dfl is the first decFloat (lhs) */
1610+/* dfr is the second decFloat (rhs) */
1611+/* set is the context */
1612+/* returns result, which may be -1, 0, 1, or NaN (Unordered) */
1613+/* ------------------------------------------------------------------ */
1614+decFloat * decFloatCompareSignal(decFloat *result,
1615+ const decFloat *dfl, const decFloat *dfr,
1616+ decContext *set) {
1617+ Int comp; /* work */
1618+ /* NaNs are handled as usual, except that all NaNs signal */
1619+ if (DFISNAN(dfl) || DFISNAN(dfr)) {
1620+ set->status|=DEC_Invalid_operation;
1621+ return decNaNs(result, dfl, dfr, set);
1622+ }
1623+ /* numeric comparison needed */
1624+ comp=decNumCompare(dfl, dfr, 0);
1625+ decFloatZero(result);
1626+ if (comp==0) return result;
1627+ DFBYTE(result, DECBYTES-1)=0x01; /* LSD=1 */
1628+ if (comp<0) DFBYTE(result, 0)|=0x80; /* set sign bit */
1629+ return result;
1630+ } /* decFloatCompareSignal */
1631+
1632+/* ------------------------------------------------------------------ */
1633+/* decFloatCompareTotal -- compare two decFloats with total ordering */
1634+/* */
1635+/* result gets the result of comparing dfl and dfr */
1636+/* dfl is the first decFloat (lhs) */
1637+/* dfr is the second decFloat (rhs) */
1638+/* returns result, which may be -1, 0, or 1 */
1639+/* ------------------------------------------------------------------ */
1640+decFloat * decFloatCompareTotal(decFloat *result,
1641+ const decFloat *dfl, const decFloat *dfr) {
1642+ Int comp; /* work */
1643+ if (DFISNAN(dfl) || DFISNAN(dfr)) {
1644+ Int nanl, nanr; /* work */
1645+ /* morph NaNs to +/- 1 or 2, leave numbers as 0 */
1646+ nanl=DFISSNAN(dfl)+DFISQNAN(dfl)*2; /* quiet > signalling */
1647+ if (DFISSIGNED(dfl)) nanl=-nanl;
1648+ nanr=DFISSNAN(dfr)+DFISQNAN(dfr)*2;
1649+ if (DFISSIGNED(dfr)) nanr=-nanr;
1650+ if (nanl>nanr) comp=+1;
1651+ else if (nanl<nanr) comp=-1;
1652+ else { /* NaNs are the same type and sign .. must compare payload */
1653+ /* buffers need +2 for QUAD */
1654+ uByte bufl[DECPMAX+4]; /* for LHS coefficient + foot */
1655+ uByte bufr[DECPMAX+4]; /* for RHS coefficient + foot */
1656+ uByte *ub, *uc; /* work */
1657+ Int sigl; /* signum of LHS */
1658+ sigl=(DFISSIGNED(dfl) ? -1 : +1);
1659+
1660+ /* decode the coefficients */
1661+ /* (shift both right two if Quad to make a multiple of four) */
1662+ #if QUAD
1663+ USHORTAT(bufl)=0;
1664+ USHORTAT(bufr)=0;
1665+ #endif
1666+ GETCOEFF(dfl, bufl+QUAD*2); /* decode from decFloat */
1667+ GETCOEFF(dfr, bufr+QUAD*2); /* .. */
1668+ /* all multiples of four, here */
1669+ comp=0; /* assume equal */
1670+ for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) {
1671+ if (UINTAT(ub)==UINTAT(uc)) continue; /* so far so same */
1672+ /* about to find a winner; go by bytes in case little-endian */
1673+ for (;; ub++, uc++) {
1674+ if (*ub==*uc) continue;
1675+ if (*ub>*uc) comp=sigl; /* difference found */
1676+ else comp=-sigl; /* .. */
1677+ break;
1678+ }
1679+ }
1680+ } /* same NaN type and sign */
1681+ }
1682+ else {
1683+ /* numeric comparison needed */
1684+ comp=decNumCompare(dfl, dfr, 1); /* total ordering */
1685+ }
1686+ decFloatZero(result);
1687+ if (comp==0) return result;
1688+ DFBYTE(result, DECBYTES-1)=0x01; /* LSD=1 */
1689+ if (comp<0) DFBYTE(result, 0)|=0x80; /* set sign bit */
1690+ return result;
1691+ } /* decFloatCompareTotal */
1692+
1693+/* ------------------------------------------------------------------ */
1694+/* decFloatCompareTotalMag -- compare magnitudes with total ordering */
1695+/* */
1696+/* result gets the result of comparing abs(dfl) and abs(dfr) */
1697+/* dfl is the first decFloat (lhs) */
1698+/* dfr is the second decFloat (rhs) */
1699+/* returns result, which may be -1, 0, or 1 */
1700+/* ------------------------------------------------------------------ */
1701+decFloat * decFloatCompareTotalMag(decFloat *result,
1702+ const decFloat *dfl, const decFloat *dfr) {
1703+ decFloat a, b; /* for copy if needed */
1704+ /* copy and redirect signed operand(s) */
1705+ if (DFISSIGNED(dfl)) {
1706+ decFloatCopyAbs(&a, dfl);
1707+ dfl=&a;
1708+ }
1709+ if (DFISSIGNED(dfr)) {
1710+ decFloatCopyAbs(&b, dfr);
1711+ dfr=&b;
1712+ }
1713+ return decFloatCompareTotal(result, dfl, dfr);
1714+ } /* decFloatCompareTotalMag */
1715+
1716+/* ------------------------------------------------------------------ */
1717+/* decFloatCopy -- copy a decFloat as-is */
1718+/* */
1719+/* result gets the copy of dfl */
1720+/* dfl is the decFloat to copy */
1721+/* returns result */
1722+/* */
1723+/* This is a bitwise operation; no errors or exceptions are possible. */
1724+/* ------------------------------------------------------------------ */
1725+decFloat * decFloatCopy(decFloat *result, const decFloat *dfl) {
1726+ if (dfl!=result) *result=*dfl; /* copy needed */
1727+ return result;
1728+ } /* decFloatCopy */
1729+
1730+/* ------------------------------------------------------------------ */
1731+/* decFloatCopyAbs -- copy a decFloat as-is and set sign bit to 0 */
1732+/* */
1733+/* result gets the copy of dfl with sign bit 0 */
1734+/* dfl is the decFloat to copy */
1735+/* returns result */
1736+/* */
1737+/* This is a bitwise operation; no errors or exceptions are possible. */
1738+/* ------------------------------------------------------------------ */
1739+decFloat * decFloatCopyAbs(decFloat *result, const decFloat *dfl) {
1740+ if (dfl!=result) *result=*dfl; /* copy needed */
1741+ DFBYTE(result, 0)&=~0x80; /* zero sign bit */
1742+ return result;
1743+ } /* decFloatCopyAbs */
1744+
1745+/* ------------------------------------------------------------------ */
1746+/* decFloatCopyNegate -- copy a decFloat as-is with inverted sign bit */
1747+/* */
1748+/* result gets the copy of dfl with sign bit inverted */
1749+/* dfl is the decFloat to copy */
1750+/* returns result */
1751+/* */
1752+/* This is a bitwise operation; no errors or exceptions are possible. */
1753+/* ------------------------------------------------------------------ */
1754+decFloat * decFloatCopyNegate(decFloat *result, const decFloat *dfl) {
1755+ if (dfl!=result) *result=*dfl; /* copy needed */
1756+ DFBYTE(result, 0)^=0x80; /* invert sign bit */
1757+ return result;
1758+ } /* decFloatCopyNegate */
1759+
1760+/* ------------------------------------------------------------------ */
1761+/* decFloatCopySign -- copy a decFloat with the sign of another */
1762+/* */
1763+/* result gets the result of copying dfl with the sign of dfr */
1764+/* dfl is the first decFloat (lhs) */
1765+/* dfr is the second decFloat (rhs) */
1766+/* returns result */
1767+/* */
1768+/* This is a bitwise operation; no errors or exceptions are possible. */
1769+/* ------------------------------------------------------------------ */
1770+decFloat * decFloatCopySign(decFloat *result,
1771+ const decFloat *dfl, const decFloat *dfr) {
1772+ uByte sign=(uByte)(DFBYTE(dfr, 0)&0x80); /* save sign bit */
1773+ if (dfl!=result) *result=*dfl; /* copy needed */
1774+ DFBYTE(result, 0)&=~0x80; /* clear sign .. */
1775+ DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); /* .. and set saved */
1776+ return result;
1777+ } /* decFloatCopySign */
1778+
1779+/* ------------------------------------------------------------------ */
1780+/* decFloatDigits -- return the number of digits in a decFloat */
1781+/* */
1782+/* df is the decFloat to investigate */
1783+/* returns the number of significant digits in the decFloat; a */
1784+/* zero coefficient returns 1 as does an infinity (a NaN returns */
1785+/* the number of digits in the payload) */
1786+/* ------------------------------------------------------------------ */
1787+/* private macro to extract a declet according to provided formula */
1788+/* (form), and if it is non-zero then return the calculated digits */
1789+/* depending on the declet number (n), where n=0 for the most */
1790+/* significant declet; uses uInt dpd for work */
1791+#define dpdlenchk(n, form) {dpd=(form)&0x3ff; \
1792+ if (dpd) return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3]);}
1793+/* next one is used when it is known that the declet must be */
1794+/* non-zero, or is the final zero declet */
1795+#define dpdlendun(n, form) {dpd=(form)&0x3ff; \
1796+ if (dpd==0) return 1; \
1797+ return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3]);}
1798+
1799+uInt decFloatDigits(const decFloat *df) {
1800+ uInt dpd; /* work */
1801+ uInt sourhi=DFWORD(df, 0); /* top word from source decFloat */
1802+ #if QUAD
1803+ uInt sourmh, sourml;
1804+ #endif
1805+ uInt sourlo;
1806+
1807+ if (DFISINF(df)) return 1;
1808+ /* A NaN effectively has an MSD of 0; otherwise if non-zero MSD */
1809+ /* then the coefficient is full-length */
1810+ if (!DFISNAN(df) && DECCOMBMSD[sourhi>>26]) return DECPMAX;
1811+
1812+ #if DOUBLE
1813+ if (sourhi&0x0003ffff) { /* ends in first */
1814+ dpdlenchk(0, sourhi>>8);
1815+ sourlo=DFWORD(df, 1);
1816+ dpdlendun(1, (sourhi<<2) | (sourlo>>30));
1817+ } /* [cannot drop through] */
1818+ sourlo=DFWORD(df, 1); /* sourhi not involved now */
1819+ if (sourlo&0xfff00000) { /* in one of first two */
1820+ dpdlenchk(1, sourlo>>30); /* very rare */
1821+ dpdlendun(2, sourlo>>20);
1822+ } /* [cannot drop through] */
1823+ dpdlenchk(3, sourlo>>10);
1824+ dpdlendun(4, sourlo);
1825+ /* [cannot drop through] */
1826+
1827+ #elif QUAD
1828+ if (sourhi&0x00003fff) { /* ends in first */
1829+ dpdlenchk(0, sourhi>>4);
1830+ sourmh=DFWORD(df, 1);
1831+ dpdlendun(1, ((sourhi)<<6) | (sourmh>>26));
1832+ } /* [cannot drop through] */
1833+ sourmh=DFWORD(df, 1);
1834+ if (sourmh) {
1835+ dpdlenchk(1, sourmh>>26);
1836+ dpdlenchk(2, sourmh>>16);
1837+ dpdlenchk(3, sourmh>>6);
1838+ sourml=DFWORD(df, 2);
1839+ dpdlendun(4, ((sourmh)<<4) | (sourml>>28));
1840+ } /* [cannot drop through] */
1841+ sourml=DFWORD(df, 2);
1842+ if (sourml) {
1843+ dpdlenchk(4, sourml>>28);
1844+ dpdlenchk(5, sourml>>18);
1845+ dpdlenchk(6, sourml>>8);
1846+ sourlo=DFWORD(df, 3);
1847+ dpdlendun(7, ((sourml)<<2) | (sourlo>>30));
1848+ } /* [cannot drop through] */
1849+ sourlo=DFWORD(df, 3);
1850+ if (sourlo&0xfff00000) { /* in one of first two */
1851+ dpdlenchk(7, sourlo>>30); /* very rare */
1852+ dpdlendun(8, sourlo>>20);
1853+ } /* [cannot drop through] */
1854+ dpdlenchk(9, sourlo>>10);
1855+ dpdlendun(10, sourlo);
1856+ /* [cannot drop through] */
1857+ #endif
1858+ } /* decFloatDigits */
1859+
1860+/* ------------------------------------------------------------------ */
1861+/* decFloatDivide -- divide a decFloat by another */
1862+/* */
1863+/* result gets the result of dividing dfl by dfr: */
1864+/* dfl is the first decFloat (lhs) */
1865+/* dfr is the second decFloat (rhs) */
1866+/* set is the context */
1867+/* returns result */
1868+/* */
1869+/* ------------------------------------------------------------------ */
1870+/* This is just a wrapper. */
1871+decFloat * decFloatDivide(decFloat *result,
1872+ const decFloat *dfl, const decFloat *dfr,
1873+ decContext *set) {
1874+ return decDivide(result, dfl, dfr, set, DIVIDE);
1875+ } /* decFloatDivide */
1876+
1877+/* ------------------------------------------------------------------ */
1878+/* decFloatDivideInteger -- integer divide a decFloat by another */
1879+/* */
1880+/* result gets the result of dividing dfl by dfr: */
1881+/* dfl is the first decFloat (lhs) */
1882+/* dfr is the second decFloat (rhs) */
1883+/* set is the context */
1884+/* returns result */
1885+/* */
1886+/* ------------------------------------------------------------------ */
1887+decFloat * decFloatDivideInteger(decFloat *result,
1888+ const decFloat *dfl, const decFloat *dfr,
1889+ decContext *set) {
1890+ return decDivide(result, dfl, dfr, set, DIVIDEINT);
1891+ } /* decFloatDivideInteger */
1892+
1893+/* ------------------------------------------------------------------ */
1894+/* decFloatFMA -- multiply and add three decFloats, fused */
1895+/* */
1896+/* result gets the result of (dfl*dfr)+dff with a single rounding */
1897+/* dfl is the first decFloat (lhs) */
1898+/* dfr is the second decFloat (rhs) */
1899+/* dff is the final decFloat (fhs) */
1900+/* set is the context */
1901+/* returns result */
1902+/* */
1903+/* ------------------------------------------------------------------ */
1904+decFloat * decFloatFMA(decFloat *result, const decFloat *dfl,
1905+ const decFloat *dfr, const decFloat *dff,
1906+ decContext *set) {
1907+ /* The accumulator has the bytes needed for FiniteMultiply, plus */
1908+ /* one byte to the left in case of carry, plus DECPMAX+2 to the */
1909+ /* right for the final addition (up to full fhs + round & sticky) */
1910+ #define FMALEN (1+ (DECPMAX9*18) +DECPMAX+2)
1911+ uByte acc[FMALEN]; /* for multiplied coefficient in BCD */
1912+ /* .. and for final result */
1913+ bcdnum mul; /* for multiplication result */
1914+ bcdnum fin; /* for final operand, expanded */
1915+ uByte coe[DECPMAX]; /* dff coefficient in BCD */
1916+ bcdnum *hi, *lo; /* bcdnum with higher/lower exponent */
1917+ uInt diffsign; /* non-zero if signs differ */
1918+ uInt hipad; /* pad digit for hi if needed */
1919+ Int padding; /* excess exponent */
1920+ uInt carry; /* +1 for ten's complement and during add */
1921+ uByte *ub, *uh, *ul; /* work */
1922+
1923+ /* handle all the special values [any special operand leads to a */
1924+ /* special result] */
1925+ if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr) || DFISSPECIAL(dff)) {
1926+ decFloat proxy; /* multiplication result proxy */
1927+ /* NaNs are handled as usual, giving priority to sNaNs */
1928+ if (DFISSNAN(dfl) || DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set);
1929+ if (DFISSNAN(dff)) return decNaNs(result, dff, NULL, set);
1930+ if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
1931+ if (DFISNAN(dff)) return decNaNs(result, dff, NULL, set);
1932+ /* One or more of the three is infinite */
1933+ /* infinity times zero is bad */
1934+ decFloatZero(&proxy);
1935+ if (DFISINF(dfl)) {
1936+ if (DFISZERO(dfr)) return decInvalid(result, set);
1937+ decInfinity(&proxy, &proxy);
1938+ }
1939+ else if (DFISINF(dfr)) {
1940+ if (DFISZERO(dfl)) return decInvalid(result, set);
1941+ decInfinity(&proxy, &proxy);
1942+ }
1943+ /* compute sign of multiplication and place in proxy */
1944+ DFWORD(&proxy, 0)|=(DFWORD(dfl, 0)^DFWORD(dfr, 0))&DECFLOAT_Sign;
1945+ if (!DFISINF(dff)) return decFloatCopy(result, &proxy);
1946+ /* dff is Infinite */
1947+ if (!DFISINF(&proxy)) return decInfinity(result, dff);
1948+ /* both sides of addition are infinite; different sign is bad */
1949+ if ((DFWORD(dff, 0)&DECFLOAT_Sign)!=(DFWORD(&proxy, 0)&DECFLOAT_Sign))
1950+ return decInvalid(result, set);
1951+ return decFloatCopy(result, &proxy);
1952+ }
1953+
1954+ /* Here when all operands are finite */
1955+
1956+ /* First multiply dfl*dfr */
1957+ decFiniteMultiply(&mul, acc+1, dfl, dfr);
1958+ /* The multiply is complete, exact and unbounded, and described in */
1959+ /* mul with the coefficient held in acc[1...] */
1960+
1961+ /* now add in dff; the algorithm is essentially the same as */
1962+ /* decFloatAdd, but the code is different because the code there */
1963+ /* is highly optimized for adding two numbers of the same size */
1964+ fin.exponent=GETEXPUN(dff); /* get dff exponent and sign */
1965+ fin.sign=DFWORD(dff, 0)&DECFLOAT_Sign;
1966+ diffsign=mul.sign^fin.sign; /* note if signs differ */
1967+ fin.msd=coe;
1968+ fin.lsd=coe+DECPMAX-1;
1969+ GETCOEFF(dff, coe); /* extract the coefficient */
1970+
1971+ /* now set hi and lo so that hi points to whichever of mul and fin */
1972+ /* has the higher exponent and lo point to the other [don't care if */
1973+ /* the same] */
1974+ if (mul.exponent>=fin.exponent) {
1975+ hi=&mul;
1976+ lo=&fin;
1977+ }
1978+ else {
1979+ hi=&fin;
1980+ lo=&mul;
1981+ }
1982+
1983+ /* remove leading zeros on both operands; this will save time later */
1984+ /* and make testing for zero trivial */
1985+ for (; UINTAT(hi->msd)==0 && hi->msd+3<hi->lsd;) hi->msd+=4;
1986+ for (; *hi->msd==0 && hi->msd<hi->lsd;) hi->msd++;
1987+ for (; UINTAT(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4;
1988+ for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++;
1989+
1990+ /* if hi is zero then result will be lo (which has the smaller */
1991+ /* exponent), which also may need to be tested for zero for the */
1992+ /* weird IEEE 754 sign rules */
1993+ if (*hi->msd==0 && hi->msd==hi->lsd) { /* hi is zero */
1994+ /* "When the sum of two operands with opposite signs is */
1995+ /* exactly zero, the sign of that sum shall be '+' in all */
1996+ /* rounding modes except round toward -Infinity, in which */
1997+ /* mode that sign shall be '-'." */
1998+ if (diffsign) {
1999+ if (*lo->msd==0 && lo->msd==lo->lsd) { /* lo is zero */
2000+ lo->sign=0;
2001+ if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign;
2002+ } /* diffsign && lo=0 */
2003+ } /* diffsign */
2004+ return decFinalize(result, lo, set); /* may need clamping */
2005+ } /* numfl is zero */
2006+ /* [here, both are minimal length and hi is non-zero] */
2007+
2008+ /* if signs differ, take the ten's complement of hi (zeros to the */
2009+ /* right do not matter because the complement of zero is zero); */
2010+ /* the +1 is done later, as part of the addition, inserted at the */
2011+ /* correct digit */
2012+ hipad=0;
2013+ carry=0;
2014+ if (diffsign) {
2015+ hipad=9;
2016+ carry=1;
2017+ /* exactly the correct number of digits must be inverted */
2018+ for (uh=hi->msd; uh<hi->lsd-3; uh+=4) UINTAT(uh)=0x09090909-UINTAT(uh);
2019+ for (; uh<=hi->lsd; uh++) *uh=(uByte)(0x09-*uh);
2020+ }
2021+
2022+ /* ready to add; note that hi has no leading zeros so gap */
2023+ /* calculation does not have to be as pessimistic as in decFloatAdd */
2024+ /* (this is much more like the arbitrary-precision algorithm in */
2025+ /* Rexx and decNumber) */
2026+
2027+ /* padding is the number of zeros that would need to be added to hi */
2028+ /* for its lsd to be aligned with the lsd of lo */
2029+ padding=hi->exponent-lo->exponent;
2030+ /* printf("FMA pad %ld\n", (LI)padding); */
2031+
2032+ /* the result of the addition will be built into the accumulator, */
2033+ /* starting from the far right; this could be either hi or lo */
2034+ ub=acc+FMALEN-1; /* where lsd of result will go */
2035+ ul=lo->lsd; /* lsd of rhs */
2036+
2037+ if (padding!=0) { /* unaligned */
2038+ /* if the msd of lo is more than DECPMAX+2 digits to the right of */
2039+ /* the original msd of hi then it can be reduced to a single */
2040+ /* digit at the right place, as it stays clear of hi digits */
2041+ /* [it must be DECPMAX+2 because during a subtraction the msd */
2042+ /* could become 0 after a borrow from 1.000 to 0.9999...] */
2043+ Int hilen=(Int)(hi->lsd-hi->msd+1); /* lengths */
2044+ Int lolen=(Int)(lo->lsd-lo->msd+1); /* .. */
2045+ Int newexp=MINI(hi->exponent, hi->exponent+hilen-DECPMAX)-3;
2046+ Int reduce=newexp-lo->exponent;
2047+ if (reduce>0) { /* [= case gives reduce=0 nop] */
2048+ /* printf("FMA reduce: %ld\n", (LI)reduce); */
2049+ if (reduce>=lolen) { /* eating all */
2050+ lo->lsd=lo->msd; /* reduce to single digit */
2051+ lo->exponent=newexp; /* [known to be non-zero] */
2052+ }
2053+ else { /* < */
2054+ uByte *up=lo->lsd;
2055+ lo->lsd=lo->lsd-reduce;
2056+ if (*lo->lsd==0) /* could need sticky bit */
2057+ for (; up>lo->lsd; up--) { /* search discarded digits */
2058+ if (*up!=0) { /* found one... */
2059+ *lo->lsd=1; /* set sticky bit */
2060+ break;
2061+ }
2062+ }
2063+ lo->exponent+=reduce;
2064+ }
2065+ padding=hi->exponent-lo->exponent; /* recalculate */
2066+ ul=lo->lsd; /* .. */
2067+ } /* maybe reduce */
2068+ /* padding is now <= DECPMAX+2 but still > 0; tricky DOUBLE case */
2069+ /* is when hi is a 1 that will become a 0.9999... by subtraction: */
2070+ /* hi: 1 E+16 */
2071+ /* lo: .................1000000000000000 E-16 */
2072+ /* which for the addition pads and reduces to: */
2073+ /* hi: 1000000000000000000 E-2 */
2074+ /* lo: .................1 E-2 */
2075+ #if DECCHECK
2076+ if (padding>DECPMAX+2) printf("FMA excess padding: %ld\n", (LI)padding);
2077+ if (padding<=0) printf("FMA low padding: %ld\n", (LI)padding);
2078+ /* printf("FMA padding: %ld\n", (LI)padding); */
2079+ #endif
2080+ /* padding digits can now be set in the result; one or more of */
2081+ /* these will come from lo; others will be zeros in the gap */
2082+ for (; ul>=lo->msd && padding>0; padding--, ul--, ub--) *ub=*ul;
2083+ for (;padding>0; padding--, ub--) *ub=0; /* mind the gap */
2084+ }
2085+
2086+ /* addition now complete to the right of the rightmost digit of hi */
2087+ uh=hi->lsd;
2088+
2089+ /* carry was set up depending on ten's complement above; do the add... */
2090+ for (;; ub--) {
2091+ uInt hid, lod;
2092+ if (uh<hi->msd) {
2093+ if (ul<lo->msd) break;
2094+ hid=hipad;
2095+ }
2096+ else hid=*uh--;
2097+ if (ul<lo->msd) lod=0;
2098+ else lod=*ul--;
2099+ *ub=(uByte)(carry+hid+lod);
2100+ if (*ub<10) carry=0;
2101+ else {
2102+ *ub-=10;
2103+ carry=1;
2104+ }
2105+ } /* addition loop */
2106+
2107+ /* addition complete -- now handle carry, borrow, etc. */
2108+ /* use lo to set up the num (its exponent is already correct, and */
2109+ /* sign usually is) */
2110+ lo->msd=ub+1;
2111+ lo->lsd=acc+FMALEN-1;
2112+ /* decShowNum(lo, "lo"); */
2113+ if (!diffsign) { /* same-sign addition */
2114+ if (carry) { /* carry out */
2115+ *ub=1; /* place the 1 .. */
2116+ lo->msd--; /* .. and update */
2117+ }
2118+ } /* same sign */
2119+ else { /* signs differed (subtraction) */
2120+ if (!carry) { /* no carry out means hi<lo */
2121+ /* borrowed -- take ten's complement of the right digits */
2122+ lo->sign=hi->sign; /* sign is lhs sign */
2123+ for (ul=lo->msd; ul<lo->lsd-3; ul+=4) UINTAT(ul)=0x09090909-UINTAT(ul);
2124+ for (; ul<=lo->lsd; ul++) *ul=(uByte)(0x09-*ul); /* [leaves ul at lsd+1] */
2125+ /* complete the ten's complement by adding 1 [cannot overrun] */
2126+ for (ul--; *ul==9; ul--) *ul=0;
2127+ *ul+=1;
2128+ } /* borrowed */
2129+ else { /* carry out means hi>=lo */
2130+ /* sign to use is lo->sign */
2131+ /* all done except for the special IEEE 754 exact-zero-result */
2132+ /* rule (see above); while testing for zero, strip leading */
2133+ /* zeros (which will save decFinalize doing it) */
2134+ for (; UINTAT(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4;
2135+ for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++;
2136+ if (*lo->msd==0) { /* must be true zero (and diffsign) */
2137+ lo->sign=0; /* assume + */
2138+ if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign;
2139+ }
2140+ /* [else was not zero, might still have leading zeros] */
2141+ } /* subtraction gave positive result */
2142+ } /* diffsign */
2143+
2144+ return decFinalize(result, lo, set); /* round, check, and lay out */
2145+ } /* decFloatFMA */
2146+
2147+/* ------------------------------------------------------------------ */
2148+/* decFloatFromInt -- initialise a decFloat from an Int */
2149+/* */
2150+/* result gets the converted Int */
2151+/* n is the Int to convert */
2152+/* returns result */
2153+/* */
2154+/* The result is Exact; no errors or exceptions are possible. */
2155+/* ------------------------------------------------------------------ */
2156+decFloat * decFloatFromInt32(decFloat *result, Int n) {
2157+ uInt u=(uInt)n; /* copy as bits */
2158+ uInt encode; /* work */
2159+ DFWORD(result, 0)=ZEROWORD; /* always */
2160+ #if QUAD
2161+ DFWORD(result, 1)=0;
2162+ DFWORD(result, 2)=0;
2163+ #endif
2164+ if (n<0) { /* handle -n with care */
2165+ /* [This can be done without the test, but is then slightly slower] */
2166+ u=(~u)+1;
2167+ DFWORD(result, 0)|=DECFLOAT_Sign;
2168+ }
2169+ /* Since the maximum value of u now is 2**31, only the low word of */
2170+ /* result is affected */
2171+ encode=BIN2DPD[u%1000];
2172+ u/=1000;
2173+ encode|=BIN2DPD[u%1000]<<10;
2174+ u/=1000;
2175+ encode|=BIN2DPD[u%1000]<<20;
2176+ u/=1000; /* now 0, 1, or 2 */
2177+ encode|=u<<30;
2178+ DFWORD(result, DECWORDS-1)=encode;
2179+ return result;
2180+ } /* decFloatFromInt32 */
2181+
2182+/* ------------------------------------------------------------------ */
2183+/* decFloatFromUInt -- initialise a decFloat from a uInt */
2184+/* */
2185+/* result gets the converted uInt */
2186+/* n is the uInt to convert */
2187+/* returns result */
2188+/* */
2189+/* The result is Exact; no errors or exceptions are possible. */
2190+/* ------------------------------------------------------------------ */
2191+decFloat * decFloatFromUInt32(decFloat *result, uInt u) {
2192+ uInt encode; /* work */
2193+ DFWORD(result, 0)=ZEROWORD; /* always */
2194+ #if QUAD
2195+ DFWORD(result, 1)=0;
2196+ DFWORD(result, 2)=0;
2197+ #endif
2198+ encode=BIN2DPD[u%1000];
2199+ u/=1000;
2200+ encode|=BIN2DPD[u%1000]<<10;
2201+ u/=1000;
2202+ encode|=BIN2DPD[u%1000]<<20;
2203+ u/=1000; /* now 0 -> 4 */
2204+ encode|=u<<30;
2205+ DFWORD(result, DECWORDS-1)=encode;
2206+ DFWORD(result, DECWORDS-2)|=u>>2; /* rarely non-zero */
2207+ return result;
2208+ } /* decFloatFromUInt32 */
2209+
2210+/* ------------------------------------------------------------------ */
2211+/* decFloatInvert -- logical digitwise INVERT of a decFloat */
2212+/* */
2213+/* result gets the result of INVERTing df */
2214+/* df is the decFloat to invert */
2215+/* set is the context */
2216+/* returns result, which will be canonical with sign=0 */
2217+/* */
2218+/* The operand must be positive, finite with exponent q=0, and */
2219+/* comprise just zeros and ones; if not, Invalid operation results. */
2220+/* ------------------------------------------------------------------ */
2221+decFloat * decFloatInvert(decFloat *result, const decFloat *df,
2222+ decContext *set) {
2223+ uInt sourhi=DFWORD(df, 0); /* top word of dfs */
2224+
2225+ if (!DFISUINT01(df) || !DFISCC01(df)) return decInvalid(result, set);
2226+ /* the operand is a finite integer (q=0) */
2227+ #if DOUBLE
2228+ DFWORD(result, 0)=ZEROWORD|((~sourhi)&0x04009124);
2229+ DFWORD(result, 1)=(~DFWORD(df, 1)) &0x49124491;
2230+ #elif QUAD
2231+ DFWORD(result, 0)=ZEROWORD|((~sourhi)&0x04000912);
2232+ DFWORD(result, 1)=(~DFWORD(df, 1)) &0x44912449;
2233+ DFWORD(result, 2)=(~DFWORD(df, 2)) &0x12449124;
2234+ DFWORD(result, 3)=(~DFWORD(df, 3)) &0x49124491;
2235+ #endif
2236+ return result;
2237+ } /* decFloatInvert */
2238+
2239+/* ------------------------------------------------------------------ */
2240+/* decFloatIs -- decFloat tests (IsSigned, etc.) */
2241+/* */
2242+/* df is the decFloat to test */
2243+/* returns 0 or 1 in an int32_t */
2244+/* */
2245+/* Many of these could be macros, but having them as real functions */
2246+/* is a bit cleaner (and they can be referred to here by the generic */
2247+/* names) */
2248+/* ------------------------------------------------------------------ */
2249+uInt decFloatIsCanonical(const decFloat *df) {
2250+ if (DFISSPECIAL(df)) {
2251+ if (DFISINF(df)) {
2252+ if (DFWORD(df, 0)&ECONMASK) return 0; /* exponent continuation */
2253+ if (!DFISCCZERO(df)) return 0; /* coefficient continuation */
2254+ return 1;
2255+ }
2256+ /* is a NaN */
2257+ if (DFWORD(df, 0)&ECONNANMASK) return 0; /* exponent continuation */
2258+ if (DFISCCZERO(df)) return 1; /* coefficient continuation */
2259+ /* drop through to check payload */
2260+ }
2261+ { /* declare block */
2262+ #if DOUBLE
2263+ uInt sourhi=DFWORD(df, 0);
2264+ uInt sourlo=DFWORD(df, 1);
2265+ if (CANONDPDOFF(sourhi, 8)
2266+ && CANONDPDTWO(sourhi, sourlo, 30)
2267+ && CANONDPDOFF(sourlo, 20)
2268+ && CANONDPDOFF(sourlo, 10)
2269+ && CANONDPDOFF(sourlo, 0)) return 1;
2270+ #elif QUAD
2271+ uInt sourhi=DFWORD(df, 0);
2272+ uInt sourmh=DFWORD(df, 1);
2273+ uInt sourml=DFWORD(df, 2);
2274+ uInt sourlo=DFWORD(df, 3);
2275+ if (CANONDPDOFF(sourhi, 4)
2276+ && CANONDPDTWO(sourhi, sourmh, 26)
2277+ && CANONDPDOFF(sourmh, 16)
2278+ && CANONDPDOFF(sourmh, 6)
2279+ && CANONDPDTWO(sourmh, sourml, 28)
2280+ && CANONDPDOFF(sourml, 18)
2281+ && CANONDPDOFF(sourml, 8)
2282+ && CANONDPDTWO(sourml, sourlo, 30)
2283+ && CANONDPDOFF(sourlo, 20)
2284+ && CANONDPDOFF(sourlo, 10)
2285+ && CANONDPDOFF(sourlo, 0)) return 1;
2286+ #endif
2287+ } /* block */
2288+ return 0; /* a declet is non-canonical */
2289+ }
2290+
2291+uInt decFloatIsFinite(const decFloat *df) {
2292+ return !DFISSPECIAL(df);
2293+ }
2294+uInt decFloatIsInfinite(const decFloat *df) {
2295+ return DFISINF(df);
2296+ }
2297+uInt decFloatIsInteger(const decFloat *df) {
2298+ return DFISINT(df);
2299+ }
2300+uInt decFloatIsNaN(const decFloat *df) {
2301+ return DFISNAN(df);
2302+ }
2303+uInt decFloatIsNormal(const decFloat *df) {
2304+ Int exp; /* exponent */
2305+ if (DFISSPECIAL(df)) return 0;
2306+ if (DFISZERO(df)) return 0;
2307+ /* is finite and non-zero */
2308+ exp=GETEXPUN(df) /* get unbiased exponent .. */
2309+ +decFloatDigits(df)-1; /* .. and make adjusted exponent */
2310+ return (exp>=DECEMIN); /* < DECEMIN is subnormal */
2311+ }
2312+uInt decFloatIsSignaling(const decFloat *df) {
2313+ return DFISSNAN(df);
2314+ }
2315+uInt decFloatIsSignalling(const decFloat *df) {
2316+ return DFISSNAN(df);
2317+ }
2318+uInt decFloatIsSigned(const decFloat *df) {
2319+ return DFISSIGNED(df);
2320+ }
2321+uInt decFloatIsSubnormal(const decFloat *df) {
2322+ if (DFISSPECIAL(df)) return 0;
2323+ /* is finite */
2324+ if (decFloatIsNormal(df)) return 0;
2325+ /* it is <Nmin, but could be zero */
2326+ if (DFISZERO(df)) return 0;
2327+ return 1; /* is subnormal */
2328+ }
2329+uInt decFloatIsZero(const decFloat *df) {
2330+ return DFISZERO(df);
2331+ } /* decFloatIs... */
2332+
2333+/* ------------------------------------------------------------------ */
2334+/* decFloatLogB -- return adjusted exponent, by 754r rules */
2335+/* */
2336+/* result gets the adjusted exponent as an integer, or a NaN etc. */
2337+/* df is the decFloat to be examined */
2338+/* set is the context */
2339+/* returns result */
2340+/* */
2341+/* Notable cases: */
2342+/* A<0 -> Use |A| */
2343+/* A=0 -> -Infinity (Division by zero) */
2344+/* A=Infinite -> +Infinity (Exact) */
2345+/* A=1 exactly -> 0 (Exact) */
2346+/* NaNs are propagated as usual */
2347+/* ------------------------------------------------------------------ */
2348+decFloat * decFloatLogB(decFloat *result, const decFloat *df,
2349+ decContext *set) {
2350+ Int ae; /* adjusted exponent */
2351+ if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
2352+ if (DFISINF(df)) {
2353+ DFWORD(result, 0)=0; /* need +ve */
2354+ return decInfinity(result, result); /* canonical +Infinity */
2355+ }
2356+ if (DFISZERO(df)) {
2357+ set->status|=DEC_Division_by_zero; /* as per 754r */
2358+ DFWORD(result, 0)=DECFLOAT_Sign; /* make negative */
2359+ return decInfinity(result, result); /* canonical -Infinity */
2360+ }
2361+ ae=GETEXPUN(df) /* get unbiased exponent .. */
2362+ +decFloatDigits(df)-1; /* .. and make adjusted exponent */
2363+ /* ae has limited range (3 digits for DOUBLE and 4 for QUAD), so */
2364+ /* it is worth using a special case of decFloatFromInt32 */
2365+ DFWORD(result, 0)=ZEROWORD; /* always */
2366+ if (ae<0) {
2367+ DFWORD(result, 0)|=DECFLOAT_Sign; /* -0 so far */
2368+ ae=-ae;
2369+ }
2370+ #if DOUBLE
2371+ DFWORD(result, 1)=BIN2DPD[ae]; /* a single declet */
2372+ #elif QUAD
2373+ DFWORD(result, 1)=0;
2374+ DFWORD(result, 2)=0;
2375+ DFWORD(result, 3)=(ae/1000)<<10; /* is <10, so need no DPD encode */
2376+ DFWORD(result, 3)|=BIN2DPD[ae%1000];
2377+ #endif
2378+ return result;
2379+ } /* decFloatLogB */
2380+
2381+/* ------------------------------------------------------------------ */
2382+/* decFloatMax -- return maxnum of two operands */
2383+/* */
2384+/* result gets the chosen decFloat */
2385+/* dfl is the first decFloat (lhs) */
2386+/* dfr is the second decFloat (rhs) */
2387+/* set is the context */
2388+/* returns result */
2389+/* */
2390+/* If just one operand is a quiet NaN it is ignored. */
2391+/* ------------------------------------------------------------------ */
2392+decFloat * decFloatMax(decFloat *result,
2393+ const decFloat *dfl, const decFloat *dfr,
2394+ decContext *set) {
2395+ Int comp;
2396+ if (DFISNAN(dfl)) {
2397+ /* sNaN or both NaNs leads to normal NaN processing */
2398+ if (DFISNAN(dfr) || DFISSNAN(dfl)) return decNaNs(result, dfl, dfr, set);
2399+ return decCanonical(result, dfr); /* RHS is numeric */
2400+ }
2401+ if (DFISNAN(dfr)) {
2402+ /* sNaN leads to normal NaN processing (both NaN handled above) */
2403+ if (DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set);
2404+ return decCanonical(result, dfl); /* LHS is numeric */
2405+ }
2406+ /* Both operands are numeric; numeric comparison needed -- use */
2407+ /* total order for a well-defined choice (and +0 > -0) */
2408+ comp=decNumCompare(dfl, dfr, 1);
2409+ if (comp>=0) return decCanonical(result, dfl);
2410+ return decCanonical(result, dfr);
2411+ } /* decFloatMax */
2412+
2413+/* ------------------------------------------------------------------ */
2414+/* decFloatMaxMag -- return maxnummag of two operands */
2415+/* */
2416+/* result gets the chosen decFloat */
2417+/* dfl is the first decFloat (lhs) */
2418+/* dfr is the second decFloat (rhs) */
2419+/* set is the context */
2420+/* returns result */
2421+/* */
2422+/* Returns according to the magnitude comparisons if both numeric and */
2423+/* unequal, otherwise returns maxnum */
2424+/* ------------------------------------------------------------------ */
2425+decFloat * decFloatMaxMag(decFloat *result,
2426+ const decFloat *dfl, const decFloat *dfr,
2427+ decContext *set) {
2428+ Int comp;
2429+ decFloat absl, absr;
2430+ if (DFISNAN(dfl) || DFISNAN(dfr)) return decFloatMax(result, dfl, dfr, set);
2431+
2432+ decFloatCopyAbs(&absl, dfl);
2433+ decFloatCopyAbs(&absr, dfr);
2434+ comp=decNumCompare(&absl, &absr, 0);
2435+ if (comp>0) return decCanonical(result, dfl);
2436+ if (comp<0) return decCanonical(result, dfr);
2437+ return decFloatMax(result, dfl, dfr, set);
2438+ } /* decFloatMaxMag */
2439+
2440+/* ------------------------------------------------------------------ */
2441+/* decFloatMin -- return minnum of two operands */
2442+/* */
2443+/* result gets the chosen decFloat */
2444+/* dfl is the first decFloat (lhs) */
2445+/* dfr is the second decFloat (rhs) */
2446+/* set is the context */
2447+/* returns result */
2448+/* */
2449+/* If just one operand is a quiet NaN it is ignored. */
2450+/* ------------------------------------------------------------------ */
2451+decFloat * decFloatMin(decFloat *result,
2452+ const decFloat *dfl, const decFloat *dfr,
2453+ decContext *set) {
2454+ Int comp;
2455+ if (DFISNAN(dfl)) {
2456+ /* sNaN or both NaNs leads to normal NaN processing */
2457+ if (DFISNAN(dfr) || DFISSNAN(dfl)) return decNaNs(result, dfl, dfr, set);
2458+ return decCanonical(result, dfr); /* RHS is numeric */
2459+ }
2460+ if (DFISNAN(dfr)) {
2461+ /* sNaN leads to normal NaN processing (both NaN handled above) */
2462+ if (DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set);
2463+ return decCanonical(result, dfl); /* LHS is numeric */
2464+ }
2465+ /* Both operands are numeric; numeric comparison needed -- use */
2466+ /* total order for a well-defined choice (and +0 > -0) */
2467+ comp=decNumCompare(dfl, dfr, 1);
2468+ if (comp<=0) return decCanonical(result, dfl);
2469+ return decCanonical(result, dfr);
2470+ } /* decFloatMin */
2471+
2472+/* ------------------------------------------------------------------ */
2473+/* decFloatMinMag -- return minnummag of two operands */
2474+/* */
2475+/* result gets the chosen decFloat */
2476+/* dfl is the first decFloat (lhs) */
2477+/* dfr is the second decFloat (rhs) */
2478+/* set is the context */
2479+/* returns result */
2480+/* */
2481+/* Returns according to the magnitude comparisons if both numeric and */
2482+/* unequal, otherwise returns minnum */
2483+/* ------------------------------------------------------------------ */
2484+decFloat * decFloatMinMag(decFloat *result,
2485+ const decFloat *dfl, const decFloat *dfr,
2486+ decContext *set) {
2487+ Int comp;
2488+ decFloat absl, absr;
2489+ if (DFISNAN(dfl) || DFISNAN(dfr)) return decFloatMin(result, dfl, dfr, set);
2490+
2491+ decFloatCopyAbs(&absl, dfl);
2492+ decFloatCopyAbs(&absr, dfr);
2493+ comp=decNumCompare(&absl, &absr, 0);
2494+ if (comp<0) return decCanonical(result, dfl);
2495+ if (comp>0) return decCanonical(result, dfr);
2496+ return decFloatMin(result, dfl, dfr, set);
2497+ } /* decFloatMinMag */
2498+
2499+/* ------------------------------------------------------------------ */
2500+/* decFloatMinus -- negate value, heeding NaNs, etc. */
2501+/* */
2502+/* result gets the canonicalized 0-df */
2503+/* df is the decFloat to minus */
2504+/* set is the context */
2505+/* returns result */
2506+/* */
2507+/* This has the same effect as 0-df where the exponent of the zero is */
2508+/* the same as that of df (if df is finite). */
2509+/* The effect is also the same as decFloatCopyNegate except that NaNs */
2510+/* are handled normally (the sign of a NaN is not affected, and an */
2511+/* sNaN will signal), the result is canonical, and zero gets sign 0. */
2512+/* ------------------------------------------------------------------ */
2513+decFloat * decFloatMinus(decFloat *result, const decFloat *df,
2514+ decContext *set) {
2515+ if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
2516+ decCanonical(result, df); /* copy and check */
2517+ if (DFISZERO(df)) DFBYTE(result, 0)&=~0x80; /* turn off sign bit */
2518+ else DFBYTE(result, 0)^=0x80; /* flip sign bit */
2519+ return result;
2520+ } /* decFloatMinus */
2521+
2522+/* ------------------------------------------------------------------ */
2523+/* decFloatMultiply -- multiply two decFloats */
2524+/* */
2525+/* result gets the result of multiplying dfl and dfr: */
2526+/* dfl is the first decFloat (lhs) */
2527+/* dfr is the second decFloat (rhs) */
2528+/* set is the context */
2529+/* returns result */
2530+/* */
2531+/* ------------------------------------------------------------------ */
2532+decFloat * decFloatMultiply(decFloat *result,
2533+ const decFloat *dfl, const decFloat *dfr,
2534+ decContext *set) {
2535+ bcdnum num; /* for final conversion */
2536+ uByte bcdacc[DECPMAX9*18+1]; /* for coefficent in BCD */
2537+
2538+ if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { /* either is special? */
2539+ /* NaNs are handled as usual */
2540+ if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
2541+ /* infinity times zero is bad */
2542+ if (DFISINF(dfl) && DFISZERO(dfr)) return decInvalid(result, set);
2543+ if (DFISINF(dfr) && DFISZERO(dfl)) return decInvalid(result, set);
2544+ /* both infinite; return canonical infinity with computed sign */
2545+ DFWORD(result, 0)=DFWORD(dfl, 0)^DFWORD(dfr, 0); /* compute sign */
2546+ return decInfinity(result, result);
2547+ }
2548+
2549+ /* Here when both operands are finite */
2550+ decFiniteMultiply(&num, bcdacc, dfl, dfr);
2551+ return decFinalize(result, &num, set); /* round, check, and lay out */
2552+ } /* decFloatMultiply */
2553+
2554+/* ------------------------------------------------------------------ */
2555+/* decFloatNextMinus -- next towards -Infinity */
2556+/* */
2557+/* result gets the next lesser decFloat */
2558+/* dfl is the decFloat to start with */
2559+/* set is the context */
2560+/* returns result */
2561+/* */
2562+/* This is 754r nextdown; Invalid is the only status possible (from */
2563+/* an sNaN). */
2564+/* ------------------------------------------------------------------ */
2565+decFloat * decFloatNextMinus(decFloat *result, const decFloat *dfl,
2566+ decContext *set) {
2567+ decFloat delta; /* tiny increment */
2568+ uInt savestat; /* saves status */
2569+ enum rounding saveround; /* .. and mode */
2570+
2571+ /* +Infinity is the special case */
2572+ if (DFISINF(dfl) && !DFISSIGNED(dfl)) {
2573+ DFSETNMAX(result);
2574+ return result; /* [no status to set] */
2575+ }
2576+ /* other cases are effected by sutracting a tiny delta -- this */
2577+ /* should be done in a wider format as the delta is unrepresentable */
2578+ /* here (but can be done with normal add if the sign of zero is */
2579+ /* treated carefully, because no Inexactitude is interesting); */
2580+ /* rounding to -Infinity then pushes the result to next below */
2581+ decFloatZero(&delta); /* set up tiny delta */
2582+ DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */
2583+ DFWORD(&delta, 0)=DECFLOAT_Sign; /* Sign=1 + biased exponent=0 */
2584+ /* set up for the directional round */
2585+ saveround=set->round; /* save mode */
2586+ set->round=DEC_ROUND_FLOOR; /* .. round towards -Infinity */
2587+ savestat=set->status; /* save status */
2588+ decFloatAdd(result, dfl, &delta, set);
2589+ /* Add rules mess up the sign when going from +Ntiny to 0 */
2590+ if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; /* correct */
2591+ set->status&=DEC_Invalid_operation; /* preserve only sNaN status */
2592+ set->status|=savestat; /* restore pending flags */
2593+ set->round=saveround; /* .. and mode */
2594+ return result;
2595+ } /* decFloatNextMinus */
2596+
2597+/* ------------------------------------------------------------------ */
2598+/* decFloatNextPlus -- next towards +Infinity */
2599+/* */
2600+/* result gets the next larger decFloat */
2601+/* dfl is the decFloat to start with */
2602+/* set is the context */
2603+/* returns result */
2604+/* */
2605+/* This is 754r nextup; Invalid is the only status possible (from */
2606+/* an sNaN). */
2607+/* ------------------------------------------------------------------ */
2608+decFloat * decFloatNextPlus(decFloat *result, const decFloat *dfl,
2609+ decContext *set) {
2610+ uInt savestat; /* saves status */
2611+ enum rounding saveround; /* .. and mode */
2612+ decFloat delta; /* tiny increment */
2613+
2614+ /* -Infinity is the special case */
2615+ if (DFISINF(dfl) && DFISSIGNED(dfl)) {
2616+ DFSETNMAX(result);
2617+ DFWORD(result, 0)|=DECFLOAT_Sign; /* make negative */
2618+ return result; /* [no status to set] */
2619+ }
2620+ /* other cases are effected by sutracting a tiny delta -- this */
2621+ /* should be done in a wider format as the delta is unrepresentable */
2622+ /* here (but can be done with normal add if the sign of zero is */
2623+ /* treated carefully, because no Inexactitude is interesting); */
2624+ /* rounding to +Infinity then pushes the result to next above */
2625+ decFloatZero(&delta); /* set up tiny delta */
2626+ DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */
2627+ DFWORD(&delta, 0)=0; /* Sign=0 + biased exponent=0 */
2628+ /* set up for the directional round */
2629+ saveround=set->round; /* save mode */
2630+ set->round=DEC_ROUND_CEILING; /* .. round towards +Infinity */
2631+ savestat=set->status; /* save status */
2632+ decFloatAdd(result, dfl, &delta, set);
2633+ /* Add rules mess up the sign when going from -Ntiny to -0 */
2634+ if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; /* correct */
2635+ set->status&=DEC_Invalid_operation; /* preserve only sNaN status */
2636+ set->status|=savestat; /* restore pending flags */
2637+ set->round=saveround; /* .. and mode */
2638+ return result;
2639+ } /* decFloatNextPlus */
2640+
2641+/* ------------------------------------------------------------------ */
2642+/* decFloatNextToward -- next towards a decFloat */
2643+/* */
2644+/* result gets the next decFloat */
2645+/* dfl is the decFloat to start with */
2646+/* dfr is the decFloat to move toward */
2647+/* set is the context */
2648+/* returns result */
2649+/* */
2650+/* This is 754r nextafter; status may be set unless the result is a */
2651+/* normal number. */
2652+/* ------------------------------------------------------------------ */
2653+decFloat * decFloatNextToward(decFloat *result,
2654+ const decFloat *dfl, const decFloat *dfr,
2655+ decContext *set) {
2656+ decFloat delta; /* tiny increment or decrement */
2657+ decFloat pointone; /* 1e-1 */
2658+ uInt savestat; /* saves status */
2659+ enum rounding saveround; /* .. and mode */
2660+ uInt deltatop; /* top word for delta */
2661+ Int comp; /* work */
2662+
2663+ if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
2664+ /* Both are numeric, so Invalid no longer a possibility */
2665+ comp=decNumCompare(dfl, dfr, 0);
2666+ if (comp==0) return decFloatCopySign(result, dfl, dfr); /* equal */
2667+ /* unequal; do NextPlus or NextMinus but with different status rules */
2668+
2669+ if (comp<0) { /* lhs<rhs, do NextPlus, see above for commentary */
2670+ if (DFISINF(dfl) && DFISSIGNED(dfl)) { /* -Infinity special case */
2671+ DFSETNMAX(result);
2672+ DFWORD(result, 0)|=DECFLOAT_Sign;
2673+ return result;
2674+ }
2675+ saveround=set->round; /* save mode */
2676+ set->round=DEC_ROUND_CEILING; /* .. round towards +Infinity */
2677+ deltatop=0; /* positive delta */
2678+ }
2679+ else { /* lhs>rhs, do NextMinus, see above for commentary */
2680+ if (DFISINF(dfl) && !DFISSIGNED(dfl)) { /* +Infinity special case */
2681+ DFSETNMAX(result);
2682+ return result;
2683+ }
2684+ saveround=set->round; /* save mode */
2685+ set->round=DEC_ROUND_FLOOR; /* .. round towards -Infinity */
2686+ deltatop=DECFLOAT_Sign; /* negative delta */
2687+ }
2688+ savestat=set->status; /* save status */
2689+ /* Here, Inexact is needed where appropriate (and hence Underflow, */
2690+ /* etc.). Therefore the tiny delta which is otherwise */
2691+ /* unrepresentable (see NextPlus and NextMinus) is constructed */
2692+ /* using the multiplication of FMA. */
2693+ decFloatZero(&delta); /* set up tiny delta */
2694+ DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */
2695+ DFWORD(&delta, 0)=deltatop; /* Sign + biased exponent=0 */
2696+ decFloatFromString(&pointone, "1E-1", set); /* set up multiplier */
2697+ decFloatFMA(result, &delta, &pointone, dfl, set);
2698+ /* [Delta is truly tiny, so no need to correct sign of zero] */
2699+ /* use new status unless the result is normal */
2700+ if (decFloatIsNormal(result)) set->status=savestat; /* else goes forward */
2701+ set->round=saveround; /* restore mode */
2702+ return result;
2703+ } /* decFloatNextToward */
2704+
2705+/* ------------------------------------------------------------------ */
2706+/* decFloatOr -- logical digitwise OR of two decFloats */
2707+/* */
2708+/* result gets the result of ORing dfl and dfr */
2709+/* dfl is the first decFloat (lhs) */
2710+/* dfr is the second decFloat (rhs) */
2711+/* set is the context */
2712+/* returns result, which will be canonical with sign=0 */
2713+/* */
2714+/* The operands must be positive, finite with exponent q=0, and */
2715+/* comprise just zeros and ones; if not, Invalid operation results. */
2716+/* ------------------------------------------------------------------ */
2717+decFloat * decFloatOr(decFloat *result,
2718+ const decFloat *dfl, const decFloat *dfr,
2719+ decContext *set) {
2720+ if (!DFISUINT01(dfl) || !DFISUINT01(dfr)
2721+ || !DFISCC01(dfl) || !DFISCC01(dfr)) return decInvalid(result, set);
2722+ /* the operands are positive finite integers (q=0) with just 0s and 1s */
2723+ #if DOUBLE
2724+ DFWORD(result, 0)=ZEROWORD
2725+ |((DFWORD(dfl, 0) | DFWORD(dfr, 0))&0x04009124);
2726+ DFWORD(result, 1)=(DFWORD(dfl, 1) | DFWORD(dfr, 1))&0x49124491;
2727+ #elif QUAD
2728+ DFWORD(result, 0)=ZEROWORD
2729+ |((DFWORD(dfl, 0) | DFWORD(dfr, 0))&0x04000912);
2730+ DFWORD(result, 1)=(DFWORD(dfl, 1) | DFWORD(dfr, 1))&0x44912449;
2731+ DFWORD(result, 2)=(DFWORD(dfl, 2) | DFWORD(dfr, 2))&0x12449124;
2732+ DFWORD(result, 3)=(DFWORD(dfl, 3) | DFWORD(dfr, 3))&0x49124491;
2733+ #endif
2734+ return result;
2735+ } /* decFloatOr */
2736+
2737+/* ------------------------------------------------------------------ */
2738+/* decFloatPlus -- add value to 0, heeding NaNs, etc. */
2739+/* */
2740+/* result gets the canonicalized 0+df */
2741+/* df is the decFloat to plus */
2742+/* set is the context */
2743+/* returns result */
2744+/* */
2745+/* This has the same effect as 0+df where the exponent of the zero is */
2746+/* the same as that of df (if df is finite). */
2747+/* The effect is also the same as decFloatCopy except that NaNs */
2748+/* are handled normally (the sign of a NaN is not affected, and an */
2749+/* sNaN will signal), the result is canonical, and zero gets sign 0. */
2750+/* ------------------------------------------------------------------ */
2751+decFloat * decFloatPlus(decFloat *result, const decFloat *df,
2752+ decContext *set) {
2753+ if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
2754+ decCanonical(result, df); /* copy and check */
2755+ if (DFISZERO(df)) DFBYTE(result, 0)&=~0x80; /* turn off sign bit */
2756+ return result;
2757+ } /* decFloatPlus */
2758+
2759+/* ------------------------------------------------------------------ */
2760+/* decFloatQuantize -- quantize a decFloat */
2761+/* */
2762+/* result gets the result of quantizing dfl to match dfr */
2763+/* dfl is the first decFloat (lhs) */
2764+/* dfr is the second decFloat (rhs), which sets the exponent */
2765+/* set is the context */
2766+/* returns result */
2767+/* */
2768+/* Unless there is an error or the result is infinite, the exponent */
2769+/* of result is guaranteed to be the same as that of dfr. */
2770+/* ------------------------------------------------------------------ */
2771+decFloat * decFloatQuantize(decFloat *result,
2772+ const decFloat *dfl, const decFloat *dfr,
2773+ decContext *set) {
2774+ Int explb, exprb; /* left and right biased exponents */
2775+ uByte *ulsd; /* local LSD pointer */
2776+ uInt *ui; /* work */
2777+ uByte *ub; /* .. */
2778+ Int drop; /* .. */
2779+ uInt dpd; /* .. */
2780+ uInt encode; /* encoding accumulator */
2781+ uInt sourhil, sourhir; /* top words from source decFloats */
2782+ /* the following buffer holds the coefficient for manipulation */
2783+ uByte buf[4+DECPMAX*3]; /* + space for zeros to left or right */
2784+ #if DECTRACE
2785+ bcdnum num; /* for trace displays */
2786+ #endif
2787+
2788+ /* Start decoding the arguments */
2789+ sourhil=DFWORD(dfl, 0); /* LHS top word */
2790+ explb=DECCOMBEXP[sourhil>>26]; /* get exponent high bits (in place) */
2791+ sourhir=DFWORD(dfr, 0); /* RHS top word */
2792+ exprb=DECCOMBEXP[sourhir>>26];
2793+
2794+ if (EXPISSPECIAL(explb | exprb)) { /* either is special? */
2795+ /* NaNs are handled as usual */
2796+ if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
2797+ /* one infinity but not both is bad */
2798+ if (DFISINF(dfl)!=DFISINF(dfr)) return decInvalid(result, set);
2799+ /* both infinite; return canonical infinity with sign of LHS */
2800+ return decInfinity(result, dfl);
2801+ }
2802+
2803+ /* Here when both arguments are finite */
2804+ /* complete extraction of the exponents [no need to unbias] */
2805+ explb+=GETECON(dfl); /* + continuation */
2806+ exprb+=GETECON(dfr); /* .. */
2807+
2808+ /* calculate the number of digits to drop from the coefficient */
2809+ drop=exprb-explb; /* 0 if nothing to do */
2810+ if (drop==0) return decCanonical(result, dfl); /* return canonical */
2811+
2812+ /* the coefficient is needed; lay it out into buf, offset so zeros */
2813+ /* can be added before or after as needed -- an extra heading is */
2814+ /* added so can safely pad Quad DECPMAX-1 zeros to the left by */
2815+ /* fours */
2816+ #define BUFOFF (buf+4+DECPMAX)
2817+ GETCOEFF(dfl, BUFOFF); /* decode from decFloat */
2818+ /* [now the msd is at BUFOFF and the lsd is at BUFOFF+DECPMAX-1] */
2819+
2820+ #if DECTRACE
2821+ num.msd=BUFOFF;
2822+ num.lsd=BUFOFF+DECPMAX-1;
2823+ num.exponent=explb-DECBIAS;
2824+ num.sign=sourhil & DECFLOAT_Sign;
2825+ decShowNum(&num, "dfl");
2826+ #endif
2827+
2828+ if (drop>0) { /* [most common case] */
2829+ /* (this code is very similar to that in decFloatFinalize, but */
2830+ /* has many differences so is duplicated here -- so any changes */
2831+ /* may need to be made there, too) */
2832+ uByte *roundat; /* -> re-round digit */
2833+ uByte reround; /* reround value */
2834+ /* printf("Rounding; drop=%ld\n", (LI)drop); */
2835+
2836+ /* there is at least one zero needed to the left, in all but one */
2837+ /* exceptional (all-nines) case, so place four zeros now; this is */
2838+ /* needed almost always and makes rounding all-nines by fours safe */
2839+ UINTAT(BUFOFF-4)=0;
2840+
2841+ /* Three cases here: */
2842+ /* 1. new LSD is in coefficient (almost always) */
2843+ /* 2. new LSD is digit to left of coefficient (so MSD is */
2844+ /* round-for-reround digit) */
2845+ /* 3. new LSD is to left of case 2 (whole coefficient is sticky) */
2846+ /* Note that leading zeros can safely be treated as useful digits */
2847+
2848+ /* [duplicate check-stickies code to save a test] */
2849+ /* [by-digit check for stickies as runs of zeros are rare] */
2850+ if (drop<DECPMAX) { /* NB lengths not addresses */
2851+ roundat=BUFOFF+DECPMAX-drop;
2852+ reround=*roundat;
2853+ for (ub=roundat+1; ub<BUFOFF+DECPMAX; ub++) {
2854+ if (*ub!=0) { /* non-zero to be discarded */
2855+ reround=DECSTICKYTAB[reround]; /* apply sticky bit */
2856+ break; /* [remainder don't-care] */
2857+ }
2858+ } /* check stickies */
2859+ ulsd=roundat-1; /* set LSD */
2860+ }
2861+ else { /* edge case */
2862+ if (drop==DECPMAX) {
2863+ roundat=BUFOFF;
2864+ reround=*roundat;
2865+ }
2866+ else {
2867+ roundat=BUFOFF-1;
2868+ reround=0;
2869+ }
2870+ for (ub=roundat+1; ub<BUFOFF+DECPMAX; ub++) {
2871+ if (*ub!=0) { /* non-zero to be discarded */
2872+ reround=DECSTICKYTAB[reround]; /* apply sticky bit */
2873+ break; /* [remainder don't-care] */
2874+ }
2875+ } /* check stickies */
2876+ *BUFOFF=0; /* make a coefficient of 0 */
2877+ ulsd=BUFOFF; /* .. at the MSD place */
2878+ }
2879+
2880+ if (reround!=0) { /* discarding non-zero */
2881+ uInt bump=0;
2882+ set->status|=DEC_Inexact;
2883+
2884+ /* next decide whether to increment the coefficient */
2885+ if (set->round==DEC_ROUND_HALF_EVEN) { /* fastpath slowest case */
2886+ if (reround>5) bump=1; /* >0.5 goes up */
2887+ else if (reround==5) /* exactly 0.5000 .. */
2888+ bump=*ulsd & 0x01; /* .. up iff [new] lsd is odd */
2889+ } /* r-h-e */
2890+ else switch (set->round) {
2891+ case DEC_ROUND_DOWN: {
2892+ /* no change */
2893+ break;} /* r-d */
2894+ case DEC_ROUND_HALF_DOWN: {
2895+ if (reround>5) bump=1;
2896+ break;} /* r-h-d */
2897+ case DEC_ROUND_HALF_UP: {
2898+ if (reround>=5) bump=1;
2899+ break;} /* r-h-u */
2900+ case DEC_ROUND_UP: {
2901+ if (reround>0) bump=1;
2902+ break;} /* r-u */
2903+ case DEC_ROUND_CEILING: {
2904+ /* same as _UP for positive numbers, and as _DOWN for negatives */
2905+ if (!(sourhil&DECFLOAT_Sign) && reround>0) bump=1;
2906+ break;} /* r-c */
2907+ case DEC_ROUND_FLOOR: {
2908+ /* same as _UP for negative numbers, and as _DOWN for positive */
2909+ /* [negative reround cannot occur on 0] */
2910+ if (sourhil&DECFLOAT_Sign && reround>0) bump=1;
2911+ break;} /* r-f */
2912+ case DEC_ROUND_05UP: {
2913+ if (reround>0) { /* anything out there is 'sticky' */
2914+ /* bump iff lsd=0 or 5; this cannot carry so it could be */
2915+ /* effected immediately with no bump -- but the code */
2916+ /* is clearer if this is done the same way as the others */
2917+ if (*ulsd==0 || *ulsd==5) bump=1;
2918+ }
2919+ break;} /* r-r */
2920+ default: { /* e.g., DEC_ROUND_MAX */
2921+ set->status|=DEC_Invalid_context;
2922+ #if DECCHECK
2923+ printf("Unknown rounding mode: %ld\n", (LI)set->round);
2924+ #endif
2925+ break;}
2926+ } /* switch (not r-h-e) */
2927+ /* printf("ReRound: %ld bump: %ld\n", (LI)reround, (LI)bump); */
2928+
2929+ if (bump!=0) { /* need increment */
2930+ /* increment the coefficient; this could give 1000... (after */
2931+ /* the all nines case) */
2932+ ub=ulsd;
2933+ for (; UINTAT(ub-3)==0x09090909; ub-=4) UINTAT(ub-3)=0;
2934+ /* now at most 3 digits left to non-9 (usually just the one) */
2935+ for (; *ub==9; ub--) *ub=0;
2936+ *ub+=1;
2937+ /* [the all-nines case will have carried one digit to the */
2938+ /* left of the original MSD -- just where it is needed] */
2939+ } /* bump needed */
2940+ } /* inexact rounding */
2941+
2942+ /* now clear zeros to the left so exactly DECPMAX digits will be */
2943+ /* available in the coefficent -- the first word to the left was */
2944+ /* cleared earlier for safe carry; now add any more needed */
2945+ if (drop>4) {
2946+ UINTAT(BUFOFF-8)=0; /* must be at least 5 */
2947+ for (ui=&UINTAT(BUFOFF-12); ui>&UINTAT(ulsd-DECPMAX-3); ui--) *ui=0;
2948+ }
2949+ } /* need round (drop>0) */
2950+
2951+ else { /* drop<0; padding with -drop digits is needed */
2952+ /* This is the case where an error can occur if the padded */
2953+ /* coefficient will not fit; checking for this can be done in the */
2954+ /* same loop as padding for zeros if the no-hope and zero cases */
2955+ /* are checked first */
2956+ if (-drop>DECPMAX-1) { /* cannot fit unless 0 */
2957+ if (!ISCOEFFZERO(BUFOFF)) return decInvalid(result, set);
2958+ /* a zero can have any exponent; just drop through and use it */
2959+ ulsd=BUFOFF+DECPMAX-1;
2960+ }
2961+ else { /* padding will fit (but may still be too long) */
2962+ /* final-word mask depends on endianess */
2963+ #if DECLITEND
2964+ static const uInt dmask[]={0, 0x000000ff, 0x0000ffff, 0x00ffffff};
2965+ #else
2966+ static const uInt dmask[]={0, 0xff000000, 0xffff0000, 0xffffff00};
2967+ #endif
2968+ for (ui=&UINTAT(BUFOFF+DECPMAX);; ui++) {
2969+ *ui=0;
2970+ if (UINTAT(&UBYTEAT(ui)-DECPMAX)!=0) { /* could be bad */
2971+ /* if all four digits should be zero, definitely bad */
2972+ if (ui<=&UINTAT(BUFOFF+DECPMAX+(-drop)-4))
2973+ return decInvalid(result, set);
2974+ /* must be a 1- to 3-digit sequence; check more carefully */
2975+ if ((UINTAT(&UBYTEAT(ui)-DECPMAX)&dmask[(-drop)%4])!=0)
2976+ return decInvalid(result, set);
2977+ break; /* no need for loop end test */
2978+ }
2979+ if (ui>=&UINTAT(BUFOFF+DECPMAX+(-drop)-4)) break; /* done */
2980+ }
2981+ ulsd=BUFOFF+DECPMAX+(-drop)-1;
2982+ } /* pad and check leading zeros */
2983+ } /* drop<0 */
2984+
2985+ #if DECTRACE
2986+ num.msd=ulsd-DECPMAX+1;
2987+ num.lsd=ulsd;
2988+ num.exponent=explb-DECBIAS;
2989+ num.sign=sourhil & DECFLOAT_Sign;
2990+ decShowNum(&num, "res");
2991+ #endif
2992+
2993+ /*------------------------------------------------------------------*/
2994+ /* At this point the result is DECPMAX digits, ending at ulsd, so */
2995+ /* fits the encoding exactly; there is no possibility of error */
2996+ /*------------------------------------------------------------------*/
2997+ encode=((exprb>>DECECONL)<<4) + *(ulsd-DECPMAX+1); /* make index */
2998+ encode=DECCOMBFROM[encode]; /* indexed by (0-2)*16+msd */
2999+ /* the exponent continuation can be extracted from the original RHS */
3000+ encode|=sourhir & ECONMASK;
3001+ encode|=sourhil&DECFLOAT_Sign; /* add the sign from LHS */
3002+
3003+ /* finally encode the coefficient */
3004+ /* private macro to encode a declet; this version can be used */
3005+ /* because all coefficient digits exist */
3006+ #define getDPD3q(dpd, n) ub=ulsd-(3*(n))-2; \
3007+ dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)];
3008+
3009+ #if DOUBLE
3010+ getDPD3q(dpd, 4); encode|=dpd<<8;
3011+ getDPD3q(dpd, 3); encode|=dpd>>2;
3012+ DFWORD(result, 0)=encode;
3013+ encode=dpd<<30;
3014+ getDPD3q(dpd, 2); encode|=dpd<<20;
3015+ getDPD3q(dpd, 1); encode|=dpd<<10;
3016+ getDPD3q(dpd, 0); encode|=dpd;
3017+ DFWORD(result, 1)=encode;
3018+
3019+ #elif QUAD
3020+ getDPD3q(dpd,10); encode|=dpd<<4;
3021+ getDPD3q(dpd, 9); encode|=dpd>>6;
3022+ DFWORD(result, 0)=encode;
3023+ encode=dpd<<26;
3024+ getDPD3q(dpd, 8); encode|=dpd<<16;
3025+ getDPD3q(dpd, 7); encode|=dpd<<6;
3026+ getDPD3q(dpd, 6); encode|=dpd>>4;
3027+ DFWORD(result, 1)=encode;
3028+ encode=dpd<<28;
3029+ getDPD3q(dpd, 5); encode|=dpd<<18;
3030+ getDPD3q(dpd, 4); encode|=dpd<<8;
3031+ getDPD3q(dpd, 3); encode|=dpd>>2;
3032+ DFWORD(result, 2)=encode;
3033+ encode=dpd<<30;
3034+ getDPD3q(dpd, 2); encode|=dpd<<20;
3035+ getDPD3q(dpd, 1); encode|=dpd<<10;
3036+ getDPD3q(dpd, 0); encode|=dpd;
3037+ DFWORD(result, 3)=encode;
3038+ #endif
3039+ return result;
3040+ } /* decFloatQuantize */
3041+
3042+/* ------------------------------------------------------------------ */
3043+/* decFloatReduce -- reduce finite coefficient to minimum length */
3044+/* */
3045+/* result gets the reduced decFloat */
3046+/* df is the source decFloat */
3047+/* set is the context */
3048+/* returns result, which will be canonical */
3049+/* */
3050+/* This removes all possible trailing zeros from the coefficient; */
3051+/* some may remain when the number is very close to Nmax. */
3052+/* Special values are unchanged and no status is set unless df=sNaN. */
3053+/* Reduced zero has an exponent q=0. */
3054+/* ------------------------------------------------------------------ */
3055+decFloat * decFloatReduce(decFloat *result, const decFloat *df,
3056+ decContext *set) {
3057+ bcdnum num; /* work */
3058+ uByte buf[DECPMAX], *ub; /* coefficient and pointer */
3059+ if (df!=result) *result=*df; /* copy, if needed */
3060+ if (DFISNAN(df)) return decNaNs(result, df, NULL, set); /* sNaN */
3061+ /* zeros and infinites propagate too */
3062+ if (DFISINF(df)) return decInfinity(result, df); /* canonical */
3063+ if (DFISZERO(df)) {
3064+ uInt sign=DFWORD(df, 0)&DECFLOAT_Sign;
3065+ decFloatZero(result);
3066+ DFWORD(result, 0)|=sign;
3067+ return result; /* exponent dropped, sign OK */
3068+ }
3069+ /* non-zero finite */
3070+ GETCOEFF(df, buf);
3071+ ub=buf+DECPMAX-1; /* -> lsd */
3072+ if (*ub) return result; /* no trailing zeros */
3073+ for (ub--; *ub==0;) ub--; /* terminates because non-zero */
3074+ /* *ub is the first non-zero from the right */
3075+ num.sign=DFWORD(df, 0)&DECFLOAT_Sign; /* set up number... */
3076+ num.exponent=GETEXPUN(df)+(Int)(buf+DECPMAX-1-ub); /* adjusted exponent */
3077+ num.msd=buf;
3078+ num.lsd=ub;
3079+ return decFinalize(result, &num, set);
3080+ } /* decFloatReduce */
3081+
3082+/* ------------------------------------------------------------------ */
3083+/* decFloatRemainder -- integer divide and return remainder */
3084+/* */
3085+/* result gets the remainder of dividing dfl by dfr: */
3086+/* dfl is the first decFloat (lhs) */
3087+/* dfr is the second decFloat (rhs) */
3088+/* set is the context */
3089+/* returns result */
3090+/* */
3091+/* ------------------------------------------------------------------ */
3092+decFloat * decFloatRemainder(decFloat *result,
3093+ const decFloat *dfl, const decFloat *dfr,
3094+ decContext *set) {
3095+ return decDivide(result, dfl, dfr, set, REMAINDER);
3096+ } /* decFloatRemainder */
3097+
3098+/* ------------------------------------------------------------------ */
3099+/* decFloatRemainderNear -- integer divide to nearest and remainder */
3100+/* */
3101+/* result gets the remainder of dividing dfl by dfr: */
3102+/* dfl is the first decFloat (lhs) */
3103+/* dfr is the second decFloat (rhs) */
3104+/* set is the context */
3105+/* returns result */
3106+/* */
3107+/* This is the IEEE remainder, where the nearest integer is used. */
3108+/* ------------------------------------------------------------------ */
3109+decFloat * decFloatRemainderNear(decFloat *result,
3110+ const decFloat *dfl, const decFloat *dfr,
3111+ decContext *set) {
3112+ return decDivide(result, dfl, dfr, set, REMNEAR);
3113+ } /* decFloatRemainderNear */
3114+
3115+/* ------------------------------------------------------------------ */
3116+/* decFloatRotate -- rotate the coefficient of a decFloat left/right */
3117+/* */
3118+/* result gets the result of rotating dfl */
3119+/* dfl is the source decFloat to rotate */
3120+/* dfr is the count of digits to rotate, an integer (with q=0) */
3121+/* set is the context */
3122+/* returns result */
3123+/* */
3124+/* The digits of the coefficient of dfl are rotated to the left (if */
3125+/* dfr is positive) or to the right (if dfr is negative) without */
3126+/* adjusting the exponent or the sign of dfl. */
3127+/* */
3128+/* dfr must be in the range -DECPMAX through +DECPMAX. */
3129+/* NaNs are propagated as usual. An infinite dfl is unaffected (but */
3130+/* dfr must be valid). No status is set unless dfr is invalid or an */
3131+/* operand is an sNaN. The result is canonical. */
3132+/* ------------------------------------------------------------------ */
3133+#define PHALF (ROUNDUP(DECPMAX/2, 4)) /* half length, rounded up */
3134+decFloat * decFloatRotate(decFloat *result,
3135+ const decFloat *dfl, const decFloat *dfr,
3136+ decContext *set) {
3137+ Int rotate; /* dfr as an Int */
3138+ uByte buf[DECPMAX+PHALF]; /* coefficient + half */
3139+ uInt digits, savestat; /* work */
3140+ bcdnum num; /* .. */
3141+ uByte *ub; /* .. */
3142+
3143+ if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
3144+ if (!DFISINT(dfr)) return decInvalid(result, set);
3145+ digits=decFloatDigits(dfr); /* calculate digits */
3146+ if (digits>2) return decInvalid(result, set); /* definitely out of range */
3147+ rotate=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; /* is in bottom declet */
3148+ if (rotate>DECPMAX) return decInvalid(result, set); /* too big */
3149+ /* [from here on no error or status change is possible] */
3150+ if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */
3151+ /* handle no-rotate cases */
3152+ if (rotate==0 || rotate==DECPMAX) return decCanonical(result, dfl);
3153+ /* a real rotate is needed: 0 < rotate < DECPMAX */
3154+ /* reduce the rotation to no more than half to reduce copying later */
3155+ /* (for QUAD in fact half + 2 digits) */
3156+ if (DFISSIGNED(dfr)) rotate=-rotate;
3157+ if (abs(rotate)>PHALF) {
3158+ if (rotate<0) rotate=DECPMAX+rotate;
3159+ else rotate=rotate-DECPMAX;
3160+ }
3161+ /* now lay out the coefficient, leaving room to the right or the */
3162+ /* left depending on the direction of rotation */
3163+ ub=buf;
3164+ if (rotate<0) ub+=PHALF; /* rotate right, so space to left */
3165+ GETCOEFF(dfl, ub);
3166+ /* copy half the digits to left or right, and set num.msd */
3167+ if (rotate<0) {
3168+ memcpy(buf, buf+DECPMAX, PHALF);
3169+ num.msd=buf+PHALF+rotate;
3170+ }
3171+ else {
3172+ memcpy(buf+DECPMAX, buf, PHALF);
3173+ num.msd=buf+rotate;
3174+ }
3175+ /* fill in rest of num */
3176+ num.lsd=num.msd+DECPMAX-1;
3177+ num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
3178+ num.exponent=GETEXPUN(dfl);
3179+ savestat=set->status; /* record */
3180+ decFinalize(result, &num, set);
3181+ set->status=savestat; /* restore */
3182+ return result;
3183+ } /* decFloatRotate */
3184+
3185+/* ------------------------------------------------------------------ */
3186+/* decFloatSameQuantum -- test decFloats for same quantum */
3187+/* */
3188+/* dfl is the first decFloat (lhs) */
3189+/* dfr is the second decFloat (rhs) */
3190+/* returns 1 if the operands have the same quantum, 0 otherwise */
3191+/* */
3192+/* No error is possible and no status results. */
3193+/* ------------------------------------------------------------------ */
3194+uInt decFloatSameQuantum(const decFloat *dfl, const decFloat *dfr) {
3195+ if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) {
3196+ if (DFISNAN(dfl) && DFISNAN(dfr)) return 1;
3197+ if (DFISINF(dfl) && DFISINF(dfr)) return 1;
3198+ return 0; /* any other special mixture gives false */
3199+ }
3200+ if (GETEXP(dfl)==GETEXP(dfr)) return 1; /* biased exponents match */
3201+ return 0;
3202+ } /* decFloatSameQuantum */
3203+
3204+/* ------------------------------------------------------------------ */
3205+/* decFloatScaleB -- multiply by a power of 10, as per 754r */
3206+/* */
3207+/* result gets the result of the operation */
3208+/* dfl is the first decFloat (lhs) */
3209+/* dfr is the second decFloat (rhs), am integer (with q=0) */
3210+/* set is the context */
3211+/* returns result */
3212+/* */
3213+/* This computes result=dfl x 10**dfr where dfr is an integer in the */
3214+/* range +/-2*(emax+pmax), typically resulting from LogB. */
3215+/* Underflow and Overflow (with Inexact) may occur. NaNs propagate */
3216+/* as usual. */
3217+/* ------------------------------------------------------------------ */
3218+#define SCALEBMAX 2*(DECEMAX+DECPMAX) /* D=800, Q=12356 */
3219+decFloat * decFloatScaleB(decFloat *result,
3220+ const decFloat *dfl, const decFloat *dfr,
3221+ decContext *set) {
3222+ uInt digits; /* work */
3223+ Int expr; /* dfr as an Int */
3224+
3225+ if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
3226+ if (!DFISINT(dfr)) return decInvalid(result, set);
3227+ digits=decFloatDigits(dfr); /* calculate digits */
3228+
3229+ #if DOUBLE
3230+ if (digits>3) return decInvalid(result, set); /* definitely out of range */
3231+ expr=DPD2BIN[DFWORD(dfr, 1)&0x3ff]; /* must be in bottom declet */
3232+ #elif QUAD
3233+ if (digits>5) return decInvalid(result, set); /* definitely out of range */
3234+ expr=DPD2BIN[DFWORD(dfr, 3)&0x3ff] /* in bottom 2 declets .. */
3235+ +DPD2BIN[(DFWORD(dfr, 3)>>10)&0x3ff]*1000; /* .. */
3236+ #endif
3237+ if (expr>SCALEBMAX) return decInvalid(result, set); /* oops */
3238+ /* [from now on no error possible] */
3239+ if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */
3240+ if (DFISSIGNED(dfr)) expr=-expr;
3241+ /* dfl is finite and expr is valid */
3242+ *result=*dfl; /* copy to target */
3243+ return decFloatSetExponent(result, set, GETEXPUN(result)+expr);
3244+ } /* decFloatScaleB */
3245+
3246+/* ------------------------------------------------------------------ */
3247+/* decFloatShift -- shift the coefficient of a decFloat left or right */
3248+/* */
3249+/* result gets the result of shifting dfl */
3250+/* dfl is the source decFloat to shift */
3251+/* dfr is the count of digits to shift, an integer (with q=0) */
3252+/* set is the context */
3253+/* returns result */
3254+/* */
3255+/* The digits of the coefficient of dfl are shifted to the left (if */
3256+/* dfr is positive) or to the right (if dfr is negative) without */
3257+/* adjusting the exponent or the sign of dfl. */
3258+/* */
3259+/* dfr must be in the range -DECPMAX through +DECPMAX. */
3260+/* NaNs are propagated as usual. An infinite dfl is unaffected (but */
3261+/* dfr must be valid). No status is set unless dfr is invalid or an */
3262+/* operand is an sNaN. The result is canonical. */
3263+/* ------------------------------------------------------------------ */
3264+decFloat * decFloatShift(decFloat *result,
3265+ const decFloat *dfl, const decFloat *dfr,
3266+ decContext *set) {
3267+ Int shift; /* dfr as an Int */
3268+ uByte buf[DECPMAX*2]; /* coefficient + padding */
3269+ uInt digits, savestat; /* work */
3270+ bcdnum num; /* .. */
3271+
3272+ if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
3273+ if (!DFISINT(dfr)) return decInvalid(result, set);
3274+ digits=decFloatDigits(dfr); /* calculate digits */
3275+ if (digits>2) return decInvalid(result, set); /* definitely out of range */
3276+ shift=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; /* is in bottom declet */
3277+ if (shift>DECPMAX) return decInvalid(result, set); /* too big */
3278+ /* [from here on no error or status change is possible] */
3279+
3280+ if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */
3281+ /* handle no-shift and all-shift (clear to zero) cases */
3282+ if (shift==0) return decCanonical(result, dfl);
3283+ if (shift==DECPMAX) { /* zero with sign */
3284+ uByte sign=(uByte)(DFBYTE(dfl, 0)&0x80); /* save sign bit */
3285+ decFloatZero(result); /* make +0 */
3286+ DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); /* and set sign */
3287+ /* [cannot safely use CopySign] */
3288+ return result;
3289+ }
3290+ /* a real shift is needed: 0 < shift < DECPMAX */
3291+ num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
3292+ num.exponent=GETEXPUN(dfl);
3293+ num.msd=buf;
3294+ GETCOEFF(dfl, buf);
3295+ if (DFISSIGNED(dfr)) { /* shift right */
3296+ /* edge cases are taken care of, so this is easy */
3297+ num.lsd=buf+DECPMAX-shift-1;
3298+ }
3299+ else { /* shift left -- zero padding needed to right */
3300+ UINTAT(buf+DECPMAX)=0; /* 8 will handle most cases */
3301+ UINTAT(buf+DECPMAX+4)=0; /* .. */
3302+ if (shift>8) memset(buf+DECPMAX+8, 0, 8+QUAD*18); /* all other cases */
3303+ num.msd+=shift;
3304+ num.lsd=num.msd+DECPMAX-1;
3305+ }
3306+ savestat=set->status; /* record */
3307+ decFinalize(result, &num, set);
3308+ set->status=savestat; /* restore */
3309+ return result;
3310+ } /* decFloatShift */
3311+
3312+/* ------------------------------------------------------------------ */
3313+/* decFloatSubtract -- subtract a decFloat from another */
3314+/* */
3315+/* result gets the result of subtracting dfr from dfl: */
3316+/* dfl is the first decFloat (lhs) */
3317+/* dfr is the second decFloat (rhs) */
3318+/* set is the context */
3319+/* returns result */
3320+/* */
3321+/* ------------------------------------------------------------------ */
3322+decFloat * decFloatSubtract(decFloat *result,
3323+ const decFloat *dfl, const decFloat *dfr,
3324+ decContext *set) {
3325+ decFloat temp;
3326+ /* NaNs must propagate without sign change */
3327+ if (DFISNAN(dfr)) return decFloatAdd(result, dfl, dfr, set);
3328+ temp=*dfr; /* make a copy */
3329+ DFBYTE(&temp, 0)^=0x80; /* flip sign */
3330+ return decFloatAdd(result, dfl, &temp, set); /* and add to the lhs */
3331+ } /* decFloatSubtract */
3332+
3333+/* ------------------------------------------------------------------ */
3334+/* decFloatToInt -- round to 32-bit binary integer (4 flavours) */
3335+/* */
3336+/* df is the decFloat to round */
3337+/* set is the context */
3338+/* round is the rounding mode to use */
3339+/* returns a uInt or an Int, rounded according to the name */
3340+/* */
3341+/* Invalid will always be signaled if df is a NaN, is Infinite, or is */
3342+/* outside the range of the target; Inexact will not be signaled for */
3343+/* simple rounding unless 'Exact' appears in the name. */
3344+/* ------------------------------------------------------------------ */
3345+uInt decFloatToUInt32(const decFloat *df, decContext *set,
3346+ enum rounding round) {
3347+ return decToInt32(df, set, round, 0, 1);}
3348+
3349+uInt decFloatToUInt32Exact(const decFloat *df, decContext *set,
3350+ enum rounding round) {
3351+ return decToInt32(df, set, round, 1, 1);}
3352+
3353+Int decFloatToInt32(const decFloat *df, decContext *set,
3354+ enum rounding round) {
3355+ return (Int)decToInt32(df, set, round, 0, 0);}
3356+
3357+Int decFloatToInt32Exact(const decFloat *df, decContext *set,
3358+ enum rounding round) {
3359+ return (Int)decToInt32(df, set, round, 1, 0);}
3360+
3361+/* ------------------------------------------------------------------ */
3362+/* decFloatToIntegral -- round to integral value (two flavours) */
3363+/* */
3364+/* result gets the result */
3365+/* df is the decFloat to round */
3366+/* set is the context */
3367+/* round is the rounding mode to use */
3368+/* returns result */
3369+/* */
3370+/* No exceptions, even Inexact, are raised except for sNaN input, or */
3371+/* if 'Exact' appears in the name. */
3372+/* ------------------------------------------------------------------ */
3373+decFloat * decFloatToIntegralValue(decFloat *result, const decFloat *df,
3374+ decContext *set, enum rounding round) {
3375+ return decToIntegral(result, df, set, round, 0);}
3376+
3377+decFloat * decFloatToIntegralExact(decFloat *result, const decFloat *df,
3378+ decContext *set) {
3379+ return decToIntegral(result, df, set, set->round, 1);}
3380+
3381+/* ------------------------------------------------------------------ */
3382+/* decFloatXor -- logical digitwise XOR of two decFloats */
3383+/* */
3384+/* result gets the result of XORing dfl and dfr */
3385+/* dfl is the first decFloat (lhs) */
3386+/* dfr is the second decFloat (rhs) */
3387+/* set is the context */
3388+/* returns result, which will be canonical with sign=0 */
3389+/* */
3390+/* The operands must be positive, finite with exponent q=0, and */
3391+/* comprise just zeros and ones; if not, Invalid operation results. */
3392+/* ------------------------------------------------------------------ */
3393+decFloat * decFloatXor(decFloat *result,
3394+ const decFloat *dfl, const decFloat *dfr,
3395+ decContext *set) {
3396+ if (!DFISUINT01(dfl) || !DFISUINT01(dfr)
3397+ || !DFISCC01(dfl) || !DFISCC01(dfr)) return decInvalid(result, set);
3398+ /* the operands are positive finite integers (q=0) with just 0s and 1s */
3399+ #if DOUBLE
3400+ DFWORD(result, 0)=ZEROWORD
3401+ |((DFWORD(dfl, 0) ^ DFWORD(dfr, 0))&0x04009124);
3402+ DFWORD(result, 1)=(DFWORD(dfl, 1) ^ DFWORD(dfr, 1))&0x49124491;
3403+ #elif QUAD
3404+ DFWORD(result, 0)=ZEROWORD
3405+ |((DFWORD(dfl, 0) ^ DFWORD(dfr, 0))&0x04000912);
3406+ DFWORD(result, 1)=(DFWORD(dfl, 1) ^ DFWORD(dfr, 1))&0x44912449;
3407+ DFWORD(result, 2)=(DFWORD(dfl, 2) ^ DFWORD(dfr, 2))&0x12449124;
3408+ DFWORD(result, 3)=(DFWORD(dfl, 3) ^ DFWORD(dfr, 3))&0x49124491;
3409+ #endif
3410+ return result;
3411+ } /* decFloatXor */
3412+
3413+/* ------------------------------------------------------------------ */
3414+/* decInvalid -- set Invalid_operation result */
3415+/* */
3416+/* result gets a canonical NaN */
3417+/* set is the context */
3418+/* returns result */
3419+/* */
3420+/* status has Invalid_operation added */
3421+/* ------------------------------------------------------------------ */
3422+static decFloat *decInvalid(decFloat *result, decContext *set) {
3423+ decFloatZero(result);
3424+ DFWORD(result, 0)=DECFLOAT_qNaN;
3425+ set->status|=DEC_Invalid_operation;
3426+ return result;
3427+ } /* decInvalid */
3428+
3429+/* ------------------------------------------------------------------ */
3430+/* decInfinity -- set canonical Infinity with sign from a decFloat */
3431+/* */
3432+/* result gets a canonical Infinity */
3433+/* df is source decFloat (only the sign is used) */
3434+/* returns result */
3435+/* */
3436+/* df may be the same as result */
3437+/* ------------------------------------------------------------------ */
3438+static decFloat *decInfinity(decFloat *result, const decFloat *df) {
3439+ uInt sign=DFWORD(df, 0); /* save source signword */
3440+ decFloatZero(result); /* clear everything */
3441+ DFWORD(result, 0)=DECFLOAT_Inf | (sign & DECFLOAT_Sign);
3442+ return result;
3443+ } /* decInfinity */
3444+
3445+/* ------------------------------------------------------------------ */
3446+/* decNaNs -- handle NaN argument(s) */
3447+/* */
3448+/* result gets the result of handling dfl and dfr, one or both of */
3449+/* which is a NaN */
3450+/* dfl is the first decFloat (lhs) */
3451+/* dfr is the second decFloat (rhs) -- may be NULL for a single- */
3452+/* operand operation */
3453+/* set is the context */
3454+/* returns result */
3455+/* */
3456+/* Called when one or both operands is a NaN, and propagates the */
3457+/* appropriate result to res. When an sNaN is found, it is changed */
3458+/* to a qNaN and Invalid operation is set. */
3459+/* ------------------------------------------------------------------ */
3460+static decFloat *decNaNs(decFloat *result,
3461+ const decFloat *dfl, const decFloat *dfr,
3462+ decContext *set) {
3463+ /* handle sNaNs first */
3464+ if (dfr!=NULL && DFISSNAN(dfr) && !DFISSNAN(dfl)) dfl=dfr; /* use RHS */
3465+ if (DFISSNAN(dfl)) {
3466+ decCanonical(result, dfl); /* propagate canonical sNaN */
3467+ DFWORD(result, 0)&=~(DECFLOAT_qNaN ^ DECFLOAT_sNaN); /* quiet */
3468+ set->status|=DEC_Invalid_operation;
3469+ return result;
3470+ }
3471+ /* one or both is a quiet NaN */
3472+ if (!DFISNAN(dfl)) dfl=dfr; /* RHS must be NaN, use it */
3473+ return decCanonical(result, dfl); /* propagate canonical qNaN */
3474+ } /* decNaNs */
3475+
3476+/* ------------------------------------------------------------------ */
3477+/* decNumCompare -- numeric comparison of two decFloats */
3478+/* */
3479+/* dfl is the left-hand decFloat, which is not a NaN */
3480+/* dfr is the right-hand decFloat, which is not a NaN */
3481+/* tot is 1 for total order compare, 0 for simple numeric */
3482+/* returns -1, 0, or +1 for dfl<dfr, dfl=dfr, dfl>dfr */
3483+/* */
3484+/* No error is possible; status and mode are unchanged. */
3485+/* ------------------------------------------------------------------ */
3486+static Int decNumCompare(const decFloat *dfl, const decFloat *dfr, Flag tot) {
3487+ Int sigl, sigr; /* LHS and RHS non-0 signums */
3488+ Int shift; /* shift needed to align operands */
3489+ uByte *ub, *uc; /* work */
3490+ /* buffers +2 if Quad (36 digits), need double plus 4 for safe padding */
3491+ uByte bufl[DECPMAX*2+QUAD*2+4]; /* for LHS coefficient + padding */
3492+ uByte bufr[DECPMAX*2+QUAD*2+4]; /* for RHS coefficient + padding */
3493+
3494+ sigl=1;
3495+ if (DFISSIGNED(dfl)) {
3496+ if (!DFISSIGNED(dfr)) { /* -LHS +RHS */
3497+ if (DFISZERO(dfl) && DFISZERO(dfr) && !tot) return 0;
3498+ return -1; /* RHS wins */
3499+ }
3500+ sigl=-1;
3501+ }
3502+ if (DFISSIGNED(dfr)) {
3503+ if (!DFISSIGNED(dfl)) { /* +LHS -RHS */
3504+ if (DFISZERO(dfl) && DFISZERO(dfr) && !tot) return 0;
3505+ return +1; /* LHS wins */
3506+ }
3507+ }
3508+
3509+ /* signs are the same; operand(s) could be zero */
3510+ sigr=-sigl; /* sign to return if abs(RHS) wins */
3511+
3512+ if (DFISINF(dfl)) {
3513+ if (DFISINF(dfr)) return 0; /* both infinite & same sign */
3514+ return sigl; /* inf > n */
3515+ }
3516+ if (DFISINF(dfr)) return sigr; /* n < inf [dfl is finite] */
3517+
3518+ /* here, both are same sign and finite; calculate their offset */
3519+ shift=GETEXP(dfl)-GETEXP(dfr); /* [0 means aligned] */
3520+ /* [bias can be ignored -- the absolute exponent is not relevant] */
3521+
3522+ if (DFISZERO(dfl)) {
3523+ if (!DFISZERO(dfr)) return sigr; /* LHS=0, RHS!=0 */
3524+ /* both are zero, return 0 if both same exponent or numeric compare */
3525+ if (shift==0 || !tot) return 0;
3526+ if (shift>0) return sigl;
3527+ return sigr; /* [shift<0] */
3528+ }
3529+ else { /* LHS!=0 */
3530+ if (DFISZERO(dfr)) return sigl; /* LHS!=0, RHS=0 */
3531+ }
3532+ /* both are known to be non-zero at this point */
3533+
3534+ /* if the exponents are so different that the coefficients do not */
3535+ /* overlap (by even one digit) then a full comparison is not needed */
3536+ if (abs(shift)>=DECPMAX) { /* no overlap */
3537+ /* coefficients are known to be non-zero */
3538+ if (shift>0) return sigl;
3539+ return sigr; /* [shift<0] */
3540+ }
3541+
3542+ /* decode the coefficients */
3543+ /* (shift both right two if Quad to make a multiple of four) */
3544+ #if QUAD
3545+ UINTAT(bufl)=0;
3546+ UINTAT(bufr)=0;
3547+ #endif
3548+ GETCOEFF(dfl, bufl+QUAD*2); /* decode from decFloat */
3549+ GETCOEFF(dfr, bufr+QUAD*2); /* .. */
3550+ if (shift==0) { /* aligned; common and easy */
3551+ /* all multiples of four, here */
3552+ for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) {
3553+ if (UINTAT(ub)==UINTAT(uc)) continue; /* so far so same */
3554+ /* about to find a winner; go by bytes in case little-endian */
3555+ for (;; ub++, uc++) {
3556+ if (*ub>*uc) return sigl; /* difference found */
3557+ if (*ub<*uc) return sigr; /* .. */
3558+ }
3559+ }
3560+ } /* aligned */
3561+ else if (shift>0) { /* lhs to left */
3562+ ub=bufl; /* RHS pointer */
3563+ /* pad bufl so right-aligned; most shifts will fit in 8 */
3564+ UINTAT(bufl+DECPMAX+QUAD*2)=0; /* add eight zeros */
3565+ UINTAT(bufl+DECPMAX+QUAD*2+4)=0; /* .. */
3566+ if (shift>8) {
3567+ /* more than eight; fill the rest, and also worth doing the */
3568+ /* lead-in by fours */
3569+ uByte *up; /* work */
3570+ uByte *upend=bufl+DECPMAX+QUAD*2+shift;
3571+ for (up=bufl+DECPMAX+QUAD*2+8; up<upend; up+=4) UINTAT(up)=0;
3572+ /* [pads up to 36 in all for Quad] */
3573+ for (;; ub+=4) {
3574+ if (UINTAT(ub)!=0) return sigl;
3575+ if (ub+4>bufl+shift-4) break;
3576+ }
3577+ }
3578+ /* check remaining leading digits */
3579+ for (; ub<bufl+shift; ub++) if (*ub!=0) return sigl;
3580+ /* now start the overlapped part; bufl has been padded, so the */
3581+ /* comparison can go for the full length of bufr, which is a */
3582+ /* multiple of 4 bytes */
3583+ for (uc=bufr; ; uc+=4, ub+=4) {
3584+ if (UINTAT(uc)!=UINTAT(ub)) { /* mismatch found */
3585+ for (;; uc++, ub++) { /* check from left [little-endian?] */
3586+ if (*ub>*uc) return sigl; /* difference found */
3587+ if (*ub<*uc) return sigr; /* .. */
3588+ }
3589+ } /* mismatch */
3590+ if (uc==bufr+QUAD*2+DECPMAX-4) break; /* all checked */
3591+ }
3592+ } /* shift>0 */
3593+
3594+ else { /* shift<0) .. RHS is to left of LHS; mirror shift>0 */
3595+ uc=bufr; /* RHS pointer */
3596+ /* pad bufr so right-aligned; most shifts will fit in 8 */
3597+ UINTAT(bufr+DECPMAX+QUAD*2)=0; /* add eight zeros */
3598+ UINTAT(bufr+DECPMAX+QUAD*2+4)=0; /* .. */
3599+ if (shift<-8) {
3600+ /* more than eight; fill the rest, and also worth doing the */
3601+ /* lead-in by fours */
3602+ uByte *up; /* work */
3603+ uByte *upend=bufr+DECPMAX+QUAD*2-shift;
3604+ for (up=bufr+DECPMAX+QUAD*2+8; up<upend; up+=4) UINTAT(up)=0;
3605+ /* [pads up to 36 in all for Quad] */
3606+ for (;; uc+=4) {
3607+ if (UINTAT(uc)!=0) return sigr;
3608+ if (uc+4>bufr-shift-4) break;
3609+ }
3610+ }
3611+ /* check remaining leading digits */
3612+ for (; uc<bufr-shift; uc++) if (*uc!=0) return sigr;
3613+ /* now start the overlapped part; bufr has been padded, so the */
3614+ /* comparison can go for the full length of bufl, which is a */
3615+ /* multiple of 4 bytes */
3616+ for (ub=bufl; ; ub+=4, uc+=4) {
3617+ if (UINTAT(ub)!=UINTAT(uc)) { /* mismatch found */
3618+ for (;; ub++, uc++) { /* check from left [little-endian?] */
3619+ if (*ub>*uc) return sigl; /* difference found */
3620+ if (*ub<*uc) return sigr; /* .. */
3621+ }
3622+ } /* mismatch */
3623+ if (ub==bufl+QUAD*2+DECPMAX-4) break; /* all checked */
3624+ }
3625+ } /* shift<0 */
3626+
3627+ /* Here when compare equal */
3628+ if (!tot) return 0; /* numerically equal */
3629+ /* total ordering .. exponent matters */
3630+ if (shift>0) return sigl; /* total order by exponent */
3631+ if (shift<0) return sigr; /* .. */
3632+ return 0;
3633+ } /* decNumCompare */
3634+
3635+/* ------------------------------------------------------------------ */
3636+/* decToInt32 -- local routine to effect ToInteger conversions */
3637+/* */
3638+/* df is the decFloat to convert */
3639+/* set is the context */
3640+/* rmode is the rounding mode to use */
3641+/* exact is 1 if Inexact should be signalled */
3642+/* unsign is 1 if the result a uInt, 0 if an Int (cast to uInt) */
3643+/* returns 32-bit result as a uInt */
3644+/* */
3645+/* Invalid is set is df is a NaN, is infinite, or is out-of-range; in */
3646+/* these cases 0 is returned. */
3647+/* ------------------------------------------------------------------ */
3648+static uInt decToInt32(const decFloat *df, decContext *set,
3649+ enum rounding rmode, Flag exact, Flag unsign) {
3650+ Int exp; /* exponent */
3651+ uInt sourhi, sourpen, sourlo; /* top word from source decFloat .. */
3652+ uInt hi, lo; /* .. penultimate, least, etc. */
3653+ decFloat zero, result; /* work */
3654+ Int i; /* .. */
3655+
3656+ /* Start decoding the argument */
3657+ sourhi=DFWORD(df, 0); /* top word */
3658+ exp=DECCOMBEXP[sourhi>>26]; /* get exponent high bits (in place) */
3659+ if (EXPISSPECIAL(exp)) { /* is special? */
3660+ set->status|=DEC_Invalid_operation; /* signal */
3661+ return 0;
3662+ }
3663+
3664+ /* Here when the argument is finite */
3665+ if (GETEXPUN(df)==0) result=*df; /* already a true integer */
3666+ else { /* need to round to integer */
3667+ enum rounding saveround; /* saver */
3668+ uInt savestatus; /* .. */
3669+ saveround=set->round; /* save rounding mode .. */
3670+ savestatus=set->status; /* .. and status */
3671+ set->round=rmode; /* set mode */
3672+ decFloatZero(&zero); /* make 0E+0 */
3673+ set->status=0; /* clear */
3674+ decFloatQuantize(&result, df, &zero, set); /* [this may fail] */
3675+ set->round=saveround; /* restore rounding mode .. */
3676+ if (exact) set->status|=savestatus; /* include Inexact */
3677+ else set->status=savestatus; /* .. or just original status */
3678+ }
3679+
3680+ /* only the last four declets of the coefficient can contain */
3681+ /* non-zero; check for others (and also NaN or Infinity from the */
3682+ /* Quantize) first (see DFISZERO for explanation): */
3683+ /* decFloatShow(&result, "sofar"); */
3684+ #if DOUBLE
3685+ if ((DFWORD(&result, 0)&0x1c03ff00)!=0
3686+ || (DFWORD(&result, 0)&0x60000000)==0x60000000) {
3687+ #elif QUAD
3688+ if ((DFWORD(&result, 2)&0xffffff00)!=0
3689+ || DFWORD(&result, 1)!=0
3690+ || (DFWORD(&result, 0)&0x1c003fff)!=0
3691+ || (DFWORD(&result, 0)&0x60000000)==0x60000000) {
3692+ #endif
3693+ set->status|=DEC_Invalid_operation; /* Invalid or out of range */
3694+ return 0;
3695+ }
3696+ /* get last twelve digits of the coefficent into hi & ho, base */
3697+ /* 10**9 (see GETCOEFFBILL): */
3698+ sourlo=DFWORD(&result, DECWORDS-1);
3699+ lo=DPD2BIN0[sourlo&0x3ff]
3700+ +DPD2BINK[(sourlo>>10)&0x3ff]
3701+ +DPD2BINM[(sourlo>>20)&0x3ff];
3702+ sourpen=DFWORD(&result, DECWORDS-2);
3703+ hi=DPD2BIN0[((sourpen<<2) | (sourlo>>30))&0x3ff];
3704+
3705+ /* according to request, check range carefully */
3706+ if (unsign) {
3707+ if (hi>4 || (hi==4 && lo>294967295) || (hi+lo!=0 && DFISSIGNED(&result))) {
3708+ set->status|=DEC_Invalid_operation; /* out of range */
3709+ return 0;
3710+ }
3711+ return hi*BILLION+lo;
3712+ }
3713+ /* signed */
3714+ if (hi>2 || (hi==2 && lo>147483647)) {
3715+ /* handle the usual edge case */
3716+ if (lo==147483648 && hi==2 && DFISSIGNED(&result)) return 0x80000000;
3717+ set->status|=DEC_Invalid_operation; /* truly out of range */
3718+ return 0;
3719+ }
3720+ i=hi*BILLION+lo;
3721+ if (DFISSIGNED(&result)) i=-i;
3722+ return (uInt)i;
3723+ } /* decToInt32 */
3724+
3725+/* ------------------------------------------------------------------ */
3726+/* decToIntegral -- local routine to effect ToIntegral value */
3727+/* */
3728+/* result gets the result */
3729+/* df is the decFloat to round */
3730+/* set is the context */
3731+/* rmode is the rounding mode to use */
3732+/* exact is 1 if Inexact should be signalled */
3733+/* returns result */
3734+/* ------------------------------------------------------------------ */
3735+static decFloat * decToIntegral(decFloat *result, const decFloat *df,
3736+ decContext *set, enum rounding rmode,
3737+ Flag exact) {
3738+ Int exp; /* exponent */
3739+ uInt sourhi; /* top word from source decFloat */
3740+ enum rounding saveround; /* saver */
3741+ uInt savestatus; /* .. */
3742+ decFloat zero; /* work */
3743+
3744+ /* Start decoding the argument */
3745+ sourhi=DFWORD(df, 0); /* top word */
3746+ exp=DECCOMBEXP[sourhi>>26]; /* get exponent high bits (in place) */
3747+
3748+ if (EXPISSPECIAL(exp)) { /* is special? */
3749+ /* NaNs are handled as usual */
3750+ if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
3751+ /* must be infinite; return canonical infinity with sign of df */
3752+ return decInfinity(result, df);
3753+ }
3754+
3755+ /* Here when the argument is finite */
3756+ /* complete extraction of the exponent */
3757+ exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */
3758+
3759+ if (exp>=0) return decCanonical(result, df); /* already integral */
3760+
3761+ saveround=set->round; /* save rounding mode .. */
3762+ savestatus=set->status; /* .. and status */
3763+ set->round=rmode; /* set mode */
3764+ decFloatZero(&zero); /* make 0E+0 */
3765+ decFloatQuantize(result, df, &zero, set); /* 'integrate'; cannot fail */
3766+ set->round=saveround; /* restore rounding mode .. */
3767+ if (!exact) set->status=savestatus; /* .. and status, unless exact */
3768+ return result;
3769+ } /* decToIntegral */
--- /dev/null
+++ b/libdecnumber/decCommon.c
@@ -0,0 +1,1771 @@
1+/* Common code for fixed-size types in the decNumber C Library.
2+ Copyright (C) 2007 Free Software Foundation, Inc.
3+ Contributed by IBM Corporation. Author Mike Cowlishaw.
4+
5+ This file is part of GCC.
6+
7+ GCC is free software; you can redistribute it and/or modify it under
8+ the terms of the GNU General Public License as published by the Free
9+ Software Foundation; either version 2, or (at your option) any later
10+ version.
11+
12+ In addition to the permissions in the GNU General Public License,
13+ the Free Software Foundation gives you unlimited permission to link
14+ the compiled version of this file into combinations with other
15+ programs, and to distribute those combinations without any
16+ restriction coming from the use of this file. (The General Public
17+ License restrictions do apply in other respects; for example, they
18+ cover modification of the file, and distribution when not linked
19+ into a combine executable.)
20+
21+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
23+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24+ for more details.
25+
26+ You should have received a copy of the GNU General Public License
27+ along with GCC; see the file COPYING. If not, write to the Free
28+ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29+ 02110-1301, USA. */
30+
31+/* ------------------------------------------------------------------ */
32+/* decCommon.c -- common code for all three fixed-size types */
33+/* ------------------------------------------------------------------ */
34+/* This module comprises code that is shared between all the formats */
35+/* (decSingle, decDouble, and decQuad); it includes set and extract */
36+/* of format components, widening, narrowing, and string conversions. */
37+/* */
38+/* Unlike decNumber, parameterization takes place at compile time */
39+/* rather than at runtime. The parameters are set in the decDouble.c */
40+/* (etc.) files, which then include this one to produce the compiled */
41+/* code. The functions here, therefore, are code shared between */
42+/* multiple formats. */
43+/* ------------------------------------------------------------------ */
44+/* Names here refer to decFloat rather than to decDouble, etc., and */
45+/* the functions are in strict alphabetical order. */
46+/* Constants, tables, and debug function(s) are included only for QUAD */
47+/* (which will always be compiled if DOUBLE or SINGLE are used). */
48+/* */
49+/* Whenever a decContext is used, only the status may be set (using */
50+/* OR) or the rounding mode read; all other fields are ignored and */
51+/* untouched. */
52+
53+/* names for simpler testing and default context */
54+#if DECPMAX==7
55+ #define SINGLE 1
56+ #define DOUBLE 0
57+ #define QUAD 0
58+ #define DEFCONTEXT DEC_INIT_DECIMAL32
59+#elif DECPMAX==16
60+ #define SINGLE 0
61+ #define DOUBLE 1
62+ #define QUAD 0
63+ #define DEFCONTEXT DEC_INIT_DECIMAL64
64+#elif DECPMAX==34
65+ #define SINGLE 0
66+ #define DOUBLE 0
67+ #define QUAD 1
68+ #define DEFCONTEXT DEC_INIT_DECIMAL128
69+#else
70+ #error Unexpected DECPMAX value
71+#endif
72+
73+/* Assertions */
74+
75+#if DECPMAX!=7 && DECPMAX!=16 && DECPMAX!=34
76+ #error Unexpected Pmax (DECPMAX) value for this module
77+#endif
78+
79+/* Assert facts about digit characters, etc. */
80+#if ('9'&0x0f)!=9
81+ #error This module assumes characters are of the form 0b....nnnn
82+ /* where .... are don't care 4 bits and nnnn is 0000 through 1001 */
83+#endif
84+#if ('9'&0xf0)==('.'&0xf0)
85+ #error This module assumes '.' has a different mask than a digit
86+#endif
87+
88+/* Assert ToString lay-out conditions */
89+#if DECSTRING<DECPMAX+9
90+ #error ToString needs at least 8 characters for lead-in and dot
91+#endif
92+#if DECPMAX+DECEMAXD+5 > DECSTRING
93+ #error Exponent form can be too long for ToString to lay out safely
94+#endif
95+#if DECEMAXD > 4
96+ #error Exponent form is too long for ToString to lay out
97+ /* Note: code for up to 9 digits exists in archives [decOct] */
98+#endif
99+
100+/* Private functions used here and possibly in decBasic.c, etc. */
101+static decFloat * decFinalize(decFloat *, bcdnum *, decContext *);
102+static Flag decBiStr(const char *, const char *, const char *);
103+
104+/* Macros and private tables; those which are not format-dependent */
105+/* are only included if decQuad is being built. */
106+
107+/* ------------------------------------------------------------------ */
108+/* Combination field lookup tables (uInts to save measurable work) */
109+/* */
110+/* DECCOMBEXP - 2 most-significant-bits of exponent (00, 01, or */
111+/* 10), shifted left for format, or DECFLOAT_Inf/NaN */
112+/* DECCOMBWEXP - The same, for the next-wider format (unless QUAD) */
113+/* DECCOMBMSD - 4-bit most-significant-digit */
114+/* [0 if the index is a special (Infinity or NaN)] */
115+/* DECCOMBFROM - 5-bit combination field from EXP top bits and MSD */
116+/* (placed in uInt so no shift is needed) */
117+/* */
118+/* DECCOMBEXP, DECCOMBWEXP, and DECCOMBMSD are indexed by the sign */
119+/* and 5-bit combination field (0-63, the second half of the table */
120+/* identical to the first half) */
121+/* DECCOMBFROM is indexed by expTopTwoBits*16 + msd */
122+/* */
123+/* DECCOMBMSD and DECCOMBFROM are not format-dependent and so are */
124+/* only included once, when QUAD is being built */
125+/* ------------------------------------------------------------------ */
126+static const uInt DECCOMBEXP[64]={
127+ 0, 0, 0, 0, 0, 0, 0, 0,
128+ 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
129+ 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
130+ 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
131+ 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
132+ 0, 0, 1<<DECECONL, 1<<DECECONL,
133+ 2<<DECECONL, 2<<DECECONL, DECFLOAT_Inf, DECFLOAT_NaN,
134+ 0, 0, 0, 0, 0, 0, 0, 0,
135+ 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
136+ 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL,
137+ 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
138+ 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL,
139+ 0, 0, 1<<DECECONL, 1<<DECECONL,
140+ 2<<DECECONL, 2<<DECECONL, DECFLOAT_Inf, DECFLOAT_NaN};
141+#if !QUAD
142+static const uInt DECCOMBWEXP[64]={
143+ 0, 0, 0, 0, 0, 0, 0, 0,
144+ 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
145+ 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
146+ 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
147+ 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
148+ 0, 0, 1<<DECWECONL, 1<<DECWECONL,
149+ 2<<DECWECONL, 2<<DECWECONL, DECFLOAT_Inf, DECFLOAT_NaN,
150+ 0, 0, 0, 0, 0, 0, 0, 0,
151+ 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
152+ 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL,
153+ 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
154+ 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL,
155+ 0, 0, 1<<DECWECONL, 1<<DECWECONL,
156+ 2<<DECWECONL, 2<<DECWECONL, DECFLOAT_Inf, DECFLOAT_NaN};
157+#endif
158+
159+#if QUAD
160+const uInt DECCOMBMSD[64]={
161+ 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
162+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 1,
163+ 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
164+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 0};
165+
166+const uInt DECCOMBFROM[48]={
167+ 0x00000000, 0x04000000, 0x08000000, 0x0C000000, 0x10000000, 0x14000000,
168+ 0x18000000, 0x1C000000, 0x60000000, 0x64000000, 0x00000000, 0x00000000,
169+ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x20000000, 0x24000000,
170+ 0x28000000, 0x2C000000, 0x30000000, 0x34000000, 0x38000000, 0x3C000000,
171+ 0x68000000, 0x6C000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
172+ 0x00000000, 0x00000000, 0x40000000, 0x44000000, 0x48000000, 0x4C000000,
173+ 0x50000000, 0x54000000, 0x58000000, 0x5C000000, 0x70000000, 0x74000000,
174+ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000};
175+
176+/* ------------------------------------------------------------------ */
177+/* Request and include the tables to use for conversions */
178+/* ------------------------------------------------------------------ */
179+#define DEC_BCD2DPD 1 /* 0-0x999 -> DPD */
180+#define DEC_BIN2DPD 1 /* 0-999 -> DPD */
181+#define DEC_BIN2BCD8 1 /* 0-999 -> ddd, len */
182+#define DEC_DPD2BCD8 1 /* DPD -> ddd, len */
183+#define DEC_DPD2BIN 1 /* DPD -> 0-999 */
184+#define DEC_DPD2BINK 1 /* DPD -> 0-999000 */
185+#define DEC_DPD2BINM 1 /* DPD -> 0-999000000 */
186+#include "decDPD.h" /* source of the lookup tables */
187+
188+#endif
189+
190+/* ----------------------------------------------------------------- */
191+/* decBiStr -- compare string with pairwise options */
192+/* */
193+/* targ is the string to compare */
194+/* str1 is one of the strings to compare against (length may be 0) */
195+/* str2 is the other; it must be the same length as str1 */
196+/* */
197+/* returns 1 if strings compare equal, (that is, targ is the same */
198+/* length as str1 and str2, and each character of targ is in one */
199+/* of str1 or str2 in the corresponding position), or 0 otherwise */
200+/* */
201+/* This is used for generic caseless compare, including the awkward */
202+/* case of the Turkish dotted and dotless Is. Use as (for example): */
203+/* if (decBiStr(test, "mike", "MIKE")) ... */
204+/* ----------------------------------------------------------------- */
205+static Flag decBiStr(const char *targ, const char *str1, const char *str2) {
206+ for (;;targ++, str1++, str2++) {
207+ if (*targ!=*str1 && *targ!=*str2) return 0;
208+ /* *targ has a match in one (or both, if terminator) */
209+ if (*targ=='\0') break;
210+ } /* forever */
211+ return 1;
212+ } /* decBiStr */
213+
214+/* ------------------------------------------------------------------ */
215+/* decFinalize -- adjust and store a final result */
216+/* */
217+/* df is the decFloat format number which gets the final result */
218+/* num is the descriptor of the number to be checked and encoded */
219+/* [its values, including the coefficient, may be modified] */
220+/* set is the context to use */
221+/* returns df */
222+/* */
223+/* The num descriptor may point to a bcd8 string of any length; this */
224+/* string may have leading insignificant zeros. If it has more than */
225+/* DECPMAX digits then the final digit can be a round-for-reround */
226+/* digit (i.e., it may include a sticky bit residue). */
227+/* */
228+/* The exponent (q) may be one of the codes for a special value and */
229+/* can be up to 999999999 for conversion from string. */
230+/* */
231+/* No error is possible, but Inexact, Underflow, and/or Overflow may */
232+/* be set. */
233+/* ------------------------------------------------------------------ */
234+/* Constant whose size varies with format; also the check for surprises */
235+static uByte allnines[DECPMAX]=
236+#if SINGLE
237+ {9, 9, 9, 9, 9, 9, 9};
238+#elif DOUBLE
239+ {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
240+#elif QUAD
241+ {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
242+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
243+#endif
244+
245+static decFloat * decFinalize(decFloat *df, bcdnum *num,
246+ decContext *set) {
247+ uByte *ub; /* work */
248+ uInt dpd; /* .. */
249+ uByte *umsd=num->msd; /* local copy */
250+ uByte *ulsd=num->lsd; /* .. */
251+ uInt encode; /* encoding accumulator */
252+ Int length; /* coefficient length */
253+
254+ #if DECCHECK
255+ Int clen=ulsd-umsd+1;
256+ #if QUAD
257+ #define COEXTRA 2 /* extra-long coefficent */
258+ #else
259+ #define COEXTRA 0
260+ #endif
261+ if (clen<1 || clen>DECPMAX*3+2+COEXTRA)
262+ printf("decFinalize: suspect coefficient [length=%ld]\n", (LI)clen);
263+ if (num->sign!=0 && num->sign!=DECFLOAT_Sign)
264+ printf("decFinalize: bad sign [%08lx]\n", (LI)num->sign);
265+ if (!EXPISSPECIAL(num->exponent)
266+ && (num->exponent>1999999999 || num->exponent<-1999999999))
267+ printf("decFinalize: improbable exponent [%ld]\n", (LI)num->exponent);
268+ /* decShowNum(num, "final"); */
269+ #endif
270+
271+ /* A special will have an 'exponent' which is very positive and a */
272+ /* coefficient < DECPMAX */
273+ length=(uInt)(ulsd-umsd+1); /* coefficient length */
274+
275+ if (!NUMISSPECIAL(num)) {
276+ Int drop; /* digits to be dropped */
277+ /* skip leading insignificant zeros to calculate an exact length */
278+ /* [this is quite expensive] */
279+ if (*umsd==0) {
280+ for (; UINTAT(umsd)==0 && umsd+3<ulsd;) umsd+=4;
281+ for (; *umsd==0 && umsd<ulsd;) umsd++;
282+ length=ulsd-umsd+1; /* recalculate */
283+ }
284+ drop=MAXI(length-DECPMAX, DECQTINY-num->exponent);
285+ /* drop can now be > digits for bottom-clamp (subnormal) cases */
286+ if (drop>0) { /* rounding needed */
287+ /* (decFloatQuantize has very similar code to this, so any */
288+ /* changes may need to be made there, too) */
289+ uByte *roundat; /* -> re-round digit */
290+ uByte reround; /* reround value */
291+ /* printf("Rounding; drop=%ld\n", (LI)drop); */
292+
293+ num->exponent+=drop; /* always update exponent */
294+
295+ /* Three cases here: */
296+ /* 1. new LSD is in coefficient (almost always) */
297+ /* 2. new LSD is digit to left of coefficient (so MSD is */
298+ /* round-for-reround digit) */
299+ /* 3. new LSD is to left of case 2 (whole coefficient is sticky) */
300+ /* [duplicate check-stickies code to save a test] */
301+ /* [by-digit check for stickies as runs of zeros are rare] */
302+ if (drop<length) { /* NB lengths not addresses */
303+ roundat=umsd+length-drop;
304+ reround=*roundat;
305+ for (ub=roundat+1; ub<=ulsd; ub++) {
306+ if (*ub!=0) { /* non-zero to be discarded */
307+ reround=DECSTICKYTAB[reround]; /* apply sticky bit */
308+ break; /* [remainder don't-care] */
309+ }
310+ } /* check stickies */
311+ ulsd=roundat-1; /* new LSD */
312+ }
313+ else { /* edge case */
314+ if (drop==length) {
315+ roundat=umsd;
316+ reround=*roundat;
317+ }
318+ else {
319+ roundat=umsd-1;
320+ reround=0;
321+ }
322+ for (ub=roundat+1; ub<=ulsd; ub++) {
323+ if (*ub!=0) { /* non-zero to be discarded */
324+ reround=DECSTICKYTAB[reround]; /* apply sticky bit */
325+ break; /* [remainder don't-care] */
326+ }
327+ } /* check stickies */
328+ *umsd=0; /* coefficient is a 0 */
329+ ulsd=umsd; /* .. */
330+ }
331+
332+ if (reround!=0) { /* discarding non-zero */
333+ uInt bump=0;
334+ set->status|=DEC_Inexact;
335+ /* if adjusted exponent [exp+digits-1] is < EMIN then num is */
336+ /* subnormal -- so raise Underflow */
337+ if (num->exponent<DECEMIN && (num->exponent+(ulsd-umsd+1)-1)<DECEMIN)
338+ set->status|=DEC_Underflow;
339+
340+ /* next decide whether increment of the coefficient is needed */
341+ if (set->round==DEC_ROUND_HALF_EVEN) { /* fastpath slowest case */
342+ if (reround>5) bump=1; /* >0.5 goes up */
343+ else if (reround==5) /* exactly 0.5000 .. */
344+ bump=*ulsd & 0x01; /* .. up iff [new] lsd is odd */
345+ } /* r-h-e */
346+ else switch (set->round) {
347+ case DEC_ROUND_DOWN: {
348+ /* no change */
349+ break;} /* r-d */
350+ case DEC_ROUND_HALF_DOWN: {
351+ if (reround>5) bump=1;
352+ break;} /* r-h-d */
353+ case DEC_ROUND_HALF_UP: {
354+ if (reround>=5) bump=1;
355+ break;} /* r-h-u */
356+ case DEC_ROUND_UP: {
357+ if (reround>0) bump=1;
358+ break;} /* r-u */
359+ case DEC_ROUND_CEILING: {
360+ /* same as _UP for positive numbers, and as _DOWN for negatives */
361+ if (!num->sign && reround>0) bump=1;
362+ break;} /* r-c */
363+ case DEC_ROUND_FLOOR: {
364+ /* same as _UP for negative numbers, and as _DOWN for positive */
365+ /* [negative reround cannot occur on 0] */
366+ if (num->sign && reround>0) bump=1;
367+ break;} /* r-f */
368+ case DEC_ROUND_05UP: {
369+ if (reround>0) { /* anything out there is 'sticky' */
370+ /* bump iff lsd=0 or 5; this cannot carry so it could be */
371+ /* effected immediately with no bump -- but the code */
372+ /* is clearer if this is done the same way as the others */
373+ if (*ulsd==0 || *ulsd==5) bump=1;
374+ }
375+ break;} /* r-r */
376+ default: { /* e.g., DEC_ROUND_MAX */
377+ set->status|=DEC_Invalid_context;
378+ #if DECCHECK
379+ printf("Unknown rounding mode: %ld\n", (LI)set->round);
380+ #endif
381+ break;}
382+ } /* switch (not r-h-e) */
383+ /* printf("ReRound: %ld bump: %ld\n", (LI)reround, (LI)bump); */
384+
385+ if (bump!=0) { /* need increment */
386+ /* increment the coefficient; this might end up with 1000... */
387+ /* (after the all nines case) */
388+ ub=ulsd;
389+ for(; ub-3>=umsd && UINTAT(ub-3)==0x09090909; ub-=4) UINTAT(ub-3)=0;
390+ /* [note ub could now be to left of msd, and it is not safe */
391+ /* to write to the the left of the msd] */
392+ /* now at most 3 digits left to non-9 (usually just the one) */
393+ for (; ub>=umsd; *ub=0, ub--) {
394+ if (*ub==9) continue; /* carry */
395+ *ub+=1;
396+ break;
397+ }
398+ if (ub<umsd) { /* had all-nines */
399+ *umsd=1; /* coefficient to 1000... */
400+ /* usually the 1000... coefficient can be used as-is */
401+ if ((ulsd-umsd+1)==DECPMAX) {
402+ num->exponent++;
403+ }
404+ else {
405+ /* if coefficient is shorter than Pmax then num is */
406+ /* subnormal, so extend it; this is safe as drop>0 */
407+ /* (or, if the coefficient was supplied above, it could */
408+ /* not be 9); this may make the result normal. */
409+ ulsd++;
410+ *ulsd=0;
411+ /* [exponent unchanged] */
412+ #if DECCHECK
413+ if (num->exponent!=DECQTINY) /* sanity check */
414+ printf("decFinalize: bad all-nines extend [^%ld, %ld]\n",
415+ (LI)num->exponent, (LI)(ulsd-umsd+1));
416+ #endif
417+ } /* subnormal extend */
418+ } /* had all-nines */
419+ } /* bump needed */
420+ } /* inexact rounding */
421+
422+ length=ulsd-umsd+1; /* recalculate (may be <DECPMAX) */
423+ } /* need round (drop>0) */
424+
425+ /* The coefficient will now fit and has final length unless overflow */
426+ /* decShowNum(num, "rounded"); */
427+
428+ /* if exponent is >=emax may have to clamp, overflow, or fold-down */
429+ if (num->exponent>DECEMAX-(DECPMAX-1)) { /* is edge case */
430+ /* printf("overflow checks...\n"); */
431+ if (*ulsd==0 && ulsd==umsd) { /* have zero */
432+ num->exponent=DECEMAX-(DECPMAX-1); /* clamp to max */
433+ }
434+ else if ((num->exponent+length-1)>DECEMAX) { /* > Nmax */
435+ /* Overflow -- these could go straight to encoding, here, but */
436+ /* instead num is adjusted to keep the code cleaner */
437+ Flag needmax=0; /* 1 for finite result */
438+ set->status|=(DEC_Overflow | DEC_Inexact);
439+ switch (set->round) {
440+ case DEC_ROUND_DOWN: {
441+ needmax=1; /* never Infinity */
442+ break;} /* r-d */
443+ case DEC_ROUND_05UP: {
444+ needmax=1; /* never Infinity */
445+ break;} /* r-05 */
446+ case DEC_ROUND_CEILING: {
447+ if (num->sign) needmax=1; /* Infinity iff non-negative */
448+ break;} /* r-c */
449+ case DEC_ROUND_FLOOR: {
450+ if (!num->sign) needmax=1; /* Infinity iff negative */
451+ break;} /* r-f */
452+ default: break; /* Infinity in all other cases */
453+ }
454+ if (!needmax) { /* easy .. set Infinity */
455+ num->exponent=DECFLOAT_Inf;
456+ *umsd=0; /* be clean: coefficient to 0 */
457+ ulsd=umsd; /* .. */
458+ }
459+ else { /* return Nmax */
460+ umsd=allnines; /* use constant array */
461+ ulsd=allnines+DECPMAX-1;
462+ num->exponent=DECEMAX-(DECPMAX-1);
463+ }
464+ }
465+ else { /* no overflow but non-zero and may have to fold-down */
466+ Int shift=num->exponent-(DECEMAX-(DECPMAX-1));
467+ if (shift>0) { /* fold-down needed */
468+ /* fold down needed; must copy to buffer in order to pad */
469+ /* with zeros safely; fortunately this is not the worst case */
470+ /* path because cannot have had a round */
471+ uByte buffer[ROUNDUP(DECPMAX+3, 4)]; /* [+3 allows uInt padding] */
472+ uByte *s=umsd; /* source */
473+ uByte *t=buffer; /* safe target */
474+ uByte *tlsd=buffer+(ulsd-umsd)+shift; /* target LSD */
475+ /* printf("folddown shift=%ld\n", (LI)shift); */
476+ for (; s<=ulsd; s+=4, t+=4) UINTAT(t)=UINTAT(s);
477+ for (t=tlsd-shift+1; t<=tlsd; t+=4) UINTAT(t)=0; /* pad */
478+ num->exponent-=shift;
479+ umsd=buffer;
480+ ulsd=tlsd;
481+ }
482+ } /* fold-down? */
483+ length=ulsd-umsd+1; /* recalculate length */
484+ } /* high-end edge case */
485+ } /* finite number */
486+
487+ /*------------------------------------------------------------------*/
488+ /* At this point the result will properly fit the decFloat */
489+ /* encoding, and it can be encoded with no possibility of error */
490+ /*------------------------------------------------------------------*/
491+ /* Following code does not alter coefficient (could be allnines array) */
492+
493+ if (length==DECPMAX) {
494+ return decFloatFromBCD(df, num->exponent, umsd, num->sign);
495+ }
496+
497+ /* Here when length is short */
498+ if (!NUMISSPECIAL(num)) { /* is still finite */
499+ /* encode the combination field and exponent continuation */
500+ uInt uexp=(uInt)(num->exponent+DECBIAS); /* biased exponent */
501+ uInt code=(uexp>>DECECONL)<<4; /* top two bits of exp */
502+ /* [msd=0] */
503+ /* look up the combination field and make high word */
504+ encode=DECCOMBFROM[code]; /* indexed by (0-2)*16+msd */
505+ encode|=(uexp<<(32-6-DECECONL)) & 0x03ffffff; /* exponent continuation */
506+ }
507+ else encode=num->exponent; /* special [already in word] */
508+ /* [coefficient length here will be < DECPMAX] */
509+
510+ encode|=num->sign; /* add sign */
511+
512+ /* private macro to extract a declet, n (where 0<=n<DECLETS and 0 */
513+ /* refers to the declet from the least significant three digits) */
514+ /* and put the corresponding DPD code into dpd. Access to umsd and */
515+ /* ulsd (pointers to the most and least significant digit of the */
516+ /* variable-length coefficient) is assumed, along with use of a */
517+ /* working pointer, uInt *ub. */
518+ /* As not full-length then chances are there are many leading zeros */
519+ /* [and there may be a partial triad] */
520+ #define getDPD(dpd, n) ub=ulsd-(3*(n))-2; \
521+ if (ub<umsd-2) dpd=0; \
522+ else if (ub>=umsd) dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)]; \
523+ else {dpd=*(ub+2); if (ub+1==umsd) dpd+=*(ub+1)*16; dpd=BCD2DPD[dpd];}
524+
525+ /* place the declets in the encoding words and copy to result (df), */
526+ /* according to endianness; in all cases complete the sign word */
527+ /* first */
528+ #if DECPMAX==7
529+ getDPD(dpd, 1);
530+ encode|=dpd<<10;
531+ getDPD(dpd, 0);
532+ encode|=dpd;
533+ DFWORD(df, 0)=encode; /* just the one word */
534+
535+ #elif DECPMAX==16
536+ getDPD(dpd, 4); encode|=dpd<<8;
537+ getDPD(dpd, 3); encode|=dpd>>2;
538+ DFWORD(df, 0)=encode;
539+ encode=dpd<<30;
540+ getDPD(dpd, 2); encode|=dpd<<20;
541+ getDPD(dpd, 1); encode|=dpd<<10;
542+ getDPD(dpd, 0); encode|=dpd;
543+ DFWORD(df, 1)=encode;
544+
545+ #elif DECPMAX==34
546+ getDPD(dpd,10); encode|=dpd<<4;
547+ getDPD(dpd, 9); encode|=dpd>>6;
548+ DFWORD(df, 0)=encode;
549+
550+ encode=dpd<<26;
551+ getDPD(dpd, 8); encode|=dpd<<16;
552+ getDPD(dpd, 7); encode|=dpd<<6;
553+ getDPD(dpd, 6); encode|=dpd>>4;
554+ DFWORD(df, 1)=encode;
555+
556+ encode=dpd<<28;
557+ getDPD(dpd, 5); encode|=dpd<<18;
558+ getDPD(dpd, 4); encode|=dpd<<8;
559+ getDPD(dpd, 3); encode|=dpd>>2;
560+ DFWORD(df, 2)=encode;
561+
562+ encode=dpd<<30;
563+ getDPD(dpd, 2); encode|=dpd<<20;
564+ getDPD(dpd, 1); encode|=dpd<<10;
565+ getDPD(dpd, 0); encode|=dpd;
566+ DFWORD(df, 3)=encode;
567+ #endif
568+
569+ /* printf("Status: %08lx\n", (LI)set->status); */
570+ /* decFloatShow(df, "final"); */
571+ return df;
572+ } /* decFinalize */
573+
574+/* ------------------------------------------------------------------ */
575+/* decFloatFromBCD -- set decFloat from exponent, BCD8, and sign */
576+/* */
577+/* df is the target decFloat */
578+/* exp is the in-range unbiased exponent, q, or a special value in */
579+/* the form returned by decFloatGetExponent */
580+/* bcdar holds DECPMAX digits to set the coefficient from, one */
581+/* digit in each byte (BCD8 encoding); the first (MSD) is ignored */
582+/* if df is a NaN; all are ignored if df is infinite. */
583+/* All bytes must be in 0-9; results undefined otherwise. */
584+/* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */
585+/* returns df, which will be canonical */
586+/* */
587+/* No error is possible, and no status will be set. */
588+/* ------------------------------------------------------------------ */
589+decFloat * decFloatFromBCD(decFloat *df, Int exp, const uByte *bcdar,
590+ Int sig) {
591+ uInt encode, dpd; /* work */
592+ const uByte *ub; /* .. */
593+
594+ if (EXPISSPECIAL(exp)) encode=exp|sig;/* specials already encoded */
595+ else { /* is finite */
596+ /* encode the combination field and exponent continuation */
597+ uInt uexp=(uInt)(exp+DECBIAS); /* biased exponent */
598+ uInt code=(uexp>>DECECONL)<<4; /* top two bits of exp */
599+ code+=bcdar[0]; /* add msd */
600+ /* look up the combination field and make high word */
601+ encode=DECCOMBFROM[code]|sig; /* indexed by (0-2)*16+msd */
602+ encode|=(uexp<<(32-6-DECECONL)) & 0x03ffffff; /* exponent continuation */
603+ }
604+
605+ /* private macro to extract a declet, n (where 0<=n<DECLETS and 0 */
606+ /* refers to the declet from the least significant three digits) */
607+ /* and put the corresponding DPD code into dpd. */
608+ /* Use of a working pointer, uInt *ub, is assumed. */
609+
610+ #define getDPDf(dpd, n) ub=bcdar+DECPMAX-1-(3*(n))-2; \
611+ dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)];
612+
613+ /* place the declets in the encoding words and copy to result (df), */
614+ /* according to endianness; in all cases complete the sign word */
615+ /* first */
616+ #if DECPMAX==7
617+ getDPDf(dpd, 1);
618+ encode|=dpd<<10;
619+ getDPDf(dpd, 0);
620+ encode|=dpd;
621+ DFWORD(df, 0)=encode; /* just the one word */
622+
623+ #elif DECPMAX==16
624+ getDPDf(dpd, 4); encode|=dpd<<8;
625+ getDPDf(dpd, 3); encode|=dpd>>2;
626+ DFWORD(df, 0)=encode;
627+ encode=dpd<<30;
628+ getDPDf(dpd, 2); encode|=dpd<<20;
629+ getDPDf(dpd, 1); encode|=dpd<<10;
630+ getDPDf(dpd, 0); encode|=dpd;
631+ DFWORD(df, 1)=encode;
632+
633+ #elif DECPMAX==34
634+ getDPDf(dpd,10); encode|=dpd<<4;
635+ getDPDf(dpd, 9); encode|=dpd>>6;
636+ DFWORD(df, 0)=encode;
637+
638+ encode=dpd<<26;
639+ getDPDf(dpd, 8); encode|=dpd<<16;
640+ getDPDf(dpd, 7); encode|=dpd<<6;
641+ getDPDf(dpd, 6); encode|=dpd>>4;
642+ DFWORD(df, 1)=encode;
643+
644+ encode=dpd<<28;
645+ getDPDf(dpd, 5); encode|=dpd<<18;
646+ getDPDf(dpd, 4); encode|=dpd<<8;
647+ getDPDf(dpd, 3); encode|=dpd>>2;
648+ DFWORD(df, 2)=encode;
649+
650+ encode=dpd<<30;
651+ getDPDf(dpd, 2); encode|=dpd<<20;
652+ getDPDf(dpd, 1); encode|=dpd<<10;
653+ getDPDf(dpd, 0); encode|=dpd;
654+ DFWORD(df, 3)=encode;
655+ #endif
656+ /* decFloatShow(df, "final"); */
657+ return df;
658+ } /* decFloatFromBCD */
659+
660+/* ------------------------------------------------------------------ */
661+/* decFloatFromPacked -- set decFloat from exponent and packed BCD */
662+/* */
663+/* df is the target decFloat */
664+/* exp is the in-range unbiased exponent, q, or a special value in */
665+/* the form returned by decFloatGetExponent */
666+/* packed holds DECPMAX packed decimal digits plus a sign nibble */
667+/* (all 6 codes are OK); the first (MSD) is ignored if df is a NaN */
668+/* and all except sign are ignored if df is infinite. For DOUBLE */
669+/* and QUAD the first (pad) nibble is also ignored in all cases. */
670+/* All coefficient nibbles must be in 0-9 and sign in A-F; results */
671+/* are undefined otherwise. */
672+/* returns df, which will be canonical */
673+/* */
674+/* No error is possible, and no status will be set. */
675+/* ------------------------------------------------------------------ */
676+decFloat * decFloatFromPacked(decFloat *df, Int exp, const uByte *packed) {
677+ uByte bcdar[DECPMAX+2]; /* work [+1 for pad, +1 for sign] */
678+ const uByte *ip; /* .. */
679+ uByte *op; /* .. */
680+ Int sig=0; /* sign */
681+
682+ /* expand coefficient and sign to BCDAR */
683+ #if SINGLE
684+ op=bcdar+1; /* no pad digit */
685+ #else
686+ op=bcdar; /* first (pad) digit ignored */
687+ #endif
688+ for (ip=packed; ip<packed+((DECPMAX+2)/2); ip++) {
689+ *op++=*ip>>4;
690+ *op++=(uByte)(*ip&0x0f); /* [final nibble is sign] */
691+ }
692+ op--; /* -> sign byte */
693+ if (*op==DECPMINUS || *op==DECPMINUSALT) sig=DECFLOAT_Sign;
694+
695+ if (EXPISSPECIAL(exp)) { /* Infinity or NaN */
696+ if (!EXPISINF(exp)) bcdar[1]=0; /* a NaN: ignore MSD */
697+ else memset(bcdar+1, 0, DECPMAX); /* Infinite: coefficient to 0 */
698+ }
699+ return decFloatFromBCD(df, exp, bcdar+1, sig);
700+ } /* decFloatFromPacked */
701+
702+/* ------------------------------------------------------------------ */
703+/* decFloatFromString -- conversion from numeric string */
704+/* */
705+/* result is the decFloat format number which gets the result of */
706+/* the conversion */
707+/* *string is the character string which should contain a valid */
708+/* number (which may be a special value), \0-terminated */
709+/* If there are too many significant digits in the */
710+/* coefficient it will be rounded. */
711+/* set is the context */
712+/* returns result */
713+/* */
714+/* The length of the coefficient and the size of the exponent are */
715+/* checked by this routine, so the correct error (Underflow or */
716+/* Overflow) can be reported or rounding applied, as necessary. */
717+/* */
718+/* There is no limit to the coefficient length for finite inputs; */
719+/* NaN payloads must be integers with no more than DECPMAX-1 digits. */
720+/* Exponents may have up to nine significant digits. */
721+/* */
722+/* If bad syntax is detected, the result will be a quiet NaN. */
723+/* ------------------------------------------------------------------ */
724+decFloat * decFloatFromString(decFloat *result, const char *string,
725+ decContext *set) {
726+ Int digits; /* count of digits in coefficient */
727+ const char *dotchar=NULL; /* where dot was found [NULL if none] */
728+ const char *cfirst=string; /* -> first character of decimal part */
729+ const char *c; /* work */
730+ uByte *ub; /* .. */
731+ bcdnum num; /* collects data for finishing */
732+ uInt error=DEC_Conversion_syntax; /* assume the worst */
733+ uByte buffer[ROUNDUP(DECSTRING+11, 8)]; /* room for most coefficents, */
734+ /* some common rounding, +3, & pad */
735+ #if DECTRACE
736+ /* printf("FromString %s ...\n", string); */
737+ #endif
738+
739+ for(;;) { /* once-only 'loop' */
740+ num.sign=0; /* assume non-negative */
741+ num.msd=buffer; /* MSD is here always */
742+
743+ /* detect and validate the coefficient, including any leading, */
744+ /* trailing, or embedded '.' */
745+ /* [could test four-at-a-time here (saving 10% for decQuads), */
746+ /* but that risks storage violation because the position of the */
747+ /* terminator is unknown] */
748+ for (c=string;; c++) { /* -> input character */
749+ if (((unsigned)(*c-'0'))<=9) continue; /* '0' through '9' is good */
750+ if (*c=='\0') break; /* most common non-digit */
751+ if (*c=='.') {
752+ if (dotchar!=NULL) break; /* not first '.' */
753+ dotchar=c; /* record offset into decimal part */
754+ continue;}
755+ if (c==string) { /* first in string... */
756+ if (*c=='-') { /* valid - sign */
757+ cfirst++;
758+ num.sign=DECFLOAT_Sign;
759+ continue;}
760+ if (*c=='+') { /* valid + sign */
761+ cfirst++;
762+ continue;}
763+ }
764+ /* *c is not a digit, terminator, or a valid +, -, or '.' */
765+ break;
766+ } /* c loop */
767+
768+ digits=(uInt)(c-cfirst); /* digits (+1 if a dot) */
769+
770+ if (digits>0) { /* had digits and/or dot */
771+ const char *clast=c-1; /* note last coefficient char position */
772+ Int exp=0; /* exponent accumulator */
773+ if (*c!='\0') { /* something follows the coefficient */
774+ uInt edig; /* unsigned work */
775+ /* had some digits and more to come; expect E[+|-]nnn now */
776+ const char *firstexp; /* exponent first non-zero */
777+ if (*c!='E' && *c!='e') break;
778+ c++; /* to (optional) sign */
779+ if (*c=='-' || *c=='+') c++; /* step over sign (c=clast+2) */
780+ if (*c=='\0') break; /* no digits! (e.g., '1.2E') */
781+ for (; *c=='0';) c++; /* skip leading zeros [even last] */
782+ firstexp=c; /* remember start [maybe '\0'] */
783+ /* gather exponent digits */
784+ edig=(uInt)*c-(uInt)'0';
785+ if (edig<=9) { /* [check not bad or terminator] */
786+ exp+=edig; /* avoid initial X10 */
787+ c++;
788+ for (;; c++) {
789+ edig=(uInt)*c-(uInt)'0';
790+ if (edig>9) break;
791+ exp=exp*10+edig;
792+ }
793+ }
794+ /* if not now on the '\0', *c must not be a digit */
795+ if (*c!='\0') break;
796+
797+ /* (this next test must be after the syntax checks) */
798+ /* if definitely more than the possible digits for format then */
799+ /* the exponent may have wrapped, so simply set it to a certain */
800+ /* over/underflow value */
801+ if (c>firstexp+DECEMAXD) exp=DECEMAX*2;
802+ if (*(clast+2)=='-') exp=-exp; /* was negative */
803+ } /* digits>0 */
804+
805+ if (dotchar!=NULL) { /* had a '.' */
806+ digits--; /* remove from digits count */
807+ if (digits==0) break; /* was dot alone: bad syntax */
808+ exp-=(Int)(clast-dotchar); /* adjust exponent */
809+ /* [the '.' can now be ignored] */
810+ }
811+ num.exponent=exp; /* exponent is good; store it */
812+
813+ /* Here when whole string has been inspected and syntax is good */
814+ /* cfirst->first digit or dot, clast->last digit or dot */
815+ error=0; /* no error possible now */
816+
817+ /* if the number of digits in the coefficient will fit in buffer */
818+ /* then it can simply be converted to bcd8 and copied -- decFinalize */
819+ /* will take care of leading zeros and rounding; the buffer is big */
820+ /* enough for all canonical coefficients, including 0.00000nn... */
821+ ub=buffer;
822+ if (digits<=(Int)(sizeof(buffer)-3)) { /* [-3 allows by-4s copy] */
823+ c=cfirst;
824+ if (dotchar!=NULL) { /* a dot to worry about */
825+ if (*(c+1)=='.') { /* common canonical case */
826+ *ub++=(uByte)(*c-'0'); /* copy leading digit */
827+ c+=2; /* prepare to handle rest */
828+ }
829+ else for (; c<=clast;) { /* '.' could be anywhere */
830+ /* as usual, go by fours when safe; NB it has been asserted */
831+ /* that a '.' does not have the same mask as a digit */
832+ if (c<=clast-3 /* safe for four */
833+ && (UINTAT(c)&0xf0f0f0f0)==CHARMASK) { /* test four */
834+ UINTAT(ub)=UINTAT(c)&0x0f0f0f0f; /* to BCD8 */
835+ ub+=4;
836+ c+=4;
837+ continue;
838+ }
839+ if (*c=='.') { /* found the dot */
840+ c++; /* step over it .. */
841+ break; /* .. and handle the rest */
842+ }
843+ *ub++=(uByte)(*c++-'0');
844+ }
845+ } /* had dot */
846+ /* Now no dot; do this by fours (where safe) */
847+ for (; c<=clast-3; c+=4, ub+=4) UINTAT(ub)=UINTAT(c)&0x0f0f0f0f;
848+ for (; c<=clast; c++, ub++) *ub=(uByte)(*c-'0');
849+ num.lsd=buffer+digits-1; /* record new LSD */
850+ } /* fits */
851+
852+ else { /* too long for buffer */
853+ /* [This is a rare and unusual case; arbitrary-length input] */
854+ /* strip leading zeros [but leave final 0 if all 0's] */
855+ if (*cfirst=='.') cfirst++; /* step past dot at start */
856+ if (*cfirst=='0') { /* [cfirst always -> digit] */
857+ for (; cfirst<clast; cfirst++) {
858+ if (*cfirst!='0') { /* non-zero found */
859+ if (*cfirst=='.') continue; /* [ignore] */
860+ break; /* done */
861+ }
862+ digits--; /* 0 stripped */
863+ } /* cfirst */
864+ } /* at least one leading 0 */
865+
866+ /* the coefficient is now as short as possible, but may still */
867+ /* be too long; copy up to Pmax+1 digits to the buffer, then */
868+ /* just record any non-zeros (set round-for-reround digit) */
869+ for (c=cfirst; c<=clast && ub<=buffer+DECPMAX; c++) {
870+ /* (see commentary just above) */
871+ if (c<=clast-3 /* safe for four */
872+ && (UINTAT(c)&0xf0f0f0f0)==CHARMASK) { /* four digits */
873+ UINTAT(ub)=UINTAT(c)&0x0f0f0f0f; /* to BCD8 */
874+ ub+=4;
875+ c+=3; /* [will become 4] */
876+ continue;
877+ }
878+ if (*c=='.') continue; /* [ignore] */
879+ *ub++=(uByte)(*c-'0');
880+ }
881+ ub--; /* -> LSD */
882+ for (; c<=clast; c++) { /* inspect remaining chars */
883+ if (*c!='0') { /* sticky bit needed */
884+ if (*c=='.') continue; /* [ignore] */
885+ *ub=DECSTICKYTAB[*ub]; /* update round-for-reround */
886+ break; /* no need to look at more */
887+ }
888+ }
889+ num.lsd=ub; /* record LSD */
890+ /* adjust exponent for dropped digits */
891+ num.exponent+=digits-(Int)(ub-buffer+1);
892+ } /* too long for buffer */
893+ } /* digits or dot */
894+
895+ else { /* no digits or dot were found */
896+ if (*c=='\0') break; /* nothing to come is bad */
897+ /* only Infinities and NaNs are allowed, here */
898+ buffer[0]=0; /* default a coefficient of 0 */
899+ num.lsd=buffer; /* .. */
900+ if (decBiStr(c, "infinity", "INFINITY")
901+ || decBiStr(c, "inf", "INF")) num.exponent=DECFLOAT_Inf;
902+ else { /* should be a NaN */
903+ num.exponent=DECFLOAT_qNaN; /* assume quiet NaN */
904+ if (*c=='s' || *c=='S') { /* probably an sNaN */
905+ c++;
906+ num.exponent=DECFLOAT_sNaN; /* assume is in fact sNaN */
907+ }
908+ if (*c!='N' && *c!='n') break; /* check caseless "NaN" */
909+ c++;
910+ if (*c!='a' && *c!='A') break; /* .. */
911+ c++;
912+ if (*c!='N' && *c!='n') break; /* .. */
913+ c++;
914+ /* now either nothing, or nnnn payload (no dots), expected */
915+ /* -> start of integer, and skip leading 0s [including plain 0] */
916+ for (cfirst=c; *cfirst=='0';) cfirst++;
917+ if (*cfirst!='\0') { /* not empty or all-0, payload */
918+ /* payload found; check all valid digits and copy to buffer as bcd8 */
919+ ub=buffer;
920+ for (c=cfirst;; c++, ub++) {
921+ if ((unsigned)(*c-'0')>9) break; /* quit if not 0-9 */
922+ if (c-cfirst==DECPMAX-1) break; /* too many digits */
923+ *ub=(uByte)(*c-'0'); /* good bcd8 */
924+ }
925+ if (*c!='\0') break; /* not all digits, or too many */
926+ num.lsd=ub-1; /* record new LSD */
927+ }
928+ } /* NaN or sNaN */
929+ error=0; /* syntax is OK */
930+ break; /* done with specials */
931+ } /* digits=0 (special expected) */
932+ break;
933+ } /* [for(;;) break] */
934+
935+ /* decShowNum(&num, "fromStr"); */
936+
937+ if (error!=0) {
938+ set->status|=error;
939+ num.exponent=DECFLOAT_qNaN; /* set up quiet NaN */
940+ num.sign=0; /* .. with 0 sign */
941+ buffer[0]=0; /* .. and coefficient */
942+ num.lsd=buffer; /* .. */
943+ /* decShowNum(&num, "oops"); */
944+ }
945+
946+ /* decShowNum(&num, "dffs"); */
947+ decFinalize(result, &num, set); /* round, check, and lay out */
948+ /* decFloatShow(result, "fromString"); */
949+ return result;
950+ } /* decFloatFromString */
951+
952+/* ------------------------------------------------------------------ */
953+/* decFloatFromWider -- conversion from next-wider format */
954+/* */
955+/* result is the decFloat format number which gets the result of */
956+/* the conversion */
957+/* wider is the decFloatWider format number which will be narrowed */
958+/* set is the context */
959+/* returns result */
960+/* */
961+/* Narrowing can cause rounding, overflow, etc., but not Invalid */
962+/* operation (sNaNs are copied and do not signal). */
963+/* ------------------------------------------------------------------ */
964+/* narrow-to is not possible for decQuad format numbers; simply omit */
965+#if !QUAD
966+decFloat * decFloatFromWider(decFloat *result, const decFloatWider *wider,
967+ decContext *set) {
968+ bcdnum num; /* collects data for finishing */
969+ uByte bcdar[DECWPMAX]; /* room for wider coefficient */
970+ uInt widerhi=DFWWORD(wider, 0); /* top word */
971+ Int exp;
972+
973+ GETWCOEFF(wider, bcdar);
974+
975+ num.msd=bcdar; /* MSD is here always */
976+ num.lsd=bcdar+DECWPMAX-1; /* LSD is here always */
977+ num.sign=widerhi&0x80000000; /* extract sign [DECFLOAT_Sign=Neg] */
978+
979+ /* decode the wider combination field to exponent */
980+ exp=DECCOMBWEXP[widerhi>>26]; /* decode from wider combination field */
981+ /* if it is a special there's nothing to do unless sNaN; if it's */
982+ /* finite then add the (wider) exponent continuation and unbias */
983+ if (EXPISSPECIAL(exp)) exp=widerhi&0x7e000000; /* include sNaN selector */
984+ else exp+=GETWECON(wider)-DECWBIAS;
985+ num.exponent=exp;
986+
987+ /* decShowNum(&num, "dffw"); */
988+ return decFinalize(result, &num, set);/* round, check, and lay out */
989+ } /* decFloatFromWider */
990+#endif
991+
992+/* ------------------------------------------------------------------ */
993+/* decFloatGetCoefficient -- get coefficient as BCD8 */
994+/* */
995+/* df is the decFloat from which to extract the coefficient */
996+/* bcdar is where DECPMAX bytes will be written, one BCD digit in */
997+/* each byte (BCD8 encoding); if df is a NaN the first byte will */
998+/* be zero, and if it is infinite they will all be zero */
999+/* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
1000+/* 0 otherwise) */
1001+/* */
1002+/* No error is possible, and no status will be set. If df is a */
1003+/* special value the array is set to zeros (for Infinity) or to the */
1004+/* payload of a qNaN or sNaN. */
1005+/* ------------------------------------------------------------------ */
1006+Int decFloatGetCoefficient(const decFloat *df, uByte *bcdar) {
1007+ if (DFISINF(df)) memset(bcdar, 0, DECPMAX);
1008+ else {
1009+ GETCOEFF(df, bcdar); /* use macro */
1010+ if (DFISNAN(df)) bcdar[0]=0; /* MSD needs correcting */
1011+ }
1012+ return DFISSIGNED(df);
1013+ } /* decFloatGetCoefficient */
1014+
1015+/* ------------------------------------------------------------------ */
1016+/* decFloatGetExponent -- get unbiased exponent */
1017+/* */
1018+/* df is the decFloat from which to extract the exponent */
1019+/* returns the exponent, q. */
1020+/* */
1021+/* No error is possible, and no status will be set. If df is a */
1022+/* special value the first seven bits of the decFloat are returned, */
1023+/* left adjusted and with the first (sign) bit set to 0 (followed by */
1024+/* 25 0 bits). e.g., -sNaN would return 0x7e000000 (DECFLOAT_sNaN). */
1025+/* ------------------------------------------------------------------ */
1026+Int decFloatGetExponent(const decFloat *df) {
1027+ if (DFISSPECIAL(df)) return DFWORD(df, 0)&0x7e000000;
1028+ return GETEXPUN(df);
1029+ } /* decFloatGetExponent */
1030+
1031+/* ------------------------------------------------------------------ */
1032+/* decFloatSetCoefficient -- set coefficient from BCD8 */
1033+/* */
1034+/* df is the target decFloat (and source of exponent/special value) */
1035+/* bcdar holds DECPMAX digits to set the coefficient from, one */
1036+/* digit in each byte (BCD8 encoding); the first (MSD) is ignored */
1037+/* if df is a NaN; all are ignored if df is infinite. */
1038+/* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */
1039+/* returns df, which will be canonical */
1040+/* */
1041+/* No error is possible, and no status will be set. */
1042+/* ------------------------------------------------------------------ */
1043+decFloat * decFloatSetCoefficient(decFloat *df, const uByte *bcdar,
1044+ Int sig) {
1045+ uInt exp; /* for exponent */
1046+ uByte bcdzero[DECPMAX]; /* for infinities */
1047+
1048+ /* Exponent/special code is extracted from df */
1049+ if (DFISSPECIAL(df)) {
1050+ exp=DFWORD(df, 0)&0x7e000000;
1051+ if (DFISINF(df)) {
1052+ memset(bcdzero, 0, DECPMAX);
1053+ return decFloatFromBCD(df, exp, bcdzero, sig);
1054+ }
1055+ }
1056+ else exp=GETEXPUN(df);
1057+ return decFloatFromBCD(df, exp, bcdar, sig);
1058+ } /* decFloatSetCoefficient */
1059+
1060+/* ------------------------------------------------------------------ */
1061+/* decFloatSetExponent -- set exponent or special value */
1062+/* */
1063+/* df is the target decFloat (and source of coefficient/payload) */
1064+/* set is the context for reporting status */
1065+/* exp is the unbiased exponent, q, or a special value in the form */
1066+/* returned by decFloatGetExponent */
1067+/* returns df, which will be canonical */
1068+/* */
1069+/* No error is possible, but Overflow or Underflow might occur. */
1070+/* ------------------------------------------------------------------ */
1071+decFloat * decFloatSetExponent(decFloat *df, decContext *set, Int exp) {
1072+ uByte bcdcopy[DECPMAX]; /* for coefficient */
1073+ bcdnum num; /* work */
1074+ num.exponent=exp;
1075+ num.sign=decFloatGetCoefficient(df, bcdcopy); /* extract coefficient */
1076+ if (DFISSPECIAL(df)) { /* MSD or more needs correcting */
1077+ if (DFISINF(df)) memset(bcdcopy, 0, DECPMAX);
1078+ bcdcopy[0]=0;
1079+ }
1080+ num.msd=bcdcopy;
1081+ num.lsd=bcdcopy+DECPMAX-1;
1082+ return decFinalize(df, &num, set);
1083+ } /* decFloatSetExponent */
1084+
1085+/* ------------------------------------------------------------------ */
1086+/* decFloatRadix -- returns the base (10) */
1087+/* */
1088+/* df is any decFloat of this format */
1089+/* ------------------------------------------------------------------ */
1090+uInt decFloatRadix(const decFloat *df) {
1091+ if (df) return 10; /* to placate compiler */
1092+ return 10;
1093+ } /* decFloatRadix */
1094+
1095+/* ------------------------------------------------------------------ */
1096+/* decFloatShow -- printf a decFloat in hexadecimal and decimal */
1097+/* df is the decFloat to show */
1098+/* tag is a tag string displayed with the number */
1099+/* */
1100+/* This is a debug aid; the precise format of the string may change. */
1101+/* ------------------------------------------------------------------ */
1102+void decFloatShow(const decFloat *df, const char *tag) {
1103+ char hexbuf[DECBYTES*2+DECBYTES/4+1]; /* NB blank after every fourth */
1104+ char buff[DECSTRING]; /* for value in decimal */
1105+ Int i, j=0;
1106+
1107+ for (i=0; i<DECBYTES; i++) {
1108+ #if DECLITEND
1109+ sprintf(&hexbuf[j], "%02x", df->bytes[DECBYTES-1-i]);
1110+ #else
1111+ sprintf(&hexbuf[j], "%02x", df->bytes[i]);
1112+ #endif
1113+ j+=2;
1114+ /* the next line adds blank (and terminator) after final pair, too */
1115+ if ((i+1)%4==0) {strcpy(&hexbuf[j], " "); j++;}
1116+ }
1117+ decFloatToString(df, buff);
1118+ printf(">%s> %s [big-endian] %s\n", tag, hexbuf, buff);
1119+ return;
1120+ } /* decFloatShow */
1121+
1122+/* ------------------------------------------------------------------ */
1123+/* decFloatToBCD -- get sign, exponent, and BCD8 from a decFloat */
1124+/* */
1125+/* df is the source decFloat */
1126+/* exp will be set to the unbiased exponent, q, or to a special */
1127+/* value in the form returned by decFloatGetExponent */
1128+/* bcdar is where DECPMAX bytes will be written, one BCD digit in */
1129+/* each byte (BCD8 encoding); if df is a NaN the first byte will */
1130+/* be zero, and if it is infinite they will all be zero */
1131+/* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
1132+/* 0 otherwise) */
1133+/* */
1134+/* No error is possible, and no status will be set. */
1135+/* ------------------------------------------------------------------ */
1136+Int decFloatToBCD(const decFloat *df, Int *exp, uByte *bcdar) {
1137+ if (DFISINF(df)) {
1138+ memset(bcdar, 0, DECPMAX);
1139+ *exp=DFWORD(df, 0)&0x7e000000;
1140+ }
1141+ else {
1142+ GETCOEFF(df, bcdar); /* use macro */
1143+ if (DFISNAN(df)) {
1144+ bcdar[0]=0; /* MSD needs correcting */
1145+ *exp=DFWORD(df, 0)&0x7e000000;
1146+ }
1147+ else { /* finite */
1148+ *exp=GETEXPUN(df);
1149+ }
1150+ }
1151+ return DFISSIGNED(df);
1152+ } /* decFloatToBCD */
1153+
1154+/* ------------------------------------------------------------------ */
1155+/* decFloatToEngString -- conversion to numeric string, engineering */
1156+/* */
1157+/* df is the decFloat format number to convert */
1158+/* string is the string where the result will be laid out */
1159+/* */
1160+/* string must be at least DECPMAX+9 characters (the worst case is */
1161+/* "-0.00000nnn...nnn\0", which is as long as the exponent form when */
1162+/* DECEMAXD<=4); this condition is asserted above */
1163+/* */
1164+/* No error is possible, and no status will be set */
1165+/* ------------------------------------------------------------------ */
1166+char * decFloatToEngString(const decFloat *df, char *string){
1167+ uInt msd; /* coefficient MSD */
1168+ Int exp; /* exponent top two bits or full */
1169+ uInt comb; /* combination field */
1170+ char *cstart; /* coefficient start */
1171+ char *c; /* output pointer in string */
1172+ char *s, *t; /* .. (source, target) */
1173+ Int pre, e; /* work */
1174+ const uByte *u; /* .. */
1175+
1176+ /* Source words; macro handles endianness */
1177+ uInt sourhi=DFWORD(df, 0); /* word with sign */
1178+ #if DECPMAX==16
1179+ uInt sourlo=DFWORD(df, 1);
1180+ #elif DECPMAX==34
1181+ uInt sourmh=DFWORD(df, 1);
1182+ uInt sourml=DFWORD(df, 2);
1183+ uInt sourlo=DFWORD(df, 3);
1184+ #endif
1185+
1186+ c=string; /* where result will go */
1187+ if (((Int)sourhi)<0) *c++='-'; /* handle sign */
1188+ comb=sourhi>>26; /* sign+combination field */
1189+ msd=DECCOMBMSD[comb]; /* decode the combination field */
1190+ exp=DECCOMBEXP[comb]; /* .. */
1191+
1192+ if (EXPISSPECIAL(exp)) { /* special */
1193+ if (exp==DECFLOAT_Inf) { /* infinity */
1194+ strcpy(c, "Inf");
1195+ strcpy(c+3, "inity");
1196+ return string; /* easy */
1197+ }
1198+ if (sourhi&0x02000000) *c++='s'; /* sNaN */
1199+ strcpy(c, "NaN"); /* complete word */
1200+ c+=3; /* step past */
1201+ /* quick exit if the payload is zero */
1202+ #if DECPMAX==7
1203+ if ((sourhi&0x000fffff)==0) return string;
1204+ #elif DECPMAX==16
1205+ if (sourlo==0 && (sourhi&0x0003ffff)==0) return string;
1206+ #elif DECPMAX==34
1207+ if (sourlo==0 && sourml==0 && sourmh==0
1208+ && (sourhi&0x00003fff)==0) return string;
1209+ #endif
1210+ /* otherwise drop through to add integer; set correct exp etc. */
1211+ exp=0; msd=0; /* setup for following code */
1212+ }
1213+ else { /* complete exponent; top two bits are in place */
1214+ exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */
1215+ }
1216+
1217+ /* convert the digits of the significand to characters */
1218+ cstart=c; /* save start of coefficient */
1219+ if (msd) *c++=(char)('0'+(char)msd); /* non-zero most significant digit */
1220+
1221+ /* Decode the declets. After extracting each declet, it is */
1222+ /* decoded to a 4-uByte sequence by table lookup; the four uBytes */
1223+ /* are the three encoded BCD8 digits followed by a 1-byte length */
1224+ /* (significant digits, except that 000 has length 0). This allows */
1225+ /* us to left-align the first declet with non-zero content, then */
1226+ /* the remaining ones are full 3-char length. Fixed-length copies */
1227+ /* are used because variable-length memcpy causes a subroutine call */
1228+ /* in at least two compilers. (The copies are length 4 for speed */
1229+ /* and are safe because the last item in the array is of length */
1230+ /* three and has the length byte following.) */
1231+ #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \
1232+ if (c!=cstart) {UINTAT(c)=UINTAT(u)|CHARMASK; c+=3;} \
1233+ else if (*(u+3)) { \
1234+ UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; c+=*(u+3);}
1235+
1236+ #if DECPMAX==7
1237+ dpd2char(sourhi>>10); /* declet 1 */
1238+ dpd2char(sourhi); /* declet 2 */
1239+
1240+ #elif DECPMAX==16
1241+ dpd2char(sourhi>>8); /* declet 1 */
1242+ dpd2char((sourhi<<2) | (sourlo>>30)); /* declet 2 */
1243+ dpd2char(sourlo>>20); /* declet 3 */
1244+ dpd2char(sourlo>>10); /* declet 4 */
1245+ dpd2char(sourlo); /* declet 5 */
1246+
1247+ #elif DECPMAX==34
1248+ dpd2char(sourhi>>4); /* declet 1 */
1249+ dpd2char((sourhi<<6) | (sourmh>>26)); /* declet 2 */
1250+ dpd2char(sourmh>>16); /* declet 3 */
1251+ dpd2char(sourmh>>6); /* declet 4 */
1252+ dpd2char((sourmh<<4) | (sourml>>28)); /* declet 5 */
1253+ dpd2char(sourml>>18); /* declet 6 */
1254+ dpd2char(sourml>>8); /* declet 7 */
1255+ dpd2char((sourml<<2) | (sourlo>>30)); /* declet 8 */
1256+ dpd2char(sourlo>>20); /* declet 9 */
1257+ dpd2char(sourlo>>10); /* declet 10 */
1258+ dpd2char(sourlo); /* declet 11 */
1259+ #endif
1260+
1261+ if (c==cstart) *c++='0'; /* all zeros, empty -- make "0" */
1262+
1263+ if (exp==0) { /* integer or NaN case -- easy */
1264+ *c='\0'; /* terminate */
1265+ return string;
1266+ }
1267+ /* non-0 exponent */
1268+
1269+ e=0; /* assume no E */
1270+ pre=(Int)(c-cstart)+exp; /* length+exp [c->LSD+1] */
1271+ /* [here, pre-exp is the digits count (==1 for zero)] */
1272+
1273+ if (exp>0 || pre<-5) { /* need exponential form */
1274+ e=pre-1; /* calculate E value */
1275+ pre=1; /* assume one digit before '.' */
1276+ if (e!=0) { /* engineering: may need to adjust */
1277+ Int adj; /* adjustment */
1278+ /* The C remainder operator is undefined for negative numbers, so */
1279+ /* a positive remainder calculation must be used here */
1280+ if (e<0) {
1281+ adj=(-e)%3;
1282+ if (adj!=0) adj=3-adj;
1283+ }
1284+ else { /* e>0 */
1285+ adj=e%3;
1286+ }
1287+ e=e-adj;
1288+ /* if dealing with zero still produce an exponent which is a */
1289+ /* multiple of three, as expected, but there will only be the */
1290+ /* one zero before the E, still. Otherwise note the padding. */
1291+ if (!DFISZERO(df)) pre+=adj;
1292+ else { /* is zero */
1293+ if (adj!=0) { /* 0.00Esnn needed */
1294+ e=e+3;
1295+ pre=-(2-adj);
1296+ }
1297+ } /* zero */
1298+ } /* engineering adjustment */
1299+ } /* exponential form */
1300+ /* printf("e=%ld pre=%ld exp=%ld\n", (LI)e, (LI)pre, (LI)exp); */
1301+
1302+ /* modify the coefficient, adding 0s, '.', and E+nn as needed */
1303+ if (pre>0) { /* ddd.ddd (plain), perhaps with E */
1304+ /* or dd00 padding for engineering */
1305+ char *dotat=cstart+pre;
1306+ if (dotat<c) { /* if embedded dot needed... */
1307+ /* move by fours; there must be space for junk at the end */
1308+ /* because there is still space for exponent */
1309+ s=dotat+ROUNDDOWN4(c-dotat); /* source */
1310+ t=s+1; /* target */
1311+ /* open the gap */
1312+ for (; s>=dotat; s-=4, t-=4) UINTAT(t)=UINTAT(s);
1313+ *dotat='.';
1314+ c++; /* length increased by one */
1315+ } /* need dot? */
1316+ else for (; c<dotat; c++) *c='0'; /* pad for engineering */
1317+ } /* pre>0 */
1318+ else {
1319+ /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (may have
1320+ E, but only for 0.00E+3 kind of case -- with plenty of spare
1321+ space in this case */
1322+ pre=-pre+2; /* gap width, including "0." */
1323+ t=cstart+ROUNDDOWN4(c-cstart)+pre; /* preferred first target point */
1324+ /* backoff if too far to the right */
1325+ if (t>string+DECSTRING-5) t=string+DECSTRING-5; /* adjust to fit */
1326+ /* now shift the entire coefficient to the right, being careful not */
1327+ /* to access to the left of string */
1328+ for (s=t-pre; s>=string; s-=4, t-=4) UINTAT(t)=UINTAT(s);
1329+ /* for Quads and Singles there may be a character or two left... */
1330+ s+=3; /* where next would come from */
1331+ for(; s>=cstart; s--, t--) *(t+3)=*(s);
1332+ /* now have fill 0. through 0.00000; use overlaps to avoid tests */
1333+ if (pre>=4) {
1334+ UINTAT(cstart+pre-4)=UINTAT("0000");
1335+ UINTAT(cstart)=UINTAT("0.00");
1336+ }
1337+ else { /* 2 or 3 */
1338+ *(cstart+pre-1)='0';
1339+ USHORTAT(cstart)=USHORTAT("0.");
1340+ }
1341+ c+=pre; /* to end */
1342+ }
1343+
1344+ /* finally add the E-part, if needed; it will never be 0, and has */
1345+ /* a maximum length of 3 or 4 digits (asserted above) */
1346+ if (e!=0) {
1347+ USHORTAT(c)=USHORTAT("E+"); /* starts with E, assume + */
1348+ c++;
1349+ if (e<0) {
1350+ *c='-'; /* oops, need '-' */
1351+ e=-e; /* uInt, please */
1352+ }
1353+ c++;
1354+ /* Three-character exponents are easy; 4-character a little trickier */
1355+ #if DECEMAXD<=3
1356+ u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
1357+ /* copy fixed 4 characters [is safe], starting at non-zero */
1358+ /* and with character mask to convert BCD to char */
1359+ UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK;
1360+ c+=*(u+3); /* bump pointer appropriately */
1361+ #elif DECEMAXD==4
1362+ if (e<1000) { /* 3 (or fewer) digits case */
1363+ u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
1364+ UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; /* [as above] */
1365+ c+=*(u+3); /* bump pointer appropriately */
1366+ }
1367+ else { /* 4-digits */
1368+ Int thou=((e>>3)*1049)>>17; /* e/1000 */
1369+ Int rem=e-(1000*thou); /* e%1000 */
1370+ *c++=(char)('0'+(char)thou); /* the thousands digit */
1371+ u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */
1372+ UINTAT(c)=UINTAT(u)|CHARMASK; /* copy fixed 3+1 characters [is safe] */
1373+ c+=3; /* bump pointer, always 3 digits */
1374+ }
1375+ #endif
1376+ }
1377+ *c='\0'; /* terminate */
1378+ /*printf("res %s\n", string); */
1379+ return string;
1380+ } /* decFloatToEngString */
1381+
1382+/* ------------------------------------------------------------------ */
1383+/* decFloatToPacked -- convert decFloat to Packed decimal + exponent */
1384+/* */
1385+/* df is the source decFloat */
1386+/* exp will be set to the unbiased exponent, q, or to a special */
1387+/* value in the form returned by decFloatGetExponent */
1388+/* packed is where DECPMAX nibbles will be written with the sign as */
1389+/* final nibble (0x0c for +, 0x0d for -); a NaN has a first nibble */
1390+/* of zero, and an infinity is all zeros. decDouble and decQuad */
1391+/* have a additional leading zero nibble, leading to result */
1392+/* lengths of 4, 9, and 18 bytes. */
1393+/* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
1394+/* 0 otherwise) */
1395+/* */
1396+/* No error is possible, and no status will be set. */
1397+/* ------------------------------------------------------------------ */
1398+Int decFloatToPacked(const decFloat *df, Int *exp, uByte *packed) {
1399+ uByte bcdar[DECPMAX+2]; /* work buffer */
1400+ uByte *ip=bcdar, *op=packed; /* work pointers */
1401+ if (DFISINF(df)) {
1402+ memset(bcdar, 0, DECPMAX+2);
1403+ *exp=DECFLOAT_Inf;
1404+ }
1405+ else {
1406+ GETCOEFF(df, bcdar+1); /* use macro */
1407+ if (DFISNAN(df)) {
1408+ bcdar[1]=0; /* MSD needs clearing */
1409+ *exp=DFWORD(df, 0)&0x7e000000;
1410+ }
1411+ else { /* finite */
1412+ *exp=GETEXPUN(df);
1413+ }
1414+ }
1415+ /* now pack; coefficient currently at bcdar+1 */
1416+ #if SINGLE
1417+ ip++; /* ignore first byte */
1418+ #else
1419+ *ip=0; /* need leading zero */
1420+ #endif
1421+ /* set final byte to Packed BCD sign value */
1422+ bcdar[DECPMAX+1]=(DFISSIGNED(df) ? DECPMINUS : DECPPLUS);
1423+ /* pack an even number of bytes... */
1424+ for (; op<packed+((DECPMAX+2)/2); op++, ip+=2) {
1425+ *op=(uByte)((*ip<<4)+*(ip+1));
1426+ }
1427+ return (bcdar[DECPMAX+1]==DECPMINUS ? DECFLOAT_Sign : 0);
1428+ } /* decFloatToPacked */
1429+
1430+/* ------------------------------------------------------------------ */
1431+/* decFloatToString -- conversion to numeric string */
1432+/* */
1433+/* df is the decFloat format number to convert */
1434+/* string is the string where the result will be laid out */
1435+/* */
1436+/* string must be at least DECPMAX+9 characters (the worst case is */
1437+/* "-0.00000nnn...nnn\0", which is as long as the exponent form when */
1438+/* DECEMAXD<=4); this condition is asserted above */
1439+/* */
1440+/* No error is possible, and no status will be set */
1441+/* ------------------------------------------------------------------ */
1442+char * decFloatToString(const decFloat *df, char *string){
1443+ uInt msd; /* coefficient MSD */
1444+ Int exp; /* exponent top two bits or full */
1445+ uInt comb; /* combination field */
1446+ char *cstart; /* coefficient start */
1447+ char *c; /* output pointer in string */
1448+ char *s, *t; /* .. (source, target) */
1449+ Int pre, e; /* work */
1450+ const uByte *u; /* .. */
1451+
1452+ /* Source words; macro handles endianness */
1453+ uInt sourhi=DFWORD(df, 0); /* word with sign */
1454+ #if DECPMAX==16
1455+ uInt sourlo=DFWORD(df, 1);
1456+ #elif DECPMAX==34
1457+ uInt sourmh=DFWORD(df, 1);
1458+ uInt sourml=DFWORD(df, 2);
1459+ uInt sourlo=DFWORD(df, 3);
1460+ #endif
1461+
1462+ c=string; /* where result will go */
1463+ if (((Int)sourhi)<0) *c++='-'; /* handle sign */
1464+ comb=sourhi>>26; /* sign+combination field */
1465+ msd=DECCOMBMSD[comb]; /* decode the combination field */
1466+ exp=DECCOMBEXP[comb]; /* .. */
1467+
1468+ if (EXPISSPECIAL(exp)) { /* special */
1469+ if (exp==DECFLOAT_Inf) { /* infinity */
1470+ strcpy(c, "Infinity");
1471+ return string; /* easy */
1472+ }
1473+ if (sourhi&0x02000000) *c++='s'; /* sNaN */
1474+ strcpy(c, "NaN"); /* complete word */
1475+ c+=3; /* step past */
1476+ /* quick exit if the payload is zero */
1477+ #if DECPMAX==7
1478+ if ((sourhi&0x000fffff)==0) return string;
1479+ #elif DECPMAX==16
1480+ if (sourlo==0 && (sourhi&0x0003ffff)==0) return string;
1481+ #elif DECPMAX==34
1482+ if (sourlo==0 && sourml==0 && sourmh==0
1483+ && (sourhi&0x00003fff)==0) return string;
1484+ #endif
1485+ /* otherwise drop through to add integer; set correct exp etc. */
1486+ exp=0; msd=0; /* setup for following code */
1487+ }
1488+ else { /* complete exponent; top two bits are in place */
1489+ exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */
1490+ }
1491+
1492+ /* convert the digits of the significand to characters */
1493+ cstart=c; /* save start of coefficient */
1494+ if (msd) *c++=(char)('0'+(char)msd); /* non-zero most significant digit */
1495+
1496+ /* Decode the declets. After extracting each declet, it is */
1497+ /* decoded to a 4-uByte sequence by table lookup; the four uBytes */
1498+ /* are the three encoded BCD8 digits followed by a 1-byte length */
1499+ /* (significant digits, except that 000 has length 0). This allows */
1500+ /* us to left-align the first declet with non-zero content, then */
1501+ /* the remaining ones are full 3-char length. Fixed-length copies */
1502+ /* are used because variable-length memcpy causes a subroutine call */
1503+ /* in at least two compilers. (The copies are length 4 for speed */
1504+ /* and are safe because the last item in the array is of length */
1505+ /* three and has the length byte following.) */
1506+ #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \
1507+ if (c!=cstart) {UINTAT(c)=UINTAT(u)|CHARMASK; c+=3;} \
1508+ else if (*(u+3)) { \
1509+ UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; c+=*(u+3);}
1510+
1511+ #if DECPMAX==7
1512+ dpd2char(sourhi>>10); /* declet 1 */
1513+ dpd2char(sourhi); /* declet 2 */
1514+
1515+ #elif DECPMAX==16
1516+ dpd2char(sourhi>>8); /* declet 1 */
1517+ dpd2char((sourhi<<2) | (sourlo>>30)); /* declet 2 */
1518+ dpd2char(sourlo>>20); /* declet 3 */
1519+ dpd2char(sourlo>>10); /* declet 4 */
1520+ dpd2char(sourlo); /* declet 5 */
1521+
1522+ #elif DECPMAX==34
1523+ dpd2char(sourhi>>4); /* declet 1 */
1524+ dpd2char((sourhi<<6) | (sourmh>>26)); /* declet 2 */
1525+ dpd2char(sourmh>>16); /* declet 3 */
1526+ dpd2char(sourmh>>6); /* declet 4 */
1527+ dpd2char((sourmh<<4) | (sourml>>28)); /* declet 5 */
1528+ dpd2char(sourml>>18); /* declet 6 */
1529+ dpd2char(sourml>>8); /* declet 7 */
1530+ dpd2char((sourml<<2) | (sourlo>>30)); /* declet 8 */
1531+ dpd2char(sourlo>>20); /* declet 9 */
1532+ dpd2char(sourlo>>10); /* declet 10 */
1533+ dpd2char(sourlo); /* declet 11 */
1534+ #endif
1535+
1536+ if (c==cstart) *c++='0'; /* all zeros, empty -- make "0" */
1537+
1538+ /*[This fast path is valid but adds 3-5 cycles to worst case length] */
1539+ /*if (exp==0) { // integer or NaN case -- easy */
1540+ /* *c='\0'; // terminate */
1541+ /* return string; */
1542+ /* } */
1543+
1544+ e=0; /* assume no E */
1545+ pre=(Int)(c-cstart)+exp; /* length+exp [c->LSD+1] */
1546+ /* [here, pre-exp is the digits count (==1 for zero)] */
1547+
1548+ if (exp>0 || pre<-5) { /* need exponential form */
1549+ e=pre-1; /* calculate E value */
1550+ pre=1; /* assume one digit before '.' */
1551+ } /* exponential form */
1552+
1553+ /* modify the coefficient, adding 0s, '.', and E+nn as needed */
1554+ if (pre>0) { /* ddd.ddd (plain), perhaps with E */
1555+ char *dotat=cstart+pre;
1556+ if (dotat<c) { /* if embedded dot needed... */
1557+ /* move by fours; there must be space for junk at the end */
1558+ /* because there is still space for exponent */
1559+ s=dotat+ROUNDDOWN4(c-dotat); /* source */
1560+ t=s+1; /* target */
1561+ /* open the gap */
1562+ for (; s>=dotat; s-=4, t-=4) UINTAT(t)=UINTAT(s);
1563+ *dotat='.';
1564+ c++; /* length increased by one */
1565+ } /* need dot? */
1566+
1567+ /* finally add the E-part, if needed; it will never be 0, and has */
1568+ /* a maximum length of 3 or 4 digits (asserted above) */
1569+ if (e!=0) {
1570+ USHORTAT(c)=USHORTAT("E+"); /* starts with E, assume + */
1571+ c++;
1572+ if (e<0) {
1573+ *c='-'; /* oops, need '-' */
1574+ e=-e; /* uInt, please */
1575+ }
1576+ c++;
1577+ /* Three-character exponents are easy; 4-character a little trickier */
1578+ #if DECEMAXD<=3
1579+ u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
1580+ /* copy fixed 4 characters [is safe], starting at non-zero */
1581+ /* and with character mask to convert BCD to char */
1582+ UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK;
1583+ c+=*(u+3); /* bump pointer appropriately */
1584+ #elif DECEMAXD==4
1585+ if (e<1000) { /* 3 (or fewer) digits case */
1586+ u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
1587+ UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; /* [as above] */
1588+ c+=*(u+3); /* bump pointer appropriately */
1589+ }
1590+ else { /* 4-digits */
1591+ Int thou=((e>>3)*1049)>>17; /* e/1000 */
1592+ Int rem=e-(1000*thou); /* e%1000 */
1593+ *c++=(char)('0'+(char)thou); /* the thousands digit */
1594+ u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */
1595+ UINTAT(c)=UINTAT(u)|CHARMASK; /* copy fixed 3+1 characters [is safe] */
1596+ c+=3; /* bump pointer, always 3 digits */
1597+ }
1598+ #endif
1599+ }
1600+ *c='\0'; /* add terminator */
1601+ /*printf("res %s\n", string); */
1602+ return string;
1603+ } /* pre>0 */
1604+
1605+ /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
1606+ /* Surprisingly, this is close to being the worst-case path, so the */
1607+ /* shift is done by fours; this is a little tricky because the */
1608+ /* rightmost character to be written must not be beyond where the */
1609+ /* rightmost terminator could be -- so backoff to not touch */
1610+ /* terminator position if need be (this can make exact alignments */
1611+ /* for full Doubles, but in some cases needs care not to access too */
1612+ /* far to the left) */
1613+
1614+ pre=-pre+2; /* gap width, including "0." */
1615+ t=cstart+ROUNDDOWN4(c-cstart)+pre; /* preferred first target point */
1616+ /* backoff if too far to the right */
1617+ if (t>string+DECSTRING-5) t=string+DECSTRING-5; /* adjust to fit */
1618+ /* now shift the entire coefficient to the right, being careful not */
1619+ /* to access to the left of string */
1620+ for (s=t-pre; s>=string; s-=4, t-=4) UINTAT(t)=UINTAT(s);
1621+ /* for Quads and Singles there may be a character or two left... */
1622+ s+=3; /* where next would come from */
1623+ for(; s>=cstart; s--, t--) *(t+3)=*(s);
1624+ /* now have fill 0. through 0.00000; use overlaps to avoid tests */
1625+ if (pre>=4) {
1626+ UINTAT(cstart+pre-4)=UINTAT("0000");
1627+ UINTAT(cstart)=UINTAT("0.00");
1628+ }
1629+ else { /* 2 or 3 */
1630+ *(cstart+pre-1)='0';
1631+ USHORTAT(cstart)=USHORTAT("0.");
1632+ }
1633+ *(c+pre)='\0'; /* terminate */
1634+ return string;
1635+ } /* decFloatToString */
1636+
1637+/* ------------------------------------------------------------------ */
1638+/* decFloatToWider -- conversion to next-wider format */
1639+/* */
1640+/* source is the decFloat format number which gets the result of */
1641+/* the conversion */
1642+/* wider is the decFloatWider format number which will be narrowed */
1643+/* returns wider */
1644+/* */
1645+/* Widening is always exact; no status is set (sNaNs are copied and */
1646+/* do not signal). The result will be canonical if the source is, */
1647+/* and may or may not be if the source is not. */
1648+/* ------------------------------------------------------------------ */
1649+/* widening is not possible for decQuad format numbers; simply omit */
1650+#if !QUAD
1651+decFloatWider * decFloatToWider(const decFloat *source, decFloatWider *wider) {
1652+ uInt msd;
1653+
1654+ /* Construct and copy the sign word */
1655+ if (DFISSPECIAL(source)) {
1656+ /* copy sign, combination, and first bit of exponent (sNaN selector) */
1657+ DFWWORD(wider, 0)=DFWORD(source, 0)&0xfe000000;
1658+ msd=0;
1659+ }
1660+ else { /* is finite number */
1661+ uInt exp=GETEXPUN(source)+DECWBIAS; /* get unbiased exponent and rebias */
1662+ uInt code=(exp>>DECWECONL)<<29; /* set two bits of exp [msd=0] */
1663+ code|=(exp<<(32-6-DECWECONL)) & 0x03ffffff; /* add exponent continuation */
1664+ code|=DFWORD(source, 0)&0x80000000; /* add sign */
1665+ DFWWORD(wider, 0)=code; /* .. and place top word in wider */
1666+ msd=GETMSD(source); /* get source coefficient MSD [0-9] */
1667+ }
1668+ /* Copy the coefficient and clear any 'unused' words to left */
1669+ #if SINGLE
1670+ DFWWORD(wider, 1)=(DFWORD(source, 0)&0x000fffff)|(msd<<20);
1671+ #elif DOUBLE
1672+ DFWWORD(wider, 2)=(DFWORD(source, 0)&0x0003ffff)|(msd<<18);
1673+ DFWWORD(wider, 3)=DFWORD(source, 1);
1674+ DFWWORD(wider, 1)=0;
1675+ #endif
1676+ return wider;
1677+ } /* decFloatToWider */
1678+#endif
1679+
1680+/* ------------------------------------------------------------------ */
1681+/* decFloatVersion -- return package version string */
1682+/* */
1683+/* returns a constant string describing this package */
1684+/* ------------------------------------------------------------------ */
1685+const char *decFloatVersion(void) {
1686+ return DECVERSION;
1687+ } /* decFloatVersion */
1688+
1689+/* ------------------------------------------------------------------ */
1690+/* decFloatZero -- set to canonical (integer) zero */
1691+/* */
1692+/* df is the decFloat format number to integer +0 (q=0, c=+0) */
1693+/* returns df */
1694+/* */
1695+/* No error is possible, and no status can be set. */
1696+/* ------------------------------------------------------------------ */
1697+decFloat * decFloatZero(decFloat *df){
1698+ DFWORD(df, 0)=ZEROWORD; /* set appropriate top word */
1699+ #if DOUBLE || QUAD
1700+ DFWORD(df, 1)=0;
1701+ #if QUAD
1702+ DFWORD(df, 2)=0;
1703+ DFWORD(df, 3)=0;
1704+ #endif
1705+ #endif
1706+ /* decFloatShow(df, "zero"); */
1707+ return df;
1708+ } /* decFloatZero */
1709+
1710+/* ------------------------------------------------------------------ */
1711+/* Private generic function (not format-specific) for development use */
1712+/* ------------------------------------------------------------------ */
1713+/* This is included once only, for all to use */
1714+#if QUAD && (DECCHECK || DECTRACE)
1715+ /* ---------------------------------------------------------------- */
1716+ /* decShowNum -- display bcd8 number in debug form */
1717+ /* */
1718+ /* num is the bcdnum to display */
1719+ /* tag is a string to label the display */
1720+ /* ---------------------------------------------------------------- */
1721+ void decShowNum(const bcdnum *num, const char *tag) {
1722+ const char *csign="+"; /* sign character */
1723+ uByte *ub; /* work */
1724+ if (num->sign==DECFLOAT_Sign) csign="-";
1725+
1726+ printf(">%s> ", tag);
1727+ if (num->exponent==DECFLOAT_Inf) printf("%sInfinity", csign);
1728+ else if (num->exponent==DECFLOAT_qNaN) printf("%sqNaN", csign);
1729+ else if (num->exponent==DECFLOAT_sNaN) printf("%ssNaN", csign);
1730+ else { /* finite */
1731+ char qbuf[10]; /* for right-aligned q */
1732+ char *c; /* work */
1733+ const uByte *u; /* .. */
1734+ Int e=num->exponent; /* .. exponent */
1735+ strcpy(qbuf, "q=");
1736+ c=&qbuf[2]; /* where exponent will go */
1737+ /* lay out the exponent */
1738+ if (e<0) {
1739+ *c++='-'; /* add '-' */
1740+ e=-e; /* uInt, please */
1741+ }
1742+ #if DECEMAXD>4
1743+ #error Exponent form is too long for ShowNum to lay out
1744+ #endif
1745+ if (e==0) *c++='0'; /* 0-length case */
1746+ else if (e<1000) { /* 3 (or fewer) digits case */
1747+ u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
1748+ UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; /* [as above] */
1749+ c+=*(u+3); /* bump pointer appropriately */
1750+ }
1751+ else { /* 4-digits */
1752+ Int thou=((e>>3)*1049)>>17; /* e/1000 */
1753+ Int rem=e-(1000*thou); /* e%1000 */
1754+ *c++=(char)('0'+(char)thou); /* the thousands digit */
1755+ u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */
1756+ UINTAT(c)=UINTAT(u)|CHARMASK; /* copy fixed 3+1 characters [is safe] */
1757+ c+=3; /* bump pointer, always 3 digits */
1758+ }
1759+ *c='\0'; /* add terminator */
1760+ printf("%7s c=%s", qbuf, csign);
1761+ }
1762+
1763+ if (!EXPISSPECIAL(num->exponent) || num->msd!=num->lsd || *num->lsd!=0) {
1764+ for (ub=num->msd; ub<=num->lsd; ub++) { /* coefficient... */
1765+ printf("%1x", *ub);
1766+ if ((num->lsd-ub)%3==0 && ub!=num->lsd) printf(" "); /* 4-space */
1767+ }
1768+ }
1769+ printf("\n");
1770+ } /* decShowNum */
1771+#endif
--- a/libdecnumber/decContext.c
+++ b/libdecnumber/decContext.c
@@ -1,5 +1,5 @@
11 /* Decimal context module for the decNumber C Library.
2- Copyright (C) 2005 Free Software Foundation, Inc.
2+ Copyright (C) 2005, 2007 Free Software Foundation, Inc.
33 Contributed by IBM Corporation. Author Mike Cowlishaw.
44
55 This file is part of GCC.
@@ -28,201 +28,405 @@
2828 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2929 02110-1301, USA. */
3030
31-/* This module compirises the routines for handling the arithmetic
32- context structures. */
33-
34-#include <string.h> /* for strcmp */
35-#include "config.h"
36-#include "decContext.h" /* context and base types */
37-#include "decNumberLocal.h" /* decNumber local types, etc. */
38-
39-/* ------------------------------------------------------------------ */
40-/* decContextDefault -- initialize a context structure */
41-/* */
42-/* context is the structure to be initialized */
43-/* kind selects the required set of default values, one of: */
44-/* DEC_INIT_BASE -- select ANSI X3-274 defaults */
45-/* DEC_INIT_DECIMAL32 -- select IEEE 754r defaults, 32-bit */
46-/* DEC_INIT_DECIMAL64 -- select IEEE 754r defaults, 64-bit */
47-/* DEC_INIT_DECIMAL128 -- select IEEE 754r defaults, 128-bit */
48-/* For any other value a valid context is returned, but with */
49-/* Invalid_operation set in the status field. */
31+/* ------------------------------------------------------------------ */
32+/* Decimal Context module */
33+/* ------------------------------------------------------------------ */
34+/* This module comprises the routines for handling arithmetic */
35+/* context structures. */
36+/* ------------------------------------------------------------------ */
37+
38+#include <string.h> /* for strcmp */
39+#include <stdio.h> /* for printf if DECCHECK */
40+#include "config.h" /* for GCC definitions */
41+#include "decContext.h" /* context and base types */
42+#include "decNumberLocal.h" /* decNumber local types, etc. */
43+
44+#if DECCHECK
45+/* compile-time endian tester [assumes sizeof(Int)>1] */
46+static const Int mfcone=1; /* constant 1 */
47+static const Flag *mfctop=(Flag *)&mfcone; /* -> top byte */
48+#define LITEND *mfctop /* named flag; 1=little-endian */
49+#endif
50+
51+/* ------------------------------------------------------------------ */
52+/* round-for-reround digits */
53+/* ------------------------------------------------------------------ */
54+const uByte DECSTICKYTAB[10]={1,1,2,3,4,6,6,7,8,9}; /* used if sticky */
55+
56+/* ------------------------------------------------------------------ */
57+/* Powers of ten (powers[n]==10**n, 0<=n<=9) */
58+/* ------------------------------------------------------------------ */
59+const uInt DECPOWERS[10]={1, 10, 100, 1000, 10000, 100000, 1000000,
60+ 10000000, 100000000, 1000000000};
61+
62+/* ------------------------------------------------------------------ */
63+/* decContextClearStatus -- clear bits in current status */
64+/* */
65+/* context is the context structure to be queried */
66+/* mask indicates the bits to be cleared (the status bit that */
67+/* corresponds to each 1 bit in the mask is cleared) */
68+/* returns context */
69+/* */
70+/* No error is possible. */
71+/* ------------------------------------------------------------------ */
72+decContext *decContextClearStatus(decContext *context, uInt mask) {
73+ context->status&=~mask;
74+ return context;
75+ } /* decContextClearStatus */
76+
77+/* ------------------------------------------------------------------ */
78+/* decContextDefault -- initialize a context structure */
79+/* */
80+/* context is the structure to be initialized */
81+/* kind selects the required set of default values, one of: */
82+/* DEC_INIT_BASE -- select ANSI X3-274 defaults */
83+/* DEC_INIT_DECIMAL32 -- select IEEE 754r defaults, 32-bit */
84+/* DEC_INIT_DECIMAL64 -- select IEEE 754r defaults, 64-bit */
85+/* DEC_INIT_DECIMAL128 -- select IEEE 754r defaults, 128-bit */
86+/* For any other value a valid context is returned, but with */
87+/* Invalid_operation set in the status field. */
5088 /* returns a context structure with the appropriate initial values. */
5189 /* ------------------------------------------------------------------ */
52-decContext *
53-decContextDefault (decContext * context, Int kind)
54-{
90+decContext * decContextDefault(decContext *context, Int kind) {
5591 /* set defaults... */
56- context->digits = 9; /* 9 digits */
57- context->emax = DEC_MAX_EMAX; /* 9-digit exponents */
58- context->emin = DEC_MIN_EMIN; /* .. balanced */
59- context->round = DEC_ROUND_HALF_UP; /* 0.5 rises */
60- context->traps = DEC_Errors; /* all but informational */
61- context->status = 0; /* cleared */
62- context->clamp = 0; /* no clamping */
63-#if DECSUBSET
64- context->extended = 0; /* cleared */
65-#endif
66- switch (kind)
67- {
92+ context->digits=9; /* 9 digits */
93+ context->emax=DEC_MAX_EMAX; /* 9-digit exponents */
94+ context->emin=DEC_MIN_EMIN; /* .. balanced */
95+ context->round=DEC_ROUND_HALF_UP; /* 0.5 rises */
96+ context->traps=DEC_Errors; /* all but informational */
97+ context->status=0; /* cleared */
98+ context->clamp=0; /* no clamping */
99+ #if DECSUBSET
100+ context->extended=0; /* cleared */
101+ #endif
102+ switch (kind) {
68103 case DEC_INIT_BASE:
69104 /* [use defaults] */
70105 break;
71106 case DEC_INIT_DECIMAL32:
72- context->digits = 7; /* digits */
73- context->emax = 96; /* Emax */
74- context->emin = -95; /* Emin */
75- context->round = DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
76- context->traps = 0; /* no traps set */
77- context->clamp = 1; /* clamp exponents */
78-#if DECSUBSET
79- context->extended = 1; /* set */
80-#endif
107+ context->digits=7; /* digits */
108+ context->emax=96; /* Emax */
109+ context->emin=-95; /* Emin */
110+ context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
111+ context->traps=0; /* no traps set */
112+ context->clamp=1; /* clamp exponents */
113+ #if DECSUBSET
114+ context->extended=1; /* set */
115+ #endif
81116 break;
82117 case DEC_INIT_DECIMAL64:
83- context->digits = 16; /* digits */
84- context->emax = 384; /* Emax */
85- context->emin = -383; /* Emin */
86- context->round = DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
87- context->traps = 0; /* no traps set */
88- context->clamp = 1; /* clamp exponents */
89-#if DECSUBSET
90- context->extended = 1; /* set */
91-#endif
118+ context->digits=16; /* digits */
119+ context->emax=384; /* Emax */
120+ context->emin=-383; /* Emin */
121+ context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
122+ context->traps=0; /* no traps set */
123+ context->clamp=1; /* clamp exponents */
124+ #if DECSUBSET
125+ context->extended=1; /* set */
126+ #endif
92127 break;
93128 case DEC_INIT_DECIMAL128:
94- context->digits = 34; /* digits */
95- context->emax = 6144; /* Emax */
96- context->emin = -6143; /* Emin */
97- context->round = DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
98- context->traps = 0; /* no traps set */
99- context->clamp = 1; /* clamp exponents */
100-#if DECSUBSET
101- context->extended = 1; /* set */
102-#endif
129+ context->digits=34; /* digits */
130+ context->emax=6144; /* Emax */
131+ context->emin=-6143; /* Emin */
132+ context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
133+ context->traps=0; /* no traps set */
134+ context->clamp=1; /* clamp exponents */
135+ #if DECSUBSET
136+ context->extended=1; /* set */
137+ #endif
103138 break;
104139
105- default: /* invalid Kind */
140+ default: /* invalid Kind */
106141 /* use defaults, and .. */
107- decContextSetStatus (context, DEC_Invalid_operation); /* trap */
142+ decContextSetStatus(context, DEC_Invalid_operation); /* trap */
143+ }
144+
145+ #if DECCHECK
146+ if (LITEND!=DECLITEND) {
147+ const char *adj;
148+ if (LITEND) adj="little";
149+ else adj="big";
150+ printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n",
151+ DECLITEND, adj);
108152 }
153+ #endif
154+ return context;} /* decContextDefault */
155+
156+/* ------------------------------------------------------------------ */
157+/* decContextGetRounding -- return current rounding mode */
158+/* */
159+/* context is the context structure to be queried */
160+/* returns the rounding mode */
161+/* */
162+/* No error is possible. */
163+/* ------------------------------------------------------------------ */
164+enum rounding decContextGetRounding(decContext *context) {
165+ return context->round;
166+ } /* decContextGetRounding */
167+
168+/* ------------------------------------------------------------------ */
169+/* decContextGetStatus -- return current status */
170+/* */
171+/* context is the context structure to be queried */
172+/* returns status */
173+/* */
174+/* No error is possible. */
175+/* ------------------------------------------------------------------ */
176+uInt decContextGetStatus(decContext *context) {
177+ return context->status;
178+ } /* decContextGetStatus */
179+
180+/* ------------------------------------------------------------------ */
181+/* decContextRestoreStatus -- restore bits in current status */
182+/* */
183+/* context is the context structure to be updated */
184+/* newstatus is the source for the bits to be restored */
185+/* mask indicates the bits to be restored (the status bit that */
186+/* corresponds to each 1 bit in the mask is set to the value of */
187+/* the correspnding bit in newstatus) */
188+/* returns context */
189+/* */
190+/* No error is possible. */
191+/* ------------------------------------------------------------------ */
192+decContext *decContextRestoreStatus(decContext *context,
193+ uInt newstatus, uInt mask) {
194+ context->status&=~mask; /* clear the selected bits */
195+ context->status|=(mask&newstatus); /* or in the new bits */
109196 return context;
110-} /* decContextDefault */
197+ } /* decContextRestoreStatus */
111198
112199 /* ------------------------------------------------------------------ */
113-/* decContextStatusToString -- convert status flags to a string */
114-/* */
115-/* context is a context with valid status field */
116-/* */
117-/* returns a constant string describing the condition. If multiple */
118-/* (or no) flags are set, a generic constant message is returned. */
200+/* decContextSaveStatus -- save bits in current status */
201+/* */
202+/* context is the context structure to be queried */
203+/* mask indicates the bits to be saved (the status bits that */
204+/* correspond to each 1 bit in the mask are saved) */
205+/* returns the AND of the mask and the current status */
206+/* */
207+/* No error is possible. */
119208 /* ------------------------------------------------------------------ */
120-const char *
121-decContextStatusToString (const decContext * context)
122-{
123- Int status = context->status;
124- if (status == DEC_Conversion_syntax)
125- return DEC_Condition_CS;
126- if (status == DEC_Division_by_zero)
127- return DEC_Condition_DZ;
128- if (status == DEC_Division_impossible)
129- return DEC_Condition_DI;
130- if (status == DEC_Division_undefined)
131- return DEC_Condition_DU;
132- if (status == DEC_Inexact)
133- return DEC_Condition_IE;
134- if (status == DEC_Insufficient_storage)
135- return DEC_Condition_IS;
136- if (status == DEC_Invalid_context)
137- return DEC_Condition_IC;
138- if (status == DEC_Invalid_operation)
139- return DEC_Condition_IO;
140-#if DECSUBSET
141- if (status == DEC_Lost_digits)
142- return DEC_Condition_LD;
143-#endif
144- if (status == DEC_Overflow)
145- return DEC_Condition_OV;
146- if (status == DEC_Clamped)
147- return DEC_Condition_PA;
148- if (status == DEC_Rounded)
149- return DEC_Condition_RO;
150- if (status == DEC_Subnormal)
151- return DEC_Condition_SU;
152- if (status == DEC_Underflow)
153- return DEC_Condition_UN;
154- if (status == 0)
155- return DEC_Condition_ZE;
156- return DEC_Condition_MU; /* Multiple errors */
157-} /* decContextStatusToString */
158-
159-/* ------------------------------------------------------------------ */
160-/* decContextSetStatusFromString -- set status from a string */
161-/* */
162-/* context is the controlling context */
209+uInt decContextSaveStatus(decContext *context, uInt mask) {
210+ return context->status&mask;
211+ } /* decContextSaveStatus */
212+
213+/* ------------------------------------------------------------------ */
214+/* decContextSetRounding -- set current rounding mode */
215+/* */
216+/* context is the context structure to be updated */
217+/* newround is the value which will replace the current mode */
218+/* returns context */
219+/* */
220+/* No error is possible. */
221+/* ------------------------------------------------------------------ */
222+decContext *decContextSetRounding(decContext *context,
223+ enum rounding newround) {
224+ context->round=newround;
225+ return context;
226+ } /* decContextSetRounding */
227+
228+/* ------------------------------------------------------------------ */
229+/* decContextSetStatus -- set status and raise trap if appropriate */
230+/* */
231+/* context is the context structure to be updated */
232+/* status is the DEC_ exception code */
233+/* returns the context structure */
234+/* */
235+/* Control may never return from this routine, if there is a signal */
236+/* handler and it takes a long jump. */
237+/* ------------------------------------------------------------------ */
238+decContext * decContextSetStatus(decContext *context, uInt status) {
239+ context->status|=status;
240+ if (status & context->traps) raise(SIGFPE);
241+ return context;} /* decContextSetStatus */
242+
243+/* ------------------------------------------------------------------ */
244+/* decContextSetStatusFromString -- set status from a string + trap */
245+/* */
246+/* context is the context structure to be updated */
163247 /* string is a string exactly equal to one that might be returned */
164-/* by decContextStatusToString */
165-/* */
248+/* by decContextStatusToString */
249+/* */
166250 /* The status bit corresponding to the string is set, and a trap */
167-/* is raised if appropriate. */
168-/* */
251+/* is raised if appropriate. */
252+/* */
169253 /* returns the context structure, unless the string is equal to */
170254 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
171-/* returned. */
172-/* ------------------------------------------------------------------ */
173-decContext *
174-decContextSetStatusFromString (decContext * context, const char *string)
175-{
176- if (strcmp (string, DEC_Condition_CS) == 0)
177- return decContextSetStatus (context, DEC_Conversion_syntax);
178- if (strcmp (string, DEC_Condition_DZ) == 0)
179- return decContextSetStatus (context, DEC_Division_by_zero);
180- if (strcmp (string, DEC_Condition_DI) == 0)
181- return decContextSetStatus (context, DEC_Division_impossible);
182- if (strcmp (string, DEC_Condition_DU) == 0)
183- return decContextSetStatus (context, DEC_Division_undefined);
184- if (strcmp (string, DEC_Condition_IE) == 0)
185- return decContextSetStatus (context, DEC_Inexact);
186- if (strcmp (string, DEC_Condition_IS) == 0)
187- return decContextSetStatus (context, DEC_Insufficient_storage);
188- if (strcmp (string, DEC_Condition_IC) == 0)
189- return decContextSetStatus (context, DEC_Invalid_context);
190- if (strcmp (string, DEC_Condition_IO) == 0)
191- return decContextSetStatus (context, DEC_Invalid_operation);
192-#if DECSUBSET
193- if (strcmp (string, DEC_Condition_LD) == 0)
194- return decContextSetStatus (context, DEC_Lost_digits);
195-#endif
196- if (strcmp (string, DEC_Condition_OV) == 0)
197- return decContextSetStatus (context, DEC_Overflow);
198- if (strcmp (string, DEC_Condition_PA) == 0)
199- return decContextSetStatus (context, DEC_Clamped);
200- if (strcmp (string, DEC_Condition_RO) == 0)
201- return decContextSetStatus (context, DEC_Rounded);
202- if (strcmp (string, DEC_Condition_SU) == 0)
203- return decContextSetStatus (context, DEC_Subnormal);
204- if (strcmp (string, DEC_Condition_UN) == 0)
205- return decContextSetStatus (context, DEC_Underflow);
206- if (strcmp (string, DEC_Condition_ZE) == 0)
255+/* returned. */
256+/* ------------------------------------------------------------------ */
257+decContext * decContextSetStatusFromString(decContext *context,
258+ const char *string) {
259+ if (strcmp(string, DEC_Condition_CS)==0)
260+ return decContextSetStatus(context, DEC_Conversion_syntax);
261+ if (strcmp(string, DEC_Condition_DZ)==0)
262+ return decContextSetStatus(context, DEC_Division_by_zero);
263+ if (strcmp(string, DEC_Condition_DI)==0)
264+ return decContextSetStatus(context, DEC_Division_impossible);
265+ if (strcmp(string, DEC_Condition_DU)==0)
266+ return decContextSetStatus(context, DEC_Division_undefined);
267+ if (strcmp(string, DEC_Condition_IE)==0)
268+ return decContextSetStatus(context, DEC_Inexact);
269+ if (strcmp(string, DEC_Condition_IS)==0)
270+ return decContextSetStatus(context, DEC_Insufficient_storage);
271+ if (strcmp(string, DEC_Condition_IC)==0)
272+ return decContextSetStatus(context, DEC_Invalid_context);
273+ if (strcmp(string, DEC_Condition_IO)==0)
274+ return decContextSetStatus(context, DEC_Invalid_operation);
275+ #if DECSUBSET
276+ if (strcmp(string, DEC_Condition_LD)==0)
277+ return decContextSetStatus(context, DEC_Lost_digits);
278+ #endif
279+ if (strcmp(string, DEC_Condition_OV)==0)
280+ return decContextSetStatus(context, DEC_Overflow);
281+ if (strcmp(string, DEC_Condition_PA)==0)
282+ return decContextSetStatus(context, DEC_Clamped);
283+ if (strcmp(string, DEC_Condition_RO)==0)
284+ return decContextSetStatus(context, DEC_Rounded);
285+ if (strcmp(string, DEC_Condition_SU)==0)
286+ return decContextSetStatus(context, DEC_Subnormal);
287+ if (strcmp(string, DEC_Condition_UN)==0)
288+ return decContextSetStatus(context, DEC_Underflow);
289+ if (strcmp(string, DEC_Condition_ZE)==0)
207290 return context;
208- return NULL; /* Multiple status, or unknown */
209-} /* decContextSetStatusFromString */
291+ return NULL; /* Multiple status, or unknown */
292+ } /* decContextSetStatusFromString */
210293
211294 /* ------------------------------------------------------------------ */
212-/* decContextSetStatus -- set status and raise trap if appropriate */
213-/* */
214-/* context is the controlling context */
215-/* status is the DEC_ exception code */
216-/* returns the context structure */
217-/* */
218-/* Control may never return from this routine, if there is a signal */
219-/* handler and it takes a long jump. */
220-/* ------------------------------------------------------------------ */
221-decContext *
222-decContextSetStatus (decContext * context, uInt status)
223-{
224- context->status |= status;
225- if (status & context->traps)
226- raise (SIGFPE);
295+/* decContextSetStatusFromStringQuiet -- set status from a string */
296+/* */
297+/* context is the context structure to be updated */
298+/* string is a string exactly equal to one that might be returned */
299+/* by decContextStatusToString */
300+/* */
301+/* The status bit corresponding to the string is set; no trap is */
302+/* raised. */
303+/* */
304+/* returns the context structure, unless the string is equal to */
305+/* DEC_Condition_MU or is not recognized. In these cases NULL is */
306+/* returned. */
307+/* ------------------------------------------------------------------ */
308+decContext * decContextSetStatusFromStringQuiet(decContext *context,
309+ const char *string) {
310+ if (strcmp(string, DEC_Condition_CS)==0)
311+ return decContextSetStatusQuiet(context, DEC_Conversion_syntax);
312+ if (strcmp(string, DEC_Condition_DZ)==0)
313+ return decContextSetStatusQuiet(context, DEC_Division_by_zero);
314+ if (strcmp(string, DEC_Condition_DI)==0)
315+ return decContextSetStatusQuiet(context, DEC_Division_impossible);
316+ if (strcmp(string, DEC_Condition_DU)==0)
317+ return decContextSetStatusQuiet(context, DEC_Division_undefined);
318+ if (strcmp(string, DEC_Condition_IE)==0)
319+ return decContextSetStatusQuiet(context, DEC_Inexact);
320+ if (strcmp(string, DEC_Condition_IS)==0)
321+ return decContextSetStatusQuiet(context, DEC_Insufficient_storage);
322+ if (strcmp(string, DEC_Condition_IC)==0)
323+ return decContextSetStatusQuiet(context, DEC_Invalid_context);
324+ if (strcmp(string, DEC_Condition_IO)==0)
325+ return decContextSetStatusQuiet(context, DEC_Invalid_operation);
326+ #if DECSUBSET
327+ if (strcmp(string, DEC_Condition_LD)==0)
328+ return decContextSetStatusQuiet(context, DEC_Lost_digits);
329+ #endif
330+ if (strcmp(string, DEC_Condition_OV)==0)
331+ return decContextSetStatusQuiet(context, DEC_Overflow);
332+ if (strcmp(string, DEC_Condition_PA)==0)
333+ return decContextSetStatusQuiet(context, DEC_Clamped);
334+ if (strcmp(string, DEC_Condition_RO)==0)
335+ return decContextSetStatusQuiet(context, DEC_Rounded);
336+ if (strcmp(string, DEC_Condition_SU)==0)
337+ return decContextSetStatusQuiet(context, DEC_Subnormal);
338+ if (strcmp(string, DEC_Condition_UN)==0)
339+ return decContextSetStatusQuiet(context, DEC_Underflow);
340+ if (strcmp(string, DEC_Condition_ZE)==0)
341+ return context;
342+ return NULL; /* Multiple status, or unknown */
343+ } /* decContextSetStatusFromStringQuiet */
344+
345+/* ------------------------------------------------------------------ */
346+/* decContextSetStatusQuiet -- set status without trap */
347+/* */
348+/* context is the context structure to be updated */
349+/* status is the DEC_ exception code */
350+/* returns the context structure */
351+/* */
352+/* No error is possible. */
353+/* ------------------------------------------------------------------ */
354+decContext * decContextSetStatusQuiet(decContext *context, uInt status) {
355+ context->status|=status;
356+ return context;} /* decContextSetStatusQuiet */
357+
358+/* ------------------------------------------------------------------ */
359+/* decContextStatusToString -- convert status flags to a string */
360+/* */
361+/* context is a context with valid status field */
362+/* */
363+/* returns a constant string describing the condition. If multiple */
364+/* (or no) flags are set, a generic constant message is returned. */
365+/* ------------------------------------------------------------------ */
366+const char *decContextStatusToString(const decContext *context) {
367+ Int status=context->status;
368+
369+ /* test the five IEEE first, as some of the others are ambiguous when */
370+ /* DECEXTFLAG=0 */
371+ if (status==DEC_Invalid_operation ) return DEC_Condition_IO;
372+ if (status==DEC_Division_by_zero ) return DEC_Condition_DZ;
373+ if (status==DEC_Overflow ) return DEC_Condition_OV;
374+ if (status==DEC_Underflow ) return DEC_Condition_UN;
375+ if (status==DEC_Inexact ) return DEC_Condition_IE;
376+
377+ if (status==DEC_Division_impossible ) return DEC_Condition_DI;
378+ if (status==DEC_Division_undefined ) return DEC_Condition_DU;
379+ if (status==DEC_Rounded ) return DEC_Condition_RO;
380+ if (status==DEC_Clamped ) return DEC_Condition_PA;
381+ if (status==DEC_Subnormal ) return DEC_Condition_SU;
382+ if (status==DEC_Conversion_syntax ) return DEC_Condition_CS;
383+ if (status==DEC_Insufficient_storage ) return DEC_Condition_IS;
384+ if (status==DEC_Invalid_context ) return DEC_Condition_IC;
385+ #if DECSUBSET
386+ if (status==DEC_Lost_digits ) return DEC_Condition_LD;
387+ #endif
388+ if (status==0 ) return DEC_Condition_ZE;
389+ return DEC_Condition_MU; /* Multiple errors */
390+ } /* decContextStatusToString */
391+
392+/* ------------------------------------------------------------------ */
393+/* decContextTestSavedStatus -- test bits in saved status */
394+/* */
395+/* oldstatus is the status word to be tested */
396+/* mask indicates the bits to be tested (the oldstatus bits that */
397+/* correspond to each 1 bit in the mask are tested) */
398+/* returns 1 if any of the tested bits are 1, or 0 otherwise */
399+/* */
400+/* No error is possible. */
401+/* ------------------------------------------------------------------ */
402+uInt decContextTestSavedStatus(uInt oldstatus, uInt mask) {
403+ return (oldstatus&mask)!=0;
404+ } /* decContextTestSavedStatus */
405+
406+/* ------------------------------------------------------------------ */
407+/* decContextTestStatus -- test bits in current status */
408+/* */
409+/* context is the context structure to be updated */
410+/* mask indicates the bits to be tested (the status bits that */
411+/* correspond to each 1 bit in the mask are tested) */
412+/* returns 1 if any of the tested bits are 1, or 0 otherwise */
413+/* */
414+/* No error is possible. */
415+/* ------------------------------------------------------------------ */
416+uInt decContextTestStatus(decContext *context, uInt mask) {
417+ return (context->status&mask)!=0;
418+ } /* decContextTestStatus */
419+
420+/* ------------------------------------------------------------------ */
421+/* decContextZeroStatus -- clear all status bits */
422+/* */
423+/* context is the context structure to be updated */
424+/* returns context */
425+/* */
426+/* No error is possible. */
427+/* ------------------------------------------------------------------ */
428+decContext *decContextZeroStatus(decContext *context) {
429+ context->status=0;
227430 return context;
228-} /* decContextSetStatus */
431+ } /* decContextZeroStatus */
432+
--- a/libdecnumber/decContext.h
+++ b/libdecnumber/decContext.h
@@ -1,5 +1,5 @@
1-/* Decimal Context module header for the decNumber C Library
2- Copyright (C) 2005, 2006 Free Software Foundation, Inc.
1+/* Decimal context header module for the decNumber C Library.
2+ Copyright (C) 2005, 2007 Free Software Foundation, Inc.
33 Contributed by IBM Corporation. Author Mike Cowlishaw.
44
55 This file is part of GCC.
@@ -29,159 +29,230 @@
2929 02110-1301, USA. */
3030
3131 /* ------------------------------------------------------------------ */
32-/* */
33-/* Context must always be set correctly: */
34-/* */
35-/* digits -- must be in the range 1 through 999999999 */
36-/* emax -- must be in the range 0 through 999999999 */
37-/* emin -- must be in the range 0 through -999999999 */
38-/* round -- must be one of the enumerated rounding modes */
39-/* traps -- only defined bits may be set */
40-/* status -- [any bits may be cleared, but not set, by user] */
41-/* clamp -- must be either 0 or 1 */
32+/* Decimal Context module header */
33+/* ------------------------------------------------------------------ */
34+/* */
35+/* Context variables must always have valid values: */
36+/* */
37+/* status -- [any bits may be cleared, but not set, by user] */
38+/* round -- must be one of the enumerated rounding modes */
39+/* */
40+/* The following variables are implied for fixed size formats (i.e., */
41+/* they are ignored) but should still be set correctly in case used */
42+/* with decNumber functions: */
43+/* */
44+/* clamp -- must be either 0 or 1 */
45+/* digits -- must be in the range 1 through 999999999 */
46+/* emax -- must be in the range 0 through 999999999 */
47+/* emin -- must be in the range 0 through -999999999 */
4248 /* extended -- must be either 0 or 1 [present only if DECSUBSET] */
43-/* */
49+/* traps -- only defined bits may be set */
50+/* */
4451 /* ------------------------------------------------------------------ */
4552
4653 #if !defined(DECCONTEXT)
47-#define DECCONTEXT
48-#define DECCNAME "decContext" /* Short name */
49-#define DECCFULLNAME "Decimal Context Descriptor" /* Verbose name */
50-#define DECCAUTHOR "Mike Cowlishaw" /* Who to blame */
51-
52-#include "gstdint.h" /* C99 standard integers */
53-#include <signal.h> /* for traps */
54-
55-
56- /* Conditional code flag -- set this to 0 for best performance */
57-#define DECSUBSET 0 /* 1 to enable subset arithmetic */
58-
59- /* Context for operations, with associated constants */
60-enum rounding
61-{
62- DEC_ROUND_CEILING, /* round towards +infinity */
63- DEC_ROUND_UP, /* round away from 0 */
64- DEC_ROUND_HALF_UP, /* 0.5 rounds up */
65- DEC_ROUND_HALF_EVEN, /* 0.5 rounds to nearest even */
66- DEC_ROUND_HALF_DOWN, /* 0.5 rounds down */
67- DEC_ROUND_DOWN, /* round towards 0 (truncate) */
68- DEC_ROUND_FLOOR, /* round towards -infinity */
69- DEC_ROUND_MAX /* enum must be less than this */
70-};
71-
72-typedef struct
73-{
74- int32_t digits; /* working precision */
75- int32_t emax; /* maximum positive exponent */
76- int32_t emin; /* minimum negative exponent */
77- enum rounding round; /* rounding mode */
78- uint32_t traps; /* trap-enabler flags */
79- uint32_t status; /* status flags */
80- uint8_t clamp; /* flag: apply IEEE exponent clamp */
81-#if DECSUBSET
82- uint8_t extended; /* flag: special-values allowed */
83-#endif
84-} decContext;
85-
86- /* Maxima and Minima */
87-#define DEC_MAX_DIGITS 999999999
88-#define DEC_MIN_DIGITS 1
89-#define DEC_MAX_EMAX 999999999
90-#define DEC_MIN_EMAX 0
91-#define DEC_MAX_EMIN 0
92-#define DEC_MIN_EMIN -999999999
93-
94- /* Trap-enabler and Status flags (exceptional conditions), and their names */
95- /* Top byte is reserved for internal use */
96-#define DEC_Conversion_syntax 0x00000001
97-#define DEC_Division_by_zero 0x00000002
98-#define DEC_Division_impossible 0x00000004
99-#define DEC_Division_undefined 0x00000008
100-#define DEC_Insufficient_storage 0x00000010 /* [used if malloc fails] */
101-#define DEC_Inexact 0x00000020
102-#define DEC_Invalid_context 0x00000040
103-#define DEC_Invalid_operation 0x00000080
104-#if DECSUBSET
105-#define DEC_Lost_digits 0x00000100
106-#endif
107-#define DEC_Overflow 0x00000200
108-#define DEC_Clamped 0x00000400
109-#define DEC_Rounded 0x00000800
110-#define DEC_Subnormal 0x00001000
111-#define DEC_Underflow 0x00002000
112-
113- /* IEEE 854 groupings for the flags */
114- /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal are */
115- /* not in IEEE 854] */
116-#define DEC_IEEE_854_Division_by_zero (DEC_Division_by_zero)
117-#if DECSUBSET
118-#define DEC_IEEE_854_Inexact (DEC_Inexact | DEC_Lost_digits)
119-#else
120-#define DEC_IEEE_854_Inexact (DEC_Inexact)
121-#endif
122-#define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax | \
123- DEC_Division_impossible | \
124- DEC_Division_undefined | \
125- DEC_Insufficient_storage | \
126- DEC_Invalid_context | \
127- DEC_Invalid_operation)
128-#define DEC_IEEE_854_Overflow (DEC_Overflow)
129-#define DEC_IEEE_854_Underflow (DEC_Underflow)
130-
131- /* flags which are normally errors (results are qNaN, infinite, or 0) */
132-#define DEC_Errors (DEC_IEEE_854_Division_by_zero | \
133- DEC_IEEE_854_Invalid_operation | \
134- DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
135- /* flags which cause a result to become qNaN */
136-#define DEC_NaNs DEC_IEEE_854_Invalid_operation
137-
138- /* flags which are normally for information only (have finite results) */
139-#if DECSUBSET
140-#define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact \
141- | DEC_Lost_digits)
142-#else
143-#define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
144-#endif
54+ #define DECCONTEXT
55+ #define DECCNAME "decContext" /* Short name */
56+ #define DECCFULLNAME "Decimal Context Descriptor" /* Verbose name */
57+ #define DECCAUTHOR "Mike Cowlishaw" /* Who to blame */
14558
146- /* name strings for the exceptional conditions */
147-
148-#define DEC_Condition_CS "Conversion syntax"
149-#define DEC_Condition_DZ "Division by zero"
150-#define DEC_Condition_DI "Division impossible"
151-#define DEC_Condition_DU "Division undefined"
152-#define DEC_Condition_IE "Inexact"
153-#define DEC_Condition_IS "Insufficient storage"
154-#define DEC_Condition_IC "Invalid context"
155-#define DEC_Condition_IO "Invalid operation"
156-#if DECSUBSET
157-#define DEC_Condition_LD "Lost digits"
158-#endif
159-#define DEC_Condition_OV "Overflow"
160-#define DEC_Condition_PA "Clamped"
161-#define DEC_Condition_RO "Rounded"
162-#define DEC_Condition_SU "Subnormal"
163-#define DEC_Condition_UN "Underflow"
164-#define DEC_Condition_ZE "No status"
165-#define DEC_Condition_MU "Multiple status"
166-#define DEC_Condition_Length 21 /* length of the longest string, */
167- /* including terminator */
168-
169- /* Initialization descriptors, used by decContextDefault */
170-#define DEC_INIT_BASE 0
171-#define DEC_INIT_DECIMAL32 32
172-#define DEC_INIT_DECIMAL64 64
173-#define DEC_INIT_DECIMAL128 128
174-
175- /* decContext routines */
176-#ifdef IN_LIBGCC2
177-#define decContextDefault __decContextDefault
178-#define decContextSetStatus __decContextSetStatus
179-#define decContextStatusToString __decContextStatusToString
180-#define decContextSetStatusFromString __decContextSetStatusFromString
181-#endif
182-decContext *decContextDefault (decContext *, int32_t);
183-decContext *decContextSetStatus (decContext *, uint32_t);
184-const char *decContextStatusToString (const decContext *);
185-decContext *decContextSetStatusFromString (decContext *, const char *);
59+ #include "gstdint.h" /* C99 standard integers */
60+ #include <stdio.h> /* for printf, etc. */
61+ #include <signal.h> /* for traps */
62+
63+ /* Extended flags setting -- set this to 0 to use only IEEE flags */
64+ #define DECEXTFLAG 1 /* 1=enable extended flags */
65+
66+ /* Conditional code flag -- set this to 0 for best performance */
67+ #define DECSUBSET 0 /* 1=enable subset arithmetic */
68+
69+ /* Context for operations, with associated constants */
70+ enum rounding {
71+ DEC_ROUND_CEILING, /* round towards +infinity */
72+ DEC_ROUND_UP, /* round away from 0 */
73+ DEC_ROUND_HALF_UP, /* 0.5 rounds up */
74+ DEC_ROUND_HALF_EVEN, /* 0.5 rounds to nearest even */
75+ DEC_ROUND_HALF_DOWN, /* 0.5 rounds down */
76+ DEC_ROUND_DOWN, /* round towards 0 (truncate) */
77+ DEC_ROUND_FLOOR, /* round towards -infinity */
78+ DEC_ROUND_05UP, /* round for reround */
79+ DEC_ROUND_MAX /* enum must be less than this */
80+ };
81+ #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
82+
83+ typedef struct {
84+ int32_t digits; /* working precision */
85+ int32_t emax; /* maximum positive exponent */
86+ int32_t emin; /* minimum negative exponent */
87+ enum rounding round; /* rounding mode */
88+ uint32_t traps; /* trap-enabler flags */
89+ uint32_t status; /* status flags */
90+ uint8_t clamp; /* flag: apply IEEE exponent clamp */
91+ #if DECSUBSET
92+ uint8_t extended; /* flag: special-values allowed */
93+ #endif
94+ } decContext;
95+
96+ /* Maxima and Minima for context settings */
97+ #define DEC_MAX_DIGITS 999999999
98+ #define DEC_MIN_DIGITS 1
99+ #define DEC_MAX_EMAX 999999999
100+ #define DEC_MIN_EMAX 0
101+ #define DEC_MAX_EMIN 0
102+ #define DEC_MIN_EMIN -999999999
103+ #define DEC_MAX_MATH 999999 /* max emax, etc., for math funcs. */
104+
105+ /* Classifications for decimal numbers, aligned with 754r (note */
106+ /* that 'normal' and 'subnormal' are meaningful only with a */
107+ /* decContext or a fixed size format). */
108+ enum decClass {
109+ DEC_CLASS_SNAN,
110+ DEC_CLASS_QNAN,
111+ DEC_CLASS_NEG_INF,
112+ DEC_CLASS_NEG_NORMAL,
113+ DEC_CLASS_NEG_SUBNORMAL,
114+ DEC_CLASS_NEG_ZERO,
115+ DEC_CLASS_POS_ZERO,
116+ DEC_CLASS_POS_SUBNORMAL,
117+ DEC_CLASS_POS_NORMAL,
118+ DEC_CLASS_POS_INF
119+ };
120+ /* Strings for the decClasses */
121+ #define DEC_ClassString_SN "sNaN"
122+ #define DEC_ClassString_QN "NaN"
123+ #define DEC_ClassString_NI "-Infinity"
124+ #define DEC_ClassString_NN "-Normal"
125+ #define DEC_ClassString_NS "-Subnormal"
126+ #define DEC_ClassString_NZ "-Zero"
127+ #define DEC_ClassString_PZ "+Zero"
128+ #define DEC_ClassString_PS "+Subnormal"
129+ #define DEC_ClassString_PN "+Normal"
130+ #define DEC_ClassString_PI "+Infinity"
131+ #define DEC_ClassString_UN "Invalid"
132+
133+ /* Trap-enabler and Status flags (exceptional conditions), and */
134+ /* their names. The top byte is reserved for internal use */
135+ #if DECEXTFLAG
136+ /* Extended flags */
137+ #define DEC_Conversion_syntax 0x00000001
138+ #define DEC_Division_by_zero 0x00000002
139+ #define DEC_Division_impossible 0x00000004
140+ #define DEC_Division_undefined 0x00000008
141+ #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */
142+ #define DEC_Inexact 0x00000020
143+ #define DEC_Invalid_context 0x00000040
144+ #define DEC_Invalid_operation 0x00000080
145+ #if DECSUBSET
146+ #define DEC_Lost_digits 0x00000100
147+ #endif
148+ #define DEC_Overflow 0x00000200
149+ #define DEC_Clamped 0x00000400
150+ #define DEC_Rounded 0x00000800
151+ #define DEC_Subnormal 0x00001000
152+ #define DEC_Underflow 0x00002000
153+ #else
154+ /* IEEE flags only */
155+ #define DEC_Conversion_syntax 0x00000010
156+ #define DEC_Division_by_zero 0x00000002
157+ #define DEC_Division_impossible 0x00000010
158+ #define DEC_Division_undefined 0x00000010
159+ #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */
160+ #define DEC_Inexact 0x00000001
161+ #define DEC_Invalid_context 0x00000010
162+ #define DEC_Invalid_operation 0x00000010
163+ #if DECSUBSET
164+ #define DEC_Lost_digits 0x00000000
165+ #endif
166+ #define DEC_Overflow 0x00000008
167+ #define DEC_Clamped 0x00000000
168+ #define DEC_Rounded 0x00000000
169+ #define DEC_Subnormal 0x00000000
170+ #define DEC_Underflow 0x00000004
171+ #endif
172+
173+ /* IEEE 854 groupings for the flags */
174+ /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal */
175+ /* are not in IEEE 854] */
176+ #define DEC_IEEE_854_Division_by_zero (DEC_Division_by_zero)
177+ #if DECSUBSET
178+ #define DEC_IEEE_854_Inexact (DEC_Inexact | DEC_Lost_digits)
179+ #else
180+ #define DEC_IEEE_854_Inexact (DEC_Inexact)
181+ #endif
182+ #define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax | \
183+ DEC_Division_impossible | \
184+ DEC_Division_undefined | \
185+ DEC_Insufficient_storage | \
186+ DEC_Invalid_context | \
187+ DEC_Invalid_operation)
188+ #define DEC_IEEE_854_Overflow (DEC_Overflow)
189+ #define DEC_IEEE_854_Underflow (DEC_Underflow)
190+
191+ /* flags which are normally errors (result is qNaN, infinite, or 0) */
192+ #define DEC_Errors (DEC_IEEE_854_Division_by_zero | \
193+ DEC_IEEE_854_Invalid_operation | \
194+ DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
195+ /* flags which cause a result to become qNaN */
196+ #define DEC_NaNs DEC_IEEE_854_Invalid_operation
197+
198+ /* flags which are normally for information only (finite results) */
199+ #if DECSUBSET
200+ #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact \
201+ | DEC_Lost_digits)
202+ #else
203+ #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
204+ #endif
205+
206+ /* Name strings for the exceptional conditions */
207+ #define DEC_Condition_CS "Conversion syntax"
208+ #define DEC_Condition_DZ "Division by zero"
209+ #define DEC_Condition_DI "Division impossible"
210+ #define DEC_Condition_DU "Division undefined"
211+ #define DEC_Condition_IE "Inexact"
212+ #define DEC_Condition_IS "Insufficient storage"
213+ #define DEC_Condition_IC "Invalid context"
214+ #define DEC_Condition_IO "Invalid operation"
215+ #if DECSUBSET
216+ #define DEC_Condition_LD "Lost digits"
217+ #endif
218+ #define DEC_Condition_OV "Overflow"
219+ #define DEC_Condition_PA "Clamped"
220+ #define DEC_Condition_RO "Rounded"
221+ #define DEC_Condition_SU "Subnormal"
222+ #define DEC_Condition_UN "Underflow"
223+ #define DEC_Condition_ZE "No status"
224+ #define DEC_Condition_MU "Multiple status"
225+ #define DEC_Condition_Length 21 /* length of the longest string, */
226+ /* including terminator */
227+
228+ /* Initialization descriptors, used by decContextDefault */
229+ #define DEC_INIT_BASE 0
230+ #define DEC_INIT_DECIMAL32 32
231+ #define DEC_INIT_DECIMAL64 64
232+ #define DEC_INIT_DECIMAL128 128
233+ /* Synonyms */
234+ #define DEC_INIT_DECSINGLE DEC_INIT_DECIMAL32
235+ #define DEC_INIT_DECDOUBLE DEC_INIT_DECIMAL64
236+ #define DEC_INIT_DECQUAD DEC_INIT_DECIMAL128
237+
238+ /* decContext routines */
239+
240+ #include "decContextSymbols.h"
241+
242+ extern decContext * decContextClearStatus(decContext *, uint32_t);
243+ extern decContext * decContextDefault(decContext *, int32_t);
244+ extern enum rounding decContextGetRounding(decContext *);
245+ extern uint32_t decContextGetStatus(decContext *);
246+ extern decContext * decContextRestoreStatus(decContext *, uint32_t, uint32_t);
247+ extern uint32_t decContextSaveStatus(decContext *, uint32_t);
248+ extern decContext * decContextSetRounding(decContext *, enum rounding);
249+ extern decContext * decContextSetStatus(decContext *, uint32_t);
250+ extern decContext * decContextSetStatusFromString(decContext *, const char *);
251+ extern decContext * decContextSetStatusFromStringQuiet(decContext *, const char *);
252+ extern decContext * decContextSetStatusQuiet(decContext *, uint32_t);
253+ extern const char * decContextStatusToString(const decContext *);
254+ extern uint32_t decContextTestSavedStatus(uint32_t, uint32_t);
255+ extern uint32_t decContextTestStatus(decContext *, uint32_t);
256+ extern decContext * decContextZeroStatus(decContext *);
186257
187258 #endif
--- /dev/null
+++ b/libdecnumber/decContextSymbols.h
@@ -0,0 +1,22 @@
1+#if !defined(DECCONTEXTSYMBOLS)
2+#define DECCONTEXTSYMBOLS
3+
4+#ifdef IN_LIBGCC2
5+#define decContextClearStatus __decContextClearStatus
6+#define decContextDefault __decContextDefault
7+#define decContextGetRounding __decContextGetRounding
8+#define decContextGetStatus __decContextGetStatus
9+#define decContextRestoreStatus __decContextRestoreStatus
10+#define decContextSaveStatus __decContextSaveStatus
11+#define decContextSetRounding __decContextSetRounding
12+#define decContextSetStatus __decContextSetStatus
13+#define decContextSetStatusFromString __decContextSetStatusFromString
14+#define decContextSetStatusFromStringQuiet __decContextSetStatusFromStringQuiet
15+#define decContextSetStatusQuiet __decContextSetStatusQuiet
16+#define decContextStatusToString __decContextStatusToString
17+#define decContextTestSavedStatus __decContextTestSavedStatus
18+#define decContextTestStatus __decContextTestStatus
19+#define decContextZeroStatus __decContextZeroStatus
20+#endif
21+
22+#endif
--- a/libdecnumber/decDPD.h
+++ b/libdecnumber/decDPD.h
@@ -1,5 +1,5 @@
1-/* Binary Coded Decimal <--> Densely Packed Decimal lookup tables.
2- Copyright (C) 2005 Free Software Foundation, Inc.
1+/* Conversion lookup tables for the decNumber C Library.
2+ Copyright (C) 2007 Free Software Foundation, Inc.
33 Contributed by IBM Corporation. Author Mike Cowlishaw.
44
55 This file is part of GCC.
@@ -29,506 +29,1186 @@
2929 02110-1301, USA. */
3030
3131 /* ------------------------------------------------------------------------ */
32-/* For details, see: http://www2.hursley.ibm.com/decimal/DPDecimal.html */
33-/* */
34-/* This include file defines conversion tables for DPD, as follows. */
35-/* */
36-/* uint16_t BCD2DPD[2458]; // BCD -> DPD (0x999 => 2457) */
37-/* uint16_t DPD2BCD[1024]; // DPD -> BCD (0x3FF => 0x999) */
38-/* uint16_t BIN2DPD[1000]; // BIN -> DPD (999 => 2457) */
39-/* uint16_t DPD2BIN[1024]; // DPD -> BIN (0x3FF => 999) */
40-/* */
32+/* Binary Coded Decimal and Densely Packed Decimal conversion lookup tables */
33+/* [Automatically generated -- do not edit. 2007.05.05] */
34+/* ------------------------------------------------------------------------ */
35+/* ------------------------------------------------------------------------ */
36+/* For details, see: http://www2.hursley.ibm.com/decimal/DPDecimal.html */
37+/* */
38+/* This include file defines several DPD and BCD conversion tables: */
39+/* */
40+/* uint16_t BCD2DPD[2458]; -- BCD -> DPD (0x999 => 2457) */
41+/* uint16_t BIN2DPD[1000]; -- Bin -> DPD (999 => 2457) */
42+/* uint8_t BIN2CHAR[4001]; -- Bin -> CHAR (999 => '\3' '9' '9' '9') */
43+/* uint8_t BIN2BCD8[4000]; -- Bin -> bytes (999 => 9 9 9 3) */
44+/* uint16_t DPD2BCD[1024]; -- DPD -> BCD (0x3FF => 0x999) */
45+/* uint16_t DPD2BIN[1024]; -- DPD -> BIN (0x3FF => 999) */
46+/* uint32_t DPD2BINK[1024]; -- DPD -> BIN * 1000 (0x3FF => 999000) */
47+/* uint32_t DPD2BINM[1024]; -- DPD -> BIN * 1E+6 (0x3FF => 999000000) */
48+/* uint8_t DPD2BCD8[4096]; -- DPD -> bytes (x3FF => 9 9 9 3) */
49+/* */
4150 /* In all cases the result (10 bits or 12 bits, or binary) is right-aligned */
42-/* in the table entry. */
43-/* */
44-/* To use a table, its name, prefixed with DEC_, must be defined with a */
45-/* value of 1 before this header file is included. For example: */
46-/* #define DEC_BCD2DPD 1 */
51+/* in the table entry. BIN2CHAR entries are a single byte length (0 for */
52+/* value 0) followed by three digit characters; a trailing terminator is */
53+/* included to allow 4-char moves always. BIN2BCD8 and DPD2BCD8 entries */
54+/* are similar with the three BCD8 digits followed by a one-byte length */
55+/* (again, length=0 for value 0). */
56+/* */
57+/* To use a table, its name, prefixed with DEC_, must be defined with a */
58+/* value of 1 before this header file is included. For example: */
59+/* #define DEC_BCD2DPD 1 */
60+/* This mechanism allows software to only include tables that are needed. */
4761 /* ------------------------------------------------------------------------ */
4862
49-#if DEC_BCD2DPD==1
63+#if defined(DEC_BCD2DPD) && DEC_BCD2DPD==1 && !defined(DECBCD2DPD)
64+#define DECBCD2DPD
65+
66+const uint16_t BCD2DPD[2458]={ 0, 1, 2, 3, 4, 5, 6, 7,
67+ 8, 9, 0, 0, 0, 0, 0, 0, 16, 17, 18, 19, 20,
68+ 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 32, 33,
69+ 34, 35, 36, 37, 38, 39, 40, 41, 0, 0, 0, 0, 0,
70+ 0, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 0,
71+ 0, 0, 0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72,
72+ 73, 0, 0, 0, 0, 0, 0, 80, 81, 82, 83, 84, 85,
73+ 86, 87, 88, 89, 0, 0, 0, 0, 0, 0, 96, 97, 98,
74+ 99, 100, 101, 102, 103, 104, 105, 0, 0, 0, 0, 0, 0,
75+ 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 0, 0, 0,
76+ 0, 0, 0, 10, 11, 42, 43, 74, 75, 106, 107, 78, 79,
77+ 0, 0, 0, 0, 0, 0, 26, 27, 58, 59, 90, 91, 122,
78+ 123, 94, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86+ 0, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 0, 0,
87+ 0, 0, 0, 0, 144, 145, 146, 147, 148, 149, 150, 151, 152,
88+ 153, 0, 0, 0, 0, 0, 0, 160, 161, 162, 163, 164, 165,
89+ 166, 167, 168, 169, 0, 0, 0, 0, 0, 0, 176, 177, 178,
90+ 179, 180, 181, 182, 183, 184, 185, 0, 0, 0, 0, 0, 0,
91+ 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 0, 0, 0,
92+ 0, 0, 0, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
93+ 0, 0, 0, 0, 0, 0, 224, 225, 226, 227, 228, 229, 230,
94+ 231, 232, 233, 0, 0, 0, 0, 0, 0, 240, 241, 242, 243,
95+ 244, 245, 246, 247, 248, 249, 0, 0, 0, 0, 0, 0, 138,
96+ 139, 170, 171, 202, 203, 234, 235, 206, 207, 0, 0, 0, 0,
97+ 0, 0, 154, 155, 186, 187, 218, 219, 250, 251, 222, 223, 0,
98+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 256, 257, 258,
106+ 259, 260, 261, 262, 263, 264, 265, 0, 0, 0, 0, 0, 0,
107+ 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 0, 0, 0,
108+ 0, 0, 0, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297,
109+ 0, 0, 0, 0, 0, 0, 304, 305, 306, 307, 308, 309, 310,
110+ 311, 312, 313, 0, 0, 0, 0, 0, 0, 320, 321, 322, 323,
111+ 324, 325, 326, 327, 328, 329, 0, 0, 0, 0, 0, 0, 336,
112+ 337, 338, 339, 340, 341, 342, 343, 344, 345, 0, 0, 0, 0,
113+ 0, 0, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 0,
114+ 0, 0, 0, 0, 0, 368, 369, 370, 371, 372, 373, 374, 375,
115+ 376, 377, 0, 0, 0, 0, 0, 0, 266, 267, 298, 299, 330,
116+ 331, 362, 363, 334, 335, 0, 0, 0, 0, 0, 0, 282, 283,
117+ 314, 315, 346, 347, 378, 379, 350, 351, 0, 0, 0, 0, 0,
118+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
120+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125+ 0, 0, 0, 0, 0, 0, 384, 385, 386, 387, 388, 389, 390,
126+ 391, 392, 393, 0, 0, 0, 0, 0, 0, 400, 401, 402, 403,
127+ 404, 405, 406, 407, 408, 409, 0, 0, 0, 0, 0, 0, 416,
128+ 417, 418, 419, 420, 421, 422, 423, 424, 425, 0, 0, 0, 0,
129+ 0, 0, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 0,
130+ 0, 0, 0, 0, 0, 448, 449, 450, 451, 452, 453, 454, 455,
131+ 456, 457, 0, 0, 0, 0, 0, 0, 464, 465, 466, 467, 468,
132+ 469, 470, 471, 472, 473, 0, 0, 0, 0, 0, 0, 480, 481,
133+ 482, 483, 484, 485, 486, 487, 488, 489, 0, 0, 0, 0, 0,
134+ 0, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 0, 0,
135+ 0, 0, 0, 0, 394, 395, 426, 427, 458, 459, 490, 491, 462,
136+ 463, 0, 0, 0, 0, 0, 0, 410, 411, 442, 443, 474, 475,
137+ 506, 507, 478, 479, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
139+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
140+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
141+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
143+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
144+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
145+ 0, 0, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 0,
146+ 0, 0, 0, 0, 0, 528, 529, 530, 531, 532, 533, 534, 535,
147+ 536, 537, 0, 0, 0, 0, 0, 0, 544, 545, 546, 547, 548,
148+ 549, 550, 551, 552, 553, 0, 0, 0, 0, 0, 0, 560, 561,
149+ 562, 563, 564, 565, 566, 567, 568, 569, 0, 0, 0, 0, 0,
150+ 0, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 0, 0,
151+ 0, 0, 0, 0, 592, 593, 594, 595, 596, 597, 598, 599, 600,
152+ 601, 0, 0, 0, 0, 0, 0, 608, 609, 610, 611, 612, 613,
153+ 614, 615, 616, 617, 0, 0, 0, 0, 0, 0, 624, 625, 626,
154+ 627, 628, 629, 630, 631, 632, 633, 0, 0, 0, 0, 0, 0,
155+ 522, 523, 554, 555, 586, 587, 618, 619, 590, 591, 0, 0, 0,
156+ 0, 0, 0, 538, 539, 570, 571, 602, 603, 634, 635, 606, 607,
157+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
162+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 640, 641,
165+ 642, 643, 644, 645, 646, 647, 648, 649, 0, 0, 0, 0, 0,
166+ 0, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 0, 0,
167+ 0, 0, 0, 0, 672, 673, 674, 675, 676, 677, 678, 679, 680,
168+ 681, 0, 0, 0, 0, 0, 0, 688, 689, 690, 691, 692, 693,
169+ 694, 695, 696, 697, 0, 0, 0, 0, 0, 0, 704, 705, 706,
170+ 707, 708, 709, 710, 711, 712, 713, 0, 0, 0, 0, 0, 0,
171+ 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 0, 0, 0,
172+ 0, 0, 0, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745,
173+ 0, 0, 0, 0, 0, 0, 752, 753, 754, 755, 756, 757, 758,
174+ 759, 760, 761, 0, 0, 0, 0, 0, 0, 650, 651, 682, 683,
175+ 714, 715, 746, 747, 718, 719, 0, 0, 0, 0, 0, 0, 666,
176+ 667, 698, 699, 730, 731, 762, 763, 734, 735, 0, 0, 0, 0,
177+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
180+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
181+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184+ 0, 0, 0, 0, 0, 0, 0, 768, 769, 770, 771, 772, 773,
185+ 774, 775, 776, 777, 0, 0, 0, 0, 0, 0, 784, 785, 786,
186+ 787, 788, 789, 790, 791, 792, 793, 0, 0, 0, 0, 0, 0,
187+ 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 0, 0, 0,
188+ 0, 0, 0, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825,
189+ 0, 0, 0, 0, 0, 0, 832, 833, 834, 835, 836, 837, 838,
190+ 839, 840, 841, 0, 0, 0, 0, 0, 0, 848, 849, 850, 851,
191+ 852, 853, 854, 855, 856, 857, 0, 0, 0, 0, 0, 0, 864,
192+ 865, 866, 867, 868, 869, 870, 871, 872, 873, 0, 0, 0, 0,
193+ 0, 0, 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 0,
194+ 0, 0, 0, 0, 0, 778, 779, 810, 811, 842, 843, 874, 875,
195+ 846, 847, 0, 0, 0, 0, 0, 0, 794, 795, 826, 827, 858,
196+ 859, 890, 891, 862, 863, 0, 0, 0, 0, 0, 0, 0, 0,
197+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
199+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
200+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
203+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
204+ 0, 0, 0, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905,
205+ 0, 0, 0, 0, 0, 0, 912, 913, 914, 915, 916, 917, 918,
206+ 919, 920, 921, 0, 0, 0, 0, 0, 0, 928, 929, 930, 931,
207+ 932, 933, 934, 935, 936, 937, 0, 0, 0, 0, 0, 0, 944,
208+ 945, 946, 947, 948, 949, 950, 951, 952, 953, 0, 0, 0, 0,
209+ 0, 0, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 0,
210+ 0, 0, 0, 0, 0, 976, 977, 978, 979, 980, 981, 982, 983,
211+ 984, 985, 0, 0, 0, 0, 0, 0, 992, 993, 994, 995, 996,
212+ 997, 998, 999, 1000, 1001, 0, 0, 0, 0, 0, 0, 1008, 1009,
213+ 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 0, 0, 0, 0, 0,
214+ 0, 906, 907, 938, 939, 970, 971, 1002, 1003, 974, 975, 0, 0,
215+ 0, 0, 0, 0, 922, 923, 954, 955, 986, 987, 1018, 1019, 990,
216+ 991, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
220+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
222+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12,
224+ 13, 268, 269, 524, 525, 780, 781, 46, 47, 0, 0, 0, 0,
225+ 0, 0, 28, 29, 284, 285, 540, 541, 796, 797, 62, 63, 0,
226+ 0, 0, 0, 0, 0, 44, 45, 300, 301, 556, 557, 812, 813,
227+ 302, 303, 0, 0, 0, 0, 0, 0, 60, 61, 316, 317, 572,
228+ 573, 828, 829, 318, 319, 0, 0, 0, 0, 0, 0, 76, 77,
229+ 332, 333, 588, 589, 844, 845, 558, 559, 0, 0, 0, 0, 0,
230+ 0, 92, 93, 348, 349, 604, 605, 860, 861, 574, 575, 0, 0,
231+ 0, 0, 0, 0, 108, 109, 364, 365, 620, 621, 876, 877, 814,
232+ 815, 0, 0, 0, 0, 0, 0, 124, 125, 380, 381, 636, 637,
233+ 892, 893, 830, 831, 0, 0, 0, 0, 0, 0, 14, 15, 270,
234+ 271, 526, 527, 782, 783, 110, 111, 0, 0, 0, 0, 0, 0,
235+ 30, 31, 286, 287, 542, 543, 798, 799, 126, 127, 0, 0, 0,
236+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
238+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
239+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
240+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
241+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
242+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
243+ 0, 0, 0, 0, 0, 0, 0, 0, 140, 141, 396, 397, 652,
244+ 653, 908, 909, 174, 175, 0, 0, 0, 0, 0, 0, 156, 157,
245+ 412, 413, 668, 669, 924, 925, 190, 191, 0, 0, 0, 0, 0,
246+ 0, 172, 173, 428, 429, 684, 685, 940, 941, 430, 431, 0, 0,
247+ 0, 0, 0, 0, 188, 189, 444, 445, 700, 701, 956, 957, 446,
248+ 447, 0, 0, 0, 0, 0, 0, 204, 205, 460, 461, 716, 717,
249+ 972, 973, 686, 687, 0, 0, 0, 0, 0, 0, 220, 221, 476,
250+ 477, 732, 733, 988, 989, 702, 703, 0, 0, 0, 0, 0, 0,
251+ 236, 237, 492, 493, 748, 749, 1004, 1005, 942, 943, 0, 0, 0,
252+ 0, 0, 0, 252, 253, 508, 509, 764, 765, 1020, 1021, 958, 959,
253+ 0, 0, 0, 0, 0, 0, 142, 143, 398, 399, 654, 655, 910,
254+ 911, 238, 239, 0, 0, 0, 0, 0, 0, 158, 159, 414, 415,
255+ 670, 671, 926, 927, 254, 255};
256+#endif
257+
258+#if defined(DEC_DPD2BCD) && DEC_DPD2BCD==1 && !defined(DECDPD2BCD)
259+#define DECDPD2BCD
260+
261+const uint16_t DPD2BCD[1024]={ 0, 1, 2, 3, 4, 5, 6, 7,
262+ 8, 9, 128, 129, 2048, 2049, 2176, 2177, 16, 17, 18, 19, 20,
263+ 21, 22, 23, 24, 25, 144, 145, 2064, 2065, 2192, 2193, 32, 33,
264+ 34, 35, 36, 37, 38, 39, 40, 41, 130, 131, 2080, 2081, 2056,
265+ 2057, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 146, 147,
266+ 2096, 2097, 2072, 2073, 64, 65, 66, 67, 68, 69, 70, 71, 72,
267+ 73, 132, 133, 2112, 2113, 136, 137, 80, 81, 82, 83, 84, 85,
268+ 86, 87, 88, 89, 148, 149, 2128, 2129, 152, 153, 96, 97, 98,
269+ 99, 100, 101, 102, 103, 104, 105, 134, 135, 2144, 2145, 2184, 2185,
270+ 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 150, 151, 2160,
271+ 2161, 2200, 2201, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265,
272+ 384, 385, 2304, 2305, 2432, 2433, 272, 273, 274, 275, 276, 277, 278,
273+ 279, 280, 281, 400, 401, 2320, 2321, 2448, 2449, 288, 289, 290, 291,
274+ 292, 293, 294, 295, 296, 297, 386, 387, 2336, 2337, 2312, 2313, 304,
275+ 305, 306, 307, 308, 309, 310, 311, 312, 313, 402, 403, 2352, 2353,
276+ 2328, 2329, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 388,
277+ 389, 2368, 2369, 392, 393, 336, 337, 338, 339, 340, 341, 342, 343,
278+ 344, 345, 404, 405, 2384, 2385, 408, 409, 352, 353, 354, 355, 356,
279+ 357, 358, 359, 360, 361, 390, 391, 2400, 2401, 2440, 2441, 368, 369,
280+ 370, 371, 372, 373, 374, 375, 376, 377, 406, 407, 2416, 2417, 2456,
281+ 2457, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 640, 641,
282+ 2050, 2051, 2178, 2179, 528, 529, 530, 531, 532, 533, 534, 535, 536,
283+ 537, 656, 657, 2066, 2067, 2194, 2195, 544, 545, 546, 547, 548, 549,
284+ 550, 551, 552, 553, 642, 643, 2082, 2083, 2088, 2089, 560, 561, 562,
285+ 563, 564, 565, 566, 567, 568, 569, 658, 659, 2098, 2099, 2104, 2105,
286+ 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 644, 645, 2114,
287+ 2115, 648, 649, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601,
288+ 660, 661, 2130, 2131, 664, 665, 608, 609, 610, 611, 612, 613, 614,
289+ 615, 616, 617, 646, 647, 2146, 2147, 2184, 2185, 624, 625, 626, 627,
290+ 628, 629, 630, 631, 632, 633, 662, 663, 2162, 2163, 2200, 2201, 768,
291+ 769, 770, 771, 772, 773, 774, 775, 776, 777, 896, 897, 2306, 2307,
292+ 2434, 2435, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 912,
293+ 913, 2322, 2323, 2450, 2451, 800, 801, 802, 803, 804, 805, 806, 807,
294+ 808, 809, 898, 899, 2338, 2339, 2344, 2345, 816, 817, 818, 819, 820,
295+ 821, 822, 823, 824, 825, 914, 915, 2354, 2355, 2360, 2361, 832, 833,
296+ 834, 835, 836, 837, 838, 839, 840, 841, 900, 901, 2370, 2371, 904,
297+ 905, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 916, 917,
298+ 2386, 2387, 920, 921, 864, 865, 866, 867, 868, 869, 870, 871, 872,
299+ 873, 902, 903, 2402, 2403, 2440, 2441, 880, 881, 882, 883, 884, 885,
300+ 886, 887, 888, 889, 918, 919, 2418, 2419, 2456, 2457, 1024, 1025, 1026,
301+ 1027, 1028, 1029, 1030, 1031, 1032, 1033, 1152, 1153, 2052, 2053, 2180, 2181,
302+ 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1168, 1169, 2068,
303+ 2069, 2196, 2197, 1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064, 1065,
304+ 1154, 1155, 2084, 2085, 2120, 2121, 1072, 1073, 1074, 1075, 1076, 1077, 1078,
305+ 1079, 1080, 1081, 1170, 1171, 2100, 2101, 2136, 2137, 1088, 1089, 1090, 1091,
306+ 1092, 1093, 1094, 1095, 1096, 1097, 1156, 1157, 2116, 2117, 1160, 1161, 1104,
307+ 1105, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1172, 1173, 2132, 2133,
308+ 1176, 1177, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129, 1158,
309+ 1159, 2148, 2149, 2184, 2185, 1136, 1137, 1138, 1139, 1140, 1141, 1142, 1143,
310+ 1144, 1145, 1174, 1175, 2164, 2165, 2200, 2201, 1280, 1281, 1282, 1283, 1284,
311+ 1285, 1286, 1287, 1288, 1289, 1408, 1409, 2308, 2309, 2436, 2437, 1296, 1297,
312+ 1298, 1299, 1300, 1301, 1302, 1303, 1304, 1305, 1424, 1425, 2324, 2325, 2452,
313+ 2453, 1312, 1313, 1314, 1315, 1316, 1317, 1318, 1319, 1320, 1321, 1410, 1411,
314+ 2340, 2341, 2376, 2377, 1328, 1329, 1330, 1331, 1332, 1333, 1334, 1335, 1336,
315+ 1337, 1426, 1427, 2356, 2357, 2392, 2393, 1344, 1345, 1346, 1347, 1348, 1349,
316+ 1350, 1351, 1352, 1353, 1412, 1413, 2372, 2373, 1416, 1417, 1360, 1361, 1362,
317+ 1363, 1364, 1365, 1366, 1367, 1368, 1369, 1428, 1429, 2388, 2389, 1432, 1433,
318+ 1376, 1377, 1378, 1379, 1380, 1381, 1382, 1383, 1384, 1385, 1414, 1415, 2404,
319+ 2405, 2440, 2441, 1392, 1393, 1394, 1395, 1396, 1397, 1398, 1399, 1400, 1401,
320+ 1430, 1431, 2420, 2421, 2456, 2457, 1536, 1537, 1538, 1539, 1540, 1541, 1542,
321+ 1543, 1544, 1545, 1664, 1665, 2054, 2055, 2182, 2183, 1552, 1553, 1554, 1555,
322+ 1556, 1557, 1558, 1559, 1560, 1561, 1680, 1681, 2070, 2071, 2198, 2199, 1568,
323+ 1569, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577, 1666, 1667, 2086, 2087,
324+ 2152, 2153, 1584, 1585, 1586, 1587, 1588, 1589, 1590, 1591, 1592, 1593, 1682,
325+ 1683, 2102, 2103, 2168, 2169, 1600, 1601, 1602, 1603, 1604, 1605, 1606, 1607,
326+ 1608, 1609, 1668, 1669, 2118, 2119, 1672, 1673, 1616, 1617, 1618, 1619, 1620,
327+ 1621, 1622, 1623, 1624, 1625, 1684, 1685, 2134, 2135, 1688, 1689, 1632, 1633,
328+ 1634, 1635, 1636, 1637, 1638, 1639, 1640, 1641, 1670, 1671, 2150, 2151, 2184,
329+ 2185, 1648, 1649, 1650, 1651, 1652, 1653, 1654, 1655, 1656, 1657, 1686, 1687,
330+ 2166, 2167, 2200, 2201, 1792, 1793, 1794, 1795, 1796, 1797, 1798, 1799, 1800,
331+ 1801, 1920, 1921, 2310, 2311, 2438, 2439, 1808, 1809, 1810, 1811, 1812, 1813,
332+ 1814, 1815, 1816, 1817, 1936, 1937, 2326, 2327, 2454, 2455, 1824, 1825, 1826,
333+ 1827, 1828, 1829, 1830, 1831, 1832, 1833, 1922, 1923, 2342, 2343, 2408, 2409,
334+ 1840, 1841, 1842, 1843, 1844, 1845, 1846, 1847, 1848, 1849, 1938, 1939, 2358,
335+ 2359, 2424, 2425, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865,
336+ 1924, 1925, 2374, 2375, 1928, 1929, 1872, 1873, 1874, 1875, 1876, 1877, 1878,
337+ 1879, 1880, 1881, 1940, 1941, 2390, 2391, 1944, 1945, 1888, 1889, 1890, 1891,
338+ 1892, 1893, 1894, 1895, 1896, 1897, 1926, 1927, 2406, 2407, 2440, 2441, 1904,
339+ 1905, 1906, 1907, 1908, 1909, 1910, 1911, 1912, 1913, 1942, 1943, 2422, 2423,
340+ 2456, 2457};
341+#endif
342+
343+#if defined(DEC_BIN2DPD) && DEC_BIN2DPD==1 && !defined(DECBIN2DPD)
344+#define DECBIN2DPD
345+
346+const uint16_t BIN2DPD[1000]={ 0, 1, 2, 3, 4, 5, 6, 7,
347+ 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32,
348+ 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51,
349+ 52, 53, 54, 55, 56, 57, 64, 65, 66, 67, 68, 69, 70,
350+ 71, 72, 73, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
351+ 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 112, 113, 114,
352+ 115, 116, 117, 118, 119, 120, 121, 10, 11, 42, 43, 74, 75,
353+ 106, 107, 78, 79, 26, 27, 58, 59, 90, 91, 122, 123, 94,
354+ 95, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 144, 145,
355+ 146, 147, 148, 149, 150, 151, 152, 153, 160, 161, 162, 163, 164,
356+ 165, 166, 167, 168, 169, 176, 177, 178, 179, 180, 181, 182, 183,
357+ 184, 185, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 208,
358+ 209, 210, 211, 212, 213, 214, 215, 216, 217, 224, 225, 226, 227,
359+ 228, 229, 230, 231, 232, 233, 240, 241, 242, 243, 244, 245, 246,
360+ 247, 248, 249, 138, 139, 170, 171, 202, 203, 234, 235, 206, 207,
361+ 154, 155, 186, 187, 218, 219, 250, 251, 222, 223, 256, 257, 258,
362+ 259, 260, 261, 262, 263, 264, 265, 272, 273, 274, 275, 276, 277,
363+ 278, 279, 280, 281, 288, 289, 290, 291, 292, 293, 294, 295, 296,
364+ 297, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 320, 321,
365+ 322, 323, 324, 325, 326, 327, 328, 329, 336, 337, 338, 339, 340,
366+ 341, 342, 343, 344, 345, 352, 353, 354, 355, 356, 357, 358, 359,
367+ 360, 361, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 266,
368+ 267, 298, 299, 330, 331, 362, 363, 334, 335, 282, 283, 314, 315,
369+ 346, 347, 378, 379, 350, 351, 384, 385, 386, 387, 388, 389, 390,
370+ 391, 392, 393, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409,
371+ 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 432, 433, 434,
372+ 435, 436, 437, 438, 439, 440, 441, 448, 449, 450, 451, 452, 453,
373+ 454, 455, 456, 457, 464, 465, 466, 467, 468, 469, 470, 471, 472,
374+ 473, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 496, 497,
375+ 498, 499, 500, 501, 502, 503, 504, 505, 394, 395, 426, 427, 458,
376+ 459, 490, 491, 462, 463, 410, 411, 442, 443, 474, 475, 506, 507,
377+ 478, 479, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 528,
378+ 529, 530, 531, 532, 533, 534, 535, 536, 537, 544, 545, 546, 547,
379+ 548, 549, 550, 551, 552, 553, 560, 561, 562, 563, 564, 565, 566,
380+ 567, 568, 569, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585,
381+ 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 608, 609, 610,
382+ 611, 612, 613, 614, 615, 616, 617, 624, 625, 626, 627, 628, 629,
383+ 630, 631, 632, 633, 522, 523, 554, 555, 586, 587, 618, 619, 590,
384+ 591, 538, 539, 570, 571, 602, 603, 634, 635, 606, 607, 640, 641,
385+ 642, 643, 644, 645, 646, 647, 648, 649, 656, 657, 658, 659, 660,
386+ 661, 662, 663, 664, 665, 672, 673, 674, 675, 676, 677, 678, 679,
387+ 680, 681, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 704,
388+ 705, 706, 707, 708, 709, 710, 711, 712, 713, 720, 721, 722, 723,
389+ 724, 725, 726, 727, 728, 729, 736, 737, 738, 739, 740, 741, 742,
390+ 743, 744, 745, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761,
391+ 650, 651, 682, 683, 714, 715, 746, 747, 718, 719, 666, 667, 698,
392+ 699, 730, 731, 762, 763, 734, 735, 768, 769, 770, 771, 772, 773,
393+ 774, 775, 776, 777, 784, 785, 786, 787, 788, 789, 790, 791, 792,
394+ 793, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 816, 817,
395+ 818, 819, 820, 821, 822, 823, 824, 825, 832, 833, 834, 835, 836,
396+ 837, 838, 839, 840, 841, 848, 849, 850, 851, 852, 853, 854, 855,
397+ 856, 857, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 880,
398+ 881, 882, 883, 884, 885, 886, 887, 888, 889, 778, 779, 810, 811,
399+ 842, 843, 874, 875, 846, 847, 794, 795, 826, 827, 858, 859, 890,
400+ 891, 862, 863, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905,
401+ 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 928, 929, 930,
402+ 931, 932, 933, 934, 935, 936, 937, 944, 945, 946, 947, 948, 949,
403+ 950, 951, 952, 953, 960, 961, 962, 963, 964, 965, 966, 967, 968,
404+ 969, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 992, 993,
405+ 994, 995, 996, 997, 998, 999, 1000, 1001, 1008, 1009, 1010, 1011, 1012,
406+ 1013, 1014, 1015, 1016, 1017, 906, 907, 938, 939, 970, 971, 1002, 1003,
407+ 974, 975, 922, 923, 954, 955, 986, 987, 1018, 1019, 990, 991, 12,
408+ 13, 268, 269, 524, 525, 780, 781, 46, 47, 28, 29, 284, 285,
409+ 540, 541, 796, 797, 62, 63, 44, 45, 300, 301, 556, 557, 812,
410+ 813, 302, 303, 60, 61, 316, 317, 572, 573, 828, 829, 318, 319,
411+ 76, 77, 332, 333, 588, 589, 844, 845, 558, 559, 92, 93, 348,
412+ 349, 604, 605, 860, 861, 574, 575, 108, 109, 364, 365, 620, 621,
413+ 876, 877, 814, 815, 124, 125, 380, 381, 636, 637, 892, 893, 830,
414+ 831, 14, 15, 270, 271, 526, 527, 782, 783, 110, 111, 30, 31,
415+ 286, 287, 542, 543, 798, 799, 126, 127, 140, 141, 396, 397, 652,
416+ 653, 908, 909, 174, 175, 156, 157, 412, 413, 668, 669, 924, 925,
417+ 190, 191, 172, 173, 428, 429, 684, 685, 940, 941, 430, 431, 188,
418+ 189, 444, 445, 700, 701, 956, 957, 446, 447, 204, 205, 460, 461,
419+ 716, 717, 972, 973, 686, 687, 220, 221, 476, 477, 732, 733, 988,
420+ 989, 702, 703, 236, 237, 492, 493, 748, 749, 1004, 1005, 942, 943,
421+ 252, 253, 508, 509, 764, 765, 1020, 1021, 958, 959, 142, 143, 398,
422+ 399, 654, 655, 910, 911, 238, 239, 158, 159, 414, 415, 670, 671,
423+ 926, 927, 254, 255};
424+#endif
425+
426+#if defined(DEC_DPD2BIN) && DEC_DPD2BIN==1 && !defined(DECDPD2BIN)
427+#define DECDPD2BIN
428+
429+const uint16_t DPD2BIN[1024]={ 0, 1, 2, 3, 4, 5, 6, 7,
430+ 8, 9, 80, 81, 800, 801, 880, 881, 10, 11, 12, 13, 14,
431+ 15, 16, 17, 18, 19, 90, 91, 810, 811, 890, 891, 20, 21,
432+ 22, 23, 24, 25, 26, 27, 28, 29, 82, 83, 820, 821, 808,
433+ 809, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 92, 93,
434+ 830, 831, 818, 819, 40, 41, 42, 43, 44, 45, 46, 47, 48,
435+ 49, 84, 85, 840, 841, 88, 89, 50, 51, 52, 53, 54, 55,
436+ 56, 57, 58, 59, 94, 95, 850, 851, 98, 99, 60, 61, 62,
437+ 63, 64, 65, 66, 67, 68, 69, 86, 87, 860, 861, 888, 889,
438+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 96, 97, 870,
439+ 871, 898, 899, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
440+ 180, 181, 900, 901, 980, 981, 110, 111, 112, 113, 114, 115, 116,
441+ 117, 118, 119, 190, 191, 910, 911, 990, 991, 120, 121, 122, 123,
442+ 124, 125, 126, 127, 128, 129, 182, 183, 920, 921, 908, 909, 130,
443+ 131, 132, 133, 134, 135, 136, 137, 138, 139, 192, 193, 930, 931,
444+ 918, 919, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 184,
445+ 185, 940, 941, 188, 189, 150, 151, 152, 153, 154, 155, 156, 157,
446+ 158, 159, 194, 195, 950, 951, 198, 199, 160, 161, 162, 163, 164,
447+ 165, 166, 167, 168, 169, 186, 187, 960, 961, 988, 989, 170, 171,
448+ 172, 173, 174, 175, 176, 177, 178, 179, 196, 197, 970, 971, 998,
449+ 999, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 280, 281,
450+ 802, 803, 882, 883, 210, 211, 212, 213, 214, 215, 216, 217, 218,
451+ 219, 290, 291, 812, 813, 892, 893, 220, 221, 222, 223, 224, 225,
452+ 226, 227, 228, 229, 282, 283, 822, 823, 828, 829, 230, 231, 232,
453+ 233, 234, 235, 236, 237, 238, 239, 292, 293, 832, 833, 838, 839,
454+ 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 284, 285, 842,
455+ 843, 288, 289, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259,
456+ 294, 295, 852, 853, 298, 299, 260, 261, 262, 263, 264, 265, 266,
457+ 267, 268, 269, 286, 287, 862, 863, 888, 889, 270, 271, 272, 273,
458+ 274, 275, 276, 277, 278, 279, 296, 297, 872, 873, 898, 899, 300,
459+ 301, 302, 303, 304, 305, 306, 307, 308, 309, 380, 381, 902, 903,
460+ 982, 983, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 390,
461+ 391, 912, 913, 992, 993, 320, 321, 322, 323, 324, 325, 326, 327,
462+ 328, 329, 382, 383, 922, 923, 928, 929, 330, 331, 332, 333, 334,
463+ 335, 336, 337, 338, 339, 392, 393, 932, 933, 938, 939, 340, 341,
464+ 342, 343, 344, 345, 346, 347, 348, 349, 384, 385, 942, 943, 388,
465+ 389, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 394, 395,
466+ 952, 953, 398, 399, 360, 361, 362, 363, 364, 365, 366, 367, 368,
467+ 369, 386, 387, 962, 963, 988, 989, 370, 371, 372, 373, 374, 375,
468+ 376, 377, 378, 379, 396, 397, 972, 973, 998, 999, 400, 401, 402,
469+ 403, 404, 405, 406, 407, 408, 409, 480, 481, 804, 805, 884, 885,
470+ 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 490, 491, 814,
471+ 815, 894, 895, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429,
472+ 482, 483, 824, 825, 848, 849, 430, 431, 432, 433, 434, 435, 436,
473+ 437, 438, 439, 492, 493, 834, 835, 858, 859, 440, 441, 442, 443,
474+ 444, 445, 446, 447, 448, 449, 484, 485, 844, 845, 488, 489, 450,
475+ 451, 452, 453, 454, 455, 456, 457, 458, 459, 494, 495, 854, 855,
476+ 498, 499, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 486,
477+ 487, 864, 865, 888, 889, 470, 471, 472, 473, 474, 475, 476, 477,
478+ 478, 479, 496, 497, 874, 875, 898, 899, 500, 501, 502, 503, 504,
479+ 505, 506, 507, 508, 509, 580, 581, 904, 905, 984, 985, 510, 511,
480+ 512, 513, 514, 515, 516, 517, 518, 519, 590, 591, 914, 915, 994,
481+ 995, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 582, 583,
482+ 924, 925, 948, 949, 530, 531, 532, 533, 534, 535, 536, 537, 538,
483+ 539, 592, 593, 934, 935, 958, 959, 540, 541, 542, 543, 544, 545,
484+ 546, 547, 548, 549, 584, 585, 944, 945, 588, 589, 550, 551, 552,
485+ 553, 554, 555, 556, 557, 558, 559, 594, 595, 954, 955, 598, 599,
486+ 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 586, 587, 964,
487+ 965, 988, 989, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579,
488+ 596, 597, 974, 975, 998, 999, 600, 601, 602, 603, 604, 605, 606,
489+ 607, 608, 609, 680, 681, 806, 807, 886, 887, 610, 611, 612, 613,
490+ 614, 615, 616, 617, 618, 619, 690, 691, 816, 817, 896, 897, 620,
491+ 621, 622, 623, 624, 625, 626, 627, 628, 629, 682, 683, 826, 827,
492+ 868, 869, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 692,
493+ 693, 836, 837, 878, 879, 640, 641, 642, 643, 644, 645, 646, 647,
494+ 648, 649, 684, 685, 846, 847, 688, 689, 650, 651, 652, 653, 654,
495+ 655, 656, 657, 658, 659, 694, 695, 856, 857, 698, 699, 660, 661,
496+ 662, 663, 664, 665, 666, 667, 668, 669, 686, 687, 866, 867, 888,
497+ 889, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 696, 697,
498+ 876, 877, 898, 899, 700, 701, 702, 703, 704, 705, 706, 707, 708,
499+ 709, 780, 781, 906, 907, 986, 987, 710, 711, 712, 713, 714, 715,
500+ 716, 717, 718, 719, 790, 791, 916, 917, 996, 997, 720, 721, 722,
501+ 723, 724, 725, 726, 727, 728, 729, 782, 783, 926, 927, 968, 969,
502+ 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 792, 793, 936,
503+ 937, 978, 979, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749,
504+ 784, 785, 946, 947, 788, 789, 750, 751, 752, 753, 754, 755, 756,
505+ 757, 758, 759, 794, 795, 956, 957, 798, 799, 760, 761, 762, 763,
506+ 764, 765, 766, 767, 768, 769, 786, 787, 966, 967, 988, 989, 770,
507+ 771, 772, 773, 774, 775, 776, 777, 778, 779, 796, 797, 976, 977,
508+ 998, 999};
509+#endif
510+
511+#if defined(DEC_DPD2BINK) && DEC_DPD2BINK==1 && !defined(DECDPD2BINK)
512+#define DECDPD2BINK
50513
51-const uint16_t BCD2DPD[2458] = { 0, 1, 2, 3, 4, 5, 6, 7,
52- 8, 9, 0, 0, 0, 0, 0, 0, 16, 17, 18, 19, 20,
53- 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 32, 33,
54- 34, 35, 36, 37, 38, 39, 40, 41, 0, 0, 0, 0, 0,
55- 0, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 0,
56- 0, 0, 0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72,
57- 73, 0, 0, 0, 0, 0, 0, 80, 81, 82, 83, 84, 85,
58- 86, 87, 88, 89, 0, 0, 0, 0, 0, 0, 96, 97, 98,
59- 99, 100, 101, 102, 103, 104, 105, 0, 0, 0, 0, 0, 0,
60- 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 0, 0, 0,
61- 0, 0, 0, 10, 11, 42, 43, 74, 75, 106, 107, 78, 79,
62- 0, 0, 0, 0, 0, 0, 26, 27, 58, 59, 90, 91, 122,
63- 123, 94, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68- 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 42, 43, 74,
69- 75, 106, 107, 78, 79, 0, 0, 0, 0, 0, 0, 0, 0,
70- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71- 0, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 0, 0,
72- 0, 0, 0, 0, 144, 145, 146, 147, 148, 149, 150, 151, 152,
73- 153, 0, 0, 0, 0, 0, 0, 160, 161, 162, 163, 164, 165,
74- 166, 167, 168, 169, 0, 0, 0, 0, 0, 0, 176, 177, 178,
75- 179, 180, 181, 182, 183, 184, 185, 0, 0, 0, 0, 0, 0,
76- 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 0, 0, 0,
77- 0, 0, 0, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
78- 0, 0, 0, 0, 0, 0, 224, 225, 226, 227, 228, 229, 230,
79- 231, 232, 233, 0, 0, 0, 0, 0, 0, 240, 241, 242, 243,
80- 244, 245, 246, 247, 248, 249, 0, 0, 0, 0, 0, 0, 138,
81- 139, 170, 171, 202, 203, 234, 235, 206, 207, 0, 0, 0, 0,
82- 0, 0, 154, 155, 186, 187, 218, 219, 250, 251, 222, 223, 0,
83- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88- 0, 0, 0, 0, 138, 139, 170, 171, 202, 203, 234, 235, 206,
89- 207, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 256, 257, 258,
91- 259, 260, 261, 262, 263, 264, 265, 0, 0, 0, 0, 0, 0,
92- 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 0, 0, 0,
93- 0, 0, 0, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297,
94- 0, 0, 0, 0, 0, 0, 304, 305, 306, 307, 308, 309, 310,
95- 311, 312, 313, 0, 0, 0, 0, 0, 0, 320, 321, 322, 323,
96- 324, 325, 326, 327, 328, 329, 0, 0, 0, 0, 0, 0, 336,
97- 337, 338, 339, 340, 341, 342, 343, 344, 345, 0, 0, 0, 0,
98- 0, 0, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 0,
99- 0, 0, 0, 0, 0, 368, 369, 370, 371, 372, 373, 374, 375,
100- 376, 377, 0, 0, 0, 0, 0, 0, 266, 267, 298, 299, 330,
101- 331, 362, 363, 334, 335, 0, 0, 0, 0, 0, 0, 282, 283,
102- 314, 315, 346, 347, 378, 379, 350, 351, 0, 0, 0, 0, 0,
103- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108- 266, 267, 298, 299, 330, 331, 362, 363, 334, 335, 0, 0, 0,
109- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110- 0, 0, 0, 0, 0, 0, 384, 385, 386, 387, 388, 389, 390,
111- 391, 392, 393, 0, 0, 0, 0, 0, 0, 400, 401, 402, 403,
112- 404, 405, 406, 407, 408, 409, 0, 0, 0, 0, 0, 0, 416,
113- 417, 418, 419, 420, 421, 422, 423, 424, 425, 0, 0, 0, 0,
114- 0, 0, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 0,
115- 0, 0, 0, 0, 0, 448, 449, 450, 451, 452, 453, 454, 455,
116- 456, 457, 0, 0, 0, 0, 0, 0, 464, 465, 466, 467, 468,
117- 469, 470, 471, 472, 473, 0, 0, 0, 0, 0, 0, 480, 481,
118- 482, 483, 484, 485, 486, 487, 488, 489, 0, 0, 0, 0, 0,
119- 0, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 0, 0,
120- 0, 0, 0, 0, 394, 395, 426, 427, 458, 459, 490, 491, 462,
121- 463, 0, 0, 0, 0, 0, 0, 410, 411, 442, 443, 474, 475,
122- 506, 507, 478, 479, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
127- 0, 0, 0, 0, 0, 0, 0, 0, 0, 394, 395, 426, 427,
128- 458, 459, 490, 491, 462, 463, 0, 0, 0, 0, 0, 0, 0,
129- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130- 0, 0, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 0,
131- 0, 0, 0, 0, 0, 528, 529, 530, 531, 532, 533, 534, 535,
132- 536, 537, 0, 0, 0, 0, 0, 0, 544, 545, 546, 547, 548,
133- 549, 550, 551, 552, 553, 0, 0, 0, 0, 0, 0, 560, 561,
134- 562, 563, 564, 565, 566, 567, 568, 569, 0, 0, 0, 0, 0,
135- 0, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 0, 0,
136- 0, 0, 0, 0, 592, 593, 594, 595, 596, 597, 598, 599, 600,
137- 601, 0, 0, 0, 0, 0, 0, 608, 609, 610, 611, 612, 613,
138- 614, 615, 616, 617, 0, 0, 0, 0, 0, 0, 624, 625, 626,
139- 627, 628, 629, 630, 631, 632, 633, 0, 0, 0, 0, 0, 0,
140- 522, 523, 554, 555, 586, 587, 618, 619, 590, 591, 0, 0, 0,
141- 0, 0, 0, 538, 539, 570, 571, 602, 603, 634, 635, 606, 607,
142- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
143- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
144- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
145- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
147- 0, 0, 0, 0, 0, 522, 523, 554, 555, 586, 587, 618, 619,
148- 590, 591, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
149- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 640, 641,
150- 642, 643, 644, 645, 646, 647, 648, 649, 0, 0, 0, 0, 0,
151- 0, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 0, 0,
152- 0, 0, 0, 0, 672, 673, 674, 675, 676, 677, 678, 679, 680,
153- 681, 0, 0, 0, 0, 0, 0, 688, 689, 690, 691, 692, 693,
154- 694, 695, 696, 697, 0, 0, 0, 0, 0, 0, 704, 705, 706,
155- 707, 708, 709, 710, 711, 712, 713, 0, 0, 0, 0, 0, 0,
156- 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 0, 0, 0,
157- 0, 0, 0, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745,
158- 0, 0, 0, 0, 0, 0, 752, 753, 754, 755, 756, 757, 758,
159- 759, 760, 761, 0, 0, 0, 0, 0, 0, 650, 651, 682, 683,
160- 714, 715, 746, 747, 718, 719, 0, 0, 0, 0, 0, 0, 666,
161- 667, 698, 699, 730, 731, 762, 763, 734, 735, 0, 0, 0, 0,
162- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
166- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
167- 0, 650, 651, 682, 683, 714, 715, 746, 747, 718, 719, 0, 0,
168- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169- 0, 0, 0, 0, 0, 0, 0, 768, 769, 770, 771, 772, 773,
170- 774, 775, 776, 777, 0, 0, 0, 0, 0, 0, 784, 785, 786,
171- 787, 788, 789, 790, 791, 792, 793, 0, 0, 0, 0, 0, 0,
172- 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 0, 0, 0,
173- 0, 0, 0, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825,
174- 0, 0, 0, 0, 0, 0, 832, 833, 834, 835, 836, 837, 838,
175- 839, 840, 841, 0, 0, 0, 0, 0, 0, 848, 849, 850, 851,
176- 852, 853, 854, 855, 856, 857, 0, 0, 0, 0, 0, 0, 864,
177- 865, 866, 867, 868, 869, 870, 871, 872, 873, 0, 0, 0, 0,
178- 0, 0, 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 0,
179- 0, 0, 0, 0, 0, 778, 779, 810, 811, 842, 843, 874, 875,
180- 846, 847, 0, 0, 0, 0, 0, 0, 794, 795, 826, 827, 858,
181- 859, 890, 891, 862, 863, 0, 0, 0, 0, 0, 0, 0, 0,
182- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 778, 779, 810,
187- 811, 842, 843, 874, 875, 846, 847, 0, 0, 0, 0, 0, 0,
188- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
189- 0, 0, 0, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905,
190- 0, 0, 0, 0, 0, 0, 912, 913, 914, 915, 916, 917, 918,
191- 919, 920, 921, 0, 0, 0, 0, 0, 0, 928, 929, 930, 931,
192- 932, 933, 934, 935, 936, 937, 0, 0, 0, 0, 0, 0, 944,
193- 945, 946, 947, 948, 949, 950, 951, 952, 953, 0, 0, 0, 0,
194- 0, 0, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 0,
195- 0, 0, 0, 0, 0, 976, 977, 978, 979, 980, 981, 982, 983,
196- 984, 985, 0, 0, 0, 0, 0, 0, 992, 993, 994, 995, 996,
197- 997, 998, 999, 1000, 1001, 0, 0, 0, 0, 0, 0, 1008, 1009,
198- 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 0, 0, 0, 0, 0,
199- 0, 906, 907, 938, 939, 970, 971, 1002, 1003, 974, 975, 0, 0,
200- 0, 0, 0, 0, 922, 923, 954, 955, 986, 987, 1018, 1019, 990,
201- 991, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
203- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
204- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
205- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206- 0, 0, 0, 0, 0, 0, 906, 907, 938, 939, 970, 971, 1002,
207- 1003, 974, 975, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
208- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12,
209- 13, 268, 269, 524, 525, 780, 781, 46, 47, 0, 0, 0, 0,
210- 0, 0, 28, 29, 284, 285, 540, 541, 796, 797, 62, 63, 0,
211- 0, 0, 0, 0, 0, 44, 45, 300, 301, 556, 557, 812, 813,
212- 302, 303, 0, 0, 0, 0, 0, 0, 60, 61, 316, 317, 572,
213- 573, 828, 829, 318, 319, 0, 0, 0, 0, 0, 0, 76, 77,
214- 332, 333, 588, 589, 844, 845, 558, 559, 0, 0, 0, 0, 0,
215- 0, 92, 93, 348, 349, 604, 605, 860, 861, 574, 575, 0, 0,
216- 0, 0, 0, 0, 108, 109, 364, 365, 620, 621, 876, 877, 814,
217- 815, 0, 0, 0, 0, 0, 0, 124, 125, 380, 381, 636, 637,
218- 892, 893, 830, 831, 0, 0, 0, 0, 0, 0, 14, 15, 270,
219- 271, 526, 527, 782, 783, 110, 111, 0, 0, 0, 0, 0, 0,
220- 30, 31, 286, 287, 542, 543, 798, 799, 126, 127, 0, 0, 0,
221- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
222- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
224- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
225- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
226- 0, 0, 14, 15, 270, 271, 526, 527, 782, 783, 110, 111, 0,
227- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
228- 0, 0, 0, 0, 0, 0, 0, 0, 140, 141, 396, 397, 652,
229- 653, 908, 909, 174, 175, 0, 0, 0, 0, 0, 0, 156, 157,
230- 412, 413, 668, 669, 924, 925, 190, 191, 0, 0, 0, 0, 0,
231- 0, 172, 173, 428, 429, 684, 685, 940, 941, 430, 431, 0, 0,
232- 0, 0, 0, 0, 188, 189, 444, 445, 700, 701, 956, 957, 446,
233- 447, 0, 0, 0, 0, 0, 0, 204, 205, 460, 461, 716, 717,
234- 972, 973, 686, 687, 0, 0, 0, 0, 0, 0, 220, 221, 476,
235- 477, 732, 733, 988, 989, 702, 703, 0, 0, 0, 0, 0, 0,
236- 236, 237, 492, 493, 748, 749, 1004, 1005, 942, 943, 0, 0, 0,
237- 0, 0, 0, 252, 253, 508, 509, 764, 765, 1020, 1021, 958, 959,
238- 0, 0, 0, 0, 0, 0, 142, 143, 398, 399, 654, 655, 910,
239- 911, 238, 239, 0, 0, 0, 0, 0, 0, 158, 159, 414, 415,
240- 670, 671, 926, 927, 254, 255
241-};
514+const uint32_t DPD2BINK[1024]={ 0, 1000, 2000, 3000, 4000, 5000,
515+ 6000, 7000, 8000, 9000, 80000, 81000, 800000, 801000, 880000, 881000,
516+ 10000, 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000,
517+ 90000, 91000, 810000, 811000, 890000, 891000, 20000, 21000, 22000, 23000,
518+ 24000, 25000, 26000, 27000, 28000, 29000, 82000, 83000, 820000, 821000,
519+ 808000, 809000, 30000, 31000, 32000, 33000, 34000, 35000, 36000, 37000,
520+ 38000, 39000, 92000, 93000, 830000, 831000, 818000, 819000, 40000, 41000,
521+ 42000, 43000, 44000, 45000, 46000, 47000, 48000, 49000, 84000, 85000,
522+ 840000, 841000, 88000, 89000, 50000, 51000, 52000, 53000, 54000, 55000,
523+ 56000, 57000, 58000, 59000, 94000, 95000, 850000, 851000, 98000, 99000,
524+ 60000, 61000, 62000, 63000, 64000, 65000, 66000, 67000, 68000, 69000,
525+ 86000, 87000, 860000, 861000, 888000, 889000, 70000, 71000, 72000, 73000,
526+ 74000, 75000, 76000, 77000, 78000, 79000, 96000, 97000, 870000, 871000,
527+ 898000, 899000, 100000, 101000, 102000, 103000, 104000, 105000, 106000, 107000,
528+ 108000, 109000, 180000, 181000, 900000, 901000, 980000, 981000, 110000, 111000,
529+ 112000, 113000, 114000, 115000, 116000, 117000, 118000, 119000, 190000, 191000,
530+ 910000, 911000, 990000, 991000, 120000, 121000, 122000, 123000, 124000, 125000,
531+ 126000, 127000, 128000, 129000, 182000, 183000, 920000, 921000, 908000, 909000,
532+ 130000, 131000, 132000, 133000, 134000, 135000, 136000, 137000, 138000, 139000,
533+ 192000, 193000, 930000, 931000, 918000, 919000, 140000, 141000, 142000, 143000,
534+ 144000, 145000, 146000, 147000, 148000, 149000, 184000, 185000, 940000, 941000,
535+ 188000, 189000, 150000, 151000, 152000, 153000, 154000, 155000, 156000, 157000,
536+ 158000, 159000, 194000, 195000, 950000, 951000, 198000, 199000, 160000, 161000,
537+ 162000, 163000, 164000, 165000, 166000, 167000, 168000, 169000, 186000, 187000,
538+ 960000, 961000, 988000, 989000, 170000, 171000, 172000, 173000, 174000, 175000,
539+ 176000, 177000, 178000, 179000, 196000, 197000, 970000, 971000, 998000, 999000,
540+ 200000, 201000, 202000, 203000, 204000, 205000, 206000, 207000, 208000, 209000,
541+ 280000, 281000, 802000, 803000, 882000, 883000, 210000, 211000, 212000, 213000,
542+ 214000, 215000, 216000, 217000, 218000, 219000, 290000, 291000, 812000, 813000,
543+ 892000, 893000, 220000, 221000, 222000, 223000, 224000, 225000, 226000, 227000,
544+ 228000, 229000, 282000, 283000, 822000, 823000, 828000, 829000, 230000, 231000,
545+ 232000, 233000, 234000, 235000, 236000, 237000, 238000, 239000, 292000, 293000,
546+ 832000, 833000, 838000, 839000, 240000, 241000, 242000, 243000, 244000, 245000,
547+ 246000, 247000, 248000, 249000, 284000, 285000, 842000, 843000, 288000, 289000,
548+ 250000, 251000, 252000, 253000, 254000, 255000, 256000, 257000, 258000, 259000,
549+ 294000, 295000, 852000, 853000, 298000, 299000, 260000, 261000, 262000, 263000,
550+ 264000, 265000, 266000, 267000, 268000, 269000, 286000, 287000, 862000, 863000,
551+ 888000, 889000, 270000, 271000, 272000, 273000, 274000, 275000, 276000, 277000,
552+ 278000, 279000, 296000, 297000, 872000, 873000, 898000, 899000, 300000, 301000,
553+ 302000, 303000, 304000, 305000, 306000, 307000, 308000, 309000, 380000, 381000,
554+ 902000, 903000, 982000, 983000, 310000, 311000, 312000, 313000, 314000, 315000,
555+ 316000, 317000, 318000, 319000, 390000, 391000, 912000, 913000, 992000, 993000,
556+ 320000, 321000, 322000, 323000, 324000, 325000, 326000, 327000, 328000, 329000,
557+ 382000, 383000, 922000, 923000, 928000, 929000, 330000, 331000, 332000, 333000,
558+ 334000, 335000, 336000, 337000, 338000, 339000, 392000, 393000, 932000, 933000,
559+ 938000, 939000, 340000, 341000, 342000, 343000, 344000, 345000, 346000, 347000,
560+ 348000, 349000, 384000, 385000, 942000, 943000, 388000, 389000, 350000, 351000,
561+ 352000, 353000, 354000, 355000, 356000, 357000, 358000, 359000, 394000, 395000,
562+ 952000, 953000, 398000, 399000, 360000, 361000, 362000, 363000, 364000, 365000,
563+ 366000, 367000, 368000, 369000, 386000, 387000, 962000, 963000, 988000, 989000,
564+ 370000, 371000, 372000, 373000, 374000, 375000, 376000, 377000, 378000, 379000,
565+ 396000, 397000, 972000, 973000, 998000, 999000, 400000, 401000, 402000, 403000,
566+ 404000, 405000, 406000, 407000, 408000, 409000, 480000, 481000, 804000, 805000,
567+ 884000, 885000, 410000, 411000, 412000, 413000, 414000, 415000, 416000, 417000,
568+ 418000, 419000, 490000, 491000, 814000, 815000, 894000, 895000, 420000, 421000,
569+ 422000, 423000, 424000, 425000, 426000, 427000, 428000, 429000, 482000, 483000,
570+ 824000, 825000, 848000, 849000, 430000, 431000, 432000, 433000, 434000, 435000,
571+ 436000, 437000, 438000, 439000, 492000, 493000, 834000, 835000, 858000, 859000,
572+ 440000, 441000, 442000, 443000, 444000, 445000, 446000, 447000, 448000, 449000,
573+ 484000, 485000, 844000, 845000, 488000, 489000, 450000, 451000, 452000, 453000,
574+ 454000, 455000, 456000, 457000, 458000, 459000, 494000, 495000, 854000, 855000,
575+ 498000, 499000, 460000, 461000, 462000, 463000, 464000, 465000, 466000, 467000,
576+ 468000, 469000, 486000, 487000, 864000, 865000, 888000, 889000, 470000, 471000,
577+ 472000, 473000, 474000, 475000, 476000, 477000, 478000, 479000, 496000, 497000,
578+ 874000, 875000, 898000, 899000, 500000, 501000, 502000, 503000, 504000, 505000,
579+ 506000, 507000, 508000, 509000, 580000, 581000, 904000, 905000, 984000, 985000,
580+ 510000, 511000, 512000, 513000, 514000, 515000, 516000, 517000, 518000, 519000,
581+ 590000, 591000, 914000, 915000, 994000, 995000, 520000, 521000, 522000, 523000,
582+ 524000, 525000, 526000, 527000, 528000, 529000, 582000, 583000, 924000, 925000,
583+ 948000, 949000, 530000, 531000, 532000, 533000, 534000, 535000, 536000, 537000,
584+ 538000, 539000, 592000, 593000, 934000, 935000, 958000, 959000, 540000, 541000,
585+ 542000, 543000, 544000, 545000, 546000, 547000, 548000, 549000, 584000, 585000,
586+ 944000, 945000, 588000, 589000, 550000, 551000, 552000, 553000, 554000, 555000,
587+ 556000, 557000, 558000, 559000, 594000, 595000, 954000, 955000, 598000, 599000,
588+ 560000, 561000, 562000, 563000, 564000, 565000, 566000, 567000, 568000, 569000,
589+ 586000, 587000, 964000, 965000, 988000, 989000, 570000, 571000, 572000, 573000,
590+ 574000, 575000, 576000, 577000, 578000, 579000, 596000, 597000, 974000, 975000,
591+ 998000, 999000, 600000, 601000, 602000, 603000, 604000, 605000, 606000, 607000,
592+ 608000, 609000, 680000, 681000, 806000, 807000, 886000, 887000, 610000, 611000,
593+ 612000, 613000, 614000, 615000, 616000, 617000, 618000, 619000, 690000, 691000,
594+ 816000, 817000, 896000, 897000, 620000, 621000, 622000, 623000, 624000, 625000,
595+ 626000, 627000, 628000, 629000, 682000, 683000, 826000, 827000, 868000, 869000,
596+ 630000, 631000, 632000, 633000, 634000, 635000, 636000, 637000, 638000, 639000,
597+ 692000, 693000, 836000, 837000, 878000, 879000, 640000, 641000, 642000, 643000,
598+ 644000, 645000, 646000, 647000, 648000, 649000, 684000, 685000, 846000, 847000,
599+ 688000, 689000, 650000, 651000, 652000, 653000, 654000, 655000, 656000, 657000,
600+ 658000, 659000, 694000, 695000, 856000, 857000, 698000, 699000, 660000, 661000,
601+ 662000, 663000, 664000, 665000, 666000, 667000, 668000, 669000, 686000, 687000,
602+ 866000, 867000, 888000, 889000, 670000, 671000, 672000, 673000, 674000, 675000,
603+ 676000, 677000, 678000, 679000, 696000, 697000, 876000, 877000, 898000, 899000,
604+ 700000, 701000, 702000, 703000, 704000, 705000, 706000, 707000, 708000, 709000,
605+ 780000, 781000, 906000, 907000, 986000, 987000, 710000, 711000, 712000, 713000,
606+ 714000, 715000, 716000, 717000, 718000, 719000, 790000, 791000, 916000, 917000,
607+ 996000, 997000, 720000, 721000, 722000, 723000, 724000, 725000, 726000, 727000,
608+ 728000, 729000, 782000, 783000, 926000, 927000, 968000, 969000, 730000, 731000,
609+ 732000, 733000, 734000, 735000, 736000, 737000, 738000, 739000, 792000, 793000,
610+ 936000, 937000, 978000, 979000, 740000, 741000, 742000, 743000, 744000, 745000,
611+ 746000, 747000, 748000, 749000, 784000, 785000, 946000, 947000, 788000, 789000,
612+ 750000, 751000, 752000, 753000, 754000, 755000, 756000, 757000, 758000, 759000,
613+ 794000, 795000, 956000, 957000, 798000, 799000, 760000, 761000, 762000, 763000,
614+ 764000, 765000, 766000, 767000, 768000, 769000, 786000, 787000, 966000, 967000,
615+ 988000, 989000, 770000, 771000, 772000, 773000, 774000, 775000, 776000, 777000,
616+ 778000, 779000, 796000, 797000, 976000, 977000, 998000, 999000};
242617 #endif
243618
244-#if DEC_DPD2BCD==1
619+#if defined(DEC_DPD2BINM) && DEC_DPD2BINM==1 && !defined(DECDPD2BINM)
620+#define DECDPD2BINM
245621
246-const uint16_t DPD2BCD[1024] = { 0, 1, 2, 3, 4, 5, 6, 7,
247- 8, 9, 128, 129, 2048, 2049, 2176, 2177, 16, 17, 18, 19, 20,
248- 21, 22, 23, 24, 25, 144, 145, 2064, 2065, 2192, 2193, 32, 33,
249- 34, 35, 36, 37, 38, 39, 40, 41, 130, 131, 2080, 2081, 2056,
250- 2057, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 146, 147,
251- 2096, 2097, 2072, 2073, 64, 65, 66, 67, 68, 69, 70, 71, 72,
252- 73, 132, 133, 2112, 2113, 136, 137, 80, 81, 82, 83, 84, 85,
253- 86, 87, 88, 89, 148, 149, 2128, 2129, 152, 153, 96, 97, 98,
254- 99, 100, 101, 102, 103, 104, 105, 134, 135, 2144, 2145, 2184, 2185,
255- 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 150, 151, 2160,
256- 2161, 2200, 2201, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265,
257- 384, 385, 2304, 2305, 2432, 2433, 272, 273, 274, 275, 276, 277, 278,
258- 279, 280, 281, 400, 401, 2320, 2321, 2448, 2449, 288, 289, 290, 291,
259- 292, 293, 294, 295, 296, 297, 386, 387, 2336, 2337, 2312, 2313, 304,
260- 305, 306, 307, 308, 309, 310, 311, 312, 313, 402, 403, 2352, 2353,
261- 2328, 2329, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 388,
262- 389, 2368, 2369, 392, 393, 336, 337, 338, 339, 340, 341, 342, 343,
263- 344, 345, 404, 405, 2384, 2385, 408, 409, 352, 353, 354, 355, 356,
264- 357, 358, 359, 360, 361, 390, 391, 2400, 2401, 2440, 2441, 368, 369,
265- 370, 371, 372, 373, 374, 375, 376, 377, 406, 407, 2416, 2417, 2456,
266- 2457, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 640, 641,
267- 2050, 2051, 2178, 2179, 528, 529, 530, 531, 532, 533, 534, 535, 536,
268- 537, 656, 657, 2066, 2067, 2194, 2195, 544, 545, 546, 547, 548, 549,
269- 550, 551, 552, 553, 642, 643, 2082, 2083, 2088, 2089, 560, 561, 562,
270- 563, 564, 565, 566, 567, 568, 569, 658, 659, 2098, 2099, 2104, 2105,
271- 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 644, 645, 2114,
272- 2115, 648, 649, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601,
273- 660, 661, 2130, 2131, 664, 665, 608, 609, 610, 611, 612, 613, 614,
274- 615, 616, 617, 646, 647, 2146, 2147, 2184, 2185, 624, 625, 626, 627,
275- 628, 629, 630, 631, 632, 633, 662, 663, 2162, 2163, 2200, 2201, 768,
276- 769, 770, 771, 772, 773, 774, 775, 776, 777, 896, 897, 2306, 2307,
277- 2434, 2435, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 912,
278- 913, 2322, 2323, 2450, 2451, 800, 801, 802, 803, 804, 805, 806, 807,
279- 808, 809, 898, 899, 2338, 2339, 2344, 2345, 816, 817, 818, 819, 820,
280- 821, 822, 823, 824, 825, 914, 915, 2354, 2355, 2360, 2361, 832, 833,
281- 834, 835, 836, 837, 838, 839, 840, 841, 900, 901, 2370, 2371, 904,
282- 905, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 916, 917,
283- 2386, 2387, 920, 921, 864, 865, 866, 867, 868, 869, 870, 871, 872,
284- 873, 902, 903, 2402, 2403, 2440, 2441, 880, 881, 882, 883, 884, 885,
285- 886, 887, 888, 889, 918, 919, 2418, 2419, 2456, 2457, 1024, 1025, 1026,
286- 1027, 1028, 1029, 1030, 1031, 1032, 1033, 1152, 1153, 2052, 2053, 2180,
287- 2181,
288- 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1168, 1169,
289- 2068,
290- 2069, 2196, 2197, 1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064,
291- 1065,
292- 1154, 1155, 2084, 2085, 2120, 2121, 1072, 1073, 1074, 1075, 1076, 1077,
293- 1078,
294- 1079, 1080, 1081, 1170, 1171, 2100, 2101, 2136, 2137, 1088, 1089, 1090,
295- 1091,
296- 1092, 1093, 1094, 1095, 1096, 1097, 1156, 1157, 2116, 2117, 1160, 1161,
297- 1104,
298- 1105, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1172, 1173, 2132,
299- 2133,
300- 1176, 1177, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129,
301- 1158,
302- 1159, 2148, 2149, 2184, 2185, 1136, 1137, 1138, 1139, 1140, 1141, 1142,
303- 1143,
304- 1144, 1145, 1174, 1175, 2164, 2165, 2200, 2201, 1280, 1281, 1282, 1283,
305- 1284,
306- 1285, 1286, 1287, 1288, 1289, 1408, 1409, 2308, 2309, 2436, 2437, 1296,
307- 1297,
308- 1298, 1299, 1300, 1301, 1302, 1303, 1304, 1305, 1424, 1425, 2324, 2325,
309- 2452,
310- 2453, 1312, 1313, 1314, 1315, 1316, 1317, 1318, 1319, 1320, 1321, 1410,
311- 1411,
312- 2340, 2341, 2376, 2377, 1328, 1329, 1330, 1331, 1332, 1333, 1334, 1335,
313- 1336,
314- 1337, 1426, 1427, 2356, 2357, 2392, 2393, 1344, 1345, 1346, 1347, 1348,
315- 1349,
316- 1350, 1351, 1352, 1353, 1412, 1413, 2372, 2373, 1416, 1417, 1360, 1361,
317- 1362,
318- 1363, 1364, 1365, 1366, 1367, 1368, 1369, 1428, 1429, 2388, 2389, 1432,
319- 1433,
320- 1376, 1377, 1378, 1379, 1380, 1381, 1382, 1383, 1384, 1385, 1414, 1415,
321- 2404,
322- 2405, 2440, 2441, 1392, 1393, 1394, 1395, 1396, 1397, 1398, 1399, 1400,
323- 1401,
324- 1430, 1431, 2420, 2421, 2456, 2457, 1536, 1537, 1538, 1539, 1540, 1541,
325- 1542,
326- 1543, 1544, 1545, 1664, 1665, 2054, 2055, 2182, 2183, 1552, 1553, 1554,
327- 1555,
328- 1556, 1557, 1558, 1559, 1560, 1561, 1680, 1681, 2070, 2071, 2198, 2199,
329- 1568,
330- 1569, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577, 1666, 1667, 2086,
331- 2087,
332- 2152, 2153, 1584, 1585, 1586, 1587, 1588, 1589, 1590, 1591, 1592, 1593,
333- 1682,
334- 1683, 2102, 2103, 2168, 2169, 1600, 1601, 1602, 1603, 1604, 1605, 1606,
335- 1607,
336- 1608, 1609, 1668, 1669, 2118, 2119, 1672, 1673, 1616, 1617, 1618, 1619,
337- 1620,
338- 1621, 1622, 1623, 1624, 1625, 1684, 1685, 2134, 2135, 1688, 1689, 1632,
339- 1633,
340- 1634, 1635, 1636, 1637, 1638, 1639, 1640, 1641, 1670, 1671, 2150, 2151,
341- 2184,
342- 2185, 1648, 1649, 1650, 1651, 1652, 1653, 1654, 1655, 1656, 1657, 1686,
343- 1687,
344- 2166, 2167, 2200, 2201, 1792, 1793, 1794, 1795, 1796, 1797, 1798, 1799,
345- 1800,
346- 1801, 1920, 1921, 2310, 2311, 2438, 2439, 1808, 1809, 1810, 1811, 1812,
347- 1813,
348- 1814, 1815, 1816, 1817, 1936, 1937, 2326, 2327, 2454, 2455, 1824, 1825,
349- 1826,
350- 1827, 1828, 1829, 1830, 1831, 1832, 1833, 1922, 1923, 2342, 2343, 2408,
351- 2409,
352- 1840, 1841, 1842, 1843, 1844, 1845, 1846, 1847, 1848, 1849, 1938, 1939,
353- 2358,
354- 2359, 2424, 2425, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864,
355- 1865,
356- 1924, 1925, 2374, 2375, 1928, 1929, 1872, 1873, 1874, 1875, 1876, 1877,
357- 1878,
358- 1879, 1880, 1881, 1940, 1941, 2390, 2391, 1944, 1945, 1888, 1889, 1890,
359- 1891,
360- 1892, 1893, 1894, 1895, 1896, 1897, 1926, 1927, 2406, 2407, 2440, 2441,
361- 1904,
362- 1905, 1906, 1907, 1908, 1909, 1910, 1911, 1912, 1913, 1942, 1943, 2422,
363- 2423,
364- 2456, 2457
365-};
622+const uint32_t DPD2BINM[1024]={0, 1000000, 2000000, 3000000, 4000000,
623+ 5000000, 6000000, 7000000, 8000000, 9000000, 80000000, 81000000,
624+ 800000000, 801000000, 880000000, 881000000, 10000000, 11000000, 12000000,
625+ 13000000, 14000000, 15000000, 16000000, 17000000, 18000000, 19000000,
626+ 90000000, 91000000, 810000000, 811000000, 890000000, 891000000, 20000000,
627+ 21000000, 22000000, 23000000, 24000000, 25000000, 26000000, 27000000,
628+ 28000000, 29000000, 82000000, 83000000, 820000000, 821000000, 808000000,
629+ 809000000, 30000000, 31000000, 32000000, 33000000, 34000000, 35000000,
630+ 36000000, 37000000, 38000000, 39000000, 92000000, 93000000, 830000000,
631+ 831000000, 818000000, 819000000, 40000000, 41000000, 42000000, 43000000,
632+ 44000000, 45000000, 46000000, 47000000, 48000000, 49000000, 84000000,
633+ 85000000, 840000000, 841000000, 88000000, 89000000, 50000000, 51000000,
634+ 52000000, 53000000, 54000000, 55000000, 56000000, 57000000, 58000000,
635+ 59000000, 94000000, 95000000, 850000000, 851000000, 98000000, 99000000,
636+ 60000000, 61000000, 62000000, 63000000, 64000000, 65000000, 66000000,
637+ 67000000, 68000000, 69000000, 86000000, 87000000, 860000000, 861000000,
638+ 888000000, 889000000, 70000000, 71000000, 72000000, 73000000, 74000000,
639+ 75000000, 76000000, 77000000, 78000000, 79000000, 96000000, 97000000,
640+ 870000000, 871000000, 898000000, 899000000, 100000000, 101000000, 102000000,
641+ 103000000, 104000000, 105000000, 106000000, 107000000, 108000000, 109000000,
642+ 180000000, 181000000, 900000000, 901000000, 980000000, 981000000, 110000000,
643+ 111000000, 112000000, 113000000, 114000000, 115000000, 116000000, 117000000,
644+ 118000000, 119000000, 190000000, 191000000, 910000000, 911000000, 990000000,
645+ 991000000, 120000000, 121000000, 122000000, 123000000, 124000000, 125000000,
646+ 126000000, 127000000, 128000000, 129000000, 182000000, 183000000, 920000000,
647+ 921000000, 908000000, 909000000, 130000000, 131000000, 132000000, 133000000,
648+ 134000000, 135000000, 136000000, 137000000, 138000000, 139000000, 192000000,
649+ 193000000, 930000000, 931000000, 918000000, 919000000, 140000000, 141000000,
650+ 142000000, 143000000, 144000000, 145000000, 146000000, 147000000, 148000000,
651+ 149000000, 184000000, 185000000, 940000000, 941000000, 188000000, 189000000,
652+ 150000000, 151000000, 152000000, 153000000, 154000000, 155000000, 156000000,
653+ 157000000, 158000000, 159000000, 194000000, 195000000, 950000000, 951000000,
654+ 198000000, 199000000, 160000000, 161000000, 162000000, 163000000, 164000000,
655+ 165000000, 166000000, 167000000, 168000000, 169000000, 186000000, 187000000,
656+ 960000000, 961000000, 988000000, 989000000, 170000000, 171000000, 172000000,
657+ 173000000, 174000000, 175000000, 176000000, 177000000, 178000000, 179000000,
658+ 196000000, 197000000, 970000000, 971000000, 998000000, 999000000, 200000000,
659+ 201000000, 202000000, 203000000, 204000000, 205000000, 206000000, 207000000,
660+ 208000000, 209000000, 280000000, 281000000, 802000000, 803000000, 882000000,
661+ 883000000, 210000000, 211000000, 212000000, 213000000, 214000000, 215000000,
662+ 216000000, 217000000, 218000000, 219000000, 290000000, 291000000, 812000000,
663+ 813000000, 892000000, 893000000, 220000000, 221000000, 222000000, 223000000,
664+ 224000000, 225000000, 226000000, 227000000, 228000000, 229000000, 282000000,
665+ 283000000, 822000000, 823000000, 828000000, 829000000, 230000000, 231000000,
666+ 232000000, 233000000, 234000000, 235000000, 236000000, 237000000, 238000000,
667+ 239000000, 292000000, 293000000, 832000000, 833000000, 838000000, 839000000,
668+ 240000000, 241000000, 242000000, 243000000, 244000000, 245000000, 246000000,
669+ 247000000, 248000000, 249000000, 284000000, 285000000, 842000000, 843000000,
670+ 288000000, 289000000, 250000000, 251000000, 252000000, 253000000, 254000000,
671+ 255000000, 256000000, 257000000, 258000000, 259000000, 294000000, 295000000,
672+ 852000000, 853000000, 298000000, 299000000, 260000000, 261000000, 262000000,
673+ 263000000, 264000000, 265000000, 266000000, 267000000, 268000000, 269000000,
674+ 286000000, 287000000, 862000000, 863000000, 888000000, 889000000, 270000000,
675+ 271000000, 272000000, 273000000, 274000000, 275000000, 276000000, 277000000,
676+ 278000000, 279000000, 296000000, 297000000, 872000000, 873000000, 898000000,
677+ 899000000, 300000000, 301000000, 302000000, 303000000, 304000000, 305000000,
678+ 306000000, 307000000, 308000000, 309000000, 380000000, 381000000, 902000000,
679+ 903000000, 982000000, 983000000, 310000000, 311000000, 312000000, 313000000,
680+ 314000000, 315000000, 316000000, 317000000, 318000000, 319000000, 390000000,
681+ 391000000, 912000000, 913000000, 992000000, 993000000, 320000000, 321000000,
682+ 322000000, 323000000, 324000000, 325000000, 326000000, 327000000, 328000000,
683+ 329000000, 382000000, 383000000, 922000000, 923000000, 928000000, 929000000,
684+ 330000000, 331000000, 332000000, 333000000, 334000000, 335000000, 336000000,
685+ 337000000, 338000000, 339000000, 392000000, 393000000, 932000000, 933000000,
686+ 938000000, 939000000, 340000000, 341000000, 342000000, 343000000, 344000000,
687+ 345000000, 346000000, 347000000, 348000000, 349000000, 384000000, 385000000,
688+ 942000000, 943000000, 388000000, 389000000, 350000000, 351000000, 352000000,
689+ 353000000, 354000000, 355000000, 356000000, 357000000, 358000000, 359000000,
690+ 394000000, 395000000, 952000000, 953000000, 398000000, 399000000, 360000000,
691+ 361000000, 362000000, 363000000, 364000000, 365000000, 366000000, 367000000,
692+ 368000000, 369000000, 386000000, 387000000, 962000000, 963000000, 988000000,
693+ 989000000, 370000000, 371000000, 372000000, 373000000, 374000000, 375000000,
694+ 376000000, 377000000, 378000000, 379000000, 396000000, 397000000, 972000000,
695+ 973000000, 998000000, 999000000, 400000000, 401000000, 402000000, 403000000,
696+ 404000000, 405000000, 406000000, 407000000, 408000000, 409000000, 480000000,
697+ 481000000, 804000000, 805000000, 884000000, 885000000, 410000000, 411000000,
698+ 412000000, 413000000, 414000000, 415000000, 416000000, 417000000, 418000000,
699+ 419000000, 490000000, 491000000, 814000000, 815000000, 894000000, 895000000,
700+ 420000000, 421000000, 422000000, 423000000, 424000000, 425000000, 426000000,
701+ 427000000, 428000000, 429000000, 482000000, 483000000, 824000000, 825000000,
702+ 848000000, 849000000, 430000000, 431000000, 432000000, 433000000, 434000000,
703+ 435000000, 436000000, 437000000, 438000000, 439000000, 492000000, 493000000,
704+ 834000000, 835000000, 858000000, 859000000, 440000000, 441000000, 442000000,
705+ 443000000, 444000000, 445000000, 446000000, 447000000, 448000000, 449000000,
706+ 484000000, 485000000, 844000000, 845000000, 488000000, 489000000, 450000000,
707+ 451000000, 452000000, 453000000, 454000000, 455000000, 456000000, 457000000,
708+ 458000000, 459000000, 494000000, 495000000, 854000000, 855000000, 498000000,
709+ 499000000, 460000000, 461000000, 462000000, 463000000, 464000000, 465000000,
710+ 466000000, 467000000, 468000000, 469000000, 486000000, 487000000, 864000000,
711+ 865000000, 888000000, 889000000, 470000000, 471000000, 472000000, 473000000,
712+ 474000000, 475000000, 476000000, 477000000, 478000000, 479000000, 496000000,
713+ 497000000, 874000000, 875000000, 898000000, 899000000, 500000000, 501000000,
714+ 502000000, 503000000, 504000000, 505000000, 506000000, 507000000, 508000000,
715+ 509000000, 580000000, 581000000, 904000000, 905000000, 984000000, 985000000,
716+ 510000000, 511000000, 512000000, 513000000, 514000000, 515000000, 516000000,
717+ 517000000, 518000000, 519000000, 590000000, 591000000, 914000000, 915000000,
718+ 994000000, 995000000, 520000000, 521000000, 522000000, 523000000, 524000000,
719+ 525000000, 526000000, 527000000, 528000000, 529000000, 582000000, 583000000,
720+ 924000000, 925000000, 948000000, 949000000, 530000000, 531000000, 532000000,
721+ 533000000, 534000000, 535000000, 536000000, 537000000, 538000000, 539000000,
722+ 592000000, 593000000, 934000000, 935000000, 958000000, 959000000, 540000000,
723+ 541000000, 542000000, 543000000, 544000000, 545000000, 546000000, 547000000,
724+ 548000000, 549000000, 584000000, 585000000, 944000000, 945000000, 588000000,
725+ 589000000, 550000000, 551000000, 552000000, 553000000, 554000000, 555000000,
726+ 556000000, 557000000, 558000000, 559000000, 594000000, 595000000, 954000000,
727+ 955000000, 598000000, 599000000, 560000000, 561000000, 562000000, 563000000,
728+ 564000000, 565000000, 566000000, 567000000, 568000000, 569000000, 586000000,
729+ 587000000, 964000000, 965000000, 988000000, 989000000, 570000000, 571000000,
730+ 572000000, 573000000, 574000000, 575000000, 576000000, 577000000, 578000000,
731+ 579000000, 596000000, 597000000, 974000000, 975000000, 998000000, 999000000,
732+ 600000000, 601000000, 602000000, 603000000, 604000000, 605000000, 606000000,
733+ 607000000, 608000000, 609000000, 680000000, 681000000, 806000000, 807000000,
734+ 886000000, 887000000, 610000000, 611000000, 612000000, 613000000, 614000000,
735+ 615000000, 616000000, 617000000, 618000000, 619000000, 690000000, 691000000,
736+ 816000000, 817000000, 896000000, 897000000, 620000000, 621000000, 622000000,
737+ 623000000, 624000000, 625000000, 626000000, 627000000, 628000000, 629000000,
738+ 682000000, 683000000, 826000000, 827000000, 868000000, 869000000, 630000000,
739+ 631000000, 632000000, 633000000, 634000000, 635000000, 636000000, 637000000,
740+ 638000000, 639000000, 692000000, 693000000, 836000000, 837000000, 878000000,
741+ 879000000, 640000000, 641000000, 642000000, 643000000, 644000000, 645000000,
742+ 646000000, 647000000, 648000000, 649000000, 684000000, 685000000, 846000000,
743+ 847000000, 688000000, 689000000, 650000000, 651000000, 652000000, 653000000,
744+ 654000000, 655000000, 656000000, 657000000, 658000000, 659000000, 694000000,
745+ 695000000, 856000000, 857000000, 698000000, 699000000, 660000000, 661000000,
746+ 662000000, 663000000, 664000000, 665000000, 666000000, 667000000, 668000000,
747+ 669000000, 686000000, 687000000, 866000000, 867000000, 888000000, 889000000,
748+ 670000000, 671000000, 672000000, 673000000, 674000000, 675000000, 676000000,
749+ 677000000, 678000000, 679000000, 696000000, 697000000, 876000000, 877000000,
750+ 898000000, 899000000, 700000000, 701000000, 702000000, 703000000, 704000000,
751+ 705000000, 706000000, 707000000, 708000000, 709000000, 780000000, 781000000,
752+ 906000000, 907000000, 986000000, 987000000, 710000000, 711000000, 712000000,
753+ 713000000, 714000000, 715000000, 716000000, 717000000, 718000000, 719000000,
754+ 790000000, 791000000, 916000000, 917000000, 996000000, 997000000, 720000000,
755+ 721000000, 722000000, 723000000, 724000000, 725000000, 726000000, 727000000,
756+ 728000000, 729000000, 782000000, 783000000, 926000000, 927000000, 968000000,
757+ 969000000, 730000000, 731000000, 732000000, 733000000, 734000000, 735000000,
758+ 736000000, 737000000, 738000000, 739000000, 792000000, 793000000, 936000000,
759+ 937000000, 978000000, 979000000, 740000000, 741000000, 742000000, 743000000,
760+ 744000000, 745000000, 746000000, 747000000, 748000000, 749000000, 784000000,
761+ 785000000, 946000000, 947000000, 788000000, 789000000, 750000000, 751000000,
762+ 752000000, 753000000, 754000000, 755000000, 756000000, 757000000, 758000000,
763+ 759000000, 794000000, 795000000, 956000000, 957000000, 798000000, 799000000,
764+ 760000000, 761000000, 762000000, 763000000, 764000000, 765000000, 766000000,
765+ 767000000, 768000000, 769000000, 786000000, 787000000, 966000000, 967000000,
766+ 988000000, 989000000, 770000000, 771000000, 772000000, 773000000, 774000000,
767+ 775000000, 776000000, 777000000, 778000000, 779000000, 796000000, 797000000,
768+ 976000000, 977000000, 998000000, 999000000};
366769 #endif
367770
368-#if DEC_BIN2DPD==1
771+#if defined(DEC_BIN2CHAR) && DEC_BIN2CHAR==1 && !defined(DECBIN2CHAR)
772+#define DECBIN2CHAR
369773
370-const uint16_t BIN2DPD[1000] = { 0, 1, 2, 3, 4, 5, 6, 7,
371- 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32,
372- 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51,
373- 52, 53, 54, 55, 56, 57, 64, 65, 66, 67, 68, 69, 70,
374- 71, 72, 73, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
375- 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 112, 113, 114,
376- 115, 116, 117, 118, 119, 120, 121, 10, 11, 42, 43, 74, 75,
377- 106, 107, 78, 79, 26, 27, 58, 59, 90, 91, 122, 123, 94,
378- 95, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 144, 145,
379- 146, 147, 148, 149, 150, 151, 152, 153, 160, 161, 162, 163, 164,
380- 165, 166, 167, 168, 169, 176, 177, 178, 179, 180, 181, 182, 183,
381- 184, 185, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 208,
382- 209, 210, 211, 212, 213, 214, 215, 216, 217, 224, 225, 226, 227,
383- 228, 229, 230, 231, 232, 233, 240, 241, 242, 243, 244, 245, 246,
384- 247, 248, 249, 138, 139, 170, 171, 202, 203, 234, 235, 206, 207,
385- 154, 155, 186, 187, 218, 219, 250, 251, 222, 223, 256, 257, 258,
386- 259, 260, 261, 262, 263, 264, 265, 272, 273, 274, 275, 276, 277,
387- 278, 279, 280, 281, 288, 289, 290, 291, 292, 293, 294, 295, 296,
388- 297, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 320, 321,
389- 322, 323, 324, 325, 326, 327, 328, 329, 336, 337, 338, 339, 340,
390- 341, 342, 343, 344, 345, 352, 353, 354, 355, 356, 357, 358, 359,
391- 360, 361, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 266,
392- 267, 298, 299, 330, 331, 362, 363, 334, 335, 282, 283, 314, 315,
393- 346, 347, 378, 379, 350, 351, 384, 385, 386, 387, 388, 389, 390,
394- 391, 392, 393, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409,
395- 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 432, 433, 434,
396- 435, 436, 437, 438, 439, 440, 441, 448, 449, 450, 451, 452, 453,
397- 454, 455, 456, 457, 464, 465, 466, 467, 468, 469, 470, 471, 472,
398- 473, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 496, 497,
399- 498, 499, 500, 501, 502, 503, 504, 505, 394, 395, 426, 427, 458,
400- 459, 490, 491, 462, 463, 410, 411, 442, 443, 474, 475, 506, 507,
401- 478, 479, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 528,
402- 529, 530, 531, 532, 533, 534, 535, 536, 537, 544, 545, 546, 547,
403- 548, 549, 550, 551, 552, 553, 560, 561, 562, 563, 564, 565, 566,
404- 567, 568, 569, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585,
405- 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 608, 609, 610,
406- 611, 612, 613, 614, 615, 616, 617, 624, 625, 626, 627, 628, 629,
407- 630, 631, 632, 633, 522, 523, 554, 555, 586, 587, 618, 619, 590,
408- 591, 538, 539, 570, 571, 602, 603, 634, 635, 606, 607, 640, 641,
409- 642, 643, 644, 645, 646, 647, 648, 649, 656, 657, 658, 659, 660,
410- 661, 662, 663, 664, 665, 672, 673, 674, 675, 676, 677, 678, 679,
411- 680, 681, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 704,
412- 705, 706, 707, 708, 709, 710, 711, 712, 713, 720, 721, 722, 723,
413- 724, 725, 726, 727, 728, 729, 736, 737, 738, 739, 740, 741, 742,
414- 743, 744, 745, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761,
415- 650, 651, 682, 683, 714, 715, 746, 747, 718, 719, 666, 667, 698,
416- 699, 730, 731, 762, 763, 734, 735, 768, 769, 770, 771, 772, 773,
417- 774, 775, 776, 777, 784, 785, 786, 787, 788, 789, 790, 791, 792,
418- 793, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 816, 817,
419- 818, 819, 820, 821, 822, 823, 824, 825, 832, 833, 834, 835, 836,
420- 837, 838, 839, 840, 841, 848, 849, 850, 851, 852, 853, 854, 855,
421- 856, 857, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 880,
422- 881, 882, 883, 884, 885, 886, 887, 888, 889, 778, 779, 810, 811,
423- 842, 843, 874, 875, 846, 847, 794, 795, 826, 827, 858, 859, 890,
424- 891, 862, 863, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905,
425- 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 928, 929, 930,
426- 931, 932, 933, 934, 935, 936, 937, 944, 945, 946, 947, 948, 949,
427- 950, 951, 952, 953, 960, 961, 962, 963, 964, 965, 966, 967, 968,
428- 969, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 992, 993,
429- 994, 995, 996, 997, 998, 999, 1000, 1001, 1008, 1009, 1010, 1011, 1012,
430- 1013, 1014, 1015, 1016, 1017, 906, 907, 938, 939, 970, 971, 1002, 1003,
431- 974, 975, 922, 923, 954, 955, 986, 987, 1018, 1019, 990, 991, 12,
432- 13, 268, 269, 524, 525, 780, 781, 46, 47, 28, 29, 284, 285,
433- 540, 541, 796, 797, 62, 63, 44, 45, 300, 301, 556, 557, 812,
434- 813, 302, 303, 60, 61, 316, 317, 572, 573, 828, 829, 318, 319,
435- 76, 77, 332, 333, 588, 589, 844, 845, 558, 559, 92, 93, 348,
436- 349, 604, 605, 860, 861, 574, 575, 108, 109, 364, 365, 620, 621,
437- 876, 877, 814, 815, 124, 125, 380, 381, 636, 637, 892, 893, 830,
438- 831, 14, 15, 270, 271, 526, 527, 782, 783, 110, 111, 30, 31,
439- 286, 287, 542, 543, 798, 799, 126, 127, 140, 141, 396, 397, 652,
440- 653, 908, 909, 174, 175, 156, 157, 412, 413, 668, 669, 924, 925,
441- 190, 191, 172, 173, 428, 429, 684, 685, 940, 941, 430, 431, 188,
442- 189, 444, 445, 700, 701, 956, 957, 446, 447, 204, 205, 460, 461,
443- 716, 717, 972, 973, 686, 687, 220, 221, 476, 477, 732, 733, 988,
444- 989, 702, 703, 236, 237, 492, 493, 748, 749, 1004, 1005, 942, 943,
445- 252, 253, 508, 509, 764, 765, 1020, 1021, 958, 959, 142, 143, 398,
446- 399, 654, 655, 910, 911, 238, 239, 158, 159, 414, 415, 670, 671,
447- 926, 927, 254, 255
448-};
774+const uint8_t BIN2CHAR[4001]={
775+ '\0','0','0','0', '\1','0','0','1', '\1','0','0','2', '\1','0','0','3', '\1','0','0','4',
776+ '\1','0','0','5', '\1','0','0','6', '\1','0','0','7', '\1','0','0','8', '\1','0','0','9',
777+ '\2','0','1','0', '\2','0','1','1', '\2','0','1','2', '\2','0','1','3', '\2','0','1','4',
778+ '\2','0','1','5', '\2','0','1','6', '\2','0','1','7', '\2','0','1','8', '\2','0','1','9',
779+ '\2','0','2','0', '\2','0','2','1', '\2','0','2','2', '\2','0','2','3', '\2','0','2','4',
780+ '\2','0','2','5', '\2','0','2','6', '\2','0','2','7', '\2','0','2','8', '\2','0','2','9',
781+ '\2','0','3','0', '\2','0','3','1', '\2','0','3','2', '\2','0','3','3', '\2','0','3','4',
782+ '\2','0','3','5', '\2','0','3','6', '\2','0','3','7', '\2','0','3','8', '\2','0','3','9',
783+ '\2','0','4','0', '\2','0','4','1', '\2','0','4','2', '\2','0','4','3', '\2','0','4','4',
784+ '\2','0','4','5', '\2','0','4','6', '\2','0','4','7', '\2','0','4','8', '\2','0','4','9',
785+ '\2','0','5','0', '\2','0','5','1', '\2','0','5','2', '\2','0','5','3', '\2','0','5','4',
786+ '\2','0','5','5', '\2','0','5','6', '\2','0','5','7', '\2','0','5','8', '\2','0','5','9',
787+ '\2','0','6','0', '\2','0','6','1', '\2','0','6','2', '\2','0','6','3', '\2','0','6','4',
788+ '\2','0','6','5', '\2','0','6','6', '\2','0','6','7', '\2','0','6','8', '\2','0','6','9',
789+ '\2','0','7','0', '\2','0','7','1', '\2','0','7','2', '\2','0','7','3', '\2','0','7','4',
790+ '\2','0','7','5', '\2','0','7','6', '\2','0','7','7', '\2','0','7','8', '\2','0','7','9',
791+ '\2','0','8','0', '\2','0','8','1', '\2','0','8','2', '\2','0','8','3', '\2','0','8','4',
792+ '\2','0','8','5', '\2','0','8','6', '\2','0','8','7', '\2','0','8','8', '\2','0','8','9',
793+ '\2','0','9','0', '\2','0','9','1', '\2','0','9','2', '\2','0','9','3', '\2','0','9','4',
794+ '\2','0','9','5', '\2','0','9','6', '\2','0','9','7', '\2','0','9','8', '\2','0','9','9',
795+ '\3','1','0','0', '\3','1','0','1', '\3','1','0','2', '\3','1','0','3', '\3','1','0','4',
796+ '\3','1','0','5', '\3','1','0','6', '\3','1','0','7', '\3','1','0','8', '\3','1','0','9',
797+ '\3','1','1','0', '\3','1','1','1', '\3','1','1','2', '\3','1','1','3', '\3','1','1','4',
798+ '\3','1','1','5', '\3','1','1','6', '\3','1','1','7', '\3','1','1','8', '\3','1','1','9',
799+ '\3','1','2','0', '\3','1','2','1', '\3','1','2','2', '\3','1','2','3', '\3','1','2','4',
800+ '\3','1','2','5', '\3','1','2','6', '\3','1','2','7', '\3','1','2','8', '\3','1','2','9',
801+ '\3','1','3','0', '\3','1','3','1', '\3','1','3','2', '\3','1','3','3', '\3','1','3','4',
802+ '\3','1','3','5', '\3','1','3','6', '\3','1','3','7', '\3','1','3','8', '\3','1','3','9',
803+ '\3','1','4','0', '\3','1','4','1', '\3','1','4','2', '\3','1','4','3', '\3','1','4','4',
804+ '\3','1','4','5', '\3','1','4','6', '\3','1','4','7', '\3','1','4','8', '\3','1','4','9',
805+ '\3','1','5','0', '\3','1','5','1', '\3','1','5','2', '\3','1','5','3', '\3','1','5','4',
806+ '\3','1','5','5', '\3','1','5','6', '\3','1','5','7', '\3','1','5','8', '\3','1','5','9',
807+ '\3','1','6','0', '\3','1','6','1', '\3','1','6','2', '\3','1','6','3', '\3','1','6','4',
808+ '\3','1','6','5', '\3','1','6','6', '\3','1','6','7', '\3','1','6','8', '\3','1','6','9',
809+ '\3','1','7','0', '\3','1','7','1', '\3','1','7','2', '\3','1','7','3', '\3','1','7','4',
810+ '\3','1','7','5', '\3','1','7','6', '\3','1','7','7', '\3','1','7','8', '\3','1','7','9',
811+ '\3','1','8','0', '\3','1','8','1', '\3','1','8','2', '\3','1','8','3', '\3','1','8','4',
812+ '\3','1','8','5', '\3','1','8','6', '\3','1','8','7', '\3','1','8','8', '\3','1','8','9',
813+ '\3','1','9','0', '\3','1','9','1', '\3','1','9','2', '\3','1','9','3', '\3','1','9','4',
814+ '\3','1','9','5', '\3','1','9','6', '\3','1','9','7', '\3','1','9','8', '\3','1','9','9',
815+ '\3','2','0','0', '\3','2','0','1', '\3','2','0','2', '\3','2','0','3', '\3','2','0','4',
816+ '\3','2','0','5', '\3','2','0','6', '\3','2','0','7', '\3','2','0','8', '\3','2','0','9',
817+ '\3','2','1','0', '\3','2','1','1', '\3','2','1','2', '\3','2','1','3', '\3','2','1','4',
818+ '\3','2','1','5', '\3','2','1','6', '\3','2','1','7', '\3','2','1','8', '\3','2','1','9',
819+ '\3','2','2','0', '\3','2','2','1', '\3','2','2','2', '\3','2','2','3', '\3','2','2','4',
820+ '\3','2','2','5', '\3','2','2','6', '\3','2','2','7', '\3','2','2','8', '\3','2','2','9',
821+ '\3','2','3','0', '\3','2','3','1', '\3','2','3','2', '\3','2','3','3', '\3','2','3','4',
822+ '\3','2','3','5', '\3','2','3','6', '\3','2','3','7', '\3','2','3','8', '\3','2','3','9',
823+ '\3','2','4','0', '\3','2','4','1', '\3','2','4','2', '\3','2','4','3', '\3','2','4','4',
824+ '\3','2','4','5', '\3','2','4','6', '\3','2','4','7', '\3','2','4','8', '\3','2','4','9',
825+ '\3','2','5','0', '\3','2','5','1', '\3','2','5','2', '\3','2','5','3', '\3','2','5','4',
826+ '\3','2','5','5', '\3','2','5','6', '\3','2','5','7', '\3','2','5','8', '\3','2','5','9',
827+ '\3','2','6','0', '\3','2','6','1', '\3','2','6','2', '\3','2','6','3', '\3','2','6','4',
828+ '\3','2','6','5', '\3','2','6','6', '\3','2','6','7', '\3','2','6','8', '\3','2','6','9',
829+ '\3','2','7','0', '\3','2','7','1', '\3','2','7','2', '\3','2','7','3', '\3','2','7','4',
830+ '\3','2','7','5', '\3','2','7','6', '\3','2','7','7', '\3','2','7','8', '\3','2','7','9',
831+ '\3','2','8','0', '\3','2','8','1', '\3','2','8','2', '\3','2','8','3', '\3','2','8','4',
832+ '\3','2','8','5', '\3','2','8','6', '\3','2','8','7', '\3','2','8','8', '\3','2','8','9',
833+ '\3','2','9','0', '\3','2','9','1', '\3','2','9','2', '\3','2','9','3', '\3','2','9','4',
834+ '\3','2','9','5', '\3','2','9','6', '\3','2','9','7', '\3','2','9','8', '\3','2','9','9',
835+ '\3','3','0','0', '\3','3','0','1', '\3','3','0','2', '\3','3','0','3', '\3','3','0','4',
836+ '\3','3','0','5', '\3','3','0','6', '\3','3','0','7', '\3','3','0','8', '\3','3','0','9',
837+ '\3','3','1','0', '\3','3','1','1', '\3','3','1','2', '\3','3','1','3', '\3','3','1','4',
838+ '\3','3','1','5', '\3','3','1','6', '\3','3','1','7', '\3','3','1','8', '\3','3','1','9',
839+ '\3','3','2','0', '\3','3','2','1', '\3','3','2','2', '\3','3','2','3', '\3','3','2','4',
840+ '\3','3','2','5', '\3','3','2','6', '\3','3','2','7', '\3','3','2','8', '\3','3','2','9',
841+ '\3','3','3','0', '\3','3','3','1', '\3','3','3','2', '\3','3','3','3', '\3','3','3','4',
842+ '\3','3','3','5', '\3','3','3','6', '\3','3','3','7', '\3','3','3','8', '\3','3','3','9',
843+ '\3','3','4','0', '\3','3','4','1', '\3','3','4','2', '\3','3','4','3', '\3','3','4','4',
844+ '\3','3','4','5', '\3','3','4','6', '\3','3','4','7', '\3','3','4','8', '\3','3','4','9',
845+ '\3','3','5','0', '\3','3','5','1', '\3','3','5','2', '\3','3','5','3', '\3','3','5','4',
846+ '\3','3','5','5', '\3','3','5','6', '\3','3','5','7', '\3','3','5','8', '\3','3','5','9',
847+ '\3','3','6','0', '\3','3','6','1', '\3','3','6','2', '\3','3','6','3', '\3','3','6','4',
848+ '\3','3','6','5', '\3','3','6','6', '\3','3','6','7', '\3','3','6','8', '\3','3','6','9',
849+ '\3','3','7','0', '\3','3','7','1', '\3','3','7','2', '\3','3','7','3', '\3','3','7','4',
850+ '\3','3','7','5', '\3','3','7','6', '\3','3','7','7', '\3','3','7','8', '\3','3','7','9',
851+ '\3','3','8','0', '\3','3','8','1', '\3','3','8','2', '\3','3','8','3', '\3','3','8','4',
852+ '\3','3','8','5', '\3','3','8','6', '\3','3','8','7', '\3','3','8','8', '\3','3','8','9',
853+ '\3','3','9','0', '\3','3','9','1', '\3','3','9','2', '\3','3','9','3', '\3','3','9','4',
854+ '\3','3','9','5', '\3','3','9','6', '\3','3','9','7', '\3','3','9','8', '\3','3','9','9',
855+ '\3','4','0','0', '\3','4','0','1', '\3','4','0','2', '\3','4','0','3', '\3','4','0','4',
856+ '\3','4','0','5', '\3','4','0','6', '\3','4','0','7', '\3','4','0','8', '\3','4','0','9',
857+ '\3','4','1','0', '\3','4','1','1', '\3','4','1','2', '\3','4','1','3', '\3','4','1','4',
858+ '\3','4','1','5', '\3','4','1','6', '\3','4','1','7', '\3','4','1','8', '\3','4','1','9',
859+ '\3','4','2','0', '\3','4','2','1', '\3','4','2','2', '\3','4','2','3', '\3','4','2','4',
860+ '\3','4','2','5', '\3','4','2','6', '\3','4','2','7', '\3','4','2','8', '\3','4','2','9',
861+ '\3','4','3','0', '\3','4','3','1', '\3','4','3','2', '\3','4','3','3', '\3','4','3','4',
862+ '\3','4','3','5', '\3','4','3','6', '\3','4','3','7', '\3','4','3','8', '\3','4','3','9',
863+ '\3','4','4','0', '\3','4','4','1', '\3','4','4','2', '\3','4','4','3', '\3','4','4','4',
864+ '\3','4','4','5', '\3','4','4','6', '\3','4','4','7', '\3','4','4','8', '\3','4','4','9',
865+ '\3','4','5','0', '\3','4','5','1', '\3','4','5','2', '\3','4','5','3', '\3','4','5','4',
866+ '\3','4','5','5', '\3','4','5','6', '\3','4','5','7', '\3','4','5','8', '\3','4','5','9',
867+ '\3','4','6','0', '\3','4','6','1', '\3','4','6','2', '\3','4','6','3', '\3','4','6','4',
868+ '\3','4','6','5', '\3','4','6','6', '\3','4','6','7', '\3','4','6','8', '\3','4','6','9',
869+ '\3','4','7','0', '\3','4','7','1', '\3','4','7','2', '\3','4','7','3', '\3','4','7','4',
870+ '\3','4','7','5', '\3','4','7','6', '\3','4','7','7', '\3','4','7','8', '\3','4','7','9',
871+ '\3','4','8','0', '\3','4','8','1', '\3','4','8','2', '\3','4','8','3', '\3','4','8','4',
872+ '\3','4','8','5', '\3','4','8','6', '\3','4','8','7', '\3','4','8','8', '\3','4','8','9',
873+ '\3','4','9','0', '\3','4','9','1', '\3','4','9','2', '\3','4','9','3', '\3','4','9','4',
874+ '\3','4','9','5', '\3','4','9','6', '\3','4','9','7', '\3','4','9','8', '\3','4','9','9',
875+ '\3','5','0','0', '\3','5','0','1', '\3','5','0','2', '\3','5','0','3', '\3','5','0','4',
876+ '\3','5','0','5', '\3','5','0','6', '\3','5','0','7', '\3','5','0','8', '\3','5','0','9',
877+ '\3','5','1','0', '\3','5','1','1', '\3','5','1','2', '\3','5','1','3', '\3','5','1','4',
878+ '\3','5','1','5', '\3','5','1','6', '\3','5','1','7', '\3','5','1','8', '\3','5','1','9',
879+ '\3','5','2','0', '\3','5','2','1', '\3','5','2','2', '\3','5','2','3', '\3','5','2','4',
880+ '\3','5','2','5', '\3','5','2','6', '\3','5','2','7', '\3','5','2','8', '\3','5','2','9',
881+ '\3','5','3','0', '\3','5','3','1', '\3','5','3','2', '\3','5','3','3', '\3','5','3','4',
882+ '\3','5','3','5', '\3','5','3','6', '\3','5','3','7', '\3','5','3','8', '\3','5','3','9',
883+ '\3','5','4','0', '\3','5','4','1', '\3','5','4','2', '\3','5','4','3', '\3','5','4','4',
884+ '\3','5','4','5', '\3','5','4','6', '\3','5','4','7', '\3','5','4','8', '\3','5','4','9',
885+ '\3','5','5','0', '\3','5','5','1', '\3','5','5','2', '\3','5','5','3', '\3','5','5','4',
886+ '\3','5','5','5', '\3','5','5','6', '\3','5','5','7', '\3','5','5','8', '\3','5','5','9',
887+ '\3','5','6','0', '\3','5','6','1', '\3','5','6','2', '\3','5','6','3', '\3','5','6','4',
888+ '\3','5','6','5', '\3','5','6','6', '\3','5','6','7', '\3','5','6','8', '\3','5','6','9',
889+ '\3','5','7','0', '\3','5','7','1', '\3','5','7','2', '\3','5','7','3', '\3','5','7','4',
890+ '\3','5','7','5', '\3','5','7','6', '\3','5','7','7', '\3','5','7','8', '\3','5','7','9',
891+ '\3','5','8','0', '\3','5','8','1', '\3','5','8','2', '\3','5','8','3', '\3','5','8','4',
892+ '\3','5','8','5', '\3','5','8','6', '\3','5','8','7', '\3','5','8','8', '\3','5','8','9',
893+ '\3','5','9','0', '\3','5','9','1', '\3','5','9','2', '\3','5','9','3', '\3','5','9','4',
894+ '\3','5','9','5', '\3','5','9','6', '\3','5','9','7', '\3','5','9','8', '\3','5','9','9',
895+ '\3','6','0','0', '\3','6','0','1', '\3','6','0','2', '\3','6','0','3', '\3','6','0','4',
896+ '\3','6','0','5', '\3','6','0','6', '\3','6','0','7', '\3','6','0','8', '\3','6','0','9',
897+ '\3','6','1','0', '\3','6','1','1', '\3','6','1','2', '\3','6','1','3', '\3','6','1','4',
898+ '\3','6','1','5', '\3','6','1','6', '\3','6','1','7', '\3','6','1','8', '\3','6','1','9',
899+ '\3','6','2','0', '\3','6','2','1', '\3','6','2','2', '\3','6','2','3', '\3','6','2','4',
900+ '\3','6','2','5', '\3','6','2','6', '\3','6','2','7', '\3','6','2','8', '\3','6','2','9',
901+ '\3','6','3','0', '\3','6','3','1', '\3','6','3','2', '\3','6','3','3', '\3','6','3','4',
902+ '\3','6','3','5', '\3','6','3','6', '\3','6','3','7', '\3','6','3','8', '\3','6','3','9',
903+ '\3','6','4','0', '\3','6','4','1', '\3','6','4','2', '\3','6','4','3', '\3','6','4','4',
904+ '\3','6','4','5', '\3','6','4','6', '\3','6','4','7', '\3','6','4','8', '\3','6','4','9',
905+ '\3','6','5','0', '\3','6','5','1', '\3','6','5','2', '\3','6','5','3', '\3','6','5','4',
906+ '\3','6','5','5', '\3','6','5','6', '\3','6','5','7', '\3','6','5','8', '\3','6','5','9',
907+ '\3','6','6','0', '\3','6','6','1', '\3','6','6','2', '\3','6','6','3', '\3','6','6','4',
908+ '\3','6','6','5', '\3','6','6','6', '\3','6','6','7', '\3','6','6','8', '\3','6','6','9',
909+ '\3','6','7','0', '\3','6','7','1', '\3','6','7','2', '\3','6','7','3', '\3','6','7','4',
910+ '\3','6','7','5', '\3','6','7','6', '\3','6','7','7', '\3','6','7','8', '\3','6','7','9',
911+ '\3','6','8','0', '\3','6','8','1', '\3','6','8','2', '\3','6','8','3', '\3','6','8','4',
912+ '\3','6','8','5', '\3','6','8','6', '\3','6','8','7', '\3','6','8','8', '\3','6','8','9',
913+ '\3','6','9','0', '\3','6','9','1', '\3','6','9','2', '\3','6','9','3', '\3','6','9','4',
914+ '\3','6','9','5', '\3','6','9','6', '\3','6','9','7', '\3','6','9','8', '\3','6','9','9',
915+ '\3','7','0','0', '\3','7','0','1', '\3','7','0','2', '\3','7','0','3', '\3','7','0','4',
916+ '\3','7','0','5', '\3','7','0','6', '\3','7','0','7', '\3','7','0','8', '\3','7','0','9',
917+ '\3','7','1','0', '\3','7','1','1', '\3','7','1','2', '\3','7','1','3', '\3','7','1','4',
918+ '\3','7','1','5', '\3','7','1','6', '\3','7','1','7', '\3','7','1','8', '\3','7','1','9',
919+ '\3','7','2','0', '\3','7','2','1', '\3','7','2','2', '\3','7','2','3', '\3','7','2','4',
920+ '\3','7','2','5', '\3','7','2','6', '\3','7','2','7', '\3','7','2','8', '\3','7','2','9',
921+ '\3','7','3','0', '\3','7','3','1', '\3','7','3','2', '\3','7','3','3', '\3','7','3','4',
922+ '\3','7','3','5', '\3','7','3','6', '\3','7','3','7', '\3','7','3','8', '\3','7','3','9',
923+ '\3','7','4','0', '\3','7','4','1', '\3','7','4','2', '\3','7','4','3', '\3','7','4','4',
924+ '\3','7','4','5', '\3','7','4','6', '\3','7','4','7', '\3','7','4','8', '\3','7','4','9',
925+ '\3','7','5','0', '\3','7','5','1', '\3','7','5','2', '\3','7','5','3', '\3','7','5','4',
926+ '\3','7','5','5', '\3','7','5','6', '\3','7','5','7', '\3','7','5','8', '\3','7','5','9',
927+ '\3','7','6','0', '\3','7','6','1', '\3','7','6','2', '\3','7','6','3', '\3','7','6','4',
928+ '\3','7','6','5', '\3','7','6','6', '\3','7','6','7', '\3','7','6','8', '\3','7','6','9',
929+ '\3','7','7','0', '\3','7','7','1', '\3','7','7','2', '\3','7','7','3', '\3','7','7','4',
930+ '\3','7','7','5', '\3','7','7','6', '\3','7','7','7', '\3','7','7','8', '\3','7','7','9',
931+ '\3','7','8','0', '\3','7','8','1', '\3','7','8','2', '\3','7','8','3', '\3','7','8','4',
932+ '\3','7','8','5', '\3','7','8','6', '\3','7','8','7', '\3','7','8','8', '\3','7','8','9',
933+ '\3','7','9','0', '\3','7','9','1', '\3','7','9','2', '\3','7','9','3', '\3','7','9','4',
934+ '\3','7','9','5', '\3','7','9','6', '\3','7','9','7', '\3','7','9','8', '\3','7','9','9',
935+ '\3','8','0','0', '\3','8','0','1', '\3','8','0','2', '\3','8','0','3', '\3','8','0','4',
936+ '\3','8','0','5', '\3','8','0','6', '\3','8','0','7', '\3','8','0','8', '\3','8','0','9',
937+ '\3','8','1','0', '\3','8','1','1', '\3','8','1','2', '\3','8','1','3', '\3','8','1','4',
938+ '\3','8','1','5', '\3','8','1','6', '\3','8','1','7', '\3','8','1','8', '\3','8','1','9',
939+ '\3','8','2','0', '\3','8','2','1', '\3','8','2','2', '\3','8','2','3', '\3','8','2','4',
940+ '\3','8','2','5', '\3','8','2','6', '\3','8','2','7', '\3','8','2','8', '\3','8','2','9',
941+ '\3','8','3','0', '\3','8','3','1', '\3','8','3','2', '\3','8','3','3', '\3','8','3','4',
942+ '\3','8','3','5', '\3','8','3','6', '\3','8','3','7', '\3','8','3','8', '\3','8','3','9',
943+ '\3','8','4','0', '\3','8','4','1', '\3','8','4','2', '\3','8','4','3', '\3','8','4','4',
944+ '\3','8','4','5', '\3','8','4','6', '\3','8','4','7', '\3','8','4','8', '\3','8','4','9',
945+ '\3','8','5','0', '\3','8','5','1', '\3','8','5','2', '\3','8','5','3', '\3','8','5','4',
946+ '\3','8','5','5', '\3','8','5','6', '\3','8','5','7', '\3','8','5','8', '\3','8','5','9',
947+ '\3','8','6','0', '\3','8','6','1', '\3','8','6','2', '\3','8','6','3', '\3','8','6','4',
948+ '\3','8','6','5', '\3','8','6','6', '\3','8','6','7', '\3','8','6','8', '\3','8','6','9',
949+ '\3','8','7','0', '\3','8','7','1', '\3','8','7','2', '\3','8','7','3', '\3','8','7','4',
950+ '\3','8','7','5', '\3','8','7','6', '\3','8','7','7', '\3','8','7','8', '\3','8','7','9',
951+ '\3','8','8','0', '\3','8','8','1', '\3','8','8','2', '\3','8','8','3', '\3','8','8','4',
952+ '\3','8','8','5', '\3','8','8','6', '\3','8','8','7', '\3','8','8','8', '\3','8','8','9',
953+ '\3','8','9','0', '\3','8','9','1', '\3','8','9','2', '\3','8','9','3', '\3','8','9','4',
954+ '\3','8','9','5', '\3','8','9','6', '\3','8','9','7', '\3','8','9','8', '\3','8','9','9',
955+ '\3','9','0','0', '\3','9','0','1', '\3','9','0','2', '\3','9','0','3', '\3','9','0','4',
956+ '\3','9','0','5', '\3','9','0','6', '\3','9','0','7', '\3','9','0','8', '\3','9','0','9',
957+ '\3','9','1','0', '\3','9','1','1', '\3','9','1','2', '\3','9','1','3', '\3','9','1','4',
958+ '\3','9','1','5', '\3','9','1','6', '\3','9','1','7', '\3','9','1','8', '\3','9','1','9',
959+ '\3','9','2','0', '\3','9','2','1', '\3','9','2','2', '\3','9','2','3', '\3','9','2','4',
960+ '\3','9','2','5', '\3','9','2','6', '\3','9','2','7', '\3','9','2','8', '\3','9','2','9',
961+ '\3','9','3','0', '\3','9','3','1', '\3','9','3','2', '\3','9','3','3', '\3','9','3','4',
962+ '\3','9','3','5', '\3','9','3','6', '\3','9','3','7', '\3','9','3','8', '\3','9','3','9',
963+ '\3','9','4','0', '\3','9','4','1', '\3','9','4','2', '\3','9','4','3', '\3','9','4','4',
964+ '\3','9','4','5', '\3','9','4','6', '\3','9','4','7', '\3','9','4','8', '\3','9','4','9',
965+ '\3','9','5','0', '\3','9','5','1', '\3','9','5','2', '\3','9','5','3', '\3','9','5','4',
966+ '\3','9','5','5', '\3','9','5','6', '\3','9','5','7', '\3','9','5','8', '\3','9','5','9',
967+ '\3','9','6','0', '\3','9','6','1', '\3','9','6','2', '\3','9','6','3', '\3','9','6','4',
968+ '\3','9','6','5', '\3','9','6','6', '\3','9','6','7', '\3','9','6','8', '\3','9','6','9',
969+ '\3','9','7','0', '\3','9','7','1', '\3','9','7','2', '\3','9','7','3', '\3','9','7','4',
970+ '\3','9','7','5', '\3','9','7','6', '\3','9','7','7', '\3','9','7','8', '\3','9','7','9',
971+ '\3','9','8','0', '\3','9','8','1', '\3','9','8','2', '\3','9','8','3', '\3','9','8','4',
972+ '\3','9','8','5', '\3','9','8','6', '\3','9','8','7', '\3','9','8','8', '\3','9','8','9',
973+ '\3','9','9','0', '\3','9','9','1', '\3','9','9','2', '\3','9','9','3', '\3','9','9','4',
974+ '\3','9','9','5', '\3','9','9','6', '\3','9','9','7', '\3','9','9','8', '\3','9','9','9', '\0'};
449975 #endif
450976
451-#if DEC_DPD2BIN==1
977+#if defined(DEC_DPD2BCD8) && DEC_DPD2BCD8==1 && !defined(DECDPD2BCD8)
978+#define DECDPD2BCD8
452979
453-const uint16_t DPD2BIN[1024] = { 0, 1, 2, 3, 4, 5, 6, 7,
454- 8, 9, 80, 81, 800, 801, 880, 881, 10, 11, 12, 13, 14,
455- 15, 16, 17, 18, 19, 90, 91, 810, 811, 890, 891, 20, 21,
456- 22, 23, 24, 25, 26, 27, 28, 29, 82, 83, 820, 821, 808,
457- 809, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 92, 93,
458- 830, 831, 818, 819, 40, 41, 42, 43, 44, 45, 46, 47, 48,
459- 49, 84, 85, 840, 841, 88, 89, 50, 51, 52, 53, 54, 55,
460- 56, 57, 58, 59, 94, 95, 850, 851, 98, 99, 60, 61, 62,
461- 63, 64, 65, 66, 67, 68, 69, 86, 87, 860, 861, 888, 889,
462- 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 96, 97, 870,
463- 871, 898, 899, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
464- 180, 181, 900, 901, 980, 981, 110, 111, 112, 113, 114, 115, 116,
465- 117, 118, 119, 190, 191, 910, 911, 990, 991, 120, 121, 122, 123,
466- 124, 125, 126, 127, 128, 129, 182, 183, 920, 921, 908, 909, 130,
467- 131, 132, 133, 134, 135, 136, 137, 138, 139, 192, 193, 930, 931,
468- 918, 919, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 184,
469- 185, 940, 941, 188, 189, 150, 151, 152, 153, 154, 155, 156, 157,
470- 158, 159, 194, 195, 950, 951, 198, 199, 160, 161, 162, 163, 164,
471- 165, 166, 167, 168, 169, 186, 187, 960, 961, 988, 989, 170, 171,
472- 172, 173, 174, 175, 176, 177, 178, 179, 196, 197, 970, 971, 998,
473- 999, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 280, 281,
474- 802, 803, 882, 883, 210, 211, 212, 213, 214, 215, 216, 217, 218,
475- 219, 290, 291, 812, 813, 892, 893, 220, 221, 222, 223, 224, 225,
476- 226, 227, 228, 229, 282, 283, 822, 823, 828, 829, 230, 231, 232,
477- 233, 234, 235, 236, 237, 238, 239, 292, 293, 832, 833, 838, 839,
478- 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 284, 285, 842,
479- 843, 288, 289, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259,
480- 294, 295, 852, 853, 298, 299, 260, 261, 262, 263, 264, 265, 266,
481- 267, 268, 269, 286, 287, 862, 863, 888, 889, 270, 271, 272, 273,
482- 274, 275, 276, 277, 278, 279, 296, 297, 872, 873, 898, 899, 300,
483- 301, 302, 303, 304, 305, 306, 307, 308, 309, 380, 381, 902, 903,
484- 982, 983, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 390,
485- 391, 912, 913, 992, 993, 320, 321, 322, 323, 324, 325, 326, 327,
486- 328, 329, 382, 383, 922, 923, 928, 929, 330, 331, 332, 333, 334,
487- 335, 336, 337, 338, 339, 392, 393, 932, 933, 938, 939, 340, 341,
488- 342, 343, 344, 345, 346, 347, 348, 349, 384, 385, 942, 943, 388,
489- 389, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 394, 395,
490- 952, 953, 398, 399, 360, 361, 362, 363, 364, 365, 366, 367, 368,
491- 369, 386, 387, 962, 963, 988, 989, 370, 371, 372, 373, 374, 375,
492- 376, 377, 378, 379, 396, 397, 972, 973, 998, 999, 400, 401, 402,
493- 403, 404, 405, 406, 407, 408, 409, 480, 481, 804, 805, 884, 885,
494- 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 490, 491, 814,
495- 815, 894, 895, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429,
496- 482, 483, 824, 825, 848, 849, 430, 431, 432, 433, 434, 435, 436,
497- 437, 438, 439, 492, 493, 834, 835, 858, 859, 440, 441, 442, 443,
498- 444, 445, 446, 447, 448, 449, 484, 485, 844, 845, 488, 489, 450,
499- 451, 452, 453, 454, 455, 456, 457, 458, 459, 494, 495, 854, 855,
500- 498, 499, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 486,
501- 487, 864, 865, 888, 889, 470, 471, 472, 473, 474, 475, 476, 477,
502- 478, 479, 496, 497, 874, 875, 898, 899, 500, 501, 502, 503, 504,
503- 505, 506, 507, 508, 509, 580, 581, 904, 905, 984, 985, 510, 511,
504- 512, 513, 514, 515, 516, 517, 518, 519, 590, 591, 914, 915, 994,
505- 995, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 582, 583,
506- 924, 925, 948, 949, 530, 531, 532, 533, 534, 535, 536, 537, 538,
507- 539, 592, 593, 934, 935, 958, 959, 540, 541, 542, 543, 544, 545,
508- 546, 547, 548, 549, 584, 585, 944, 945, 588, 589, 550, 551, 552,
509- 553, 554, 555, 556, 557, 558, 559, 594, 595, 954, 955, 598, 599,
510- 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 586, 587, 964,
511- 965, 988, 989, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579,
512- 596, 597, 974, 975, 998, 999, 600, 601, 602, 603, 604, 605, 606,
513- 607, 608, 609, 680, 681, 806, 807, 886, 887, 610, 611, 612, 613,
514- 614, 615, 616, 617, 618, 619, 690, 691, 816, 817, 896, 897, 620,
515- 621, 622, 623, 624, 625, 626, 627, 628, 629, 682, 683, 826, 827,
516- 868, 869, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 692,
517- 693, 836, 837, 878, 879, 640, 641, 642, 643, 644, 645, 646, 647,
518- 648, 649, 684, 685, 846, 847, 688, 689, 650, 651, 652, 653, 654,
519- 655, 656, 657, 658, 659, 694, 695, 856, 857, 698, 699, 660, 661,
520- 662, 663, 664, 665, 666, 667, 668, 669, 686, 687, 866, 867, 888,
521- 889, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 696, 697,
522- 876, 877, 898, 899, 700, 701, 702, 703, 704, 705, 706, 707, 708,
523- 709, 780, 781, 906, 907, 986, 987, 710, 711, 712, 713, 714, 715,
524- 716, 717, 718, 719, 790, 791, 916, 917, 996, 997, 720, 721, 722,
525- 723, 724, 725, 726, 727, 728, 729, 782, 783, 926, 927, 968, 969,
526- 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 792, 793, 936,
527- 937, 978, 979, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749,
528- 784, 785, 946, 947, 788, 789, 750, 751, 752, 753, 754, 755, 756,
529- 757, 758, 759, 794, 795, 956, 957, 798, 799, 760, 761, 762, 763,
530- 764, 765, 766, 767, 768, 769, 786, 787, 966, 967, 988, 989, 770,
531- 771, 772, 773, 774, 775, 776, 777, 778, 779, 796, 797, 976, 977,
532- 998, 999
533-};
980+const uint8_t DPD2BCD8[4096]={
981+ 0,0,0,0, 0,0,1,1, 0,0,2,1, 0,0,3,1, 0,0,4,1, 0,0,5,1, 0,0,6,1, 0,0,7,1, 0,0,8,1,
982+ 0,0,9,1, 0,8,0,2, 0,8,1,2, 8,0,0,3, 8,0,1,3, 8,8,0,3, 8,8,1,3, 0,1,0,2, 0,1,1,2,
983+ 0,1,2,2, 0,1,3,2, 0,1,4,2, 0,1,5,2, 0,1,6,2, 0,1,7,2, 0,1,8,2, 0,1,9,2, 0,9,0,2,
984+ 0,9,1,2, 8,1,0,3, 8,1,1,3, 8,9,0,3, 8,9,1,3, 0,2,0,2, 0,2,1,2, 0,2,2,2, 0,2,3,2,
985+ 0,2,4,2, 0,2,5,2, 0,2,6,2, 0,2,7,2, 0,2,8,2, 0,2,9,2, 0,8,2,2, 0,8,3,2, 8,2,0,3,
986+ 8,2,1,3, 8,0,8,3, 8,0,9,3, 0,3,0,2, 0,3,1,2, 0,3,2,2, 0,3,3,2, 0,3,4,2, 0,3,5,2,
987+ 0,3,6,2, 0,3,7,2, 0,3,8,2, 0,3,9,2, 0,9,2,2, 0,9,3,2, 8,3,0,3, 8,3,1,3, 8,1,8,3,
988+ 8,1,9,3, 0,4,0,2, 0,4,1,2, 0,4,2,2, 0,4,3,2, 0,4,4,2, 0,4,5,2, 0,4,6,2, 0,4,7,2,
989+ 0,4,8,2, 0,4,9,2, 0,8,4,2, 0,8,5,2, 8,4,0,3, 8,4,1,3, 0,8,8,2, 0,8,9,2, 0,5,0,2,
990+ 0,5,1,2, 0,5,2,2, 0,5,3,2, 0,5,4,2, 0,5,5,2, 0,5,6,2, 0,5,7,2, 0,5,8,2, 0,5,9,2,
991+ 0,9,4,2, 0,9,5,2, 8,5,0,3, 8,5,1,3, 0,9,8,2, 0,9,9,2, 0,6,0,2, 0,6,1,2, 0,6,2,2,
992+ 0,6,3,2, 0,6,4,2, 0,6,5,2, 0,6,6,2, 0,6,7,2, 0,6,8,2, 0,6,9,2, 0,8,6,2, 0,8,7,2,
993+ 8,6,0,3, 8,6,1,3, 8,8,8,3, 8,8,9,3, 0,7,0,2, 0,7,1,2, 0,7,2,2, 0,7,3,2, 0,7,4,2,
994+ 0,7,5,2, 0,7,6,2, 0,7,7,2, 0,7,8,2, 0,7,9,2, 0,9,6,2, 0,9,7,2, 8,7,0,3, 8,7,1,3,
995+ 8,9,8,3, 8,9,9,3, 1,0,0,3, 1,0,1,3, 1,0,2,3, 1,0,3,3, 1,0,4,3, 1,0,5,3, 1,0,6,3,
996+ 1,0,7,3, 1,0,8,3, 1,0,9,3, 1,8,0,3, 1,8,1,3, 9,0,0,3, 9,0,1,3, 9,8,0,3, 9,8,1,3,
997+ 1,1,0,3, 1,1,1,3, 1,1,2,3, 1,1,3,3, 1,1,4,3, 1,1,5,3, 1,1,6,3, 1,1,7,3, 1,1,8,3,
998+ 1,1,9,3, 1,9,0,3, 1,9,1,3, 9,1,0,3, 9,1,1,3, 9,9,0,3, 9,9,1,3, 1,2,0,3, 1,2,1,3,
999+ 1,2,2,3, 1,2,3,3, 1,2,4,3, 1,2,5,3, 1,2,6,3, 1,2,7,3, 1,2,8,3, 1,2,9,3, 1,8,2,3,
1000+ 1,8,3,3, 9,2,0,3, 9,2,1,3, 9,0,8,3, 9,0,9,3, 1,3,0,3, 1,3,1,3, 1,3,2,3, 1,3,3,3,
1001+ 1,3,4,3, 1,3,5,3, 1,3,6,3, 1,3,7,3, 1,3,8,3, 1,3,9,3, 1,9,2,3, 1,9,3,3, 9,3,0,3,
1002+ 9,3,1,3, 9,1,8,3, 9,1,9,3, 1,4,0,3, 1,4,1,3, 1,4,2,3, 1,4,3,3, 1,4,4,3, 1,4,5,3,
1003+ 1,4,6,3, 1,4,7,3, 1,4,8,3, 1,4,9,3, 1,8,4,3, 1,8,5,3, 9,4,0,3, 9,4,1,3, 1,8,8,3,
1004+ 1,8,9,3, 1,5,0,3, 1,5,1,3, 1,5,2,3, 1,5,3,3, 1,5,4,3, 1,5,5,3, 1,5,6,3, 1,5,7,3,
1005+ 1,5,8,3, 1,5,9,3, 1,9,4,3, 1,9,5,3, 9,5,0,3, 9,5,1,3, 1,9,8,3, 1,9,9,3, 1,6,0,3,
1006+ 1,6,1,3, 1,6,2,3, 1,6,3,3, 1,6,4,3, 1,6,5,3, 1,6,6,3, 1,6,7,3, 1,6,8,3, 1,6,9,3,
1007+ 1,8,6,3, 1,8,7,3, 9,6,0,3, 9,6,1,3, 9,8,8,3, 9,8,9,3, 1,7,0,3, 1,7,1,3, 1,7,2,3,
1008+ 1,7,3,3, 1,7,4,3, 1,7,5,3, 1,7,6,3, 1,7,7,3, 1,7,8,3, 1,7,9,3, 1,9,6,3, 1,9,7,3,
1009+ 9,7,0,3, 9,7,1,3, 9,9,8,3, 9,9,9,3, 2,0,0,3, 2,0,1,3, 2,0,2,3, 2,0,3,3, 2,0,4,3,
1010+ 2,0,5,3, 2,0,6,3, 2,0,7,3, 2,0,8,3, 2,0,9,3, 2,8,0,3, 2,8,1,3, 8,0,2,3, 8,0,3,3,
1011+ 8,8,2,3, 8,8,3,3, 2,1,0,3, 2,1,1,3, 2,1,2,3, 2,1,3,3, 2,1,4,3, 2,1,5,3, 2,1,6,3,
1012+ 2,1,7,3, 2,1,8,3, 2,1,9,3, 2,9,0,3, 2,9,1,3, 8,1,2,3, 8,1,3,3, 8,9,2,3, 8,9,3,3,
1013+ 2,2,0,3, 2,2,1,3, 2,2,2,3, 2,2,3,3, 2,2,4,3, 2,2,5,3, 2,2,6,3, 2,2,7,3, 2,2,8,3,
1014+ 2,2,9,3, 2,8,2,3, 2,8,3,3, 8,2,2,3, 8,2,3,3, 8,2,8,3, 8,2,9,3, 2,3,0,3, 2,3,1,3,
1015+ 2,3,2,3, 2,3,3,3, 2,3,4,3, 2,3,5,3, 2,3,6,3, 2,3,7,3, 2,3,8,3, 2,3,9,3, 2,9,2,3,
1016+ 2,9,3,3, 8,3,2,3, 8,3,3,3, 8,3,8,3, 8,3,9,3, 2,4,0,3, 2,4,1,3, 2,4,2,3, 2,4,3,3,
1017+ 2,4,4,3, 2,4,5,3, 2,4,6,3, 2,4,7,3, 2,4,8,3, 2,4,9,3, 2,8,4,3, 2,8,5,3, 8,4,2,3,
1018+ 8,4,3,3, 2,8,8,3, 2,8,9,3, 2,5,0,3, 2,5,1,3, 2,5,2,3, 2,5,3,3, 2,5,4,3, 2,5,5,3,
1019+ 2,5,6,3, 2,5,7,3, 2,5,8,3, 2,5,9,3, 2,9,4,3, 2,9,5,3, 8,5,2,3, 8,5,3,3, 2,9,8,3,
1020+ 2,9,9,3, 2,6,0,3, 2,6,1,3, 2,6,2,3, 2,6,3,3, 2,6,4,3, 2,6,5,3, 2,6,6,3, 2,6,7,3,
1021+ 2,6,8,3, 2,6,9,3, 2,8,6,3, 2,8,7,3, 8,6,2,3, 8,6,3,3, 8,8,8,3, 8,8,9,3, 2,7,0,3,
1022+ 2,7,1,3, 2,7,2,3, 2,7,3,3, 2,7,4,3, 2,7,5,3, 2,7,6,3, 2,7,7,3, 2,7,8,3, 2,7,9,3,
1023+ 2,9,6,3, 2,9,7,3, 8,7,2,3, 8,7,3,3, 8,9,8,3, 8,9,9,3, 3,0,0,3, 3,0,1,3, 3,0,2,3,
1024+ 3,0,3,3, 3,0,4,3, 3,0,5,3, 3,0,6,3, 3,0,7,3, 3,0,8,3, 3,0,9,3, 3,8,0,3, 3,8,1,3,
1025+ 9,0,2,3, 9,0,3,3, 9,8,2,3, 9,8,3,3, 3,1,0,3, 3,1,1,3, 3,1,2,3, 3,1,3,3, 3,1,4,3,
1026+ 3,1,5,3, 3,1,6,3, 3,1,7,3, 3,1,8,3, 3,1,9,3, 3,9,0,3, 3,9,1,3, 9,1,2,3, 9,1,3,3,
1027+ 9,9,2,3, 9,9,3,3, 3,2,0,3, 3,2,1,3, 3,2,2,3, 3,2,3,3, 3,2,4,3, 3,2,5,3, 3,2,6,3,
1028+ 3,2,7,3, 3,2,8,3, 3,2,9,3, 3,8,2,3, 3,8,3,3, 9,2,2,3, 9,2,3,3, 9,2,8,3, 9,2,9,3,
1029+ 3,3,0,3, 3,3,1,3, 3,3,2,3, 3,3,3,3, 3,3,4,3, 3,3,5,3, 3,3,6,3, 3,3,7,3, 3,3,8,3,
1030+ 3,3,9,3, 3,9,2,3, 3,9,3,3, 9,3,2,3, 9,3,3,3, 9,3,8,3, 9,3,9,3, 3,4,0,3, 3,4,1,3,
1031+ 3,4,2,3, 3,4,3,3, 3,4,4,3, 3,4,5,3, 3,4,6,3, 3,4,7,3, 3,4,8,3, 3,4,9,3, 3,8,4,3,
1032+ 3,8,5,3, 9,4,2,3, 9,4,3,3, 3,8,8,3, 3,8,9,3, 3,5,0,3, 3,5,1,3, 3,5,2,3, 3,5,3,3,
1033+ 3,5,4,3, 3,5,5,3, 3,5,6,3, 3,5,7,3, 3,5,8,3, 3,5,9,3, 3,9,4,3, 3,9,5,3, 9,5,2,3,
1034+ 9,5,3,3, 3,9,8,3, 3,9,9,3, 3,6,0,3, 3,6,1,3, 3,6,2,3, 3,6,3,3, 3,6,4,3, 3,6,5,3,
1035+ 3,6,6,3, 3,6,7,3, 3,6,8,3, 3,6,9,3, 3,8,6,3, 3,8,7,3, 9,6,2,3, 9,6,3,3, 9,8,8,3,
1036+ 9,8,9,3, 3,7,0,3, 3,7,1,3, 3,7,2,3, 3,7,3,3, 3,7,4,3, 3,7,5,3, 3,7,6,3, 3,7,7,3,
1037+ 3,7,8,3, 3,7,9,3, 3,9,6,3, 3,9,7,3, 9,7,2,3, 9,7,3,3, 9,9,8,3, 9,9,9,3, 4,0,0,3,
1038+ 4,0,1,3, 4,0,2,3, 4,0,3,3, 4,0,4,3, 4,0,5,3, 4,0,6,3, 4,0,7,3, 4,0,8,3, 4,0,9,3,
1039+ 4,8,0,3, 4,8,1,3, 8,0,4,3, 8,0,5,3, 8,8,4,3, 8,8,5,3, 4,1,0,3, 4,1,1,3, 4,1,2,3,
1040+ 4,1,3,3, 4,1,4,3, 4,1,5,3, 4,1,6,3, 4,1,7,3, 4,1,8,3, 4,1,9,3, 4,9,0,3, 4,9,1,3,
1041+ 8,1,4,3, 8,1,5,3, 8,9,4,3, 8,9,5,3, 4,2,0,3, 4,2,1,3, 4,2,2,3, 4,2,3,3, 4,2,4,3,
1042+ 4,2,5,3, 4,2,6,3, 4,2,7,3, 4,2,8,3, 4,2,9,3, 4,8,2,3, 4,8,3,3, 8,2,4,3, 8,2,5,3,
1043+ 8,4,8,3, 8,4,9,3, 4,3,0,3, 4,3,1,3, 4,3,2,3, 4,3,3,3, 4,3,4,3, 4,3,5,3, 4,3,6,3,
1044+ 4,3,7,3, 4,3,8,3, 4,3,9,3, 4,9,2,3, 4,9,3,3, 8,3,4,3, 8,3,5,3, 8,5,8,3, 8,5,9,3,
1045+ 4,4,0,3, 4,4,1,3, 4,4,2,3, 4,4,3,3, 4,4,4,3, 4,4,5,3, 4,4,6,3, 4,4,7,3, 4,4,8,3,
1046+ 4,4,9,3, 4,8,4,3, 4,8,5,3, 8,4,4,3, 8,4,5,3, 4,8,8,3, 4,8,9,3, 4,5,0,3, 4,5,1,3,
1047+ 4,5,2,3, 4,5,3,3, 4,5,4,3, 4,5,5,3, 4,5,6,3, 4,5,7,3, 4,5,8,3, 4,5,9,3, 4,9,4,3,
1048+ 4,9,5,3, 8,5,4,3, 8,5,5,3, 4,9,8,3, 4,9,9,3, 4,6,0,3, 4,6,1,3, 4,6,2,3, 4,6,3,3,
1049+ 4,6,4,3, 4,6,5,3, 4,6,6,3, 4,6,7,3, 4,6,8,3, 4,6,9,3, 4,8,6,3, 4,8,7,3, 8,6,4,3,
1050+ 8,6,5,3, 8,8,8,3, 8,8,9,3, 4,7,0,3, 4,7,1,3, 4,7,2,3, 4,7,3,3, 4,7,4,3, 4,7,5,3,
1051+ 4,7,6,3, 4,7,7,3, 4,7,8,3, 4,7,9,3, 4,9,6,3, 4,9,7,3, 8,7,4,3, 8,7,5,3, 8,9,8,3,
1052+ 8,9,9,3, 5,0,0,3, 5,0,1,3, 5,0,2,3, 5,0,3,3, 5,0,4,3, 5,0,5,3, 5,0,6,3, 5,0,7,3,
1053+ 5,0,8,3, 5,0,9,3, 5,8,0,3, 5,8,1,3, 9,0,4,3, 9,0,5,3, 9,8,4,3, 9,8,5,3, 5,1,0,3,
1054+ 5,1,1,3, 5,1,2,3, 5,1,3,3, 5,1,4,3, 5,1,5,3, 5,1,6,3, 5,1,7,3, 5,1,8,3, 5,1,9,3,
1055+ 5,9,0,3, 5,9,1,3, 9,1,4,3, 9,1,5,3, 9,9,4,3, 9,9,5,3, 5,2,0,3, 5,2,1,3, 5,2,2,3,
1056+ 5,2,3,3, 5,2,4,3, 5,2,5,3, 5,2,6,3, 5,2,7,3, 5,2,8,3, 5,2,9,3, 5,8,2,3, 5,8,3,3,
1057+ 9,2,4,3, 9,2,5,3, 9,4,8,3, 9,4,9,3, 5,3,0,3, 5,3,1,3, 5,3,2,3, 5,3,3,3, 5,3,4,3,
1058+ 5,3,5,3, 5,3,6,3, 5,3,7,3, 5,3,8,3, 5,3,9,3, 5,9,2,3, 5,9,3,3, 9,3,4,3, 9,3,5,3,
1059+ 9,5,8,3, 9,5,9,3, 5,4,0,3, 5,4,1,3, 5,4,2,3, 5,4,3,3, 5,4,4,3, 5,4,5,3, 5,4,6,3,
1060+ 5,4,7,3, 5,4,8,3, 5,4,9,3, 5,8,4,3, 5,8,5,3, 9,4,4,3, 9,4,5,3, 5,8,8,3, 5,8,9,3,
1061+ 5,5,0,3, 5,5,1,3, 5,5,2,3, 5,5,3,3, 5,5,4,3, 5,5,5,3, 5,5,6,3, 5,5,7,3, 5,5,8,3,
1062+ 5,5,9,3, 5,9,4,3, 5,9,5,3, 9,5,4,3, 9,5,5,3, 5,9,8,3, 5,9,9,3, 5,6,0,3, 5,6,1,3,
1063+ 5,6,2,3, 5,6,3,3, 5,6,4,3, 5,6,5,3, 5,6,6,3, 5,6,7,3, 5,6,8,3, 5,6,9,3, 5,8,6,3,
1064+ 5,8,7,3, 9,6,4,3, 9,6,5,3, 9,8,8,3, 9,8,9,3, 5,7,0,3, 5,7,1,3, 5,7,2,3, 5,7,3,3,
1065+ 5,7,4,3, 5,7,5,3, 5,7,6,3, 5,7,7,3, 5,7,8,3, 5,7,9,3, 5,9,6,3, 5,9,7,3, 9,7,4,3,
1066+ 9,7,5,3, 9,9,8,3, 9,9,9,3, 6,0,0,3, 6,0,1,3, 6,0,2,3, 6,0,3,3, 6,0,4,3, 6,0,5,3,
1067+ 6,0,6,3, 6,0,7,3, 6,0,8,3, 6,0,9,3, 6,8,0,3, 6,8,1,3, 8,0,6,3, 8,0,7,3, 8,8,6,3,
1068+ 8,8,7,3, 6,1,0,3, 6,1,1,3, 6,1,2,3, 6,1,3,3, 6,1,4,3, 6,1,5,3, 6,1,6,3, 6,1,7,3,
1069+ 6,1,8,3, 6,1,9,3, 6,9,0,3, 6,9,1,3, 8,1,6,3, 8,1,7,3, 8,9,6,3, 8,9,7,3, 6,2,0,3,
1070+ 6,2,1,3, 6,2,2,3, 6,2,3,3, 6,2,4,3, 6,2,5,3, 6,2,6,3, 6,2,7,3, 6,2,8,3, 6,2,9,3,
1071+ 6,8,2,3, 6,8,3,3, 8,2,6,3, 8,2,7,3, 8,6,8,3, 8,6,9,3, 6,3,0,3, 6,3,1,3, 6,3,2,3,
1072+ 6,3,3,3, 6,3,4,3, 6,3,5,3, 6,3,6,3, 6,3,7,3, 6,3,8,3, 6,3,9,3, 6,9,2,3, 6,9,3,3,
1073+ 8,3,6,3, 8,3,7,3, 8,7,8,3, 8,7,9,3, 6,4,0,3, 6,4,1,3, 6,4,2,3, 6,4,3,3, 6,4,4,3,
1074+ 6,4,5,3, 6,4,6,3, 6,4,7,3, 6,4,8,3, 6,4,9,3, 6,8,4,3, 6,8,5,3, 8,4,6,3, 8,4,7,3,
1075+ 6,8,8,3, 6,8,9,3, 6,5,0,3, 6,5,1,3, 6,5,2,3, 6,5,3,3, 6,5,4,3, 6,5,5,3, 6,5,6,3,
1076+ 6,5,7,3, 6,5,8,3, 6,5,9,3, 6,9,4,3, 6,9,5,3, 8,5,6,3, 8,5,7,3, 6,9,8,3, 6,9,9,3,
1077+ 6,6,0,3, 6,6,1,3, 6,6,2,3, 6,6,3,3, 6,6,4,3, 6,6,5,3, 6,6,6,3, 6,6,7,3, 6,6,8,3,
1078+ 6,6,9,3, 6,8,6,3, 6,8,7,3, 8,6,6,3, 8,6,7,3, 8,8,8,3, 8,8,9,3, 6,7,0,3, 6,7,1,3,
1079+ 6,7,2,3, 6,7,3,3, 6,7,4,3, 6,7,5,3, 6,7,6,3, 6,7,7,3, 6,7,8,3, 6,7,9,3, 6,9,6,3,
1080+ 6,9,7,3, 8,7,6,3, 8,7,7,3, 8,9,8,3, 8,9,9,3, 7,0,0,3, 7,0,1,3, 7,0,2,3, 7,0,3,3,
1081+ 7,0,4,3, 7,0,5,3, 7,0,6,3, 7,0,7,3, 7,0,8,3, 7,0,9,3, 7,8,0,3, 7,8,1,3, 9,0,6,3,
1082+ 9,0,7,3, 9,8,6,3, 9,8,7,3, 7,1,0,3, 7,1,1,3, 7,1,2,3, 7,1,3,3, 7,1,4,3, 7,1,5,3,
1083+ 7,1,6,3, 7,1,7,3, 7,1,8,3, 7,1,9,3, 7,9,0,3, 7,9,1,3, 9,1,6,3, 9,1,7,3, 9,9,6,3,
1084+ 9,9,7,3, 7,2,0,3, 7,2,1,3, 7,2,2,3, 7,2,3,3, 7,2,4,3, 7,2,5,3, 7,2,6,3, 7,2,7,3,
1085+ 7,2,8,3, 7,2,9,3, 7,8,2,3, 7,8,3,3, 9,2,6,3, 9,2,7,3, 9,6,8,3, 9,6,9,3, 7,3,0,3,
1086+ 7,3,1,3, 7,3,2,3, 7,3,3,3, 7,3,4,3, 7,3,5,3, 7,3,6,3, 7,3,7,3, 7,3,8,3, 7,3,9,3,
1087+ 7,9,2,3, 7,9,3,3, 9,3,6,3, 9,3,7,3, 9,7,8,3, 9,7,9,3, 7,4,0,3, 7,4,1,3, 7,4,2,3,
1088+ 7,4,3,3, 7,4,4,3, 7,4,5,3, 7,4,6,3, 7,4,7,3, 7,4,8,3, 7,4,9,3, 7,8,4,3, 7,8,5,3,
1089+ 9,4,6,3, 9,4,7,3, 7,8,8,3, 7,8,9,3, 7,5,0,3, 7,5,1,3, 7,5,2,3, 7,5,3,3, 7,5,4,3,
1090+ 7,5,5,3, 7,5,6,3, 7,5,7,3, 7,5,8,3, 7,5,9,3, 7,9,4,3, 7,9,5,3, 9,5,6,3, 9,5,7,3,
1091+ 7,9,8,3, 7,9,9,3, 7,6,0,3, 7,6,1,3, 7,6,2,3, 7,6,3,3, 7,6,4,3, 7,6,5,3, 7,6,6,3,
1092+ 7,6,7,3, 7,6,8,3, 7,6,9,3, 7,8,6,3, 7,8,7,3, 9,6,6,3, 9,6,7,3, 9,8,8,3, 9,8,9,3,
1093+ 7,7,0,3, 7,7,1,3, 7,7,2,3, 7,7,3,3, 7,7,4,3, 7,7,5,3, 7,7,6,3, 7,7,7,3, 7,7,8,3,
1094+ 7,7,9,3, 7,9,6,3, 7,9,7,3, 9,7,6,3, 9,7,7,3, 9,9,8,3, 9,9,9,3};
5341095 #endif
1096+
1097+#if defined(DEC_BIN2BCD8) && DEC_BIN2BCD8==1 && !defined(DECBIN2BCD8)
1098+#define DECBIN2BCD8
1099+
1100+const uint8_t BIN2BCD8[4000]={
1101+ 0,0,0,0, 0,0,1,1, 0,0,2,1, 0,0,3,1, 0,0,4,1, 0,0,5,1, 0,0,6,1, 0,0,7,1, 0,0,8,1,
1102+ 0,0,9,1, 0,1,0,2, 0,1,1,2, 0,1,2,2, 0,1,3,2, 0,1,4,2, 0,1,5,2, 0,1,6,2, 0,1,7,2,
1103+ 0,1,8,2, 0,1,9,2, 0,2,0,2, 0,2,1,2, 0,2,2,2, 0,2,3,2, 0,2,4,2, 0,2,5,2, 0,2,6,2,
1104+ 0,2,7,2, 0,2,8,2, 0,2,9,2, 0,3,0,2, 0,3,1,2, 0,3,2,2, 0,3,3,2, 0,3,4,2, 0,3,5,2,
1105+ 0,3,6,2, 0,3,7,2, 0,3,8,2, 0,3,9,2, 0,4,0,2, 0,4,1,2, 0,4,2,2, 0,4,3,2, 0,4,4,2,
1106+ 0,4,5,2, 0,4,6,2, 0,4,7,2, 0,4,8,2, 0,4,9,2, 0,5,0,2, 0,5,1,2, 0,5,2,2, 0,5,3,2,
1107+ 0,5,4,2, 0,5,5,2, 0,5,6,2, 0,5,7,2, 0,5,8,2, 0,5,9,2, 0,6,0,2, 0,6,1,2, 0,6,2,2,
1108+ 0,6,3,2, 0,6,4,2, 0,6,5,2, 0,6,6,2, 0,6,7,2, 0,6,8,2, 0,6,9,2, 0,7,0,2, 0,7,1,2,
1109+ 0,7,2,2, 0,7,3,2, 0,7,4,2, 0,7,5,2, 0,7,6,2, 0,7,7,2, 0,7,8,2, 0,7,9,2, 0,8,0,2,
1110+ 0,8,1,2, 0,8,2,2, 0,8,3,2, 0,8,4,2, 0,8,5,2, 0,8,6,2, 0,8,7,2, 0,8,8,2, 0,8,9,2,
1111+ 0,9,0,2, 0,9,1,2, 0,9,2,2, 0,9,3,2, 0,9,4,2, 0,9,5,2, 0,9,6,2, 0,9,7,2, 0,9,8,2,
1112+ 0,9,9,2, 1,0,0,3, 1,0,1,3, 1,0,2,3, 1,0,3,3, 1,0,4,3, 1,0,5,3, 1,0,6,3, 1,0,7,3,
1113+ 1,0,8,3, 1,0,9,3, 1,1,0,3, 1,1,1,3, 1,1,2,3, 1,1,3,3, 1,1,4,3, 1,1,5,3, 1,1,6,3,
1114+ 1,1,7,3, 1,1,8,3, 1,1,9,3, 1,2,0,3, 1,2,1,3, 1,2,2,3, 1,2,3,3, 1,2,4,3, 1,2,5,3,
1115+ 1,2,6,3, 1,2,7,3, 1,2,8,3, 1,2,9,3, 1,3,0,3, 1,3,1,3, 1,3,2,3, 1,3,3,3, 1,3,4,3,
1116+ 1,3,5,3, 1,3,6,3, 1,3,7,3, 1,3,8,3, 1,3,9,3, 1,4,0,3, 1,4,1,3, 1,4,2,3, 1,4,3,3,
1117+ 1,4,4,3, 1,4,5,3, 1,4,6,3, 1,4,7,3, 1,4,8,3, 1,4,9,3, 1,5,0,3, 1,5,1,3, 1,5,2,3,
1118+ 1,5,3,3, 1,5,4,3, 1,5,5,3, 1,5,6,3, 1,5,7,3, 1,5,8,3, 1,5,9,3, 1,6,0,3, 1,6,1,3,
1119+ 1,6,2,3, 1,6,3,3, 1,6,4,3, 1,6,5,3, 1,6,6,3, 1,6,7,3, 1,6,8,3, 1,6,9,3, 1,7,0,3,
1120+ 1,7,1,3, 1,7,2,3, 1,7,3,3, 1,7,4,3, 1,7,5,3, 1,7,6,3, 1,7,7,3, 1,7,8,3, 1,7,9,3,
1121+ 1,8,0,3, 1,8,1,3, 1,8,2,3, 1,8,3,3, 1,8,4,3, 1,8,5,3, 1,8,6,3, 1,8,7,3, 1,8,8,3,
1122+ 1,8,9,3, 1,9,0,3, 1,9,1,3, 1,9,2,3, 1,9,3,3, 1,9,4,3, 1,9,5,3, 1,9,6,3, 1,9,7,3,
1123+ 1,9,8,3, 1,9,9,3, 2,0,0,3, 2,0,1,3, 2,0,2,3, 2,0,3,3, 2,0,4,3, 2,0,5,3, 2,0,6,3,
1124+ 2,0,7,3, 2,0,8,3, 2,0,9,3, 2,1,0,3, 2,1,1,3, 2,1,2,3, 2,1,3,3, 2,1,4,3, 2,1,5,3,
1125+ 2,1,6,3, 2,1,7,3, 2,1,8,3, 2,1,9,3, 2,2,0,3, 2,2,1,3, 2,2,2,3, 2,2,3,3, 2,2,4,3,
1126+ 2,2,5,3, 2,2,6,3, 2,2,7,3, 2,2,8,3, 2,2,9,3, 2,3,0,3, 2,3,1,3, 2,3,2,3, 2,3,3,3,
1127+ 2,3,4,3, 2,3,5,3, 2,3,6,3, 2,3,7,3, 2,3,8,3, 2,3,9,3, 2,4,0,3, 2,4,1,3, 2,4,2,3,
1128+ 2,4,3,3, 2,4,4,3, 2,4,5,3, 2,4,6,3, 2,4,7,3, 2,4,8,3, 2,4,9,3, 2,5,0,3, 2,5,1,3,
1129+ 2,5,2,3, 2,5,3,3, 2,5,4,3, 2,5,5,3, 2,5,6,3, 2,5,7,3, 2,5,8,3, 2,5,9,3, 2,6,0,3,
1130+ 2,6,1,3, 2,6,2,3, 2,6,3,3, 2,6,4,3, 2,6,5,3, 2,6,6,3, 2,6,7,3, 2,6,8,3, 2,6,9,3,
1131+ 2,7,0,3, 2,7,1,3, 2,7,2,3, 2,7,3,3, 2,7,4,3, 2,7,5,3, 2,7,6,3, 2,7,7,3, 2,7,8,3,
1132+ 2,7,9,3, 2,8,0,3, 2,8,1,3, 2,8,2,3, 2,8,3,3, 2,8,4,3, 2,8,5,3, 2,8,6,3, 2,8,7,3,
1133+ 2,8,8,3, 2,8,9,3, 2,9,0,3, 2,9,1,3, 2,9,2,3, 2,9,3,3, 2,9,4,3, 2,9,5,3, 2,9,6,3,
1134+ 2,9,7,3, 2,9,8,3, 2,9,9,3, 3,0,0,3, 3,0,1,3, 3,0,2,3, 3,0,3,3, 3,0,4,3, 3,0,5,3,
1135+ 3,0,6,3, 3,0,7,3, 3,0,8,3, 3,0,9,3, 3,1,0,3, 3,1,1,3, 3,1,2,3, 3,1,3,3, 3,1,4,3,
1136+ 3,1,5,3, 3,1,6,3, 3,1,7,3, 3,1,8,3, 3,1,9,3, 3,2,0,3, 3,2,1,3, 3,2,2,3, 3,2,3,3,
1137+ 3,2,4,3, 3,2,5,3, 3,2,6,3, 3,2,7,3, 3,2,8,3, 3,2,9,3, 3,3,0,3, 3,3,1,3, 3,3,2,3,
1138+ 3,3,3,3, 3,3,4,3, 3,3,5,3, 3,3,6,3, 3,3,7,3, 3,3,8,3, 3,3,9,3, 3,4,0,3, 3,4,1,3,
1139+ 3,4,2,3, 3,4,3,3, 3,4,4,3, 3,4,5,3, 3,4,6,3, 3,4,7,3, 3,4,8,3, 3,4,9,3, 3,5,0,3,
1140+ 3,5,1,3, 3,5,2,3, 3,5,3,3, 3,5,4,3, 3,5,5,3, 3,5,6,3, 3,5,7,3, 3,5,8,3, 3,5,9,3,
1141+ 3,6,0,3, 3,6,1,3, 3,6,2,3, 3,6,3,3, 3,6,4,3, 3,6,5,3, 3,6,6,3, 3,6,7,3, 3,6,8,3,
1142+ 3,6,9,3, 3,7,0,3, 3,7,1,3, 3,7,2,3, 3,7,3,3, 3,7,4,3, 3,7,5,3, 3,7,6,3, 3,7,7,3,
1143+ 3,7,8,3, 3,7,9,3, 3,8,0,3, 3,8,1,3, 3,8,2,3, 3,8,3,3, 3,8,4,3, 3,8,5,3, 3,8,6,3,
1144+ 3,8,7,3, 3,8,8,3, 3,8,9,3, 3,9,0,3, 3,9,1,3, 3,9,2,3, 3,9,3,3, 3,9,4,3, 3,9,5,3,
1145+ 3,9,6,3, 3,9,7,3, 3,9,8,3, 3,9,9,3, 4,0,0,3, 4,0,1,3, 4,0,2,3, 4,0,3,3, 4,0,4,3,
1146+ 4,0,5,3, 4,0,6,3, 4,0,7,3, 4,0,8,3, 4,0,9,3, 4,1,0,3, 4,1,1,3, 4,1,2,3, 4,1,3,3,
1147+ 4,1,4,3, 4,1,5,3, 4,1,6,3, 4,1,7,3, 4,1,8,3, 4,1,9,3, 4,2,0,3, 4,2,1,3, 4,2,2,3,
1148+ 4,2,3,3, 4,2,4,3, 4,2,5,3, 4,2,6,3, 4,2,7,3, 4,2,8,3, 4,2,9,3, 4,3,0,3, 4,3,1,3,
1149+ 4,3,2,3, 4,3,3,3, 4,3,4,3, 4,3,5,3, 4,3,6,3, 4,3,7,3, 4,3,8,3, 4,3,9,3, 4,4,0,3,
1150+ 4,4,1,3, 4,4,2,3, 4,4,3,3, 4,4,4,3, 4,4,5,3, 4,4,6,3, 4,4,7,3, 4,4,8,3, 4,4,9,3,
1151+ 4,5,0,3, 4,5,1,3, 4,5,2,3, 4,5,3,3, 4,5,4,3, 4,5,5,3, 4,5,6,3, 4,5,7,3, 4,5,8,3,
1152+ 4,5,9,3, 4,6,0,3, 4,6,1,3, 4,6,2,3, 4,6,3,3, 4,6,4,3, 4,6,5,3, 4,6,6,3, 4,6,7,3,
1153+ 4,6,8,3, 4,6,9,3, 4,7,0,3, 4,7,1,3, 4,7,2,3, 4,7,3,3, 4,7,4,3, 4,7,5,3, 4,7,6,3,
1154+ 4,7,7,3, 4,7,8,3, 4,7,9,3, 4,8,0,3, 4,8,1,3, 4,8,2,3, 4,8,3,3, 4,8,4,3, 4,8,5,3,
1155+ 4,8,6,3, 4,8,7,3, 4,8,8,3, 4,8,9,3, 4,9,0,3, 4,9,1,3, 4,9,2,3, 4,9,3,3, 4,9,4,3,
1156+ 4,9,5,3, 4,9,6,3, 4,9,7,3, 4,9,8,3, 4,9,9,3, 5,0,0,3, 5,0,1,3, 5,0,2,3, 5,0,3,3,
1157+ 5,0,4,3, 5,0,5,3, 5,0,6,3, 5,0,7,3, 5,0,8,3, 5,0,9,3, 5,1,0,3, 5,1,1,3, 5,1,2,3,
1158+ 5,1,3,3, 5,1,4,3, 5,1,5,3, 5,1,6,3, 5,1,7,3, 5,1,8,3, 5,1,9,3, 5,2,0,3, 5,2,1,3,
1159+ 5,2,2,3, 5,2,3,3, 5,2,4,3, 5,2,5,3, 5,2,6,3, 5,2,7,3, 5,2,8,3, 5,2,9,3, 5,3,0,3,
1160+ 5,3,1,3, 5,3,2,3, 5,3,3,3, 5,3,4,3, 5,3,5,3, 5,3,6,3, 5,3,7,3, 5,3,8,3, 5,3,9,3,
1161+ 5,4,0,3, 5,4,1,3, 5,4,2,3, 5,4,3,3, 5,4,4,3, 5,4,5,3, 5,4,6,3, 5,4,7,3, 5,4,8,3,
1162+ 5,4,9,3, 5,5,0,3, 5,5,1,3, 5,5,2,3, 5,5,3,3, 5,5,4,3, 5,5,5,3, 5,5,6,3, 5,5,7,3,
1163+ 5,5,8,3, 5,5,9,3, 5,6,0,3, 5,6,1,3, 5,6,2,3, 5,6,3,3, 5,6,4,3, 5,6,5,3, 5,6,6,3,
1164+ 5,6,7,3, 5,6,8,3, 5,6,9,3, 5,7,0,3, 5,7,1,3, 5,7,2,3, 5,7,3,3, 5,7,4,3, 5,7,5,3,
1165+ 5,7,6,3, 5,7,7,3, 5,7,8,3, 5,7,9,3, 5,8,0,3, 5,8,1,3, 5,8,2,3, 5,8,3,3, 5,8,4,3,
1166+ 5,8,5,3, 5,8,6,3, 5,8,7,3, 5,8,8,3, 5,8,9,3, 5,9,0,3, 5,9,1,3, 5,9,2,3, 5,9,3,3,
1167+ 5,9,4,3, 5,9,5,3, 5,9,6,3, 5,9,7,3, 5,9,8,3, 5,9,9,3, 6,0,0,3, 6,0,1,3, 6,0,2,3,
1168+ 6,0,3,3, 6,0,4,3, 6,0,5,3, 6,0,6,3, 6,0,7,3, 6,0,8,3, 6,0,9,3, 6,1,0,3, 6,1,1,3,
1169+ 6,1,2,3, 6,1,3,3, 6,1,4,3, 6,1,5,3, 6,1,6,3, 6,1,7,3, 6,1,8,3, 6,1,9,3, 6,2,0,3,
1170+ 6,2,1,3, 6,2,2,3, 6,2,3,3, 6,2,4,3, 6,2,5,3, 6,2,6,3, 6,2,7,3, 6,2,8,3, 6,2,9,3,
1171+ 6,3,0,3, 6,3,1,3, 6,3,2,3, 6,3,3,3, 6,3,4,3, 6,3,5,3, 6,3,6,3, 6,3,7,3, 6,3,8,3,
1172+ 6,3,9,3, 6,4,0,3, 6,4,1,3, 6,4,2,3, 6,4,3,3, 6,4,4,3, 6,4,5,3, 6,4,6,3, 6,4,7,3,
1173+ 6,4,8,3, 6,4,9,3, 6,5,0,3, 6,5,1,3, 6,5,2,3, 6,5,3,3, 6,5,4,3, 6,5,5,3, 6,5,6,3,
1174+ 6,5,7,3, 6,5,8,3, 6,5,9,3, 6,6,0,3, 6,6,1,3, 6,6,2,3, 6,6,3,3, 6,6,4,3, 6,6,5,3,
1175+ 6,6,6,3, 6,6,7,3, 6,6,8,3, 6,6,9,3, 6,7,0,3, 6,7,1,3, 6,7,2,3, 6,7,3,3, 6,7,4,3,
1176+ 6,7,5,3, 6,7,6,3, 6,7,7,3, 6,7,8,3, 6,7,9,3, 6,8,0,3, 6,8,1,3, 6,8,2,3, 6,8,3,3,
1177+ 6,8,4,3, 6,8,5,3, 6,8,6,3, 6,8,7,3, 6,8,8,3, 6,8,9,3, 6,9,0,3, 6,9,1,3, 6,9,2,3,
1178+ 6,9,3,3, 6,9,4,3, 6,9,5,3, 6,9,6,3, 6,9,7,3, 6,9,8,3, 6,9,9,3, 7,0,0,3, 7,0,1,3,
1179+ 7,0,2,3, 7,0,3,3, 7,0,4,3, 7,0,5,3, 7,0,6,3, 7,0,7,3, 7,0,8,3, 7,0,9,3, 7,1,0,3,
1180+ 7,1,1,3, 7,1,2,3, 7,1,3,3, 7,1,4,3, 7,1,5,3, 7,1,6,3, 7,1,7,3, 7,1,8,3, 7,1,9,3,
1181+ 7,2,0,3, 7,2,1,3, 7,2,2,3, 7,2,3,3, 7,2,4,3, 7,2,5,3, 7,2,6,3, 7,2,7,3, 7,2,8,3,
1182+ 7,2,9,3, 7,3,0,3, 7,3,1,3, 7,3,2,3, 7,3,3,3, 7,3,4,3, 7,3,5,3, 7,3,6,3, 7,3,7,3,
1183+ 7,3,8,3, 7,3,9,3, 7,4,0,3, 7,4,1,3, 7,4,2,3, 7,4,3,3, 7,4,4,3, 7,4,5,3, 7,4,6,3,
1184+ 7,4,7,3, 7,4,8,3, 7,4,9,3, 7,5,0,3, 7,5,1,3, 7,5,2,3, 7,5,3,3, 7,5,4,3, 7,5,5,3,
1185+ 7,5,6,3, 7,5,7,3, 7,5,8,3, 7,5,9,3, 7,6,0,3, 7,6,1,3, 7,6,2,3, 7,6,3,3, 7,6,4,3,
1186+ 7,6,5,3, 7,6,6,3, 7,6,7,3, 7,6,8,3, 7,6,9,3, 7,7,0,3, 7,7,1,3, 7,7,2,3, 7,7,3,3,
1187+ 7,7,4,3, 7,7,5,3, 7,7,6,3, 7,7,7,3, 7,7,8,3, 7,7,9,3, 7,8,0,3, 7,8,1,3, 7,8,2,3,
1188+ 7,8,3,3, 7,8,4,3, 7,8,5,3, 7,8,6,3, 7,8,7,3, 7,8,8,3, 7,8,9,3, 7,9,0,3, 7,9,1,3,
1189+ 7,9,2,3, 7,9,3,3, 7,9,4,3, 7,9,5,3, 7,9,6,3, 7,9,7,3, 7,9,8,3, 7,9,9,3, 8,0,0,3,
1190+ 8,0,1,3, 8,0,2,3, 8,0,3,3, 8,0,4,3, 8,0,5,3, 8,0,6,3, 8,0,7,3, 8,0,8,3, 8,0,9,3,
1191+ 8,1,0,3, 8,1,1,3, 8,1,2,3, 8,1,3,3, 8,1,4,3, 8,1,5,3, 8,1,6,3, 8,1,7,3, 8,1,8,3,
1192+ 8,1,9,3, 8,2,0,3, 8,2,1,3, 8,2,2,3, 8,2,3,3, 8,2,4,3, 8,2,5,3, 8,2,6,3, 8,2,7,3,
1193+ 8,2,8,3, 8,2,9,3, 8,3,0,3, 8,3,1,3, 8,3,2,3, 8,3,3,3, 8,3,4,3, 8,3,5,3, 8,3,6,3,
1194+ 8,3,7,3, 8,3,8,3, 8,3,9,3, 8,4,0,3, 8,4,1,3, 8,4,2,3, 8,4,3,3, 8,4,4,3, 8,4,5,3,
1195+ 8,4,6,3, 8,4,7,3, 8,4,8,3, 8,4,9,3, 8,5,0,3, 8,5,1,3, 8,5,2,3, 8,5,3,3, 8,5,4,3,
1196+ 8,5,5,3, 8,5,6,3, 8,5,7,3, 8,5,8,3, 8,5,9,3, 8,6,0,3, 8,6,1,3, 8,6,2,3, 8,6,3,3,
1197+ 8,6,4,3, 8,6,5,3, 8,6,6,3, 8,6,7,3, 8,6,8,3, 8,6,9,3, 8,7,0,3, 8,7,1,3, 8,7,2,3,
1198+ 8,7,3,3, 8,7,4,3, 8,7,5,3, 8,7,6,3, 8,7,7,3, 8,7,8,3, 8,7,9,3, 8,8,0,3, 8,8,1,3,
1199+ 8,8,2,3, 8,8,3,3, 8,8,4,3, 8,8,5,3, 8,8,6,3, 8,8,7,3, 8,8,8,3, 8,8,9,3, 8,9,0,3,
1200+ 8,9,1,3, 8,9,2,3, 8,9,3,3, 8,9,4,3, 8,9,5,3, 8,9,6,3, 8,9,7,3, 8,9,8,3, 8,9,9,3,
1201+ 9,0,0,3, 9,0,1,3, 9,0,2,3, 9,0,3,3, 9,0,4,3, 9,0,5,3, 9,0,6,3, 9,0,7,3, 9,0,8,3,
1202+ 9,0,9,3, 9,1,0,3, 9,1,1,3, 9,1,2,3, 9,1,3,3, 9,1,4,3, 9,1,5,3, 9,1,6,3, 9,1,7,3,
1203+ 9,1,8,3, 9,1,9,3, 9,2,0,3, 9,2,1,3, 9,2,2,3, 9,2,3,3, 9,2,4,3, 9,2,5,3, 9,2,6,3,
1204+ 9,2,7,3, 9,2,8,3, 9,2,9,3, 9,3,0,3, 9,3,1,3, 9,3,2,3, 9,3,3,3, 9,3,4,3, 9,3,5,3,
1205+ 9,3,6,3, 9,3,7,3, 9,3,8,3, 9,3,9,3, 9,4,0,3, 9,4,1,3, 9,4,2,3, 9,4,3,3, 9,4,4,3,
1206+ 9,4,5,3, 9,4,6,3, 9,4,7,3, 9,4,8,3, 9,4,9,3, 9,5,0,3, 9,5,1,3, 9,5,2,3, 9,5,3,3,
1207+ 9,5,4,3, 9,5,5,3, 9,5,6,3, 9,5,7,3, 9,5,8,3, 9,5,9,3, 9,6,0,3, 9,6,1,3, 9,6,2,3,
1208+ 9,6,3,3, 9,6,4,3, 9,6,5,3, 9,6,6,3, 9,6,7,3, 9,6,8,3, 9,6,9,3, 9,7,0,3, 9,7,1,3,
1209+ 9,7,2,3, 9,7,3,3, 9,7,4,3, 9,7,5,3, 9,7,6,3, 9,7,7,3, 9,7,8,3, 9,7,9,3, 9,8,0,3,
1210+ 9,8,1,3, 9,8,2,3, 9,8,3,3, 9,8,4,3, 9,8,5,3, 9,8,6,3, 9,8,7,3, 9,8,8,3, 9,8,9,3,
1211+ 9,9,0,3, 9,9,1,3, 9,9,2,3, 9,9,3,3, 9,9,4,3, 9,9,5,3, 9,9,6,3, 9,9,7,3, 9,9,8,3,
1212+ 9,9,9,3};
1213+#endif
1214+
--- /dev/null
+++ b/libdecnumber/decDouble.c
@@ -0,0 +1,154 @@
1+/* decDouble module for the decNumber C Library.
2+ Copyright (C) 2007 Free Software Foundation, Inc.
3+ Contributed by IBM Corporation. Author Mike Cowlishaw.
4+
5+ This file is part of GCC.
6+
7+ GCC is free software; you can redistribute it and/or modify it under
8+ the terms of the GNU General Public License as published by the Free
9+ Software Foundation; either version 2, or (at your option) any later
10+ version.
11+
12+ In addition to the permissions in the GNU General Public License,
13+ the Free Software Foundation gives you unlimited permission to link
14+ the compiled version of this file into combinations with other
15+ programs, and to distribute those combinations without any
16+ restriction coming from the use of this file. (The General Public
17+ License restrictions do apply in other respects; for example, they
18+ cover modification of the file, and distribution when not linked
19+ into a combine executable.)
20+
21+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
23+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24+ for more details.
25+
26+ You should have received a copy of the GNU General Public License
27+ along with GCC; see the file COPYING. If not, write to the Free
28+ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29+ 02110-1301, USA. */
30+
31+/* ------------------------------------------------------------------ */
32+/* decDouble.c -- decDouble operations module */
33+/* ------------------------------------------------------------------ */
34+/* This module comprises decDouble operations (including conversions) */
35+/* ------------------------------------------------------------------ */
36+
37+#include "decContext.h" /* public includes */
38+#include "decDouble.h" /* .. */
39+
40+/* Constant mappings for shared code */
41+#define DECPMAX DECDOUBLE_Pmax
42+#define DECEMIN DECDOUBLE_Emin
43+#define DECEMAX DECDOUBLE_Emax
44+#define DECEMAXD DECDOUBLE_EmaxD
45+#define DECBYTES DECDOUBLE_Bytes
46+#define DECSTRING DECDOUBLE_String
47+#define DECECONL DECDOUBLE_EconL
48+#define DECBIAS DECDOUBLE_Bias
49+#define DECLETS DECDOUBLE_Declets
50+#define DECQTINY (-DECDOUBLE_Bias)
51+/* parameters of next-wider format */
52+#define DECWBYTES DECQUAD_Bytes
53+#define DECWPMAX DECQUAD_Pmax
54+#define DECWECONL DECQUAD_EconL
55+#define DECWBIAS DECQUAD_Bias
56+
57+/* Type and function mappings for shared code */
58+#define decFloat decDouble /* Type name */
59+#define decFloatWider decQuad /* Type name */
60+
61+/* Utilities and conversions (binary results, extractors, etc.) */
62+#define decFloatFromBCD decDoubleFromBCD
63+#define decFloatFromInt32 decDoubleFromInt32
64+#define decFloatFromPacked decDoubleFromPacked
65+#define decFloatFromString decDoubleFromString
66+#define decFloatFromUInt32 decDoubleFromUInt32
67+#define decFloatFromWider decDoubleFromWider
68+#define decFloatGetCoefficient decDoubleGetCoefficient
69+#define decFloatGetExponent decDoubleGetExponent
70+#define decFloatSetCoefficient decDoubleSetCoefficient
71+#define decFloatSetExponent decDoubleSetExponent
72+#define decFloatShow decDoubleShow
73+#define decFloatToBCD decDoubleToBCD
74+#define decFloatToEngString decDoubleToEngString
75+#define decFloatToInt32 decDoubleToInt32
76+#define decFloatToInt32Exact decDoubleToInt32Exact
77+#define decFloatToPacked decDoubleToPacked
78+#define decFloatToString decDoubleToString
79+#define decFloatToUInt32 decDoubleToUInt32
80+#define decFloatToUInt32Exact decDoubleToUInt32Exact
81+#define decFloatToWider decDoubleToWider
82+#define decFloatZero decDoubleZero
83+
84+/* Computational (result is a decFloat) */
85+#define decFloatAbs decDoubleAbs
86+#define decFloatAdd decDoubleAdd
87+#define decFloatAnd decDoubleAnd
88+#define decFloatDivide decDoubleDivide
89+#define decFloatDivideInteger decDoubleDivideInteger
90+#define decFloatFMA decDoubleFMA
91+#define decFloatInvert decDoubleInvert
92+#define decFloatLogB decDoubleLogB
93+#define decFloatMax decDoubleMax
94+#define decFloatMaxMag decDoubleMaxMag
95+#define decFloatMin decDoubleMin
96+#define decFloatMinMag decDoubleMinMag
97+#define decFloatMinus decDoubleMinus
98+#define decFloatMultiply decDoubleMultiply
99+#define decFloatNextMinus decDoubleNextMinus
100+#define decFloatNextPlus decDoubleNextPlus
101+#define decFloatNextToward decDoubleNextToward
102+#define decFloatOr decDoubleOr
103+#define decFloatPlus decDoublePlus
104+#define decFloatQuantize decDoubleQuantize
105+#define decFloatReduce decDoubleReduce
106+#define decFloatRemainder decDoubleRemainder
107+#define decFloatRemainderNear decDoubleRemainderNear
108+#define decFloatRotate decDoubleRotate
109+#define decFloatScaleB decDoubleScaleB
110+#define decFloatShift decDoubleShift
111+#define decFloatSubtract decDoubleSubtract
112+#define decFloatToIntegralValue decDoubleToIntegralValue
113+#define decFloatToIntegralExact decDoubleToIntegralExact
114+#define decFloatXor decDoubleXor
115+
116+/* Comparisons */
117+#define decFloatCompare decDoubleCompare
118+#define decFloatCompareSignal decDoubleCompareSignal
119+#define decFloatCompareTotal decDoubleCompareTotal
120+#define decFloatCompareTotalMag decDoubleCompareTotalMag
121+
122+/* Copies */
123+#define decFloatCanonical decDoubleCanonical
124+#define decFloatCopy decDoubleCopy
125+#define decFloatCopyAbs decDoubleCopyAbs
126+#define decFloatCopyNegate decDoubleCopyNegate
127+#define decFloatCopySign decDoubleCopySign
128+
129+/* Non-computational */
130+#define decFloatClass decDoubleClass
131+#define decFloatClassString decDoubleClassString
132+#define decFloatDigits decDoubleDigits
133+#define decFloatIsCanonical decDoubleIsCanonical
134+#define decFloatIsFinite decDoubleIsFinite
135+#define decFloatIsInfinite decDoubleIsInfinite
136+#define decFloatIsInteger decDoubleIsInteger
137+#define decFloatIsNaN decDoubleIsNaN
138+#define decFloatIsNormal decDoubleIsNormal
139+#define decFloatIsSignaling decDoubleIsSignaling
140+#define decFloatIsSignalling decDoubleIsSignalling
141+#define decFloatIsSigned decDoubleIsSigned
142+#define decFloatIsSubnormal decDoubleIsSubnormal
143+#define decFloatIsZero decDoubleIsZero
144+#define decFloatRadix decDoubleRadix
145+#define decFloatSameQuantum decDoubleSameQuantum
146+#define decFloatVersion decDoubleVersion
147+
148+
149+#include "decNumberLocal.h" /* local includes (need DECPMAX) */
150+#include "decCommon.c" /* non-arithmetic decFloat routines */
151+#include "decBasic.c" /* basic formats routines */
152+
153+/* Below here will move to shared file as completed */
154+
--- /dev/null
+++ b/libdecnumber/decDouble.h
@@ -0,0 +1,164 @@
1+/* decDouble module header for the decNumber C Library.
2+ Copyright (C) 2007 Free Software Foundation, Inc.
3+ Contributed by IBM Corporation. Author Mike Cowlishaw.
4+
5+ This file is part of GCC.
6+
7+ GCC is free software; you can redistribute it and/or modify it under
8+ the terms of the GNU General Public License as published by the Free
9+ Software Foundation; either version 2, or (at your option) any later
10+ version.
11+
12+ In addition to the permissions in the GNU General Public License,
13+ the Free Software Foundation gives you unlimited permission to link
14+ the compiled version of this file into combinations with other
15+ programs, and to distribute those combinations without any
16+ restriction coming from the use of this file. (The General Public
17+ License restrictions do apply in other respects; for example, they
18+ cover modification of the file, and distribution when not linked
19+ into a combine executable.)
20+
21+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
23+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24+ for more details.
25+
26+ You should have received a copy of the GNU General Public License
27+ along with GCC; see the file COPYING. If not, write to the Free
28+ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29+ 02110-1301, USA. */
30+
31+/* ------------------------------------------------------------------ */
32+/* decDouble.h -- Decimal 64-bit format module header */
33+/* ------------------------------------------------------------------ */
34+/* Please see decFloats.h for an overview and documentation details. */
35+/* ------------------------------------------------------------------ */
36+
37+#if !defined(DECDOUBLE)
38+ #define DECDOUBLE
39+
40+ #define DECDOUBLENAME "decimalDouble" /* Short name */
41+ #define DECDOUBLETITLE "Decimal 64-bit datum" /* Verbose name */
42+ #define DECDOUBLEAUTHOR "Mike Cowlishaw" /* Who to blame */
43+
44+ /* parameters for decDoubles */
45+ #define DECDOUBLE_Bytes 8 /* length */
46+ #define DECDOUBLE_Pmax 16 /* maximum precision (digits) */
47+ #define DECDOUBLE_Emin -383 /* minimum adjusted exponent */
48+ #define DECDOUBLE_Emax 384 /* maximum adjusted exponent */
49+ #define DECDOUBLE_EmaxD 3 /* maximum exponent digits */
50+ #define DECDOUBLE_Bias 398 /* bias for the exponent */
51+ #define DECDOUBLE_String 25 /* maximum string length, +1 */
52+ #define DECDOUBLE_EconL 8 /* exponent continuation length */
53+ #define DECDOUBLE_Declets 5 /* count of declets */
54+ /* highest biased exponent (Elimit-1) */
55+ #define DECDOUBLE_Ehigh (DECDOUBLE_Emax + DECDOUBLE_Bias - (DECDOUBLE_Pmax-1))
56+
57+ /* Required includes */
58+ #include "decContext.h"
59+ #include "decQuad.h"
60+
61+ /* The decDouble decimal 64-bit type, accessible by bytes */
62+ typedef struct {
63+ uint8_t bytes[DECDOUBLE_Bytes]; /* fields: 1, 5, 8, 50 bits */
64+ } decDouble;
65+
66+ /* ---------------------------------------------------------------- */
67+ /* Routines -- implemented as decFloat routines in common files */
68+ /* ---------------------------------------------------------------- */
69+
70+ #include "decDoubleSymbols.h"
71+
72+ /* Utilities and conversions, extractors, etc.) */
73+ extern decDouble * decDoubleFromBCD(decDouble *, int32_t, const uint8_t *, int32_t);
74+ extern decDouble * decDoubleFromInt32(decDouble *, int32_t);
75+ extern decDouble * decDoubleFromPacked(decDouble *, int32_t, const uint8_t *);
76+ extern decDouble * decDoubleFromString(decDouble *, const char *, decContext *);
77+ extern decDouble * decDoubleFromUInt32(decDouble *, uint32_t);
78+ extern decDouble * decDoubleFromWider(decDouble *, const decQuad *, decContext *);
79+ extern int32_t decDoubleGetCoefficient(const decDouble *, uint8_t *);
80+ extern int32_t decDoubleGetExponent(const decDouble *);
81+ extern decDouble * decDoubleSetCoefficient(decDouble *, const uint8_t *, int32_t);
82+ extern decDouble * decDoubleSetExponent(decDouble *, decContext *, int32_t);
83+ extern void decDoubleShow(const decDouble *, const char *);
84+ extern int32_t decDoubleToBCD(const decDouble *, int32_t *, uint8_t *);
85+ extern char * decDoubleToEngString(const decDouble *, char *);
86+ extern int32_t decDoubleToInt32(const decDouble *, decContext *, enum rounding);
87+ extern int32_t decDoubleToInt32Exact(const decDouble *, decContext *, enum rounding);
88+ extern int32_t decDoubleToPacked(const decDouble *, int32_t *, uint8_t *);
89+ extern char * decDoubleToString(const decDouble *, char *);
90+ extern uint32_t decDoubleToUInt32(const decDouble *, decContext *, enum rounding);
91+ extern uint32_t decDoubleToUInt32Exact(const decDouble *, decContext *, enum rounding);
92+ extern decQuad * decDoubleToWider(const decDouble *, decQuad *);
93+ extern decDouble * decDoubleZero(decDouble *);
94+
95+ /* Computational (result is a decDouble) */
96+ extern decDouble * decDoubleAbs(decDouble *, const decDouble *, decContext *);
97+ extern decDouble * decDoubleAdd(decDouble *, const decDouble *, const decDouble *, decContext *);
98+ extern decDouble * decDoubleAnd(decDouble *, const decDouble *, const decDouble *, decContext *);
99+ extern decDouble * decDoubleDivide(decDouble *, const decDouble *, const decDouble *, decContext *);
100+ extern decDouble * decDoubleDivideInteger(decDouble *, const decDouble *, const decDouble *, decContext *);
101+ extern decDouble * decDoubleFMA(decDouble *, const decDouble *, const decDouble *, const decDouble *, decContext *);
102+ extern decDouble * decDoubleInvert(decDouble *, const decDouble *, decContext *);
103+ extern decDouble * decDoubleLogB(decDouble *, const decDouble *, decContext *);
104+ extern decDouble * decDoubleMax(decDouble *, const decDouble *, const decDouble *, decContext *);
105+ extern decDouble * decDoubleMaxMag(decDouble *, const decDouble *, const decDouble *, decContext *);
106+ extern decDouble * decDoubleMin(decDouble *, const decDouble *, const decDouble *, decContext *);
107+ extern decDouble * decDoubleMinMag(decDouble *, const decDouble *, const decDouble *, decContext *);
108+ extern decDouble * decDoubleMinus(decDouble *, const decDouble *, decContext *);
109+ extern decDouble * decDoubleMultiply(decDouble *, const decDouble *, const decDouble *, decContext *);
110+ extern decDouble * decDoubleNextMinus(decDouble *, const decDouble *, decContext *);
111+ extern decDouble * decDoubleNextPlus(decDouble *, const decDouble *, decContext *);
112+ extern decDouble * decDoubleNextToward(decDouble *, const decDouble *, const decDouble *, decContext *);
113+ extern decDouble * decDoubleOr(decDouble *, const decDouble *, const decDouble *, decContext *);
114+ extern decDouble * decDoublePlus(decDouble *, const decDouble *, decContext *);
115+ extern decDouble * decDoubleQuantize(decDouble *, const decDouble *, const decDouble *, decContext *);
116+ extern decDouble * decDoubleReduce(decDouble *, const decDouble *, decContext *);
117+ extern decDouble * decDoubleRemainder(decDouble *, const decDouble *, const decDouble *, decContext *);
118+ extern decDouble * decDoubleRemainderNear(decDouble *, const decDouble *, const decDouble *, decContext *);
119+ extern decDouble * decDoubleRotate(decDouble *, const decDouble *, const decDouble *, decContext *);
120+ extern decDouble * decDoubleScaleB(decDouble *, const decDouble *, const decDouble *, decContext *);
121+ extern decDouble * decDoubleShift(decDouble *, const decDouble *, const decDouble *, decContext *);
122+ extern decDouble * decDoubleSubtract(decDouble *, const decDouble *, const decDouble *, decContext *);
123+ extern decDouble * decDoubleToIntegralValue(decDouble *, const decDouble *, decContext *, enum rounding);
124+ extern decDouble * decDoubleToIntegralExact(decDouble *, const decDouble *, decContext *);
125+ extern decDouble * decDoubleXor(decDouble *, const decDouble *, const decDouble *, decContext *);
126+
127+ /* Comparisons */
128+ extern decDouble * decDoubleCompare(decDouble *, const decDouble *, const decDouble *, decContext *);
129+ extern decDouble * decDoubleCompareSignal(decDouble *, const decDouble *, const decDouble *, decContext *);
130+ extern decDouble * decDoubleCompareTotal(decDouble *, const decDouble *, const decDouble *);
131+ extern decDouble * decDoubleCompareTotalMag(decDouble *, const decDouble *, const decDouble *);
132+
133+ /* Copies */
134+ extern decDouble * decDoubleCanonical(decDouble *, const decDouble *);
135+ extern decDouble * decDoubleCopy(decDouble *, const decDouble *);
136+ extern decDouble * decDoubleCopyAbs(decDouble *, const decDouble *);
137+ extern decDouble * decDoubleCopyNegate(decDouble *, const decDouble *);
138+ extern decDouble * decDoubleCopySign(decDouble *, const decDouble *, const decDouble *);
139+
140+ /* Non-computational */
141+ extern enum decClass decDoubleClass(const decDouble *);
142+ extern const char * decDoubleClassString(const decDouble *);
143+ extern uint32_t decDoubleDigits(const decDouble *);
144+ extern uint32_t decDoubleIsCanonical(const decDouble *);
145+ extern uint32_t decDoubleIsFinite(const decDouble *);
146+ extern uint32_t decDoubleIsInfinite(const decDouble *);
147+ extern uint32_t decDoubleIsInteger(const decDouble *);
148+ extern uint32_t decDoubleIsNaN(const decDouble *);
149+ extern uint32_t decDoubleIsNormal(const decDouble *);
150+ extern uint32_t decDoubleIsSignaling(const decDouble *);
151+ extern uint32_t decDoubleIsSignalling(const decDouble *);
152+ extern uint32_t decDoubleIsSigned(const decDouble *);
153+ extern uint32_t decDoubleIsSubnormal(const decDouble *);
154+ extern uint32_t decDoubleIsZero(const decDouble *);
155+ extern uint32_t decDoubleRadix(const decDouble *);
156+ extern uint32_t decDoubleSameQuantum(const decDouble *, const decDouble *);
157+ extern const char * decDoubleVersion(void);
158+
159+ /* decNumber conversions; these are implemented as macros so as not */
160+ /* to force a dependency on decimal64 and decNumber in decDouble. */
161+ #define decDoubleToNumber(dq, dn) decimal64ToNumber((decimal64 *)(dq), dn)
162+ #define decDoubleFromNumber(dq, dn, set) (decDouble *)decimal64FromNumber((decimal64 *)(dq), dn, set)
163+
164+#endif
--- /dev/null
+++ b/libdecnumber/decDoubleSymbols.h
@@ -0,0 +1,84 @@
1+#if !defined(DECDOUBLESYMBOLS)
2+#define DECDOUBLESYMBOLS
3+
4+#ifdef IN_LIBGCC2
5+#define decDoubleAbs __decDoubleAbs
6+#define decDoubleAdd __decDoubleAdd
7+#define decDoubleAnd __decDoubleAnd
8+#define decDoubleCanonical __decDoubleCanonical
9+#define decDoubleClass __decDoubleClass
10+#define decDoubleClassString __decDoubleClassString
11+#define decDoubleCompare __decDoubleCompare
12+#define decDoubleCompareSignal __decDoubleCompareSignal
13+#define decDoubleCompareTotal __decDoubleCompareTotal
14+#define decDoubleCompareTotalMag __decDoubleCompareTotalMag
15+#define decDoubleCopy __decDoubleCopy
16+#define decDoubleCopyAbs __decDoubleCopyAbs
17+#define decDoubleCopyNegate __decDoubleCopyNegate
18+#define decDoubleCopySign __decDoubleCopySign
19+#define decDoubleDigits __decDoubleDigits
20+#define decDoubleDivide __decDoubleDivide
21+#define decDoubleDivideInteger __decDoubleDivideInteger
22+#define decDoubleFMA __decDoubleFMA
23+#define decDoubleFromBCD __decDoubleFromBCD
24+#define decDoubleFromInt32 __decDoubleFromInt32
25+#define decDoubleFromPacked __decDoubleFromPacked
26+#define decDoubleFromString __decDoubleFromString
27+#define decDoubleFromUInt32 __decDoubleFromUInt32
28+#define decDoubleFromWider __decDoubleFromWider
29+#define decDoubleGetCoefficient __decDoubleGetCoefficient
30+#define decDoubleGetExponent __decDoubleGetExponent
31+#define decDoubleInvert __decDoubleInvert
32+#define decDoubleIsCanonical __decDoubleIsCanonical
33+#define decDoubleIsFinite __decDoubleIsFinite
34+#define decDoubleIsInfinite __decDoubleIsInfinite
35+#define decDoubleIsInteger __decDoubleIsInteger
36+#define decDoubleIsNaN __decDoubleIsNaN
37+#define decDoubleIsNormal __decDoubleIsNormal
38+#define decDoubleIsSignaling __decDoubleIsSignaling
39+#define decDoubleIsSignalling __decDoubleIsSignalling
40+#define decDoubleIsSigned __decDoubleIsSigned
41+#define decDoubleIsSubnormal __decDoubleIsSubnormal
42+#define decDoubleIsZero __decDoubleIsZero
43+#define decDoubleLogB __decDoubleLogB
44+#define decDoubleMax __decDoubleMax
45+#define decDoubleMaxMag __decDoubleMaxMag
46+#define decDoubleMin __decDoubleMin
47+#define decDoubleMinMag __decDoubleMinMag
48+#define decDoubleMinus __decDoubleMinus
49+#define decDoubleMultiply __decDoubleMultiply
50+#define decDoubleNextMinus __decDoubleNextMinus
51+#define decDoubleNextPlus __decDoubleNextPlus
52+#define decDoubleNextToward __decDoubleNextToward
53+#define decDoubleOr __decDoubleOr
54+#define decDoublePlus __decDoublePlus
55+#define decDoubleQuantize __decDoubleQuantize
56+#define decDoubleRadix __decDoubleRadix
57+#define decDoubleReduce __decDoubleReduce
58+#define decDoubleRemainder __decDoubleRemainder
59+#define decDoubleRemainderNear __decDoubleRemainderNear
60+#define decDoubleRotate __decDoubleRotate
61+#define decDoubleSameQuantum __decDoubleSameQuantum
62+#define decDoubleScaleB __decDoubleScaleB
63+#define decDoubleSetCoefficient __decDoubleSetCoefficient
64+#define decDoubleSetExponent __decDoubleSetExponent
65+#define decDoubleShift __decDoubleShift
66+#define decDoubleShow __decDoubleShow
67+#define decDoubleSubtract __decDoubleSubtract
68+#define decDoubleToBCD __decDoubleToBCD
69+#define decDoubleToEngString __decDoubleToEngString
70+#define decDoubleToInt32 __decDoubleToInt32
71+#define decDoubleToInt32Exact __decDoubleToInt32Exact
72+#define decDoubleToIntegralExact __decDoubleToIntegralExact
73+#define decDoubleToIntegralValue __decDoubleToIntegralValue
74+#define decDoubleToPacked __decDoubleToPacked
75+#define decDoubleToString __decDoubleToString
76+#define decDoubleToUInt32 __decDoubleToUInt32
77+#define decDoubleToUInt32Exact __decDoubleToUInt32Exact
78+#define decDoubleToWider __decDoubleToWider
79+#define decDoubleVersion __decDoubleVersion
80+#define decDoubleXor __decDoubleXor
81+#define decDoubleZero __decDoubleZero
82+#endif
83+
84+#endif
--- a/libdecnumber/decLibrary.c
+++ b/libdecnumber/decLibrary.c
@@ -74,22 +74,3 @@ isinfd128 (_Decimal128 arg)
7474 decimal128ToNumber (&d128, &dn);
7575 return (decNumberIsInfinite (&dn));
7676 }
77-
78-uint32_t
79-__dec_byte_swap (uint32_t in)
80-{
81- uint32_t out = 0;
82- unsigned char *p = (unsigned char *) &out;
83- union {
84- uint32_t i;
85- unsigned char b[4];
86- } u;
87-
88- u.i = in;
89- p[0] = u.b[3];
90- p[1] = u.b[2];
91- p[2] = u.b[1];
92- p[3] = u.b[0];
93-
94- return out;
95-}
--- a/libdecnumber/decNumber.c
+++ b/libdecnumber/decNumber.c
@@ -1,5 +1,5 @@
1-/* Decimal Number module for the decNumber C Library
2- Copyright (C) 2005 Free Software Foundation, Inc.
1+/* Decimal number arithmetic module for the decNumber C Library.
2+ Copyright (C) 2005, 2007 Free Software Foundation, Inc.
33 Contributed by IBM Corporation. Author Mike Cowlishaw.
44
55 This file is part of GCC.
@@ -29,236 +29,257 @@
2929 02110-1301, USA. */
3030
3131 /* ------------------------------------------------------------------ */
32-/* This module comprises the routines for Standard Decimal Arithmetic */
33-/* as defined in the specification which may be found on the */
32+/* Decimal Number arithmetic module */
33+/* ------------------------------------------------------------------ */
34+/* This module comprises the routines for General Decimal Arithmetic */
35+/* as defined in the specification which may be found on the */
3436 /* http://www2.hursley.ibm.com/decimal web pages. It implements both */
35-/* the full ('extended') arithmetic and the simpler ('subset') */
36-/* arithmetic. */
37-/* */
38-/* Usage notes: */
39-/* */
40-/* 1. This code is ANSI C89 except: */
41-/* */
42-/* a) Line comments (double forward slash) are used. (Most C */
43-/* compilers accept these. If yours does not, a simple script */
44-/* can be used to convert them to ANSI C comments.) */
45-/* */
46-/* b) Types from C99 stdint.h are used. If you do not have this */
47-/* header file, see the User's Guide section of the decNumber */
48-/* documentation; this lists the necessary definitions. */
49-/* */
50-/* c) If DECDPUN>4, non-ANSI 64-bit 'long long' types are used. */
51-/* To avoid these, set DECDPUN <= 4 (see documentation). */
52-/* */
37+/* the full ('extended') arithmetic and the simpler ('subset') */
38+/* arithmetic. */
39+/* */
40+/* Usage notes: */
41+/* */
42+/* 1. This code is ANSI C89 except: */
43+/* */
44+/* If DECDPUN>4 or DECUSE64=1, the C99 64-bit int64_t and */
45+/* uint64_t types may be used. To avoid these, set DECUSE64=0 */
46+/* and DECDPUN<=4 (see documentation). */
47+/* */
5348 /* 2. The decNumber format which this library uses is optimized for */
5449 /* efficient processing of relatively short numbers; in particular */
5550 /* it allows the use of fixed sized structures and minimizes copy */
56-/* and move operations. It does, however, support arbitrary */
51+/* and move operations. It does, however, support arbitrary */
5752 /* precision (up to 999,999,999 digits) and arbitrary exponent */
5853 /* range (Emax in the range 0 through 999,999,999 and Emin in the */
59-/* range -999,999,999 through 0). */
60-/* */
61-/* 3. Operands to operator functions are never modified unless they */
54+/* range -999,999,999 through 0). Mathematical functions (for */
55+/* example decNumberExp) as identified below are restricted more */
56+/* tightly: digits, emax, and -emin in the context must be <= */
57+/* DEC_MAX_MATH (999999), and their operand(s) must be within */
58+/* these bounds. */
59+/* */
60+/* 3. Logical functions are further restricted; their operands must */
61+/* be finite, positive, have an exponent of zero, and all digits */
62+/* must be either 0 or 1. The result will only contain digits */
63+/* which are 0 or 1 (and will have exponent=0 and a sign of 0). */
64+/* */
65+/* 4. Operands to operator functions are never modified unless they */
6266 /* are also specified to be the result number (which is always */
63-/* permitted). Other than that case, operands may not overlap. */
64-/* */
65-/* 4. Error handling: the type of the error is ORed into the status */
66-/* flags in the current context (decContext structure). The */
67+/* permitted). Other than that case, operands must not overlap. */
68+/* */
69+/* 5. Error handling: the type of the error is ORed into the status */
70+/* flags in the current context (decContext structure). The */
6771 /* SIGFPE signal is then raised if the corresponding trap-enabler */
68-/* flag in the decContext is set (is 1). */
69-/* */
72+/* flag in the decContext is set (is 1). */
73+/* */
7074 /* It is the responsibility of the caller to clear the status */
71-/* flags as required. */
72-/* */
75+/* flags as required. */
76+/* */
7377 /* The result of any routine which returns a number will always */
7478 /* be a valid number (which may be a special value, such as an */
75-/* Infinity or NaN). */
76-/* */
77-/* 5. The decNumber format is not an exchangeable concrete */
79+/* Infinity or NaN). */
80+/* */
81+/* 6. The decNumber format is not an exchangeable concrete */
7882 /* representation as it comprises fields which may be machine- */
79-/* dependent (big-endian or little-endian, for example). */
83+/* dependent (packed or unpacked, or special length, for example). */
8084 /* Canonical conversions to and from strings are provided; other */
81-/* conversions are available in separate modules. */
82-/* */
83-/* 6. Normally, input operands are assumed to be valid. Set DECCHECK */
85+/* conversions are available in separate modules. */
86+/* */
87+/* 7. Normally, input operands are assumed to be valid. Set DECCHECK */
8488 /* to 1 for extended operand checking (including NULL operands). */
8589 /* Results are undefined if a badly-formed structure (or a NULL */
86-/* NULL pointer to a structure) is provided, though with DECCHECK */
90+/* pointer to a structure) is provided, though with DECCHECK */
8791 /* enabled the operator routines are protected against exceptions. */
8892 /* (Except if the result pointer is NULL, which is unrecoverable.) */
89-/* */
93+/* */
9094 /* However, the routines will never cause exceptions if they are */
9195 /* given well-formed operands, even if the value of the operands */
9296 /* is inappropriate for the operation and DECCHECK is not set. */
93-/* */
94-/* 7. Subset arithmetic is available only if DECSUBSET is set to 1. */
97+/* (Except for SIGFPE, as and where documented.) */
98+/* */
99+/* 8. Subset arithmetic is available only if DECSUBSET is set to 1. */
95100 /* ------------------------------------------------------------------ */
96-/* Implementation notes for maintenance of this module: */
97-/* */
98-/* 1. Storage leak protection: Routines which use malloc are not */
101+/* Implementation notes for maintenance of this module: */
102+/* */
103+/* 1. Storage leak protection: Routines which use malloc are not */
99104 /* permitted to use return for fastpath or error exits (i.e., */
100-/* they follow strict structured programming conventions). */
105+/* they follow strict structured programming conventions). */
101106 /* Instead they have a do{}while(0); construct surrounding the */
102-/* code which is protected -- break may be used from this. */
103-/* Other routines are allowed to use the return statement inline. */
104-/* */
105-/* Storage leak accounting can be enabled using DECALLOC. */
106-/* */
107-/* 2. All loops use the for(;;) construct. Any do construct is for */
108-/* protection as just described. */
109-/* */
107+/* code which is protected -- break may be used to exit this. */
108+/* Other routines can safely use the return statement inline. */
109+/* */
110+/* Storage leak accounting can be enabled using DECALLOC. */
111+/* */
112+/* 2. All loops use the for(;;) construct. Any do construct does */
113+/* not loop; it is for allocation protection as just described. */
114+/* */
110115 /* 3. Setting status in the context must always be the very last */
111116 /* action in a routine, as non-0 status may raise a trap and hence */
112117 /* the call to set status may not return (if the handler uses long */
113-/* jump). Therefore all cleanup must be done first. In general, */
114-/* to achieve this we accumulate status and only finally apply it */
115-/* by calling decContextSetStatus (via decStatus). */
116-/* */
117-/* Routines which allocate storage cannot, therefore, use the */
118-/* 'top level' routines which could cause a non-returning */
118+/* jump). Therefore all cleanup must be done first. In general, */
119+/* to achieve this status is accumulated and is only applied just */
120+/* before return by calling decContextSetStatus (via decStatus). */
121+/* */
122+/* Routines which allocate storage cannot, in general, use the */
123+/* 'top level' routines which could cause a non-returning */
119124 /* transfer of control. The decXxxxOp routines are safe (do not */
120125 /* call decStatus even if traps are set in the context) and should */
121-/* be used instead (they are also a little faster). */
122-/* */
126+/* be used instead (they are also a little faster). */
127+/* */
123128 /* 4. Exponent checking is minimized by allowing the exponent to */
124129 /* grow outside its limits during calculations, provided that */
125-/* the decFinalize function is called later. Multiplication and */
130+/* the decFinalize function is called later. Multiplication and */
126131 /* division, and intermediate calculations in exponentiation, */
127-/* require more careful checks because of the risk of 31-bit */
132+/* require more careful checks because of the risk of 31-bit */
128133 /* overflow (the most negative valid exponent is -1999999997, for */
129134 /* a 999999999-digit number with adjusted exponent of -999999999). */
130-/* */
135+/* */
131136 /* 5. Rounding is deferred until finalization of results, with any */
132137 /* 'off to the right' data being represented as a single digit */
133138 /* residue (in the range -1 through 9). This avoids any double- */
134-/* rounding when more than one shortening takes place (for */
135-/* example, when a result is subnormal). */
136-/* */
139+/* rounding when more than one shortening takes place (for */
140+/* example, when a result is subnormal). */
141+/* */
137142 /* 6. The digits count is allowed to rise to a multiple of DECDPUN */
138143 /* during many operations, so whole Units are handled and exact */
139144 /* accounting of digits is not needed. The correct digits value */
140145 /* is found by decGetDigits, which accounts for leading zeros. */
141146 /* This must be called before any rounding if the number of digits */
142-/* is not known exactly. */
143-/* */
144-/* 7. We use the multiply-by-reciprocal 'trick' for partitioning */
147+/* is not known exactly. */
148+/* */
149+/* 7. The multiply-by-reciprocal 'trick' is used for partitioning */
145150 /* numbers up to four digits, using appropriate constants. This */
146151 /* is not useful for longer numbers because overflow of 32 bits */
147152 /* would lead to 4 multiplies, which is almost as expensive as */
148-/* a divide (unless we assumed floating-point multiply available). */
149-/* */
150-/* 8. Unusual abbreviations possibly used in the commentary: */
151-/* lhs -- left hand side (operand, of an operation) */
152-/* lsd -- least significant digit (of coefficient) */
153-/* lsu -- least significant Unit (of coefficient) */
154-/* msd -- most significant digit (of coefficient) */
155-/* msu -- most significant Unit (of coefficient) */
156-/* rhs -- right hand side (operand, of an operation) */
157-/* +ve -- positive */
158-/* -ve -- negative */
159-/* ------------------------------------------------------------------ */
160-
161-/* Some of glibc's string inlines cause warnings. Plus we'd rather
162- rely on (and therefore test) GCC's string builtins. */
163-#define __NO_STRING_INLINES
164-
165-#include <stdlib.h> /* for malloc, free, etc. */
166-#include <stdio.h> /* for printf [if needed] */
167-#include <string.h> /* for strcpy */
168-#include <ctype.h> /* for lower */
169-#include "config.h"
170-#include "decNumber.h" /* base number library */
171-#include "decNumberLocal.h" /* decNumber local types, etc. */
153+/* a divide (unless a floating-point or 64-bit multiply is */
154+/* assumed to be available). */
155+/* */
156+/* 8. Unusual abbreviations that may be used in the commentary: */
157+/* lhs -- left hand side (operand, of an operation) */
158+/* lsd -- least significant digit (of coefficient) */
159+/* lsu -- least significant Unit (of coefficient) */
160+/* msd -- most significant digit (of coefficient) */
161+/* msi -- most significant item (in an array) */
162+/* msu -- most significant Unit (of coefficient) */
163+/* rhs -- right hand side (operand, of an operation) */
164+/* +ve -- positive */
165+/* -ve -- negative */
166+/* ** -- raise to the power */
167+/* ------------------------------------------------------------------ */
168+
169+#include <stdlib.h> /* for malloc, free, etc. */
170+#include <stdio.h> /* for printf [if needed] */
171+#include <string.h> /* for strcpy */
172+#include <ctype.h> /* for lower */
173+#include "config.h" /* for GCC definitions */
174+#include "decNumber.h" /* base number library */
175+#include "decNumberLocal.h" /* decNumber local types, etc. */
172176
173177 /* Constants */
174-/* Public constant array: powers of ten (powers[n]==10**n) */
175-const uInt powers[] = { 1, 10, 100, 1000, 10000, 100000, 1000000,
176- 10000000, 100000000, 1000000000
177-};
178-
179-/* Local constants */
180-#define DIVIDE 0x80 /* Divide operators */
181-#define REMAINDER 0x40 /* .. */
182-#define DIVIDEINT 0x20 /* .. */
183-#define REMNEAR 0x10 /* .. */
184-#define COMPARE 0x01 /* Compare operators */
185-#define COMPMAX 0x02 /* .. */
186-#define COMPMIN 0x03 /* .. */
187-#define COMPNAN 0x04 /* .. [NaN processing] */
178+/* Public lookup table used by the D2U macro */
179+const uByte d2utable[DECMAXD2U+1]=D2UTABLE;
188180
189-#define DEC_sNaN 0x40000000 /* local status: sNaN signal */
190-#define BADINT (Int)0x80000000 /* most-negative Int; error indicator */
181+#define DECVERB 1 /* set to 1 for verbose DECCHECK */
182+#define powers DECPOWERS /* old internal name */
191183
192-static Unit one[] = { 1 }; /* Unit array of 1, used for incrementing */
184+/* Local constants */
185+#define DIVIDE 0x80 /* Divide operators */
186+#define REMAINDER 0x40 /* .. */
187+#define DIVIDEINT 0x20 /* .. */
188+#define REMNEAR 0x10 /* .. */
189+#define COMPARE 0x01 /* Compare operators */
190+#define COMPMAX 0x02 /* .. */
191+#define COMPMIN 0x03 /* .. */
192+#define COMPTOTAL 0x04 /* .. */
193+#define COMPNAN 0x05 /* .. [NaN processing] */
194+#define COMPSIG 0x06 /* .. [signaling COMPARE] */
195+#define COMPMAXMAG 0x07 /* .. */
196+#define COMPMINMAG 0x08 /* .. */
197+
198+#define DEC_sNaN 0x40000000 /* local status: sNaN signal */
199+#define BADINT (Int)0x80000000 /* most-negative Int; error indicator */
200+/* Next two indicate an integer >= 10**6, and its parity (bottom bit) */
201+#define BIGEVEN (Int)0x80000002
202+#define BIGODD (Int)0x80000003
203+
204+static Unit uarrone[1]={1}; /* Unit array of 1, used for incrementing */
193205
194206 /* Granularity-dependent code */
195207 #if DECDPUN<=4
196-#define eInt Int /* extended integer */
197-#define ueInt uInt /* unsigned extended integer */
208+ #define eInt Int /* extended integer */
209+ #define ueInt uInt /* unsigned extended integer */
198210 /* Constant multipliers for divide-by-power-of five using reciprocal */
199211 /* multiply, after removing powers of 2 by shifting, and final shift */
200212 /* of 17 [we only need up to **4] */
201-static const uInt multies[] = { 131073, 26215, 5243, 1049, 210 };
202-
213+ static const uInt multies[]={131073, 26215, 5243, 1049, 210};
203214 /* QUOT10 -- macro to return the quotient of unit u divided by 10**n */
204-#define QUOT10(u, n) ((((uInt)(u)>>(n))*multies[n])>>17)
215+ #define QUOT10(u, n) ((((uInt)(u)>>(n))*multies[n])>>17)
205216 #else
206- /* For DECDPUN>4 we currently use non-ANSI 64-bit types. These could */
207- /* be replaced by subroutine calls later. */
208-#ifdef long
209-#undef long
210-#endif
211-typedef signed long long Long;
212-typedef unsigned long long uLong;
213-#define eInt Long /* extended integer */
214-#define ueInt uLong /* unsigned extended integer */
217+ /* For DECDPUN>4 non-ANSI-89 64-bit types are needed. */
218+ #if !DECUSE64
219+ #error decNumber.c: DECUSE64 must be 1 when DECDPUN>4
220+ #endif
221+ #define eInt Long /* extended integer */
222+ #define ueInt uLong /* unsigned extended integer */
215223 #endif
216224
217225 /* Local routines */
218-static decNumber *decAddOp (decNumber *, const decNumber *,
219- const decNumber *, decContext *,
220- uByte, uInt *);
221-static void decApplyRound (decNumber *, decContext *, Int, uInt *);
222-static Int decCompare (const decNumber * lhs, const decNumber * rhs);
223-static decNumber *decCompareOp (decNumber *, const decNumber *, const decNumber *,
224- decContext *, Flag, uInt *);
225-static void decCopyFit (decNumber *, const decNumber *, decContext *,
226- Int *, uInt *);
227-static decNumber *decDivideOp (decNumber *, const decNumber *, const decNumber *,
228- decContext *, Flag, uInt *);
229-static void decFinalize (decNumber *, decContext *, Int *, uInt *);
230-static Int decGetDigits (const Unit *, Int);
231-#if DECSUBSET
232-static Int decGetInt (const decNumber *, decContext *);
233-#else
234-static Int decGetInt (const decNumber *);
235-#endif
236-static decNumber *decMultiplyOp (decNumber *, const decNumber *,
237- const decNumber *, decContext *, uInt *);
238-static decNumber *decNaNs (decNumber *, const decNumber *, const decNumber *, uInt *);
239-static decNumber *decQuantizeOp (decNumber *, const decNumber *,
240- const decNumber *, decContext *, Flag, uInt *);
241-static void decSetCoeff (decNumber *, decContext *, const Unit *,
242- Int, Int *, uInt *);
243-static void decSetOverflow (decNumber *, decContext *, uInt *);
244-static void decSetSubnormal (decNumber *, decContext *, Int *, uInt *);
245-static Int decShiftToLeast (Unit *, Int, Int);
246-static Int decShiftToMost (Unit *, Int, Int);
247-static void decStatus (decNumber *, uInt, decContext *);
248-static Flag decStrEq (const char *, const char *);
249-static void decToString (const decNumber *, char[], Flag);
250-static decNumber *decTrim (decNumber *, Flag, Int *);
251-static Int decUnitAddSub (const Unit *, Int, const Unit *, Int, Int, Unit *, Int);
252-static Int decUnitCompare (const Unit *, Int, const Unit *, Int, Int);
226+static decNumber * decAddOp(decNumber *, const decNumber *, const decNumber *,
227+ decContext *, uByte, uInt *);
228+static Flag decBiStr(const char *, const char *, const char *);
229+static uInt decCheckMath(const decNumber *, decContext *, uInt *);
230+static void decApplyRound(decNumber *, decContext *, Int, uInt *);
231+static Int decCompare(const decNumber *lhs, const decNumber *rhs, Flag);
232+static decNumber * decCompareOp(decNumber *, const decNumber *,
233+ const decNumber *, decContext *,
234+ Flag, uInt *);
235+static void decCopyFit(decNumber *, const decNumber *, decContext *,
236+ Int *, uInt *);
237+static decNumber * decDecap(decNumber *, Int);
238+static decNumber * decDivideOp(decNumber *, const decNumber *,
239+ const decNumber *, decContext *, Flag, uInt *);
240+static decNumber * decExpOp(decNumber *, const decNumber *,
241+ decContext *, uInt *);
242+static void decFinalize(decNumber *, decContext *, Int *, uInt *);
243+static Int decGetDigits(Unit *, Int);
244+static Int decGetInt(const decNumber *);
245+static decNumber * decLnOp(decNumber *, const decNumber *,
246+ decContext *, uInt *);
247+static decNumber * decMultiplyOp(decNumber *, const decNumber *,
248+ const decNumber *, decContext *,
249+ uInt *);
250+static decNumber * decNaNs(decNumber *, const decNumber *,
251+ const decNumber *, decContext *, uInt *);
252+static decNumber * decQuantizeOp(decNumber *, const decNumber *,
253+ const decNumber *, decContext *, Flag,
254+ uInt *);
255+static void decReverse(Unit *, Unit *);
256+static void decSetCoeff(decNumber *, decContext *, const Unit *,
257+ Int, Int *, uInt *);
258+static void decSetMaxValue(decNumber *, decContext *);
259+static void decSetOverflow(decNumber *, decContext *, uInt *);
260+static void decSetSubnormal(decNumber *, decContext *, Int *, uInt *);
261+static Int decShiftToLeast(Unit *, Int, Int);
262+static Int decShiftToMost(Unit *, Int, Int);
263+static void decStatus(decNumber *, uInt, decContext *);
264+static void decToString(const decNumber *, char[], Flag);
265+static decNumber * decTrim(decNumber *, decContext *, Flag, Int *);
266+static Int decUnitAddSub(const Unit *, Int, const Unit *, Int, Int,
267+ Unit *, Int);
268+static Int decUnitCompare(const Unit *, Int, const Unit *, Int, Int);
253269
254270 #if !DECSUBSET
255271 /* decFinish == decFinalize when no subset arithmetic needed */
256272 #define decFinish(a,b,c,d) decFinalize(a,b,c,d)
257273 #else
258-static void decFinish (decNumber *, decContext *, Int *, uInt *);
259-static decNumber *decRoundOperand (const decNumber *, decContext *, uInt *);
274+static void decFinish(decNumber *, decContext *, Int *, uInt *);
275+static decNumber * decRoundOperand(const decNumber *, decContext *, uInt *);
260276 #endif
261277
278+/* Local macros */
279+/* masked special-values bits */
280+#define SPECIALARG (rhs->bits & DECSPECIAL)
281+#define SPECIALARGS ((lhs->bits | rhs->bits) & DECSPECIAL)
282+
262283 /* Diagnostic macros, etc. */
263284 #if DECALLOC
264285 /* Handle malloc/free accounting. If enabled, our accountable routines */
@@ -266,5698 +287,7836 @@ static decNumber *decRoundOperand (const decNumber *, decContext *, uInt *);
266287 /* and free routines. */
267288 #define malloc(a) decMalloc(a)
268289 #define free(a) decFree(a)
269-#define DECFENCE 0x5a /* corruption detector */
290+#define DECFENCE 0x5a /* corruption detector */
270291 /* 'Our' malloc and free: */
271-static void *decMalloc (size_t);
272-static void decFree (void *);
273-uInt decAllocBytes = 0; /* count of bytes allocated */
292+static void *decMalloc(size_t);
293+static void decFree(void *);
294+uInt decAllocBytes=0; /* count of bytes allocated */
274295 /* Note that DECALLOC code only checks for storage buffer overflow. */
275-/* To check for memory leaks, the decAllocBytes variable should be */
296+/* To check for memory leaks, the decAllocBytes variable must be */
276297 /* checked to be 0 at appropriate times (e.g., after the test */
277298 /* harness completes a set of tests). This checking may be unreliable */
278299 /* if the testing is done in a multi-thread environment. */
279300 #endif
280301
281302 #if DECCHECK
282-/* Optional operand checking routines. Enabling these means that */
283-/* decNumber and decContext operands to operator routines are checked */
284-/* for correctness. This roughly doubles the execution time of the */
303+/* Optional checking routines. Enabling these means that decNumber */
304+/* and decContext operands to operator routines are checked for */
305+/* correctness. This roughly doubles the execution time of the */
285306 /* fastest routines (and adds 600+ bytes), so should not normally be */
286307 /* used in 'production'. */
287-#define DECUNUSED (void *)(0xffffffff)
288-static Flag decCheckOperands (decNumber *, const decNumber *,
289- const decNumber *, decContext *);
290-static Flag decCheckNumber (const decNumber *, decContext *);
308+/* decCheckInexact is used to check that inexact results have a full */
309+/* complement of digits (where appropriate -- this is not the case */
310+/* for Quantize, for example) */
311+#define DECUNRESU ((decNumber *)(void *)0xffffffff)
312+#define DECUNUSED ((const decNumber *)(void *)0xffffffff)
313+#define DECUNCONT ((decContext *)(void *)(0xffffffff))
314+static Flag decCheckOperands(decNumber *, const decNumber *,
315+ const decNumber *, decContext *);
316+static Flag decCheckNumber(const decNumber *);
317+static void decCheckInexact(const decNumber *, decContext *);
291318 #endif
292319
293320 #if DECTRACE || DECCHECK
294-/* Optional trace/debugging routines. */
295-void decNumberShow (const decNumber *); /* displays the components of a number */
296-static void decDumpAr (char, const Unit *, Int);
321+/* Optional trace/debugging routines (may or may not be used) */
322+void decNumberShow(const decNumber *); /* displays the components of a number */
323+static void decDumpAr(char, const Unit *, Int);
297324 #endif
298325
299326 /* ================================================================== */
300-/* Conversions */
327+/* Conversions */
301328 /* ================================================================== */
302329
303330 /* ------------------------------------------------------------------ */
304-/* to-scientific-string -- conversion to numeric string */
305-/* to-engineering-string -- conversion to numeric string */
306-/* */
307-/* decNumberToString(dn, string); */
308-/* decNumberToEngString(dn, string); */
309-/* */
310-/* dn is the decNumber to convert */
311-/* string is the string where the result will be laid out */
312-/* */
313-/* string must be at least dn->digits+14 characters long */
314-/* */
315-/* No error is possible, and no status can be set. */
316-/* ------------------------------------------------------------------ */
317-char *
318-decNumberToString (const decNumber * dn, char *string)
319-{
320- decToString (dn, string, 0);
331+/* from-int32 -- conversion from Int or uInt */
332+/* */
333+/* dn is the decNumber to receive the integer */
334+/* in or uin is the integer to be converted */
335+/* returns dn */
336+/* */
337+/* No error is possible. */
338+/* ------------------------------------------------------------------ */
339+decNumber * decNumberFromInt32(decNumber *dn, Int in) {
340+ uInt unsig;
341+ if (in>=0) unsig=in;
342+ else { /* negative (possibly BADINT) */
343+ if (in==BADINT) unsig=(uInt)1073741824*2; /* special case */
344+ else unsig=-in; /* invert */
345+ }
346+ /* in is now positive */
347+ decNumberFromUInt32(dn, unsig);
348+ if (in<0) dn->bits=DECNEG; /* sign needed */
349+ return dn;
350+ } /* decNumberFromInt32 */
351+
352+decNumber * decNumberFromUInt32(decNumber *dn, uInt uin) {
353+ Unit *up; /* work pointer */
354+ decNumberZero(dn); /* clean */
355+ if (uin==0) return dn; /* [or decGetDigits bad call] */
356+ for (up=dn->lsu; uin>0; up++) {
357+ *up=(Unit)(uin%(DECDPUNMAX+1));
358+ uin=uin/(DECDPUNMAX+1);
359+ }
360+ dn->digits=decGetDigits(dn->lsu, up-dn->lsu);
361+ return dn;
362+ } /* decNumberFromUInt32 */
363+
364+/* ------------------------------------------------------------------ */
365+/* to-int32 -- conversion to Int or uInt */
366+/* */
367+/* dn is the decNumber to convert */
368+/* set is the context for reporting errors */
369+/* returns the converted decNumber, or 0 if Invalid is set */
370+/* */
371+/* Invalid is set if the decNumber does not have exponent==0 or if */
372+/* it is a NaN, Infinite, or out-of-range. */
373+/* ------------------------------------------------------------------ */
374+Int decNumberToInt32(const decNumber *dn, decContext *set) {
375+ #if DECCHECK
376+ if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
377+ #endif
378+
379+ /* special or too many digits, or bad exponent */
380+ if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0) ; /* bad */
381+ else { /* is a finite integer with 10 or fewer digits */
382+ Int d; /* work */
383+ const Unit *up; /* .. */
384+ uInt hi=0, lo; /* .. */
385+ up=dn->lsu; /* -> lsu */
386+ lo=*up; /* get 1 to 9 digits */
387+ #if DECDPUN>1 /* split to higher */
388+ hi=lo/10;
389+ lo=lo%10;
390+ #endif
391+ up++;
392+ /* collect remaining Units, if any, into hi */
393+ for (d=DECDPUN; d<dn->digits; up++, d+=DECDPUN) hi+=*up*powers[d-1];
394+ /* now low has the lsd, hi the remainder */
395+ if (hi>214748364 || (hi==214748364 && lo>7)) { /* out of range? */
396+ /* most-negative is a reprieve */
397+ if (dn->bits&DECNEG && hi==214748364 && lo==8) return 0x80000000;
398+ /* bad -- drop through */
399+ }
400+ else { /* in-range always */
401+ Int i=X10(hi)+lo;
402+ if (dn->bits&DECNEG) return -i;
403+ return i;
404+ }
405+ } /* integer */
406+ decContextSetStatus(set, DEC_Invalid_operation); /* [may not return] */
407+ return 0;
408+ } /* decNumberToInt32 */
409+
410+uInt decNumberToUInt32(const decNumber *dn, decContext *set) {
411+ #if DECCHECK
412+ if (decCheckOperands(DECUNRESU, DECUNUSED, dn, set)) return 0;
413+ #endif
414+ /* special or too many digits, or bad exponent, or negative (<0) */
415+ if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0
416+ || (dn->bits&DECNEG && !ISZERO(dn))); /* bad */
417+ else { /* is a finite integer with 10 or fewer digits */
418+ Int d; /* work */
419+ const Unit *up; /* .. */
420+ uInt hi=0, lo; /* .. */
421+ up=dn->lsu; /* -> lsu */
422+ lo=*up; /* get 1 to 9 digits */
423+ #if DECDPUN>1 /* split to higher */
424+ hi=lo/10;
425+ lo=lo%10;
426+ #endif
427+ up++;
428+ /* collect remaining Units, if any, into hi */
429+ for (d=DECDPUN; d<dn->digits; up++, d+=DECDPUN) hi+=*up*powers[d-1];
430+
431+ /* now low has the lsd, hi the remainder */
432+ if (hi>429496729 || (hi==429496729 && lo>5)) ; /* no reprieve possible */
433+ else return X10(hi)+lo;
434+ } /* integer */
435+ decContextSetStatus(set, DEC_Invalid_operation); /* [may not return] */
436+ return 0;
437+ } /* decNumberToUInt32 */
438+
439+/* ------------------------------------------------------------------ */
440+/* to-scientific-string -- conversion to numeric string */
441+/* to-engineering-string -- conversion to numeric string */
442+/* */
443+/* decNumberToString(dn, string); */
444+/* decNumberToEngString(dn, string); */
445+/* */
446+/* dn is the decNumber to convert */
447+/* string is the string where the result will be laid out */
448+/* */
449+/* string must be at least dn->digits+14 characters long */
450+/* */
451+/* No error is possible, and no status can be set. */
452+/* ------------------------------------------------------------------ */
453+char * decNumberToString(const decNumber *dn, char *string){
454+ decToString(dn, string, 0);
321455 return string;
322-}
456+ } /* DecNumberToString */
323457
324-char *
325-decNumberToEngString (const decNumber * dn, char *string)
326-{
327- decToString (dn, string, 1);
458+char * decNumberToEngString(const decNumber *dn, char *string){
459+ decToString(dn, string, 1);
328460 return string;
329-}
330-
331-/* ------------------------------------------------------------------ */
332-/* to-number -- conversion from numeric string */
333-/* */
334-/* decNumberFromString -- convert string to decNumber */
335-/* dn -- the number structure to fill */
336-/* chars[] -- the string to convert ('\0' terminated) */
337-/* set -- the context used for processing any error, */
338-/* determining the maximum precision available */
339-/* (set.digits), determining the maximum and minimum */
340-/* exponent (set.emax and set.emin), determining if */
341-/* extended values are allowed, and checking the */
342-/* rounding mode if overflow occurs or rounding is */
343-/* needed. */
344-/* */
461+ } /* DecNumberToEngString */
462+
463+/* ------------------------------------------------------------------ */
464+/* to-number -- conversion from numeric string */
465+/* */
466+/* decNumberFromString -- convert string to decNumber */
467+/* dn -- the number structure to fill */
468+/* chars[] -- the string to convert ('\0' terminated) */
469+/* set -- the context used for processing any error, */
470+/* determining the maximum precision available */
471+/* (set.digits), determining the maximum and minimum */
472+/* exponent (set.emax and set.emin), determining if */
473+/* extended values are allowed, and checking the */
474+/* rounding mode if overflow occurs or rounding is */
475+/* needed. */
476+/* */
345477 /* The length of the coefficient and the size of the exponent are */
346-/* checked by this routine, so the correct error (Underflow or */
347-/* Overflow) can be reported or rounding applied, as necessary. */
348-/* */
349-/* If bad syntax is detected, the result will be a quiet NaN. */
350-/* ------------------------------------------------------------------ */
351-decNumber *
352-decNumberFromString (decNumber * dn, const char chars[], decContext * set)
353-{
354- Int exponent = 0; /* working exponent [assume 0] */
355- uByte bits = 0; /* working flags [assume +ve] */
356- Unit *res; /* where result will be built */
357- Unit resbuff[D2U (DECBUFFER + 1)]; /* local buffer in case need temporary */
358- Unit *allocres = NULL; /* -> allocated result, iff allocated */
359- Int need; /* units needed for result */
360- Int d = 0; /* count of digits found in decimal part */
361- const char *dotchar = NULL; /* where dot was found */
362- const char *cfirst; /* -> first character of decimal part */
363- const char *last = NULL; /* -> last digit of decimal part */
364- const char *firstexp; /* -> first significant exponent digit */
365- const char *c; /* work */
366- Unit *up; /* .. */
367-#if DECDPUN>1
368- Int i; /* .. */
369-#endif
370- Int residue = 0; /* rounding residue */
371- uInt status = 0; /* error code */
372-
373-#if DECCHECK
374- if (decCheckOperands (DECUNUSED, DECUNUSED, DECUNUSED, set))
375- return decNumberZero (dn);
376-#endif
377-
378- do
379- { /* status & malloc protection */
380- c = chars; /* -> input character */
381- if (*c == '-')
382- { /* handle leading '-' */
383- bits = DECNEG;
384- c++;
478+/* checked by this routine, so the correct error (Underflow or */
479+/* Overflow) can be reported or rounding applied, as necessary. */
480+/* */
481+/* If bad syntax is detected, the result will be a quiet NaN. */
482+/* ------------------------------------------------------------------ */
483+decNumber * decNumberFromString(decNumber *dn, const char chars[],
484+ decContext *set) {
485+ Int exponent=0; /* working exponent [assume 0] */
486+ uByte bits=0; /* working flags [assume +ve] */
487+ Unit *res; /* where result will be built */
488+ Unit resbuff[SD2U(DECBUFFER+9)];/* local buffer in case need temporary */
489+ /* [+9 allows for ln() constants] */
490+ Unit *allocres=NULL; /* -> allocated result, iff allocated */
491+ Int d=0; /* count of digits found in decimal part */
492+ const char *dotchar=NULL; /* where dot was found */
493+ const char *cfirst=chars; /* -> first character of decimal part */
494+ const char *last=NULL; /* -> last digit of decimal part */
495+ const char *c; /* work */
496+ Unit *up; /* .. */
497+ #if DECDPUN>1
498+ Int cut, out; /* .. */
499+ #endif
500+ Int residue; /* rounding residue */
501+ uInt status=0; /* error code */
502+
503+ #if DECCHECK
504+ if (decCheckOperands(DECUNRESU, DECUNUSED, DECUNUSED, set))
505+ return decNumberZero(dn);
506+ #endif
507+
508+ do { /* status & malloc protection */
509+ for (c=chars;; c++) { /* -> input character */
510+ if (*c>='0' && *c<='9') { /* test for Arabic digit */
511+ last=c;
512+ d++; /* count of real digits */
513+ continue; /* still in decimal part */
385514 }
386- else if (*c == '+')
387- c++; /* step over leading '+' */
388- /* We're at the start of the number [we think] */
389- cfirst = c; /* save */
390- for (;; c++)
391- {
392- if (*c >= '0' && *c <= '9')
393- { /* test for Arabic digit */
394- last = c;
395- d++; /* count of real digits */
396- continue; /* still in decimal part */
397- }
398- if (*c != '.')
399- break; /* done with decimal part */
400- /* dot: record, check, and ignore */
401- if (dotchar != NULL)
402- { /* two dots */
403- last = NULL; /* indicate bad */
404- break;
405- } /* .. and go report */
406- dotchar = c; /* offset into decimal part */
407- } /* c */
408-
409- if (last == NULL)
410- { /* no decimal digits, or >1 . */
411-#if DECSUBSET
412- /* If subset then infinities and NaNs are not allowed */
413- if (!set->extended)
414- {
415- status = DEC_Conversion_syntax;
416- break; /* all done */
417- }
418- else
419- {
420-#endif
421- /* Infinities and NaNs are possible, here */
422- decNumberZero (dn); /* be optimistic */
423- if (decStrEq (c, "Infinity") || decStrEq (c, "Inf"))
424- {
425- dn->bits = bits | DECINF;
426- break; /* all done */
427- }
428- else
429- { /* a NaN expected */
430- /* 2003.09.10 NaNs are now permitted to have a sign */
431- status = DEC_Conversion_syntax; /* assume the worst */
432- dn->bits = bits | DECNAN; /* assume simple NaN */
433- if (*c == 's' || *c == 'S')
434- { /* looks like an` sNaN */
435- c++;
436- dn->bits = bits | DECSNAN;
437- }
438- if (*c != 'n' && *c != 'N')
439- break; /* check caseless "NaN" */
440- c++;
441- if (*c != 'a' && *c != 'A')
442- break; /* .. */
443- c++;
444- if (*c != 'n' && *c != 'N')
445- break; /* .. */
446- c++;
447- /* now nothing, or nnnn, expected */
448- /* -> start of integer and skip leading 0s [including plain 0] */
449- for (cfirst = c; *cfirst == '0';)
450- cfirst++;
451- if (*cfirst == '\0')
452- { /* "NaN" or "sNaN", maybe with all 0s */
453- status = 0; /* it's good */
454- break; /* .. */
455- }
456- /* something other than 0s; setup last and d as usual [no dots] */
457- for (c = cfirst;; c++, d++)
458- {
459- if (*c < '0' || *c > '9')
460- break; /* test for Arabic digit */
461- last = c;
462- }
463- if (*c != '\0')
464- break; /* not all digits */
465- if (d > set->digits)
466- break; /* too many digits */
467- /* good; drop through and convert the integer */
468- status = 0;
469- bits = dn->bits; /* for copy-back */
470- } /* NaN expected */
471-#if DECSUBSET
472- }
473-#endif
474- } /* last==NULL */
475-
476- if (*c != '\0')
477- { /* more there; exponent expected... */
478- Flag nege = 0; /* 1=negative exponent */
479- if (*c != 'e' && *c != 'E')
480- {
481- status = DEC_Conversion_syntax;
482- break;
483- }
484-
485- /* Found 'e' or 'E' -- now process explicit exponent */
486- /* 1998.07.11: sign no longer required */
487- c++; /* to (expected) sign */
488- if (*c == '-')
489- {
490- nege = 1;
491- c++;
492- }
493- else if (*c == '+')
494- c++;
495- if (*c == '\0')
496- {
497- status = DEC_Conversion_syntax;
498- break;
499- }
500-
501- for (; *c == '0' && *(c + 1) != '\0';)
502- c++; /* strip insignificant zeros */
503- firstexp = c; /* save exponent digit place */
504- for (;; c++)
505- {
506- if (*c < '0' || *c > '9')
507- break; /* not a digit */
508- exponent = X10 (exponent) + (Int) * c - (Int) '0';
509- } /* c */
510- /* if we didn't end on '\0' must not be a digit */
511- if (*c != '\0')
512- {
513- status = DEC_Conversion_syntax;
514- break;
515- }
516-
517- /* (this next test must be after the syntax check) */
518- /* if it was too long the exponent may have wrapped, so check */
519- /* carefully and set it to a certain overflow if wrap possible */
520- if (c >= firstexp + 9 + 1)
521- {
522- if (c > firstexp + 9 + 1 || *firstexp > '1')
523- exponent = DECNUMMAXE * 2;
524- /* [up to 1999999999 is OK, for example 1E-1000000998] */
525- }
526- if (nege)
527- exponent = -exponent; /* was negative */
528- } /* had exponent */
529- /* Here when all inspected; syntax is good */
530-
531- /* Handle decimal point... */
532- if (dotchar != NULL && dotchar < last) /* embedded . found, so */
533- exponent = exponent - (last - dotchar); /* .. adjust exponent */
534- /* [we can now ignore the .] */
535-
536- /* strip leading zeros/dot (leave final if all 0's) */
537- for (c = cfirst; c < last; c++)
538- {
539- if (*c == '0')
540- d--; /* 0 stripped */
541- else if (*c != '.')
542- break;
543- cfirst++; /* step past leader */
544- } /* c */
545-
546-#if DECSUBSET
547- /* We can now make a rapid exit for zeros if !extended */
548- if (*cfirst == '0' && !set->extended)
549- {
550- decNumberZero (dn); /* clean result */
551- break; /* [could be return] */
515+ if (*c=='.' && dotchar==NULL) { /* first '.' */
516+ dotchar=c; /* record offset into decimal part */
517+ if (c==cfirst) cfirst++; /* first digit must follow */
518+ continue;}
519+ if (c==chars) { /* first in string... */
520+ if (*c=='-') { /* valid - sign */
521+ cfirst++;
522+ bits=DECNEG;
523+ continue;}
524+ if (*c=='+') { /* valid + sign */
525+ cfirst++;
526+ continue;}
552527 }
553-#endif
554-
555- /* OK, the digits string is good. Copy to the decNumber, or to
556- a temporary decNumber if rounding is needed */
557- if (d <= set->digits)
558- res = dn->lsu; /* fits into given decNumber */
559- else
560- { /* rounding needed */
561- need = D2U (d); /* units needed */
562- res = resbuff; /* assume use local buffer */
563- if (need * sizeof (Unit) > sizeof (resbuff))
564- { /* too big for local */
565- allocres = (Unit *) malloc (need * sizeof (Unit));
566- if (allocres == NULL)
567- {
568- status |= DEC_Insufficient_storage;
569- break;
570- }
571- res = allocres;
572- }
528+ /* *c is not a digit, or a valid +, -, or '.' */
529+ break;
530+ } /* c */
531+
532+ if (last==NULL) { /* no digits yet */
533+ status=DEC_Conversion_syntax;/* assume the worst */
534+ if (*c=='\0') break; /* and no more to come... */
535+ #if DECSUBSET
536+ /* if subset then infinities and NaNs are not allowed */
537+ if (!set->extended) break; /* hopeless */
538+ #endif
539+ /* Infinities and NaNs are possible, here */
540+ if (dotchar!=NULL) break; /* .. unless had a dot */
541+ decNumberZero(dn); /* be optimistic */
542+ if (decBiStr(c, "infinity", "INFINITY")
543+ || decBiStr(c, "inf", "INF")) {
544+ dn->bits=bits | DECINF;
545+ status=0; /* is OK */
546+ break; /* all done */
573547 }
574- /* res now -> number lsu, buffer, or allocated storage for Unit array */
575-
576- /* Place the coefficient into the selected Unit array */
577-#if DECDPUN>1
578- i = d % DECDPUN; /* digits in top unit */
579- if (i == 0)
580- i = DECDPUN;
581- up = res + D2U (d) - 1; /* -> msu */
582- *up = 0;
583- for (c = cfirst;; c++)
584- { /* along the digits */
585- if (*c == '.')
586- { /* ignore . [don't decrement i] */
587- if (c != last)
588- continue;
589- break;
590- }
591- *up = (Unit) (X10 (*up) + (Int) * c - (Int) '0');
592- i--;
593- if (i > 0)
594- continue; /* more for this unit */
595- if (up == res)
596- break; /* just filled the last unit */
597- i = DECDPUN;
598- up--;
599- *up = 0;
600- } /* c */
601-#else
602- /* DECDPUN==1 */
603- up = res; /* -> lsu */
604- for (c = last; c >= cfirst; c--)
605- { /* over each character, from least */
606- if (*c == '.')
607- continue; /* ignore . [don't step b] */
608- *up = (Unit) ((Int) * c - (Int) '0');
609- up++;
610- } /* c */
611-#endif
612-
613- dn->bits = bits;
614- dn->exponent = exponent;
615- dn->digits = d;
616-
617- /* if not in number (too long) shorten into the number */
618- if (d > set->digits)
619- decSetCoeff (dn, set, res, d, &residue, &status);
620-
621- /* Finally check for overflow or subnormal and round as needed */
622- decFinalize (dn, set, &residue, &status);
623- /* decNumberShow(dn); */
624- }
625- while (0); /* [for break] */
548+ /* a NaN expected */
549+ /* 2003.09.10 NaNs are now permitted to have a sign */
550+ dn->bits=bits | DECNAN; /* assume simple NaN */
551+ if (*c=='s' || *c=='S') { /* looks like an sNaN */
552+ c++;
553+ dn->bits=bits | DECSNAN;
554+ }
555+ if (*c!='n' && *c!='N') break; /* check caseless "NaN" */
556+ c++;
557+ if (*c!='a' && *c!='A') break; /* .. */
558+ c++;
559+ if (*c!='n' && *c!='N') break; /* .. */
560+ c++;
561+ /* now either nothing, or nnnn payload, expected */
562+ /* -> start of integer and skip leading 0s [including plain 0] */
563+ for (cfirst=c; *cfirst=='0';) cfirst++;
564+ if (*cfirst=='\0') { /* "NaN" or "sNaN", maybe with all 0s */
565+ status=0; /* it's good */
566+ break; /* .. */
567+ }
568+ /* something other than 0s; setup last and d as usual [no dots] */
569+ for (c=cfirst;; c++, d++) {
570+ if (*c<'0' || *c>'9') break; /* test for Arabic digit */
571+ last=c;
572+ }
573+ if (*c!='\0') break; /* not all digits */
574+ if (d>set->digits-1) {
575+ /* [NB: payload in a decNumber can be full length unless */
576+ /* clamped, in which case can only be digits-1] */
577+ if (set->clamp) break;
578+ if (d>set->digits) break;
579+ } /* too many digits? */
580+ /* good; drop through to convert the integer to coefficient */
581+ status=0; /* syntax is OK */
582+ bits=dn->bits; /* for copy-back */
583+ } /* last==NULL */
584+
585+ else if (*c!='\0') { /* more to process... */
586+ /* had some digits; exponent is only valid sequence now */
587+ Flag nege; /* 1=negative exponent */
588+ const char *firstexp; /* -> first significant exponent digit */
589+ status=DEC_Conversion_syntax;/* assume the worst */
590+ if (*c!='e' && *c!='E') break;
591+ /* Found 'e' or 'E' -- now process explicit exponent */
592+ /* 1998.07.11: sign no longer required */
593+ nege=0;
594+ c++; /* to (possible) sign */
595+ if (*c=='-') {nege=1; c++;}
596+ else if (*c=='+') c++;
597+ if (*c=='\0') break;
598+
599+ for (; *c=='0' && *(c+1)!='\0';) c++; /* strip insignificant zeros */
600+ firstexp=c; /* save exponent digit place */
601+ for (; ;c++) {
602+ if (*c<'0' || *c>'9') break; /* not a digit */
603+ exponent=X10(exponent)+(Int)*c-(Int)'0';
604+ } /* c */
605+ /* if not now on a '\0', *c must not be a digit */
606+ if (*c!='\0') break;
607+
608+ /* (this next test must be after the syntax checks) */
609+ /* if it was too long the exponent may have wrapped, so check */
610+ /* carefully and set it to a certain overflow if wrap possible */
611+ if (c>=firstexp+9+1) {
612+ if (c>firstexp+9+1 || *firstexp>'1') exponent=DECNUMMAXE*2;
613+ /* [up to 1999999999 is OK, for example 1E-1000000998] */
614+ }
615+ if (nege) exponent=-exponent; /* was negative */
616+ status=0; /* is OK */
617+ } /* stuff after digits */
618+
619+ /* Here when whole string has been inspected; syntax is good */
620+ /* cfirst->first digit (never dot), last->last digit (ditto) */
621+
622+ /* strip leading zeros/dot [leave final 0 if all 0's] */
623+ if (*cfirst=='0') { /* [cfirst has stepped over .] */
624+ for (c=cfirst; c<last; c++, cfirst++) {
625+ if (*c=='.') continue; /* ignore dots */
626+ if (*c!='0') break; /* non-zero found */
627+ d--; /* 0 stripped */
628+ } /* c */
629+ #if DECSUBSET
630+ /* make a rapid exit for easy zeros if !extended */
631+ if (*cfirst=='0' && !set->extended) {
632+ decNumberZero(dn); /* clean result */
633+ break; /* [could be return] */
634+ }
635+ #endif
636+ } /* at least one leading 0 */
637+
638+ /* Handle decimal point... */
639+ if (dotchar!=NULL && dotchar<last) /* non-trailing '.' found? */
640+ exponent-=(last-dotchar); /* adjust exponent */
641+ /* [we can now ignore the .] */
642+
643+ /* OK, the digits string is good. Assemble in the decNumber, or in */
644+ /* a temporary units array if rounding is needed */
645+ if (d<=set->digits) res=dn->lsu; /* fits into supplied decNumber */
646+ else { /* rounding needed */
647+ Int needbytes=D2U(d)*sizeof(Unit);/* bytes needed */
648+ res=resbuff; /* assume use local buffer */
649+ if (needbytes>(Int)sizeof(resbuff)) { /* too big for local */
650+ allocres=(Unit *)malloc(needbytes);
651+ if (allocres==NULL) {status|=DEC_Insufficient_storage; break;}
652+ res=allocres;
653+ }
654+ }
655+ /* res now -> number lsu, buffer, or allocated storage for Unit array */
656+
657+ /* Place the coefficient into the selected Unit array */
658+ /* [this is often 70% of the cost of this function when DECDPUN>1] */
659+ #if DECDPUN>1
660+ out=0; /* accumulator */
661+ up=res+D2U(d)-1; /* -> msu */
662+ cut=d-(up-res)*DECDPUN; /* digits in top unit */
663+ for (c=cfirst;; c++) { /* along the digits */
664+ if (*c=='.') continue; /* ignore '.' [don't decrement cut] */
665+ out=X10(out)+(Int)*c-(Int)'0';
666+ if (c==last) break; /* done [never get to trailing '.'] */
667+ cut--;
668+ if (cut>0) continue; /* more for this unit */
669+ *up=(Unit)out; /* write unit */
670+ up--; /* prepare for unit below.. */
671+ cut=DECDPUN; /* .. */
672+ out=0; /* .. */
673+ } /* c */
674+ *up=(Unit)out; /* write lsu */
675+
676+ #else
677+ /* DECDPUN==1 */
678+ up=res; /* -> lsu */
679+ for (c=last; c>=cfirst; c--) { /* over each character, from least */
680+ if (*c=='.') continue; /* ignore . [don't step up] */
681+ *up=(Unit)((Int)*c-(Int)'0');
682+ up++;
683+ } /* c */
684+ #endif
685+
686+ dn->bits=bits;
687+ dn->exponent=exponent;
688+ dn->digits=d;
689+
690+ /* if not in number (too long) shorten into the number */
691+ if (d>set->digits) {
692+ residue=0;
693+ decSetCoeff(dn, set, res, d, &residue, &status);
694+ /* always check for overflow or subnormal and round as needed */
695+ decFinalize(dn, set, &residue, &status);
696+ }
697+ else { /* no rounding, but may still have overflow or subnormal */
698+ /* [these tests are just for performance; finalize repeats them] */
699+ if ((dn->exponent-1<set->emin-dn->digits)
700+ || (dn->exponent-1>set->emax-set->digits)) {
701+ residue=0;
702+ decFinalize(dn, set, &residue, &status);
703+ }
704+ }
705+ /* decNumberShow(dn); */
706+ } while(0); /* [for break] */
626707
627- if (allocres != NULL)
628- free (allocres); /* drop any storage we used */
629- if (status != 0)
630- decStatus (dn, status, set);
708+ if (allocres!=NULL) free(allocres); /* drop any storage used */
709+ if (status!=0) decStatus(dn, status, set);
631710 return dn;
632-}
711+ } /* decNumberFromString */
633712
634713 /* ================================================================== */
635-/* Operators */
714+/* Operators */
636715 /* ================================================================== */
637716
638717 /* ------------------------------------------------------------------ */
639-/* decNumberAbs -- absolute value operator */
640-/* */
641-/* This computes C = abs(A) */
642-/* */
643-/* res is C, the result. C may be A */
644-/* rhs is A */
645-/* set is the context */
646-/* */
647-/* C must have space for set->digits digits. */
718+/* decNumberAbs -- absolute value operator */
719+/* */
720+/* This computes C = abs(A) */
721+/* */
722+/* res is C, the result. C may be A */
723+/* rhs is A */
724+/* set is the context */
725+/* */
726+/* See also decNumberCopyAbs for a quiet bitwise version of this. */
727+/* C must have space for set->digits digits. */
648728 /* ------------------------------------------------------------------ */
649729 /* This has the same effect as decNumberPlus unless A is negative, */
650-/* in which case it has the same effect as decNumberMinus. */
651-/* ------------------------------------------------------------------ */
652-decNumber *
653-decNumberAbs (decNumber * res, const decNumber * rhs, decContext * set)
654-{
655- decNumber dzero; /* for 0 */
656- uInt status = 0; /* accumulator */
657-
658-#if DECCHECK
659- if (decCheckOperands (res, DECUNUSED, rhs, set))
730+/* in which case it has the same effect as decNumberMinus. */
731+/* ------------------------------------------------------------------ */
732+decNumber * decNumberAbs(decNumber *res, const decNumber *rhs,
733+ decContext *set) {
734+ decNumber dzero; /* for 0 */
735+ uInt status=0; /* accumulator */
736+
737+ #if DECCHECK
738+ if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
739+ #endif
740+
741+ decNumberZero(&dzero); /* set 0 */
742+ dzero.exponent=rhs->exponent; /* [no coefficient expansion] */
743+ decAddOp(res, &dzero, rhs, set, (uByte)(rhs->bits & DECNEG), &status);
744+ if (status!=0) decStatus(res, status, set);
745+ #if DECCHECK
746+ decCheckInexact(res, set);
747+ #endif
748+ return res;
749+ } /* decNumberAbs */
750+
751+/* ------------------------------------------------------------------ */
752+/* decNumberAdd -- add two Numbers */
753+/* */
754+/* This computes C = A + B */
755+/* */
756+/* res is C, the result. C may be A and/or B (e.g., X=X+X) */
757+/* lhs is A */
758+/* rhs is B */
759+/* set is the context */
760+/* */
761+/* C must have space for set->digits digits. */
762+/* ------------------------------------------------------------------ */
763+/* This just calls the routine shared with Subtract */
764+decNumber * decNumberAdd(decNumber *res, const decNumber *lhs,
765+ const decNumber *rhs, decContext *set) {
766+ uInt status=0; /* accumulator */
767+ decAddOp(res, lhs, rhs, set, 0, &status);
768+ if (status!=0) decStatus(res, status, set);
769+ #if DECCHECK
770+ decCheckInexact(res, set);
771+ #endif
772+ return res;
773+ } /* decNumberAdd */
774+
775+/* ------------------------------------------------------------------ */
776+/* decNumberAnd -- AND two Numbers, digitwise */
777+/* */
778+/* This computes C = A & B */
779+/* */
780+/* res is C, the result. C may be A and/or B (e.g., X=X&X) */
781+/* lhs is A */
782+/* rhs is B */
783+/* set is the context (used for result length and error report) */
784+/* */
785+/* C must have space for set->digits digits. */
786+/* */
787+/* Logical function restrictions apply (see above); a NaN is */
788+/* returned with Invalid_operation if a restriction is violated. */
789+/* ------------------------------------------------------------------ */
790+decNumber * decNumberAnd(decNumber *res, const decNumber *lhs,
791+ const decNumber *rhs, decContext *set) {
792+ const Unit *ua, *ub; /* -> operands */
793+ const Unit *msua, *msub; /* -> operand msus */
794+ Unit *uc, *msuc; /* -> result and its msu */
795+ Int msudigs; /* digits in res msu */
796+ #if DECCHECK
797+ if (decCheckOperands(res, lhs, rhs, set)) return res;
798+ #endif
799+
800+ if (lhs->exponent!=0 || decNumberIsSpecial(lhs) || decNumberIsNegative(lhs)
801+ || rhs->exponent!=0 || decNumberIsSpecial(rhs) || decNumberIsNegative(rhs)) {
802+ decStatus(res, DEC_Invalid_operation, set);
660803 return res;
661-#endif
804+ }
662805
663- decNumberZero (&dzero); /* set 0 */
664- dzero.exponent = rhs->exponent; /* [no coefficient expansion] */
665- decAddOp (res, &dzero, rhs, set, (uByte) (rhs->bits & DECNEG), &status);
666- if (status != 0)
667- decStatus (res, status, set);
806+ /* operands are valid */
807+ ua=lhs->lsu; /* bottom-up */
808+ ub=rhs->lsu; /* .. */
809+ uc=res->lsu; /* .. */
810+ msua=ua+D2U(lhs->digits)-1; /* -> msu of lhs */
811+ msub=ub+D2U(rhs->digits)-1; /* -> msu of rhs */
812+ msuc=uc+D2U(set->digits)-1; /* -> msu of result */
813+ msudigs=MSUDIGITS(set->digits); /* [faster than remainder] */
814+ for (; uc<=msuc; ua++, ub++, uc++) { /* Unit loop */
815+ Unit a, b; /* extract units */
816+ if (ua>msua) a=0;
817+ else a=*ua;
818+ if (ub>msub) b=0;
819+ else b=*ub;
820+ *uc=0; /* can now write back */
821+ if (a|b) { /* maybe 1 bits to examine */
822+ Int i, j;
823+ *uc=0; /* can now write back */
824+ /* This loop could be unrolled and/or use BIN2BCD tables */
825+ for (i=0; i<DECDPUN; i++) {
826+ if (a&b&1) *uc=*uc+(Unit)powers[i]; /* effect AND */
827+ j=a%10;
828+ a=a/10;
829+ j|=b%10;
830+ b=b/10;
831+ if (j>1) {
832+ decStatus(res, DEC_Invalid_operation, set);
833+ return res;
834+ }
835+ if (uc==msuc && i==msudigs-1) break; /* just did final digit */
836+ } /* each digit */
837+ } /* both OK */
838+ } /* each unit */
839+ /* [here uc-1 is the msu of the result] */
840+ res->digits=decGetDigits(res->lsu, uc-res->lsu);
841+ res->exponent=0; /* integer */
842+ res->bits=0; /* sign=0 */
843+ return res; /* [no status to set] */
844+ } /* decNumberAnd */
845+
846+/* ------------------------------------------------------------------ */
847+/* decNumberCompare -- compare two Numbers */
848+/* */
849+/* This computes C = A ? B */
850+/* */
851+/* res is C, the result. C may be A and/or B (e.g., X=X?X) */
852+/* lhs is A */
853+/* rhs is B */
854+/* set is the context */
855+/* */
856+/* C must have space for one digit (or NaN). */
857+/* ------------------------------------------------------------------ */
858+decNumber * decNumberCompare(decNumber *res, const decNumber *lhs,
859+ const decNumber *rhs, decContext *set) {
860+ uInt status=0; /* accumulator */
861+ decCompareOp(res, lhs, rhs, set, COMPARE, &status);
862+ if (status!=0) decStatus(res, status, set);
668863 return res;
669-}
670-
671-/* ------------------------------------------------------------------ */
672-/* decNumberAdd -- add two Numbers */
673-/* */
674-/* This computes C = A + B */
675-/* */
676-/* res is C, the result. C may be A and/or B (e.g., X=X+X) */
677-/* lhs is A */
678-/* rhs is B */
679-/* set is the context */
680-/* */
681-/* C must have space for set->digits digits. */
682-/* ------------------------------------------------------------------ */
683-/* This just calls the routine shared with Subtract */
684-decNumber *
685-decNumberAdd (decNumber * res, const decNumber * lhs,
686- const decNumber * rhs, decContext * set)
687-{
688- uInt status = 0; /* accumulator */
689- decAddOp (res, lhs, rhs, set, 0, &status);
690- if (status != 0)
691- decStatus (res, status, set);
864+ } /* decNumberCompare */
865+
866+/* ------------------------------------------------------------------ */
867+/* decNumberCompareSignal -- compare, signalling on all NaNs */
868+/* */
869+/* This computes C = A ? B */
870+/* */
871+/* res is C, the result. C may be A and/or B (e.g., X=X?X) */
872+/* lhs is A */
873+/* rhs is B */
874+/* set is the context */
875+/* */
876+/* C must have space for one digit (or NaN). */
877+/* ------------------------------------------------------------------ */
878+decNumber * decNumberCompareSignal(decNumber *res, const decNumber *lhs,
879+ const decNumber *rhs, decContext *set) {
880+ uInt status=0; /* accumulator */
881+ decCompareOp(res, lhs, rhs, set, COMPSIG, &status);
882+ if (status!=0) decStatus(res, status, set);
692883 return res;
693-}
694-
695-/* ------------------------------------------------------------------ */
696-/* decNumberCompare -- compare two Numbers */
697-/* */
698-/* This computes C = A ? B */
699-/* */
700-/* res is C, the result. C may be A and/or B (e.g., X=X?X) */
701-/* lhs is A */
702-/* rhs is B */
703-/* set is the context */
704-/* */
705-/* C must have space for one digit. */
706-/* ------------------------------------------------------------------ */
707-decNumber *
708-decNumberCompare (decNumber * res, const decNumber * lhs,
709- const decNumber * rhs, decContext * set)
710-{
711- uInt status = 0; /* accumulator */
712- decCompareOp (res, lhs, rhs, set, COMPARE, &status);
713- if (status != 0)
714- decStatus (res, status, set);
884+ } /* decNumberCompareSignal */
885+
886+/* ------------------------------------------------------------------ */
887+/* decNumberCompareTotal -- compare two Numbers, using total ordering */
888+/* */
889+/* This computes C = A ? B, under total ordering */
890+/* */
891+/* res is C, the result. C may be A and/or B (e.g., X=X?X) */
892+/* lhs is A */
893+/* rhs is B */
894+/* set is the context */
895+/* */
896+/* C must have space for one digit; the result will always be one of */
897+/* -1, 0, or 1. */
898+/* ------------------------------------------------------------------ */
899+decNumber * decNumberCompareTotal(decNumber *res, const decNumber *lhs,
900+ const decNumber *rhs, decContext *set) {
901+ uInt status=0; /* accumulator */
902+ decCompareOp(res, lhs, rhs, set, COMPTOTAL, &status);
903+ if (status!=0) decStatus(res, status, set);
904+ return res;
905+ } /* decNumberCompareTotal */
906+
907+/* ------------------------------------------------------------------ */
908+/* decNumberCompareTotalMag -- compare, total ordering of magnitudes */
909+/* */
910+/* This computes C = |A| ? |B|, under total ordering */
911+/* */
912+/* res is C, the result. C may be A and/or B (e.g., X=X?X) */
913+/* lhs is A */
914+/* rhs is B */
915+/* set is the context */
916+/* */
917+/* C must have space for one digit; the result will always be one of */
918+/* -1, 0, or 1. */
919+/* ------------------------------------------------------------------ */
920+decNumber * decNumberCompareTotalMag(decNumber *res, const decNumber *lhs,
921+ const decNumber *rhs, decContext *set) {
922+ uInt status=0; /* accumulator */
923+ uInt needbytes; /* for space calculations */
924+ decNumber bufa[D2N(DECBUFFER+1)];/* +1 in case DECBUFFER=0 */
925+ decNumber *allocbufa=NULL; /* -> allocated bufa, iff allocated */
926+ decNumber bufb[D2N(DECBUFFER+1)];
927+ decNumber *allocbufb=NULL; /* -> allocated bufb, iff allocated */
928+ decNumber *a, *b; /* temporary pointers */
929+
930+ #if DECCHECK
931+ if (decCheckOperands(res, lhs, rhs, set)) return res;
932+ #endif
933+
934+ do { /* protect allocated storage */
935+ /* if either is negative, take a copy and absolute */
936+ if (decNumberIsNegative(lhs)) { /* lhs<0 */
937+ a=bufa;
938+ needbytes=sizeof(decNumber)+(D2U(lhs->digits)-1)*sizeof(Unit);
939+ if (needbytes>sizeof(bufa)) { /* need malloc space */
940+ allocbufa=(decNumber *)malloc(needbytes);
941+ if (allocbufa==NULL) { /* hopeless -- abandon */
942+ status|=DEC_Insufficient_storage;
943+ break;}
944+ a=allocbufa; /* use the allocated space */
945+ }
946+ decNumberCopy(a, lhs); /* copy content */
947+ a->bits&=~DECNEG; /* .. and clear the sign */
948+ lhs=a; /* use copy from here on */
949+ }
950+ if (decNumberIsNegative(rhs)) { /* rhs<0 */
951+ b=bufb;
952+ needbytes=sizeof(decNumber)+(D2U(rhs->digits)-1)*sizeof(Unit);
953+ if (needbytes>sizeof(bufb)) { /* need malloc space */
954+ allocbufb=(decNumber *)malloc(needbytes);
955+ if (allocbufb==NULL) { /* hopeless -- abandon */
956+ status|=DEC_Insufficient_storage;
957+ break;}
958+ b=allocbufb; /* use the allocated space */
959+ }
960+ decNumberCopy(b, rhs); /* copy content */
961+ b->bits&=~DECNEG; /* .. and clear the sign */
962+ rhs=b; /* use copy from here on */
963+ }
964+ decCompareOp(res, lhs, rhs, set, COMPTOTAL, &status);
965+ } while(0); /* end protected */
966+
967+ if (allocbufa!=NULL) free(allocbufa); /* drop any storage used */
968+ if (allocbufb!=NULL) free(allocbufb); /* .. */
969+ if (status!=0) decStatus(res, status, set);
715970 return res;
716-}
717-
718-/* ------------------------------------------------------------------ */
719-/* decNumberDivide -- divide one number by another */
720-/* */
721-/* This computes C = A / B */
722-/* */
723-/* res is C, the result. C may be A and/or B (e.g., X=X/X) */
724-/* lhs is A */
725-/* rhs is B */
726-/* set is the context */
727-/* */
728-/* C must have space for set->digits digits. */
729-/* ------------------------------------------------------------------ */
730-decNumber *
731-decNumberDivide (decNumber * res, const decNumber * lhs,
732- const decNumber * rhs, decContext * set)
733-{
734- uInt status = 0; /* accumulator */
735- decDivideOp (res, lhs, rhs, set, DIVIDE, &status);
736- if (status != 0)
737- decStatus (res, status, set);
971+ } /* decNumberCompareTotalMag */
972+
973+/* ------------------------------------------------------------------ */
974+/* decNumberDivide -- divide one number by another */
975+/* */
976+/* This computes C = A / B */
977+/* */
978+/* res is C, the result. C may be A and/or B (e.g., X=X/X) */
979+/* lhs is A */
980+/* rhs is B */
981+/* set is the context */
982+/* */
983+/* C must have space for set->digits digits. */
984+/* ------------------------------------------------------------------ */
985+decNumber * decNumberDivide(decNumber *res, const decNumber *lhs,
986+ const decNumber *rhs, decContext *set) {
987+ uInt status=0; /* accumulator */
988+ decDivideOp(res, lhs, rhs, set, DIVIDE, &status);
989+ if (status!=0) decStatus(res, status, set);
990+ #if DECCHECK
991+ decCheckInexact(res, set);
992+ #endif
738993 return res;
739-}
994+ } /* decNumberDivide */
740995
741996 /* ------------------------------------------------------------------ */
742-/* decNumberDivideInteger -- divide and return integer quotient */
743-/* */
997+/* decNumberDivideInteger -- divide and return integer quotient */
998+/* */
744999 /* This computes C = A # B, where # is the integer divide operator */
745-/* */
746-/* res is C, the result. C may be A and/or B (e.g., X=X#X) */
747-/* lhs is A */
748-/* rhs is B */
749-/* set is the context */
750-/* */
751-/* C must have space for set->digits digits. */
752-/* ------------------------------------------------------------------ */
753-decNumber *
754-decNumberDivideInteger (decNumber * res, const decNumber * lhs,
755- const decNumber * rhs, decContext * set)
756-{
757- uInt status = 0; /* accumulator */
758- decDivideOp (res, lhs, rhs, set, DIVIDEINT, &status);
759- if (status != 0)
760- decStatus (res, status, set);
1000+/* */
1001+/* res is C, the result. C may be A and/or B (e.g., X=X#X) */
1002+/* lhs is A */
1003+/* rhs is B */
1004+/* set is the context */
1005+/* */
1006+/* C must have space for set->digits digits. */
1007+/* ------------------------------------------------------------------ */
1008+decNumber * decNumberDivideInteger(decNumber *res, const decNumber *lhs,
1009+ const decNumber *rhs, decContext *set) {
1010+ uInt status=0; /* accumulator */
1011+ decDivideOp(res, lhs, rhs, set, DIVIDEINT, &status);
1012+ if (status!=0) decStatus(res, status, set);
7611013 return res;
762-}
763-
764-/* ------------------------------------------------------------------ */
765-/* decNumberMax -- compare two Numbers and return the maximum */
766-/* */
767-/* This computes C = A ? B, returning the maximum or A if equal */
768-/* */
769-/* res is C, the result. C may be A and/or B (e.g., X=X?X) */
770-/* lhs is A */
771-/* rhs is B */
772-/* set is the context */
773-/* */
774-/* C must have space for set->digits digits. */
775-/* ------------------------------------------------------------------ */
776-decNumber *
777-decNumberMax (decNumber * res, const decNumber * lhs,
778- const decNumber * rhs, decContext * set)
779-{
780- uInt status = 0; /* accumulator */
781- decCompareOp (res, lhs, rhs, set, COMPMAX, &status);
782- if (status != 0)
783- decStatus (res, status, set);
1014+ } /* decNumberDivideInteger */
1015+
1016+/* ------------------------------------------------------------------ */
1017+/* decNumberExp -- exponentiation */
1018+/* */
1019+/* This computes C = exp(A) */
1020+/* */
1021+/* res is C, the result. C may be A */
1022+/* rhs is A */
1023+/* set is the context; note that rounding mode has no effect */
1024+/* */
1025+/* C must have space for set->digits digits. */
1026+/* */
1027+/* Mathematical function restrictions apply (see above); a NaN is */
1028+/* returned with Invalid_operation if a restriction is violated. */
1029+/* */
1030+/* Finite results will always be full precision and Inexact, except */
1031+/* when A is a zero or -Infinity (giving 1 or 0 respectively). */
1032+/* */
1033+/* An Inexact result is rounded using DEC_ROUND_HALF_EVEN; it will */
1034+/* almost always be correctly rounded, but may be up to 1 ulp in */
1035+/* error in rare cases. */
1036+/* ------------------------------------------------------------------ */
1037+/* This is a wrapper for decExpOp which can handle the slightly wider */
1038+/* (double) range needed by Ln (which has to be able to calculate */
1039+/* exp(-a) where a can be the tiniest number (Ntiny). */
1040+/* ------------------------------------------------------------------ */
1041+decNumber * decNumberExp(decNumber *res, const decNumber *rhs,
1042+ decContext *set) {
1043+ uInt status=0; /* accumulator */
1044+ #if DECSUBSET
1045+ decNumber *allocrhs=NULL; /* non-NULL if rounded rhs allocated */
1046+ #endif
1047+
1048+ #if DECCHECK
1049+ if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
1050+ #endif
1051+
1052+ /* Check restrictions; these restrictions ensure that if h=8 (see */
1053+ /* decExpOp) then the result will either overflow or underflow to 0. */
1054+ /* Other math functions restrict the input range, too, for inverses. */
1055+ /* If not violated then carry out the operation. */
1056+ if (!decCheckMath(rhs, set, &status)) do { /* protect allocation */
1057+ #if DECSUBSET
1058+ if (!set->extended) {
1059+ /* reduce operand and set lostDigits status, as needed */
1060+ if (rhs->digits>set->digits) {
1061+ allocrhs=decRoundOperand(rhs, set, &status);
1062+ if (allocrhs==NULL) break;
1063+ rhs=allocrhs;
1064+ }
1065+ }
1066+ #endif
1067+ decExpOp(res, rhs, set, &status);
1068+ } while(0); /* end protected */
1069+
1070+ #if DECSUBSET
1071+ if (allocrhs !=NULL) free(allocrhs); /* drop any storage used */
1072+ #endif
1073+ /* apply significant status */
1074+ if (status!=0) decStatus(res, status, set);
1075+ #if DECCHECK
1076+ decCheckInexact(res, set);
1077+ #endif
7841078 return res;
785-}
786-
787-/* ------------------------------------------------------------------ */
788-/* decNumberMin -- compare two Numbers and return the minimum */
789-/* */
790-/* This computes C = A ? B, returning the minimum or A if equal */
791-/* */
792-/* res is C, the result. C may be A and/or B (e.g., X=X?X) */
793-/* lhs is A */
794-/* rhs is B */
795-/* set is the context */
796-/* */
797-/* C must have space for set->digits digits. */
798-/* ------------------------------------------------------------------ */
799-decNumber *
800-decNumberMin (decNumber * res, const decNumber * lhs,
801- const decNumber * rhs, decContext * set)
802-{
803- uInt status = 0; /* accumulator */
804- decCompareOp (res, lhs, rhs, set, COMPMIN, &status);
805- if (status != 0)
806- decStatus (res, status, set);
1079+ } /* decNumberExp */
1080+
1081+/* ------------------------------------------------------------------ */
1082+/* decNumberFMA -- fused multiply add */
1083+/* */
1084+/* This computes D = (A * B) + C with only one rounding */
1085+/* */
1086+/* res is D, the result. D may be A or B or C (e.g., X=FMA(X,X,X)) */
1087+/* lhs is A */
1088+/* rhs is B */
1089+/* fhs is C [far hand side] */
1090+/* set is the context */
1091+/* */
1092+/* Mathematical function restrictions apply (see above); a NaN is */
1093+/* returned with Invalid_operation if a restriction is violated. */
1094+/* */
1095+/* C must have space for set->digits digits. */
1096+/* ------------------------------------------------------------------ */
1097+decNumber * decNumberFMA(decNumber *res, const decNumber *lhs,
1098+ const decNumber *rhs, const decNumber *fhs,
1099+ decContext *set) {
1100+ uInt status=0; /* accumulator */
1101+ decContext dcmul; /* context for the multiplication */
1102+ uInt needbytes; /* for space calculations */
1103+ decNumber bufa[D2N(DECBUFFER*2+1)];
1104+ decNumber *allocbufa=NULL; /* -> allocated bufa, iff allocated */
1105+ decNumber *acc; /* accumulator pointer */
1106+ decNumber dzero; /* work */
1107+
1108+ #if DECCHECK
1109+ if (decCheckOperands(res, lhs, rhs, set)) return res;
1110+ if (decCheckOperands(res, fhs, DECUNUSED, set)) return res;
1111+ #endif
1112+
1113+ do { /* protect allocated storage */
1114+ #if DECSUBSET
1115+ if (!set->extended) { /* [undefined if subset] */
1116+ status|=DEC_Invalid_operation;
1117+ break;}
1118+ #endif
1119+ /* Check math restrictions [these ensure no overflow or underflow] */
1120+ if ((!decNumberIsSpecial(lhs) && decCheckMath(lhs, set, &status))
1121+ || (!decNumberIsSpecial(rhs) && decCheckMath(rhs, set, &status))
1122+ || (!decNumberIsSpecial(fhs) && decCheckMath(fhs, set, &status))) break;
1123+ /* set up context for multiply */
1124+ dcmul=*set;
1125+ dcmul.digits=lhs->digits+rhs->digits; /* just enough */
1126+ /* [The above may be an over-estimate for subset arithmetic, but that's OK] */
1127+ dcmul.emax=DEC_MAX_EMAX; /* effectively unbounded .. */
1128+ dcmul.emin=DEC_MIN_EMIN; /* [thanks to Math restrictions] */
1129+ /* set up decNumber space to receive the result of the multiply */
1130+ acc=bufa; /* may fit */
1131+ needbytes=sizeof(decNumber)+(D2U(dcmul.digits)-1)*sizeof(Unit);
1132+ if (needbytes>sizeof(bufa)) { /* need malloc space */
1133+ allocbufa=(decNumber *)malloc(needbytes);
1134+ if (allocbufa==NULL) { /* hopeless -- abandon */
1135+ status|=DEC_Insufficient_storage;
1136+ break;}
1137+ acc=allocbufa; /* use the allocated space */
1138+ }
1139+ /* multiply with extended range and necessary precision */
1140+ /*printf("emin=%ld\n", dcmul.emin); */
1141+ decMultiplyOp(acc, lhs, rhs, &dcmul, &status);
1142+ /* Only Invalid operation (from sNaN or Inf * 0) is possible in */
1143+ /* status; if either is seen than ignore fhs (in case it is */
1144+ /* another sNaN) and set acc to NaN unless we had an sNaN */
1145+ /* [decMultiplyOp leaves that to caller] */
1146+ /* Note sNaN has to go through addOp to shorten payload if */
1147+ /* necessary */
1148+ if ((status&DEC_Invalid_operation)!=0) {
1149+ if (!(status&DEC_sNaN)) { /* but be true invalid */
1150+ decNumberZero(res); /* acc not yet set */
1151+ res->bits=DECNAN;
1152+ break;
1153+ }
1154+ decNumberZero(&dzero); /* make 0 (any non-NaN would do) */
1155+ fhs=&dzero; /* use that */
1156+ }
1157+ #if DECCHECK
1158+ else { /* multiply was OK */
1159+ if (status!=0) printf("Status=%08lx after FMA multiply\n", status);
1160+ }
1161+ #endif
1162+ /* add the third operand and result -> res, and all is done */
1163+ decAddOp(res, acc, fhs, set, 0, &status);
1164+ } while(0); /* end protected */
1165+
1166+ if (allocbufa!=NULL) free(allocbufa); /* drop any storage used */
1167+ if (status!=0) decStatus(res, status, set);
1168+ #if DECCHECK
1169+ decCheckInexact(res, set);
1170+ #endif
8071171 return res;
808-}
809-
810-/* ------------------------------------------------------------------ */
811-/* decNumberMinus -- prefix minus operator */
812-/* */
813-/* This computes C = 0 - A */
814-/* */
815-/* res is C, the result. C may be A */
816-/* rhs is A */
817-/* set is the context */
818-/* */
819-/* C must have space for set->digits digits. */
820-/* ------------------------------------------------------------------ */
821-/* We simply use AddOp for the subtract, which will do the necessary. */
822-/* ------------------------------------------------------------------ */
823-decNumber *
824-decNumberMinus (decNumber * res, const decNumber * rhs, decContext * set)
825-{
826- decNumber dzero;
827- uInt status = 0; /* accumulator */
828-
829-#if DECCHECK
830- if (decCheckOperands (res, DECUNUSED, rhs, set))
1172+ } /* decNumberFMA */
1173+
1174+/* ------------------------------------------------------------------ */
1175+/* decNumberInvert -- invert a Number, digitwise */
1176+/* */
1177+/* This computes C = ~A */
1178+/* */
1179+/* res is C, the result. C may be A (e.g., X=~X) */
1180+/* rhs is A */
1181+/* set is the context (used for result length and error report) */
1182+/* */
1183+/* C must have space for set->digits digits. */
1184+/* */
1185+/* Logical function restrictions apply (see above); a NaN is */
1186+/* returned with Invalid_operation if a restriction is violated. */
1187+/* ------------------------------------------------------------------ */
1188+decNumber * decNumberInvert(decNumber *res, const decNumber *rhs,
1189+ decContext *set) {
1190+ const Unit *ua, *msua; /* -> operand and its msu */
1191+ Unit *uc, *msuc; /* -> result and its msu */
1192+ Int msudigs; /* digits in res msu */
1193+ #if DECCHECK
1194+ if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
1195+ #endif
1196+
1197+ if (rhs->exponent!=0 || decNumberIsSpecial(rhs) || decNumberIsNegative(rhs)) {
1198+ decStatus(res, DEC_Invalid_operation, set);
8311199 return res;
832-#endif
1200+ }
1201+ /* operand is valid */
1202+ ua=rhs->lsu; /* bottom-up */
1203+ uc=res->lsu; /* .. */
1204+ msua=ua+D2U(rhs->digits)-1; /* -> msu of rhs */
1205+ msuc=uc+D2U(set->digits)-1; /* -> msu of result */
1206+ msudigs=MSUDIGITS(set->digits); /* [faster than remainder] */
1207+ for (; uc<=msuc; ua++, uc++) { /* Unit loop */
1208+ Unit a; /* extract unit */
1209+ Int i, j; /* work */
1210+ if (ua>msua) a=0;
1211+ else a=*ua;
1212+ *uc=0; /* can now write back */
1213+ /* always need to examine all bits in rhs */
1214+ /* This loop could be unrolled and/or use BIN2BCD tables */
1215+ for (i=0; i<DECDPUN; i++) {
1216+ if ((~a)&1) *uc=*uc+(Unit)powers[i]; /* effect INVERT */
1217+ j=a%10;
1218+ a=a/10;
1219+ if (j>1) {
1220+ decStatus(res, DEC_Invalid_operation, set);
1221+ return res;
1222+ }
1223+ if (uc==msuc && i==msudigs-1) break; /* just did final digit */
1224+ } /* each digit */
1225+ } /* each unit */
1226+ /* [here uc-1 is the msu of the result] */
1227+ res->digits=decGetDigits(res->lsu, uc-res->lsu);
1228+ res->exponent=0; /* integer */
1229+ res->bits=0; /* sign=0 */
1230+ return res; /* [no status to set] */
1231+ } /* decNumberInvert */
1232+
1233+/* ------------------------------------------------------------------ */
1234+/* decNumberLn -- natural logarithm */
1235+/* */
1236+/* This computes C = ln(A) */
1237+/* */
1238+/* res is C, the result. C may be A */
1239+/* rhs is A */
1240+/* set is the context; note that rounding mode has no effect */
1241+/* */
1242+/* C must have space for set->digits digits. */
1243+/* */
1244+/* Notable cases: */
1245+/* A<0 -> Invalid */
1246+/* A=0 -> -Infinity (Exact) */
1247+/* A=+Infinity -> +Infinity (Exact) */
1248+/* A=1 exactly -> 0 (Exact) */
1249+/* */
1250+/* Mathematical function restrictions apply (see above); a NaN is */
1251+/* returned with Invalid_operation if a restriction is violated. */
1252+/* */
1253+/* An Inexact result is rounded using DEC_ROUND_HALF_EVEN; it will */
1254+/* almost always be correctly rounded, but may be up to 1 ulp in */
1255+/* error in rare cases. */
1256+/* ------------------------------------------------------------------ */
1257+/* This is a wrapper for decLnOp which can handle the slightly wider */
1258+/* (+11) range needed by Ln, Log10, etc. (which may have to be able */
1259+/* to calculate at p+e+2). */
1260+/* ------------------------------------------------------------------ */
1261+decNumber * decNumberLn(decNumber *res, const decNumber *rhs,
1262+ decContext *set) {
1263+ uInt status=0; /* accumulator */
1264+ #if DECSUBSET
1265+ decNumber *allocrhs=NULL; /* non-NULL if rounded rhs allocated */
1266+ #endif
1267+
1268+ #if DECCHECK
1269+ if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
1270+ #endif
1271+
1272+ /* Check restrictions; this is a math function; if not violated */
1273+ /* then carry out the operation. */
1274+ if (!decCheckMath(rhs, set, &status)) do { /* protect allocation */
1275+ #if DECSUBSET
1276+ if (!set->extended) {
1277+ /* reduce operand and set lostDigits status, as needed */
1278+ if (rhs->digits>set->digits) {
1279+ allocrhs=decRoundOperand(rhs, set, &status);
1280+ if (allocrhs==NULL) break;
1281+ rhs=allocrhs;
1282+ }
1283+ /* special check in subset for rhs=0 */
1284+ if (ISZERO(rhs)) { /* +/- zeros -> error */
1285+ status|=DEC_Invalid_operation;
1286+ break;}
1287+ } /* extended=0 */
1288+ #endif
1289+ decLnOp(res, rhs, set, &status);
1290+ } while(0); /* end protected */
1291+
1292+ #if DECSUBSET
1293+ if (allocrhs !=NULL) free(allocrhs); /* drop any storage used */
1294+ #endif
1295+ /* apply significant status */
1296+ if (status!=0) decStatus(res, status, set);
1297+ #if DECCHECK
1298+ decCheckInexact(res, set);
1299+ #endif
1300+ return res;
1301+ } /* decNumberLn */
1302+
1303+/* ------------------------------------------------------------------ */
1304+/* decNumberLogB - get adjusted exponent, by 754r rules */
1305+/* */
1306+/* This computes C = adjustedexponent(A) */
1307+/* */
1308+/* res is C, the result. C may be A */
1309+/* rhs is A */
1310+/* set is the context, used only for digits and status */
1311+/* */
1312+/* C must have space for 10 digits (A might have 10**9 digits and */
1313+/* an exponent of +999999999, or one digit and an exponent of */
1314+/* -1999999999). */
1315+/* */
1316+/* This returns the adjusted exponent of A after (in theory) padding */
1317+/* with zeros on the right to set->digits digits while keeping the */
1318+/* same value. The exponent is not limited by emin/emax. */
1319+/* */
1320+/* Notable cases: */
1321+/* A<0 -> Use |A| */
1322+/* A=0 -> -Infinity (Division by zero) */
1323+/* A=Infinite -> +Infinity (Exact) */
1324+/* A=1 exactly -> 0 (Exact) */
1325+/* NaNs are propagated as usual */
1326+/* ------------------------------------------------------------------ */
1327+decNumber * decNumberLogB(decNumber *res, const decNumber *rhs,
1328+ decContext *set) {
1329+ uInt status=0; /* accumulator */
1330+
1331+ #if DECCHECK
1332+ if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
1333+ #endif
1334+
1335+ /* NaNs as usual; Infinities return +Infinity; 0->oops */
1336+ if (decNumberIsNaN(rhs)) decNaNs(res, rhs, NULL, set, &status);
1337+ else if (decNumberIsInfinite(rhs)) decNumberCopyAbs(res, rhs);
1338+ else if (decNumberIsZero(rhs)) {
1339+ decNumberZero(res); /* prepare for Infinity */
1340+ res->bits=DECNEG|DECINF; /* -Infinity */
1341+ status|=DEC_Division_by_zero; /* as per 754r */
1342+ }
1343+ else { /* finite non-zero */
1344+ Int ae=rhs->exponent+rhs->digits-1; /* adjusted exponent */
1345+ decNumberFromInt32(res, ae)

Part of diff was cut off due to size limit. Use your local client to view the full diff.