ldecNumber

Check-in [3cc84d46a5]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Update to decnumber 3.41. Use dectest 2.55.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:3cc84d46a53f8dd12b570a4adbc507abb1ab3bc9
User & Date: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc 2007-08-07 22:26:53
Original Comment: Update to decnumber 3.41. Use dectest 2.55.
Context
2011-01-16
03:41
Add a wiki-format version of the library documentation. check-in: ac0742b219 user: e tags: trunk
2007-08-07
22:26
Update to decnumber 3.41. Use dectest 2.55. check-in: 3cc84d46a5 user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
2007-06-18
21:32
Make context table weak on keys so threads are GC'd if cache off. Cache is off by default. Add test for thread memory leak. check-in: 55d4fc73d1 user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to decNumber/decContext.c.

17
18
19
20
21
22
23

24
25







26
27
28
29
30
31
32
..
87
88
89
90
91
92
93










94
95
96
97
98
99
100
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */
/* This module comprises the routines for handling arithmetic         */
/* context structures.                                                */
/* ------------------------------------------------------------------ */

#include <string.h>           // for strcmp

#include "decContext.h"       // context and base types
#include "decNumberLocal.h"   // decNumber local types, etc.








/* ------------------------------------------------------------------ */
/* decContextDefault -- initialize a context structure                */
/*                                                                    */
/*  context is the structure to be initialized                        */
/*  kind selects the required set of default values, one of:          */
/*      DEC_INIT_BASE       -- select ANSI X3-274 defaults            */
................................................................................
      #endif
      break;

    default:                                 // invalid Kind
      // use defaults, and ..
      decContextSetStatus(context, DEC_Invalid_operation); // trap
    }










  return context;} // decContextDefault

/* ------------------------------------------------------------------ */
/* decContextStatusToString -- convert status flags to a string       */
/*                                                                    */
/*  context is a context with valid status field                      */
/*                                                                    */







>


>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
..
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */
/* This module comprises the routines for handling arithmetic         */
/* context structures.                                                */
/* ------------------------------------------------------------------ */

#include <string.h>           // for strcmp
#include <stdio.h>            // for printf if DECCHECK
#include "decContext.h"       // context and base types
#include "decNumberLocal.h"   // decNumber local types, etc.

#if DECCHECK
/* compile-time endian tester [assumes sizeof(Int)>1] */
static  const  Int mfcone=1;                 // constant 1
static  const  Flag *mfctop=(Flag *)&mfcone; // -> top byte
#define LITEND *mfctop             // named flag; 1=little-endian
#endif

/* ------------------------------------------------------------------ */
/* decContextDefault -- initialize a context structure                */
/*                                                                    */
/*  context is the structure to be initialized                        */
/*  kind selects the required set of default values, one of:          */
/*      DEC_INIT_BASE       -- select ANSI X3-274 defaults            */
................................................................................
      #endif
      break;

    default:                                 // invalid Kind
      // use defaults, and ..
      decContextSetStatus(context, DEC_Invalid_operation); // trap
    }

  #if DECCHECK
  if (LITEND!=DECLITEND) {
    char *adj;
    if (LITEND) adj="little";
           else adj="big";
    printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n",
           DECLITEND, adj);
    }
  #endif
  return context;} // decContextDefault

/* ------------------------------------------------------------------ */
/* decContextStatusToString -- convert status flags to a string       */
/*                                                                    */
/*  context is a context with valid status field                      */
/*                                                                    */

Changes to decNumber/decContext.h.

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
..
33
34
35
36
37
38
39
40
41
42
43



44
45
46
47
48
49
50
51
52
53
54
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98



















99
100
101
102
103
104
105
106
107
108
109
...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*                                                                    */
/* Please send comments, suggestions, and corrections to the author:  */
/*   mfc@uk.ibm.com                                                   */
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */
/*                                                                    */
/* Context must always be set correctly:                              */
/*                                                                    */
/*  digits   -- must be in the range 1 through 999999999              */
/*  emax     -- must be in the range 0 through 999999999              */
/*  emin     -- must be in the range 0 through -999999999             */
/*  round    -- must be one of the enumerated rounding modes          */
/*  traps    -- only defined bits may be set                          */
/*  status   -- [any bits may be cleared, but not set, by user]       */
................................................................................
#if !defined(DECCONTEXT)
  #define DECCONTEXT
  #define DECCNAME     "decContext"                     /* Short name */
  #define DECCFULLNAME "Decimal Context Descriptor"   /* Verbose name */
  #define DECCAUTHOR   "Mike Cowlishaw"               /* Who to blame */

  #if !defined(int32_t)
    #include <stdint.h>            // C99 standard integers
  #endif
  #include <signal.h>              // for traps




  /* Conditional code flag -- set this to 0 for best performance */
  #define DECSUBSET 0              // 1=enable subset arithmetic

  /* Context for operations, with associated constants */
  enum rounding {
    DEC_ROUND_CEILING,             // round towards +infinity
    DEC_ROUND_UP,                  // round away from 0
    DEC_ROUND_HALF_UP,             // 0.5 rounds up
    DEC_ROUND_HALF_EVEN,           // 0.5 rounds to nearest even
    DEC_ROUND_HALF_DOWN,           // 0.5 rounds down
    DEC_ROUND_DOWN,                // round towards 0 (truncate)
    DEC_ROUND_FLOOR,               // round towards -infinity

    DEC_ROUND_MAX                  // enum must be less than this
    };

  typedef struct {
    int32_t  digits;               // working precision
    int32_t  emax;                 // maximum positive exponent
    int32_t  emin;                 // minimum negative exponent
    enum     rounding round;       // rounding mode
    uint32_t traps;                // trap-enabler flags
    uint32_t status;               // status flags
    uint8_t  clamp;                // flag: apply IEEE exponent clamp
    #if DECSUBSET
    uint8_t  extended;             // flag: special-values allowed
    #endif
    } decContext;

  /* Maxima and Minima */
  #define DEC_MAX_DIGITS 999999999
  #define DEC_MIN_DIGITS         1
  #define DEC_MAX_EMAX   999999999
  #define DEC_MIN_EMAX           0
  #define DEC_MAX_EMIN           0
  #define DEC_MIN_EMIN  -999999999
  #define DEC_MAX_MATH      999999 // max emax, etc., for math functions

  /* Trap-enabler and Status flags (exceptional conditions), and their names */
  // Top byte is reserved for internal use


  #define DEC_Conversion_syntax    0x00000001
  #define DEC_Division_by_zero     0x00000002
  #define DEC_Division_impossible  0x00000004
  #define DEC_Division_undefined   0x00000008
  #define DEC_Insufficient_storage 0x00000010  // [used if malloc fails]
  #define DEC_Inexact              0x00000020
  #define DEC_Invalid_context      0x00000040
  #define DEC_Invalid_operation    0x00000080
  #if DECSUBSET
  #define DEC_Lost_digits          0x00000100
  #endif
  #define DEC_Overflow             0x00000200
  #define DEC_Clamped              0x00000400
  #define DEC_Rounded              0x00000800
  #define DEC_Subnormal            0x00001000
  #define DEC_Underflow            0x00002000




















  /* IEEE 854 groupings for the flags */
  // [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal are
  // not in IEEE 854]
  #define DEC_IEEE_854_Division_by_zero  (DEC_Division_by_zero)
  #if DECSUBSET
  #define DEC_IEEE_854_Inexact           (DEC_Inexact | DEC_Lost_digits)
  #else
  #define DEC_IEEE_854_Inexact           (DEC_Inexact)
  #endif
  #define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax |     \
................................................................................
                                          DEC_Division_undefined |    \
                                          DEC_Insufficient_storage |  \
                                          DEC_Invalid_context |       \
                                          DEC_Invalid_operation)
  #define DEC_IEEE_854_Overflow          (DEC_Overflow)
  #define DEC_IEEE_854_Underflow         (DEC_Underflow)

  // flags which are normally errors (results are qNaN, infinite, or 0)
  #define DEC_Errors (DEC_IEEE_854_Division_by_zero |                 \
                      DEC_IEEE_854_Invalid_operation |                \
                      DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
  // flags which cause a result to become qNaN
  #define DEC_NaNs    DEC_IEEE_854_Invalid_operation

  // flags which are normally for information only (have finite results)
  #if DECSUBSET
  #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact     \
                          | DEC_Lost_digits)
  #else
  #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
  #endif

  // name strings for the exceptional conditions

  #define DEC_Condition_CS "Conversion syntax"
  #define DEC_Condition_DZ "Division by zero"
  #define DEC_Condition_DI "Division impossible"
  #define DEC_Condition_DU "Division undefined"
  #define DEC_Condition_IE "Inexact"
  #define DEC_Condition_IS "Insufficient storage"
................................................................................
  #define DEC_Condition_OV "Overflow"
  #define DEC_Condition_PA "Clamped"
  #define DEC_Condition_RO "Rounded"
  #define DEC_Condition_SU "Subnormal"
  #define DEC_Condition_UN "Underflow"
  #define DEC_Condition_ZE "No status"
  #define DEC_Condition_MU "Multiple status"
  #define DEC_Condition_Length 21  // length of the longest string,
                                   // including terminator

  /* Initialization descriptors, used by decContextDefault */
  #define DEC_INIT_BASE         0
  #define DEC_INIT_DECIMAL32   32
  #define DEC_INIT_DECIMAL64   64
  #define DEC_INIT_DECIMAL128 128

  /* decContext routines */
  decContext * decContextDefault(decContext *, int32_t);
  decContext * decContextSetStatus(decContext *, uint32_t);
  const char * decContextStatusToString(const decContext *);
  decContext * decContextSetStatusFromString(decContext *, const char *);

#endif







|







 







|

|

>
>
>
|
|

|

|
|
|
|
|
|
|
>
|



|
|
|
|
|
|
|

|



|






|

|
|
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
|
|







 







|



|


|

|





|







 







|
|

|





|






13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
..
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*                                                                    */
/* Please send comments, suggestions, and corrections to the author:  */
/*   mfc@uk.ibm.com                                                   */
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */
/*                                                                    */
/* Context variables must always have valid values:                   */
/*                                                                    */
/*  digits   -- must be in the range 1 through 999999999              */
/*  emax     -- must be in the range 0 through 999999999              */
/*  emin     -- must be in the range 0 through -999999999             */
/*  round    -- must be one of the enumerated rounding modes          */
/*  traps    -- only defined bits may be set                          */
/*  status   -- [any bits may be cleared, but not set, by user]       */
................................................................................
#if !defined(DECCONTEXT)
  #define DECCONTEXT
  #define DECCNAME     "decContext"                     /* Short name */
  #define DECCFULLNAME "Decimal Context Descriptor"   /* Verbose name */
  #define DECCAUTHOR   "Mike Cowlishaw"               /* Who to blame */

  #if !defined(int32_t)
    #include <stdint.h>            /* C99 standard integers           */
  #endif
  #include <signal.h>              /* for traps                       */

  /* Extended flags setting -- set this to 0 to use only IEEE flags   */
  #define DECEXTFLAG 1             /* 1=enable extended flags         */

  /* Conditional code flag -- set this to 0 for best performance      */
  #define DECSUBSET  0             /* 1=enable subset arithmetic      */

  /* Context for operations, with associated constants                */
  enum rounding {
    DEC_ROUND_CEILING,             /* round towards +infinity         */
    DEC_ROUND_UP,                  /* round away from 0               */
    DEC_ROUND_HALF_UP,             /* 0.5 rounds up                   */
    DEC_ROUND_HALF_EVEN,           /* 0.5 rounds to nearest even      */
    DEC_ROUND_HALF_DOWN,           /* 0.5 rounds down                 */
    DEC_ROUND_DOWN,                /* round towards 0 (truncate)      */
    DEC_ROUND_FLOOR,               /* round towards -infinity         */
    DEC_ROUND_05UP,                /* round for reround               */
    DEC_ROUND_MAX                  /* enum must be less than this     */
    };

  typedef struct {
    int32_t  digits;               /* working precision               */
    int32_t  emax;                 /* maximum positive exponent       */
    int32_t  emin;                 /* minimum negative exponent       */
    enum     rounding round;       /* rounding mode                   */
    uint32_t traps;                /* trap-enabler flags              */
    uint32_t status;               /* status flags                    */
    uint8_t  clamp;                /* flag: apply IEEE exponent clamp */
    #if DECSUBSET
    uint8_t  extended;             /* flag: special-values allowed    */
    #endif
    } decContext;

  /* Maxima and Minima                                                */
  #define DEC_MAX_DIGITS 999999999
  #define DEC_MIN_DIGITS         1
  #define DEC_MAX_EMAX   999999999
  #define DEC_MIN_EMAX           0
  #define DEC_MAX_EMIN           0
  #define DEC_MIN_EMIN  -999999999
  #define DEC_MAX_MATH      999999 /* max emax, etc., for math funcs. */

  /* Trap-enabler and Status flags (exceptional conditions), and      */
  /* their names.  The top byte is reserved for internal use          */
  #if DECEXTFLAG
    /* Extended flags */
    #define DEC_Conversion_syntax    0x00000001
    #define DEC_Division_by_zero     0x00000002
    #define DEC_Division_impossible  0x00000004
    #define DEC_Division_undefined   0x00000008
    #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]  */
    #define DEC_Inexact              0x00000020
    #define DEC_Invalid_context      0x00000040
    #define DEC_Invalid_operation    0x00000080
    #if DECSUBSET
    #define DEC_Lost_digits          0x00000100
    #endif
    #define DEC_Overflow             0x00000200
    #define DEC_Clamped              0x00000400
    #define DEC_Rounded              0x00000800
    #define DEC_Subnormal            0x00001000
    #define DEC_Underflow            0x00002000
  #else
    /* IEEE flags only */
    #define DEC_Conversion_syntax    0x00000010
    #define DEC_Division_by_zero     0x00000002
    #define DEC_Division_impossible  0x00000010
    #define DEC_Division_undefined   0x00000010
    #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]  */
    #define DEC_Inexact              0x00000001
    #define DEC_Invalid_context      0x00000010
    #define DEC_Invalid_operation    0x00000010
    #if DECSUBSET
    #define DEC_Lost_digits          0x00000000
    #endif
    #define DEC_Overflow             0x00000008
    #define DEC_Clamped              0x00000000
    #define DEC_Rounded              0x00000000
    #define DEC_Subnormal            0x00000000
    #define DEC_Underflow            0x00000004
  #endif

  /* IEEE 854 groupings for the flags                                 */
  /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal    */
  /* are not in IEEE 854]                                             */
  #define DEC_IEEE_854_Division_by_zero  (DEC_Division_by_zero)
  #if DECSUBSET
  #define DEC_IEEE_854_Inexact           (DEC_Inexact | DEC_Lost_digits)
  #else
  #define DEC_IEEE_854_Inexact           (DEC_Inexact)
  #endif
  #define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax |     \
................................................................................
                                          DEC_Division_undefined |    \
                                          DEC_Insufficient_storage |  \
                                          DEC_Invalid_context |       \
                                          DEC_Invalid_operation)
  #define DEC_IEEE_854_Overflow          (DEC_Overflow)
  #define DEC_IEEE_854_Underflow         (DEC_Underflow)

  /* flags which are normally errors (result is qNaN, infinite, or 0) */
  #define DEC_Errors (DEC_IEEE_854_Division_by_zero |                 \
                      DEC_IEEE_854_Invalid_operation |                \
                      DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
  /* flags which cause a result to become qNaN                        */
  #define DEC_NaNs    DEC_IEEE_854_Invalid_operation

  /* flags which are normally for information only (finite results)   */
  #if DECSUBSET
  #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact    \
                          | DEC_Lost_digits)
  #else
  #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
  #endif

  /* name strings for the exceptional conditions                      */

  #define DEC_Condition_CS "Conversion syntax"
  #define DEC_Condition_DZ "Division by zero"
  #define DEC_Condition_DI "Division impossible"
  #define DEC_Condition_DU "Division undefined"
  #define DEC_Condition_IE "Inexact"
  #define DEC_Condition_IS "Insufficient storage"
................................................................................
  #define DEC_Condition_OV "Overflow"
  #define DEC_Condition_PA "Clamped"
  #define DEC_Condition_RO "Rounded"
  #define DEC_Condition_SU "Subnormal"
  #define DEC_Condition_UN "Underflow"
  #define DEC_Condition_ZE "No status"
  #define DEC_Condition_MU "Multiple status"
  #define DEC_Condition_Length 21  /* length of the longest string,   */
                                   /* including terminator            */

  /* Initialization descriptors, used by decContextDefault            */
  #define DEC_INIT_BASE         0
  #define DEC_INIT_DECIMAL32   32
  #define DEC_INIT_DECIMAL64   64
  #define DEC_INIT_DECIMAL128 128

  /* decContext routines                                              */
  decContext * decContextDefault(decContext *, int32_t);
  decContext * decContextSetStatus(decContext *, uint32_t);
  const char * decContextStatusToString(const decContext *);
  decContext * decContextSetStatusFromString(decContext *, const char *);

#endif

Changes to decNumber/decDPD.h.

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15


16
17
18
19
20
21
22

23
24
25
26

27
28
29
30
31
32
33
..
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
..
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
..
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
...
469
470
471
472
473
474
475




































































































































































































































































476
477
478
479
480
481
482
...
795
796
797
798
799
800
801
802






















































































































/* ------------------------------------------------------------------------ */
/* Binary Coded Decimal <--> Densely Packed Decimal lookup tables           */
/* [Automatically generated -- do not edit.  2006.11.09]                    */
/* ------------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2006. All rights reserved.          */
/* ------------------------------------------------------------------------ */
/* For details, see: http://www2.hursley.ibm.com/decimal/DPDecimal.html     */
/*                                                                          */
/* This include file defines conversion tables for DPD, as follows.         */
/*                                                                          */
/*   uint16_t BCD2DPD[2458];     // BCD -> DPD (0x999 => 2457)              */
/*   uint16_t BIN2DPD[1000];     // BIN -> DPD (999 => 2457)                */
/*   uint8_t  BIN2CHAR[4001];    // Bin -> CHAR (999 => '\3' '9' '9' '9')   */

/*   uint16_t DPD2BCD[1024];     // DPD -> BCD (0x3FF => 0x999)             */
/*   uint16_t DPD2BIN[1024];     // DPD -> BIN (0x3FF => 999)               */


/*   uint8_t  DPD2BCD8[4096];    // DPD -> bytes (x3FF => 9 9 9 3)          */
/*                                                                          */
/* In all cases the result (10 bits or 12 bits, or binary) is right-aligned */
/* in the table entry.  BIN2CHAR entries are a single byte length (0 for    */
/* value 0) followed by three digit characters; a trailing terminator is    */
/* included to allow 4-char moves always.  DPD2BCD8 entries are similar     */
/* with the three BCD8 digits followed by a one-byte length.                */

/*                                                                          */
/* To use a table, its name, prefixed with DEC_, must be defined with a     */
/* value of 1 before this header file is included.  For example:            */
/*    #define DEC_BCD2DPD 1                                                 */

/* ------------------------------------------------------------------------ */
 
#if DEC_BCD2DPD==1 && !defined(DECBCD2DPD)
#define DECBCD2DPD
 
const uint16_t BCD2DPD[2458]={    0,    1,    2,    3,    4,    5,    6,    7, 
    8,    9,    0,    0,    0,    0,    0,    0,   16,   17,   18,   19,   20, 
................................................................................
    0,    0,    0,   10,   11,   42,   43,   74,   75,  106,  107,   78,   79, 
    0,    0,    0,    0,    0,    0,   26,   27,   58,   59,   90,   91,  122, 
  123,   94,   95,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,   10,   11,   42,   43,   74, 
   75,  106,  107,   78,   79,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,  128,  129,  130,  131,  132,  133,  134,  135,  136,  137,    0,    0, 
    0,    0,    0,    0,  144,  145,  146,  147,  148,  149,  150,  151,  152, 
  153,    0,    0,    0,    0,    0,    0,  160,  161,  162,  163,  164,  165, 
  166,  167,  168,  169,    0,    0,    0,    0,    0,    0,  176,  177,  178, 
  179,  180,  181,  182,  183,  184,  185,    0,    0,    0,    0,    0,    0, 
  192,  193,  194,  195,  196,  197,  198,  199,  200,  201,    0,    0,    0, 
................................................................................
  139,  170,  171,  202,  203,  234,  235,  206,  207,    0,    0,    0,    0, 
    0,    0,  154,  155,  186,  187,  218,  219,  250,  251,  222,  223,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,  138,  139,  170,  171,  202,  203,  234,  235,  206, 
  207,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,  256,  257,  258, 
  259,  260,  261,  262,  263,  264,  265,    0,    0,    0,    0,    0,    0, 
  272,  273,  274,  275,  276,  277,  278,  279,  280,  281,    0,    0,    0, 
    0,    0,    0,  288,  289,  290,  291,  292,  293,  294,  295,  296,  297, 
    0,    0,    0,    0,    0,    0,  304,  305,  306,  307,  308,  309,  310, 
  311,  312,  313,    0,    0,    0,    0,    0,    0,  320,  321,  322,  323, 
  324,  325,  326,  327,  328,  329,    0,    0,    0,    0,    0,    0,  336, 
................................................................................
  331,  362,  363,  334,  335,    0,    0,    0,    0,    0,    0,  282,  283, 
  314,  315,  346,  347,  378,  379,  350,  351,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
  266,  267,  298,  299,  330,  331,  362,  363,  334,  335,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,  384,  385,  386,  387,  388,  389,  390, 
  391,  392,  393,    0,    0,    0,    0,    0,    0,  400,  401,  402,  403, 
  404,  405,  406,  407,  408,  409,    0,    0,    0,    0,    0,    0,  416, 
  417,  418,  419,  420,  421,  422,  423,  424,  425,    0,    0,    0,    0, 
    0,    0,  432,  433,  434,  435,  436,  437,  438,  439,  440,  441,    0, 
    0,    0,    0,    0,    0,  448,  449,  450,  451,  452,  453,  454,  455, 
................................................................................
    0,    0,    0,    0,  394,  395,  426,  427,  458,  459,  490,  491,  462, 
  463,    0,    0,    0,    0,    0,    0,  410,  411,  442,  443,  474,  475, 
  506,  507,  478,  479,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,  394,  395,  426,  427, 
  458,  459,  490,  491,  462,  463,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,  512,  513,  514,  515,  516,  517,  518,  519,  520,  521,    0, 
    0,    0,    0,    0,    0,  528,  529,  530,  531,  532,  533,  534,  535, 
  536,  537,    0,    0,    0,    0,    0,    0,  544,  545,  546,  547,  548, 
  549,  550,  551,  552,  553,    0,    0,    0,    0,    0,    0,  560,  561, 
  562,  563,  564,  565,  566,  567,  568,  569,    0,    0,    0,    0,    0, 
    0,  576,  577,  578,  579,  580,  581,  582,  583,  584,  585,    0,    0, 
................................................................................
  522,  523,  554,  555,  586,  587,  618,  619,  590,  591,    0,    0,    0, 
    0,    0,    0,  538,  539,  570,  571,  602,  603,  634,  635,  606,  607, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,  522,  523,  554,  555,  586,  587,  618,  619, 
  590,  591,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,  640,  641, 
  642,  643,  644,  645,  646,  647,  648,  649,    0,    0,    0,    0,    0, 
    0,  656,  657,  658,  659,  660,  661,  662,  663,  664,  665,    0,    0, 
    0,    0,    0,    0,  672,  673,  674,  675,  676,  677,  678,  679,  680, 
  681,    0,    0,    0,    0,    0,    0,  688,  689,  690,  691,  692,  693, 
  694,  695,  696,  697,    0,    0,    0,    0,    0,    0,  704,  705,  706, 
  707,  708,  709,  710,  711,  712,  713,    0,    0,    0,    0,    0,    0, 
................................................................................
  714,  715,  746,  747,  718,  719,    0,    0,    0,    0,    0,    0,  666, 
  667,  698,  699,  730,  731,  762,  763,  734,  735,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,  650,  651,  682,  683,  714,  715,  746,  747,  718,  719,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,  768,  769,  770,  771,  772,  773, 
  774,  775,  776,  777,    0,    0,    0,    0,    0,    0,  784,  785,  786, 
  787,  788,  789,  790,  791,  792,  793,    0,    0,    0,    0,    0,    0, 
  800,  801,  802,  803,  804,  805,  806,  807,  808,  809,    0,    0,    0, 
    0,    0,    0,  816,  817,  818,  819,  820,  821,  822,  823,  824,  825, 
    0,    0,    0,    0,    0,    0,  832,  833,  834,  835,  836,  837,  838, 
................................................................................
    0,    0,    0,    0,    0,  778,  779,  810,  811,  842,  843,  874,  875, 
  846,  847,    0,    0,    0,    0,    0,    0,  794,  795,  826,  827,  858, 
  859,  890,  891,  862,  863,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,  778,  779,  810, 
  811,  842,  843,  874,  875,  846,  847,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,  896,  897,  898,  899,  900,  901,  902,  903,  904,  905, 
    0,    0,    0,    0,    0,    0,  912,  913,  914,  915,  916,  917,  918, 
  919,  920,  921,    0,    0,    0,    0,    0,    0,  928,  929,  930,  931, 
  932,  933,  934,  935,  936,  937,    0,    0,    0,    0,    0,    0,  944, 
  945,  946,  947,  948,  949,  950,  951,  952,  953,    0,    0,    0,    0, 
    0,    0,  960,  961,  962,  963,  964,  965,  966,  967,  968,  969,    0, 
................................................................................
    0,  906,  907,  938,  939,  970,  971, 1002, 1003,  974,  975,    0,    0, 
    0,    0,    0,    0,  922,  923,  954,  955,  986,  987, 1018, 1019,  990, 
  991,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,  906,  907,  938,  939,  970,  971, 1002, 
 1003,  974,  975,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,   12, 
   13,  268,  269,  524,  525,  780,  781,   46,   47,    0,    0,    0,    0, 
    0,    0,   28,   29,  284,  285,  540,  541,  796,  797,   62,   63,    0, 
    0,    0,    0,    0,    0,   44,   45,  300,  301,  556,  557,  812,  813, 
  302,  303,    0,    0,    0,    0,    0,    0,   60,   61,  316,  317,  572, 
  573,  828,  829,  318,  319,    0,    0,    0,    0,    0,    0,   76,   77, 
  332,  333,  588,  589,  844,  845,  558,  559,    0,    0,    0,    0,    0, 
................................................................................
  271,  526,  527,  782,  783,  110,  111,    0,    0,    0,    0,    0,    0, 
   30,   31,  286,  287,  542,  543,  798,  799,  126,  127,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,   14,   15,  270,  271,  526,  527,  782,  783,  110,  111,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,  140,  141,  396,  397,  652, 
  653,  908,  909,  174,  175,    0,    0,    0,    0,    0,    0,  156,  157, 
  412,  413,  668,  669,  924,  925,  190,  191,    0,    0,    0,    0,    0, 
    0,  172,  173,  428,  429,  684,  685,  940,  941,  430,  431,    0,    0, 
    0,    0,    0,    0,  188,  189,  444,  445,  700,  701,  956,  957,  446, 
  447,    0,    0,    0,    0,    0,    0,  204,  205,  460,  461,  716,  717, 
................................................................................
  937,  978,  979,  740,  741,  742,  743,  744,  745,  746,  747,  748,  749, 
  784,  785,  946,  947,  788,  789,  750,  751,  752,  753,  754,  755,  756, 
  757,  758,  759,  794,  795,  956,  957,  798,  799,  760,  761,  762,  763, 
  764,  765,  766,  767,  768,  769,  786,  787,  966,  967,  988,  989,  770, 
  771,  772,  773,  774,  775,  776,  777,  778,  779,  796,  797,  976,  977, 
  998,  999};
#endif




































































































































































































































































 
#if DEC_BIN2CHAR==1 && !defined(DECBIN2CHAR)
#define DECBIN2CHAR
 
const uint8_t BIN2CHAR[4001]={
 '\0','0','0','0', '\1','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','0','0','9', 
................................................................................
 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, 
 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, 
 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, 
 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, 
 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, 
 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};
#endif
 























































































































|
|

|



|

|
|
|
>
|
|
>
>
|




|
|
>




>







 







|
|







 







|
|







 







|







 







|
|







 







|
|







 







|







 







|
|







 







|
|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
..
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
..
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
..
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
...
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
....
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
/* ------------------------------------------------------------------------ */
/* Binary Coded Decimal and Densely Packed Decimal conversion lookup tables */
/* [Automatically generated -- do not edit.  2007.04.16]                    */
/* ------------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2007. All rights reserved.          */
/* ------------------------------------------------------------------------ */
/* For details, see: http://www2.hursley.ibm.com/decimal/DPDecimal.html     */
/*                                                                          */
/* This include file defines several DPD and BCD conversion tables:         */
/*                                                                          */
/*   uint16_t BCD2DPD[2458];     -- BCD -> DPD (0x999 => 2457)              */
/*   uint16_t BIN2DPD[1000];     -- Bin -> DPD (999 => 2457)                */
/*   uint8_t  BIN2CHAR[4001];    -- Bin -> CHAR (999 => '\3' '9' '9' '9')   */
/*   uint8_t  BIN2BCD8[4000];    -- Bin -> bytes (999 => 9 9 9 3)           */
/*   uint16_t DPD2BCD[1024];     -- DPD -> BCD (0x3FF => 0x999)             */
/*   uint16_t DPD2BIN[1024];     -- DPD -> BIN (0x3FF => 999)               */
/*   uint32_t DPD2BINK[1024];    -- DPD -> BIN * 1000 (0x3FF => 999000)     */
/*   uint32_t DPD2BINM[1024];    -- DPD -> BIN * 1E+6 (0x3FF => 999000000)  */
/*   uint8_t  DPD2BCD8[4096];    -- DPD -> bytes (x3FF => 9 9 9 3)          */
/*                                                                          */
/* In all cases the result (10 bits or 12 bits, or binary) is right-aligned */
/* in the table entry.  BIN2CHAR entries are a single byte length (0 for    */
/* value 0) followed by three digit characters; a trailing terminator is    */
/* included to allow 4-char moves always.  BIN2BCD8 and DPD2BCD8 entries    */
/* are similar with the three BCD8 digits followed by a one-byte length     */
/* (again, length=0 for value 0).                                           */
/*                                                                          */
/* To use a table, its name, prefixed with DEC_, must be defined with a     */
/* value of 1 before this header file is included.  For example:            */
/*    #define DEC_BCD2DPD 1                                                 */
/* This mechanism allows software to only include tables that are needed.   */
/* ------------------------------------------------------------------------ */
 
#if DEC_BCD2DPD==1 && !defined(DECBCD2DPD)
#define DECBCD2DPD
 
const uint16_t BCD2DPD[2458]={    0,    1,    2,    3,    4,    5,    6,    7, 
    8,    9,    0,    0,    0,    0,    0,    0,   16,   17,   18,   19,   20, 
................................................................................
    0,    0,    0,   10,   11,   42,   43,   74,   75,  106,  107,   78,   79, 
    0,    0,    0,    0,    0,    0,   26,   27,   58,   59,   90,   91,  122, 
  123,   94,   95,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,  128,  129,  130,  131,  132,  133,  134,  135,  136,  137,    0,    0, 
    0,    0,    0,    0,  144,  145,  146,  147,  148,  149,  150,  151,  152, 
  153,    0,    0,    0,    0,    0,    0,  160,  161,  162,  163,  164,  165, 
  166,  167,  168,  169,    0,    0,    0,    0,    0,    0,  176,  177,  178, 
  179,  180,  181,  182,  183,  184,  185,    0,    0,    0,    0,    0,    0, 
  192,  193,  194,  195,  196,  197,  198,  199,  200,  201,    0,    0,    0, 
................................................................................
  139,  170,  171,  202,  203,  234,  235,  206,  207,    0,    0,    0,    0, 
    0,    0,  154,  155,  186,  187,  218,  219,  250,  251,  222,  223,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,  256,  257,  258, 
  259,  260,  261,  262,  263,  264,  265,    0,    0,    0,    0,    0,    0, 
  272,  273,  274,  275,  276,  277,  278,  279,  280,  281,    0,    0,    0, 
    0,    0,    0,  288,  289,  290,  291,  292,  293,  294,  295,  296,  297, 
    0,    0,    0,    0,    0,    0,  304,  305,  306,  307,  308,  309,  310, 
  311,  312,  313,    0,    0,    0,    0,    0,    0,  320,  321,  322,  323, 
  324,  325,  326,  327,  328,  329,    0,    0,    0,    0,    0,    0,  336, 
................................................................................
  331,  362,  363,  334,  335,    0,    0,    0,    0,    0,    0,  282,  283, 
  314,  315,  346,  347,  378,  379,  350,  351,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,  384,  385,  386,  387,  388,  389,  390, 
  391,  392,  393,    0,    0,    0,    0,    0,    0,  400,  401,  402,  403, 
  404,  405,  406,  407,  408,  409,    0,    0,    0,    0,    0,    0,  416, 
  417,  418,  419,  420,  421,  422,  423,  424,  425,    0,    0,    0,    0, 
    0,    0,  432,  433,  434,  435,  436,  437,  438,  439,  440,  441,    0, 
    0,    0,    0,    0,    0,  448,  449,  450,  451,  452,  453,  454,  455, 
................................................................................
    0,    0,    0,    0,  394,  395,  426,  427,  458,  459,  490,  491,  462, 
  463,    0,    0,    0,    0,    0,    0,  410,  411,  442,  443,  474,  475, 
  506,  507,  478,  479,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,  512,  513,  514,  515,  516,  517,  518,  519,  520,  521,    0, 
    0,    0,    0,    0,    0,  528,  529,  530,  531,  532,  533,  534,  535, 
  536,  537,    0,    0,    0,    0,    0,    0,  544,  545,  546,  547,  548, 
  549,  550,  551,  552,  553,    0,    0,    0,    0,    0,    0,  560,  561, 
  562,  563,  564,  565,  566,  567,  568,  569,    0,    0,    0,    0,    0, 
    0,  576,  577,  578,  579,  580,  581,  582,  583,  584,  585,    0,    0, 
................................................................................
  522,  523,  554,  555,  586,  587,  618,  619,  590,  591,    0,    0,    0, 
    0,    0,    0,  538,  539,  570,  571,  602,  603,  634,  635,  606,  607, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,  640,  641, 
  642,  643,  644,  645,  646,  647,  648,  649,    0,    0,    0,    0,    0, 
    0,  656,  657,  658,  659,  660,  661,  662,  663,  664,  665,    0,    0, 
    0,    0,    0,    0,  672,  673,  674,  675,  676,  677,  678,  679,  680, 
  681,    0,    0,    0,    0,    0,    0,  688,  689,  690,  691,  692,  693, 
  694,  695,  696,  697,    0,    0,    0,    0,    0,    0,  704,  705,  706, 
  707,  708,  709,  710,  711,  712,  713,    0,    0,    0,    0,    0,    0, 
................................................................................
  714,  715,  746,  747,  718,  719,    0,    0,    0,    0,    0,    0,  666, 
  667,  698,  699,  730,  731,  762,  763,  734,  735,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,  768,  769,  770,  771,  772,  773, 
  774,  775,  776,  777,    0,    0,    0,    0,    0,    0,  784,  785,  786, 
  787,  788,  789,  790,  791,  792,  793,    0,    0,    0,    0,    0,    0, 
  800,  801,  802,  803,  804,  805,  806,  807,  808,  809,    0,    0,    0, 
    0,    0,    0,  816,  817,  818,  819,  820,  821,  822,  823,  824,  825, 
    0,    0,    0,    0,    0,    0,  832,  833,  834,  835,  836,  837,  838, 
................................................................................
    0,    0,    0,    0,    0,  778,  779,  810,  811,  842,  843,  874,  875, 
  846,  847,    0,    0,    0,    0,    0,    0,  794,  795,  826,  827,  858, 
  859,  890,  891,  862,  863,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,  896,  897,  898,  899,  900,  901,  902,  903,  904,  905, 
    0,    0,    0,    0,    0,    0,  912,  913,  914,  915,  916,  917,  918, 
  919,  920,  921,    0,    0,    0,    0,    0,    0,  928,  929,  930,  931, 
  932,  933,  934,  935,  936,  937,    0,    0,    0,    0,    0,    0,  944, 
  945,  946,  947,  948,  949,  950,  951,  952,  953,    0,    0,    0,    0, 
    0,    0,  960,  961,  962,  963,  964,  965,  966,  967,  968,  969,    0, 
................................................................................
    0,  906,  907,  938,  939,  970,  971, 1002, 1003,  974,  975,    0,    0, 
    0,    0,    0,    0,  922,  923,  954,  955,  986,  987, 1018, 1019,  990, 
  991,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,   12, 
   13,  268,  269,  524,  525,  780,  781,   46,   47,    0,    0,    0,    0, 
    0,    0,   28,   29,  284,  285,  540,  541,  796,  797,   62,   63,    0, 
    0,    0,    0,    0,    0,   44,   45,  300,  301,  556,  557,  812,  813, 
  302,  303,    0,    0,    0,    0,    0,    0,   60,   61,  316,  317,  572, 
  573,  828,  829,  318,  319,    0,    0,    0,    0,    0,    0,   76,   77, 
  332,  333,  588,  589,  844,  845,  558,  559,    0,    0,    0,    0,    0, 
................................................................................
  271,  526,  527,  782,  783,  110,  111,    0,    0,    0,    0,    0,    0, 
   30,   31,  286,  287,  542,  543,  798,  799,  126,  127,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0, 
    0,    0,    0,    0,    0,    0,    0,    0,  140,  141,  396,  397,  652, 
  653,  908,  909,  174,  175,    0,    0,    0,    0,    0,    0,  156,  157, 
  412,  413,  668,  669,  924,  925,  190,  191,    0,    0,    0,    0,    0, 
    0,  172,  173,  428,  429,  684,  685,  940,  941,  430,  431,    0,    0, 
    0,    0,    0,    0,  188,  189,  444,  445,  700,  701,  956,  957,  446, 
  447,    0,    0,    0,    0,    0,    0,  204,  205,  460,  461,  716,  717, 
................................................................................
  937,  978,  979,  740,  741,  742,  743,  744,  745,  746,  747,  748,  749, 
  784,  785,  946,  947,  788,  789,  750,  751,  752,  753,  754,  755,  756, 
  757,  758,  759,  794,  795,  956,  957,  798,  799,  760,  761,  762,  763, 
  764,  765,  766,  767,  768,  769,  786,  787,  966,  967,  988,  989,  770, 
  771,  772,  773,  774,  775,  776,  777,  778,  779,  796,  797,  976,  977, 
  998,  999};
#endif
 
#if DEC_DPD2BINK==1 && !defined(DECDPD2BINK)
#define DECDPD2BINK
 
const uint32_t DPD2BINK[1024]={       0,   1000,   2000,   3000,   4000,   5000, 
   6000,   7000,   8000,   9000,  80000,  81000, 800000, 801000, 880000, 881000, 
  10000,  11000,  12000,  13000,  14000,  15000,  16000,  17000,  18000,  19000, 
  90000,  91000, 810000, 811000, 890000, 891000,  20000,  21000,  22000,  23000, 
  24000,  25000,  26000,  27000,  28000,  29000,  82000,  83000, 820000, 821000, 
 808000, 809000,  30000,  31000,  32000,  33000,  34000,  35000,  36000,  37000, 
  38000,  39000,  92000,  93000, 830000, 831000, 818000, 819000,  40000,  41000, 
  42000,  43000,  44000,  45000,  46000,  47000,  48000,  49000,  84000,  85000, 
 840000, 841000,  88000,  89000,  50000,  51000,  52000,  53000,  54000,  55000, 
  56000,  57000,  58000,  59000,  94000,  95000, 850000, 851000,  98000,  99000, 
  60000,  61000,  62000,  63000,  64000,  65000,  66000,  67000,  68000,  69000, 
  86000,  87000, 860000, 861000, 888000, 889000,  70000,  71000,  72000,  73000, 
  74000,  75000,  76000,  77000,  78000,  79000,  96000,  97000, 870000, 871000, 
 898000, 899000, 100000, 101000, 102000, 103000, 104000, 105000, 106000, 107000, 
 108000, 109000, 180000, 181000, 900000, 901000, 980000, 981000, 110000, 111000, 
 112000, 113000, 114000, 115000, 116000, 117000, 118000, 119000, 190000, 191000, 
 910000, 911000, 990000, 991000, 120000, 121000, 122000, 123000, 124000, 125000, 
 126000, 127000, 128000, 129000, 182000, 183000, 920000, 921000, 908000, 909000, 
 130000, 131000, 132000, 133000, 134000, 135000, 136000, 137000, 138000, 139000, 
 192000, 193000, 930000, 931000, 918000, 919000, 140000, 141000, 142000, 143000, 
 144000, 145000, 146000, 147000, 148000, 149000, 184000, 185000, 940000, 941000, 
 188000, 189000, 150000, 151000, 152000, 153000, 154000, 155000, 156000, 157000, 
 158000, 159000, 194000, 195000, 950000, 951000, 198000, 199000, 160000, 161000, 
 162000, 163000, 164000, 165000, 166000, 167000, 168000, 169000, 186000, 187000, 
 960000, 961000, 988000, 989000, 170000, 171000, 172000, 173000, 174000, 175000, 
 176000, 177000, 178000, 179000, 196000, 197000, 970000, 971000, 998000, 999000, 
 200000, 201000, 202000, 203000, 204000, 205000, 206000, 207000, 208000, 209000, 
 280000, 281000, 802000, 803000, 882000, 883000, 210000, 211000, 212000, 213000, 
 214000, 215000, 216000, 217000, 218000, 219000, 290000, 291000, 812000, 813000, 
 892000, 893000, 220000, 221000, 222000, 223000, 224000, 225000, 226000, 227000, 
 228000, 229000, 282000, 283000, 822000, 823000, 828000, 829000, 230000, 231000, 
 232000, 233000, 234000, 235000, 236000, 237000, 238000, 239000, 292000, 293000, 
 832000, 833000, 838000, 839000, 240000, 241000, 242000, 243000, 244000, 245000, 
 246000, 247000, 248000, 249000, 284000, 285000, 842000, 843000, 288000, 289000, 
 250000, 251000, 252000, 253000, 254000, 255000, 256000, 257000, 258000, 259000, 
 294000, 295000, 852000, 853000, 298000, 299000, 260000, 261000, 262000, 263000, 
 264000, 265000, 266000, 267000, 268000, 269000, 286000, 287000, 862000, 863000, 
 888000, 889000, 270000, 271000, 272000, 273000, 274000, 275000, 276000, 277000, 
 278000, 279000, 296000, 297000, 872000, 873000, 898000, 899000, 300000, 301000, 
 302000, 303000, 304000, 305000, 306000, 307000, 308000, 309000, 380000, 381000, 
 902000, 903000, 982000, 983000, 310000, 311000, 312000, 313000, 314000, 315000, 
 316000, 317000, 318000, 319000, 390000, 391000, 912000, 913000, 992000, 993000, 
 320000, 321000, 322000, 323000, 324000, 325000, 326000, 327000, 328000, 329000, 
 382000, 383000, 922000, 923000, 928000, 929000, 330000, 331000, 332000, 333000, 
 334000, 335000, 336000, 337000, 338000, 339000, 392000, 393000, 932000, 933000, 
 938000, 939000, 340000, 341000, 342000, 343000, 344000, 345000, 346000, 347000, 
 348000, 349000, 384000, 385000, 942000, 943000, 388000, 389000, 350000, 351000, 
 352000, 353000, 354000, 355000, 356000, 357000, 358000, 359000, 394000, 395000, 
 952000, 953000, 398000, 399000, 360000, 361000, 362000, 363000, 364000, 365000, 
 366000, 367000, 368000, 369000, 386000, 387000, 962000, 963000, 988000, 989000, 
 370000, 371000, 372000, 373000, 374000, 375000, 376000, 377000, 378000, 379000, 
 396000, 397000, 972000, 973000, 998000, 999000, 400000, 401000, 402000, 403000, 
 404000, 405000, 406000, 407000, 408000, 409000, 480000, 481000, 804000, 805000, 
 884000, 885000, 410000, 411000, 412000, 413000, 414000, 415000, 416000, 417000, 
 418000, 419000, 490000, 491000, 814000, 815000, 894000, 895000, 420000, 421000, 
 422000, 423000, 424000, 425000, 426000, 427000, 428000, 429000, 482000, 483000, 
 824000, 825000, 848000, 849000, 430000, 431000, 432000, 433000, 434000, 435000, 
 436000, 437000, 438000, 439000, 492000, 493000, 834000, 835000, 858000, 859000, 
 440000, 441000, 442000, 443000, 444000, 445000, 446000, 447000, 448000, 449000, 
 484000, 485000, 844000, 845000, 488000, 489000, 450000, 451000, 452000, 453000, 
 454000, 455000, 456000, 457000, 458000, 459000, 494000, 495000, 854000, 855000, 
 498000, 499000, 460000, 461000, 462000, 463000, 464000, 465000, 466000, 467000, 
 468000, 469000, 486000, 487000, 864000, 865000, 888000, 889000, 470000, 471000, 
 472000, 473000, 474000, 475000, 476000, 477000, 478000, 479000, 496000, 497000, 
 874000, 875000, 898000, 899000, 500000, 501000, 502000, 503000, 504000, 505000, 
 506000, 507000, 508000, 509000, 580000, 581000, 904000, 905000, 984000, 985000, 
 510000, 511000, 512000, 513000, 514000, 515000, 516000, 517000, 518000, 519000, 
 590000, 591000, 914000, 915000, 994000, 995000, 520000, 521000, 522000, 523000, 
 524000, 525000, 526000, 527000, 528000, 529000, 582000, 583000, 924000, 925000, 
 948000, 949000, 530000, 531000, 532000, 533000, 534000, 535000, 536000, 537000, 
 538000, 539000, 592000, 593000, 934000, 935000, 958000, 959000, 540000, 541000, 
 542000, 543000, 544000, 545000, 546000, 547000, 548000, 549000, 584000, 585000, 
 944000, 945000, 588000, 589000, 550000, 551000, 552000, 553000, 554000, 555000, 
 556000, 557000, 558000, 559000, 594000, 595000, 954000, 955000, 598000, 599000, 
 560000, 561000, 562000, 563000, 564000, 565000, 566000, 567000, 568000, 569000, 
 586000, 587000, 964000, 965000, 988000, 989000, 570000, 571000, 572000, 573000, 
 574000, 575000, 576000, 577000, 578000, 579000, 596000, 597000, 974000, 975000, 
 998000, 999000, 600000, 601000, 602000, 603000, 604000, 605000, 606000, 607000, 
 608000, 609000, 680000, 681000, 806000, 807000, 886000, 887000, 610000, 611000, 
 612000, 613000, 614000, 615000, 616000, 617000, 618000, 619000, 690000, 691000, 
 816000, 817000, 896000, 897000, 620000, 621000, 622000, 623000, 624000, 625000, 
 626000, 627000, 628000, 629000, 682000, 683000, 826000, 827000, 868000, 869000, 
 630000, 631000, 632000, 633000, 634000, 635000, 636000, 637000, 638000, 639000, 
 692000, 693000, 836000, 837000, 878000, 879000, 640000, 641000, 642000, 643000, 
 644000, 645000, 646000, 647000, 648000, 649000, 684000, 685000, 846000, 847000, 
 688000, 689000, 650000, 651000, 652000, 653000, 654000, 655000, 656000, 657000, 
 658000, 659000, 694000, 695000, 856000, 857000, 698000, 699000, 660000, 661000, 
 662000, 663000, 664000, 665000, 666000, 667000, 668000, 669000, 686000, 687000, 
 866000, 867000, 888000, 889000, 670000, 671000, 672000, 673000, 674000, 675000, 
 676000, 677000, 678000, 679000, 696000, 697000, 876000, 877000, 898000, 899000, 
 700000, 701000, 702000, 703000, 704000, 705000, 706000, 707000, 708000, 709000, 
 780000, 781000, 906000, 907000, 986000, 987000, 710000, 711000, 712000, 713000, 
 714000, 715000, 716000, 717000, 718000, 719000, 790000, 791000, 916000, 917000, 
 996000, 997000, 720000, 721000, 722000, 723000, 724000, 725000, 726000, 727000, 
 728000, 729000, 782000, 783000, 926000, 927000, 968000, 969000, 730000, 731000, 
 732000, 733000, 734000, 735000, 736000, 737000, 738000, 739000, 792000, 793000, 
 936000, 937000, 978000, 979000, 740000, 741000, 742000, 743000, 744000, 745000, 
 746000, 747000, 748000, 749000, 784000, 785000, 946000, 947000, 788000, 789000, 
 750000, 751000, 752000, 753000, 754000, 755000, 756000, 757000, 758000, 759000, 
 794000, 795000, 956000, 957000, 798000, 799000, 760000, 761000, 762000, 763000, 
 764000, 765000, 766000, 767000, 768000, 769000, 786000, 787000, 966000, 967000, 
 988000, 989000, 770000, 771000, 772000, 773000, 774000, 775000, 776000, 777000, 
 778000, 779000, 796000, 797000, 976000, 977000, 998000, 999000};
#endif
 
#if DEC_DPD2BINM==1 && !defined(DECDPD2BINM)
#define DECDPD2BINM
 
const uint32_t DPD2BINM[1024]={0,   1000000,   2000000,   3000000,   4000000, 
   5000000,   6000000,   7000000,   8000000,   9000000,  80000000,  81000000, 
 800000000, 801000000, 880000000, 881000000,  10000000,  11000000,  12000000, 
  13000000,  14000000,  15000000,  16000000,  17000000,  18000000,  19000000, 
  90000000,  91000000, 810000000, 811000000, 890000000, 891000000,  20000000, 
  21000000,  22000000,  23000000,  24000000,  25000000,  26000000,  27000000, 
  28000000,  29000000,  82000000,  83000000, 820000000, 821000000, 808000000, 
 809000000,  30000000,  31000000,  32000000,  33000000,  34000000,  35000000, 
  36000000,  37000000,  38000000,  39000000,  92000000,  93000000, 830000000, 
 831000000, 818000000, 819000000,  40000000,  41000000,  42000000,  43000000, 
  44000000,  45000000,  46000000,  47000000,  48000000,  49000000,  84000000, 
  85000000, 840000000, 841000000,  88000000,  89000000,  50000000,  51000000, 
  52000000,  53000000,  54000000,  55000000,  56000000,  57000000,  58000000, 
  59000000,  94000000,  95000000, 850000000, 851000000,  98000000,  99000000, 
  60000000,  61000000,  62000000,  63000000,  64000000,  65000000,  66000000, 
  67000000,  68000000,  69000000,  86000000,  87000000, 860000000, 861000000, 
 888000000, 889000000,  70000000,  71000000,  72000000,  73000000,  74000000, 
  75000000,  76000000,  77000000,  78000000,  79000000,  96000000,  97000000, 
 870000000, 871000000, 898000000, 899000000, 100000000, 101000000, 102000000, 
 103000000, 104000000, 105000000, 106000000, 107000000, 108000000, 109000000, 
 180000000, 181000000, 900000000, 901000000, 980000000, 981000000, 110000000, 
 111000000, 112000000, 113000000, 114000000, 115000000, 116000000, 117000000, 
 118000000, 119000000, 190000000, 191000000, 910000000, 911000000, 990000000, 
 991000000, 120000000, 121000000, 122000000, 123000000, 124000000, 125000000, 
 126000000, 127000000, 128000000, 129000000, 182000000, 183000000, 920000000, 
 921000000, 908000000, 909000000, 130000000, 131000000, 132000000, 133000000, 
 134000000, 135000000, 136000000, 137000000, 138000000, 139000000, 192000000, 
 193000000, 930000000, 931000000, 918000000, 919000000, 140000000, 141000000, 
 142000000, 143000000, 144000000, 145000000, 146000000, 147000000, 148000000, 
 149000000, 184000000, 185000000, 940000000, 941000000, 188000000, 189000000, 
 150000000, 151000000, 152000000, 153000000, 154000000, 155000000, 156000000, 
 157000000, 158000000, 159000000, 194000000, 195000000, 950000000, 951000000, 
 198000000, 199000000, 160000000, 161000000, 162000000, 163000000, 164000000, 
 165000000, 166000000, 167000000, 168000000, 169000000, 186000000, 187000000, 
 960000000, 961000000, 988000000, 989000000, 170000000, 171000000, 172000000, 
 173000000, 174000000, 175000000, 176000000, 177000000, 178000000, 179000000, 
 196000000, 197000000, 970000000, 971000000, 998000000, 999000000, 200000000, 
 201000000, 202000000, 203000000, 204000000, 205000000, 206000000, 207000000, 
 208000000, 209000000, 280000000, 281000000, 802000000, 803000000, 882000000, 
 883000000, 210000000, 211000000, 212000000, 213000000, 214000000, 215000000, 
 216000000, 217000000, 218000000, 219000000, 290000000, 291000000, 812000000, 
 813000000, 892000000, 893000000, 220000000, 221000000, 222000000, 223000000, 
 224000000, 225000000, 226000000, 227000000, 228000000, 229000000, 282000000, 
 283000000, 822000000, 823000000, 828000000, 829000000, 230000000, 231000000, 
 232000000, 233000000, 234000000, 235000000, 236000000, 237000000, 238000000, 
 239000000, 292000000, 293000000, 832000000, 833000000, 838000000, 839000000, 
 240000000, 241000000, 242000000, 243000000, 244000000, 245000000, 246000000, 
 247000000, 248000000, 249000000, 284000000, 285000000, 842000000, 843000000, 
 288000000, 289000000, 250000000, 251000000, 252000000, 253000000, 254000000, 
 255000000, 256000000, 257000000, 258000000, 259000000, 294000000, 295000000, 
 852000000, 853000000, 298000000, 299000000, 260000000, 261000000, 262000000, 
 263000000, 264000000, 265000000, 266000000, 267000000, 268000000, 269000000, 
 286000000, 287000000, 862000000, 863000000, 888000000, 889000000, 270000000, 
 271000000, 272000000, 273000000, 274000000, 275000000, 276000000, 277000000, 
 278000000, 279000000, 296000000, 297000000, 872000000, 873000000, 898000000, 
 899000000, 300000000, 301000000, 302000000, 303000000, 304000000, 305000000, 
 306000000, 307000000, 308000000, 309000000, 380000000, 381000000, 902000000, 
 903000000, 982000000, 983000000, 310000000, 311000000, 312000000, 313000000, 
 314000000, 315000000, 316000000, 317000000, 318000000, 319000000, 390000000, 
 391000000, 912000000, 913000000, 992000000, 993000000, 320000000, 321000000, 
 322000000, 323000000, 324000000, 325000000, 326000000, 327000000, 328000000, 
 329000000, 382000000, 383000000, 922000000, 923000000, 928000000, 929000000, 
 330000000, 331000000, 332000000, 333000000, 334000000, 335000000, 336000000, 
 337000000, 338000000, 339000000, 392000000, 393000000, 932000000, 933000000, 
 938000000, 939000000, 340000000, 341000000, 342000000, 343000000, 344000000, 
 345000000, 346000000, 347000000, 348000000, 349000000, 384000000, 385000000, 
 942000000, 943000000, 388000000, 389000000, 350000000, 351000000, 352000000, 
 353000000, 354000000, 355000000, 356000000, 357000000, 358000000, 359000000, 
 394000000, 395000000, 952000000, 953000000, 398000000, 399000000, 360000000, 
 361000000, 362000000, 363000000, 364000000, 365000000, 366000000, 367000000, 
 368000000, 369000000, 386000000, 387000000, 962000000, 963000000, 988000000, 
 989000000, 370000000, 371000000, 372000000, 373000000, 374000000, 375000000, 
 376000000, 377000000, 378000000, 379000000, 396000000, 397000000, 972000000, 
 973000000, 998000000, 999000000, 400000000, 401000000, 402000000, 403000000, 
 404000000, 405000000, 406000000, 407000000, 408000000, 409000000, 480000000, 
 481000000, 804000000, 805000000, 884000000, 885000000, 410000000, 411000000, 
 412000000, 413000000, 414000000, 415000000, 416000000, 417000000, 418000000, 
 419000000, 490000000, 491000000, 814000000, 815000000, 894000000, 895000000, 
 420000000, 421000000, 422000000, 423000000, 424000000, 425000000, 426000000, 
 427000000, 428000000, 429000000, 482000000, 483000000, 824000000, 825000000, 
 848000000, 849000000, 430000000, 431000000, 432000000, 433000000, 434000000, 
 435000000, 436000000, 437000000, 438000000, 439000000, 492000000, 493000000, 
 834000000, 835000000, 858000000, 859000000, 440000000, 441000000, 442000000, 
 443000000, 444000000, 445000000, 446000000, 447000000, 448000000, 449000000, 
 484000000, 485000000, 844000000, 845000000, 488000000, 489000000, 450000000, 
 451000000, 452000000, 453000000, 454000000, 455000000, 456000000, 457000000, 
 458000000, 459000000, 494000000, 495000000, 854000000, 855000000, 498000000, 
 499000000, 460000000, 461000000, 462000000, 463000000, 464000000, 465000000, 
 466000000, 467000000, 468000000, 469000000, 486000000, 487000000, 864000000, 
 865000000, 888000000, 889000000, 470000000, 471000000, 472000000, 473000000, 
 474000000, 475000000, 476000000, 477000000, 478000000, 479000000, 496000000, 
 497000000, 874000000, 875000000, 898000000, 899000000, 500000000, 501000000, 
 502000000, 503000000, 504000000, 505000000, 506000000, 507000000, 508000000, 
 509000000, 580000000, 581000000, 904000000, 905000000, 984000000, 985000000, 
 510000000, 511000000, 512000000, 513000000, 514000000, 515000000, 516000000, 
 517000000, 518000000, 519000000, 590000000, 591000000, 914000000, 915000000, 
 994000000, 995000000, 520000000, 521000000, 522000000, 523000000, 524000000, 
 525000000, 526000000, 527000000, 528000000, 529000000, 582000000, 583000000, 
 924000000, 925000000, 948000000, 949000000, 530000000, 531000000, 532000000, 
 533000000, 534000000, 535000000, 536000000, 537000000, 538000000, 539000000, 
 592000000, 593000000, 934000000, 935000000, 958000000, 959000000, 540000000, 
 541000000, 542000000, 543000000, 544000000, 545000000, 546000000, 547000000, 
 548000000, 549000000, 584000000, 585000000, 944000000, 945000000, 588000000, 
 589000000, 550000000, 551000000, 552000000, 553000000, 554000000, 555000000, 
 556000000, 557000000, 558000000, 559000000, 594000000, 595000000, 954000000, 
 955000000, 598000000, 599000000, 560000000, 561000000, 562000000, 563000000, 
 564000000, 565000000, 566000000, 567000000, 568000000, 569000000, 586000000, 
 587000000, 964000000, 965000000, 988000000, 989000000, 570000000, 571000000, 
 572000000, 573000000, 574000000, 575000000, 576000000, 577000000, 578000000, 
 579000000, 596000000, 597000000, 974000000, 975000000, 998000000, 999000000, 
 600000000, 601000000, 602000000, 603000000, 604000000, 605000000, 606000000, 
 607000000, 608000000, 609000000, 680000000, 681000000, 806000000, 807000000, 
 886000000, 887000000, 610000000, 611000000, 612000000, 613000000, 614000000, 
 615000000, 616000000, 617000000, 618000000, 619000000, 690000000, 691000000, 
 816000000, 817000000, 896000000, 897000000, 620000000, 621000000, 622000000, 
 623000000, 624000000, 625000000, 626000000, 627000000, 628000000, 629000000, 
 682000000, 683000000, 826000000, 827000000, 868000000, 869000000, 630000000, 
 631000000, 632000000, 633000000, 634000000, 635000000, 636000000, 637000000, 
 638000000, 639000000, 692000000, 693000000, 836000000, 837000000, 878000000, 
 879000000, 640000000, 641000000, 642000000, 643000000, 644000000, 645000000, 
 646000000, 647000000, 648000000, 649000000, 684000000, 685000000, 846000000, 
 847000000, 688000000, 689000000, 650000000, 651000000, 652000000, 653000000, 
 654000000, 655000000, 656000000, 657000000, 658000000, 659000000, 694000000, 
 695000000, 856000000, 857000000, 698000000, 699000000, 660000000, 661000000, 
 662000000, 663000000, 664000000, 665000000, 666000000, 667000000, 668000000, 
 669000000, 686000000, 687000000, 866000000, 867000000, 888000000, 889000000, 
 670000000, 671000000, 672000000, 673000000, 674000000, 675000000, 676000000, 
 677000000, 678000000, 679000000, 696000000, 697000000, 876000000, 877000000, 
 898000000, 899000000, 700000000, 701000000, 702000000, 703000000, 704000000, 
 705000000, 706000000, 707000000, 708000000, 709000000, 780000000, 781000000, 
 906000000, 907000000, 986000000, 987000000, 710000000, 711000000, 712000000, 
 713000000, 714000000, 715000000, 716000000, 717000000, 718000000, 719000000, 
 790000000, 791000000, 916000000, 917000000, 996000000, 997000000, 720000000, 
 721000000, 722000000, 723000000, 724000000, 725000000, 726000000, 727000000, 
 728000000, 729000000, 782000000, 783000000, 926000000, 927000000, 968000000, 
 969000000, 730000000, 731000000, 732000000, 733000000, 734000000, 735000000, 
 736000000, 737000000, 738000000, 739000000, 792000000, 793000000, 936000000, 
 937000000, 978000000, 979000000, 740000000, 741000000, 742000000, 743000000, 
 744000000, 745000000, 746000000, 747000000, 748000000, 749000000, 784000000, 
 785000000, 946000000, 947000000, 788000000, 789000000, 750000000, 751000000, 
 752000000, 753000000, 754000000, 755000000, 756000000, 757000000, 758000000, 
 759000000, 794000000, 795000000, 956000000, 957000000, 798000000, 799000000, 
 760000000, 761000000, 762000000, 763000000, 764000000, 765000000, 766000000, 
 767000000, 768000000, 769000000, 786000000, 787000000, 966000000, 967000000, 
 988000000, 989000000, 770000000, 771000000, 772000000, 773000000, 774000000, 
 775000000, 776000000, 777000000, 778000000, 779000000, 796000000, 797000000, 
 976000000, 977000000, 998000000, 999000000};
#endif
 
#if DEC_BIN2CHAR==1 && !defined(DECBIN2CHAR)
#define DECBIN2CHAR
 
const uint8_t BIN2CHAR[4001]={
 '\0','0','0','0', '\1','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','0','0','9', 
................................................................................
 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, 
 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, 
 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, 
 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, 
 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, 
 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};
#endif
 
#if DEC_BIN2BCD8==1 && !defined(DECBIN2BCD8)
#define DECBIN2BCD8
 
const uint8_t BIN2BCD8[4000]={
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 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, 
 9,9,9,3};
#endif
 

Changes to decNumber/decNumber.c.

1
2
3
4
5
6
7
8
9
10
11
..
46
47
48
49
50
51
52





53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180



181
182
183
184
185
186


187
188
189
190
191
192
193
...
214
215
216
217
218
219
220

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

239
240

241
242
243
244
245
246
247
248
249
250
251
252
253
254
...
302
303
304
305
306
307
308













































































































309
310
311
312
313
314
315
...
352
353
354
355
356
357
358
359

360
361
362
363
364
365
366
...
589
590
591
592
593
594
595

596
597
598
599
600
601
602
...
636
637
638
639
640
641
642







































































643
644
645
646
647
648
649
...
656
657
658
659
660
661
662




















663
664
665
666
667
668
669
...
671
672
673
674
675
676
677
678
679
680
681
682
683


































































684
685
686
687
688
689
690
...
785
786
787
788
789
790
791
























































































































































792
793
794
795
796
797
798
...
855
856
857
858
859
860
861

















































862
863
864
865
866
867
868
...
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
...
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
....
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
....
1045
1046
1047
1048
1049
1050
1051























1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
....
1068
1069
1070
1071
1072
1073
1074























1075
1076
1077
1078
1079
1080
1081
1082
1083
1084

1085
1086
1087
1088
1089
1090
1091
....
1100
1101
1102
1103
1104
1105
1106
1107
1108
















































































































































































































1109
1110
1111
1112
1113
1114
1115
1116

1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
....
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
....
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
....
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
....
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
....
1664
1665
1666
1667
1668
1669
1670









































































































































1671
1672
1673
1674
1675
1676
1677
....
1693
1694
1695
1696
1697
1698
1699

































































































































1700
1701
1702
1703
1704
1705
1706
....
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
....
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
....
2032
2033
2034
2035
2036
2037
2038
2039

2040
2041
2042
2043
2044
2045
2046
....
2080
2081
2082
2083
2084
2085
2086

2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098

2099
2100


2101
2102
2103
2104
2105
2106

2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120

2121
2122
2123
2124
2125
2126
2127
2128















2129
2130






































































2131
2132
2133
















































2134
2135
2136
2137
2138
2139
2140
....
2166
2167
2168
2169
2170
2171
2172
2173
2174










































































































































































2175
2176
2177
2178
2179
2180
2181
2182
2183
2184





2185
2186
2187
2188
2189
2190
2191
2192
....
2391
2392
2393
2394
2395
2396
2397

2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415


2416
2417
2418
2419
2420
2421
2422
....
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
....
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
....
2539
2540
2541
2542
2543
2544
2545

2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557

2558
2559
2560
2561
2562
2563
2564
....
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
....
2648
2649
2650
2651
2652
2653
2654

2655
2656
2657
2658
2659
2660
2661
....
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
....
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
....
2819
2820
2821
2822
2823
2824
2825


2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
....
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
....
3009
3010
3011
3012
3013
3014
3015

3016
3017
3018
3019
3020
3021
3022
....
3033
3034
3035
3036
3037
3038
3039

3040
3041
3042
3043
3044
3045
3046
....
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
....
3274
3275
3276
3277
3278
3279
3280

3281
3282
3283
3284
3285
3286
3287
....
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
....
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
....
3433
3434
3435
3436
3437
3438
3439


3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
....
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
....
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
....
3728
3729
3730
3731
3732
3733
3734

3735
3736
3737
3738
3739
3740
3741
....
3821
3822
3823
3824
3825
3826
3827



3828
3829
3830
3831
3832
3833
3834
3835
3836
3837

3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
....
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
....
3894
3895
3896
3897
3898
3899
3900
3901




3902
3903
3904
3905
3906
3907
3908
....
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
....
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
....
4173
4174
4175
4176
4177
4178
4179



4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
....
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
....
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
....
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
....
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
....
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574


4575
4576

4577

4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
....
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
....
4635
4636
4637
4638
4639
4640
4641
4642


4643
4644
4645
4646
4647
4648
4649
4650
....
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675


4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
....
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
....
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
....
4757
4758
4759
4760
4761
4762
4763
4764



4765
4766
4767
4768
4769
4770
4771
4772

4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
....
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
....
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
....
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166

5167
5168
5169
5170


5171
5172
5173
5174

5175
5176
5177
5178
5179
5180
5181
....
5210
5211
5212
5213
5214
5215
5216
5217







5218
5219
5220
5221
5222
5223
5224
5225




















5226
5227
5228
5229
5230
5231
5232
....
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301




5302
5303
5304
5305
5306
5307
5308
....
5595
5596
5597
5598
5599
5600
5601
5602


5603
5604
5605
5606
5607
5608
5609
....
5623
5624
5625
5626
5627
5628
5629












5630
5631
5632
5633
5634
5635
5636
....
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
....
5816
5817
5818
5819
5820
5821
5822

5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834





5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845

5846
5847
5848
5849
5850
5851
5852
....
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
....
5899
5900
5901
5902
5903
5904
5905
















5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
....
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
....
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
....
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140

6141
6142



6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153

6154
6155
6156
6157
6158
6159
6160


6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
....
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199

6200
6201
6202
6203
6204
6205
6206
6207
6208

6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220

6221












6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
....
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276




6277
6278
6279
6280
6281
6282
6283
....
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
....
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
....
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
....
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430

6431
6432
6433
6434
6435
6436
6437
....
6456
6457
6458
6459
6460
6461
6462

6463
6464
6465
6466
6467
6468
6469
6470
6471

6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558

6559
6560
6561
6562
6563
6564
6565
6566
6567
6568

6569
6570
6571
6572
6573
6574
6575
....
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
....
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
/* ------------------------------------------------------------------ */
/* Decimal Number arithmetic module                                   */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2006.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
/*    range (Emax in the range 0 through 999,999,999 and Emin in the  */
/*    range -999,999,999 through 0).  Mathematical functions (for     */
/*    example decNumberExp) as identified below are restricted more   */
/*    tightly: digits, emax, and -emin in the context must be <=      */
/*    DEC_MAX_MATH (999999), and their operand(s) must be within      */
/*    these bounds.                                                   */
/*                                                                    */





/* 3. Operands to operator functions are never modified unless they   */
/*    are also specified to be the result number (which is always     */
/*    permitted).  Other than that case, operands must not overlap.   */
/*                                                                    */
/* 4. Error handling: the type of the error is ORed into the status   */
/*    flags in the current context (decContext structure).  The       */
/*    SIGFPE signal is then raised if the corresponding trap-enabler  */
/*    flag in the decContext is set (is 1).                           */
/*                                                                    */
/*    It is the responsibility of the caller to clear the status      */
/*    flags as required.                                              */
/*                                                                    */
/*    The result of any routine which returns a number will always    */
/*    be a valid number (which may be a special value, such as an     */
/*    Infinity or NaN).                                               */
/*                                                                    */
/* 5. The decNumber format is not an exchangeable concrete            */
/*    representation as it comprises fields which may be machine-     */
/*    dependent (packed or unpacked, or special length, for example). */
/*    Canonical conversions to and from strings are provided; other   */
/*    conversions are available in separate modules.                  */
/*                                                                    */
/* 6. Normally, input operands are assumed to be valid.  Set DECCHECK */
/*    to 1 for extended operand checking (including NULL operands).   */
/*    Results are undefined if a badly-formed structure (or a NULL    */
/*    pointer to a structure) is provided, though with DECCHECK       */
/*    enabled the operator routines are protected against exceptions. */
/*    (Except if the result pointer is NULL, which is unrecoverable.) */
/*                                                                    */
/*    However, the routines will never cause exceptions if they are   */
/*    given well-formed operands, even if the value of the operands   */
/*    is inappropriate for the operation and DECCHECK is not set.     */
/*    (Except for SIGFPE, as and where documented.)                   */
/*                                                                    */
/* 7. Subset arithmetic is available only if DECSUBSET is set to 1.   */
/* ------------------------------------------------------------------ */
/* Implementation notes for maintenance of this module:               */
/*                                                                    */
/* 1. Storage leak protection:  Routines which use malloc are not     */
/*    permitted to use return for fastpath or error exits (i.e.,      */
/*    they follow strict structured programming conventions).         */
/*    Instead they have a do{}while(0); construct surrounding the     */
................................................................................
// Public constant array: powers of ten (powers[n]==10**n, 0<=n<=9)
const uInt powers[10]={1, 10, 100, 1000, 10000, 100000, 1000000,
                       10000000, 100000000, 1000000000};
// Public lookup table used by the D2U macro
const uByte d2utable[DECMAXD2U+1]=D2UTABLE;

// Local constants
#define DIVIDE    0x80             // Divide operators
#define REMAINDER 0x40             // ..
#define DIVIDEINT 0x20             // ..
#define REMNEAR   0x10             // ..
#define COMPARE   0x01             // Compare operators
#define COMPMAX   0x02             // ..
#define COMPMIN   0x03             // ..
#define COMPTOTAL 0x04             // ..
#define COMPNAN   0x05             // .. [NaN processing]




#define DEC_sNaN     0x40000000    // local status: sNaN signal
#define BADINT  (Int)0x80000000    // most-negative Int; error indicator
// Next two indicate an integer >= 10**6, and its parity (bottom bit)
#define BIGEVEN (Int)0x80000002
#define BIGODD  (Int)0x80000003



static Unit uarrone[1]={1};   // Unit array of 1, used for incrementing

/* Granularity-dependent code */
#if DECDPUN<=4
  #define eInt  Int           // extended integer
  #define ueInt uInt          // unsigned extended integer
................................................................................
static void        decApplyRound(decNumber *, decContext *, Int, uInt *);
static Int         decCompare(const decNumber *lhs, const decNumber *rhs, Flag);
static decNumber * decCompareOp(decNumber *, const decNumber *,
                              const decNumber *, decContext *,
                              Flag, uInt *);
static void        decCopyFit(decNumber *, const decNumber *, decContext *,
                              Int *, uInt *);

static decNumber * decDivideOp(decNumber *, const decNumber *,
                              const decNumber *, decContext *, Flag, uInt *);
static decNumber * decExpOp(decNumber *, const decNumber *,
                              decContext *, uInt *);
static void        decFinalize(decNumber *, decContext *, Int *, uInt *);
static Int         decGetDigits(Unit *, Int);
static Int         decGetInt(const decNumber *);
static decNumber * decLnOp(decNumber *, const decNumber *,
                              decContext *, uInt *);
static decNumber * decMultiplyOp(decNumber *, const decNumber *,
                              const decNumber *, decContext *,
                              uInt *);
static decNumber * decNaNs(decNumber *, const decNumber *,
                              const decNumber *, uInt *);
static decNumber * decPutInt(decNumber *, Int);
static decNumber * decQuantizeOp(decNumber *, const decNumber *,
                              const decNumber *, decContext *, Flag,
                              uInt *);

static void        decSetCoeff(decNumber *, decContext *, const Unit *,
                              Int, Int *, uInt *);

static void        decSetOverflow(decNumber *, decContext *, uInt *);
static void        decSetSubnormal(decNumber *, decContext *, Int *, uInt *);
static Int         decShiftToLeast(Unit *, Int, Int);
static Int         decShiftToMost(Unit *, Int, Int);
static void        decStatus(decNumber *, uInt, decContext *);
static void        decToString(const decNumber *, char[], Flag);
static decNumber * decTrim(decNumber *, Flag, Int *);
static Int         decUnitAddSub(const Unit *, Int, const Unit *, Int, Int,
                              Unit *, Int);
static Int         decUnitCompare(const Unit *, Int, const Unit *, Int, Int);

#if !DECSUBSET
/* decFinish == decFinalize when no subset arithmetic needed */
#define decFinish(a,b,c,d) decFinalize(a,b,c,d)
................................................................................
void decNumberShow(const decNumber *);  // displays the components of a number
static void decDumpAr(char, const Unit *, Int);
#endif

/* ================================================================== */
/* Conversions                                                        */
/* ================================================================== */














































































































/* ------------------------------------------------------------------ */
/* to-scientific-string -- conversion to numeric string               */
/* to-engineering-string -- conversion to numeric string              */
/*                                                                    */
/*   decNumberToString(dn, string);                                   */
/*   decNumberToEngString(dn, string);                                */
................................................................................
/* If bad syntax is detected, the result will be a quiet NaN.         */
/* ------------------------------------------------------------------ */
decNumber * decNumberFromString(decNumber *dn, const char chars[],
                                decContext *set) {
  Int   exponent=0;                // working exponent [assume 0]
  uByte bits=0;                    // working flags [assume +ve]
  Unit  *res;                      // where result will be built
  Unit  resbuff[SD2U(DECBUFFER+1)];// local buffer in case need temporary

  Unit  *allocres=NULL;            // -> allocated result, iff allocated
  Int   d=0;                       // count of digits found in decimal part
  const char *dotchar=NULL;        // where dot was found
  const char *cfirst=chars;        // -> first character of decimal part
  const char *last=NULL;           // -> last digit of decimal part
  const char *c;                   // work
  Unit  *up;                       // ..
................................................................................
/*                                                                    */
/*   This computes C = abs(A)                                         */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*   set is the context                                               */
/*                                                                    */

/* C must have space for set->digits digits.                          */
/* ------------------------------------------------------------------ */
/* This has the same effect as decNumberPlus unless A is negative,    */
/* in which case it has the same effect as decNumberMinus.            */
/* ------------------------------------------------------------------ */
decNumber * decNumberAbs(decNumber *res, const decNumber *rhs,
                         decContext *set) {
................................................................................
  decAddOp(res, lhs, rhs, set, 0, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberAdd








































































/* ------------------------------------------------------------------ */
/* decNumberCompare -- compare two Numbers                            */
/*                                                                    */
/*   This computes C = A ? B                                          */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
................................................................................
decNumber * decNumberCompare(decNumber *res, const decNumber *lhs,
                             const decNumber *rhs, decContext *set) {
  uInt status=0;                        // accumulator
  decCompareOp(res, lhs, rhs, set, COMPARE, &status);
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberCompare





















/* ------------------------------------------------------------------ */
/* decNumberCompareTotal -- compare two Numbers, using total ordering */
/*                                                                    */
/*   This computes C = A ? B, under total ordering                    */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
................................................................................
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for one digit; the result will always be one of  */
/* -1, 0, or 1.                                                       */
/* ------------------------------------------------------------------ */
decNumber * decNumberCompareTotal(decNumber *res, const decNumber *lhs,
                             const decNumber *rhs, decContext *set) {
  uInt status=0;                        // accumulator
  decCompareOp(res, lhs, rhs, set, COMPTOTAL, &status);
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberCompareTotal



































































/* ------------------------------------------------------------------ */
/* decNumberDivide -- divide one number by another                    */
/*                                                                    */
/*   This computes C = A / B                                          */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X/X)         */
................................................................................
  // apply significant status
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberExp

























































































































































/* ------------------------------------------------------------------ */
/* decNumberLn -- natural logarithm                                   */
/*                                                                    */
/*   This computes C = ln(A)                                          */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
................................................................................
  // apply significant status
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberLn


















































/* ------------------------------------------------------------------ */
/* decNumberLog10 -- logarithm in base 10                             */
/*                                                                    */
/*   This computes C = log10(A)                                       */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
................................................................................

  // buffers for a and b working decimals
  // (adjustment calculator, same size)
  decNumber bufa[D2N(DECBUFFER+2)];
  decNumber *allocbufa=NULL;       // -> allocated bufa, iff allocated
  decNumber *a=bufa;               // temporary a
  decNumber bufb[D2N(DECBUFFER+2)];
  decNumber *allocbufb=NULL;       // -> allocated bufa, iff allocated
  decNumber *b=bufb;               // temporary b
  decNumber bufw[D2N(10)];         // working 2-10 digit number
  decNumber *w=bufw;               // ..
  #if DECSUBSET
  decNumber *allocrhs=NULL;        // non-NULL if rounded rhs allocated
  #endif

................................................................................
      decCopyFit(w, rhs, &aset, &residue, &copystat); // copy & shorten
      // if exact and the digit is 1, rhs is a power of 10
      if (!(copystat&DEC_Inexact) && w->lsu[0]==1) {
        // the exponent, conveniently, is the power of 10; making
        // this the result needs a little care as it might not fit,
        // so first convert it into the working number, and then move
        // to res
        decPutInt(w, w->exponent);
        residue=0;
        decCopyFit(res, w, set, &residue, &status); // copy & round
        decFinish(res, set, &residue, &status);     // cleanup/set flags
        break;
        } // not a power of 10
      } // not a candidate for exact

................................................................................
  #endif
  return res;
  } // decNumberLog10

/* ------------------------------------------------------------------ */
/* decNumberMax -- compare two Numbers and return the maximum         */
/*                                                                    */
/*   This computes C = A ? B, returning the maximum or A if equal     */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for set->digits digits.                          */
................................................................................
  decCompareOp(res, lhs, rhs, set, COMPMAX, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberMax
























/* ------------------------------------------------------------------ */
/* decNumberMin -- compare two Numbers and return the minimum         */
/*                                                                    */
/*   This computes C = A ? B, returning the minimum or A if equal     */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for set->digits digits.                          */
................................................................................
  decCompareOp(res, lhs, rhs, set, COMPMIN, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberMin
























/* ------------------------------------------------------------------ */
/* decNumberMinus -- prefix minus operator                            */
/*                                                                    */
/*   This computes C = 0 - A                                          */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*   set is the context                                               */
/*                                                                    */

/* C must have space for set->digits digits.                          */
/* ------------------------------------------------------------------ */
/* Simply use AddOp for the subtract, which will do the necessary.    */
/* ------------------------------------------------------------------ */
decNumber * decNumberMinus(decNumber *res, const decNumber *rhs,
                           decContext *set) {
  decNumber dzero;
................................................................................
  decAddOp(res, &dzero, rhs, set, DECNEG, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberMinus

/* ------------------------------------------------------------------ */
















































































































































































































/* decNumberPlus -- prefix plus operator                              */
/*                                                                    */
/*   This computes C = 0 + A                                          */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*   set is the context                                               */
/*                                                                    */

/* C must have space for set->digits digits.                          */
/* ------------------------------------------------------------------ */
/* This simply uses AddOp; Add will take fast path after preparing A. */
/* Performance is a concern here, as this routine is often used to    */
/* check operands and apply rounding and overflow/underflow testing.  */
/* ------------------------------------------------------------------ */
decNumber * decNumberPlus(decNumber *res, const decNumber *rhs,
                          decContext *set) {
  decNumber dzero;
  uInt status=0;                        // accumulator

  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
  #endif

  decNumberZero(&dzero);                // make 0
  dzero.exponent=rhs->exponent;         // [no coefficient expansion]
  decAddOp(res, &dzero, rhs, set, 0, &status);
................................................................................
        if (allocrhs==NULL) break;
        rhs=allocrhs;
        }
      }
    #endif
    // [following code does not require input rounding]

    // specials copy through, except NaNs need care
    if (decNumberIsNaN(rhs)) {
      decNaNs(res, rhs, NULL, &status);
      break;
      }

    // reduce result to the requested length and copy to result
    decCopyFit(res, rhs, set, &residue, &status); // copy & round
    decFinish(res, set, &residue, &status);       // cleanup/set flags
    decTrim(res, 1, &dropped);               // normalize in place
    } while(0);                              // end protected

  #if DECSUBSET
  if (allocrhs !=NULL) free(allocrhs);       // ..
  #endif
  if (status!=0) decStatus(res, status, set);// then report status
  return res;
................................................................................
      }
    #endif
    // [following code does not require input rounding]

    // handle NaNs and rhs Infinity (lhs infinity is harder)
    if (SPECIALARGS) {
      if (decNumberIsNaN(lhs) || decNumberIsNaN(rhs)) { // NaNs
        decNaNs(res, lhs, rhs, &status);
        break;}
      if (decNumberIsInfinite(rhs)) {   // rhs Infinity
        Flag rhsneg=rhs->bits&DECNEG;   // save rhs sign
        if (decNumberIsNegative(lhs)    // lhs<0
         && !decNumberIsZero(lhs))      // ..
          status|=DEC_Invalid_operation;
         else {                         // lhs >=0
................................................................................
          }
        if (i==31) break;          // that was the last bit
        if (!seenbit) continue;    // no need to square 1
        decMultiplyOp(dac, dac, dac, &aset, &status); // dac=dac*dac [square]
        } /*i*/ // 32 bits

      // complete internal overflow or underflow processing
      if (status & (DEC_Overflow|DEC_Subnormal)) {
        #if DECSUBSET
        // If subset, and power was negative, reverse the kind of -erflow
        // [1/x not yet done]
        if (!set->extended && decNumberIsNegative(rhs)) {
          if (status & DEC_Overflow)
            status^=DEC_Overflow | DEC_Underflow | DEC_Subnormal;
           else { // trickier -- Underflow may or may not be set
................................................................................
      #endif
      } // rhs integer path

    // reduce result to the requested length and copy to result
    decCopyFit(res, dac, set, &residue, &status);
    decFinish(res, set, &residue, &status);  // final cleanup
    #if DECSUBSET
    if (!set->extended) decTrim(res, 0, &dropped); // trailing zeros
    #endif
    } while(0);                         // end protected

  if (allocdac!=NULL) free(allocdac);   // drop any storage used
  if (allocinv!=NULL) free(allocinv);   // ..
  #if DECSUBSET
  if (alloclhs!=NULL) free(alloclhs);   // ..
................................................................................
  decDivideOp(res, lhs, rhs, set, REMNEAR, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberRemainderNear










































































































































/* ------------------------------------------------------------------ */
/* decNumberSameQuantum -- test for equal exponents                   */
/*                                                                    */
/*   res is the result number, which will contain either 0 or 1       */
/*   lhs is a number to test                                          */
/*   rhs is the second (usually a pattern)                            */
................................................................................
    }
   else if (lhs->exponent==rhs->exponent) ret=1;

  decNumberZero(res);              // OK to overwrite an operand now
  *res->lsu=ret;
  return res;
  } // decNumberSameQuantum


































































































































/* ------------------------------------------------------------------ */
/* decNumberSquareRoot -- square root operator                        */
/*                                                                    */
/*   This computes C = squareroot(A)                                  */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
................................................................................

    // handle infinities and NaNs
    if (SPECIALARG) {
      if (decNumberIsInfinite(rhs)) {         // an infinity
        if (decNumberIsNegative(rhs)) status|=DEC_Invalid_operation;
         else decNumberCopy(res, rhs);        // +Infinity
        }
       else decNaNs(res, rhs, NULL, &status); // a NaN
      break;
      }

    // calculate the ideal (preferred) exponent [floor(exp/2)]
    // [We would like to write: ideal=rhs->exponent>>1, but this
    // generates a compiler warning.  Generated code is the same.]
    ideal=(rhs->exponent&~1)/2;         // target
................................................................................

    // Process Subnormals
    decFinalize(a, set, &residue, &status);

    // count droppable zeros [after any subnormal rounding] by
    // trimming a copy
    decNumberCopy(b, a);
    decTrim(b, 1, &dropped);                 // [drops trailing zeros]

    // Finally set Inexact and Rounded.  The answer can only be exact if
    // it is short enough so that squaring it could fit in set->digits,
    // so this is the only (relatively rare) time a careful check is
    // needed
    if (b->digits*2-1 > set->digits) {       // cannot fit
      status|=DEC_Inexact|DEC_Rounded;
................................................................................
            }
          }
        }
      }

    // make sure there is a full complement of digits for normal
    // inexact results
    if ((status & (DEC_Inexact|DEC_Subnormal))==DEC_Inexact) {

      Int shift=set->digits-a->digits;
      if (shift>0) {
        a->digits=decShiftToMost(a->lsu, a->digits, shift);
        a->exponent-=shift;                  // adjust the exponent.
        }
      }
    decNumberCopy(res, a);                   // a is now the result
................................................................................
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberSubtract

/* ------------------------------------------------------------------ */

/* decNumberToIntegralValue -- round-to-integral-value                */
/*                                                                    */
/*   res is the result                                                */
/*   rhs is input number                                              */
/*   set is the context                                               */
/*                                                                    */
/* res must have space for any value of rhs.                          */
/*                                                                    */
/* This implements the IEEE special operator and therefore treats     */
/* special values as valid, and also never sets Inexact.  For finite  */
/* numbers it returns rescale(rhs, 0) if rhs->exponent is <0.         */
/* Otherwise the result is rhs (so no error is possible).             */

/*                                                                    */
/* The context is used for rounding mode and status after sNaN, but   */


/* the digits setting is ignored.                                     */
/* ------------------------------------------------------------------ */
decNumber * decNumberToIntegralValue(decNumber *res, const decNumber *rhs,
                                     decContext *set) {
  decNumber dn;
  decContext workset;              // working context


  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
  #endif

  // handle infinities and NaNs
  if (SPECIALARG) {
    uInt status=0;
    if (decNumberIsInfinite(rhs)) decNumberCopy(res, rhs); // an Infinity
     else decNaNs(res, rhs, NULL, &status); // a NaN
    if (status!=0) decStatus(res, status, set);
    return res;
    }


  // have a finite number; no error possible (res must be big enough)
  if (rhs->exponent>=0) return decNumberCopy(res, rhs);
  // that was easy, but if negative exponent there is work to do...
  workset=*set;                    // clone rounding, etc.
  workset.digits=rhs->digits;      // no length rounding
  workset.traps=0;                 // no traps
  decNumberZero(&dn);              // make a number with exponent 0
  return decNumberQuantize(res, rhs, &dn, &workset);















  } // decNumberToIntegralValue







































































/* ================================================================== */
/* Utility routines                                                   */
/* ================================================================== */

















































/* ------------------------------------------------------------------ */
/* decNumberCopy -- copy a number                                     */
/*                                                                    */
/*   dest is the target decNumber                                     */
/*   src  is the source decNumber                                     */
/*   returns dest                                                     */
................................................................................
    // overlap.  However, this explicit loop is faster in short cases.
    d=dest->lsu+1;                           // -> first destination
    smsup=src->lsu+D2U(src->digits);         // -> source msu+1
    for (s=src->lsu+1; s<smsup; s++, d++) *d=*s;
    }
  return dest;
  } // decNumberCopy

/* ------------------------------------------------------------------ */










































































































































































/* decNumberTrim -- remove insignificant zeros                        */
/*                                                                    */
/*   dn is the number to trim                                         */
/*   returns dn                                                       */
/*                                                                    */
/* All fields are updated as required.  This is a utility operation,  */
/* so special values are unchanged and no error is possible.          */
/* ------------------------------------------------------------------ */
decNumber * decNumberTrim(decNumber *dn) {
  Int  dropped;                    // work





  return decTrim(dn, 0, &dropped);
  } // decNumberTrim

/* ------------------------------------------------------------------ */
/* decNumberVersion -- return the name and version of this module     */
/*                                                                    */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
................................................................................
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*   negate is DECNEG if rhs should be negated, or 0 otherwise        */
/*   status accumulates status for the caller                         */
/*                                                                    */
/* C must have space for set->digits digits.                          */

/* ------------------------------------------------------------------ */
/* If possible, the coefficient is calculated directly into C.        */
/* However, if:                                                       */
/*   -- a digits+1 calculation is needed because the numbers are      */
/*      unaligned and span more than set->digits digits               */
/*   -- a carry to digits+1 digits looks possible                     */
/*   -- C is the same as A or B, and the result would destructively   */
/*      overlap the A or B coefficient                                */
/* then the result must be calculated into a temporary buffer.  In    */
/* this case a local (stack) buffer is used if possible, and only if  */
/* too long for that does malloc become the last resort.              */
/*                                                                    */
/* Misalignment is handled as follows:                                */
/*   Apad: (AExp>BExp) Swap operands and proceed as for BExp>AExp.    */
/*   BPad: Apply the padding by a combination of shifting (whole      */
/*         units) and multiplication (part units).                    */
/*                                                                    */
/* Addition, especially x=x+1, is speed-critical.                     */


/* ------------------------------------------------------------------ */
static decNumber * decAddOp(decNumber *res, const decNumber *lhs,
                            const decNumber *rhs, decContext *set,
                            uByte negate, uInt *status) {
  #if DECSUBSET
  decNumber *alloclhs=NULL;        // non-NULL if rounded lhs allocated
  decNumber *allocrhs=NULL;        // .., rhs
................................................................................
  Int   rhsshift;                  // working shift (in Units)
  Int   maxdigits;                 // longest logical length
  Int   mult;                      // multiplier
  Int   residue;                   // rounding accumulator
  uByte bits;                      // result bits
  Flag  diffsign;                  // non-0 if arguments have different sign
  Unit  *acc;                      // accumulator for result
  Unit  accbuff[SD2U(DECBUFFER+20)]; // local buffer [+20 reduces many
                                   // allocations when called from
                                   // other operations]
  Unit  *allocacc=NULL;            // -> allocated acc buffer, iff allocated
  Int   reqdigits=set->digits;     // local copy; requested DIGITS
  Int   padding;                   // work

  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  #endif
................................................................................

    // note whether signs differ [used all paths]
    diffsign=(Flag)((lhs->bits^rhs->bits^negate)&DECNEG);

    // handle infinities and NaNs
    if (SPECIALARGS) {                  // a special bit set
      if (SPECIALARGS & (DECSNAN | DECNAN))  // a NaN
        decNaNs(res, lhs, rhs, status);
       else { // one or two infinities
        if (decNumberIsInfinite(lhs)) { // LHS is infinity
          // two infinities with different signs is invalid
          if (decNumberIsInfinite(rhs) && diffsign) {
            *status|=DEC_Invalid_operation;
            break;
            }
................................................................................
          res->exponent+=adjust;                // set the exponent.
          }
      #if DECSUBSET
        } // extended
      #endif
      decFinish(res, set, &residue, status);      // clean and finalize
      break;}

    // [NB: both fastpath and mainpath code below assume these cases
    // (notably 0-0) have already been handled]

    // calculate the padding needed to align the operands
    padding=rhs->exponent-lhs->exponent;

    // Fastpath cases where the numbers are aligned and normal, the RHS
    // is all in one unit, no operand rounding is needed, and no carry,
    // lengthening, or borrow is needed
    if (padding==0
        && rhs->digits<=DECDPUN
        && rhs->exponent>=set->emin     // [some normals drop through]

        && rhs->digits<=reqdigits
        && lhs->digits<=reqdigits) {
      Int partial=*lhs->lsu;
      if (!diffsign) {                  // adding
        partial+=*rhs->lsu;
        if ((partial<=DECDPUNMAX)       // result fits in unit
         && (lhs->digits>=DECDPUN ||    // .. and no digits-count change
................................................................................
    // other) padding with up to DIGITS-1 trailing zeros may be
    // needed; then apply rounding (as exotic rounding modes may be
    // affected by the residue).
    rhsshift=0;               // rhs shift to left (padding) in Units
    bits=lhs->bits;           // assume sign is that of LHS
    mult=1;                   // likely multiplier

    // if padding==0 the operands are aligned; no padding needed
    if (padding!=0) {
      // some padding needed; always pad the RHS, as any required
      // padding can then be effected by a simple combination of
      // shifts and a multiply
      Flag swapped=0;
      if (padding<0) {                  // LHS needs the padding
        const decNumber *t;
................................................................................
    if ((maxdigits>=reqdigits)          // is, or could be, too large
     || (res==rhs && rhsshift>0)) {     // destructive overlap
      // buffer needed, choose it; units for maxdigits digits will be
      // needed, +1 Unit for carry or borrow
      Int need=D2U(maxdigits)+1;
      acc=accbuff;                      // assume use local buffer
      if (need*sizeof(Unit)>sizeof(accbuff)) {

        allocacc=(Unit *)malloc(need*sizeof(Unit));
        if (allocacc==NULL) {           // hopeless -- abandon
          *status|=DEC_Insufficient_storage;
          break;}
        acc=allocacc;
        }
      }
................................................................................

    res->bits=(uByte)(bits&DECNEG);     // it's now safe to overwrite..
    res->exponent=lhs->exponent;        // .. operands (even if aliased)

    #if DECTRACE
      decDumpAr('A', lhs->lsu, D2U(lhs->digits));
      decDumpAr('B', rhs->lsu, D2U(rhs->digits));
      printf("  :h: %d %d\n", rhsshift, mult);
    #endif

    // add [A+B*m] or subtract [A+B*(-m)]
    res->digits=decUnitAddSub(lhs->lsu, D2U(lhs->digits),
                              rhs->lsu, D2U(rhs->digits),
                              rhsshift, acc, mult)
               *DECDPUN;           // [units -> digits]
................................................................................
    // apply checks and rounding
    decFinish(res, set, &residue, status);

    // "When the sum of two operands with opposite signs is exactly
    // zero, the sign of that sum shall be '+' in all rounding modes
    // except round toward -Infinity, in which mode that sign shall be
    // '-'."  [Subset zeros also never have '-', set by decFinish.]
    if (ISZERO(res)
     && diffsign
     #if DECSUBSET
     && set->extended
     #endif
     && (*status&DEC_Inexact)==0) {
      if (set->round==DEC_ROUND_FLOOR) res->bits|=DECNEG;   // sign -
                                  else res->bits&=~DECNEG;  // sign +
      }
................................................................................
/*     Return (Result is defined by Var1)                             */
/*                                                                    */
/* ------------------------------------------------------------------ */
/* Two working buffers are needed during the division; one (digits+   */
/* 1) to accumulate the result, and the other (up to 2*digits+1) for  */
/* long subtractions.  These are acc and var1 respectively.           */
/* var1 is a copy of the lhs coefficient, var2 is the rhs coefficient.*/


/* ------------------------------------------------------------------ */
static decNumber * decDivideOp(decNumber *res,
                               const decNumber *lhs, const decNumber *rhs,
                               decContext *set, Flag op, uInt *status) {
  #if DECSUBSET
  decNumber *alloclhs=NULL;        // non-NULL if rounded lhs allocated
  decNumber *allocrhs=NULL;        // .., rhs
  #endif
  Unit  accbuff[SD2U(DECBUFFER+DECDPUN)]; // local buffer
  Unit  *acc=accbuff;              // -> accumulator array for result
  Unit  *allocacc=NULL;            // -> allocated buffer, iff allocated
  Unit  *accnext;                  // -> where next digit will go
  Int   acclength;                 // length of acc needed [Units]
  Int   accunits;                  // count of units accumulated
  Int   accdigits;                 // count of digits accumulated

................................................................................
    // [following code does not require input rounding]

    bits=(lhs->bits^rhs->bits)&DECNEG;  // assumed sign for divisions

    // handle infinities and NaNs
    if (SPECIALARGS) {                  // a special bit set
      if (SPECIALARGS & (DECSNAN | DECNAN)) { // one or two NaNs
        decNaNs(res, lhs, rhs, status);
        break;
        }
      // one or two infinities
      if (decNumberIsInfinite(lhs)) {   // LHS (dividend) is infinite
        if (decNumberIsInfinite(rhs) || // two infinities are invalid ..
            op & (REMAINDER | REMNEAR)) { // as is remainder of infinity
          *status|=DEC_Invalid_operation;
................................................................................

    /* Long (slow) division is needed; roll up the sleeves... */

    // The accumulator will hold the quotient of the division.
    // If it needs to be too long for stack storage, then allocate.
    acclength=D2U(reqdigits+DECDPUN);   // in Units
    if (acclength*sizeof(Unit)>sizeof(accbuff)) {

      allocacc=(Unit *)malloc(acclength*sizeof(Unit));
      if (allocacc==NULL) {             // hopeless -- abandon
        *status|=DEC_Insufficient_storage;
        break;}
      acc=allocacc;                     // use the allocated space
      }

................................................................................
    // [Note: unused units do not participate in decUnitAddSub data]
    maxdigits=rhs->digits+reqdigits-1;
    if (lhs->digits>maxdigits) maxdigits=lhs->digits;
    var1units=D2U(maxdigits)+2;
    // allocate a guard unit above msu1 for REMAINDERNEAR
    if (!(op&DIVIDE)) var1units++;
    if ((var1units+1)*sizeof(Unit)>sizeof(varbuff)) {

      varalloc=(Unit *)malloc((var1units+1)*sizeof(Unit));
      if (varalloc==NULL) {             // hopeless -- abandon
        *status|=DEC_Insufficient_storage;
        break;}
      var1=varalloc;                    // use the allocated space
      }

................................................................................
        thisunit=(Unit)(thisunit+mult);      // accumulate
        // subtract var1-var2, into var1; only the overlap needs
        // processing, as this is an in-place calculation
        shift=var2ulen-var2units;
        #if DECTRACE
          decDumpAr('1', &var1[shift], var1units-shift);
          decDumpAr('2', var2, var2units);
          printf("m=%d\n", -mult);
        #endif
        decUnitAddSub(&var1[shift], var1units-shift,
                      var2, var2units, 0,
                      &var1[shift], -mult);
        #if DECTRACE
          decDumpAr('#', &var1[shift], var1units-shift);
        #endif
................................................................................
          Int exp=lhs->exponent;             // save min(exponents)
          if (rhs->exponent<exp) exp=rhs->exponent;
          decNumberZero(res);                // 0 coefficient
          #if DECSUBSET
          if (set->extended)
          #endif
          res->exponent=exp;                 // .. with proper exponent

          break;
          }
        // note if the quotient was odd
        if (*accnext & 0x01) wasodd=1;       // acc is odd
        quotlsu=accnext;                     // save in case need to reinspect
        quotdigits=accdigits;                // ..

................................................................................

        // Now correct the result if doing remainderNear; if it
        // (looking just at coefficients) is > rhs/2, or == rhs/2 and
        // the integer was odd then the result should be rem-rhs.
        if (op&REMNEAR) {
          Int compare, tarunits;        // work
          Unit *up;                     // ..


          // calculate remainder*2 into the var1 buffer (which has
          // 'headroom' of an extra unit and hence enough space)
          // [a dedicated 'double' loop would be faster, here]
          tarunits=decUnitAddSub(accnext, accunits, accnext, accunits,
                                 0, accnext, 1);
          // decDumpAr('r', accnext, tarunits);

................................................................................
    // Now the coefficient.
    decSetCoeff(res, set, accnext, accdigits, &residue, status);

    decFinish(res, set, &residue, status);   // final cleanup

    #if DECSUBSET
    // If a divide then strip trailing zeros if subset [after round]
    if (!set->extended && (op==DIVIDE)) decTrim(res, 0, &dropped);
    #endif
    } while(0);                              // end protected

  if (varalloc!=NULL) free(varalloc);   // drop any storage used
  if (allocacc!=NULL) free(allocacc);   // ..
  #if DECSUBSET
  if (allocrhs!=NULL) free(allocrhs);   // ..
................................................................................
/* units for continuing processing.  Despite this overhead, the       */
/* fastpath can speed up some 16-digit operations by 10x (and much    */
/* more for higher-precision calculations).                           */
/*                                                                    */
/* A buffer always has to be used for the accumulator; in the         */
/* fastpath, buffers are also always needed for the chunked copies of */
/* of the operand coefficients.                                       */


/* ------------------------------------------------------------------ */
#define FASTMUL (DECUSE64 && DECDPUN<5)
static decNumber * decMultiplyOp(decNumber *res, const decNumber *lhs,
                                 const decNumber *rhs, decContext *set,
                                 uInt *status) {
  Int    accunits;                 // Units of accumulator in use
  Int    exponent;                 // work
  Int    residue=0;                // rounding residue
  uByte  bits;                     // result sign
  Unit  *acc;                      // -> accumulator Unit array
  Int    needbytes;                // size calculator
  void  *allocacc=NULL;            // -> allocated accumulator, iff allocated
  Unit accbuff[SD2U(DECBUFFER*2+5)]; // buffer (+1 for DECBUFFER==0,
                                   // + 4 for calls from other operations)
  const Unit *mer, *mermsup;       // work
  Int  madlength;                  // Units in multiplicand
  Int  shift;                      // Units to shift multiplicand by

  #if FASTMUL
    // if DECDPUN is 1 or 3 work in base 10**9, otherwise
    // (DECDPUN is 2 or 4) then work in base 10**8
    #if DECDPUN & 1                // odd
      #define FASTBASE 1000000000  // base
      #define FASTDIGS          9  // digits in base
................................................................................
      #define FASTBASE  100000000
      #define FASTDIGS          8
      #define FASTLAZY       1844  // carry resolution point [1->1844]
    #endif
    // three buffers are used, two for chunked copies of the operands
    // (base 10**8 or base 10**9) and one base 2**64 accumulator with
    // lazy carry evaluation
    uInt   zlhibuff[(DECBUFFER+7)/8+1]; // buffer (+1 for DECBUFFER==0)
    uInt  *zlhi=zlhibuff;               // -> lhs array
    uInt  *alloclhi=NULL;               // -> allocated buffer, iff allocated
    uInt   zrhibuff[(DECBUFFER+7)/8+1]; // buffer (+1 for DECBUFFER==0)
    uInt  *zrhi=zrhibuff;               // -> rhs array
    uInt  *allocrhi=NULL;               // -> allocated buffer, iff allocated
    uLong  zaccbuff[(DECBUFFER+3)/4+2]; // buffer (+1 for DECBUFFER==0)
                                        // + 1 for calls from other operations)
    // [allocacc is shared for both paths, as only one will run]
    uLong *zacc=zaccbuff;          // -> accumulator array for exact result
    #if DECDPUN==1
    Int    zoff;                   // accumulator offset
    #endif
    uInt  *lip, *rip;              // item pointers
    uInt  *lmsi, *rmsi;            // most significant items
................................................................................

  // precalculate result sign
  bits=(uByte)((lhs->bits^rhs->bits)&DECNEG);

  // handle infinities and NaNs
  if (SPECIALARGS) {               // a special bit set
    if (SPECIALARGS & (DECSNAN | DECNAN)) { // one or two NaNs
      decNaNs(res, lhs, rhs, status);
      return res;}
    // one or two infinities; Infinity * 0 is invalid
    if (((lhs->bits & DECINF)==0 && ISZERO(lhs))
      ||((rhs->bits & DECINF)==0 && ISZERO(rhs))) {
      *status|=DEC_Invalid_operation;
      return res;}
    decNumberZero(res);
................................................................................
    // both their magnitudes are large.  If there was a wrap, set a
    // safe very negative exponent, from which decFinalize() will
    // raise a hard underflow shortly.
    exponent=lhs->exponent+rhs->exponent;    // calculate exponent
    if (lhs->exponent<0 && rhs->exponent<0 && exponent>0)
      exponent=-2*DECNUMMAXE;                // force underflow
    res->exponent=exponent;                  // OK to overwrite now


    // Set the coefficient.  If any rounding, residue records
    decSetCoeff(res, set, acc, res->digits, &residue, status);
    decFinish(res, set, &residue, status);   // final cleanup
    } while(0);                         // end protected

  if (allocacc!=NULL) free(allocacc);   // drop any storage used
................................................................................
/*    which dominates when the number of iterations is small (less    */
/*    than ten) or when rhs is short.  As an example, the adjustment  */
/*    x**10,000,000 needs 31 multiplications, all but one full-width. */
/*                                                                    */
/* 3. The restrictions (especially precision) could be raised with    */
/*    care, but the full decNumber range seems very hard within the   */
/*    32-bit limits.                                                  */



/* ------------------------------------------------------------------ */
decNumber * decExpOp(decNumber *res, const decNumber *rhs,
                         decContext *set, uInt *status) {
  uInt ignore=0;                   // working status
  Int h;                           // adjusted exponent for 0.xxxx
  Int p;                           // working precision
  Int residue;                     // rounding residue
  uInt needbytes;                  // for space calculations
  const decNumber *x=rhs;          // (may point to safe copy later)
  decContext aset, tset, dset;     // working contexts


  // the argument is often copied to normalize it, so (unusually) it
  // is treated like other buffers, using DECBUFFER, +1 in case
  // DECBUFFER is 0
  decNumber bufr[D2N(DECBUFFER+1)];
  decNumber *allocrhs=NULL;        // non-NULL if rhs buffer allocated

  // the working precision will be no more than set->digits+8+1
  // so for on-stack buffers DECBUFFER+9 is used, +1 in case DECBUFFER
  // is 0 (and twice that for the accumulator)

  // buffer for t, term (working precision plus)
  decNumber buft[D2N(DECBUFFER+9+1)];
  decNumber *allocbuft=NULL;       // -> allocated buft, iff allocated
  decNumber *t=buft;               // term
  // buffer for a, accumulator (working precision * 2), at least 9
  decNumber bufa[D2N(DECBUFFER*2+18+1)];
  decNumber *allocbufa=NULL;       // -> allocated bufa, iff allocated
  decNumber *a=bufa;               // accumulator
  // decNumber for the divisor term; this needs at most 9 digits
  // and so can be fixed size [16 so can use standard context]
  decNumber bufd[D2N(16)];
  decNumber *d=bufd;               // divisor
  decNumber numone;                // constant 1
................................................................................
  do {                                  // protect allocated storage
    if (SPECIALARG) {                   // handle infinities and NaNs
      if (decNumberIsInfinite(rhs)) {   // an infinity
        if (decNumberIsNegative(rhs))   // -Infinity -> +0
          decNumberZero(res);
         else decNumberCopy(res, rhs);  // +Infinity -> self
        }
       else decNaNs(res, rhs, NULL, status); // a NaN
      break;}

    if (ISZERO(rhs)) {                  // zeros -> exact 1
      decNumberZero(res);               // make clean 1
      *res->lsu=1;                      // ..
      break;}                           // [no status to set]

................................................................................
    // 1.0000001.  Hence, tiny will be 0.0000004  if digits=7 and x>0
    // or 0.00000004 if digits=7 and x<0.  If RHS not larger than
    // this then the result will be 1.000000
    decNumberZero(d);                   // clean
    *d->lsu=4;                          // set 4 ..
    d->exponent=-set->digits;           // * 10**(-d)
    if (decNumberIsNegative(rhs)) d->exponent--;  // negative case
    if (decCompare(d, rhs, 1)>=0) {     // signless compare




      Int shift=set->digits-1;
      decNumberZero(res);               // set 1
      *res->lsu=1;                      // ..
      res->digits=decShiftToMost(res->lsu, 1, shift);
      res->exponent=-shift;                  // make 1.0000...
      *status|=DEC_Inexact | DEC_Rounded;    // .. inexactly
      break;} // tiny
................................................................................
      // a=1, and the divisor d=2.

      // First determine the working precision.  From Hull & Abrham
      // this is set->digits+h+2.  However, if x is 'over-precise' we
      // need to allow for all its digits to potentially participate
      // (consider an x where all the excess digits are 9s) so in
      // this case use x->digits+h+2
      p=MAX(x->digits, set->digits)+h+2;

      // a and t are variable precision, and depend on p, so space
      // must be allocated for them if necessary

      // the accumulator needs to be able to hold 2p digits so that
      // the additions on the second and subsequent iterations are
      // sufficiently exact.
................................................................................
            && (a->digits>=p)) break;
        decAddOp(d, d, &numone, &dset, 0, &ignore);    // d=d+1
        } // iterate

      #if DECCHECK
      // just a sanity check; comment out test to show always
      if (iterations>p+3)
        printf("Exp iterations=%d, status=%08x, p=%d, d=%d\n",
               iterations, *status, p, x->digits);
      #endif
      } // h<=8

    // apply postconditioning: a=a**(10**h) -- this is calculated
    // at a slightly higher precision than Hull & Abrham suggest
    if (h>0) {
................................................................................
/* 4. An iteration might be saved by widening the LNnn table, and     */
/*    would certainly save at least one if it were made ten times     */
/*    bigger, too (for truncated fractions 0.100 through 0.999).      */
/*    However, for most practical evaluations, at least four or five  */
/*    iterations will be neede -- so this would only speed up by      */
/*    20-25% and that probably does not justify increasing the table  */
/*    size.                                                           */



/* ------------------------------------------------------------------ */
decNumber * decLnOp(decNumber *res, const decNumber *rhs,
                    decContext *set, uInt *status) {
  uInt ignore=0;                   // working status accumulator
  uInt needbytes;                  // for space calculations
  Int residue;                     // rounding residue
  Int r;                           // rhs=f*10**r [see below]
  Int p;                           // working precision
  Int pp;                          // precision for iteration
  Int t;                           // work

  // buffers for a (accumulator, typically precision+2) and b
  // (adjustment calculator, same size)
  decNumber bufa[D2N(DECBUFFER+2)];
  decNumber *allocbufa=NULL;       // -> allocated bufa, iff allocated
  decNumber *a=bufa;               // accumulator/work
  decNumber bufb[D2N(DECBUFFER+2)];
  decNumber *allocbufb=NULL;       // -> allocated bufa, iff allocated
  decNumber *b=bufb;               // adjustment/work

  decNumber  numone;               // constant 1
  decNumber  cmp;                  // work
  decContext aset, bset;           // working contexts

................................................................................
  do {                                  // protect allocated storage
    if (SPECIALARG) {                   // handle infinities and NaNs
      if (decNumberIsInfinite(rhs)) {   // an infinity
        if (decNumberIsNegative(rhs))   // -Infinity -> error
          *status|=DEC_Invalid_operation;
         else decNumberCopy(res, rhs);  // +Infinity -> self
        }
       else decNaNs(res, rhs, NULL, status); // a NaN
      break;}

    if (ISZERO(rhs)) {                  // +/- zeros -> -Infinity
      decNumberZero(res);               // make clean
      res->bits=DECINF|DECNEG;          // set - infinity
      break;}                           // [no status to set]

................................................................................
    //   ln(x) = ln(f) + ln(10)*r
    // Get the initial estimate for ln(f) from a small lookup
    // table (see above) indexed by the first two digits of f,
    // truncated.

    decContextDefault(&aset, DEC_INIT_DECIMAL64); // 16-digit extended
    r=rhs->exponent+rhs->digits;        // 'normalised' exponent
    decPutInt(a, r);                    // a=r
    decPutInt(b, 2302585);              // b=ln(10) (2.302585)
    b->exponent=-6;                     //  ..
    decMultiplyOp(a, a, b, &aset, &ignore);  // a=a*b
    // now get top two digits of rhs into b by simple truncate and
    // force to integer
    residue=0;                          // (no residue)
    aset.digits=2; aset.round=DEC_ROUND_DOWN;
    decCopyFit(b, rhs, &aset, &residue, &ignore); // copy & shorten
    b->exponent=0;                      // make integer
    t=decGetInt(b);                     // [cannot fail]
    if (t<10) t=X10(t);                 // adjust single-digit b
    t=LNnn[t-10];                       // look up ln(b)
    decPutInt(b, t>>2);                 // b=ln(b) coefficient
    b->exponent=-(t&3)-3;               // set exponent
    b->bits=DECNEG;                     // ln(0.10)->ln(0.99) always -ve
    aset.digits=16; aset.round=DEC_ROUND_HALF_EVEN; // restore
    decAddOp(a, a, b, &aset, 0, &ignore); // acc=a+b
    // the initial estimate is now in a, with up to 4 digits correct.
    // When rhs is at or near Nmax the estimate will be low, so we
    // will approach it from below, avoiding overflow when calling exp.
................................................................................
      aset.digits=pp;                        // working context
      bset.digits=pp+rhs->digits;            // wider context
      } // Newton's iteration

    #if DECCHECK
    // just a sanity check; remove the test to show always
    if (iterations>24)
      printf("Ln iterations=%d, status=%08x, p=%d, d=%d\n",
            iterations, *status, p, rhs->digits);
    #endif

    // Copy and round the result to res
    residue=1;                          // indicate dirt to right
    if (ISZERO(a)) residue=0;           // .. unless underflowed to 0
    aset.digits=set->digits;            // [use default rounding]
................................................................................
    #endif
    // [following code does not require input rounding]

    // Handle special values
    if (SPECIALARGS) {
      // NaNs get usual processing
      if (SPECIALARGS & (DECSNAN | DECNAN))
        decNaNs(res, lhs, rhs, status);
      // one infinity but not both is bad
      else if ((lhs->bits ^ rhs->bits) & DECINF)
        *status|=DEC_Invalid_operation;
      // both infinity: return lhs
      else decNumberCopy(res, lhs);          // [nop if in place]
      break;
      }
................................................................................
  return res;
  } // decQuantizeOp

/* ------------------------------------------------------------------ */
/* decCompareOp -- compare, min, or max two Numbers                   */
/*                                                                    */
/*   This computes C = A ? B and carries out one of four operations:  */
/*     COMPARE   -- returns the signum (as a number) giving the       */
/*                  result of a comparison unless one or both         */
/*                  operands is a NaN (in which case a NaN results)   */


/*     COMPMAX   -- returns the larger of the operands, using the     */
/*                  754r maxnum operation                             */

/*     COMPMIN   -- the 754r minnum operation                         */

/*     COMTOTAL  -- returns the signum (as a number) giving the       */
/*                  result of a comparison using 754r total ordering  */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*   op  is the operation flag                                        */
/*   status is the usual accumulator                                  */
/*                                                                    */
/* C must have space for one digit for COMPARE or set->digits for     */
/* COMPMAX and COMPMIN.                                               */
/* ------------------------------------------------------------------ */
/* The emphasis here is on speed for common cases, and avoiding       */
/* coefficient comparison if possible.                                */
/* ------------------------------------------------------------------ */
decNumber * decCompareOp(decNumber *res, const decNumber *lhs,
                         const decNumber *rhs, decContext *set,
                         Flag op, uInt *status) {
................................................................................
        rhs=allocrhs;
        }
      }
    #endif
    // [following code does not require input rounding]

    // If total ordering then handle differing signs 'up front'
    if (op == COMPTOTAL) {              // total ordering
      if (decNumberIsNegative(lhs) & !decNumberIsNegative(rhs)) {
        result=-1;
        break;
        }
      if (!decNumberIsNegative(lhs) & decNumberIsNegative(rhs)) {
        result=+1;
        break;
................................................................................
        }
      }

    // handle NaNs specially; let infinities drop through
    // This assumes sNaN (even just one) leads to NaN.
    merged=(lhs->bits | rhs->bits) & (DECSNAN | DECNAN);
    if (merged) {                       // a NaN bit set
      if (op == COMPARE);               // result will be NaN


       else if (op == COMPTOTAL) {      // total ordering, always finite
        // signs are known to be the same; compute the ordering here
        // as if the signs are both positive, then invert for negatives
        if (!decNumberIsNaN(lhs)) result=-1;
         else if (!decNumberIsNaN(rhs)) result=+1;
         // here if both NaNs
         else if (decNumberIsSNaN(lhs) && decNumberIsQNaN(rhs)) result=-1;
         else if (decNumberIsQNaN(lhs) && decNumberIsSNaN(rhs)) result=+1;
................................................................................
                                rhs->lsu, D2U(rhs->digits), 0);
          // [Error not possible, as these are 'aligned']
          } // both same NaNs
        if (decNumberIsNegative(lhs)) result=-result;
        break;
        } // total order

       else if (merged & DECSNAN);      // sNaN -> qNaN
       else { // here if MIN or MAX and one or two quiet NaNs
        // min or max -- 754r rules ignore single NaN
        if (!decNumberIsNaN(lhs) || !decNumberIsNaN(rhs)) {
          // just one NaN; force choice to be the non-NaN operand
          op=COMPMAX;
          if (lhs->bits & DECNAN) result=-1; // pick rhs
                             else result=+1; // pick lhs
          break;
          }
        } // max or min
      op = COMPNAN;                     // use special path
      decNaNs(res, lhs, rhs, status);
      break;
      }


    result=decCompare(lhs, rhs, 0);     // have numbers
    } while(0);                         // end protected

  if (result==BADINT) *status|=DEC_Insufficient_storage; // rare
   else {
    if (op == COMPARE || op == COMPTOTAL) {  // returning signum
      if (op == COMPTOTAL && result==0) {
        // operands are numerically equal or same NaN (and same sign,
        // tested first); if identical, leave result 0
        if (lhs->exponent!=rhs->exponent) {
          if (lhs->exponent<rhs->exponent) result=-1;
           else result=+1;
          if (decNumberIsNegative(lhs)) result=-result;
          } // lexp!=rexp
................................................................................
        } // total-order by exponent
      decNumberZero(res);               // [always a valid result]
      if (result!=0) {                  // must be -1 or +1
        *res->lsu=1;
        if (result<0) res->bits=DECNEG;
        }
      }
     else if (op == COMPNAN);           // special, drop through
     else {                             // MAX or MIN, non-NaN result
      Int residue=0;                    // rounding accumulator
      // choose the operand for the result
      const decNumber *choice;
      if (result==0) { // operands are numerically equal
        // choose according to sign then exponent (see 754r)
        uByte slhs=(lhs->bits & DECNEG);
................................................................................
          }
         else {                    // both positive
          if (lhs->exponent>rhs->exponent) result=+1;
                                      else result=-1;
          // [ditto]
          }
        } // numerically equal
      // here result will be non-0
      if (op == COMPMIN) result=-result;// reverse if looking for MIN
      choice=(result>0 ? lhs : rhs);    // choose
      // copy chosen to result, rounding if need be
      decCopyFit(res, choice, set, &residue, status);
      decFinish(res, set, &residue, status);
      }
    }
  #if DECSUBSET
................................................................................
  Int   result;                    // result value
  Int   sigr;                      // rhs signum
  Int   compare;                   // work

  result=1;                                  // assume signum(lhs)
  if (ISZERO(lhs)) result=0;
  if (abs) {
    if (!ISZERO(rhs)) result=1;              // not both 0



    }
   else {                                    // signs matter
    if (result && decNumberIsNegative(lhs)) result=-1;
    sigr=1;                                  // compute signum(rhs)
    if (ISZERO(rhs)) sigr=0;
     else if (decNumberIsNegative(rhs)) sigr=-1;
    if (result > sigr) return +1;            // L > R, return 1
    if (result < sigr) return -1;            // R < L, return -1

    }

  // signums are the same
  if (result==0) return 0;                   // both 0
  // Both non-zero
  if ((lhs->bits | rhs->bits) & DECINF) {    // one or more infinities
    if (decNumberIsInfinite(rhs)) {
      if (decNumberIsInfinite(lhs)) result=0;// both infinite
       else result=-result;                  // only rhs infinite
      }
    return result;
    }

  // must compare the coefficients, allowing for exponents
  if (lhs->exponent>rhs->exponent) {         // LHS exponent larger
    // swap sides, and sign
    const decNumber *temp=lhs;
    lhs=rhs;
    rhs=temp;
    result=-result;
    }

  compare=decUnitCompare(lhs->lsu, D2U(lhs->digits),
                         rhs->lsu, D2U(rhs->digits),
                         rhs->exponent-lhs->exponent);

  if (compare!=BADINT) compare*=result;      // comparison succeeded
  return compare;
  } // decCompare

/* ------------------------------------------------------------------ */
/* decUnitCompare -- compare two >=0 integers in Unit arrays          */
/*                                                                    */
................................................................................
/*  returns -1, 0, or 1 for A<B, A==B, or A>B, or BADINT if failure   */
/*  (the only possible failure is an allocation error, which can      */
/*  only occur if E!=0)                                               */
/* ------------------------------------------------------------------ */
static Int decUnitCompare(const Unit *a, Int alength,
                          const Unit *b, Int blength, Int exp) {
  Unit  *acc;                      // accumulator for result
  Unit  accbuff[SD2U(DECBUFFER+1)];// local buffer
  Unit  *allocacc=NULL;            // -> allocated acc buffer, iff allocated
  Int   accunits, need;            // units in use or needed for acc
  const Unit *l, *r, *u;           // work
  Int   expunits, exprem, result;  // ..

  if (exp==0) {                    // aligned; fastpath
    if (alength>blength) return 1;
................................................................................
  Int  add;                        // work
  #if DECDPUN<=4                   // myriadal, millenary, etc.
  Int  est;                        // estimated quotient
  #endif

  #if DECTRACE
  if (alength<1 || blength<1)
    printf("decUnitAddSub: alen blen m %d %d [%d]\n", alength, blength, m);
  #endif

  maxC=c+alength;                  // A is usually the longer
  minC=c+blength;                  // .. and B the shorter
  if (bshift!=0) {                 // B is shifted; low As copy across
    minC+=bshift;
    // if in place [common], skip copy unless there's a gap [rare]
................................................................................
     else {
      *c=0;
      add=1;
      }
    }
  // add an extra unit iff it would be non-zero
  #if DECTRACE
    printf("UAS borrow: add %d, carry %d\n", add, carry);
  #endif
  if ((add-carry-1)!=0) {
    *c=(Unit)(add-carry-1);
    c++;                      // interesting, include it
    }
  return clsu-c;              // -ve result indicates borrowed
  } // decUnitAddSub

/* ------------------------------------------------------------------ */
/* decTrim -- trim trailing zeros or normalize                        */
/*                                                                    */
/*   dn is the number to trim or normalize                            */

/*   all is 1 to remove all trailing zeros, 0 for just fraction ones  */
/*   dropped returns the number of discarded trailing zeros           */
/*   returns dn                                                       */
/*                                                                    */


/* All fields are updated as required.  This is a utility operation,  */
/* so special values are unchanged and no error is possible.          */
/* ------------------------------------------------------------------ */
static decNumber * decTrim(decNumber *dn, Flag all, Int *dropped) {

  Int   d, exp;                    // work
  uInt  cut;                       // ..
  Unit  *up;                       // -> current Unit

  #if DECCHECK
  if (decCheckOperands(dn, DECUNUSED, DECUNUSED, DECUNUSED)) return dn;
  #endif
................................................................................
      }
    cut++;                         // next power
    if (cut>DECDPUN) {             // need new Unit
      up++;
      cut=1;
      }
    } // d
  if (d==0) return dn;             // none dropped








  // effect the drop
  decShiftToLeast(dn->lsu, D2U(dn->digits), d);
  dn->exponent+=d;                 // maintain numerical value
  dn->digits-=d;                   // new length
  *dropped=d;                      // report the count
  return dn;
  } // decTrim





















/* ------------------------------------------------------------------ */
/* decShiftToMost -- shift digits in array towards most significant   */
/*                                                                    */
/*   uar    is the array                                              */
/*   digits is the count of digits in use in the array                */
/*   shift  is the number of zeros to pad with (least significant);   */
................................................................................

/* ------------------------------------------------------------------ */
/* decShiftToLeast -- shift digits in array towards least significant */
/*                                                                    */
/*   uar   is the array                                               */
/*   units is length of the array, in units                           */
/*   shift is the number of digits to remove from the lsu end; it     */
/*     must be zero or positive and less than units*DECDPUN.          */
/*                                                                    */
/*   returns the new length of the integer in the array, in units     */
/*                                                                    */
/* Removed digits are discarded (lost).  Units not required to hold   */
/* the final result are unchanged.                                    */
/* ------------------------------------------------------------------ */
static Int decShiftToLeast(Unit *uar, Int units, Int shift) {
  Unit  *target, *up;              // work
  Int   cut, count;                // work
  Int   quot, rem;                 // for division

  if (shift==0) return units;      // [fastpath] nothing to do





  target=uar;                      // both paths
  cut=MSUDIGITS(shift);
  if (cut==DECDPUN) {              // unit-boundary case; easy
    up=uar+D2U(shift);
    for (; up<uar+units; target++, up++) *target=*up;
    return target-uar;
................................................................................
/*         guard and sticky information.  It may be:                  */
/*         6-9: rounding digit is >5                                  */
/*         5:   rounding digit is exactly half-way                    */
/*         1-4: rounding digit is <5 and >0                           */
/*         0:   the coefficient is exact                              */
/*        -1:   as 1, but the hidden digits are subtractive, that     */
/*              is, of the opposite sign to dn.  In this case the     */
/*              coefficient must be non-0.                            */


/*   status is the status accumulator, as usual                       */
/*                                                                    */
/* This routine applies rounding while keeping the length of the      */
/* coefficient constant.  The exponent and status are unchanged       */
/* except if:                                                         */
/*                                                                    */
/*   -- the coefficient was increased and is all nines (in which      */
................................................................................

  if (residue==0) return;     // nothing to apply

  bump=0;                     // assume a smooth ride

  // now decide whether, and how, to round, depending on mode
  switch (set->round) {












    case DEC_ROUND_DOWN: {
      // no change, except if negative residue
      if (residue<0) bump=-1;
      break;} // r-d

    case DEC_ROUND_HALF_DOWN: {
      if (residue>5) bump=1;
................................................................................
       else {
        if (residue>0) bump=1;
        }
      break;} // r-f

    default: {      // e.g., DEC_ROUND_MAX
      *status|=DEC_Invalid_context;
      #if DECTRACE
      printf("Unknown rounding mode: %d\n", set->round);
      #endif
      break;}
    } // switch

  // now bump the number, up or down, if need be
  if (bump==0) return;                       // no action required
................................................................................
  // Must be careful, here, when checking the exponent as the
  // adjusted exponent could overflow 31 bits [because it may already
  // be up to twice the expected].

  // First test for subnormal.  This must be done before any final
  // round as the result could be rounded to Nmin or 0.
  if (dn->exponent<=tinyexp) {          // prefilter

    decNumber nmin;
    // A very nasty case here is dn == Nmin and residue<0
    if (dn->exponent<tinyexp) {
      // Go handle subnormals; this will apply round if needed.
      decSetSubnormal(dn, set, residue, status);
      return;
      }
    // Equals case: only subnormal if dn=Nmin and negative residue
    decNumberZero(&nmin);
    nmin.lsu[0]=1;
    nmin.exponent=set->emin;
    if (*residue<0 && decCompare(dn, &nmin, 1)==0) { // (signless compare)





      decApplyRound(dn, set, *residue, status);   // might force down
      decSetSubnormal(dn, set, residue, status);
      return;
      }
    }

  // now apply any pending round (this could raise overflow).
  if (*residue!=0) decApplyRound(dn, set, *residue, status);

  // Check for overflow [redundant in the 'rare' case] or clamp
  if (dn->exponent<=set->emax-set->digits+1) return;   // neither needed


  // here when might have an overflow or clamp to do
  if (dn->exponent>set->emax-dn->digits+1) {           // too big
    decSetOverflow(dn, set, status);
    return;
    }
  // here when the result is normal but in clamp range
................................................................................
  return;
  } // decFinalize

/* ------------------------------------------------------------------ */
/* decSetOverflow -- set number to proper overflow value              */
/*                                                                    */
/*   dn is the number (used for sign [only] and result)               */
/*   set is the context [used for the rounding mode]                  */
/*   status contains the current status to be updated                 */
/*                                                                    */
/* This sets the sign of a number and sets its value to either        */
/* Infinity or the maximum finite value, depending on the sign of     */
/* dn and therounding mode, following IEEE 854 rules.                 */
/* ------------------------------------------------------------------ */
static void decSetOverflow(decNumber *dn, decContext *set, uInt *status) {
  Flag needmax=0;                  // result is maximum finite value
  uByte sign=dn->bits&DECNEG;      // clean and save sign bit

  if (ISZERO(dn)) {                // zero does not overflow magnitude
    Int emax=set->emax;                      // limit value
................................................................................
      break;} // r-c
    case DEC_ROUND_FLOOR: {
      if (!sign) needmax=1;        // Infinity if negative
      break;} // r-f
    default: break;                // Infinity in all other cases
    }
  if (needmax) {
















    Unit *up;                      // work
    Int count=set->digits;         // nines to add
    dn->digits=count;
    // fill in all nines to set maximum value
    for (up=dn->lsu; ; up++) {
      if (count>DECDPUN) *up=DECDPUNMAX;     // unit full o'nines
       else {                                // this is the msu
        *up=(Unit)(powers[count]-1);
        break;
        }
      count-=DECDPUN;              // filled those digits
      } // up
    dn->bits=sign;                 // sign
    dn->exponent=set->emax-set->digits+1;
    }
   else dn->bits=sign|DECINF;      // Value is +/-Infinity
  *status|=DEC_Overflow | DEC_Inexact | DEC_Rounded;
  } // decSetOverflow

/* ------------------------------------------------------------------ */
/* decSetSubnormal -- process value whose exponent is <Emin           */
/*                                                                    */
/*   dn is the number (used as input as well as output; it may have   */
/*         an allowed subnormal value, which may need to be rounded)  */
/*   set is the context [used for the rounding mode]                  */
................................................................................
  // (Etiny) if needed
  etiny=set->emin-(set->digits-1);      // smallest allowed exponent

  if ISZERO(dn) {                       // value is zero
    // residue can never be non-zero here
    #if DECCHECK
      if (*residue!=0) {
        printf("++ Subnormal 0 residue %d\n", *residue);
        *status|=DEC_Invalid_operation;
        }
    #endif
    if (dn->exponent<etiny) {           // clamp required
      dn->exponent=etiny;
      *status|=DEC_Clamped;
      }
................................................................................
/* decGetInt -- get integer from a number                             */
/*                                                                    */
/*   dn is the number [which will not be altered]                     */
/*                                                                    */
/*   returns one of:                                                  */
/*     BADINT if there is a non-zero fraction                         */
/*     the converted integer                                          */
/*     BIGEVEN if the integer is even and > 2*10**9                   */
/*     BIGODD  if the integer is odd  and > 2*10**9                   */
/*                                                                    */
/* This checks and gets a whole number from the input decNumber.      */
/* The sign can be determined from dn by the caller when BIGEVEN or   */
/* BIGODD is returned.                                                */
/* ------------------------------------------------------------------ */
static Int decGetInt(const decNumber *dn) {
  Int  theInt;                          // result accumulator
................................................................................
    }

  if (neg) theInt=-theInt;              // apply sign
  return theInt;
  } // decGetInt

/* ------------------------------------------------------------------ */
/* decPutInt -- put integer into a number                             */
/*                                                                    */
/*   res is the target number, with enough space for the biggest      */
/*       integer that the second argument will be                     */
/*   in  is the input integer                                         */

/*                                                                    */
/* Returns res, an integral value; no error is possible.              */



/* ------------------------------------------------------------------ */
static decNumber *decPutInt(decNumber *res, Int in) {
  Unit *up;                             // work pointer

  decNumberZero(res);                   // clean
  if (in<=0) {                          // handle sign, 0, and BADINT
    if (in==0) return res;              // easy
     else if (in!=BADINT) {
      res->bits=DECNEG;                 // set sign
      in=-in;                           // invert and drop through
      }

     else {                             // BADINT: invert would fail
      decContext set;
      decContextDefault(&set, DEC_INIT_DECIMAL64);     // 16 digits
      decNumberFromString(res, "-2147483648", &set);   // ugh
      return res;
      }
    }


  // in is now positive
  for (up=res->lsu; in>0; up++) {
    *up=(Unit)(in%(DECDPUNMAX+1));
    in=in/(DECDPUNMAX+1);
    }
  res->digits=decGetDigits(res->lsu, up-res->lsu);
  return res;
  } // decPutInt

/* ------------------------------------------------------------------ */
/* decBiStr -- compare string with pairwise options                   */
/*                                                                    */
/*   targ is the string to compare                                    */
/*   str1 is one of the strings to compare against (length may be 0)  */
/*   str2 is the other; it must be the same length as str1            */
................................................................................
    } // forever
  return 1;
  } // decBiStr

/* ------------------------------------------------------------------ */
/* decNaNs -- handle NaN operand or operands                          */
/*                                                                    */
/*   res    is the result number                                      */
/*   lhs    is the first operand                                      */
/*   rhs    is the second operand, or NULL if none                    */

/*   status contains the current status                               */
/*   returns res in case convenient                                   */
/*                                                                    */
/* Called when one or both operands is a NaN, and propagates the      */
/* appropriate result to res.  When an sNaN is found, it is changed   */
/* to a qNaN and Invalid operation is set.                            */
/* ------------------------------------------------------------------ */
static decNumber * decNaNs(decNumber *res, const decNumber *lhs,
                           const decNumber *rhs, uInt *status) {

  // This decision tree ends up with LHS being the source pointer,
  // and status updated if need be
  if (lhs->bits & DECSNAN)
    *status|=DEC_Invalid_operation | DEC_sNaN;
   else if (rhs==NULL);
   else if (rhs->bits & DECSNAN) {
    lhs=rhs;
    *status|=DEC_Invalid_operation | DEC_sNaN;
    }
   else if (lhs->bits & DECNAN);
   else lhs=rhs;


  decNumberCopy(res, lhs);












  res->bits&=~DECSNAN;        // convert any sNaN to NaN, while
  res->bits|=DECNAN;          // .. preserving sign
  res->exponent=0;            // clean exponent
                              // [coefficient was copied]
  return res;
  } // decNaNs

/* ------------------------------------------------------------------ */
/* decStatus -- apply non-zero status                                 */
/*                                                                    */
/*   dn     is the number to set if error                             */
................................................................................
  } // decStatus

/* ------------------------------------------------------------------ */
/* decGetDigits -- count digits in a Units array                      */
/*                                                                    */
/*   uar is the Unit array holding the number (this is often an       */
/*          accumulator of some sort)                                 */
/*   len is the length of the array in units                          */
/*                                                                    */
/*   returns the number of (significant) digits in the array          */
/*                                                                    */
/* All leading zeros are excluded, except the last if the array has   */
/* only zero Units.                                                   */
/* ------------------------------------------------------------------ */
// This may be called twice during some operations.
static Int decGetDigits(Unit *uar, Int len) {
  Unit *up=uar+(len-1);            // -> msu
  Int  digits=(len-1)*DECDPUN+1;   // possible digits excluding msu
                                   // (at least 1 in final msu)
  #if DECDPUN>4
  uInt const *pow;                 // work
  #endif





  for (; up>=uar; up--) {
    if (*up==0) {                  // unit is all 0s
      if (digits==1) break;        // a zero has one digit
      digits-=DECDPUN;             // adjust for 0 unit
      continue;}
    // found the first (most significant) non-zero Unit
................................................................................
    #endif
    #endif
    #endif
    break;
    } // up
  return digits;
  } // decGetDigits


#if DECTRACE | DECCHECK
/* ------------------------------------------------------------------ */
/* decNumberShow -- display a number [debug aid]                      */
/*   dn is the number to show                                         */
/*                                                                    */
/* Shows: sign, exponent, coefficient (msu first), digits             */
................................................................................
      return;}
    // drop through to report other information
    printf(" ");
    }

  // now carefully display the coefficient
  up=dn->lsu+D2U(dn->digits)-1;         // msu
  printf("%d", *up);
  for (up=up-1; up>=dn->lsu; up--) {
    u=*up;
    printf(":");
    for (cut=DECDPUN-1; cut>=0; cut--) {
      d=u/powers[cut];
      u-=d*powers[cut];
      printf("%d", d);
      } // cut
    } // up
  if (dn->exponent!=0) {
    char esign='+';
    if (dn->exponent<0) esign='-';
    printf(" E%c%d", esign, abs(dn->exponent));
    }
  printf(" [%d]\n", dn->digits);
  } // decNumberShow
#endif

#if DECTRACE || DECCHECK
/* ------------------------------------------------------------------ */
/* decDumpAr -- display a unit array [debug aid]                      */
/*   name is a single-character tag name                              */
................................................................................
  #elif DECDPUN==2
    char *spec="%02d ";
  #else
    char *spec="%d ";
  #endif
  printf("  :%c: ", name);
  for (i=len-1; i>=0; i--) {
    if (i==len-1) printf("%d ", ar[i]);
     else printf(spec, ar[i]);
    }
  printf("\n");
  return;}
#endif

#if DECCHECK
................................................................................
/*     handle this so res=NULL case is safe.                          */
/* The caller is expected to abandon immediately if 1 is returned.    */
/* ------------------------------------------------------------------ */
static Flag decCheckOperands(decNumber *res, const decNumber *lhs,
                             const decNumber *rhs, decContext *set) {
  Flag bad=0;
  if (set==NULL) {                 // oops; hopeless
    #if DECTRACE
    printf("Context is NULL.\n");
    #endif
    bad=1;
    return 1;}
   else if (set!=DECUNUSED
     && (set->digits<1 || set->round<0 || set->round>=DEC_ROUND_MAX)) {
    bad=1;
    #if DECTRACE
    printf("Bad context [digits=%d round=%d].\n", set->digits, set->round);
    #endif
    }
   else {
    if (res==NULL) {
      bad=1;
      #if DECTRACE
      printf("Bad result [is NULL].\n");

      #endif
      }
    if (!bad && lhs!=DECUNUSED) bad=(decCheckNumber(lhs, set));
    if (!bad && rhs!=DECUNUSED) bad=(decCheckNumber(rhs, set));
    }
  if (bad) {
    if (set!=DECUNUSED) decContextSetStatus(set, DEC_Invalid_operation);
................................................................................
  const Unit *up;             // work
  uInt maxuint;               // ..
  Int ae, d, digits;          // ..
  Int emin, emax;             // ..

  if (dn==NULL) {             // hopeless
    #if DECTRACE

    printf("Reference to decNumber is NULL.\n");
    #endif
    return 1;}

  // check special values
  if (dn->bits & DECSPECIAL) {
    if (dn->exponent!=0) {
      #if DECTRACE
      printf("Exponent %d (not 0) for a special value.\n", dn->exponent);

      #endif
      return 1;}

    // 2003.09.08: NaNs may now have coefficients, so next tests Inf only
    if (decNumberIsInfinite(dn)) {
      if (dn->digits!=1) {
        #if DECTRACE
        printf("Digits %d (not 1) for an infinity.\n", dn->digits);
        #endif
        return 1;}
      if (*dn->lsu!=0) {
        #if DECTRACE
        printf("LSU %d (not 0) for an infinity.\n", *dn->lsu);
        #endif
        return 1;}
      } // Inf
    // 2002.12.26: negative NaNs can now appear through proposed IEEE
    //             concrete formats (decimal64, etc.), though they are
    //             never visible in strings.
    return 0;

    // if ((dn->bits & DECINF) || (dn->bits & DECNEG)==0) return 0;
    // #if DECTRACE
    // printf("Negative NaN in number.\n");
    // #endif
    // return 1;
    }

  // check the coefficient
  if (dn->digits<1 || dn->digits>DECNUMMAXP) {
    #if DECTRACE
    printf("Digits %d in number.\n", dn->digits);
    #endif
    return 1;}

  d=dn->digits;

  for (up=dn->lsu; d>0; up++) {
    if (d>DECDPUN) maxuint=DECDPUNMAX;
     else {                   // reached the msu
      maxuint=powers[d]-1;
      if (dn->digits>1 && *up<powers[d-1]) {
        #if DECTRACE
        printf("Leading 0 in number.\n");
        decNumberShow(dn);
        #endif
        return 1;}
      }
    if (*up>maxuint) {
      #if DECTRACE
      printf("Bad Unit [%08x] in %d-digit number at offset %d [maxuint %d].\n",
              *up, dn->digits, up-dn->lsu, maxuint);
      #endif
      return 1;}
    d-=DECDPUN;
    }

  // check the exponent.  Note that input operands can have exponents
  // which are out of the set->emin/set->emax and set->digits range
  // (just as they can have more digits than set->digits).
  ae=dn->exponent+dn->digits-1;    // adjusted exponent
  emax=DECNUMMAXE;
  emin=DECNUMMINE;
  digits=DECNUMMAXP;
  if (ae<emin-(digits-1)) {
    #if DECTRACE
    printf("Adjusted exponent underflow [%d].\n", ae);
    decNumberShow(dn);
    #endif
    return 1;}
  if (ae>+emax) {
    #if DECTRACE
    printf("Adjusted exponent overflow [%d].\n", ae);
    decNumberShow(dn);
    #endif
    return 1;}

  return 0;              // it's OK
  } // decCheckNumber


/* ------------------------------------------------------------------ */
/* decCheckInexact -- check a normal finite inexact result has digits */
/*   dn is the number to check                                        */
/*   set is the context (for status and precision)                    */
/*   sets Invalid operation, etc., if some digits are missing         */
/* [this check is not made for DECSUBSET compilation]                 */

/* ------------------------------------------------------------------ */
static void decCheckInexact(const decNumber *dn, decContext *set) {
  #if !DECSUBSET
    if ((set->status & (DEC_Inexact|DEC_Subnormal))==DEC_Inexact
     && (set->digits!=dn->digits) && !(dn->bits & DECSPECIAL)) {
      decContextSetStatus(set, DEC_Invalid_operation);
      #if DECTRACE
      printf("Insufficient digits [%d] on normal Inexact result.\n", dn->digits);
      decNumberShow(dn);
      #endif

      }
  #endif
  return;
  } // decCheckInexact
#endif

#if DECALLOC
................................................................................

  alloc=malloc(size);              // -> allocated storage
  if (alloc==NULL) return NULL;    // out of strorage
  b0=(uByte *)alloc;               // as bytes
  decAllocBytes+=n;                // account for storage
  j=(uInt *)alloc;                 // -> first four bytes
  *j=n;                            // save n
  // printf(" allo ++ dAB: %d (%d)\n", decAllocBytes, n);
  for (b=b0+4; b<b0+8; b++) *b=DECFENCE;
  for (b=b0+n+8; b<b0+n+12; b++) *b=DECFENCE;
  return b0+8;                     // -> play area
  } // decMalloc

/* ------------------------------------------------------------------ */
/* decFree -- accountable free routine                                */
................................................................................

  if (alloc==NULL) return;         // allowed; it's a nop
  b0=(uByte *)alloc;               // as bytes
  b0-=8;                           // -> true start of storage
  j=(uInt *)b0;                    // -> first four bytes
  n=*j;                            // lift
  for (b=b0+4; b<b0+8; b++) if (*b!=DECFENCE)
    printf("=== Corrupt byte [%02x] at offset %d from %d ===\n", *b,
           b-b0-8, (Int)b0);
  for (b=b0+n+8; b<b0+n+12; b++) if (*b!=DECFENCE)
    printf("=== Corrupt byte [%02x] at offset +%d from %d, n=%d ===\n", *b,
           b-b0-8, (Int)b0, n);
  free(b0);                        // drop the storage
  decAllocBytes-=n;                // account for storage
  // printf(" free -- dAB: %d (%d)\n", decAllocBytes, -n);
  } // decFree
#define malloc(a) decMalloc(a)
#define free(a) decFree(a)
#endif



|







 







>
>
>
>
>
|



|











|





|











|







 







|
|
|
|
|
|
|
|
|
>
>
>






>
>







 







>













|
<



>


>






|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
>







 







>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










>







 









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








>










<







 







|

|






|







 







|







 







|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







|







 







|
>







 







>








|
|
|
|
>


>
>
|

|



>







<

|
<
<

<
>
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










>
>
>
>
>
|







 







>










|







>
>







 







|

|







 







|







 







>












>







 







|







 







>







 







|







 







|
<







 







>
>








|







 







|







 







>







 







>







 







|







 







>







 







<
<







 







|







 







>
>












|
|

|
|







 







|
|
|
|
|
|
|
<







 







|







 







>







 







>
>
>










>




|







|



|







 







|







 







|
>
>
>
>







 







|







 







|







 







>
>
>













|


|







 







|







 







|
|











|







 







|







 







|







 







|
|
|
>
>
|
|
>
|
>
|
|









|







 







|







 







|
>
>
|







 







|










|
|


>
>
|
|



|
|







 







|







 







|
|







 







|
>
>
>







|
>


|
<
<







<








<



<







 







|







 







|







 







|












>




>
>



|
>







 







|
>
>
>
>
>
>
>








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|












>
>
>
>







 







|
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







>











|
>
>
>
>
>











>







 







|




|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
|







 







|







 







|
|







 







|

|
|
|
>

<
>
>
>

|
|
<
<
|
<
|
|
|
<
>
|
|
|
<
|
|
<
>
>
|
|
<
<
<
|
|
|







 







|
|
|
>
|







|
>












>
|
>
>
>
>
>
>
>
>
>
>
>
>



|







 







|










<



>
>
>
>







 







<







 







|






|







|







 







|







 







|
|






|
|






|
>







 







>







|
|
>






|
|



|
|




|
<

<
<
<
<
<
<




|
|










|






|
|
|













|
|




|
|







<





|
>


|


<
|
|


>







 







|







 







|


|








1
2
3
4
5
6
7
8
9
10
11
..
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
...
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
...
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
...
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
...
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
...
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
...
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
...
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
....
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
....
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
....
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
....
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
....
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
....
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
....
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
....
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863

1864
1865
1866
1867
1868
1869
1870
....
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
....
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
....
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
....
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
....
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
....
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
....
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
....
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
....
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
....
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121

3122
3123


3124

3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
....
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
....
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
....
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
....
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
....
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
....
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
....
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
....
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
....
4052
4053
4054
4055
4056
4057
4058
4059

4060
4061
4062
4063
4064
4065
4066
....
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
....
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
....
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
....
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
....
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
....
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
....
4628
4629
4630
4631
4632
4633
4634


4635
4636
4637
4638
4639
4640
4641
....
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
....
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
....
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802

4803
4804
4805
4806
4807
4808
4809
....
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
....
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
....
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
....
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
....
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
....
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
....
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
....
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
....
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
....
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
....
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
....
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
....
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
....
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
....
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
....
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
....
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
....
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
....
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121


6122
6123
6124
6125
6126
6127
6128

6129
6130
6131
6132
6133
6134
6135
6136

6137
6138
6139

6140
6141
6142
6143
6144
6145
6146
....
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
....
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
....
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
....
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
....
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
....
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
....
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
....
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
....
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
....
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
....
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332



7333
7334
7335
7336
7337
7338
7339
7340
....
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
....
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
....
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552

7553
7554
7555
7556
7557
7558


7559

7560
7561
7562

7563
7564
7565
7566

7567
7568

7569
7570
7571
7572



7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
....
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
....
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694

7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
....
7721
7722
7723
7724
7725
7726
7727

7728
7729
7730
7731
7732
7733
7734
....
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
....
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
....
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
....
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916

7917






7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971

7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983

7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
....
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
....
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
/* ------------------------------------------------------------------ */
/* Decimal Number arithmetic module                                   */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2007.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
/*    range (Emax in the range 0 through 999,999,999 and Emin in the  */
/*    range -999,999,999 through 0).  Mathematical functions (for     */
/*    example decNumberExp) as identified below are restricted more   */
/*    tightly: digits, emax, and -emin in the context must be <=      */
/*    DEC_MAX_MATH (999999), and their operand(s) must be within      */
/*    these bounds.                                                   */
/*                                                                    */
/* 3. Logical functions are further restricted; their operands must   */
/*    be finite, positive, have an exponent of zero, and all digits   */
/*    must be either 0 or 1.  The result will only contain digits     */
/*    which are 0 or 1 (and will have exponent=0 and a sign of 0).    */
/*                                                                    */
/* 4. Operands to operator functions are never modified unless they   */
/*    are also specified to be the result number (which is always     */
/*    permitted).  Other than that case, operands must not overlap.   */
/*                                                                    */
/* 5. Error handling: the type of the error is ORed into the status   */
/*    flags in the current context (decContext structure).  The       */
/*    SIGFPE signal is then raised if the corresponding trap-enabler  */
/*    flag in the decContext is set (is 1).                           */
/*                                                                    */
/*    It is the responsibility of the caller to clear the status      */
/*    flags as required.                                              */
/*                                                                    */
/*    The result of any routine which returns a number will always    */
/*    be a valid number (which may be a special value, such as an     */
/*    Infinity or NaN).                                               */
/*                                                                    */
/* 6. The decNumber format is not an exchangeable concrete            */
/*    representation as it comprises fields which may be machine-     */
/*    dependent (packed or unpacked, or special length, for example). */
/*    Canonical conversions to and from strings are provided; other   */
/*    conversions are available in separate modules.                  */
/*                                                                    */
/* 7. Normally, input operands are assumed to be valid.  Set DECCHECK */
/*    to 1 for extended operand checking (including NULL operands).   */
/*    Results are undefined if a badly-formed structure (or a NULL    */
/*    pointer to a structure) is provided, though with DECCHECK       */
/*    enabled the operator routines are protected against exceptions. */
/*    (Except if the result pointer is NULL, which is unrecoverable.) */
/*                                                                    */
/*    However, the routines will never cause exceptions if they are   */
/*    given well-formed operands, even if the value of the operands   */
/*    is inappropriate for the operation and DECCHECK is not set.     */
/*    (Except for SIGFPE, as and where documented.)                   */
/*                                                                    */
/* 8. Subset arithmetic is available only if DECSUBSET is set to 1.   */
/* ------------------------------------------------------------------ */
/* Implementation notes for maintenance of this module:               */
/*                                                                    */
/* 1. Storage leak protection:  Routines which use malloc are not     */
/*    permitted to use return for fastpath or error exits (i.e.,      */
/*    they follow strict structured programming conventions).         */
/*    Instead they have a do{}while(0); construct surrounding the     */
................................................................................
// Public constant array: powers of ten (powers[n]==10**n, 0<=n<=9)
const uInt powers[10]={1, 10, 100, 1000, 10000, 100000, 1000000,
                       10000000, 100000000, 1000000000};
// Public lookup table used by the D2U macro
const uByte d2utable[DECMAXD2U+1]=D2UTABLE;

// Local constants
#define DIVIDE      0x80           // Divide operators
#define REMAINDER   0x40           // ..
#define DIVIDEINT   0x20           // ..
#define REMNEAR     0x10           // ..
#define COMPARE     0x01           // Compare operators
#define COMPMAX     0x02           // ..
#define COMPMIN     0x03           // ..
#define COMPTOTAL   0x04           // ..
#define COMPNAN     0x05           // .. [NaN processing]
#define COMPSIG     0x06           // .. [signaling COMPARE]
#define COMPMAXMAG  0x07           // ..
#define COMPMINMAG  0x08           // ..

#define DEC_sNaN     0x40000000    // local status: sNaN signal
#define BADINT  (Int)0x80000000    // most-negative Int; error indicator
// Next two indicate an integer >= 10**6, and its parity (bottom bit)
#define BIGEVEN (Int)0x80000002
#define BIGODD  (Int)0x80000003

#define DECVERB     1              // set to 1 for verbose DECCHECK

static Unit uarrone[1]={1};   // Unit array of 1, used for incrementing

/* Granularity-dependent code */
#if DECDPUN<=4
  #define eInt  Int           // extended integer
  #define ueInt uInt          // unsigned extended integer
................................................................................
static void        decApplyRound(decNumber *, decContext *, Int, uInt *);
static Int         decCompare(const decNumber *lhs, const decNumber *rhs, Flag);
static decNumber * decCompareOp(decNumber *, const decNumber *,
                              const decNumber *, decContext *,
                              Flag, uInt *);
static void        decCopyFit(decNumber *, const decNumber *, decContext *,
                              Int *, uInt *);
static decNumber * decDecap(decNumber *, Int);
static decNumber * decDivideOp(decNumber *, const decNumber *,
                              const decNumber *, decContext *, Flag, uInt *);
static decNumber * decExpOp(decNumber *, const decNumber *,
                              decContext *, uInt *);
static void        decFinalize(decNumber *, decContext *, Int *, uInt *);
static Int         decGetDigits(Unit *, Int);
static Int         decGetInt(const decNumber *);
static decNumber * decLnOp(decNumber *, const decNumber *,
                              decContext *, uInt *);
static decNumber * decMultiplyOp(decNumber *, const decNumber *,
                              const decNumber *, decContext *,
                              uInt *);
static decNumber * decNaNs(decNumber *, const decNumber *,
                              const decNumber *, decContext *, uInt *);

static decNumber * decQuantizeOp(decNumber *, const decNumber *,
                              const decNumber *, decContext *, Flag,
                              uInt *);
static void        decReverse(Unit *, Unit *);
static void        decSetCoeff(decNumber *, decContext *, const Unit *,
                              Int, Int *, uInt *);
static void        decSetMaxValue(decNumber *, decContext *);
static void        decSetOverflow(decNumber *, decContext *, uInt *);
static void        decSetSubnormal(decNumber *, decContext *, Int *, uInt *);
static Int         decShiftToLeast(Unit *, Int, Int);
static Int         decShiftToMost(Unit *, Int, Int);
static void        decStatus(decNumber *, uInt, decContext *);
static void        decToString(const decNumber *, char[], Flag);
static decNumber * decTrim(decNumber *, decContext *, Flag, Int *);
static Int         decUnitAddSub(const Unit *, Int, const Unit *, Int, Int,
                              Unit *, Int);
static Int         decUnitCompare(const Unit *, Int, const Unit *, Int, Int);

#if !DECSUBSET
/* decFinish == decFinalize when no subset arithmetic needed */
#define decFinish(a,b,c,d) decFinalize(a,b,c,d)
................................................................................
void decNumberShow(const decNumber *);  // displays the components of a number
static void decDumpAr(char, const Unit *, Int);
#endif

/* ================================================================== */
/* Conversions                                                        */
/* ================================================================== */

/* ------------------------------------------------------------------ */
/* from-int32 -- conversion from Int or uInt                          */
/*                                                                    */
/*  dn is the decNumber to receive the integer                        */
/*  in or uin is the integer to be converted                          */
/*  returns dn                                                        */
/*                                                                    */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
decNumber * decNumberFromInt32(decNumber *dn, Int in) {
  uInt unsig;
  if (in>=0) unsig=in;
   else {                               // negative (possibly BADINT)
    if (in==BADINT) unsig=(uInt)1073741824*2; // special case
     else unsig=-in;                    // invert
    }
  // in is now positive
  decNumberFromUInt32(dn, unsig);
  if (in<0) dn->bits=DECNEG;            // sign needed
  return dn;
  } // decNumberFromInt32

decNumber * decNumberFromUInt32(decNumber *dn, uInt uin) {
  Unit *up;                             // work pointer
  decNumberZero(dn);                    // clean
  if (uin==0) return dn;                // [or decGetDigits bad call]
  for (up=dn->lsu; uin>0; up++) {
    *up=(Unit)(uin%(DECDPUNMAX+1));
    uin=uin/(DECDPUNMAX+1);
    }
  dn->digits=decGetDigits(dn->lsu, up-dn->lsu);
  return dn;
  } // decNumberFromUInt32

/* ------------------------------------------------------------------ */
/* to-int32 -- conversion to Int or uInt                              */
/*                                                                    */
/*  dn is the decNumber to convert                                    */
/*  set is the context for reporting errors                           */
/*  returns the converted decNumber, or 0 if Invalid is set           */
/*                                                                    */
/* Invalid is set if the decNumber does not have exponent==0 or if    */
/* it is a NaN, Infinite, or out-of-range.                            */
/* ------------------------------------------------------------------ */
Int decNumberToInt32(const decNumber *dn, decContext *set) {
  #if DECCHECK
  if (decCheckOperands(DECUNUSED, DECUNUSED, dn, set)) return 0;
  #endif

  // special or too many digits, or bad exponent
  if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0) ; // bad
   else { // is a finite integer with 10 or fewer digits
    Int d;                         // work
    const Unit *up;                // ..
    uInt hi=0, lo;                 // ..
    up=dn->lsu;                    // -> lsu
    lo=*up;                        // get 1 to 9 digits
    #if DECDPUN>1                  // split to higher
      hi=lo/10;
      lo=lo%10;
    #endif
    up++;
    // collect remaining Units, if any, into hi
    for (d=DECDPUN; d<dn->digits; up++, d+=DECDPUN) hi+=*up*powers[d-1];
    // now low has the lsd, hi the remainder
    if (hi>214748364 || (hi==214748364 && lo>7)) { // out of range?
      // most-negative is a reprieve
      if (dn->bits&DECNEG && hi==214748364 && lo==8) return 0x80000000;
      // bad -- drop through
      }
     else { // in-range always
      Int i=X10(hi)+lo;
      if (dn->bits&DECNEG) return -i;
      return i;
      }
    } // integer
  decContextSetStatus(set, DEC_Invalid_operation); // [may not return]
  return 0;
  } // decNumberToInt32

uInt decNumberToUInt32(const decNumber *dn, decContext *set) {
  #if DECCHECK
  if (decCheckOperands(DECUNUSED, DECUNUSED, dn, set)) return 0;
  #endif
  // special or too many digits, or bad exponent, or negative (<0)
  if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0
    || (dn->bits&DECNEG && !ISZERO(dn)));                   // bad
   else { // is a finite integer with 10 or fewer digits
    Int d;                         // work
    const Unit *up;                // ..
    uInt hi=0, lo;                 // ..
    up=dn->lsu;                    // -> lsu
    lo=*up;                        // get 1 to 9 digits
    #if DECDPUN>1                  // split to higher
      hi=lo/10;
      lo=lo%10;
    #endif
    up++;
    // collect remaining Units, if any, into hi
    for (d=DECDPUN; d<dn->digits; up++, d+=DECDPUN) hi+=*up*powers[d-1];

    // now low has the lsd, hi the remainder
    if (hi>429496729 || (hi==429496729 && lo>5)) ; // no reprieve possible
     else return X10(hi)+lo;
    } // integer
  decContextSetStatus(set, DEC_Invalid_operation); // [may not return]
  return 0;
  } // decNumberToUInt32

/* ------------------------------------------------------------------ */
/* to-scientific-string -- conversion to numeric string               */
/* to-engineering-string -- conversion to numeric string              */
/*                                                                    */
/*   decNumberToString(dn, string);                                   */
/*   decNumberToEngString(dn, string);                                */
................................................................................
/* If bad syntax is detected, the result will be a quiet NaN.         */
/* ------------------------------------------------------------------ */
decNumber * decNumberFromString(decNumber *dn, const char chars[],
                                decContext *set) {
  Int   exponent=0;                // working exponent [assume 0]
  uByte bits=0;                    // working flags [assume +ve]
  Unit  *res;                      // where result will be built
  Unit  resbuff[SD2U(DECBUFFER+9)];// local buffer in case need temporary
                                   // [+9 allows for ln() constants]
  Unit  *allocres=NULL;            // -> allocated result, iff allocated
  Int   d=0;                       // count of digits found in decimal part
  const char *dotchar=NULL;        // where dot was found
  const char *cfirst=chars;        // -> first character of decimal part
  const char *last=NULL;           // -> last digit of decimal part
  const char *c;                   // work
  Unit  *up;                       // ..
................................................................................
/*                                                                    */
/*   This computes C = abs(A)                                         */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*   set is the context                                               */
/*                                                                    */
/* See also decNumberCopyAbs for a quiet bitwise version of this.     */
/* C must have space for set->digits digits.                          */
/* ------------------------------------------------------------------ */
/* This has the same effect as decNumberPlus unless A is negative,    */
/* in which case it has the same effect as decNumberMinus.            */
/* ------------------------------------------------------------------ */
decNumber * decNumberAbs(decNumber *res, const decNumber *rhs,
                         decContext *set) {
................................................................................
  decAddOp(res, lhs, rhs, set, 0, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberAdd

/* ------------------------------------------------------------------ */
/* decNumberAnd -- AND two Numbers, digitwise                         */
/*                                                                    */
/*   This computes C = A & B                                          */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X&X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context (used for result length and error report)     */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/*                                                                    */
/* Logical function restrictions apply (see above); a NaN is          */
/* returned with Invalid_operation if a restriction is violated.      */
/* ------------------------------------------------------------------ */
decNumber * decNumberAnd(decNumber *res, const decNumber *lhs,
                         const decNumber *rhs, decContext *set) {
  const Unit *ua, *ub;                  // -> operands
  const Unit *msua, *msub;              // -> operand msus
  Unit *uc,  *msuc;                     // -> result and its msu
  Int   msudigs;                        // digits in res msu
  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  #endif

  if (lhs->exponent!=0 || decNumberIsSpecial(lhs) || decNumberIsNegative(lhs)
   || rhs->exponent!=0 || decNumberIsSpecial(rhs) || decNumberIsNegative(rhs)) {
    decStatus(res, DEC_Invalid_operation, set);
    return res;
    }

  // operands are valid
  ua=lhs->lsu;                          // bottom-up
  ub=rhs->lsu;                          // ..
  uc=res->lsu;                          // ..
  msua=ua+D2U(lhs->digits)-1;           // -> msu of lhs
  msub=ub+D2U(rhs->digits)-1;           // -> msu of rhs
  msuc=uc+D2U(set->digits)-1;           // -> msu of result
  msudigs=MSUDIGITS(set->digits);       // [faster than remainder]
  for (; uc<=msuc; ua++, ub++, uc++) {  // Unit loop
    Unit a, b;                          // extract units
    if (ua>msua) a=0;
     else a=*ua;
    if (ub>msub) b=0;
     else b=*ub;
    *uc=0;                              // can now write back
    if (a|b) {                          // maybe 1 bits to examine
      Int i, j;
      *uc=0;                            // can now write back
      // This loop could be unrolled and/or use BIN2BCD tables
      for (i=0; i<DECDPUN; i++) {
        if (a&b&1) *uc=*uc+(Unit)powers[i];  // effect AND
        j=a%10;
        a=a/10;
        j|=b%10;
        b=b/10;
        if (j>1) {
          decStatus(res, DEC_Invalid_operation, set);
          return res;
          }
        if (uc==msuc && i==msudigs-1) break; // just did final digit
        } // each digit
      } // both OK
    } // each unit
  // [here uc-1 is the msu of the result]
  res->digits=decGetDigits(res->lsu, uc-res->lsu);
  res->exponent=0;                      // integer
  res->bits=0;                          // sign=0
  return res;  // [no status to set]
  } // decNumberAnd

/* ------------------------------------------------------------------ */
/* decNumberCompare -- compare two Numbers                            */
/*                                                                    */
/*   This computes C = A ? B                                          */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
................................................................................
decNumber * decNumberCompare(decNumber *res, const decNumber *lhs,
                             const decNumber *rhs, decContext *set) {
  uInt status=0;                        // accumulator
  decCompareOp(res, lhs, rhs, set, COMPARE, &status);
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberCompare

/* ------------------------------------------------------------------ */
/* decNumberCompareSignal -- compare, signalling on all NaNs          */
/*                                                                    */
/*   This computes C = A ? B                                          */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for one digit (or NaN).                          */
/* ------------------------------------------------------------------ */
decNumber * decNumberCompareSignal(decNumber *res, const decNumber *lhs,
                                   const decNumber *rhs, decContext *set) {
  uInt status=0;                        // accumulator
  decCompareOp(res, lhs, rhs, set, COMPSIG, &status);
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberCompareSignal

/* ------------------------------------------------------------------ */
/* decNumberCompareTotal -- compare two Numbers, using total ordering */
/*                                                                    */
/*   This computes C = A ? B, under total ordering                    */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
................................................................................
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for one digit; the result will always be one of  */
/* -1, 0, or 1.                                                       */
/* ------------------------------------------------------------------ */
decNumber * decNumberCompareTotal(decNumber *res, const decNumber *lhs,
                                  const decNumber *rhs, decContext *set) {
  uInt status=0;                        // accumulator
  decCompareOp(res, lhs, rhs, set, COMPTOTAL, &status);
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberCompareTotal

/* ------------------------------------------------------------------ */
/* decNumberCompareTotalMag -- compare, total ordering of magnitudes  */
/*                                                                    */
/*   This computes C = |A| ? |B|, under total ordering                */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for one digit; the result will always be one of  */
/* -1, 0, or 1.                                                       */
/* ------------------------------------------------------------------ */
decNumber * decNumberCompareTotalMag(decNumber *res, const decNumber *lhs,
                                     const decNumber *rhs, decContext *set) {
  uInt status=0;                   // accumulator
  uInt needbytes;                  // for space calculations
  decNumber bufa[D2N(DECBUFFER+1)];// +1 in case DECBUFFER=0
  decNumber *allocbufa=NULL;       // -> allocated bufa, iff allocated
  decNumber bufb[D2N(DECBUFFER+1)];
  decNumber *allocbufb=NULL;       // -> allocated bufb, iff allocated
  decNumber *a, *b;                // temporary pointers

  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  #endif

  do {                                  // protect allocated storage
    // if either is negative, take a copy and absolute
    if (decNumberIsNegative(lhs)) {     // lhs<0
      a=bufa;
      needbytes=sizeof(decNumber)+(D2U(lhs->digits)-1)*sizeof(Unit);
      if (needbytes>sizeof(bufa)) {     // need malloc space
        allocbufa=(decNumber *)malloc(needbytes);
        if (allocbufa==NULL) {          // hopeless -- abandon
          status|=DEC_Insufficient_storage;
          break;}
        a=allocbufa;                    // use the allocated space
        }
      decNumberCopy(a, lhs);            // copy content
      a->bits&=~DECNEG;                 // .. and clear the sign
      lhs=a;                            // use copy from here on
      }
    if (decNumberIsNegative(rhs)) {     // rhs<0
      b=bufb;
      needbytes=sizeof(decNumber)+(D2U(rhs->digits)-1)*sizeof(Unit);
      if (needbytes>sizeof(bufb)) {     // need malloc space
        allocbufb=(decNumber *)malloc(needbytes);
        if (allocbufb==NULL) {          // hopeless -- abandon
          status|=DEC_Insufficient_storage;
          break;}
        b=allocbufb;                    // use the allocated space
        }
      decNumberCopy(b, rhs);            // copy content
      b->bits&=~DECNEG;                 // .. and clear the sign
      rhs=b;                            // use copy from here on
      }
    decCompareOp(res, lhs, rhs, set, COMPTOTAL, &status);
    } while(0);                         // end protected

  if (allocbufa!=NULL) free(allocbufa); // drop any storage used
  if (allocbufb!=NULL) free(allocbufb); // ..
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberCompareTotalMag

/* ------------------------------------------------------------------ */
/* decNumberDivide -- divide one number by another                    */
/*                                                                    */
/*   This computes C = A / B                                          */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X/X)         */
................................................................................
  // apply significant status
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberExp

/* ------------------------------------------------------------------ */
/* decNumberFMA -- fused multiply add                                 */
/*                                                                    */
/*   This computes D = (A * B) + C with only one rounding             */
/*                                                                    */
/*   res is D, the result.  D may be A or B or C (e.g., X=FMA(X,X,X)) */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   fhs is C [far hand side]                                         */
/*   set is the context                                               */
/*                                                                    */
/* Mathematical function restrictions apply (see above); a NaN is     */
/* returned with Invalid_operation if a restriction is violated.      */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/* ------------------------------------------------------------------ */
decNumber * decNumberFMA(decNumber *res, const decNumber *lhs,
                         const decNumber *rhs, const decNumber *fhs,
                         decContext *set) {
  uInt status=0;                   // accumulator
  decContext dcmul;                // context for the multiplication
  uInt needbytes;                  // for space calculations
  decNumber bufa[D2N(DECBUFFER*2+1)];
  decNumber *allocbufa=NULL;       // -> allocated bufa, iff allocated
  decNumber *acc;                  // accumulator pointer
  decNumber dzero;                 // work

  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  if (decCheckOperands(res, fhs, DECUNUSED, set)) return res;
  #endif

  do {                                  // protect allocated storage
    #if DECSUBSET
    if (!set->extended) {               // [undefined if subset]
      status|=DEC_Invalid_operation;
      break;}
    #endif
    // Check math restrictions [these ensure no overflow or underflow]
    if ((!decNumberIsSpecial(lhs) && decCheckMath(lhs, set, &status))
     || (!decNumberIsSpecial(rhs) && decCheckMath(rhs, set, &status))
     || (!decNumberIsSpecial(fhs) && decCheckMath(fhs, set, &status))) break;
    // set up context for multiply
    dcmul=*set;
    dcmul.digits=lhs->digits+rhs->digits; // just enough
    // [The above may be an over-estimate for subset arithmetic, but that's OK]
    dcmul.emax=DEC_MAX_EMAX;            // effectively unbounded ..
    dcmul.emin=DEC_MIN_EMIN;            // [thanks to Math restrictions]
    // set up decNumber space to receive the result of the multiply
    acc=bufa;                           // may fit
    needbytes=sizeof(decNumber)+(D2U(dcmul.digits)-1)*sizeof(Unit);
    if (needbytes>sizeof(bufa)) {       // need malloc space
      allocbufa=(decNumber *)malloc(needbytes);
      if (allocbufa==NULL) {            // hopeless -- abandon
        status|=DEC_Insufficient_storage;
        break;}
      acc=allocbufa;                    // use the allocated space
      }
    // multiply with extended range and necessary precision
    //printf("emin=%ld\n", dcmul.emin);
    decMultiplyOp(acc, lhs, rhs, &dcmul, &status);
    // Only Invalid operation (from sNaN or Inf * 0) is possible in
    // status; if either is seen than ignore fhs (in case it is
    // another sNaN) and set acc to NaN unless we had an sNaN
    // [decMultiplyOp leaves that to caller]
    // Note sNaN has to go through addOp to shorten payload if
    // necessary
    if ((status&DEC_Invalid_operation)!=0) {
      if (!(status&DEC_sNaN)) {         // but be true invalid
        decNumberZero(res);             // acc not yet set
        res->bits=DECNAN;
        break;
        }
      decNumberZero(&dzero);            // make 0 (any non-NaN would do)
      fhs=&dzero;                       // use that
      }
    #if DECCHECK
     else { // multiply was OK
      if (status!=0) printf("Status=%08lx after FMA multiply\n", status);
      }
    #endif
    // add the third operand and result -> res, and all is done
    decAddOp(res, acc, fhs, set, 0, &status);
    } while(0);                         // end protected

  if (allocbufa!=NULL) free(allocbufa); // drop any storage used
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberFMA

/* ------------------------------------------------------------------ */
/* decNumberInvert -- invert a Number, digitwise                      */
/*                                                                    */
/*   This computes C = ~A                                             */
/*                                                                    */
/*   res is C, the result.  C may be A (e.g., X=~X)                   */
/*   rhs is A                                                         */
/*   set is the context (used for result length and error report)     */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/*                                                                    */
/* Logical function restrictions apply (see above); a NaN is          */
/* returned with Invalid_operation if a restriction is violated.      */
/* ------------------------------------------------------------------ */
decNumber * decNumberInvert(decNumber *res, const decNumber *rhs,
                            decContext *set) {
  const Unit *ua, *msua;                // -> operand and its msu
  Unit  *uc, *msuc;                     // -> result and its msu
  Int   msudigs;                        // digits in res msu
  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
  #endif

  if (rhs->exponent!=0 || decNumberIsSpecial(rhs) || decNumberIsNegative(rhs)) {
    decStatus(res, DEC_Invalid_operation, set);
    return res;
    }
  // operand is valid
  ua=rhs->lsu;                          // bottom-up
  uc=res->lsu;                          // ..
  msua=ua+D2U(rhs->digits)-1;           // -> msu of rhs
  msuc=uc+D2U(set->digits)-1;           // -> msu of result
  msudigs=MSUDIGITS(set->digits);       // [faster than remainder]
  for (; uc<=msuc; ua++, uc++) {        // Unit loop
    Unit a;                             // extract unit
    Int  i, j;                          // work
    if (ua>msua) a=0;
     else a=*ua;
    *uc=0;                              // can now write back
    // always need to examine all bits in rhs
    // This loop could be unrolled and/or use BIN2BCD tables
    for (i=0; i<DECDPUN; i++) {
      if ((~a)&1) *uc=*uc+(Unit)powers[i];   // effect INVERT
      j=a%10;
      a=a/10;
      if (j>1) {
        decStatus(res, DEC_Invalid_operation, set);
        return res;
        }
      if (uc==msuc && i==msudigs-1) break;   // just did final digit
      } // each digit
    } // each unit
  // [here uc-1 is the msu of the result]
  res->digits=decGetDigits(res->lsu, uc-res->lsu);
  res->exponent=0;                      // integer
  res->bits=0;                          // sign=0
  return res;  // [no status to set]
  } // decNumberInvert

/* ------------------------------------------------------------------ */
/* decNumberLn -- natural logarithm                                   */
/*                                                                    */
/*   This computes C = ln(A)                                          */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
................................................................................
  // apply significant status
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberLn

/* ------------------------------------------------------------------ */
/* decNumberLogB - get adjusted exponent, by 754r rules               */
/*                                                                    */
/*   This computes C = adjustedexponent(A)                            */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*   set is the context, used only for digits and status              */
/*                                                                    */
/* C must have space for 10 digits (A might have 10**9 digits and     */
/* an exponent of +999999999, or one digit and an exponent of         */
/* -1999999999).                                                      */
/*                                                                    */
/* This returns the adjusted exponent of A after (in theory) padding  */
/* with zeros on the right to set->digits digits while keeping the    */
/* same value.  The exponent is not limited by emin/emax.             */
/*                                                                    */
/* Notable cases:                                                     */
/*   A<0 -> Use |A|                                                   */
/*   A=0 -> -Infinity (Division by zero)                              */
/*   A=Infinite -> +Infinity (Exact)                                  */
/*   A=1 exactly -> 0 (Exact)                                         */
/*   NaNs are propagated as usual                                     */
/* ------------------------------------------------------------------ */
decNumber * decNumberLogB(decNumber *res, const decNumber *rhs,
                          decContext *set) {
  uInt status=0;                   // accumulator

  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
  #endif

  // NaNs as usual; Infinities return +Infinity; 0->oops
  if (decNumberIsNaN(rhs)) decNaNs(res, rhs, NULL, set, &status);
   else if (decNumberIsInfinite(rhs)) decNumberCopyAbs(res, rhs);
   else if (decNumberIsZero(rhs)) {
    decNumberZero(res);                 // prepare for Infinity
    res->bits=DECNEG|DECINF;            // -Infinity
    status|=DEC_Division_by_zero;       // as per 754r
    }
   else { // finite non-zero
    Int ae=rhs->exponent+rhs->digits-1; // adjusted exponent
    decNumberFromInt32(res, ae);        // lay it out
    }

  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberLogB

/* ------------------------------------------------------------------ */
/* decNumberLog10 -- logarithm in base 10                             */
/*                                                                    */
/*   This computes C = log10(A)                                       */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
................................................................................

  // buffers for a and b working decimals
  // (adjustment calculator, same size)
  decNumber bufa[D2N(DECBUFFER+2)];
  decNumber *allocbufa=NULL;       // -> allocated bufa, iff allocated
  decNumber *a=bufa;               // temporary a
  decNumber bufb[D2N(DECBUFFER+2)];
  decNumber *allocbufb=NULL;       // -> allocated bufb, iff allocated
  decNumber *b=bufb;               // temporary b
  decNumber bufw[D2N(10)];         // working 2-10 digit number
  decNumber *w=bufw;               // ..
  #if DECSUBSET
  decNumber *allocrhs=NULL;        // non-NULL if rounded rhs allocated
  #endif

................................................................................
      decCopyFit(w, rhs, &aset, &residue, &copystat); // copy & shorten
      // if exact and the digit is 1, rhs is a power of 10
      if (!(copystat&DEC_Inexact) && w->lsu[0]==1) {
        // the exponent, conveniently, is the power of 10; making
        // this the result needs a little care as it might not fit,
        // so first convert it into the working number, and then move
        // to res
        decNumberFromInt32(w, w->exponent);
        residue=0;
        decCopyFit(res, w, set, &residue, &status); // copy & round
        decFinish(res, set, &residue, &status);     // cleanup/set flags
        break;
        } // not a power of 10
      } // not a candidate for exact

................................................................................
  #endif
  return res;
  } // decNumberLog10

/* ------------------------------------------------------------------ */
/* decNumberMax -- compare two Numbers and return the maximum         */
/*                                                                    */
/*   This computes C = A ? B, returning the maximum by 754R rules     */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for set->digits digits.                          */
................................................................................
  decCompareOp(res, lhs, rhs, set, COMPMAX, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberMax

/* ------------------------------------------------------------------ */
/* decNumberMaxMag -- compare and return the maximum by magnitude     */
/*                                                                    */
/*   This computes C = A ? B, returning the maximum by 754R rules     */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/* ------------------------------------------------------------------ */
decNumber * decNumberMaxMag(decNumber *res, const decNumber *lhs,
                         const decNumber *rhs, decContext *set) {
  uInt status=0;                        // accumulator
  decCompareOp(res, lhs, rhs, set, COMPMAXMAG, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberMaxMag

/* ------------------------------------------------------------------ */
/* decNumberMin -- compare two Numbers and return the minimum         */
/*                                                                    */
/*   This computes C = A ? B, returning the minimum by 754R rules     */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for set->digits digits.                          */
................................................................................
  decCompareOp(res, lhs, rhs, set, COMPMIN, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberMin

/* ------------------------------------------------------------------ */
/* decNumberMinMag -- compare and return the minimum by magnitude     */
/*                                                                    */
/*   This computes C = A ? B, returning the minimum by 754R rules     */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/* ------------------------------------------------------------------ */
decNumber * decNumberMinMag(decNumber *res, const decNumber *lhs,
                         const decNumber *rhs, decContext *set) {
  uInt status=0;                        // accumulator
  decCompareOp(res, lhs, rhs, set, COMPMINMAG, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberMinMag

/* ------------------------------------------------------------------ */
/* decNumberMinus -- prefix minus operator                            */
/*                                                                    */
/*   This computes C = 0 - A                                          */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*   set is the context                                               */
/*                                                                    */
/* See also decNumberCopyNegate for a quiet bitwise version of this.  */
/* C must have space for set->digits digits.                          */
/* ------------------------------------------------------------------ */
/* Simply use AddOp for the subtract, which will do the necessary.    */
/* ------------------------------------------------------------------ */
decNumber * decNumberMinus(decNumber *res, const decNumber *rhs,
                           decContext *set) {
  decNumber dzero;
................................................................................
  decAddOp(res, &dzero, rhs, set, DECNEG, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberMinus

/* ------------------------------------------------------------------ */
/* decNumberNextMinus -- next towards -Infinity                       */
/*                                                                    */
/*   This computes C = A - infinitesimal, rounded towards -Infinity   */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*   set is the context                                               */
/*                                                                    */
/* This is a generalization of 754r NextDown.                         */
/* ------------------------------------------------------------------ */
decNumber * decNumberNextMinus(decNumber *res, const decNumber *rhs,
                               decContext *set) {
  decNumber dtiny;                           // constant
  decContext workset=*set;                   // work
  uInt status=0;                             // accumulator
  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
  #endif

  // +Infinity is the special case
  if ((rhs->bits&(DECINF|DECNEG))==DECINF) {
    decSetMaxValue(res, set);                // is +ve
    // there is no status to set
    return res;
    }
  decNumberZero(&dtiny);                     // start with 0
  dtiny.lsu[0]=1;                            // make number that is ..
  dtiny.exponent=DEC_MIN_EMIN-1;             // .. smaller than tiniest
  workset.round=DEC_ROUND_FLOOR;
  decAddOp(res, rhs, &dtiny, &workset, DECNEG, &status);
  status&=DEC_Invalid_operation|DEC_sNaN;    // only sNaN Invalid please
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberNextMinus

/* ------------------------------------------------------------------ */
/* decNumberNextPlus -- next towards +Infinity                        */
/*                                                                    */
/*   This computes C = A + infinitesimal, rounded towards +Infinity   */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*   set is the context                                               */
/*                                                                    */
/* This is a generalization of 754r NextUp.                           */
/* ------------------------------------------------------------------ */
decNumber * decNumberNextPlus(decNumber *res, const decNumber *rhs,
                              decContext *set) {
  decNumber dtiny;                           // constant
  decContext workset=*set;                   // work
  uInt status=0;                             // accumulator
  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
  #endif

  // -Infinity is the special case
  if ((rhs->bits&(DECINF|DECNEG))==(DECINF|DECNEG)) {
    decSetMaxValue(res, set);
    res->bits=DECNEG;                        // negative
    // there is no status to set
    return res;
    }
  decNumberZero(&dtiny);                     // start with 0
  dtiny.lsu[0]=1;                            // make number that is ..
  dtiny.exponent=DEC_MIN_EMIN-1;             // .. smaller than tiniest
  workset.round=DEC_ROUND_CEILING;
  decAddOp(res, rhs, &dtiny, &workset, 0, &status);
  status&=DEC_Invalid_operation|DEC_sNaN;    // only sNaN Invalid please
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberNextPlus

/* ------------------------------------------------------------------ */
/* decNumberNextToward -- next towards rhs                            */
/*                                                                    */
/*   This computes C = A +/- infinitesimal, rounded towards           */
/*   +/-Infinity in the direction of B, as per 754r nextafter rules   */
/*                                                                    */
/*   res is C, the result.  C may be A or B.                          */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*                                                                    */
/* This is a generalization of 754r NextAfter.                        */
/* ------------------------------------------------------------------ */
decNumber * decNumberNextToward(decNumber *res, const decNumber *lhs,
                                const decNumber *rhs, decContext *set) {
  decNumber dtiny;                           // constant
  decContext workset=*set;                   // work
  Int result;                                // ..
  uInt status=0;                             // accumulator
  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  #endif

  if (decNumberIsNaN(lhs) || decNumberIsNaN(rhs)) {
    decNaNs(res, lhs, rhs, set, &status);
    }
   else { // Is numeric, so no chance of sNaN Invalid, etc.
    result=decCompare(lhs, rhs, 0);     // sign matters
    if (result==BADINT) status|=DEC_Insufficient_storage; // rare
     else { // valid compare
      if (result==0) decNumberCopySign(res, lhs, rhs); // easy
       else { // differ: need NextPlus or NextMinus
        uByte sub;                      // add or subtract
        if (result<0) {                 // lhs<rhs, do nextplus
          // -Infinity is the special case
          if ((lhs->bits&(DECINF|DECNEG))==(DECINF|DECNEG)) {
            decSetMaxValue(res, set);
            res->bits=DECNEG;           // negative
            return res;                 // there is no status to set
            }
          workset.round=DEC_ROUND_CEILING;
          sub=0;                        // add, please
          } // plus
         else {                         // lhs>rhs, do nextminus
          // +Infinity is the special case
          if ((lhs->bits&(DECINF|DECNEG))==DECINF) {
            decSetMaxValue(res, set);
            return res;                 // there is no status to set
            }
          workset.round=DEC_ROUND_FLOOR;
          sub=DECNEG;                   // subtract, please
          } // minus
        decNumberZero(&dtiny);          // start with 0
        dtiny.lsu[0]=1;                 // make number that is ..
        dtiny.exponent=DEC_MIN_EMIN-1;  // .. smaller than tiniest
        decAddOp(res, lhs, &dtiny, &workset, sub, &status); // + or -
        // turn off exceptions if the result is a normal number
        // (including Nmin), otherwise let all status through
        if (decNumberIsNormal(res, set)) status=0;
        } // unequal
      } // compare OK
    } // numeric
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberNextToward

/* ------------------------------------------------------------------ */
/* decNumberOr -- OR two Numbers, digitwise                           */
/*                                                                    */
/*   This computes C = A | B                                          */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X|X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context (used for result length and error report)     */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/*                                                                    */
/* Logical function restrictions apply (see above); a NaN is          */
/* returned with Invalid_operation if a restriction is violated.      */
/* ------------------------------------------------------------------ */
decNumber * decNumberOr(decNumber *res, const decNumber *lhs,
                        const decNumber *rhs, decContext *set) {
  const Unit *ua, *ub;                  // -> operands
  const Unit *msua, *msub;              // -> operand msus
  Unit  *uc, *msuc;                     // -> result and its msu
  Int   msudigs;                        // digits in res msu
  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  #endif

  if (lhs->exponent!=0 || decNumberIsSpecial(lhs) || decNumberIsNegative(lhs)
   || rhs->exponent!=0 || decNumberIsSpecial(rhs) || decNumberIsNegative(rhs)) {
    decStatus(res, DEC_Invalid_operation, set);
    return res;
    }
  // operands are valid
  ua=lhs->lsu;                          // bottom-up
  ub=rhs->lsu;                          // ..
  uc=res->lsu;                          // ..
  msua=ua+D2U(lhs->digits)-1;           // -> msu of lhs
  msub=ub+D2U(rhs->digits)-1;           // -> msu of rhs
  msuc=uc+D2U(set->digits)-1;           // -> msu of result
  msudigs=MSUDIGITS(set->digits);       // [faster than remainder]
  for (; uc<=msuc; ua++, ub++, uc++) {  // Unit loop
    Unit a, b;                          // extract units
    if (ua>msua) a=0;
     else a=*ua;
    if (ub>msub) b=0;
     else b=*ub;
    *uc=0;                              // can now write back
    if (a|b) {                          // maybe 1 bits to examine
      Int i, j;
      // This loop could be unrolled and/or use BIN2BCD tables
      for (i=0; i<DECDPUN; i++) {
        if ((a|b)&1) *uc=*uc+(Unit)powers[i];     // effect OR
        j=a%10;
        a=a/10;
        j|=b%10;
        b=b/10;
        if (j>1) {
          decStatus(res, DEC_Invalid_operation, set);
          return res;
          }
        if (uc==msuc && i==msudigs-1) break;      // just did final digit
        } // each digit
      } // non-zero
    } // each unit
  // [here uc-1 is the msu of the result]
  res->digits=decGetDigits(res->lsu, uc-res->lsu);
  res->exponent=0;                      // integer
  res->bits=0;                          // sign=0
  return res;  // [no status to set]
  } // decNumberOr

/* ------------------------------------------------------------------ */
/* decNumberPlus -- prefix plus operator                              */
/*                                                                    */
/*   This computes C = 0 + A                                          */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*   set is the context                                               */
/*                                                                    */
/* See also decNumberCopy for a quiet bitwise version of this.        */
/* C must have space for set->digits digits.                          */
/* ------------------------------------------------------------------ */
/* This simply uses AddOp; Add will take fast path after preparing A. */
/* Performance is a concern here, as this routine is often used to    */
/* check operands and apply rounding and overflow/underflow testing.  */
/* ------------------------------------------------------------------ */
decNumber * decNumberPlus(decNumber *res, const decNumber *rhs,
                          decContext *set) {
  decNumber dzero;
  uInt status=0;                        // accumulator

  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
  #endif

  decNumberZero(&dzero);                // make 0
  dzero.exponent=rhs->exponent;         // [no coefficient expansion]
  decAddOp(res, &dzero, rhs, set, 0, &status);
................................................................................
        if (allocrhs==NULL) break;
        rhs=allocrhs;
        }
      }
    #endif
    // [following code does not require input rounding]

    // Infinities copy through; NaNs need usual treatment
    if (decNumberIsNaN(rhs)) {
      decNaNs(res, rhs, NULL, set, &status);
      break;
      }

    // reduce result to the requested length and copy to result
    decCopyFit(res, rhs, set, &residue, &status); // copy & round
    decFinish(res, set, &residue, &status);       // cleanup/set flags
    decTrim(res, set, 1, &dropped);               // normalize in place
    } while(0);                              // end protected

  #if DECSUBSET
  if (allocrhs !=NULL) free(allocrhs);       // ..
  #endif
  if (status!=0) decStatus(res, status, set);// then report status
  return res;
................................................................................
      }
    #endif
    // [following code does not require input rounding]

    // handle NaNs and rhs Infinity (lhs infinity is harder)
    if (SPECIALARGS) {
      if (decNumberIsNaN(lhs) || decNumberIsNaN(rhs)) { // NaNs
        decNaNs(res, lhs, rhs, set, &status);
        break;}
      if (decNumberIsInfinite(rhs)) {   // rhs Infinity
        Flag rhsneg=rhs->bits&DECNEG;   // save rhs sign
        if (decNumberIsNegative(lhs)    // lhs<0
         && !decNumberIsZero(lhs))      // ..
          status|=DEC_Invalid_operation;
         else {                         // lhs >=0
................................................................................
          }
        if (i==31) break;          // that was the last bit
        if (!seenbit) continue;    // no need to square 1
        decMultiplyOp(dac, dac, dac, &aset, &status); // dac=dac*dac [square]
        } /*i*/ // 32 bits

      // complete internal overflow or underflow processing
      if (status & (DEC_Overflow|DEC_Underflow)) {
        #if DECSUBSET
        // If subset, and power was negative, reverse the kind of -erflow
        // [1/x not yet done]
        if (!set->extended && decNumberIsNegative(rhs)) {
          if (status & DEC_Overflow)
            status^=DEC_Overflow | DEC_Underflow | DEC_Subnormal;
           else { // trickier -- Underflow may or may not be set
................................................................................
      #endif
      } // rhs integer path

    // reduce result to the requested length and copy to result
    decCopyFit(res, dac, set, &residue, &status);
    decFinish(res, set, &residue, &status);  // final cleanup
    #if DECSUBSET
    if (!set->extended) decTrim(res, set, 0, &dropped); // trailing zeros
    #endif
    } while(0);                         // end protected

  if (allocdac!=NULL) free(allocdac);   // drop any storage used
  if (allocinv!=NULL) free(allocinv);   // ..
  #if DECSUBSET
  if (alloclhs!=NULL) free(alloclhs);   // ..
................................................................................
  decDivideOp(res, lhs, rhs, set, REMNEAR, &status);
  if (status!=0) decStatus(res, status, set);
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberRemainderNear

/* ------------------------------------------------------------------ */
/* decNumberRotate -- rotate the coefficient of a Number left/right   */
/*                                                                    */
/*   This computes C = A rot B  (in base ten and rotating set->digits */
/*   digits).                                                         */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=XrotX)       */
/*   lhs is A                                                         */
/*   rhs is B, the number of digits to rotate (-ve to right)          */
/*   set is the context                                               */
/*                                                                    */
/* The digits of the coefficient of A are rotated to the left (if B   */
/* is positive) or to the right (if B is negative) without adjusting  */
/* the exponent or the sign of A.  If lhs->digits is less than        */
/* set->digits the coefficient is padded with zeros on the left       */
/* before the rotate.  Any leading zeros in the result are removed    */
/* as usual.                                                          */
/*                                                                    */
/* B must be an integer (q=0) and in the range -set->digits through   */
/* +set->digits.                                                      */
/* C must have space for set->digits digits.                          */
/* NaNs are propagated as usual.  Infinities are unaffected (but      */
/* B must be valid).  No status is set unless B is invalid or an      */
/* operand is an sNaN.                                                */
/* ------------------------------------------------------------------ */
decNumber * decNumberRotate(decNumber *res, const decNumber *lhs,
                           const decNumber *rhs, decContext *set) {
  uInt status=0;              // accumulator
  Int  rotate;                // rhs as an Int

  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  #endif

  // NaNs propagate as normal
  if (decNumberIsNaN(lhs) || decNumberIsNaN(rhs))
    decNaNs(res, lhs, rhs, set, &status);
   // rhs must be an integer
   else if (decNumberIsInfinite(rhs) || rhs->exponent!=0)
    status=DEC_Invalid_operation;
   else { // both numeric, rhs is an integer
    rotate=decGetInt(rhs);                   // [cannot fail]
    if (rotate==BADINT                       // something bad ..
     || rotate==BIGODD || rotate==BIGEVEN    // .. very big ..
     || abs(rotate)>set->digits)             // .. or out of range
      status=DEC_Invalid_operation;
     else {                                  // rhs is OK
      decNumberCopy(res, lhs);
      // convert -ve rotate to equivalent positive rotation
      if (rotate<0) rotate=set->digits+rotate;
      if (rotate!=0 && rotate!=set->digits   // zero or full rotation
       && !decNumberIsInfinite(res)) {       // lhs was infinite
        // left-rotate to do; 0 < rotate < set->digits
        uInt units, shift;                   // work
        uInt msudigits;                      // digits in result msu
        Unit *msu=res->lsu+D2U(res->digits)-1;    // current msu
        Unit *msumax=res->lsu+D2U(set->digits)-1; // rotation msu
        for (msu++; msu<=msumax; msu++) *msu=0;   // ensure high units=0
        res->digits=set->digits;                  // now full-length
        msudigits=MSUDIGITS(res->digits);         // actual digits in msu

        // rotation here is done in-place, in three steps
        // 1. shift all to least up to one unit to unit-align final
        //    lsd [any digits shifted out are rotated to the left,
        //    abutted to the original msd (which may require split)]
        //
        //    [if there are no whole units left to rotate, the
        //    rotation is now complete]
        //
        // 2. shift to least, from below the split point only, so that
        //    the final msd is in the right place in its Unit [any
        //    digits shifted out will fit exactly in the current msu,
        //    left aligned, no split required]
        //
        // 3. rotate all the units by reversing left part, right
        //    part, and then whole
        //
        // example: rotate right 8 digits (2 units + 2), DECDPUN=3.
        //
        //   start: 00a bcd efg hij klm npq
        //
        //      1a  000 0ab cde fgh|ijk lmn [pq saved]
        //      1b  00p qab cde fgh|ijk lmn
        //
        //      2a  00p qab cde fgh|00i jkl [mn saved]
        //      2b  mnp qab cde fgh|00i jkl
        //
        //      3a  fgh cde qab mnp|00i jkl
        //      3b  fgh cde qab mnp|jkl 00i
        //      3c  00i jkl mnp qab cde fgh

        // Step 1: amount to shift is the partial right-rotate count
        rotate=set->digits-rotate;      // make it right-rotate
        units=rotate/DECDPUN;           // whole units to rotate
        shift=rotate%DECDPUN;           // left-over digits count
        if (shift>0) {                  // not an exact number of units
          uInt save=res->lsu[0]%powers[shift];    // save low digit(s)
          decShiftToLeast(res->lsu, D2U(res->digits), shift);
          if (shift>msudigits) {        // msumax-1 needs >0 digits
            uInt rem=save%powers[shift-msudigits];// split save
            *msumax=(Unit)(save/powers[shift-msudigits]); // and insert
            *(msumax-1)=*(msumax-1)
                       +(Unit)(rem*powers[DECDPUN-(shift-msudigits)]); // ..
            }
           else { // all fits in msumax
            *msumax=*msumax+(Unit)(save*powers[msudigits-shift]); // [maybe *1]
            }
          } // digits shift needed

        // If whole units to rotate...
        if (units>0) {                  // some to do
          // Step 2: the units to touch are the whole ones in rotate,
          //   if any, and the shift is DECDPUN-msudigits (which may be
          //   0, again)
          shift=DECDPUN-msudigits;
          if (shift>0) {                // not an exact number of units
            uInt save=res->lsu[0]%powers[shift];  // save low digit(s)
            decShiftToLeast(res->lsu, units, shift);
            *msumax=*msumax+(Unit)(save*powers[msudigits]);
            } // partial shift needed

          // Step 3: rotate the units array using triple reverse
          // (reversing is easy and fast)
          decReverse(res->lsu+units, msumax);     // left part
          decReverse(res->lsu, res->lsu+units-1); // right part
          decReverse(res->lsu, msumax);           // whole
          } // whole units to rotate
        // the rotation may have left an undetermined number of zeros
        // on the left, so true length needs to be calculated
        res->digits=decGetDigits(res->lsu, msumax-res->lsu+1);
        } // rotate needed
      } // rhs OK
    } // numerics
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberRotate

/* ------------------------------------------------------------------ */
/* decNumberSameQuantum -- test for equal exponents                   */
/*                                                                    */
/*   res is the result number, which will contain either 0 or 1       */
/*   lhs is a number to test                                          */
/*   rhs is the second (usually a pattern)                            */
................................................................................
    }
   else if (lhs->exponent==rhs->exponent) ret=1;

  decNumberZero(res);              // OK to overwrite an operand now
  *res->lsu=ret;
  return res;
  } // decNumberSameQuantum

/* ------------------------------------------------------------------ */
/* decNumberScaleB -- multiply by a power of 10                       */
/*                                                                    */
/* This computes C = A x 10**B where B is an integer (q=0) with       */
/* maximum magnitude 2*(emax+digits)                                  */
/*                                                                    */
/*   res is C, the result.  C may be A or B                           */
/*   lhs is A, the number to adjust                                   */
/*   rhs is B, the requested power of ten to use                      */
/*   set is the context                                               */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/*                                                                    */
/* The result may underflow or overflow.                              */
/* ------------------------------------------------------------------ */
decNumber * decNumberScaleB(decNumber *res, const decNumber *lhs,
                            const decNumber *rhs, decContext *set) {
  Int  reqexp;                // requested exponent change [B]
  uInt status=0;              // accumulator
  Int  residue;               // work

  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  #endif

  // Handle special values except lhs infinite
  if (decNumberIsNaN(lhs) || decNumberIsNaN(rhs))
    decNaNs(res, lhs, rhs, set, &status);
    // rhs must be an integer
   else if (decNumberIsInfinite(rhs) || rhs->exponent!=0)
    status=DEC_Invalid_operation;
   else {
    // lhs is a number; rhs is a finite with q==0
    reqexp=decGetInt(rhs);                   // [cannot fail]
    if (reqexp==BADINT                       // something bad ..
     || reqexp==BIGODD || reqexp==BIGEVEN    // .. very big ..
     || abs(reqexp)>(2*(set->digits+set->emax))) // .. or out of range
      status=DEC_Invalid_operation;
     else {                                  // rhs is OK
      decNumberCopy(res, lhs);               // all done if infinite lhs
      if (!decNumberIsInfinite(res)) {       // prepare to scale
        res->exponent+=reqexp;               // adjust the exponent
        residue=0;
        decFinalize(res, set, &residue, &status); // .. and check
        } // finite LHS
      } // rhs OK
    } // rhs finite
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberScaleB

/* ------------------------------------------------------------------ */
/* decNumberShift -- shift the coefficient of a Number left or right  */
/*                                                                    */
/*   This computes C = A << B or C = A >> -B  (in base ten).          */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X<<X)        */
/*   lhs is A                                                         */
/*   rhs is B, the number of digits to shift (-ve to right)           */
/*   set is the context                                               */
/*                                                                    */
/* The digits of the coefficient of A are shifted to the left (if B   */
/* is positive) or to the right (if B is negative) without adjusting  */
/* the exponent or the sign of A.                                     */
/*                                                                    */
/* B must be an integer (q=0) and in the range -set->digits through   */
/* +set->digits.                                                      */
/* C must have space for set->digits digits.                          */
/* NaNs are propagated as usual.  Infinities are unaffected (but      */
/* B must be valid).  No status is set unless B is invalid or an      */
/* operand is an sNaN.                                                */
/* ------------------------------------------------------------------ */
decNumber * decNumberShift(decNumber *res, const decNumber *lhs,
                           const decNumber *rhs, decContext *set) {
  uInt status=0;              // accumulator
  Int  shift;                 // rhs as an Int

  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  #endif

  // NaNs propagate as normal
  if (decNumberIsNaN(lhs) || decNumberIsNaN(rhs))
    decNaNs(res, lhs, rhs, set, &status);
   // rhs must be an integer
   else if (decNumberIsInfinite(rhs) || rhs->exponent!=0)
    status=DEC_Invalid_operation;
   else { // both numeric, rhs is an integer
    shift=decGetInt(rhs);                    // [cannot fail]
    if (shift==BADINT                        // something bad ..
     || shift==BIGODD || shift==BIGEVEN      // .. very big ..
     || abs(shift)>set->digits)              // .. or out of range
      status=DEC_Invalid_operation;
     else {                                  // rhs is OK
      decNumberCopy(res, lhs);
      if (shift!=0 && !decNumberIsInfinite(res)) { // something to do
        if (shift>0) {                       // to left
          if (shift==set->digits) {          // removing all
            *res->lsu=0;                     // so place 0
            res->digits=1;                   // ..
            }
           else {                            //
            // first remove leading digits if necessary
            if (res->digits+shift>set->digits) {
              decDecap(res, res->digits+shift-set->digits);
              // that updated res->digits; may have gone to 1 (for a
              // single digit or for zero
              }
            if (res->digits>1 || *res->lsu)  // if non-zero..
              res->digits=decShiftToMost(res->lsu, res->digits, shift);
            } // partial left
          } // left
         else { // to right
          if (-shift>=res->digits) {         // discarding all
            *res->lsu=0;                     // so place 0
            res->digits=1;                   // ..
            }
           else {
            decShiftToLeast(res->lsu, D2U(res->digits), -shift);
            res->digits-=(-shift);
            }
          } // to right
        } // non-0 non-Inf shift
      } // rhs OK
    } // numerics
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberShift

/* ------------------------------------------------------------------ */
/* decNumberSquareRoot -- square root operator                        */
/*                                                                    */
/*   This computes C = squareroot(A)                                  */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
................................................................................

    // handle infinities and NaNs
    if (SPECIALARG) {
      if (decNumberIsInfinite(rhs)) {         // an infinity
        if (decNumberIsNegative(rhs)) status|=DEC_Invalid_operation;
         else decNumberCopy(res, rhs);        // +Infinity
        }
       else decNaNs(res, rhs, NULL, set, &status); // a NaN
      break;
      }

    // calculate the ideal (preferred) exponent [floor(exp/2)]
    // [We would like to write: ideal=rhs->exponent>>1, but this
    // generates a compiler warning.  Generated code is the same.]
    ideal=(rhs->exponent&~1)/2;         // target
................................................................................

    // Process Subnormals
    decFinalize(a, set, &residue, &status);

    // count droppable zeros [after any subnormal rounding] by
    // trimming a copy
    decNumberCopy(b, a);
    decTrim(b, set, 1, &dropped);            // [drops trailing zeros]

    // Finally set Inexact and Rounded.  The answer can only be exact if
    // it is short enough so that squaring it could fit in set->digits,
    // so this is the only (relatively rare) time a careful check is
    // needed
    if (b->digits*2-1 > set->digits) {       // cannot fit
      status|=DEC_Inexact|DEC_Rounded;
................................................................................
            }
          }
        }
      }

    // make sure there is a full complement of digits for normal
    // inexact results
    if ((status & DEC_Inexact)
     && (a->exponent+a->digits-1>=set->emin)) {
      Int shift=set->digits-a->digits;
      if (shift>0) {
        a->digits=decShiftToMost(a->lsu, a->digits, shift);
        a->exponent-=shift;                  // adjust the exponent.
        }
      }
    decNumberCopy(res, a);                   // a is now the result
................................................................................
  #if DECCHECK
  decCheckInexact(res, set);
  #endif
  return res;
  } // decNumberSubtract

/* ------------------------------------------------------------------ */
/* decNumberToIntegralExact -- round-to-integral-value with InExact   */
/* decNumberToIntegralValue -- round-to-integral-value                */
/*                                                                    */
/*   res is the result                                                */
/*   rhs is input number                                              */
/*   set is the context                                               */
/*                                                                    */
/* res must have space for any value of rhs.                          */
/*                                                                    */
/* This implements the IEEE special operators and therefore treats    */
/* special values as valid.  For finite numbers it returns            */
/* rescale(rhs, 0) if rhs->exponent is <0.                            */
/* Otherwise the result is rhs (so no error is possible, except for   */
/* sNaN).                                                             */
/*                                                                    */
/* The context is used for rounding mode and status after sNaN, but   */
/* the digits setting is ignored.  The Exact version will signal      */
/* Inexact if the result differs numerically from rhs; the other      */
/* never signals Inexact.                                             */
/* ------------------------------------------------------------------ */
decNumber * decNumberToIntegralExact(decNumber *res, const decNumber *rhs,
                                     decContext *set) {
  decNumber dn;
  decContext workset;              // working context
  uInt status=0;                   // accumulator

  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, set)) return res;
  #endif

  // handle infinities and NaNs
  if (SPECIALARG) {

    if (decNumberIsInfinite(rhs)) decNumberCopy(res, rhs); // an Infinity
     else decNaNs(res, rhs, NULL, set, &status); // a NaN


    }

   else { // finite
    // have a finite number; no error possible (res must be big enough)
    if (rhs->exponent>=0) return decNumberCopy(res, rhs);
    // that was easy, but if negative exponent there is work to do...
    workset=*set;                  // clone rounding, etc.
    workset.digits=rhs->digits;    // no length rounding
    workset.traps=0;               // no traps
    decNumberZero(&dn);            // make a number with exponent 0
    decNumberQuantize(res, rhs, &dn, &workset);
    status|=workset.status;
    }
  if (status!=0) decStatus(res, status, set);
  return res;
  } // decNumberToIntegralExact

decNumber * decNumberToIntegralValue(decNumber *res, const decNumber *rhs,
                                     decContext *set) {
  decContext workset=*set;         // working context
  workset.traps=0;                 // no traps
  decNumberToIntegralExact(res, rhs, &workset);
  // this never affects set, except for sNaNs; NaN will have been set
  // or propagated already, so no need to call decStatus
  set->status|=workset.status&DEC_Invalid_operation;
  return res;
  } // decNumberToIntegralValue

/* ------------------------------------------------------------------ */
/* decNumberXor -- XOR two Numbers, digitwise                         */
/*                                                                    */
/*   This computes C = A ^ B                                          */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X^X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context (used for result length and error report)     */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/*                                                                    */
/* Logical function restrictions apply (see above); a NaN is          */
/* returned with Invalid_operation if a restriction is violated.      */
/* ------------------------------------------------------------------ */
decNumber * decNumberXor(decNumber *res, const decNumber *lhs,
                         const decNumber *rhs, decContext *set) {
  const Unit *ua, *ub;                  // -> operands
  const Unit *msua, *msub;              // -> operand msus
  Unit  *uc, *msuc;                     // -> result and its msu
  Int   msudigs;                        // digits in res msu
  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  #endif

  if (lhs->exponent!=0 || decNumberIsSpecial(lhs) || decNumberIsNegative(lhs)
   || rhs->exponent!=0 || decNumberIsSpecial(rhs) || decNumberIsNegative(rhs)) {
    decStatus(res, DEC_Invalid_operation, set);
    return res;
    }
  // operands are valid
  ua=lhs->lsu;                          // bottom-up
  ub=rhs->lsu;                          // ..
  uc=res->lsu;                          // ..
  msua=ua+D2U(lhs->digits)-1;           // -> msu of lhs
  msub=ub+D2U(rhs->digits)-1;           // -> msu of rhs
  msuc=uc+D2U(set->digits)-1;           // -> msu of result
  msudigs=MSUDIGITS(set->digits);       // [faster than remainder]
  for (; uc<=msuc; ua++, ub++, uc++) {  // Unit loop
    Unit a, b;                          // extract units
    if (ua>msua) a=0;
     else a=*ua;
    if (ub>msub) b=0;
     else b=*ub;
    *uc=0;                              // can now write back
    if (a|b) {                          // maybe 1 bits to examine
      Int i, j;
      // This loop could be unrolled and/or use BIN2BCD tables
      for (i=0; i<DECDPUN; i++) {
        if ((a^b)&1) *uc=*uc+(Unit)powers[i];     // effect XOR
        j=a%10;
        a=a/10;
        j|=b%10;
        b=b/10;
        if (j>1) {
          decStatus(res, DEC_Invalid_operation, set);
          return res;
          }
        if (uc==msuc && i==msudigs-1) break;      // just did final digit
        } // each digit
      } // non-zero
    } // each unit
  // [here uc-1 is the msu of the result]
  res->digits=decGetDigits(res->lsu, uc-res->lsu);
  res->exponent=0;                      // integer
  res->bits=0;                          // sign=0
  return res;  // [no status to set]
  } // decNumberXor


/* ================================================================== */
/* Utility routines                                                   */
/* ================================================================== */

/* ------------------------------------------------------------------ */
/* decNumberClass -- return the decClass of a decNumber               */
/*   dn -- the decNumber to test                                      */
/*   set -- the context to use for Emin                               */
/*   returns the decClass enum                                        */
/* ------------------------------------------------------------------ */
enum decClass decNumberClass(const decNumber *dn, decContext *set) {
  if (decNumberIsSpecial(dn)) {
    if (decNumberIsQNaN(dn)) return DEC_CLASS_QNAN;
    if (decNumberIsSNaN(dn)) return DEC_CLASS_SNAN;
    // must be an infinity
    if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_INF;
    return DEC_CLASS_POS_INF;
    }
  // is finite
  if (decNumberIsNormal(dn, set)) { // most common
    if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_NORMAL;
    return DEC_CLASS_POS_NORMAL;
    }
  // is subnormal or zero
  if (decNumberIsZero(dn)) {    // most common
    if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_ZERO;
    return DEC_CLASS_POS_ZERO;
    }
  if (decNumberIsNegative(dn)) return DEC_CLASS_NEG_SUBNORMAL;
  return DEC_CLASS_POS_SUBNORMAL;
  } // decNumberClass

/* ------------------------------------------------------------------ */
/* decNumberClassToString -- convert decClass to a string             */
/*                                                                    */
/*  eclass is a valid decClass                                        */
/*  returns a constant string describing the class (max 13+1 chars)   */
/* ------------------------------------------------------------------ */
const char *decNumberClassToString(enum decClass eclass) {
  if (eclass==DEC_CLASS_POS_NORMAL)    return DEC_ClassString_PN;
  if (eclass==DEC_CLASS_NEG_NORMAL)    return DEC_ClassString_NN;
  if (eclass==DEC_CLASS_POS_ZERO)      return DEC_ClassString_PZ;
  if (eclass==DEC_CLASS_NEG_ZERO)      return DEC_ClassString_NZ;
  if (eclass==DEC_CLASS_POS_SUBNORMAL) return DEC_ClassString_PS;
  if (eclass==DEC_CLASS_NEG_SUBNORMAL) return DEC_ClassString_NS;
  if (eclass==DEC_CLASS_POS_INF)       return DEC_ClassString_PI;
  if (eclass==DEC_CLASS_NEG_INF)       return DEC_ClassString_NI;
  if (eclass==DEC_CLASS_QNAN)          return DEC_ClassString_QN;
  if (eclass==DEC_CLASS_SNAN)          return DEC_ClassString_SN;
  return DEC_ClassString_UN;           // Unknown
  } // decNumberClassToString

/* ------------------------------------------------------------------ */
/* decNumberCopy -- copy a number                                     */
/*                                                                    */
/*   dest is the target decNumber                                     */
/*   src  is the source decNumber                                     */
/*   returns dest                                                     */
................................................................................
    // overlap.  However, this explicit loop is faster in short cases.
    d=dest->lsu+1;                           // -> first destination
    smsup=src->lsu+D2U(src->digits);         // -> source msu+1
    for (s=src->lsu+1; s<smsup; s++, d++) *d=*s;
    }
  return dest;
  } // decNumberCopy

/* ------------------------------------------------------------------ */
/* decNumberCopyAbs -- quiet absolute value operator                  */
/*                                                                    */
/*   This sets C = abs(A)                                             */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/* No exception or error can occur; this is a quiet bitwise operation.*/
/* See also decNumberAbs for a checking version of this.              */
/* ------------------------------------------------------------------ */
decNumber * decNumberCopyAbs(decNumber *res, const decNumber *rhs) {
  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, DECUNUSED)) return res;
  #endif
  decNumberCopy(res, rhs);
  res->bits&=~DECNEG;                   // turn off sign
  return res;
  } // decNumberCopyAbs

/* ------------------------------------------------------------------ */
/* decNumberCopyNegate -- quiet negate value operator                 */
/*                                                                    */
/*   This sets C = negate(A)                                          */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   rhs is A                                                         */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/* No exception or error can occur; this is a quiet bitwise operation.*/
/* See also decNumberMinus for a checking version of this.            */
/* ------------------------------------------------------------------ */
decNumber * decNumberCopyNegate(decNumber *res, const decNumber *rhs) {
  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, DECUNUSED)) return res;
  #endif
  decNumberCopy(res, rhs);
  res->bits^=DECNEG;                    // invert the sign
  return res;
  } // decNumberCopyNegate

/* ------------------------------------------------------------------ */
/* decNumberCopySign -- quiet copy and set sign operator              */
/*                                                                    */
/*   This sets C = A with the sign of B                               */
/*                                                                    */
/*   res is C, the result.  C may be A                                */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/* No exception or error can occur; this is a quiet bitwise operation.*/
/* ------------------------------------------------------------------ */
decNumber * decNumberCopySign(decNumber *res, const decNumber *lhs,
                              const decNumber *rhs) {
  uByte sign;                           // rhs sign
  #if DECCHECK
  if (decCheckOperands(res, DECUNUSED, rhs, DECUNUSED)) return res;
  #endif
  sign=rhs->bits & DECNEG;              // save sign bit
  decNumberCopy(res, lhs);
  res->bits&=~DECNEG;                   // clear the sign
  res->bits|=sign;                      // set from rhs
  return res;
  } // decNumberCopySign

/* ------------------------------------------------------------------ */
/* decNumberGetBCD -- get the coefficient in BCD8                     */
/*   dn is the source decNumber                                       */
/*   bcd is the uInt array that will receive dn->digits BCD bytes,    */
/*     most-significant at offset 0                                   */
/*   returns bcd                                                      */
/*                                                                    */
/* bcd must have at least dn->digits bytes.  No error is possible; if */
/* dn is a NaN or Infinite, digits must be 1 and the coefficient 0.   */
/* ------------------------------------------------------------------ */
uByte * decNumberGetBCD(const decNumber *dn, uint8_t *bcd) {
  uByte *ub=bcd+dn->digits-1;      // -> lsd
  const Unit *up=dn->lsu;          // Unit pointer, -> lsu

  #if DECDPUN==1                   // trivial simple copy
    for (; ub>=bcd; ub--, up++) *ub=*up;
  #else                            // chopping needed
    uInt u=*up;                    // work
    uInt cut=DECDPUN;              // downcounter through unit
    for (; ub>=bcd; ub--) {
      *ub=(uByte)(u%10);           // [*6554 trick inhibits, here]
      u=u/10;
      cut--;
      if (cut>0) continue;         // more in this unit
      up++;
      u=*up;
      cut=DECDPUN;
      }
  #endif
  return bcd;
  } // decNumberGetBCD

/* ------------------------------------------------------------------ */
/* decNumberSetBCD -- set (replace) the coefficient from BCD8         */
/*   dn is the target decNumber                                       */
/*   bcd is the uInt array that will source n BCD bytes, most-        */
/*     significant at offset 0                                        */
/*   n is the number of digits in the source BCD array (bcd)          */
/*   returns dn                                                       */
/*                                                                    */
/* dn must have space for at least n digits.  No error is possible;   */
/* if dn is a NaN, or Infinite, or is to become a zero, n must be 1   */
/* and bcd[0] zero.                                                   */
/* ------------------------------------------------------------------ */
decNumber * decNumberSetBCD(decNumber *dn, const uByte *bcd, uInt n) {
  Unit *up=dn->lsu+D2U(dn->digits)-1;   // -> msu [target pointer]
  const uByte *ub=bcd;                  // -> source msd

  #if DECDPUN==1                        // trivial simple copy
    for (; ub<bcd+n; ub++, up--) *up=*ub;
  #else                                 // some assembly needed
    // calculate how many digits in msu, and hence first cut
    Int cut=MSUDIGITS(n);               // [faster than remainder]
    for (;up>=dn->lsu; up--) {          // each Unit from msu
      *up=0;                            // will take <=DECDPUN digits
      for (; cut>0; ub++, cut--) *up=X10(*up)+*ub;
      cut=DECDPUN;                      // next Unit has all digits
      }
  #endif
  dn->digits=n;                         // set digit count
  return dn;
  } // decNumberSetBCD

/* ------------------------------------------------------------------ */
/* decNumberIsNormal -- test normality of a decNumber                 */
/*   dn is the decNumber to test                                      */
/*   set is the context to use for Emin                               */
/*   returns 1 if |dn| is finite and >=Nmin, 0 otherwise              */
/* ------------------------------------------------------------------ */
Int decNumberIsNormal(const decNumber *dn, decContext *set) {
  Int ae;                               // adjusted exponent
  #if DECCHECK
  if (decCheckOperands(DECUNUSED, DECUNUSED, dn, set)) return 0;
  #endif

  if (decNumberIsSpecial(dn)) return 0; // not finite
  if (decNumberIsZero(dn)) return 0;    // not non-zero

  ae=dn->exponent+dn->digits-1;         // adjusted exponent
  if (ae<set->emin) return 0;           // is subnormal
  return 1;
  } // decNumberIsNormal

/* ------------------------------------------------------------------ */
/* decNumberIsSubnormal -- test subnormality of a decNumber           */
/*   dn is the decNumber to test                                      */
/*   set is the context to use for Emin                               */
/*   returns 1 if |dn| is finite, non-zero, and <Nmin, 0 otherwise    */
/* ------------------------------------------------------------------ */
Int decNumberIsSubnormal(const decNumber *dn, decContext *set) {
  Int ae;                               // adjusted exponent
  #if DECCHECK
  if (decCheckOperands(DECUNUSED, DECUNUSED, dn, set)) return 0;
  #endif

  if (decNumberIsSpecial(dn)) return 0; // not finite
  if (decNumberIsZero(dn)) return 0;    // not non-zero

  ae=dn->exponent+dn->digits-1;         // adjusted exponent
  if (ae<set->emin) return 1;           // is subnormal
  return 0;
  } // decNumberIsSubnormal

/* ------------------------------------------------------------------ */
/* decNumberTrim -- remove insignificant zeros                        */
/*                                                                    */
/*   dn is the number to trim                                         */
/*   returns dn                                                       */
/*                                                                    */
/* All fields are updated as required.  This is a utility operation,  */
/* so special values are unchanged and no error is possible.          */
/* ------------------------------------------------------------------ */
decNumber * decNumberTrim(decNumber *dn) {
  Int  dropped;                    // work
  decContext set;                  // ..
  #if DECCHECK
  if (decCheckOperands(DECUNUSED, DECUNUSED, dn, DECUNUSED)) return dn;
  #endif
  decContextDefault(&set, DEC_INIT_BASE);    // clamp=0
  return decTrim(dn, &set, 0, &dropped);
  } // decNumberTrim

/* ------------------------------------------------------------------ */
/* decNumberVersion -- return the name and version of this module     */
/*                                                                    */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
................................................................................
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*   negate is DECNEG if rhs should be negated, or 0 otherwise        */
/*   status accumulates status for the caller                         */
/*                                                                    */
/* C must have space for set->digits digits.                          */
/* Inexact in status must be 0 for correct Exact zero sign in result  */
/* ------------------------------------------------------------------ */
/* If possible, the coefficient is calculated directly into C.        */
/* However, if:                                                       */
/*   -- a digits+1 calculation is needed because the numbers are      */
/*      unaligned and span more than set->digits digits               */
/*   -- a carry to digits+1 digits looks possible                     */
/*   -- C is the same as A or B, and the result would destructively   */
/*      overlap the A or B coefficient                                */
/* then the result must be calculated into a temporary buffer.  In    */
/* this case a local (stack) buffer is used if possible, and only if  */
/* too long for that does malloc become the final resort.             */
/*                                                                    */
/* Misalignment is handled as follows:                                */
/*   Apad: (AExp>BExp) Swap operands and proceed as for BExp>AExp.    */
/*   BPad: Apply the padding by a combination of shifting (whole      */
/*         units) and multiplication (part units).                    */
/*                                                                    */
/* Addition, especially x=x+1, is speed-critical.                     */
/* The static buffer is larger than might be expected to allow for    */
/* calls from higher-level funtions (notable exp).                    */
/* ------------------------------------------------------------------ */
static decNumber * decAddOp(decNumber *res, const decNumber *lhs,
                            const decNumber *rhs, decContext *set,
                            uByte negate, uInt *status) {
  #if DECSUBSET
  decNumber *alloclhs=NULL;        // non-NULL if rounded lhs allocated
  decNumber *allocrhs=NULL;        // .., rhs
................................................................................
  Int   rhsshift;                  // working shift (in Units)
  Int   maxdigits;                 // longest logical length
  Int   mult;                      // multiplier
  Int   residue;                   // rounding accumulator
  uByte bits;                      // result bits
  Flag  diffsign;                  // non-0 if arguments have different sign
  Unit  *acc;                      // accumulator for result
  Unit  accbuff[SD2U(DECBUFFER*2+20)]; // local buffer [*2+20 reduces many
                                   // allocations when called from
                                   // other operations, notable exp]
  Unit  *allocacc=NULL;            // -> allocated acc buffer, iff allocated
  Int   reqdigits=set->digits;     // local copy; requested DIGITS
  Int   padding;                   // work

  #if DECCHECK
  if (decCheckOperands(res, lhs, rhs, set)) return res;
  #endif
................................................................................

    // note whether signs differ [used all paths]
    diffsign=(Flag)((lhs->bits^rhs->bits^negate)&DECNEG);

    // handle infinities and NaNs
    if (SPECIALARGS) {                  // a special bit set
      if (SPECIALARGS & (DECSNAN | DECNAN))  // a NaN
        decNaNs(res, lhs, rhs, set, status);
       else { // one or two infinities
        if (decNumberIsInfinite(lhs)) { // LHS is infinity
          // two infinities with different signs is invalid
          if (decNumberIsInfinite(rhs) && diffsign) {
            *status|=DEC_Invalid_operation;
            break;
            }
................................................................................
          res->exponent+=adjust;                // set the exponent.
          }
      #if DECSUBSET
        } // extended
      #endif
      decFinish(res, set, &residue, status);      // clean and finalize
      break;}

    // [NB: both fastpath and mainpath code below assume these cases
    // (notably 0-0) have already been handled]

    // calculate the padding needed to align the operands
    padding=rhs->exponent-lhs->exponent;

    // Fastpath cases where the numbers are aligned and normal, the RHS
    // is all in one unit, no operand rounding is needed, and no carry,
    // lengthening, or borrow is needed
    if (padding==0
        && rhs->digits<=DECDPUN
        && rhs->exponent>=set->emin     // [some normals drop through]
        && rhs->exponent<=set->emax-set->digits+1 // [could clamp]
        && rhs->digits<=reqdigits
        && lhs->digits<=reqdigits) {
      Int partial=*lhs->lsu;
      if (!diffsign) {                  // adding
        partial+=*rhs->lsu;
        if ((partial<=DECDPUNMAX)       // result fits in unit
         && (lhs->digits>=DECDPUN ||    // .. and no digits-count change
................................................................................
    // other) padding with up to DIGITS-1 trailing zeros may be
    // needed; then apply rounding (as exotic rounding modes may be
    // affected by the residue).
    rhsshift=0;               // rhs shift to left (padding) in Units
    bits=lhs->bits;           // assume sign is that of LHS
    mult=1;                   // likely multiplier

    // [if padding==0 the operands are aligned; no padding is needed]
    if (padding!=0) {
      // some padding needed; always pad the RHS, as any required
      // padding can then be effected by a simple combination of
      // shifts and a multiply
      Flag swapped=0;
      if (padding<0) {                  // LHS needs the padding
        const decNumber *t;
................................................................................
    if ((maxdigits>=reqdigits)          // is, or could be, too large
     || (res==rhs && rhsshift>0)) {     // destructive overlap
      // buffer needed, choose it; units for maxdigits digits will be
      // needed, +1 Unit for carry or borrow
      Int need=D2U(maxdigits)+1;
      acc=accbuff;                      // assume use local buffer
      if (need*sizeof(Unit)>sizeof(accbuff)) {
        // printf("malloc add %ld %ld\n", need, sizeof(accbuff));
        allocacc=(Unit *)malloc(need*sizeof(Unit));
        if (allocacc==NULL) {           // hopeless -- abandon
          *status|=DEC_Insufficient_storage;
          break;}
        acc=allocacc;
        }
      }
................................................................................

    res->bits=(uByte)(bits&DECNEG);     // it's now safe to overwrite..
    res->exponent=lhs->exponent;        // .. operands (even if aliased)

    #if DECTRACE
      decDumpAr('A', lhs->lsu, D2U(lhs->digits));
      decDumpAr('B', rhs->lsu, D2U(rhs->digits));
      printf("  :h: %ld %ld\n", rhsshift, mult);
    #endif

    // add [A+B*m] or subtract [A+B*(-m)]
    res->digits=decUnitAddSub(lhs->lsu, D2U(lhs->digits),
                              rhs->lsu, D2U(rhs->digits),
                              rhsshift, acc, mult)
               *DECDPUN;           // [units -> digits]
................................................................................
    // apply checks and rounding
    decFinish(res, set, &residue, status);

    // "When the sum of two operands with opposite signs is exactly
    // zero, the sign of that sum shall be '+' in all rounding modes
    // except round toward -Infinity, in which mode that sign shall be
    // '-'."  [Subset zeros also never have '-', set by decFinish.]
    if (ISZERO(res) && diffsign

     #if DECSUBSET
     && set->extended
     #endif
     && (*status&DEC_Inexact)==0) {
      if (set->round==DEC_ROUND_FLOOR) res->bits|=DECNEG;   // sign -
                                  else res->bits&=~DECNEG;  // sign +
      }
................................................................................
/*     Return (Result is defined by Var1)                             */
/*                                                                    */
/* ------------------------------------------------------------------ */
/* Two working buffers are needed during the division; one (digits+   */
/* 1) to accumulate the result, and the other (up to 2*digits+1) for  */
/* long subtractions.  These are acc and var1 respectively.           */
/* var1 is a copy of the lhs coefficient, var2 is the rhs coefficient.*/
/* The static buffers may be larger than might be expected to allow   */
/* for calls from higher-level funtions (notable exp).                */
/* ------------------------------------------------------------------ */
static decNumber * decDivideOp(decNumber *res,
                               const decNumber *lhs, const decNumber *rhs,
                               decContext *set, Flag op, uInt *status) {
  #if DECSUBSET
  decNumber *alloclhs=NULL;        // non-NULL if rounded lhs allocated
  decNumber *allocrhs=NULL;        // .., rhs
  #endif
  Unit  accbuff[SD2U(DECBUFFER+DECDPUN+10)]; // local buffer
  Unit  *acc=accbuff;              // -> accumulator array for result
  Unit  *allocacc=NULL;            // -> allocated buffer, iff allocated
  Unit  *accnext;                  // -> where next digit will go
  Int   acclength;                 // length of acc needed [Units]
  Int   accunits;                  // count of units accumulated
  Int   accdigits;                 // count of digits accumulated

................................................................................
    // [following code does not require input rounding]

    bits=(lhs->bits^rhs->bits)&DECNEG;  // assumed sign for divisions

    // handle infinities and NaNs
    if (SPECIALARGS) {                  // a special bit set
      if (SPECIALARGS & (DECSNAN | DECNAN)) { // one or two NaNs
        decNaNs(res, lhs, rhs, set, status);
        break;
        }
      // one or two infinities
      if (decNumberIsInfinite(lhs)) {   // LHS (dividend) is infinite
        if (decNumberIsInfinite(rhs) || // two infinities are invalid ..
            op & (REMAINDER | REMNEAR)) { // as is remainder of infinity
          *status|=DEC_Invalid_operation;
................................................................................

    /* Long (slow) division is needed; roll up the sleeves... */

    // The accumulator will hold the quotient of the division.
    // If it needs to be too long for stack storage, then allocate.
    acclength=D2U(reqdigits+DECDPUN);   // in Units
    if (acclength*sizeof(Unit)>sizeof(accbuff)) {
      // printf("malloc dvacc %ld units\n", acclength);
      allocacc=(Unit *)malloc(acclength*sizeof(Unit));
      if (allocacc==NULL) {             // hopeless -- abandon
        *status|=DEC_Insufficient_storage;
        break;}
      acc=allocacc;                     // use the allocated space
      }

................................................................................
    // [Note: unused units do not participate in decUnitAddSub data]
    maxdigits=rhs->digits+reqdigits-1;
    if (lhs->digits>maxdigits) maxdigits=lhs->digits;
    var1units=D2U(maxdigits)+2;
    // allocate a guard unit above msu1 for REMAINDERNEAR
    if (!(op&DIVIDE)) var1units++;
    if ((var1units+1)*sizeof(Unit)>sizeof(varbuff)) {
      // printf("malloc dvvar %ld units\n", var1units+1);
      varalloc=(Unit *)malloc((var1units+1)*sizeof(Unit));
      if (varalloc==NULL) {             // hopeless -- abandon
        *status|=DEC_Insufficient_storage;
        break;}
      var1=varalloc;                    // use the allocated space
      }

................................................................................
        thisunit=(Unit)(thisunit+mult);      // accumulate
        // subtract var1-var2, into var1; only the overlap needs
        // processing, as this is an in-place calculation
        shift=var2ulen-var2units;
        #if DECTRACE
          decDumpAr('1', &var1[shift], var1units-shift);
          decDumpAr('2', var2, var2units);
          printf("m=%ld\n", -mult);
        #endif
        decUnitAddSub(&var1[shift], var1units-shift,
                      var2, var2units, 0,
                      &var1[shift], -mult);
        #if DECTRACE
          decDumpAr('#', &var1[shift], var1units-shift);
        #endif
................................................................................
          Int exp=lhs->exponent;             // save min(exponents)
          if (rhs->exponent<exp) exp=rhs->exponent;
          decNumberZero(res);                // 0 coefficient
          #if DECSUBSET
          if (set->extended)
          #endif
          res->exponent=exp;                 // .. with proper exponent
          decFinish(res, set, &residue, status);   // might clamp
          break;
          }
        // note if the quotient was odd
        if (*accnext & 0x01) wasodd=1;       // acc is odd
        quotlsu=accnext;                     // save in case need to reinspect
        quotdigits=accdigits;                // ..

................................................................................

        // Now correct the result if doing remainderNear; if it
        // (looking just at coefficients) is > rhs/2, or == rhs/2 and
        // the integer was odd then the result should be rem-rhs.
        if (op&REMNEAR) {
          Int compare, tarunits;        // work
          Unit *up;                     // ..


          // calculate remainder*2 into the var1 buffer (which has
          // 'headroom' of an extra unit and hence enough space)
          // [a dedicated 'double' loop would be faster, here]
          tarunits=decUnitAddSub(accnext, accunits, accnext, accunits,
                                 0, accnext, 1);
          // decDumpAr('r', accnext, tarunits);

................................................................................
    // Now the coefficient.
    decSetCoeff(res, set, accnext, accdigits, &residue, status);

    decFinish(res, set, &residue, status);   // final cleanup

    #if DECSUBSET
    // If a divide then strip trailing zeros if subset [after round]
    if (!set->extended && (op==DIVIDE)) decTrim(res, set, 0, &dropped);
    #endif
    } while(0);                              // end protected

  if (varalloc!=NULL) free(varalloc);   // drop any storage used
  if (allocacc!=NULL) free(allocacc);   // ..
  #if DECSUBSET
  if (allocrhs!=NULL) free(allocrhs);   // ..
................................................................................
/* units for continuing processing.  Despite this overhead, the       */
/* fastpath can speed up some 16-digit operations by 10x (and much    */
/* more for higher-precision calculations).                           */
/*                                                                    */
/* A buffer always has to be used for the accumulator; in the         */
/* fastpath, buffers are also always needed for the chunked copies of */
/* of the operand coefficients.                                       */
/* Static buffers are larger than needed just for multiply, to allow  */
/* for calls from other operations (notably exp).                     */
/* ------------------------------------------------------------------ */
#define FASTMUL (DECUSE64 && DECDPUN<5)
static decNumber * decMultiplyOp(decNumber *res, const decNumber *lhs,
                                 const decNumber *rhs, decContext *set,
                                 uInt *status) {
  Int    accunits;                 // Units of accumulator in use
  Int    exponent;                 // work
  Int    residue=0;                // rounding residue
  uByte  bits;                     // result sign
  Unit  *acc;                      // -> accumulator Unit array
  Int    needbytes;                // size calculator
  void  *allocacc=NULL;            // -> allocated accumulator, iff allocated
  Unit  accbuff[SD2U(DECBUFFER*4+1)]; // buffer (+1 for DECBUFFER==0,
                                   // *4 for calls from other operations)
  const Unit *mer, *mermsup;       // work
  Int   madlength;                 // Units in multiplicand
  Int   shift;                     // Units to shift multiplicand by

  #if FASTMUL
    // if DECDPUN is 1 or 3 work in base 10**9, otherwise
    // (DECDPUN is 2 or 4) then work in base 10**8
    #if DECDPUN & 1                // odd
      #define FASTBASE 1000000000  // base
      #define FASTDIGS          9  // digits in base
................................................................................
      #define FASTBASE  100000000
      #define FASTDIGS          8
      #define FASTLAZY       1844  // carry resolution point [1->1844]
    #endif
    // three buffers are used, two for chunked copies of the operands
    // (base 10**8 or base 10**9) and one base 2**64 accumulator with
    // lazy carry evaluation
    uInt   zlhibuff[(DECBUFFER*2+1)/8+1]; // buffer (+1 for DECBUFFER==0)
    uInt  *zlhi=zlhibuff;                 // -> lhs array
    uInt  *alloclhi=NULL;                 // -> allocated buffer, iff allocated
    uInt   zrhibuff[(DECBUFFER*2+1)/8+1]; // buffer (+1 for DECBUFFER==0)
    uInt  *zrhi=zrhibuff;                 // -> rhs array
    uInt  *allocrhi=NULL;                 // -> allocated buffer, iff allocated
    uLong  zaccbuff[(DECBUFFER*2+1)/4+2]; // buffer (+1 for DECBUFFER==0)

    // [allocacc is shared for both paths, as only one will run]
    uLong *zacc=zaccbuff;          // -> accumulator array for exact result
    #if DECDPUN==1
    Int    zoff;                   // accumulator offset
    #endif
    uInt  *lip, *rip;              // item pointers
    uInt  *lmsi, *rmsi;            // most significant items
................................................................................

  // precalculate result sign
  bits=(uByte)((lhs->bits^rhs->bits)&DECNEG);

  // handle infinities and NaNs
  if (SPECIALARGS) {               // a special bit set
    if (SPECIALARGS & (DECSNAN | DECNAN)) { // one or two NaNs
      decNaNs(res, lhs, rhs, set, status);
      return res;}
    // one or two infinities; Infinity * 0 is invalid
    if (((lhs->bits & DECINF)==0 && ISZERO(lhs))
      ||((rhs->bits & DECINF)==0 && ISZERO(rhs))) {
      *status|=DEC_Invalid_operation;
      return res;}
    decNumberZero(res);
................................................................................
    // both their magnitudes are large.  If there was a wrap, set a
    // safe very negative exponent, from which decFinalize() will
    // raise a hard underflow shortly.
    exponent=lhs->exponent+rhs->exponent;    // calculate exponent
    if (lhs->exponent<0 && rhs->exponent<0 && exponent>0)
      exponent=-2*DECNUMMAXE;                // force underflow
    res->exponent=exponent;                  // OK to overwrite now


    // Set the coefficient.  If any rounding, residue records
    decSetCoeff(res, set, acc, res->digits, &residue, status);
    decFinish(res, set, &residue, status);   // final cleanup
    } while(0);                         // end protected

  if (allocacc!=NULL) free(allocacc);   // drop any storage used
................................................................................
/*    which dominates when the number of iterations is small (less    */
/*    than ten) or when rhs is short.  As an example, the adjustment  */
/*    x**10,000,000 needs 31 multiplications, all but one full-width. */
/*                                                                    */
/* 3. The restrictions (especially precision) could be raised with    */
/*    care, but the full decNumber range seems very hard within the   */
/*    32-bit limits.                                                  */
/*                                                                    */
/* 4. The working precisions for the static buffers are twice the     */
/*    obvious size to allow for calls from decNumberPower.            */
/* ------------------------------------------------------------------ */
decNumber * decExpOp(decNumber *res, const decNumber *rhs,
                         decContext *set, uInt *status) {
  uInt ignore=0;                   // working status
  Int h;                           // adjusted exponent for 0.xxxx
  Int p;                           // working precision
  Int residue;                     // rounding residue
  uInt needbytes;                  // for space calculations
  const decNumber *x=rhs;          // (may point to safe copy later)
  decContext aset, tset, dset;     // working contexts
  Int comp;                        // work

  // the argument is often copied to normalize it, so (unusually) it
  // is treated like other buffers, using DECBUFFER, +1 in case
  // DECBUFFER is 0
  decNumber bufr[D2N(DECBUFFER*2+1)];
  decNumber *allocrhs=NULL;        // non-NULL if rhs buffer allocated

  // the working precision will be no more than set->digits+8+1
  // so for on-stack buffers DECBUFFER+9 is used, +1 in case DECBUFFER
  // is 0 (and twice that for the accumulator)

  // buffer for t, term (working precision plus)
  decNumber buft[D2N(DECBUFFER*2+9+1)];
  decNumber *allocbuft=NULL;       // -> allocated buft, iff allocated
  decNumber *t=buft;               // term
  // buffer for a, accumulator (working precision * 2), at least 9
  decNumber bufa[D2N(DECBUFFER*4+18+1)];
  decNumber *allocbufa=NULL;       // -> allocated bufa, iff allocated
  decNumber *a=bufa;               // accumulator
  // decNumber for the divisor term; this needs at most 9 digits
  // and so can be fixed size [16 so can use standard context]
  decNumber bufd[D2N(16)];
  decNumber *d=bufd;               // divisor
  decNumber numone;                // constant 1
................................................................................
  do {                                  // protect allocated storage
    if (SPECIALARG) {                   // handle infinities and NaNs
      if (decNumberIsInfinite(rhs)) {   // an infinity
        if (decNumberIsNegative(rhs))   // -Infinity -> +0
          decNumberZero(res);
         else decNumberCopy(res, rhs);  // +Infinity -> self
        }
       else decNaNs(res, rhs, NULL, set, status); // a NaN
      break;}

    if (ISZERO(rhs)) {                  // zeros -> exact 1
      decNumberZero(res);               // make clean 1
      *res->lsu=1;                      // ..
      break;}                           // [no status to set]

................................................................................
    // 1.0000001.  Hence, tiny will be 0.0000004  if digits=7 and x>0
    // or 0.00000004 if digits=7 and x<0.  If RHS not larger than
    // this then the result will be 1.000000
    decNumberZero(d);                   // clean
    *d->lsu=4;                          // set 4 ..
    d->exponent=-set->digits;           // * 10**(-d)
    if (decNumberIsNegative(rhs)) d->exponent--;  // negative case
    comp=decCompare(d, rhs, 1);         // signless compare
    if (comp==BADINT) {
      *status|=DEC_Insufficient_storage;
      break;}
    if (comp>=0) {                      // rhs < d
      Int shift=set->digits-1;
      decNumberZero(res);               // set 1
      *res->lsu=1;                      // ..
      res->digits=decShiftToMost(res->lsu, 1, shift);
      res->exponent=-shift;                  // make 1.0000...
      *status|=DEC_Inexact | DEC_Rounded;    // .. inexactly
      break;} // tiny
................................................................................
      // a=1, and the divisor d=2.

      // First determine the working precision.  From Hull & Abrham
      // this is set->digits+h+2.  However, if x is 'over-precise' we
      // need to allow for all its digits to potentially participate
      // (consider an x where all the excess digits are 9s) so in
      // this case use x->digits+h+2
      p=MAX(x->digits, set->digits)+h+2;     // [h<=8]

      // a and t are variable precision, and depend on p, so space
      // must be allocated for them if necessary

      // the accumulator needs to be able to hold 2p digits so that
      // the additions on the second and subsequent iterations are
      // sufficiently exact.
................................................................................
            && (a->digits>=p)) break;
        decAddOp(d, d, &numone, &dset, 0, &ignore);    // d=d+1
        } // iterate

      #if DECCHECK
      // just a sanity check; comment out test to show always
      if (iterations>p+3)
        printf("Exp iterations=%ld, status=%08lx, p=%ld, d=%ld\n",
               iterations, *status, p, x->digits);
      #endif
      } // h<=8

    // apply postconditioning: a=a**(10**h) -- this is calculated
    // at a slightly higher precision than Hull & Abrham suggest
    if (h>0) {
................................................................................
/* 4. An iteration might be saved by widening the LNnn table, and     */
/*    would certainly save at least one if it were made ten times     */
/*    bigger, too (for truncated fractions 0.100 through 0.999).      */
/*    However, for most practical evaluations, at least four or five  */
/*    iterations will be neede -- so this would only speed up by      */
/*    20-25% and that probably does not justify increasing the table  */
/*    size.                                                           */
/*                                                                    */
/* 5. The static buffers are larger than might be expected to allow   */
/*    for calls from decNumberPower.                                  */
/* ------------------------------------------------------------------ */
decNumber * decLnOp(decNumber *res, const decNumber *rhs,
                    decContext *set, uInt *status) {
  uInt ignore=0;                   // working status accumulator
  uInt needbytes;                  // for space calculations
  Int residue;                     // rounding residue
  Int r;                           // rhs=f*10**r [see below]
  Int p;                           // working precision
  Int pp;                          // precision for iteration
  Int t;                           // work

  // buffers for a (accumulator, typically precision+2) and b
  // (adjustment calculator, same size)
  decNumber bufa[D2N(DECBUFFER+12)];
  decNumber *allocbufa=NULL;       // -> allocated bufa, iff allocated
  decNumber *a=bufa;               // accumulator/work
  decNumber bufb[D2N(DECBUFFER*2+2)];
  decNumber *allocbufb=NULL;       // -> allocated bufa, iff allocated
  decNumber *b=bufb;               // adjustment/work

  decNumber  numone;               // constant 1
  decNumber  cmp;                  // work
  decContext aset, bset;           // working contexts

................................................................................
  do {                                  // protect allocated storage
    if (SPECIALARG) {                   // handle infinities and NaNs
      if (decNumberIsInfinite(rhs)) {   // an infinity
        if (decNumberIsNegative(rhs))   // -Infinity -> error
          *status|=DEC_Invalid_operation;
         else decNumberCopy(res, rhs);  // +Infinity -> self
        }
       else decNaNs(res, rhs, NULL, set, status); // a NaN
      break;}

    if (ISZERO(rhs)) {                  // +/- zeros -> -Infinity
      decNumberZero(res);               // make clean
      res->bits=DECINF|DECNEG;          // set - infinity
      break;}                           // [no status to set]

................................................................................
    //   ln(x) = ln(f) + ln(10)*r
    // Get the initial estimate for ln(f) from a small lookup
    // table (see above) indexed by the first two digits of f,
    // truncated.

    decContextDefault(&aset, DEC_INIT_DECIMAL64); // 16-digit extended
    r=rhs->exponent+rhs->digits;        // 'normalised' exponent
    decNumberFromInt32(a, r);           // a=r
    decNumberFromInt32(b, 2302585);     // b=ln(10) (2.302585)
    b->exponent=-6;                     //  ..
    decMultiplyOp(a, a, b, &aset, &ignore);  // a=a*b
    // now get top two digits of rhs into b by simple truncate and
    // force to integer
    residue=0;                          // (no residue)
    aset.digits=2; aset.round=DEC_ROUND_DOWN;
    decCopyFit(b, rhs, &aset, &residue, &ignore); // copy & shorten
    b->exponent=0;                      // make integer
    t=decGetInt(b);                     // [cannot fail]
    if (t<10) t=X10(t);                 // adjust single-digit b
    t=LNnn[t-10];                       // look up ln(b)
    decNumberFromInt32(b, t>>2);        // b=ln(b) coefficient
    b->exponent=-(t&3)-3;               // set exponent
    b->bits=DECNEG;                     // ln(0.10)->ln(0.99) always -ve
    aset.digits=16; aset.round=DEC_ROUND_HALF_EVEN; // restore
    decAddOp(a, a, b, &aset, 0, &ignore); // acc=a+b
    // the initial estimate is now in a, with up to 4 digits correct.
    // When rhs is at or near Nmax the estimate will be low, so we
    // will approach it from below, avoiding overflow when calling exp.
................................................................................
      aset.digits=pp;                        // working context
      bset.digits=pp+rhs->digits;            // wider context
      } // Newton's iteration

    #if DECCHECK
    // just a sanity check; remove the test to show always
    if (iterations>24)
      printf("Ln iterations=%ld, status=%08lx, p=%ld, d=%ld\n",
            iterations, *status, p, rhs->digits);
    #endif

    // Copy and round the result to res
    residue=1;                          // indicate dirt to right
    if (ISZERO(a)) residue=0;           // .. unless underflowed to 0
    aset.digits=set->digits;            // [use default rounding]
................................................................................
    #endif
    // [following code does not require input rounding]

    // Handle special values
    if (SPECIALARGS) {
      // NaNs get usual processing
      if (SPECIALARGS & (DECSNAN | DECNAN))
        decNaNs(res, lhs, rhs, set, status);
      // one infinity but not both is bad
      else if ((lhs->bits ^ rhs->bits) & DECINF)
        *status|=DEC_Invalid_operation;
      // both infinity: return lhs
      else decNumberCopy(res, lhs);          // [nop if in place]
      break;
      }
................................................................................
  return res;
  } // decQuantizeOp

/* ------------------------------------------------------------------ */
/* decCompareOp -- compare, min, or max two Numbers                   */
/*                                                                    */
/*   This computes C = A ? B and carries out one of four operations:  */
/*     COMPARE    -- returns the signum (as a number) giving the      */
/*                   result of a comparison unless one or both        */
/*                   operands is a NaN (in which case a NaN results)  */
/*     COMPSIG    -- as COMPARE except that a quiet NaN raises        */
/*                   Invalid operation.                               */
/*     COMPMAX    -- returns the larger of the operands, using the    */
/*                   754r maxnum operation                            */
/*     COMPMAXMAG -- ditto, comparing absolute values                 */
/*     COMPMIN    -- the 754r minnum operation                        */
/*     COMPMINMAG -- ditto, comparing absolute values                 */
/*     COMTOTAL   -- returns the signum (as a number) giving the      */
/*                   result of a comparison using 754r total ordering */
/*                                                                    */
/*   res is C, the result.  C may be A and/or B (e.g., X=X?X)         */
/*   lhs is A                                                         */
/*   rhs is B                                                         */
/*   set is the context                                               */
/*   op  is the operation flag                                        */
/*   status is the usual accumulator                                  */
/*                                                                    */
/* C must have space for one digit for COMPARE or set->digits for     */
/* COMPMAX, COMPMIN, COMPMAXMAG, or COMPMINMAG.                       */
/* ------------------------------------------------------------------ */
/* The emphasis here is on speed for common cases, and avoiding       */
/* coefficient comparison if possible.                                */
/* ------------------------------------------------------------------ */
decNumber * decCompareOp(decNumber *res, const decNumber *lhs,
                         const decNumber *rhs, decContext *set,
                         Flag op, uInt *status) {
................................................................................
        rhs=allocrhs;
        }
      }
    #endif
    // [following code does not require input rounding]

    // If total ordering then handle differing signs 'up front'
    if (op==COMPTOTAL) {                // total ordering
      if (decNumberIsNegative(lhs) & !decNumberIsNegative(rhs)) {
        result=-1;
        break;
        }
      if (!decNumberIsNegative(lhs) & decNumberIsNegative(rhs)) {
        result=+1;
        break;
................................................................................
        }
      }

    // handle NaNs specially; let infinities drop through
    // This assumes sNaN (even just one) leads to NaN.
    merged=(lhs->bits | rhs->bits) & (DECSNAN | DECNAN);
    if (merged) {                       // a NaN bit set
      if (op==COMPARE);                 // result will be NaN
       else if (op==COMPSIG)            // treat qNaN as sNaN
        *status|=DEC_Invalid_operation | DEC_sNaN;
       else if (op==COMPTOTAL) {        // total ordering, always finite
        // signs are known to be the same; compute the ordering here
        // as if the signs are both positive, then invert for negatives
        if (!decNumberIsNaN(lhs)) result=-1;
         else if (!decNumberIsNaN(rhs)) result=+1;
         // here if both NaNs
         else if (decNumberIsSNaN(lhs) && decNumberIsQNaN(rhs)) result=-1;
         else if (decNumberIsQNaN(lhs) && decNumberIsSNaN(rhs)) result=+1;
................................................................................
                                rhs->lsu, D2U(rhs->digits), 0);
          // [Error not possible, as these are 'aligned']
          } // both same NaNs
        if (decNumberIsNegative(lhs)) result=-result;
        break;
        } // total order

       else if (merged & DECSNAN);           // sNaN -> qNaN
       else { // here if MIN or MAX and one or two quiet NaNs
        // min or max -- 754r rules ignore single NaN
        if (!decNumberIsNaN(lhs) || !decNumberIsNaN(rhs)) {
          // just one NaN; force choice to be the non-NaN operand
          op=COMPMAX;
          if (lhs->bits & DECNAN) result=-1; // pick rhs
                             else result=+1; // pick lhs
          break;
          }
        } // max or min
      op=COMPNAN;                            // use special path
      decNaNs(res, lhs, rhs, set, status);   // propagate NaN
      break;
      }
    // have numbers
    if (op==COMPMAXMAG || op==COMPMINMAG) result=decCompare(lhs, rhs, 1);
     else result=decCompare(lhs, rhs, 0);    // sign matters
    } while(0);                              // end protected

  if (result==BADINT) *status|=DEC_Insufficient_storage; // rare
   else {
    if (op==COMPARE || op==COMPSIG ||op==COMPTOTAL) { // returning signum
      if (op==COMPTOTAL && result==0) {
        // operands are numerically equal or same NaN (and same sign,
        // tested first); if identical, leave result 0
        if (lhs->exponent!=rhs->exponent) {
          if (lhs->exponent<rhs->exponent) result=-1;
           else result=+1;
          if (decNumberIsNegative(lhs)) result=-result;
          } // lexp!=rexp
................................................................................
        } // total-order by exponent
      decNumberZero(res);               // [always a valid result]
      if (result!=0) {                  // must be -1 or +1
        *res->lsu=1;
        if (result<0) res->bits=DECNEG;
        }
      }
     else if (op==COMPNAN);             // special, drop through
     else {                             // MAX or MIN, non-NaN result
      Int residue=0;                    // rounding accumulator
      // choose the operand for the result
      const decNumber *choice;
      if (result==0) { // operands are numerically equal
        // choose according to sign then exponent (see 754r)
        uByte slhs=(lhs->bits & DECNEG);
................................................................................
          }
         else {                    // both positive
          if (lhs->exponent>rhs->exponent) result=+1;
                                      else result=-1;
          // [ditto]
          }
        } // numerically equal
      // here result will be non-0; reverse if looking for MIN
      if (op==COMPMIN || op==COMPMINMAG) result=-result;
      choice=(result>0 ? lhs : rhs);    // choose
      // copy chosen to result, rounding if need be
      decCopyFit(res, choice, set, &residue, status);
      decFinish(res, set, &residue, status);
      }
    }
  #if DECSUBSET
................................................................................
  Int   result;                    // result value
  Int   sigr;                      // rhs signum
  Int   compare;                   // work

  result=1;                                  // assume signum(lhs)
  if (ISZERO(lhs)) result=0;
  if (abs) {
    if (ISZERO(rhs)) return result;          // LHS wins or both 0
    // RHS is non-zero
    if (result==0) return -1;                // LHS is 0; RHS wins
    // [here, both non-zero, result=1]
    }
   else {                                    // signs matter
    if (result && decNumberIsNegative(lhs)) result=-1;
    sigr=1;                                  // compute signum(rhs)
    if (ISZERO(rhs)) sigr=0;
     else if (decNumberIsNegative(rhs)) sigr=-1;
    if (result > sigr) return +1;            // L > R, return 1
    if (result < sigr) return -1;            // L < R, return -1
    if (result==0) return 0;                   // both 0
    }

  // signums are the same; both are non-zero


  if ((lhs->bits | rhs->bits) & DECINF) {    // one or more infinities
    if (decNumberIsInfinite(rhs)) {
      if (decNumberIsInfinite(lhs)) result=0;// both infinite
       else result=-result;                  // only rhs infinite
      }
    return result;
    }

  // must compare the coefficients, allowing for exponents
  if (lhs->exponent>rhs->exponent) {         // LHS exponent larger
    // swap sides, and sign
    const decNumber *temp=lhs;
    lhs=rhs;
    rhs=temp;
    result=-result;
    }

  compare=decUnitCompare(lhs->lsu, D2U(lhs->digits),
                         rhs->lsu, D2U(rhs->digits),
                         rhs->exponent-lhs->exponent);

  if (compare!=BADINT) compare*=result;      // comparison succeeded
  return compare;
  } // decCompare

/* ------------------------------------------------------------------ */
/* decUnitCompare -- compare two >=0 integers in Unit arrays          */
/*                                                                    */
................................................................................
/*  returns -1, 0, or 1 for A<B, A==B, or A>B, or BADINT if failure   */
/*  (the only possible failure is an allocation error, which can      */
/*  only occur if E!=0)                                               */
/* ------------------------------------------------------------------ */
static Int decUnitCompare(const Unit *a, Int alength,
                          const Unit *b, Int blength, Int exp) {
  Unit  *acc;                      // accumulator for result
  Unit  accbuff[SD2U(DECBUFFER*2+1)]; // local buffer
  Unit  *allocacc=NULL;            // -> allocated acc buffer, iff allocated
  Int   accunits, need;            // units in use or needed for acc
  const Unit *l, *r, *u;           // work
  Int   expunits, exprem, result;  // ..

  if (exp==0) {                    // aligned; fastpath
    if (alength>blength) return 1;
................................................................................
  Int  add;                        // work
  #if DECDPUN<=4                   // myriadal, millenary, etc.
  Int  est;                        // estimated quotient
  #endif

  #if DECTRACE
  if (alength<1 || blength<1)
    printf("decUnitAddSub: alen blen m %ld %ld [%ld]\n", alength, blength, m);
  #endif

  maxC=c+alength;                  // A is usually the longer
  minC=c+blength;                  // .. and B the shorter
  if (bshift!=0) {                 // B is shifted; low As copy across
    minC+=bshift;
    // if in place [common], skip copy unless there's a gap [rare]
................................................................................
     else {
      *c=0;
      add=1;
      }
    }
  // add an extra unit iff it would be non-zero
  #if DECTRACE
    printf("UAS borrow: add %ld, carry %ld\n", add, carry);
  #endif
  if ((add-carry-1)!=0) {
    *c=(Unit)(add-carry-1);
    c++;                      // interesting, include it
    }
  return clsu-c;              // -ve result indicates borrowed
  } // decUnitAddSub

/* ------------------------------------------------------------------ */
/* decTrim -- trim trailing zeros or normalize                        */
/*                                                                    */
/*   dn is the number to trim or normalize                            */
/*   set is the context to use to check for clamp                     */
/*   all is 1 to remove all trailing zeros, 0 for just fraction ones  */
/*   dropped returns the number of discarded trailing zeros           */
/*   returns dn                                                       */
/*                                                                    */
/* If clamp is set in the context then the number of zeros trimmed    */
/* may be limited if the exponent is high.                            */
/* All fields are updated as required.  This is a utility operation,  */
/* so special values are unchanged and no error is possible.          */
/* ------------------------------------------------------------------ */
static decNumber * decTrim(decNumber *dn, decContext *set, Flag all,
                           Int *dropped) {
  Int   d, exp;                    // work
  uInt  cut;                       // ..
  Unit  *up;                       // -> current Unit

  #if DECCHECK
  if (decCheckOperands(dn, DECUNUSED, DECUNUSED, DECUNUSED)) return dn;
  #endif
................................................................................
      }
    cut++;                         // next power
    if (cut>DECDPUN) {             // need new Unit
      up++;
      cut=1;
      }
    } // d
  if (d==0) return dn;             // none to drop

  // may need to limit drop if clamping
  if (set->clamp) {
    Int maxd=set->emax-set->digits+1-dn->exponent;
    if (maxd<=0) return dn;        // nothing possible
    if (d>maxd) d=maxd;
    }

  // effect the drop
  decShiftToLeast(dn->lsu, D2U(dn->digits), d);
  dn->exponent+=d;                 // maintain numerical value
  dn->digits-=d;                   // new length
  *dropped=d;                      // report the count
  return dn;
  } // decTrim

/* ------------------------------------------------------------------ */
/* decReverse -- reverse a Unit array in place                        */
/*                                                                    */
/*   ulo    is the start of the array                                 */
/*   uhi    is the end of the array (highest Unit to include)         */
/*                                                                    */
/* The units ulo through uhi are reversed in place (if the number     */
/* of units is odd, the middle one is untouched).  Note that the      */
/* digit(s) in each unit are unaffected.                              */
/* ------------------------------------------------------------------ */
static void decReverse(Unit *ulo, Unit *uhi) {
  Unit temp;
  for (; ulo<uhi; ulo++, uhi--) {
    temp=*ulo;
    *ulo=*uhi;
    *uhi=temp;
    }
  return;
  } // decReverse

/* ------------------------------------------------------------------ */
/* decShiftToMost -- shift digits in array towards most significant   */
/*                                                                    */
/*   uar    is the array                                              */
/*   digits is the count of digits in use in the array                */
/*   shift  is the number of zeros to pad with (least significant);   */
................................................................................

/* ------------------------------------------------------------------ */
/* decShiftToLeast -- shift digits in array towards least significant */
/*                                                                    */
/*   uar   is the array                                               */
/*   units is length of the array, in units                           */
/*   shift is the number of digits to remove from the lsu end; it     */
/*     must be zero or positive and <= than units*DECDPUN.            */
/*                                                                    */
/*   returns the new length of the integer in the array, in units     */
/*                                                                    */
/* Removed digits are discarded (lost).  Units not required to hold   */
/* the final result are unchanged.                                    */
/* ------------------------------------------------------------------ */
static Int decShiftToLeast(Unit *uar, Int units, Int shift) {
  Unit  *target, *up;              // work
  Int   cut, count;                // work
  Int   quot, rem;                 // for division

  if (shift==0) return units;      // [fastpath] nothing to do
  if (shift==units*DECDPUN) {      // [fastpath] little to do
    *uar=0;                        // all digits cleared gives zero
    return 1;                      // leaves just the one
    }

  target=uar;                      // both paths
  cut=MSUDIGITS(shift);
  if (cut==DECDPUN) {              // unit-boundary case; easy
    up=uar+D2U(shift);
    for (; up<uar+units; target++, up++) *target=*up;
    return target-uar;
................................................................................
/*         guard and sticky information.  It may be:                  */
/*         6-9: rounding digit is >5                                  */
/*         5:   rounding digit is exactly half-way                    */
/*         1-4: rounding digit is <5 and >0                           */
/*         0:   the coefficient is exact                              */
/*        -1:   as 1, but the hidden digits are subtractive, that     */
/*              is, of the opposite sign to dn.  In this case the     */
/*              coefficient must be non-0.  This case occurs when     */
/*              subtracting a small number (which can be reduced to   */
/*              a sticky bit); see decAddOp.                          */
/*   status is the status accumulator, as usual                       */
/*                                                                    */
/* This routine applies rounding while keeping the length of the      */
/* coefficient constant.  The exponent and status are unchanged       */
/* except if:                                                         */
/*                                                                    */
/*   -- the coefficient was increased and is all nines (in which      */
................................................................................

  if (residue==0) return;     // nothing to apply

  bump=0;                     // assume a smooth ride

  // now decide whether, and how, to round, depending on mode
  switch (set->round) {
    case DEC_ROUND_05UP: {    // round zero or five up (for reround)
      // This is the same as DEC_ROUND_DOWN unless there is a
      // positive residue and the lsd of dn is 0 or 5, in which case
      // it is bumped; when residue is <0, the number is therefore
      // bumped down unless the final digit was 1 or 6 (in which
      // case it is bumped down and then up -- a no-op)
      Int lsd5=*dn->lsu%5;     // get lsd and quintate
      if (residue<0 && lsd5!=1) bump=-1;
       else if (residue>0 && lsd5==0) bump=1;
      // [bump==1 could be applied directly; use common path for clarity]
      break;} // r-05

    case DEC_ROUND_DOWN: {
      // no change, except if negative residue
      if (residue<0) bump=-1;
      break;} // r-d

    case DEC_ROUND_HALF_DOWN: {
      if (residue>5) bump=1;
................................................................................
       else {
        if (residue>0) bump=1;
        }
      break;} // r-f

    default: {      // e.g., DEC_ROUND_MAX
      *status|=DEC_Invalid_context;
      #if DECTRACE || (DECCHECK && DECVERB)
      printf("Unknown rounding mode: %d\n", set->round);
      #endif
      break;}
    } // switch

  // now bump the number, up or down, if need be
  if (bump==0) return;                       // no action required
................................................................................
  // Must be careful, here, when checking the exponent as the
  // adjusted exponent could overflow 31 bits [because it may already
  // be up to twice the expected].

  // First test for subnormal.  This must be done before any final
  // round as the result could be rounded to Nmin or 0.
  if (dn->exponent<=tinyexp) {          // prefilter
    Int comp;
    decNumber nmin;
    // A very nasty case here is dn == Nmin and residue<0
    if (dn->exponent<tinyexp) {
      // Go handle subnormals; this will apply round if needed.
      decSetSubnormal(dn, set, residue, status);
      return;
      }
    // Equals case: only subnormal if dn=Nmin and negative residue
    decNumberZero(&nmin);
    nmin.lsu[0]=1;
    nmin.exponent=set->emin;
    comp=decCompare(dn, &nmin, 1);                // (signless compare)
    if (comp==BADINT) {                           // oops
      *status|=DEC_Insufficient_storage;          // abandon...
      return;
      }
    if (*residue<0 && comp==0) {                  // neg residue and dn==Nmin
      decApplyRound(dn, set, *residue, status);   // might force down
      decSetSubnormal(dn, set, residue, status);
      return;
      }
    }

  // now apply any pending round (this could raise overflow).
  if (*residue!=0) decApplyRound(dn, set, *residue, status);

  // Check for overflow [redundant in the 'rare' case] or clamp
  if (dn->exponent<=set->emax-set->digits+1) return;   // neither needed


  // here when might have an overflow or clamp to do
  if (dn->exponent>set->emax-dn->digits+1) {           // too big
    decSetOverflow(dn, set, status);
    return;
    }
  // here when the result is normal but in clamp range
................................................................................
  return;
  } // decFinalize

/* ------------------------------------------------------------------ */
/* decSetOverflow -- set number to proper overflow value              */
/*                                                                    */
/*   dn is the number (used for sign [only] and result)               */
/*   set is the context [used for the rounding mode, etc.]            */
/*   status contains the current status to be updated                 */
/*                                                                    */
/* This sets the sign of a number and sets its value to either        */
/* Infinity or the maximum finite value, depending on the sign of     */
/* dn and the rounding mode, following IEEE 854 rules.                */
/* ------------------------------------------------------------------ */
static void decSetOverflow(decNumber *dn, decContext *set, uInt *status) {
  Flag needmax=0;                  // result is maximum finite value
  uByte sign=dn->bits&DECNEG;      // clean and save sign bit

  if (ISZERO(dn)) {                // zero does not overflow magnitude
    Int emax=set->emax;                      // limit value
................................................................................
      break;} // r-c
    case DEC_ROUND_FLOOR: {
      if (!sign) needmax=1;        // Infinity if negative
      break;} // r-f
    default: break;                // Infinity in all other cases
    }
  if (needmax) {
    decSetMaxValue(dn, set);
    dn->bits=sign;                 // set sign
    }
   else dn->bits=sign|DECINF;      // Value is +/-Infinity
  *status|=DEC_Overflow | DEC_Inexact | DEC_Rounded;
  } // decSetOverflow

/* ------------------------------------------------------------------ */
/* decSetMaxValue -- set number to +Nmax (maximum normal value)       */
/*                                                                    */
/*   dn is the number to set                                          */
/*   set is the context [used for digits and emax]                    */
/*                                                                    */
/* This sets the number to the maximum positive value.                */
/* ------------------------------------------------------------------ */
static void decSetMaxValue(decNumber *dn, decContext *set) {
  Unit *up;                        // work
  Int count=set->digits;           // nines to add
  dn->digits=count;
  // fill in all nines to set maximum value
  for (up=dn->lsu; ; up++) {
    if (count>DECDPUN) *up=DECDPUNMAX;  // unit full o'nines
     else {                             // this is the msu
      *up=(Unit)(powers[count]-1);
      break;
      }
    count-=DECDPUN;                // filled those digits
    } // up
  dn->bits=0;                      // + sign
  dn->exponent=set->emax-set->digits+1;



  } // decSetMaxValue

/* ------------------------------------------------------------------ */
/* decSetSubnormal -- process value whose exponent is <Emin           */
/*                                                                    */
/*   dn is the number (used as input as well as output; it may have   */
/*         an allowed subnormal value, which may need to be rounded)  */
/*   set is the context [used for the rounding mode]                  */
................................................................................
  // (Etiny) if needed
  etiny=set->emin-(set->digits-1);      // smallest allowed exponent

  if ISZERO(dn) {                       // value is zero
    // residue can never be non-zero here
    #if DECCHECK
      if (*residue!=0) {
        printf("++ Subnormal 0 residue %ld\n", *residue);
        *status|=DEC_Invalid_operation;
        }
    #endif
    if (dn->exponent<etiny) {           // clamp required
      dn->exponent=etiny;
      *status|=DEC_Clamped;
      }
................................................................................
/* decGetInt -- get integer from a number                             */
/*                                                                    */
/*   dn is the number [which will not be altered]                     */
/*                                                                    */
/*   returns one of:                                                  */
/*     BADINT if there is a non-zero fraction                         */
/*     the converted integer                                          */
/*     BIGEVEN if the integer is even and magnitude > 2*10**9         */
/*     BIGODD  if the integer is odd  and magnitude > 2*10**9         */
/*                                                                    */
/* This checks and gets a whole number from the input decNumber.      */
/* The sign can be determined from dn by the caller when BIGEVEN or   */
/* BIGODD is returned.                                                */
/* ------------------------------------------------------------------ */
static Int decGetInt(const decNumber *dn) {
  Int  theInt;                          // result accumulator
................................................................................
    }

  if (neg) theInt=-theInt;              // apply sign
  return theInt;
  } // decGetInt

/* ------------------------------------------------------------------ */
/* decDecap -- decapitate the coefficient of a number                 */
/*                                                                    */
/*   dn   is the number to be decapitated                             */
/*   drop is the number of digits to be removed from the left of dn;  */
/*     this must be <= dn->digits (if equal, the coefficient is       */
/*     set to 0)                                                      */
/*                                                                    */

/* Returns dn; dn->digits will be <= the initial digits less drop     */
/* (after removing drop digits there may be leading zero digits       */
/* which will also be removed).  Only dn->lsu and dn->digits change.  */
/* ------------------------------------------------------------------ */
static decNumber *decDecap(decNumber *dn, Int drop) {
  Unit *msu;                            // -> target cut point


  Int cut;                              // work

  if (drop>=dn->digits) {               // losing the whole thing
    #if DECCHECK
    if (drop>dn->digits)

      printf("decDecap called with drop>digits [%ld>%ld]\n", drop, dn->digits);
    #endif
    dn->lsu[0]=0;
    dn->digits=1;

    return dn;
    }

  msu=dn->lsu+D2U(dn->digits-drop)-1;   // -> likely msu
  cut=MSUDIGITS(dn->digits-drop);       // digits to be in use in msu
  if (cut!=DECDPUN) *msu%=powers[cut];  // clear left digits
  // that may have left leading zero digits, so do a proper count...



  dn->digits=decGetDigits(dn->lsu, msu-dn->lsu+1);
  return dn;
  } // decDecap

/* ------------------------------------------------------------------ */
/* decBiStr -- compare string with pairwise options                   */
/*                                                                    */
/*   targ is the string to compare                                    */
/*   str1 is one of the strings to compare against (length may be 0)  */
/*   str2 is the other; it must be the same length as str1            */
................................................................................
    } // forever
  return 1;
  } // decBiStr

/* ------------------------------------------------------------------ */
/* decNaNs -- handle NaN operand or operands                          */
/*                                                                    */
/*   res     is the result number                                     */
/*   lhs     is the first operand                                     */
/*   rhs     is the second operand, or NULL if none                   */
/*   context is used to limit payload length                          */
/*   status  contains the current status                              */
/*   returns res in case convenient                                   */
/*                                                                    */
/* Called when one or both operands is a NaN, and propagates the      */
/* appropriate result to res.  When an sNaN is found, it is changed   */
/* to a qNaN and Invalid operation is set.                            */
/* ------------------------------------------------------------------ */
static decNumber * decNaNs(decNumber *res, const decNumber *lhs,
                           const decNumber *rhs, decContext *set,
                           uInt *status) {
  // This decision tree ends up with LHS being the source pointer,
  // and status updated if need be
  if (lhs->bits & DECSNAN)
    *status|=DEC_Invalid_operation | DEC_sNaN;
   else if (rhs==NULL);
   else if (rhs->bits & DECSNAN) {
    lhs=rhs;
    *status|=DEC_Invalid_operation | DEC_sNaN;
    }
   else if (lhs->bits & DECNAN);
   else lhs=rhs;

  // propagate the payload
  if (lhs->digits<=set->digits) decNumberCopy(res, lhs); // easy
   else { // too long
    const Unit *ul;
    Unit *ur, *uresp1;
    // copy safe number of units, then decapitate
    res->bits=lhs->bits;                // need sign etc.
    uresp1=res->lsu+D2U(set->digits);
    for (ur=res->lsu, ul=lhs->lsu; ur<uresp1; ur++, ul++) *ur=*ul;
    res->digits=D2U(set->digits)*DECDPUN;
    // maybe still too long
    if (res->digits>set->digits) decDecap(res, res->digits-set->digits);
    }

  res->bits&=~DECSNAN;        // convert any sNaN to NaN, while
  res->bits|=DECNAN;          // .. preserving sign
  res->exponent=0;            // clean exponent
                              // [coefficient was copied/decapitated]
  return res;
  } // decNaNs

/* ------------------------------------------------------------------ */
/* decStatus -- apply non-zero status                                 */
/*                                                                    */
/*   dn     is the number to set if error                             */
................................................................................
  } // decStatus

/* ------------------------------------------------------------------ */
/* decGetDigits -- count digits in a Units array                      */
/*                                                                    */
/*   uar is the Unit array holding the number (this is often an       */
/*          accumulator of some sort)                                 */
/*   len is the length of the array in units [>=1]                    */
/*                                                                    */
/*   returns the number of (significant) digits in the array          */
/*                                                                    */
/* All leading zeros are excluded, except the last if the array has   */
/* only zero Units.                                                   */
/* ------------------------------------------------------------------ */
// This may be called twice during some operations.
static Int decGetDigits(Unit *uar, Int len) {
  Unit *up=uar+(len-1);            // -> msu
  Int  digits=(len-1)*DECDPUN+1;   // possible digits excluding msu

  #if DECDPUN>4
  uInt const *pow;                 // work
  #endif
                                   // (at least 1 in final msu)
  #if DECCHECK
  if (len<1) printf("decGetDigits called with len<1 [%ld]\n", len);
  #endif

  for (; up>=uar; up--) {
    if (*up==0) {                  // unit is all 0s
      if (digits==1) break;        // a zero has one digit
      digits-=DECDPUN;             // adjust for 0 unit
      continue;}
    // found the first (most significant) non-zero Unit
................................................................................
    #endif
    #endif
    #endif
    break;
    } // up
  return digits;
  } // decGetDigits


#if DECTRACE | DECCHECK
/* ------------------------------------------------------------------ */
/* decNumberShow -- display a number [debug aid]                      */
/*   dn is the number to show                                         */
/*                                                                    */
/* Shows: sign, exponent, coefficient (msu first), digits             */
................................................................................
      return;}
    // drop through to report other information
    printf(" ");
    }

  // now carefully display the coefficient
  up=dn->lsu+D2U(dn->digits)-1;         // msu
  printf("%ld", (Int)*up);
  for (up=up-1; up>=dn->lsu; up--) {
    u=*up;
    printf(":");
    for (cut=DECDPUN-1; cut>=0; cut--) {
      d=u/powers[cut];
      u-=d*powers[cut];
      printf("%ld", d);
      } // cut
    } // up
  if (dn->exponent!=0) {
    char esign='+';
    if (dn->exponent<0) esign='-';
    printf(" E%c%d", esign, abs(dn->exponent));
    }
  printf(" [%ld]\n", dn->digits);
  } // decNumberShow
#endif

#if DECTRACE || DECCHECK
/* ------------------------------------------------------------------ */
/* decDumpAr -- display a unit array [debug aid]                      */
/*   name is a single-character tag name                              */
................................................................................
  #elif DECDPUN==2
    char *spec="%02d ";
  #else
    char *spec="%d ";
  #endif
  printf("  :%c: ", name);
  for (i=len-1; i>=0; i--) {
    if (i==len-1) printf("%ld ", (Int)ar[i]);
     else printf(spec, ar[i]);
    }
  printf("\n");
  return;}
#endif

#if DECCHECK
................................................................................
/*     handle this so res=NULL case is safe.                          */
/* The caller is expected to abandon immediately if 1 is returned.    */
/* ------------------------------------------------------------------ */
static Flag decCheckOperands(decNumber *res, const decNumber *lhs,
                             const decNumber *rhs, decContext *set) {
  Flag bad=0;
  if (set==NULL) {                 // oops; hopeless
    #if DECTRACE || DECVERB
    printf("Reference to context is NULL.\n");
    #endif
    bad=1;
    return 1;}
   else if (set!=DECUNUSED
     && (set->digits<1 || set->round<0 || set->round>=DEC_ROUND_MAX)) {
    bad=1;
    #if DECTRACE || DECVERB
    printf("Bad context [digits=%ld round=%d].\n", set->digits, set->round);
    #endif
    }
   else {
    if (res==NULL) {
      bad=1;
      #if DECTRACE
      // this one not DECVERB as standard tests include NULL
      printf("Reference to result is NULL.\n");
      #endif
      }
    if (!bad && lhs!=DECUNUSED) bad=(decCheckNumber(lhs, set));
    if (!bad && rhs!=DECUNUSED) bad=(decCheckNumber(rhs, set));
    }
  if (bad) {
    if (set!=DECUNUSED) decContextSetStatus(set, DEC_Invalid_operation);
................................................................................
  const Unit *up;             // work
  uInt maxuint;               // ..
  Int ae, d, digits;          // ..
  Int emin, emax;             // ..

  if (dn==NULL) {             // hopeless
    #if DECTRACE
    // this one not DECVERB as standard tests include NULL
    printf("Reference to decNumber is NULL.\n");
    #endif
    return 1;}

  // check special values
  if (dn->bits & DECSPECIAL) {
    if (dn->exponent!=0) {
      #if DECTRACE || DECVERB
      printf("Exponent %ld (not 0) for a special value [%02x].\n",
        dn->exponent, dn->bits);
      #endif
      return 1;}

    // 2003.09.08: NaNs may now have coefficients, so next tests Inf only
    if (decNumberIsInfinite(dn)) {
      if (dn->digits!=1) {
        #if DECTRACE || DECVERB
        printf("Digits %ld (not 1) for an infinity.\n", dn->digits);
        #endif
        return 1;}
      if (*dn->lsu!=0) {
        #if DECTRACE || DECVERB
        printf("LSU %ld (not 0) for an infinity.\n", (Int)*dn->lsu);
        #endif
        return 1;}
      } // Inf
    // 2002.12.26: negative NaNs can now appear through proposed IEEE
    //             concrete formats (decimal64, etc.).

    return 0;






    }

  // check the coefficient
  if (dn->digits<1 || dn->digits>DECNUMMAXP) {
    #if DECTRACE || DECVERB
    printf("Digits %ld in number.\n", dn->digits);
    #endif
    return 1;}

  d=dn->digits;

  for (up=dn->lsu; d>0; up++) {
    if (d>DECDPUN) maxuint=DECDPUNMAX;
     else {                   // reached the msu
      maxuint=powers[d]-1;
      if (dn->digits>1 && *up<powers[d-1]) {
        #if DECTRACE || DECVERB
        printf("Leading 0 in number.\n");
        decNumberShow(dn);
        #endif
        return 1;}
      }
    if (*up>maxuint) {
      #if DECTRACE || DECVERB
      printf("Bad Unit [%08lx] in %ld-digit number at offset %ld [maxuint %ld].\n",
              (Int)*up, dn->digits, (Int)(up-dn->lsu), maxuint);
      #endif
      return 1;}
    d-=DECDPUN;
    }

  // check the exponent.  Note that input operands can have exponents
  // which are out of the set->emin/set->emax and set->digits range
  // (just as they can have more digits than set->digits).
  ae=dn->exponent+dn->digits-1;    // adjusted exponent
  emax=DECNUMMAXE;
  emin=DECNUMMINE;
  digits=DECNUMMAXP;
  if (ae<emin-(digits-1)) {
    #if DECTRACE || DECVERB
    printf("Adjusted exponent underflow [%ld].\n", ae);
    decNumberShow(dn);
    #endif
    return 1;}
  if (ae>+emax) {
    #if DECTRACE || DECVERB
    printf("Adjusted exponent overflow [%ld].\n", ae);
    decNumberShow(dn);
    #endif
    return 1;}

  return 0;              // it's OK
  } // decCheckNumber


/* ------------------------------------------------------------------ */
/* decCheckInexact -- check a normal finite inexact result has digits */
/*   dn is the number to check                                        */
/*   set is the context (for status and precision)                    */
/*   sets Invalid operation, etc., if some digits are missing         */
/* [this check is not made for DECSUBSET compilation or when          */
/* subnormal is not set]                                              */
/* ------------------------------------------------------------------ */
static void decCheckInexact(const decNumber *dn, decContext *set) {
  #if !DECSUBSET && DECEXTFLAG
    if ((set->status & (DEC_Inexact|DEC_Subnormal))==DEC_Inexact
     && (set->digits!=dn->digits) && !(dn->bits & DECSPECIAL)) {

      #if DECTRACE || DECVERB
      printf("Insufficient digits [%ld] on normal Inexact result.\n", dn->digits);
      decNumberShow(dn);
      #endif
      decContextSetStatus(set, DEC_Invalid_operation);
      }
  #endif
  return;
  } // decCheckInexact
#endif

#if DECALLOC
................................................................................

  alloc=malloc(size);              // -> allocated storage
  if (alloc==NULL) return NULL;    // out of strorage
  b0=(uByte *)alloc;               // as bytes
  decAllocBytes+=n;                // account for storage
  j=(uInt *)alloc;                 // -> first four bytes
  *j=n;                            // save n
  // printf(" alloc ++ dAB: %ld (%d)\n", decAllocBytes, n);
  for (b=b0+4; b<b0+8; b++) *b=DECFENCE;
  for (b=b0+n+8; b<b0+n+12; b++) *b=DECFENCE;
  return b0+8;                     // -> play area
  } // decMalloc

/* ------------------------------------------------------------------ */
/* decFree -- accountable free routine                                */
................................................................................

  if (alloc==NULL) return;         // allowed; it's a nop
  b0=(uByte *)alloc;               // as bytes
  b0-=8;                           // -> true start of storage
  j=(uInt *)b0;                    // -> first four bytes
  n=*j;                            // lift
  for (b=b0+4; b<b0+8; b++) if (*b!=DECFENCE)
    printf("=== Corrupt byte [%02x] at offset %d from %ld ===\n", *b,
           b-b0-8, (Int)b0);
  for (b=b0+n+8; b<b0+n+12; b++) if (*b!=DECFENCE)
    printf("=== Corrupt byte [%02x] at offset +%d from %ld, n=%ld ===\n", *b,
           b-b0-8, (Int)b0, n);
  free(b0);                        // drop the storage
  decAllocBytes-=n;                // account for storage
  // printf(" free -- dAB: %d (%d)\n", decAllocBytes, -n);
  } // decFree
#define malloc(a) decMalloc(a)
#define free(a) decFree(a)
#endif

Changes to decNumber/decNumber.h.

1
2
3
4
5
6
7
8
9
10
11
..
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

73
74
75

76
77
78
79
80
81
82


83
84
85
86

87
88
89
90


91


























92
93
94
95
96



97
98
99




100
101
102
103

104

105

106
107
108


109

110
111

112

113
114
115

116
117
118
119
120
121

122


123
124

125

126
127



128






129
130
131
132




133
134
135

136


137
138
139
140

141

142

143
144
/* ------------------------------------------------------------------ */
/* Decimal Number arithmetic module header                            */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2006.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */

#if !defined(DECNUMBER)
  #define DECNUMBER
  #define DECNAME     "decNumber"                       /* Short name */
  #define DECVERSION  "decNumber 3.37"           /* Version [16 max.] */
  #define DECFULLNAME "Decimal Number Module"         /* Verbose name */
  #define DECAUTHOR   "Mike Cowlishaw"                /* Who to blame */

  #if !defined(DECCONTEXT)
    #include "decContext.h"
  #endif

  // Bit settings for decNumber.bits
  #define DECNEG    0x80        // Sign; 1=negative, 0=positive or zero
  #define DECINF    0x40        // 1=Infinity
  #define DECNAN    0x20        // 1=NaN
  #define DECSNAN   0x10        // 1=sNaN
  // The remaining bits are reserved; they must be 0
  #define DECSPECIAL (DECINF|DECNAN|DECSNAN) // any special value

  // Define the decNumber data structure.  The size and shape of the
  // units array in the structure is determined by the following
  // constant.  This must not be changed without recompiling the
  // decNumber library modules.

  #define DECDPUN 3                // DECimal Digits Per UNit [must be in
                                   // range 1-9; 3 or powers of 2 are best].

  // DECNUMDIGITS is the default number of digits that can be held in
  // the structure.  If undefined, 1 is assumed and it is assumed that
  // the structure will be immediately followed by extra space, as
  // required.  DECNUMDIGITS is always >0.
  #if !defined(DECNUMDIGITS)
    #define DECNUMDIGITS 1
  #endif

  // The size (integer data type) of each unit is determined by the
  // number of digits it will hold.
  #if   DECDPUN<=2
    #define decNumberUnit uint8_t
  #elif DECDPUN<=4
    #define decNumberUnit uint16_t
  #else
    #define decNumberUnit uint32_t
  #endif
  // The number of decNumberUnits needed is ceil(DECNUMDIGITS/DECDPUN)
  #define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN)

  // The data structure...
  typedef struct {
    int32_t digits;                // Count of digits in the coefficient; >0
    int32_t exponent;              // Unadjusted exponent, unbiased, in
                                   // range: -1999999997 through 999999999
    uint8_t bits;                  // Indicator bits (see above)
    decNumberUnit lsu[DECNUMUNITS];// Coefficient, from least significant unit

    } decNumber;

  // Notes:

  // 1. If digits is > DECDPUN then there will one or more
  //    decNumberUnits immediately following the first element of lsu.
  //    These contain the remaining (more significant) digits of the
  //    number, and may be in the lsu array, or may be guaranteed by
  //    some other mechanism (such as being contained in another
  //    structure, or being overlaid on dynamically allocated storage).
  //


  //    Each integer of the coefficient (except potentially the last)
  //    contains DECDPUN digits (e.g., a value in the range 0 through
  //    99999999 if DECDPUN is 8, or 0 through 999 if DECDPUN is 3).
  //

  // 2. A decNumber converted to a string may need up to digits+14
  //    characters.  The worst cases (non-exponential and exponential
  //    formats) are: -0.00000{9...}#
  //             and: -9.{9...}E+999999999#   (where # is '\0')






























  /* ------------------------------------------------------------------ */
  /* decNumber public functions and macros                              */
  /* ------------------------------------------------------------------ */
  // Conversions



  decNumber * decNumberFromString(decNumber *, const char *, decContext *);
  char * decNumberToString(const decNumber *, char *);
  char * decNumberToEngString(const decNumber *, char *);





  // Operators and elementary functions
  decNumber * decNumberAbs(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberAdd(decNumber *, const decNumber *, const decNumber *, decContext *);

  decNumber * decNumberCompare(decNumber *, const decNumber *, const decNumber *, decContext *);

  decNumber * decNumberCompareTotal(decNumber *, const decNumber *, const decNumber *, decContext *);

  decNumber * decNumberDivide(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberDivideInteger(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberExp(decNumber *, const decNumber *, decContext *);


  decNumber * decNumberLn(decNumber *, const decNumber *, decContext *);

  decNumber * decNumberLog10(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberMax(decNumber *, const decNumber *, const decNumber *, decContext *);

  decNumber * decNumberMin(decNumber *, const decNumber *, const decNumber *, decContext *);

  decNumber * decNumberMinus(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberMultiply(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberNormalize(decNumber *, const decNumber *, decContext *);

  decNumber * decNumberPlus(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberPower(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberQuantize(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberRemainder(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberRemainderNear(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberRescale(decNumber *, const decNumber *, const decNumber *, decContext *);

  decNumber * decNumberSameQuantum(decNumber *, const decNumber *, const decNumber *);


  decNumber * decNumberSquareRoot(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberSubtract(decNumber *, const decNumber *, const decNumber *, decContext *);

  decNumber * decNumberToIntegralValue(decNumber *, const decNumber *, decContext *);


  // Utilities



  decNumber  * decNumberCopy(decNumber *, const decNumber *);






  decNumber  * decNumberTrim(decNumber *);
  const char * decNumberVersion(void);
  decNumber  * decNumberZero(decNumber *);





  // Macros for testing decNumbers
  #define decNumberIsZero(dn)     (*(dn)->lsu==0 \
                                   && (dn)->digits==1 \

                                   && (((dn)->bits&DECSPECIAL)==0))


  #define decNumberIsNegative(dn) (((dn)->bits&DECNEG)!=0)
  #define decNumberIsNaN(dn)      (((dn)->bits&(DECNAN|DECSNAN))!=0)
  #define decNumberIsQNaN(dn)     (((dn)->bits&(DECNAN))!=0)
  #define decNumberIsSNaN(dn)     (((dn)->bits&(DECSNAN))!=0)

  #define decNumberIsInfinite(dn) (((dn)->bits&DECINF)!=0)

  #define decNumberIsSpecial(dn)  (((dn)->bits&DECSPECIAL)!=0)


#endif



|







 







|







|
|
|
|
|
|
|

|
|
|
|
>
|
|

|
|
|
|




|
|







|


|

|
|
|
|
|
>


<
>
|
|
|
|
|
|
<
>
>
|
|
|
<
>
|
|
<
<
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
|
|
<
>
>
>

|
|
>
>
>
>

|


>

>

>



>
>

>


>

>



>






>

>
>


>

>

<
>
>
>

>
>
>
>
>
>




>
>
>
>
|
<
<
>
|
>
>
|
<
|
|
>
|
>
|
>


1
2
3
4
5
6
7
8
9
10
11
..
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

77
78
79
80
81
82
83

84
85
86
87
88

89
90
91


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194


195
196
197
198
199

200
201
202
203
204
205
206
207
208
/* ------------------------------------------------------------------ */
/* Decimal Number arithmetic module header                            */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2007.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */

#if !defined(DECNUMBER)
  #define DECNUMBER
  #define DECNAME     "decNumber"                       /* Short name */
  #define DECVERSION  "decNumber 3.41"           /* Version [16 max.] */
  #define DECFULLNAME "Decimal Number Module"         /* Verbose name */
  #define DECAUTHOR   "Mike Cowlishaw"                /* Who to blame */

  #if !defined(DECCONTEXT)
    #include "decContext.h"
  #endif

  /* Bit settings for decNumber.bits                                  */
  #define DECNEG    0x80      /* Sign; 1=negative, 0=positive or zero */
  #define DECINF    0x40      /* 1=Infinity                           */
  #define DECNAN    0x20      /* 1=NaN                                */
  #define DECSNAN   0x10      /* 1=sNaN                               */
  /* The remaining bits are reserved; they must be 0                  */
  #define DECSPECIAL (DECINF|DECNAN|DECSNAN) /* any special value     */

  /* Define the decNumber data structure.  The size and shape of the  */
  /* units array in the structure is determined by the following      */
  /* constant.  This must not be changed without recompiling the      */
  /* decNumber library modules. */

  #define DECDPUN 3           /* DECimal Digits Per UNit [must be >0  */
                              /* and <10; 3 or powers of 2 are best]. */

  /* DECNUMDIGITS is the default number of digits that can be held in */
  /* the structure.  If undefined, 1 is assumed and it is assumed     */
  /* that the structure will be immediately followed by extra space,  */
  /* as required.  DECNUMDIGITS is always >0.                         */
  #if !defined(DECNUMDIGITS)
    #define DECNUMDIGITS 1
  #endif

  /* The size (integer data type) of each unit is determined by the   */
  /* number of digits it will hold.                                   */
  #if   DECDPUN<=2
    #define decNumberUnit uint8_t
  #elif DECDPUN<=4
    #define decNumberUnit uint16_t
  #else
    #define decNumberUnit uint32_t
  #endif
  /* The number of units needed is ceil(DECNUMDIGITS/DECDPUN)         */
  #define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN)

  /* The data structure... */
  typedef struct {
    int32_t digits;      /* Count of digits in the coefficient; >0    */
    int32_t exponent;    /* Unadjusted exponent, unbiased, in         */
                         /* range: -1999999997 through 999999999      */
    uint8_t bits;        /* Indicator bits (see above)                */
                         /* Coefficient, from least significant unit  */
    decNumberUnit lsu[DECNUMUNITS];
    } decNumber;


  /* Notes:                                                           */
  /* 1. If digits is > DECDPUN then there will one or more            */
  /*    decNumberUnits immediately following the first element of lsu.*/
  /*    These contain the remaining (more significant) digits of the  */
  /*    number, and may be in the lsu array, or may be guaranteed by  */
  /*    some other mechanism (such as being contained in another      */
  /*    structure, or being overlaid on dynamically allocated         */

  /*    storage).                                                     */
  /*                                                                  */
  /*    Each integer of the coefficient (except potentially the last) */
  /*    contains DECDPUN digits (e.g., a value in the range 0 through */
  /*    99999999 if DECDPUN is 8, or 0 through 999 if DECDPUN is 3).  */

  /*                                                                  */
  /* 2. A decNumber converted to a string may need up to digits+14    */
  /*    characters.  The worst cases (non-exponential and exponential */


  /*    formats) are -0.00000{9...}# and -9.{9...}E+999999999#        */
  /*    (where # is '\0')                                             */

  /* Classifications for decNumbers, aligned with 754r (note that     */
  /* 'normal' and 'subnormal' are meaningful only with a decContext)  */
  enum decClass {
    DEC_CLASS_SNAN,
    DEC_CLASS_QNAN,
    DEC_CLASS_NEG_INF,
    DEC_CLASS_NEG_NORMAL,
    DEC_CLASS_NEG_SUBNORMAL,
    DEC_CLASS_NEG_ZERO,
    DEC_CLASS_POS_ZERO,
    DEC_CLASS_POS_SUBNORMAL,
    DEC_CLASS_POS_NORMAL,
    DEC_CLASS_POS_INF,
    };
  /* Strings for the decClasses */
  #define DEC_ClassString_SN  "sNaN"
  #define DEC_ClassString_QN  "NaN"
  #define DEC_ClassString_NI  "-Infinity"
  #define DEC_ClassString_NN  "-Normal"
  #define DEC_ClassString_NS  "-Subnormal"
  #define DEC_ClassString_NZ  "-Zero"
  #define DEC_ClassString_PZ  "+Zero"
  #define DEC_ClassString_PS  "+Subnormal"
  #define DEC_ClassString_PN  "+Normal"
  #define DEC_ClassString_PI  "+Infinity"
  #define DEC_ClassString_UN  "Invalid"

  /* ---------------------------------------------------------------- */
  /* decNumber public functions and macros                            */
  /* ---------------------------------------------------------------- */

  /* Conversions                                                      */
  decNumber * decNumberFromInt32(decNumber *, int32_t);
  decNumber * decNumberFromUInt32(decNumber *, uint32_t);
  decNumber * decNumberFromString(decNumber *, const char *, decContext *);
  char      * decNumberToString(const decNumber *, char *);
  char      * decNumberToEngString(const decNumber *, char *);
  uint32_t    decNumberToUInt32(const decNumber *, decContext *);
  int32_t     decNumberToInt32(const decNumber *, decContext *);
  uint8_t   * decNumberGetBCD(const decNumber *, uint8_t *);
  decNumber * decNumberSetBCD(decNumber *, const uint8_t *, uint32_t);

  /* Operators and elementary functions                               */
  decNumber * decNumberAbs(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberAdd(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberAnd(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberCompare(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberCompareSignal(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberCompareTotal(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberCompareTotalMag(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberDivide(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberDivideInteger(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberExp(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberFMA(decNumber *, const decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberInvert(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberLn(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberLogB(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberLog10(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberMax(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberMaxMag(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberMin(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberMinMag(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberMinus(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberMultiply(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberNormalize(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberOr(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberPlus(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberPower(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberQuantize(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberRemainder(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberRemainderNear(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberRescale(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberRotate(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberSameQuantum(decNumber *, const decNumber *, const decNumber *);
  decNumber * decNumberScaleB(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberShift(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberSquareRoot(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberSubtract(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber * decNumberToIntegralExact(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberToIntegralValue(decNumber *, const decNumber *, decContext *);
  decNumber * decNumberXor(decNumber *, const decNumber *, const decNumber *, decContext *);


  /* Utilities                                                        */
  enum decClass decNumberClass(const decNumber *, decContext *);
  const char * decNumberClassToString(enum decClass);
  decNumber  * decNumberCopy(decNumber *, const decNumber *);
  decNumber  * decNumberCopyAbs(decNumber *, const decNumber *);
  decNumber  * decNumberCopyNegate(decNumber *, const decNumber *);
  decNumber  * decNumberCopySign(decNumber *, const decNumber *, const decNumber *);
  decNumber  * decNumberNextMinus(decNumber *, const decNumber *, decContext *);
  decNumber  * decNumberNextPlus(decNumber *, const decNumber *, decContext *);
  decNumber  * decNumberNextToward(decNumber *, const decNumber *, const decNumber *, decContext *);
  decNumber  * decNumberTrim(decNumber *);
  const char * decNumberVersion(void);
  decNumber  * decNumberZero(decNumber *);

  /* Functions for testing decNumbers (normality depends on context)  */
  int32_t decNumberIsNormal(const decNumber *, decContext *);
  int32_t decNumberIsSubnormal(const decNumber *, decContext *);

  /* Macros for testing decNumber *dn                                 */


  #define decNumberIsCanonical(dn) (1)  /* All decNumbers are saintly */
  #define decNumberIsFinite(dn)    (((dn)->bits&DECSPECIAL)==0)
  #define decNumberIsInfinite(dn)  (((dn)->bits&DECINF)!=0)
  #define decNumberIsNaN(dn)       (((dn)->bits&(DECNAN|DECSNAN))!=0)
  #define decNumberIsNegative(dn)  (((dn)->bits&DECNEG)!=0)

  #define decNumberIsQNaN(dn)      (((dn)->bits&(DECNAN))!=0)
  #define decNumberIsSNaN(dn)      (((dn)->bits&(DECSNAN))!=0)
  #define decNumberIsSpecial(dn)   (((dn)->bits&DECSPECIAL)!=0)
  #define decNumberIsZero(dn)      (*(dn)->lsu==0 \
                                    && (dn)->digits==1 \
                                    && (((dn)->bits&DECSPECIAL)==0))
  #define decNumberRadix(dn)       (10)

#endif

Changes to decNumber/decNumberLocal.h.

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

30
31
32
33
34
35
36

37
38
39
40
41
42
43






44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* Please send comments, suggestions, and corrections to the author:  */
/*   mfc@uk.ibm.com                                                   */
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */
/* This header file is included by all modules in the decNumber       */
/* library, and contains local type definitions, tuning parameters,   */
/* etc.  It must only be included once, and should not need to be     */
/* used by application programs.  decNumber.h must be included first. */
/* ------------------------------------------------------------------ */

#if !defined(DECNUMBERLOC)
  #define DECNUMBERLOC
  #define DECNLAUTHOR   "Mike Cowlishaw"              /* Who to blame */

  /* Tuning parameter */

  #define DECBUFFER 36        // Maximum size basis for local buffers.
                              // Should be a common maximum precision
                              // rounded up to a multiple of 4; must
                              // be zero or positive.

  /* Conditional code flags -- set these to 1 for best performance */
  #define DECENDIAN 1         // 1=concrete formats are endian

  #define DECUSE64  1         // 1 to allow use of 64-bit integers

  /* Conditional check flags -- set these to 0 for best performance */
  #define DECCHECK  0         // 1 to enable robust checking
  #define DECALLOC  0         // 1 to enable memory allocation accounting
  #define DECTRACE  0         // 1 to trace critical intermediates, etc.







  /* Local names for common types -- for safety, decNumber modules do
     not use int or long directly */
  #define Flag   uint8_t
  #define Byte   int8_t
  #define uByte  uint8_t
  #define Short  int16_t
  #define uShort uint16_t
  #define Int    int32_t
  #define uInt   uint32_t
  #define Unit   decNumberUnit
  #if DECUSE64
  #define Long   int64_t
  #define uLong  uint64_t
  #endif

  /* Development-use defines */

  #if DECALLOC
    // if these interfere with your C includes, just comment them out
    #define  int ?            // enable to ensure that plain C 'int' or
    #define  long ??          // .. 'long' types are not used
  #endif

  /* Limits and constants */
  #define DECNUMMAXP 999999999  // maximum precision code can handle
  #define DECNUMMAXE 999999999  // maximum adjusted exponent ditto
  #define DECNUMMINE -999999999 // minimum adjusted exponent ditto
  #if (DECNUMMAXP != DEC_MAX_DIGITS)
    #error Maximum digits mismatch
  #endif
  #if (DECNUMMAXE != DEC_MAX_EMAX)
    #error Maximum exponent mismatch
  #endif
  #if (DECNUMMINE != DEC_MIN_EMIN)
    #error Minimum exponent mismatch
  #endif

  /* Set DECDPUNMAX -- the maximum integer that fits in DECDPUN    */
  /* digits, and D2UTABLE -- the initializer for the D2U table     */
  #if   DECDPUN==1
    #define DECDPUNMAX 9
    #define D2UTABLE {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,  \
                      18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, \
                      33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, \
                      48,49}
  #elif DECDPUN==2
................................................................................
                      3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,  \
                      5,5,6,6,6,6}
  #elif defined(DECDPUN)
    #error DECDPUN must be in the range 1-9
  #endif

  /* ----- Shared data (in decNumber.c) ----- */
  // Public powers of of ten array (powers[n]==10**n, 0<=n<=10)
  extern const uInt powers[];
  // Public lookup table used by the D2U macro (see below)
  #define DECMAXD2U 49
  extern const uByte d2utable[DECMAXD2U+1];

  /* ----- Macros ----- */
  // ISZERO -- return true if decNumber dn is a zero
  // [performance-critical in some situations]
  #define ISZERO(dn) decNumberIsZero(dn)     // now just a local name

  // X10 and X100 -- multiply integer i by 10 or 100
  // [shifts are usually faster than multiply; could be conditional]
  #define X10(i)  (((i)<<1)+((i)<<3))
  #define X100(i) (((i)<<2)+((i)<<5)+((i)<<6))

  // D2U -- return the number of Units needed to hold d digits
  // (runtime version, with table lookaside for small d)
  #if DECDPUN==8
    #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+7)>>3))
  #elif DECDPUN==4
    #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+3)>>2))
  #else
    #define D2U(d) ((d)<=DECMAXD2U?d2utable[d]:((d)+DECDPUN-1)/DECDPUN)
  #endif
  // SD2U -- static D2U macro (for compile-time calculation)
  #define SD2U(d) (((d)+DECDPUN-1)/DECDPUN)

  // MSUDIGITS -- returns digits in msu, calculated using D2U

  #define MSUDIGITS(d) ((d)-(D2U(d)-1)*DECDPUN)

  // D2N -- return the number of decNumber structs that would be
  // needed to contain that number of digits (and the initial
  // decNumber struct) safely.  Note that one Unit is included in the
  // initial structure.  Used for allocating space that is aligned on
  // a decNumber struct boundary.
  #define D2N(d) \
    ((((SD2U(d)-1)*sizeof(Unit))+sizeof(decNumber)*2-1)/sizeof(decNumber))

  // TODIGIT -- macro to remove the leading digit from the unsigned
  // integer u at column cut (counting from the right, LSD=0) and
  // place it as an ASCII character into the character pointed to by
  // c.  Note that cut must be <= 9, and the maximum value for u is
  // 2,000,000,000 (as is needed for negative exponents of
  // subnormals).  The unsigned integer pow is used as a temporary
  // variable.
  #define TODIGIT(u, cut, c, pow) {       \
    *(c)='0';                             \
    pow=powers[cut]*2;                    \
    if ((u)>pow) {                        \
      pow*=4;                             \
      if ((u)>=pow) {(u)-=pow; *(c)+=8;}  \
      pow/=2;                             \
................................................................................
      pow/=2;                             \
      }                                   \
    if ((u)>=pow) {(u)-=pow; *(c)+=2;}    \
    pow/=2;                               \
    if ((u)>=pow) {(u)-=pow; *(c)+=1;}    \
    }

  // MAX and MIN -- general max & min (not in ANSI)
  #define MAX(x,y) ((x)<(y)?(y):(x))
  #define MIN(x,y) ((x)>(y)?(y):(x))

#else
  #error decNumberLocal included more than once
#endif







|
|






<
>
|
|
<
<

<
<
>
|

|
|
|
|

>
>
>
>
>
>
|
|













|
>
|
|
|
|



|
|
|










|
|







 







|

|




|
|
|

|
|



|
|







|


|
>


|
|
|
|
|



|
|
|
|
|
|
|







 







|






14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

29
30
31


32


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Please send comments, suggestions, and corrections to the author:  */
/*   mfc@uk.ibm.com                                                   */
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */
/* This header file is included by all modules in the decNumber       */
/* library, and contains local type definitions, tuning parameters,   */
/* etc.  It should not need to be used by application programs.       */
/* decNumber.h must be included first.                                */
/* ------------------------------------------------------------------ */

#if !defined(DECNUMBERLOC)
  #define DECNUMBERLOC
  #define DECNLAUTHOR   "Mike Cowlishaw"              /* Who to blame */



  /* Conditional code flag -- set this to match hardware platform     */
  #define DECLITEND 1         /* 1=little-endian, 0=big-endian        */





  /* Conditional code flag -- set this to 1 for best performance      */
  #define DECUSE64  1         /* 1 to allow use of 64-bit integers    */

  /* Conditional check flags -- set these to 0 for best performance   */
  #define DECCHECK  0         /* 1 to enable robust checking          */
  #define DECALLOC  0         /* 1 to enable memory accounting        */
  #define DECTRACE  0         /* 1 to trace certain internals, etc.   */

  /* Tuning parameter */
  #define DECBUFFER 36        /* Size basis for local buffers.  This  */
                              /* should be a common maximum precision */
                              /* rounded up to a multiple of 4; must  */
                              /* be zero or positive.                 */

  /* Local names for common types -- for safety, decNumber modules do */
  /* not use int or long directly.                                    */
  #define Flag   uint8_t
  #define Byte   int8_t
  #define uByte  uint8_t
  #define Short  int16_t
  #define uShort uint16_t
  #define Int    int32_t
  #define uInt   uint32_t
  #define Unit   decNumberUnit
  #if DECUSE64
  #define Long   int64_t
  #define uLong  uint64_t
  #endif

  /* Development-use definitions */
  #define DECNOINT  0         /* 1 to check no internal use of 'int'  */
  #if DECNOINT
    /* if these interfere with your C includes, do not set DECNOINT   */
    #define  int ?            /* enable to ensure that plain C 'int'  */
    #define  long ??          /* .. or 'long' types are not used      */
  #endif

  /* Limits and constants */
  #define DECNUMMAXP 999999999  /* maximum precision code can handle  */
  #define DECNUMMAXE 999999999  /* maximum adjusted exponent ditto    */
  #define DECNUMMINE -999999999 /* minimum adjusted exponent ditto    */
  #if (DECNUMMAXP != DEC_MAX_DIGITS)
    #error Maximum digits mismatch
  #endif
  #if (DECNUMMAXE != DEC_MAX_EMAX)
    #error Maximum exponent mismatch
  #endif
  #if (DECNUMMINE != DEC_MIN_EMIN)
    #error Minimum exponent mismatch
  #endif

  /* Set DECDPUNMAX -- the maximum integer that fits in DECDPUN       */
  /* digits, and D2UTABLE -- the initializer for the D2U table        */
  #if   DECDPUN==1
    #define DECDPUNMAX 9
    #define D2UTABLE {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,  \
                      18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, \
                      33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, \
                      48,49}
  #elif DECDPUN==2
................................................................................
                      3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,  \
                      5,5,6,6,6,6}
  #elif defined(DECDPUN)
    #error DECDPUN must be in the range 1-9
  #endif

  /* ----- Shared data (in decNumber.c) ----- */
  /* Public powers of of ten array (powers[n]==10**n, 0<=n<=10)       */
  extern const uInt powers[];
  /* Public lookup table used by the D2U macro (see below)            */
  #define DECMAXD2U 49
  extern const uByte d2utable[DECMAXD2U+1];

  /* ----- Macros ----- */
  /* ISZERO -- return true if decNumber dn is a zero                  */
  /* [performance-critical in some situations]                        */
  #define ISZERO(dn) decNumberIsZero(dn)     /* now just a local name */

  /* X10 and X100 -- multiply integer i by 10 or 100                  */
  /* [shifts are usually faster than multiply; could be conditional]  */
  #define X10(i)  (((i)<<1)+((i)<<3))
  #define X100(i) (((i)<<2)+((i)<<5)+((i)<<6))

  /* D2U -- return the number of Units needed to hold d digits        */
  /* (runtime version, with table lookaside for small d)              */
  #if DECDPUN==8
    #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+7)>>3))
  #elif DECDPUN==4
    #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+3)>>2))
  #else
    #define D2U(d) ((d)<=DECMAXD2U?d2utable[d]:((d)+DECDPUN-1)/DECDPUN)
  #endif
  /* SD2U -- static D2U macro (for compile-time calculation)          */
  #define SD2U(d) (((d)+DECDPUN-1)/DECDPUN)

  /* MSUDIGITS -- returns digits in msu, from digits, calculated      */
  /* using D2U                                                        */
  #define MSUDIGITS(d) ((d)-(D2U(d)-1)*DECDPUN)

  /* D2N -- return the number of decNumber structs that would be      */
  /* needed to contain that number of digits (and the initial         */
  /* decNumber struct) safely.  Note that one Unit is included in the */
  /* initial structure.  Used for allocating space that is aligned on */
  /* a decNumber struct boundary. */
  #define D2N(d) \
    ((((SD2U(d)-1)*sizeof(Unit))+sizeof(decNumber)*2-1)/sizeof(decNumber))

  /* TODIGIT -- macro to remove the leading digit from the unsigned   */
  /* integer u at column cut (counting from the right, LSD=0) and     */
  /* place it as an ASCII character into the character pointed to by  */
  /* c.  Note that cut must be <= 9, and the maximum value for u is   */
  /* 2,000,000,000 (as is needed for negative exponents of            */
  /* subnormals).  The unsigned integer pow is used as a temporary    */
  /* variable. */
  #define TODIGIT(u, cut, c, pow) {       \
    *(c)='0';                             \
    pow=powers[cut]*2;                    \
    if ((u)>pow) {                        \
      pow*=4;                             \
      if ((u)>=pow) {(u)-=pow; *(c)+=8;}  \
      pow/=2;                             \
................................................................................
      pow/=2;                             \
      }                                   \
    if ((u)>=pow) {(u)-=pow; *(c)+=2;}    \
    pow/=2;                               \
    if ((u)>=pow) {(u)-=pow; *(c)+=1;}    \
    }

  /* MAX and MIN -- general max & min (not in ANSI)                   */
  #define MAX(x,y) ((x)<(y)?(y):(x))
  #define MIN(x,y) ((x)>(y)?(y):(x))

#else
  #error decNumberLocal included more than once
#endif

Changes to decNumber/decPacked.h.

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#if !defined(DECPACKED)
  #define DECPACKED
  #define DECPNAME     "decPacked"                      /* Short name */
  #define DECPFULLNAME "Packed Decimal conversions"   /* Verbose name */
  #define DECPAUTHOR   "Mike Cowlishaw"               /* Who to blame */

  #define DECPACKED_DefP 32             // default precision

  #ifndef  DECNUMDIGITS
    #define DECNUMDIGITS DECPACKED_DefP // size if not already defined
  #endif
  #include "decNumber.h"                // context and number library

  /* Sign nibble constants */
  #define DECPPLUSALT  0x0A   // alternate plus nibble
  #define DECPMINUSALT 0x0B   // alternate minus nibble
  #define DECPPLUS     0x0C   // preferred plus nibble
  #define DECPMINUS    0x0D   // preferred minus nibble
  #define DECPPLUSALT2 0x0E   // alternate plus nibble
  #define DECPUNSIGNED 0x0F   // alternate plus nibble (unsigned)

  /* ---------------------------------------------------------------- */
  /* decPacked public routines                                        */
  /* ---------------------------------------------------------------- */
  // Conversions
  uint8_t * decPackedFromNumber(uint8_t *, int32_t, int32_t *,
                                const decNumber *);
  decNumber * decPackedToNumber(const uint8_t *, int32_t, const int32_t *,
                                decNumber *);

#endif







|


|

|

|
|
|
|
|
|
|




|






19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#if !defined(DECPACKED)
  #define DECPACKED
  #define DECPNAME     "decPacked"                      /* Short name */
  #define DECPFULLNAME "Packed Decimal conversions"   /* Verbose name */
  #define DECPAUTHOR   "Mike Cowlishaw"               /* Who to blame */

  #define DECPACKED_DefP 32             /* default precision          */

  #ifndef  DECNUMDIGITS
    #define DECNUMDIGITS DECPACKED_DefP /* size if not already defined*/
  #endif
  #include "decNumber.h"                /* context and number library */

  /* Sign nibble constants                                            */
  #define DECPPLUSALT  0x0A   /* alternate plus  nibble               */
  #define DECPMINUSALT 0x0B   /* alternate minus nibble               */
  #define DECPPLUS     0x0C   /* preferred plus  nibble               */
  #define DECPMINUS    0x0D   /* preferred minus nibble               */
  #define DECPPLUSALT2 0x0E   /* alternate plus  nibble               */
  #define DECPUNSIGNED 0x0F   /* alternate plus  nibble (unsigned)    */

  /* ---------------------------------------------------------------- */
  /* decPacked public routines                                        */
  /* ---------------------------------------------------------------- */
  /* Conversions                                                      */
  uint8_t * decPackedFromNumber(uint8_t *, int32_t, int32_t *,
                                const decNumber *);
  decNumber * decPackedToNumber(const uint8_t *, int32_t, const int32_t *,
                                decNumber *);

#endif

Changes to decNumber/decimal128.c.

1
2
3
4
5
6
7
8
9
10
11
..
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
...
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
...
523
524
525
526
527
528
529
































530


























531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* ------------------------------------------------------------------ */
/* Decimal 128-bit format module                                      */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2006.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
extern void decDigitsToDPD(const decNumber *, uInt *, Int);

#if DECTRACE || DECCHECK
void decimal128Show(const decimal128 *);          // for debug
extern void decNumberShow(const decNumber *);     // ..
#endif

/* compile-time endian tester [assumes sizeof(int)>1] */
static  const  Int mfcone=1;                 // constant 1
static  const  Flag *mfctop=(Flag *)&mfcone; // -> top byte
#define LITEND mfctop[0]           // named flag; 1=little-endian

/* Useful macro */
// Clear a structure (e.g., a decNumber)
#define DEC_clear(d) memset(d, 0, sizeof(*d))

/* ------------------------------------------------------------------ */
/* decimal128FromNumber -- convert decNumber to decimal128            */
/*                                                                    */
................................................................................
      }
    targhi|=comb<<26;              // add combination field ..
    targhi|=(exp&0xfff)<<14;       // .. and exponent continuation
    } // finite

  if (dn->bits&DECNEG) targhi|=0x80000000; // add sign bit

  // now write to storage; this may be endian, or not
  #if DECENDIAN
  // DECENDIAN -- direct store, in the right order
  pu=(uInt *)d128->bytes;          // overlay
  if (LITEND) {
    pu[0]=targlo;                  // directly store the low int
    pu[1]=targml;                  // then the mid-low
    pu[2]=targmh;                  // then the mid-high
    pu[3]=targhi;                  // then the high int
    }
   else {
    pu[0]=targhi;                  // directly store the high int
    pu[1]=targmh;                  // then the mid-high
    pu[2]=targml;                  // then the mid-low
    pu[3]=targlo;                  // then the low int
    }
  #else
  // not DECENDIAN -- use network byte order
  if (LITEND) {                    // little-endian needs reversal
    uByte *pb;                     // work
    Int off;                       // ..
    for (pb=&d128->bytes[15]; pb>=d128->bytes; pb--) {
      off=3-((pb-d128->bytes)>>2); // 0, then 1, 2, 3
      *pb=(uByte)(targar[off]&0xff);
      targar[off]>>=8;
      } // i
    }
   else { // big-endian; it's the right way round already
    pu=(uInt *)d128->bytes;        // overlay
    pu[0]=targhi;                  // directly store the high int
    pu[1]=targmh;                  // then the mid-high
    pu[2]=targml;                  // then the mid-low
    pu[3]=targlo;                  // then the low int
    }
  #endif

  if (status!=0) decContextSetStatus(set, status); // pass on status
  // decimal128Show(d128);
  return d128;
  } // decimal128FromNumber

/* ------------------------------------------------------------------ */
................................................................................
  Int  need;                       // ..
  uInt sourar[4];                  // source 128-bit
  #define sourhi sourar[3]         // name the word with the sign
  #define sourmh sourar[2]         // and the mid-high word
  #define sourml sourar[1]         // and the mod-low word
  #define sourlo sourar[0]         // and the lowest word

  // load source from storage; this may be endian, or not
  #if DECENDIAN
  // DECENDIAN -- direct load, in the right order
  pu=(uInt *)d128->bytes;          // overlay
  if (LITEND) {
    sourlo=pu[0];                  // directly load the low int
    sourml=pu[1];                  // then the mid-low
    sourmh=pu[2];                  // then the mid-high
    sourhi=pu[3];                  // then the high int
    }
   else {
    sourhi=pu[0];                  // directly load the high int
    sourmh=pu[1];                  // then the mid-high
    sourml=pu[2];                  // then the mid-low
    sourlo=pu[3];                  // then the low int
    }
  #else
  // not DECENDIAN -- use network byte order
  if (LITEND) {                    // little-endian needs reversal
    const uByte *pb;               // work
    Int off;                       // ..
    for (pb=d128->bytes; pb<=&d128->bytes[15]; pb++) {
      off=3-((pb-d128->bytes)>>2); // 3, then 2, 1, 0
      sourar[off]<<=8;
      sourar[off]|=*pb;
      } // i
    }
   else { // big-endian; it's the right way round already
    pu=(uInt *)d128->bytes;        // overlay
    sourhi=pu[0];                  // directly load the high int
    sourmh=pu[1];                  // then the mid-high
    sourml=pu[2];                  // then the mid-low
    sourlo=pu[3];                  // then the low int
    }
  #endif

  comb=(sourhi>>26)&0x1f;          // combination field

  decNumberZero(dn);               // clean number
  if (sourhi&0x80000000) dn->bits=DECNEG; // set sign if negative

  msd=COMBMSD[comb];               // decode the combination field
................................................................................

  uInt sourar[4];                  // source 128-bit
  #define sourhi sourar[3]         // name the word with the sign
  #define sourmh sourar[2]         // and the mid-high word
  #define sourml sourar[1]         // and the mod-low word
  #define sourlo sourar[0]         // and the lowest word

  // load source from storage; this may be endian, or not
  #if DECENDIAN
  // DECENDIAN -- direct load, in the right order
  pu=(uInt *)d128->bytes;          // overlay
  if (LITEND) {
    sourlo=pu[0];                  // directly load the low int
    sourml=pu[1];                  // then the mid-low
    sourmh=pu[2];                  // then the mid-high
    sourhi=pu[3];                  // then the high int
    }
   else {
    sourhi=pu[0];                  // directly load the high int
    sourmh=pu[1];                  // then the mid-high
    sourml=pu[2];                  // then the mid-low
    sourlo=pu[3];                  // then the low int
    }
  #else
  // not DECENDIAN -- use network byte order
  if (LITEND) {                    // little-endian needs reversal
    const uByte *pb;               // work
    Int off;                       // ..
    for (pb=d128->bytes; pb<=&d128->bytes[15]; pb++) {
      off=3-((pb-d128->bytes)>>2); // 3, then 2, 1, 0
      sourar[off]<<=8;
      sourar[off]|=*pb;
      } // i
    }
   else { // big-endian; it's the right way round already
    pu=(uInt *)d128->bytes;        // overlay
    sourhi=pu[0];                  // directly load the high int
    sourmh=pu[1];                  // then the mid-high
    sourml=pu[2];                  // then the mid-low
    sourlo=pu[3];                  // then the low int
    }
  #endif

  c=string;                        // where result will go
  if (((Int)sourhi)<0) *c++='-';   // handle sign

  comb=(sourhi>>26)&0x1f;          // combination field
  msd=COMBMSD[comb];               // decode the combination field
  exp=COMBEXP[comb];               // ..
................................................................................
  decimal128FromNumber(result, &dn, &dc);
  if (dc.status!=0) {                        // something happened
    decContextSetStatus(set, dc.status);     // .. pass it on
    }
  return result;
  } // decimal128FromString

































#if DECTRACE || DECCHECK


























/* ------------------------------------------------------------------ */
/* decimal128Show -- display a decimal128 in hexadecimal [debug aid]  */
/*   d128 -- the number to show                                       */
/* ------------------------------------------------------------------ */
// Also shows sign/cob/expconfields extracted
void decimal128Show(const decimal128 *d128) {
  char buf[DECIMAL128_Bytes*2+1];
  Int i, j=0;

  #if DECENDIAN
  if (LITEND) {
    for (i=0; i<DECIMAL128_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d128->bytes[15-i]);
      }
    printf(" D128> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
           d128->bytes[15]>>7, (d128->bytes[15]>>2)&0x1f,
           ((d128->bytes[15]&0x3)<<10)|(d128->bytes[14]<<2)|
           (d128->bytes[13]>>6));
    }
   else {
  #endif
    for (i=0; i<DECIMAL128_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d128->bytes[i]);
      }
    printf(" D128> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
           decimal128Sign(d128), decimal128Comb(d128),
           decimal128ExpCon(d128));
  #if DECENDIAN
    }
  #endif
  } // decimal128Show
#endif



|







 







<
<
<
<
<







 







|
<
<

|











<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
<
<

|











<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
<
<

|











<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









<
|









<






<

<


1
2
3
4
5
6
7
8
9
10
11
..
41
42
43
44
45
46
47





48
49
50
51
52
53
54
...
150
151
152
153
154
155
156
157


158
159
160
161
162
163
164
165
166
167
168
169
170



















171
172
173
174
175
176
177
...
188
189
190
191
192
193
194
195


196
197
198
199
200
201
202
203
204
205
206
207
208



















209
210
211
212
213
214
215
...
282
283
284
285
286
287
288
289


290
291
292
293
294
295
296
297
298
299
300
301
302



















303
304
305
306
307
308
309
...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529

530
531
532
533
534
535
536
537
538
539

540
541
542
543
544
545

546

547
548
/* ------------------------------------------------------------------ */
/* Decimal 128-bit format module                                      */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2007.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
extern void decDigitsToDPD(const decNumber *, uInt *, Int);

#if DECTRACE || DECCHECK
void decimal128Show(const decimal128 *);          // for debug
extern void decNumberShow(const decNumber *);     // ..
#endif






/* Useful macro */
// Clear a structure (e.g., a decNumber)
#define DEC_clear(d) memset(d, 0, sizeof(*d))

/* ------------------------------------------------------------------ */
/* decimal128FromNumber -- convert decNumber to decimal128            */
/*                                                                    */
................................................................................
      }
    targhi|=comb<<26;              // add combination field ..
    targhi|=(exp&0xfff)<<14;       // .. and exponent continuation
    } // finite

  if (dn->bits&DECNEG) targhi|=0x80000000; // add sign bit

  // now write to storage; this is endian


  pu=(uInt *)d128->bytes;          // overlay
  if (DECLITEND) {
    pu[0]=targlo;                  // directly store the low int
    pu[1]=targml;                  // then the mid-low
    pu[2]=targmh;                  // then the mid-high
    pu[3]=targhi;                  // then the high int
    }
   else {
    pu[0]=targhi;                  // directly store the high int
    pu[1]=targmh;                  // then the mid-high
    pu[2]=targml;                  // then the mid-low
    pu[3]=targlo;                  // then the low int
    }




















  if (status!=0) decContextSetStatus(set, status); // pass on status
  // decimal128Show(d128);
  return d128;
  } // decimal128FromNumber

/* ------------------------------------------------------------------ */
................................................................................
  Int  need;                       // ..
  uInt sourar[4];                  // source 128-bit
  #define sourhi sourar[3]         // name the word with the sign
  #define sourmh sourar[2]         // and the mid-high word
  #define sourml sourar[1]         // and the mod-low word
  #define sourlo sourar[0]         // and the lowest word

  // load source from storage; this is endian


  pu=(uInt *)d128->bytes;          // overlay
  if (DECLITEND) {
    sourlo=pu[0];                  // directly load the low int
    sourml=pu[1];                  // then the mid-low
    sourmh=pu[2];                  // then the mid-high
    sourhi=pu[3];                  // then the high int
    }
   else {
    sourhi=pu[0];                  // directly load the high int
    sourmh=pu[1];                  // then the mid-high
    sourml=pu[2];                  // then the mid-low
    sourlo=pu[3];                  // then the low int
    }




















  comb=(sourhi>>26)&0x1f;          // combination field

  decNumberZero(dn);               // clean number
  if (sourhi&0x80000000) dn->bits=DECNEG; // set sign if negative

  msd=COMBMSD[comb];               // decode the combination field
................................................................................

  uInt sourar[4];                  // source 128-bit
  #define sourhi sourar[3]         // name the word with the sign
  #define sourmh sourar[2]         // and the mid-high word
  #define sourml sourar[1]         // and the mod-low word
  #define sourlo sourar[0]         // and the lowest word

  // load source from storage; this is endian


  pu=(uInt *)d128->bytes;          // overlay
  if (DECLITEND) {
    sourlo=pu[0];                  // directly load the low int
    sourml=pu[1];                  // then the mid-low
    sourmh=pu[2];                  // then the mid-high
    sourhi=pu[3];                  // then the high int
    }
   else {
    sourhi=pu[0];                  // directly load the high int
    sourmh=pu[1];                  // then the mid-high
    sourml=pu[2];                  // then the mid-low
    sourlo=pu[3];                  // then the low int
    }




















  c=string;                        // where result will go
  if (((Int)sourhi)<0) *c++='-';   // handle sign

  comb=(sourhi>>26)&0x1f;          // combination field
  msd=COMBMSD[comb];               // decode the combination field
  exp=COMBEXP[comb];               // ..
................................................................................
  decimal128FromNumber(result, &dn, &dc);
  if (dc.status!=0) {                        // something happened
    decContextSetStatus(set, dc.status);     // .. pass it on
    }
  return result;
  } // decimal128FromString

/* ------------------------------------------------------------------ */
/* decimal128IsCanonical -- test whether encoding is canonical        */
/*   d128 is the source decimal128                                    */
/*   returns 1 if the encoding of d128 is canonical, 0 otherwise      */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
uint32_t decimal128IsCanonical(const decimal128 *d128) {
  decNumber dn;                         // work
  decimal128 canon;                      // ..
  decContext dc;                        // ..
  decContextDefault(&dc, DEC_INIT_DECIMAL128);
  decimal128ToNumber(d128, &dn);
  decimal128FromNumber(&canon, &dn, &dc);// canon will now be canonical
  return memcmp(d128, &canon, DECIMAL128_Bytes)==0;
  } // decimal128IsCanonical

/* ------------------------------------------------------------------ */
/* decimal128Canonical -- copy an encoding, ensuring it is canonical  */
/*   d128 is the source decimal128                                    */
/*   result is the target (may be the same decimal128)                */
/*   returns result                                                   */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
decimal128 * decimal128Canonical(decimal128 *result, const decimal128 *d128) {
  decNumber dn;                         // work
  decContext dc;                        // ..
  decContextDefault(&dc, DEC_INIT_DECIMAL128);
  decimal128ToNumber(d128, &dn);
  decimal128FromNumber(result, &dn, &dc);// result will now be canonical
  return result;
  } // decimal128Canonical

#if DECTRACE || DECCHECK
/* Macros for accessing decimal128 fields.  These assume the argument
   is a reference (pointer) to the decimal128 structure, and the
   decimal128 is in network byte order (big-endian) */
// Get sign
#define decimal128Sign(d)       ((unsigned)(d)->bytes[0]>>7)

// Get combination field
#define decimal128Comb(d)       (((d)->bytes[0] & 0x7c)>>2)

// Get exponent continuation [does not remove bias]
#define decimal128ExpCon(d)     ((((d)->bytes[0] & 0x03)<<10)         \
                              | ((unsigned)(d)->bytes[1]<<2)          \
                              | ((unsigned)(d)->bytes[2]>>6))

// Set sign [this assumes sign previously 0] */
#define decimal128SetSign(d, b) {                                     \
  (d)->bytes[0]|=((unsigned)(b)<<7);}

// Set exponent continuation [does not apply bias]
// This assumes range has been checked and exponent previously 0;
// type of exponent must be unsigned
#define decimal128SetExpCon(d, e) {                                   \
  (d)->bytes[0]|=(uint8_t)((e)>>10);                                  \
  (d)->bytes[1] =(uint8_t)(((e)&0x3fc)>>2);                           \
  (d)->bytes[2]|=(uint8_t)(((e)&0x03)<<6);}

/* ------------------------------------------------------------------ */
/* decimal128Show -- display a decimal128 in hexadecimal [debug aid]  */
/*   d128 -- the number to show                                       */
/* ------------------------------------------------------------------ */
// Also shows sign/cob/expconfields extracted
void decimal128Show(const decimal128 *d128) {
  char buf[DECIMAL128_Bytes*2+1];
  Int i, j=0;


  if (DECLITEND) {
    for (i=0; i<DECIMAL128_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d128->bytes[15-i]);
      }
    printf(" D128> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
           d128->bytes[15]>>7, (d128->bytes[15]>>2)&0x1f,
           ((d128->bytes[15]&0x3)<<10)|(d128->bytes[14]<<2)|
           (d128->bytes[13]>>6));
    }
   else {

    for (i=0; i<DECIMAL128_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d128->bytes[i]);
      }
    printf(" D128> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
           decimal128Sign(d128), decimal128Comb(d128),
           decimal128ExpCon(d128));

    }

  } // decimal128Show
#endif

Changes to decNumber/decimal128.h.

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102




103
/*   mfc@uk.ibm.com                                                   */
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */

#if !defined(DECIMAL128)
  #define DECIMAL128
  #define DEC128NAME     "decimal128"                 /* Short name */
  #define DEC128FULLNAME "Decimal 128-bit Number"     /* Verbose name */
  #define DEC128AUTHOR   "Mike Cowlishaw"             /* Who to blame */

  /* parameters for decimal128s */
  #define DECIMAL128_Bytes  16          // length
  #define DECIMAL128_Pmax   34          // maximum precision (digits)
  #define DECIMAL128_Emax   6144        // maximum adjusted exponent
  #define DECIMAL128_Emin  -6143        // minimum adjusted exponent
  #define DECIMAL128_Bias   6176        // bias for the exponent
  #define DECIMAL128_String 43          // maximum string length, +1
  #define DECIMAL128_EconL  12          // exponent continuation length
  // highest biased exponent (Elimit-1)
  #define DECIMAL128_Ehigh  (DECIMAL128_Emax+DECIMAL128_Bias-DECIMAL128_Pmax+1)

  // check enough digits, if pre-defined
  #if defined(DECNUMDIGITS)
    #if (DECNUMDIGITS<DECIMAL128_Pmax)
      #error decimal128.h needs pre-defined DECNUMDIGITS>=34 for safe use
    #endif
  #endif

  #ifndef DECNUMDIGITS
    #define DECNUMDIGITS DECIMAL128_Pmax // size if not already defined
  #endif
  #ifndef DECNUMBER
    #include "decNumber.h"              // context and number library
  #endif

  /* Decimal 128-bit type, accessible by bytes */
  typedef struct {
    uint8_t bytes[DECIMAL128_Bytes];    // decimal128: 1, 5, 12, 110 bits
    } decimal128;

  /* special values [top byte excluding sign bit; last two bits are
     don't-care for Infinity on input, last bit don't-care for NaN] */
  #if !defined(DECIMAL_NaN)
    #define DECIMAL_NaN     0x7c        // 0 11111 00 NaN
    #define DECIMAL_sNaN    0x7e        // 0 11111 10 sNaN
    #define DECIMAL_Inf     0x78        // 0 11110 00 Infinity
  #endif

  /* Macros for accessing decimal128 fields.  These assume the argument
     is a reference (pointer) to the decimal128 structure, and the
     decimal128 is in network byte order (big-endian) */
  // Get sign
  #define decimal128Sign(d)       ((unsigned)(d)->bytes[0]>>7)

  // Get combination field
  #define decimal128Comb(d)       (((d)->bytes[0] & 0x7c)>>2)

  // Get exponent continuation [does not remove bias]
  #define decimal128ExpCon(d)     ((((d)->bytes[0] & 0x03)<<10)       \
                                | ((unsigned)(d)->bytes[1]<<2)        \
                                | ((unsigned)(d)->bytes[2]>>6))

  // Set sign [this assumes sign previously 0]
  #define decimal128SetSign(d, b) {                                   \
    (d)->bytes[0]|=((unsigned)(b)<<7);}

  // Set exponent continuation [does not apply bias]
  // This assumes range has been checked and exponent previously 0;
  // type of exponent must be unsigned
  #define decimal128SetExpCon(d, e) {                                 \
    (d)->bytes[0]|=(uint8_t)((e)>>10);                                \
    (d)->bytes[1] =(uint8_t)(((e)&0x3fc)>>2);                         \
    (d)->bytes[2]|=(uint8_t)(((e)&0x03)<<6);}

  /* ------------------------------------------------------------------ */
  /* Routines                                                           */
  /* ------------------------------------------------------------------ */
  // String conversions
  decimal128 * decimal128FromString(decimal128 *, const char *, decContext *);
  char * decimal128ToString(const decimal128 *, char *);
  char * decimal128ToEngString(const decimal128 *, char *);

  // decNumber conversions
  decimal128 * decimal128FromNumber(decimal128 *, const decNumber *,
                                    decContext *);
  decNumber * decimal128ToNumber(const decimal128 *, decNumber *);





#endif







|




|
|
|
|
|
|
|
|


|







|


|


|

|


|
|

|
|
|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|




|




>
>
>
>

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63


























64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*   mfc@uk.ibm.com                                                   */
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */

#if !defined(DECIMAL128)
  #define DECIMAL128
  #define DEC128NAME     "decimal128"                 /* Short name   */
  #define DEC128FULLNAME "Decimal 128-bit Number"     /* Verbose name */
  #define DEC128AUTHOR   "Mike Cowlishaw"             /* Who to blame */

  /* parameters for decimal128s */
  #define DECIMAL128_Bytes  16          /* length                     */
  #define DECIMAL128_Pmax   34          /* maximum precision (digits) */
  #define DECIMAL128_Emax   6144        /* maximum adjusted exponent  */
  #define DECIMAL128_Emin  -6143        /* minimum adjusted exponent  */
  #define DECIMAL128_Bias   6176        /* bias for the exponent      */
  #define DECIMAL128_String 43          /* maximum string length, +1  */
  #define DECIMAL128_EconL  12          /* exp. continuation length   */
  /* highest biased exponent (Elimit-1)                               */
  #define DECIMAL128_Ehigh  (DECIMAL128_Emax+DECIMAL128_Bias-DECIMAL128_Pmax+1)

  /* check enough digits, if pre-defined                              */
  #if defined(DECNUMDIGITS)
    #if (DECNUMDIGITS<DECIMAL128_Pmax)
      #error decimal128.h needs pre-defined DECNUMDIGITS>=34 for safe use
    #endif
  #endif

  #ifndef DECNUMDIGITS
    #define DECNUMDIGITS DECIMAL128_Pmax /* size if not already defined*/
  #endif
  #ifndef DECNUMBER
    #include "decNumber.h"              /* context and number library */
  #endif

  /* Decimal 128-bit type, accessible by bytes                        */
  typedef struct {
    uint8_t bytes[DECIMAL128_Bytes]; /* decimal128: 1, 5, 12, 110 bits*/
    } decimal128;

  /* special values [top byte excluding sign bit; last two bits are   */
  /* don't-care for Infinity on input, last bit don't-care for NaN]   */
  #if !defined(DECIMAL_NaN)
    #define DECIMAL_NaN     0x7c        /* 0 11111 00 NaN             */
    #define DECIMAL_sNaN    0x7e        /* 0 11111 10 sNaN            */
    #define DECIMAL_Inf     0x78        /* 0 11110 00 Infinity        */
  #endif



























  /* ---------------------------------------------------------------- */
  /* Routines                                                         */
  /* ---------------------------------------------------------------- */
  /* String conversions                                               */
  decimal128 * decimal128FromString(decimal128 *, const char *, decContext *);
  char * decimal128ToString(const decimal128 *, char *);
  char * decimal128ToEngString(const decimal128 *, char *);

  /* decNumber conversions                                            */
  decimal128 * decimal128FromNumber(decimal128 *, const decNumber *,
                                    decContext *);
  decNumber * decimal128ToNumber(const decimal128 *, decNumber *);

  /* Format-dependent utilities                                       */
  uint32_t    decimal128IsCanonical(const decimal128 *);
  decimal128 * decimal128Canonical(decimal128 *, const decimal128 *);

#endif

Changes to decNumber/decimal32.c.

1
2
3
4
5
6
7
8
9
10
11
..
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
...
441
442
443
444
445
446
447
































448
























449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* ------------------------------------------------------------------ */
/* Decimal 32-bit format module                                       */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2006.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
extern void decNumberShow(const decNumber *);     // ..
#endif

/* Useful macro */
// Clear a structure (e.g., a decNumber)
#define DEC_clear(d) memset(d, 0, sizeof(*d))

#if !DECENDIAN || DECTRACE || DECCHECK
/* compile-time endian tester [assumes sizeof(int)>1] */
static  const  Int mfcone=1;                 // constant 1
static  const  Flag *mfctop=(Flag *)&mfcone; // -> top byte
#define LITEND mfctop[0]           // named flag; 1=little-endian
#endif

/* ------------------------------------------------------------------ */
/* decimal32FromNumber -- convert decNumber to decimal32              */
/*                                                                    */
/*   ds is the target decimal32                                       */
/*   dn is the source number (assumed valid)                          */
/*   set is the context, used only for reporting errors               */
/*                                                                    */
................................................................................
      }
    targ|=comb<<26;                // add combination field ..
    targ|=(exp&0x3f)<<20;          // .. and exponent continuation
    } // finite

  if (dn->bits&DECNEG) targ|=0x80000000;  // add sign bit

  // now write to storage; this may be endian, or not
  #if DECENDIAN
  // DECENDIAN -- direct store
  pu=(uInt *)d32->bytes;           // overlay
  *pu=targ;                        // directly store the int
  #else
  // not DECENDIAN -- use network byte order
  if (LITEND) {                    // little-endian needs reversal
    uByte *pb;                     // work
    for (pb=&d32->bytes[3]; pb>=d32->bytes; pb--) {
      *pb=(uByte)(targ&0xff);
      targ>>=8;
      } // i
    }
   else { // big-endian; it's the right way round already
    pu=(uInt *)d32->bytes;         // overlay
    *pu=targ;                      // directly store the int
    }
  #endif

  if (status!=0) decContextSetStatus(set, status); // pass on status
  // decimal32Show(d32);
  return d32;
  } // decimal32FromNumber

/* ------------------------------------------------------------------ */
................................................................................
decNumber * decimal32ToNumber(const decimal32 *d32, decNumber *dn) {
  uInt msd;                        // coefficient MSD
  uInt exp;                        // exponent top two bits
  uInt comb;                       // combination field
  uInt *pu;                        // work
  uInt sour;                       // source 32-bit

  // load source from storage; this may be endian, or not
  #if DECENDIAN
  // DECENDIAN -- direct load
  pu=(uInt *)d32->bytes;           // overlay
  sour=*pu;                        // directly load the int
  #else
  // not DECENDIAN -- use network byte order
  if (LITEND) {                    // little-endian needs reversal
    const uByte *pb;               // work
    sour=0;                        // [keep compiler quiet]
    for (pb=d32->bytes; pb<=&d32->bytes[3]; pb++) {
      sour<<=8;
      sour|=*pb;
      } // i
    }
   else { // big-endian; it's the right way round already
    pu=(uInt *)d32->bytes;         // overlay
    sour=*pu;                      // directly load the int
    }
  #endif

  comb=(sour>>26)&0x1f;            // combination field

  decNumberZero(dn);               // clean number
  if (sour&0x80000000) dn->bits=DECNEG; // set sign if negative

  msd=COMBMSD[comb];               // decode the combination field
................................................................................
  uInt *pu;                        // work
  char *s, *t;                     // .. (source, target)
  Int  dpd;                        // ..
  Int  pre, e;                     // ..
  const uByte *u;                  // ..
  uInt sour;                       // source 32-bit

  // load source from storage; this may be endian, or not
  #if DECENDIAN
  // DECENDIAN -- direct load
  pu=(uInt *)d32->bytes;           // overlay
  sour=*pu;                        // directly load the int
  #else
  // not DECENDIAN -- use network byte order
  if (LITEND) {                    // little-endian needs reversal
    const uByte *pb;               // work
    sour=0;                        // [keep compiler quiet]
    for (pb=d32->bytes; pb<=&d32->bytes[3]; pb++) {
      sour<<=8;
      sour|=*pb;
      } // i
    }
   else { // big-endian; it's the right way round already
    pu=(uInt *)d32->bytes;         // overlay
    sour=*pu;                      // directly load the int
    }
  #endif

  c=string;                        // where result will go
  if (((Int)sour)<0) *c++='-';     // handle sign

  comb=(sour>>26)&0x1f;            // combination field
  msd=COMBMSD[comb];               // decode the combination field
  exp=COMBEXP[comb];               // ..
................................................................................
  decimal32FromNumber(result, &dn, &dc);
  if (dc.status!=0) {                        // something happened
    decContextSetStatus(set, dc.status);     // .. pass it on
    }
  return result;
  } // decimal32FromString

































#if DECTRACE || DECCHECK
























/* ------------------------------------------------------------------ */
/* decimal32Show -- display a decimal32 in hexadecimal [debug aid]    */
/*   d32 -- the number to show                                        */
/* ------------------------------------------------------------------ */
// Also shows sign/cob/expconfields extracted - valid bigendian only
void decimal32Show(const decimal32 *d32) {
  char buf[DECIMAL32_Bytes*2+1];
  Int i, j=0;

  #if DECENDIAN
  if (LITEND) {
    for (i=0; i<DECIMAL32_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d32->bytes[3-i]);
      }
    printf(" D32> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
           d32->bytes[3]>>7, (d32->bytes[3]>>2)&0x1f,
           ((d32->bytes[3]&0x3)<<4)| (d32->bytes[2]>>4));
    }
   else {
  #endif
    for (i=0; i<DECIMAL32_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d32->bytes[i]);
      }
    printf(" D32> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
           decimal32Sign(d32), decimal32Comb(d32), decimal32ExpCon(d32));
  #if DECENDIAN
    }
  #endif
  } // decimal32Show
#endif



|







 







<
<
<
<
<
<
<







 







|
<
<


<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
<
<


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
<
<


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









<
|








<





<

<


1
2
3
4
5
6
7
8
9
10
11
..
45
46
47
48
49
50
51







52
53
54
55
56
57
58
...
153
154
155
156
157
158
159
160


161
162














163
164
165
166
167
168
169
...
175
176
177
178
179
180
181
182


183
184















185
186
187
188
189
190
191
...
250
251
252
253
254
255
256
257


258
259















260
261
262
263
264
265
266
...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456

457
458
459
460
461
462
463
464
465

466
467
468
469
470

471

472
473
/* ------------------------------------------------------------------ */
/* Decimal 32-bit format module                                       */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2007.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
extern void decNumberShow(const decNumber *);     // ..
#endif

/* Useful macro */
// Clear a structure (e.g., a decNumber)
#define DEC_clear(d) memset(d, 0, sizeof(*d))








/* ------------------------------------------------------------------ */
/* decimal32FromNumber -- convert decNumber to decimal32              */
/*                                                                    */
/*   ds is the target decimal32                                       */
/*   dn is the source number (assumed valid)                          */
/*   set is the context, used only for reporting errors               */
/*                                                                    */
................................................................................
      }
    targ|=comb<<26;                // add combination field ..
    targ|=(exp&0x3f)<<20;          // .. and exponent continuation
    } // finite

  if (dn->bits&DECNEG) targ|=0x80000000;  // add sign bit

  // now write to storage; this is endian


  pu=(uInt *)d32->bytes;           // overlay
  *pu=targ;                        // directly store the int















  if (status!=0) decContextSetStatus(set, status); // pass on status
  // decimal32Show(d32);
  return d32;
  } // decimal32FromNumber

/* ------------------------------------------------------------------ */
................................................................................
decNumber * decimal32ToNumber(const decimal32 *d32, decNumber *dn) {
  uInt msd;                        // coefficient MSD
  uInt exp;                        // exponent top two bits
  uInt comb;                       // combination field
  uInt *pu;                        // work
  uInt sour;                       // source 32-bit

  // load source from storage; this is endian


  pu=(uInt *)d32->bytes;           // overlay
  sour=*pu;                        // directly load the int
















  comb=(sour>>26)&0x1f;            // combination field

  decNumberZero(dn);               // clean number
  if (sour&0x80000000) dn->bits=DECNEG; // set sign if negative

  msd=COMBMSD[comb];               // decode the combination field
................................................................................
  uInt *pu;                        // work
  char *s, *t;                     // .. (source, target)
  Int  dpd;                        // ..
  Int  pre, e;                     // ..
  const uByte *u;                  // ..
  uInt sour;                       // source 32-bit

  // load source from storage; this is endian


  pu=(uInt *)d32->bytes;           // overlay
  sour=*pu;                        // directly load the int
















  c=string;                        // where result will go
  if (((Int)sour)<0) *c++='-';     // handle sign

  comb=(sour>>26)&0x1f;            // combination field
  msd=COMBMSD[comb];               // decode the combination field
  exp=COMBEXP[comb];               // ..
................................................................................
  decimal32FromNumber(result, &dn, &dc);
  if (dc.status!=0) {                        // something happened
    decContextSetStatus(set, dc.status);     // .. pass it on
    }
  return result;
  } // decimal32FromString

/* ------------------------------------------------------------------ */
/* decimal32IsCanonical -- test whether encoding is canonical         */
/*   d32 is the source decimal32                                      */
/*   returns 1 if the encoding of d32 is canonical, 0 otherwise       */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
uint32_t decimal32IsCanonical(const decimal32 *d32) {
  decNumber dn;                         // work
  decimal32 canon;                      // ..
  decContext dc;                        // ..
  decContextDefault(&dc, DEC_INIT_DECIMAL32);
  decimal32ToNumber(d32, &dn);
  decimal32FromNumber(&canon, &dn, &dc);// canon will now be canonical
  return memcmp(d32, &canon, DECIMAL32_Bytes)==0;
  } // decimal32IsCanonical

/* ------------------------------------------------------------------ */
/* decimal32Canonical -- copy an encoding, ensuring it is canonical   */
/*   d32 is the source decimal32                                      */
/*   result is the target (may be the same decimal32)                 */
/*   returns result                                                   */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
decimal32 * decimal32Canonical(decimal32 *result, const decimal32 *d32) {
  decNumber dn;                         // work
  decContext dc;                        // ..
  decContextDefault(&dc, DEC_INIT_DECIMAL32);
  decimal32ToNumber(d32, &dn);
  decimal32FromNumber(result, &dn, &dc);// result will now be canonical
  return result;
  } // decimal32Canonical

#if DECTRACE || DECCHECK
/* Macros for accessing decimal32 fields.  These assume the argument
   is a reference (pointer) to the decimal32 structure, and the
   decimal32 is in network byte order (big-endian) */
// Get sign
#define decimal32Sign(d)       ((unsigned)(d)->bytes[0]>>7)

// Get combination field
#define decimal32Comb(d)       (((d)->bytes[0] & 0x7c)>>2)

// Get exponent continuation [does not remove bias]
#define decimal32ExpCon(d)     ((((d)->bytes[0] & 0x03)<<4)           \
                             | ((unsigned)(d)->bytes[1]>>4))

// Set sign [this assumes sign previously 0]
#define decimal32SetSign(d, b) {                                      \
  (d)->bytes[0]|=((unsigned)(b)<<7);}

// Set exponent continuation [does not apply bias]
// This assumes range has been checked and exponent previously 0;
// type of exponent must be unsigned
#define decimal32SetExpCon(d, e) {                                    \
  (d)->bytes[0]|=(uint8_t)((e)>>4);                                   \
  (d)->bytes[1]|=(uint8_t)(((e)&0x0F)<<4);}

/* ------------------------------------------------------------------ */
/* decimal32Show -- display a decimal32 in hexadecimal [debug aid]    */
/*   d32 -- the number to show                                        */
/* ------------------------------------------------------------------ */
// Also shows sign/cob/expconfields extracted - valid bigendian only
void decimal32Show(const decimal32 *d32) {
  char buf[DECIMAL32_Bytes*2+1];
  Int i, j=0;


  if (DECLITEND) {
    for (i=0; i<DECIMAL32_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d32->bytes[3-i]);
      }
    printf(" D32> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
           d32->bytes[3]>>7, (d32->bytes[3]>>2)&0x1f,
           ((d32->bytes[3]&0x3)<<4)| (d32->bytes[2]>>4));
    }
   else {

    for (i=0; i<DECIMAL32_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d32->bytes[i]);
      }
    printf(" D32> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
           decimal32Sign(d32), decimal32Comb(d32), decimal32ExpCon(d32));

    }

  } // decimal32Show
#endif

Changes to decNumber/decimal32.h.

1
2
3
4
5
6
7
8
9
10
11
..
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100




101
/* ------------------------------------------------------------------ */
/* Decimal 32-bit format module header                                */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2005.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
/*   mfc@uk.ibm.com                                                   */
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */

#if !defined(DECIMAL32)
  #define DECIMAL32
  #define DEC32NAME     "decimal32"                   /* Short name */
  #define DEC32FULLNAME "Decimal 32-bit Number"       /* Verbose name */
  #define DEC32AUTHOR   "Mike Cowlishaw"              /* Who to blame */

  /* parameters for decimal32s */
  #define DECIMAL32_Bytes  4            // length
  #define DECIMAL32_Pmax   7            // maximum precision (digits)
  #define DECIMAL32_Emax   96           // maximum adjusted exponent
  #define DECIMAL32_Emin  -95           // minimum adjusted exponent
  #define DECIMAL32_Bias   101          // bias for the exponent
  #define DECIMAL32_String 15           // maximum string length, +1
  #define DECIMAL32_EconL  6            // exponent continuation length
  // highest biased exponent (Elimit-1)
  #define DECIMAL32_Ehigh  (DECIMAL32_Emax+DECIMAL32_Bias-DECIMAL32_Pmax+1)

  // check enough digits, if pre-defined
  #if defined(DECNUMDIGITS)
    #if (DECNUMDIGITS<DECIMAL32_Pmax)
      #error decimal32.h needs pre-defined DECNUMDIGITS>=7 for safe use
    #endif
  #endif

  #ifndef DECNUMDIGITS
    #define DECNUMDIGITS DECIMAL32_Pmax // size if not already defined
  #endif
  #ifndef DECNUMBER
    #include "decNumber.h"              // context and number library
  #endif

  /* Decimal 32-bit type, accessible by bytes */
  typedef struct {
    uint8_t bytes[DECIMAL32_Bytes];     // decimal32: 1, 5, 6, 20 bits
    } decimal32;

  /* special values [top byte excluding sign bit; last two bits are
     don't-care for Infinity on input, last bit don't-care for NaN] */
  #if !defined(DECIMAL_NaN)
    #define DECIMAL_NaN     0x7c        // 0 11111 00 NaN
    #define DECIMAL_sNaN    0x7e        // 0 11111 10 sNaN
    #define DECIMAL_Inf     0x78        // 0 11110 00 Infinity
  #endif

  /* Macros for accessing decimal32 fields.  These assume the argument
     is a reference (pointer) to the decimal32 structure, and the
     decimal32 is in network byte order (big-endian) */
  // Get sign
  #define decimal32Sign(d)       ((unsigned)(d)->bytes[0]>>7)

  // Get combination field
  #define decimal32Comb(d)       (((d)->bytes[0] & 0x7c)>>2)

  // Get exponent continuation [does not remove bias]
  #define decimal32ExpCon(d)     ((((d)->bytes[0] & 0x03)<<4)         \
                               | ((unsigned)(d)->bytes[1]>>4))

  // Set sign [this assumes sign previously 0]
  #define decimal32SetSign(d, b) {                                    \
    (d)->bytes[0]|=((unsigned)(b)<<7);}

  // Set exponent continuation [does not apply bias]
  // This assumes range has been checked and exponent previously 0;
  // type of exponent must be unsigned
  #define decimal32SetExpCon(d, e) {                                  \
    (d)->bytes[0]|=(uint8_t)((e)>>4);                                 \
    (d)->bytes[1]|=(uint8_t)(((e)&0x0F)<<4);}

  /* ------------------------------------------------------------------ */
  /* Routines                                                           */
  /* ------------------------------------------------------------------ */
  // String conversions
  decimal32 * decimal32FromString(decimal32 *, const char *, decContext *);
  char * decimal32ToString(const decimal32 *, char *);
  char * decimal32ToEngString(const decimal32 *, char *);

  // decNumber conversions
  decimal32 * decimal32FromNumber(decimal32 *, const decNumber *,
                                  decContext *);
  decNumber * decimal32ToNumber(const decimal32 *, decNumber *);





#endif



|







 







|




|
|
|
|
|
|
|
|


|







|


|




|


|
|

|
|
|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|




|




>
>
>
>

1
2
3
4
5
6
7
8
9
10
11
..
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
























64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* ------------------------------------------------------------------ */
/* Decimal 32-bit format module header                                */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2006.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
/*   mfc@uk.ibm.com                                                   */
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */

#if !defined(DECIMAL32)
  #define DECIMAL32
  #define DEC32NAME     "decimal32"                   /* Short name   */
  #define DEC32FULLNAME "Decimal 32-bit Number"       /* Verbose name */
  #define DEC32AUTHOR   "Mike Cowlishaw"              /* Who to blame */

  /* parameters for decimal32s */
  #define DECIMAL32_Bytes  4            /* length                     */
  #define DECIMAL32_Pmax   7            /* maximum precision (digits) */
  #define DECIMAL32_Emax   96           /* maximum adjusted exponent  */
  #define DECIMAL32_Emin  -95           /* minimum adjusted exponent  */
  #define DECIMAL32_Bias   101          /* bias for the exponent      */
  #define DECIMAL32_String 15           /* maximum string length, +1  */
  #define DECIMAL32_EconL  6            /* exp. continuation length   */
  /* highest biased exponent (Elimit-1)                               */
  #define DECIMAL32_Ehigh  (DECIMAL32_Emax+DECIMAL32_Bias-DECIMAL32_Pmax+1)

  /* check enough digits, if pre-defined                              */
  #if defined(DECNUMDIGITS)
    #if (DECNUMDIGITS<DECIMAL32_Pmax)
      #error decimal32.h needs pre-defined DECNUMDIGITS>=7 for safe use
    #endif
  #endif

  #ifndef DECNUMDIGITS
    #define DECNUMDIGITS DECIMAL32_Pmax /* size if not already defined*/
  #endif
  #ifndef DECNUMBER
    #include "decNumber.h"              /* context and number library */
  #endif

  /* Decimal 32-bit type, accessible by bytes */
  typedef struct {
    uint8_t bytes[DECIMAL32_Bytes];     /* decimal32: 1, 5, 6, 20 bits*/
    } decimal32;

  /* special values [top byte excluding sign bit; last two bits are   */
  /* don't-care for Infinity on input, last bit don't-care for NaN]   */
  #if !defined(DECIMAL_NaN)
    #define DECIMAL_NaN     0x7c        /* 0 11111 00 NaN             */
    #define DECIMAL_sNaN    0x7e        /* 0 11111 10 sNaN            */
    #define DECIMAL_Inf     0x78        /* 0 11110 00 Infinity        */
  #endif

























  /* ---------------------------------------------------------------- */
  /* Routines                                                         */
  /* ---------------------------------------------------------------- */
  /* String conversions                                               */
  decimal32 * decimal32FromString(decimal32 *, const char *, decContext *);
  char * decimal32ToString(const decimal32 *, char *);
  char * decimal32ToEngString(const decimal32 *, char *);

  /* decNumber conversions                                            */
  decimal32 * decimal32FromNumber(decimal32 *, const decNumber *,
                                  decContext *);
  decNumber * decimal32ToNumber(const decimal32 *, decNumber *);

  /* Format-dependent utilities                                       */
  uint32_t    decimal32IsCanonical(const decimal32 *);
  decimal32 * decimal32Canonical(decimal32 *, const decimal32 *);

#endif

Changes to decNumber/decimal64.c.

1
2
3
4
5
6
7
8
9
10
11
..
27
28
29
30
31
32
33
34
35




36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
...
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
...
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
...
502
503
504
505
506
507
508
































509
























510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
...
563
564
565
566
567
568
569
570


571
572


573
574
575
576
577
578
579
580
/* ------------------------------------------------------------------ */
/* Decimal 64-bit format module                                       */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2006.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
#include <stdio.h>            // [for printf]

#define  DECNUMDIGITS 16      // make decNumbers with space for 16
#include "decNumber.h"        // base number library
#include "decNumberLocal.h"   // decNumber local types, etc.
#include "decimal64.h"        // our primary include

/* Utility routines and tables [in decimal64.c] */
extern const uInt COMBEXP[32], COMBMSD[32];




extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
extern void decDigitsToDPD(const decNumber *, uInt *, Int);

#if DECTRACE || DECCHECK
void decimal64Show(const decimal64 *);            // for debug
extern void decNumberShow(const decNumber *);     // ..
#endif

/* compile-time endian tester [assumes sizeof(Int)>1] */
static  const  Int mfcone=1;                 // constant 1
static  const  Flag *mfctop=(Flag *)&mfcone; // -> top byte
#define LITEND *mfctop             // named flag; 1=little-endian

/* Useful macro */
// Clear a structure (e.g., a decNumber)
#define DEC_clear(d) memset(d, 0, sizeof(*d))

/* define and include the tables to use for conversions */
#define DEC_BIN2CHAR 1
#define DEC_DPD2BIN  1
................................................................................
      }
    targhi|=comb<<26;              // add combination field ..
    targhi|=(exp&0xff)<<18;        // .. and exponent continuation
    } // finite

  if (dn->bits&DECNEG) targhi|=0x80000000; // add sign bit

  // now write to storage; this may be endian, or not
  #if DECENDIAN
  // DECENDIAN -- direct store, in the right order
  pu=(uInt *)d64->bytes;           // overlay
  if (LITEND) {
    pu[0]=targar[0];               // directly store the low int
    pu[1]=targar[1];               // then the high int
    }
   else {
    pu[0]=targar[1];               // directly store the high int
    pu[1]=targar[0];               // then the low int
    }
  #else
  // not DECENDIAN -- use network byte order
  if (LITEND) {                    // little-endian needs reversal
    uByte *pb;                     // work
    Int off;                       // ..
    for (pb=&d64->bytes[7]; pb>=d64->bytes; pb--) {
      off=1-((pb-d64->bytes)>>2);  // 0 then 1
      *pb=(uByte)(targar[off]&0xff);
      targar[off]>>=8;
      } // i
    }
   else { // big-endian; it's the right way round already
    pu=(uInt *)d64->bytes;         // overlay
    pu[0]=targar[1];               // directly store the high int
    pu[1]=targar[0];               // then the low int
    }
  #endif

  if (status!=0) decContextSetStatus(set, status); // pass on status
  // decimal64Show(d64);
  return d64;
  } // decimal64FromNumber

/* ------------------------------------------------------------------ */
................................................................................
  uInt comb;                       // combination field
  uInt *pu;                        // work
  Int  need;                       // ..
  uInt sourar[2];                  // source 64-bit
  #define sourhi sourar[1]         // name the word with the sign
  #define sourlo sourar[0]         // and the lower word

  // load source from storage; this may be endian, or not
  #if DECENDIAN
  // DECENDIAN -- direct load, in the right order
  pu=(uInt *)d64->bytes;           // overlay
  if (LITEND) {
    sourlo=pu[0];                  // directly load the low int
    sourhi=pu[1];                  // then the high int
    }
   else {
    sourhi=pu[0];                  // directly load the high int
    sourlo=pu[1];                  // then the low int
    }
  #else
  // not DECENDIAN -- use network byte order
  if (LITEND) {                    // little-endian needs reversal
    const uByte *pb;               // work
    Int off;                       // ..
    for (pb=d64->bytes; pb<=&d64->bytes[7]; pb++) {
      off=1-((pb-d64->bytes)>>2);  // 1 then 0
      sourar[off]<<=8;
      sourar[off]|=*pb;
      } // i
    }
   else { // big-endian; it's the right way round already
    pu=(uInt *)d64->bytes;         // overlay
    sourhi=pu[0];                  // directly load the high int
    sourlo=pu[1];                  // then the low int
    }
  #endif

  comb=(sourhi>>26)&0x1f;          // combination field

  decNumberZero(dn);               // clean number
  if (sourhi&0x80000000) dn->bits=DECNEG; // set sign if negative

  msd=COMBMSD[comb];               // decode the combination field
................................................................................
  Int  pre, e;                     // ..
  const uByte *u;                  // ..

  uInt sourar[2];                  // source 64-bit
  #define sourhi sourar[1]         // name the word with the sign
  #define sourlo sourar[0]         // and the lower word

  // load source from storage; this may be endian, or not
  #if DECENDIAN
  // DECENDIAN -- direct load, in the right order
  pu=(uInt *)d64->bytes;           // overlay
  if (LITEND) {
    sourlo=pu[0];                  // directly load the low int
    sourhi=pu[1];                  // then the high int
    }
   else {
    sourhi=pu[0];                  // directly load the high int
    sourlo=pu[1];                  // then the low int
    }
  #else
  // not DECENDIAN -- use network byte order
  if (LITEND) {                    // little-endian needs reversal
    const uByte *pb;               // work
    Int off;                       // ..
    for (pb=d64->bytes; pb<=&d64->bytes[7]; pb++) {
      off=1-((pb-d64->bytes)>>2);  // 1 then 0
      sourar[off]<<=8;
      sourar[off]|=*pb;
      } // i
    }
   else { // big-endian; it's the right way round already
    pu=(uInt *)d64->bytes;         // overlay
    sourhi=pu[0];                  // directly load the high int
    sourlo=pu[1];                  // then the low int
    }
  #endif

  c=string;                        // where result will go
  if (((Int)sourhi)<0) *c++='-';   // handle sign

  comb=(sourhi>>26)&0x1f;          // combination field
  msd=COMBMSD[comb];               // decode the combination field
  exp=COMBEXP[comb];               // ..
................................................................................
  decimal64FromNumber(result, &dn, &dc);
  if (dc.status!=0) {                        // something happened
    decContextSetStatus(set, dc.status);     // .. pass it on
    }
  return result;
  } // decimal64FromString

































#if DECTRACE || DECCHECK
























/* ------------------------------------------------------------------ */
/* decimal64Show -- display a decimal64 in hexadecimal [debug aid]    */
/*   d64 -- the number to show                                        */
/* ------------------------------------------------------------------ */
// Also shows sign/cob/expconfields extracted
void decimal64Show(const decimal64 *d64) {
  char buf[DECIMAL64_Bytes*2+1];
  Int i, j=0;

  #if DECENDIAN
  if (LITEND) {
    for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d64->bytes[7-i]);
      }
    printf(" D64> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
           d64->bytes[7]>>7, (d64->bytes[7]>>2)&0x1f,
           ((d64->bytes[7]&0x3)<<6)| (d64->bytes[6]>>2));
    }
   else {
  #endif
    for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d64->bytes[i]);
      }
    printf(" D64> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
           decimal64Sign(d64), decimal64Comb(d64), decimal64ExpCon(d64));
  #if DECENDIAN
    }
  #endif
  } // decimal64Show
#endif

/* ================================================================== */
/* Shared utility routines and tables                                 */
/* ================================================================== */
// define and include the conversion tables to use for shared code
................................................................................
/*      COMBEXP - 2-bit most-significant-bits of exponent             */
/*                [11 if an Infinity or NaN]                          */
/*      COMBMSD - 4-bit most-significant-digit                        */
/*                [0=Infinity, 1=NaN if COMBEXP=11]                   */
/*                                                                    */
/* Both are indexed by the 5-bit combination field (0-31)             */
/* ------------------------------------------------------------------ */
const uInt COMBEXP[32]={0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,


                        2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 1, 2, 2, 3, 3};
const uInt COMBMSD[32]={0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,


                        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 1};

/* ------------------------------------------------------------------ */
/* decDigitsToDPD -- pack coefficient into DPD form                   */
/*                                                                    */
/*   dn   is the source number (assumed valid, max DECMAX754 digits)  */
/*   targ is 1, 2, or 4-element uInt array, which the caller must     */
/*        have cleared to zeros                                       */



|







 







|

>
>
>
>








<
<
<
<
<







 







|
<
<

|







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
<
<

|







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
<
<

|







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









<
|







|
<





<

<







 







|
>
>
|
|
>
>
|







1
2
3
4
5
6
7
8
9
10
11
..
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47





48
49
50
51
52
53
54
...
171
172
173
174
175
176
177
178


179
180
181
182
183
184
185
186
187

















188
189
190
191
192
193
194
...
203
204
205
206
207
208
209
210


211
212
213
214
215
216
217
218
219

















220
221
222
223
224
225
226
...
296
297
298
299
300
301
302
303


304
305
306
307
308
309
310
311
312

















313
314
315
316
317
318
319
...
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516

517
518
519
520
521
522
523
524
525

526
527
528
529
530

531

532
533
534
535
536
537
538
...
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* ------------------------------------------------------------------ */
/* Decimal 64-bit format module                                       */
/* ------------------------------------------------------------------ */
/* Copyright (c) IBM Corporation, 2000, 2007.  All rights reserved.   */
/*                                                                    */
/* This software is made available under the terms of the             */
/* ICU License -- ICU 1.8.1 and later.                                */
/*                                                                    */
/* The description and User's Guide ("The decNumber C Library") for   */
/* this software is called decNumber.pdf.  This document is           */
/* available, together with arithmetic and format specifications,     */
................................................................................
#include <stdio.h>            // [for printf]

#define  DECNUMDIGITS 16      // make decNumbers with space for 16
#include "decNumber.h"        // base number library
#include "decNumberLocal.h"   // decNumber local types, etc.
#include "decimal64.h"        // our primary include

/* Utility routines and tables [in decimal64.c]; externs for C++ */
extern const uInt COMBEXP[32], COMBMSD[32];
extern const uShort DPD2BIN[1024];
extern const uShort BIN2DPD[1000];
extern const uByte  BIN2CHAR[4001];

extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
extern void decDigitsToDPD(const decNumber *, uInt *, Int);

#if DECTRACE || DECCHECK
void decimal64Show(const decimal64 *);            // for debug
extern void decNumberShow(const decNumber *);     // ..
#endif






/* Useful macro */
// Clear a structure (e.g., a decNumber)
#define DEC_clear(d) memset(d, 0, sizeof(*d))

/* define and include the tables to use for conversions */
#define DEC_BIN2CHAR 1
#define DEC_DPD2BIN  1
................................................................................
      }
    targhi|=comb<<26;              // add combination field ..
    targhi|=(exp&0xff)<<18;        // .. and exponent continuation
    } // finite

  if (dn->bits&DECNEG) targhi|=0x80000000; // add sign bit

  // now write to storage; this is now always endian


  pu=(uInt *)d64->bytes;           // overlay
  if (DECLITEND) {
    pu[0]=targar[0];               // directly store the low int
    pu[1]=targar[1];               // then the high int
    }
   else {
    pu[0]=targar[1];               // directly store the high int
    pu[1]=targar[0];               // then the low int
    }


















  if (status!=0) decContextSetStatus(set, status); // pass on status
  // decimal64Show(d64);
  return d64;
  } // decimal64FromNumber

/* ------------------------------------------------------------------ */
................................................................................
  uInt comb;                       // combination field
  uInt *pu;                        // work
  Int  need;                       // ..
  uInt sourar[2];                  // source 64-bit
  #define sourhi sourar[1]         // name the word with the sign
  #define sourlo sourar[0]         // and the lower word

  // load source from storage; this is endian


  pu=(uInt *)d64->bytes;           // overlay
  if (DECLITEND) {
    sourlo=pu[0];                  // directly load the low int
    sourhi=pu[1];                  // then the high int
    }
   else {
    sourhi=pu[0];                  // directly load the high int
    sourlo=pu[1];                  // then the low int
    }


















  comb=(sourhi>>26)&0x1f;          // combination field

  decNumberZero(dn);               // clean number
  if (sourhi&0x80000000) dn->bits=DECNEG; // set sign if negative

  msd=COMBMSD[comb];               // decode the combination field
................................................................................
  Int  pre, e;                     // ..
  const uByte *u;                  // ..

  uInt sourar[2];                  // source 64-bit
  #define sourhi sourar[1]         // name the word with the sign
  #define sourlo sourar[0]         // and the lower word

  // load source from storage; this is endian


  pu=(uInt *)d64->bytes;           // overlay
  if (DECLITEND) {
    sourlo=pu[0];                  // directly load the low int
    sourhi=pu[1];                  // then the high int
    }
   else {
    sourhi=pu[0];                  // directly load the high int
    sourlo=pu[1];                  // then the low int
    }


















  c=string;                        // where result will go
  if (((Int)sourhi)<0) *c++='-';   // handle sign

  comb=(sourhi>>26)&0x1f;          // combination field
  msd=COMBMSD[comb];               // decode the combination field
  exp=COMBEXP[comb];               // ..
................................................................................
  decimal64FromNumber(result, &dn, &dc);
  if (dc.status!=0) {                        // something happened
    decContextSetStatus(set, dc.status);     // .. pass it on
    }
  return result;
  } // decimal64FromString

/* ------------------------------------------------------------------ */
/* decimal64IsCanonical -- test whether encoding is canonical         */
/*   d64 is the source decimal64                                      */
/*   returns 1 if the encoding of d64 is canonical, 0 otherwise       */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
uint32_t decimal64IsCanonical(const decimal64 *d64) {
  decNumber dn;                         // work
  decimal64 canon;                      // ..
  decContext dc;                        // ..
  decContextDefault(&dc, DEC_INIT_DECIMAL64);
  decimal64ToNumber(d64, &dn);
  decimal64FromNumber(&canon, &dn, &dc);// canon will now be canonical
  return memcmp(d64, &canon, DECIMAL64_Bytes)==0;
  } // decimal64IsCanonical

/* ------------------------------------------------------------------ */
/* decimal64Canonical -- copy an encoding, ensuring it is canonical   */
/*   d64 is the source decimal64                                      */
/*   result is the target (may be the same decimal64)                 */
/*   returns result                                                   */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
decimal64 * decimal64Canonical(decimal64 *result, const decimal64 *d64) {
  decNumber dn;                         // work
  decContext dc;                        // ..
  decContextDefault(&dc, DEC_INIT_DECIMAL64);
  decimal64ToNumber(d64, &dn);
  decimal64FromNumber(result, &dn, &dc);// result will now be canonical
  return result;
  } // decimal64Canonical

#if DECTRACE || DECCHECK
/* Macros for accessing decimal64 fields.  These assume the
   argument is a reference (pointer) to the decimal64 structure,
   and the decimal64 is in network byte order (big-endian) */
// Get sign
#define decimal64Sign(d)       ((unsigned)(d)->bytes[0]>>7)

// Get combination field
#define decimal64Comb(d)       (((d)->bytes[0] & 0x7c)>>2)

// Get exponent continuation [does not remove bias]
#define decimal64ExpCon(d)     ((((d)->bytes[0] & 0x03)<<6)           \
                             | ((unsigned)(d)->bytes[1]>>2))

// Set sign [this assumes sign previously 0]
#define decimal64SetSign(d, b) {                                      \
  (d)->bytes[0]|=((unsigned)(b)<<7);}

// Set exponent continuation [does not apply bias]
// This assumes range has been checked and exponent previously 0;
// type of exponent must be unsigned
#define decimal64SetExpCon(d, e) {                                    \
  (d)->bytes[0]|=(uint8_t)((e)>>6);                                   \
  (d)->bytes[1]|=(uint8_t)(((e)&0x3F)<<2);}

/* ------------------------------------------------------------------ */
/* decimal64Show -- display a decimal64 in hexadecimal [debug aid]    */
/*   d64 -- the number to show                                        */
/* ------------------------------------------------------------------ */
// Also shows sign/cob/expconfields extracted
void decimal64Show(const decimal64 *d64) {
  char buf[DECIMAL64_Bytes*2+1];
  Int i, j=0;


  if (DECLITEND) {
    for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d64->bytes[7-i]);
      }
    printf(" D64> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
           d64->bytes[7]>>7, (d64->bytes[7]>>2)&0x1f,
           ((d64->bytes[7]&0x3)<<6)| (d64->bytes[6]>>2));
    }
   else { // big-endian

    for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d64->bytes[i]);
      }
    printf(" D64> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
           decimal64Sign(d64), decimal64Comb(d64), decimal64ExpCon(d64));

    }

  } // decimal64Show
#endif

/* ================================================================== */
/* Shared utility routines and tables                                 */
/* ================================================================== */
// define and include the conversion tables to use for shared code
................................................................................
/*      COMBEXP - 2-bit most-significant-bits of exponent             */
/*                [11 if an Infinity or NaN]                          */
/*      COMBMSD - 4-bit most-significant-digit                        */
/*                [0=Infinity, 1=NaN if COMBEXP=11]                   */
/*                                                                    */
/* Both are indexed by the 5-bit combination field (0-31)             */
/* ------------------------------------------------------------------ */
const uInt COMBEXP[32]={0, 0, 0, 0, 0, 0, 0, 0,
                        1, 1, 1, 1, 1, 1, 1, 1,
                        2, 2, 2, 2, 2, 2, 2, 2,
                        0, 0, 1, 1, 2, 2, 3, 3};
const uInt COMBMSD[32]={0, 1, 2, 3, 4, 5, 6, 7,
                        0, 1, 2, 3, 4, 5, 6, 7,
                        0, 1, 2, 3, 4, 5, 6, 7,
                        8, 9, 8, 9, 8, 9, 0, 1};

/* ------------------------------------------------------------------ */
/* decDigitsToDPD -- pack coefficient into DPD form                   */
/*                                                                    */
/*   dn   is the source number (assumed valid, max DECMAX754 digits)  */
/*   targ is 1, 2, or 4-element uInt array, which the caller must     */
/*        have cleared to zeros                                       */

Changes to decNumber/decimal64.h.

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102




103
/*   mfc@uk.ibm.com                                                   */
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */

#if !defined(DECIMAL64)
  #define DECIMAL64
  #define DEC64NAME     "decimal64"                   /* Short name */
  #define DEC64FULLNAME "Decimal 64-bit Number"       /* Verbose name */
  #define DEC64AUTHOR   "Mike Cowlishaw"              /* Who to blame */


  /* parameters for decimal64s */
  #define DECIMAL64_Bytes  8            // length
  #define DECIMAL64_Pmax   16           // maximum precision (digits)
  #define DECIMAL64_Emax   384          // maximum adjusted exponent
  #define DECIMAL64_Emin  -383          // minimum adjusted exponent
  #define DECIMAL64_Bias   398          // bias for the exponent
  #define DECIMAL64_String 24           // maximum string length, +1
  #define DECIMAL64_EconL  8            // exponent continuation length
  // highest biased exponent (Elimit-1)
  #define DECIMAL64_Ehigh  (DECIMAL64_Emax+DECIMAL64_Bias-DECIMAL64_Pmax+1)

  // check enough digits, if pre-defined
  #if defined(DECNUMDIGITS)
    #if (DECNUMDIGITS<DECIMAL64_Pmax)
      #error decimal64.h needs pre-defined DECNUMDIGITS>=16 for safe use
    #endif
  #endif


  #ifndef DECNUMDIGITS
    #define DECNUMDIGITS DECIMAL64_Pmax // size if not already defined
  #endif
  #ifndef DECNUMBER
    #include "decNumber.h"              // context and number library
  #endif

  /* Decimal 64-bit type, accessible by bytes */
  typedef struct {
    uint8_t bytes[DECIMAL64_Bytes];     // decimal64: 1, 5, 8, 50 bits
    } decimal64;

  /* special values [top byte excluding sign bit; last two bits are
     don't-care for Infinity on input, last bit don't-care for NaN] */
  #if !defined(DECIMAL_NaN)
    #define DECIMAL_NaN     0x7c        // 0 11111 00 NaN
    #define DECIMAL_sNaN    0x7e        // 0 11111 10 sNaN
    #define DECIMAL_Inf     0x78        // 0 11110 00 Infinity
  #endif

  /* Macros for accessing decimal64 fields.  These assume the argument
     is a reference (pointer) to the decimal64 structure, and the
     decimal64 is in network byte order (big-endian) */
  // Get sign
  #define decimal64Sign(d)       ((unsigned)(d)->bytes[0]>>7)

  // Get combination field
  #define decimal64Comb(d)       (((d)->bytes[0] & 0x7c)>>2)

  // Get exponent continuation [does not remove bias]
  #define decimal64ExpCon(d)     ((((d)->bytes[0] & 0x03)<<6)         \
                               | ((unsigned)(d)->bytes[1]>>2))

  // Set sign [this assumes sign previously 0]
  #define decimal64SetSign(d, b) {                                    \
    (d)->bytes[0]|=((unsigned)(b)<<7);}

  // Set exponent continuation [does not apply bias]
  // This assumes range has been checked and exponent previously 0; type
  // of exponent must be unsigned
  #define decimal64SetExpCon(d, e) {                                  \
    (d)->bytes[0]|=(uint8_t)((e)>>6);                                 \
    (d)->bytes[1]|=(uint8_t)(((e)&0x3F)<<2);}

  /* ------------------------------------------------------------------ */
  /* Routines                                                           */
  /* ------------------------------------------------------------------ */
  // String conversions
  decimal64 * decimal64FromString(decimal64 *, const char *, decContext *);
  char * decimal64ToString(const decimal64 *, char *);
  char * decimal64ToEngString(const decimal64 *, char *);

  // decNumber conversions
  decimal64 * decimal64FromNumber(decimal64 *, const decNumber *,
                                  decContext *);
  decNumber * decimal64ToNumber(const decimal64 *, decNumber *);





#endif







|




|
|
|
|
|
|
|
|
|


|








|


|


|

|


|
|

|
|
|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|




|




>
>
>
>

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
























66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*   mfc@uk.ibm.com                                                   */
/*   Mike Cowlishaw, IBM Fellow                                       */
/*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
/* ------------------------------------------------------------------ */

#if !defined(DECIMAL64)
  #define DECIMAL64
  #define DEC64NAME     "decimal64"                   /* Short name   */
  #define DEC64FULLNAME "Decimal 64-bit Number"       /* Verbose name */
  #define DEC64AUTHOR   "Mike Cowlishaw"              /* Who to blame */


  /* parameters for decimal64s                                        */
  #define DECIMAL64_Bytes  8            /* length                     */
  #define DECIMAL64_Pmax   16           /* maximum precision (digits) */
  #define DECIMAL64_Emax   384          /* maximum adjusted exponent  */
  #define DECIMAL64_Emin  -383          /* minimum adjusted exponent  */
  #define DECIMAL64_Bias   398          /* bias for the exponent      */
  #define DECIMAL64_String 24           /* maximum string length, +1  */
  #define DECIMAL64_EconL  8            /* exp. continuation length   */
  /* highest biased exponent (Elimit-1)                               */
  #define DECIMAL64_Ehigh  (DECIMAL64_Emax+DECIMAL64_Bias-DECIMAL64_Pmax+1)

  /* check enough digits, if pre-defined                              */
  #if defined(DECNUMDIGITS)
    #if (DECNUMDIGITS<DECIMAL64_Pmax)
      #error decimal64.h needs pre-defined DECNUMDIGITS>=16 for safe use
    #endif
  #endif


  #ifndef DECNUMDIGITS
    #define DECNUMDIGITS DECIMAL64_Pmax /* size if not already defined*/
  #endif
  #ifndef DECNUMBER
    #include "decNumber.h"              /* context and number library */
  #endif

  /* Decimal 64-bit type, accessible by bytes                         */
  typedef struct {
    uint8_t bytes[DECIMAL64_Bytes];     /* decimal64: 1, 5, 8, 50 bits*/
    } decimal64;

  /* special values [top byte excluding sign bit; last two bits are   */
  /* don't-care for Infinity on input, last bit don't-care for NaN]   */
  #if !defined(DECIMAL_NaN)
    #define DECIMAL_NaN     0x7c        /* 0 11111 00 NaN             */
    #define DECIMAL_sNaN    0x7e        /* 0 11111 10 sNaN            */
    #define DECIMAL_Inf     0x78        /* 0 11110 00 Infinity        */
  #endif

























  /* ---------------------------------------------------------------- */
  /* Routines                                                         */
  /* ---------------------------------------------------------------- */
  /* String conversions                                               */
  decimal64 * decimal64FromString(decimal64 *, const char *, decContext *);
  char * decimal64ToString(const decimal64 *, char *);
  char * decimal64ToEngString(const decimal64 *, char *);

  /* decNumber conversions                                            */
  decimal64 * decimal64FromNumber(decimal64 *, const decNumber *,
                                  decContext *);
  decNumber * decimal64ToNumber(const decimal64 *, decNumber *);

  /* Format-dependent utilities                                       */
  uint32_t    decimal64IsCanonical(const decimal64 *);
  decimal64 * decimal64Canonical(decimal64 *, const decimal64 *);

#endif

Changes to decNumber/example6.c.

48
49
50
51
52
53
54
55
56
57
58
59

  decPackedFromNumber(respack, sizeof(respack), &resscale, &total);

  // lay out the total as sixteen hexadecimal pairs
  for (i=0; i<16; i++) {
    sprintf(&hexes[i*3], "%02x ", respack[i]);
    }
  printf("Result: %s (scale=%d)\n", hexes, resscale);

  } //---------------------------------------------------------------|
  return 0;
  } // main







|




48
49
50
51
52
53
54
55
56
57
58
59

  decPackedFromNumber(respack, sizeof(respack), &resscale, &total);

  // lay out the total as sixteen hexadecimal pairs
  for (i=0; i<16; i++) {
    sprintf(&hexes[i*3], "%02x ", respack[i]);
    }
  printf("Result: %s (scale=%ld)\n", hexes, resscale);

  } //---------------------------------------------------------------|
  return 0;
  } // main

Changes to decNumber/readme.txt.

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
..
53
54
55
56
57
58
59
60


61
62
63
64
65
66
67

     Note: a commercial license for this code is also available by
     following the 'License this technology' link from the alphaWorks
     page: http://www.alphaWorks.ibm.com/tech/decnumber

  *  decNumber.pdf (documentation)

  *  The .c and .h file for each module in the package (see
     documentation), decDPD.h (used by decimal32), and
     decNumberLocal.h (local definitions)

  *  The .c files for each of the examples (example1.c through
     example6.c)

The alphaWorks package is made available under the terms of the IBM
alphaWorks License Agreement (included in various languages in the
file alphaWorks-license-files.zip), unless you have agreed different
licensing terms with IBM.  Your use of that package indicates your
acceptance of the terms and conditions of that Agreement.

................................................................................



To use and check decNumber
--------------------------

  Please read the appropriate license and documentation before using
  this package.



  1. Compile and link example1.c, decNumber.c, and decContext.c
     For example:

       gcc -o example1 example1.c decNumber.c decContext.c

     Note: If your compiler does not provide stdint.h or if your C







|
|



|







 







|
>
>







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
..
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

     Note: a commercial license for this code is also available by
     following the 'License this technology' link from the alphaWorks
     page: http://www.alphaWorks.ibm.com/tech/decnumber

  *  decNumber.pdf (documentation)

  *  The .c and .h file for each module in the package (see the
     decNumber documentation), decDPD.h (used by decimal64), and
     decNumberLocal.h (local definitions)

  *  The .c files for each of the examples (example1.c through
     example6.c).

The alphaWorks package is made available under the terms of the IBM
alphaWorks License Agreement (included in various languages in the
file alphaWorks-license-files.zip), unless you have agreed different
licensing terms with IBM.  Your use of that package indicates your
acceptance of the terms and conditions of that Agreement.

................................................................................



To use and check decNumber
--------------------------

  Please read the appropriate license and documentation before using
  this package.  If you are upgrading an existing use of decNumber
  (version <= 3.37) please read the Changes Appendix for later
  versions -- you may need to change the DECLITEND flag.

  1. Compile and link example1.c, decNumber.c, and decContext.c
     For example:

       gcc -o example1 example1.c decNumber.c decContext.c

     Note: If your compiler does not provide stdint.h or if your C

Changes to doc/ldecNumber.odt.

cannot compute difference between binary files

Changes to doc/ldecNumber.pod.

134
135
136
137
138
139
140





141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
...
312
313
314
315
316
317
318

319
320
321
322
323
324
325
...
343
344
345
346
347
348
349





















350
351
352
353
354
355
356
...
673
674
675
676
677
678
679




















































680
681
682
683
684
685
686
...
732
733
734
735
736
737
738



































739
740
741
742
743
744
745
...
752
753
754
755
756
757
758

























759
760
761
762
763
764
765
766
767












768
769
770
771
772
773
774
775
776












777
778
779
780
781
782
783
...
826
827
828
829
830
831
832




















































833
834
835
836
837
838
839
840
...
931
932
933
934
935
936
937




















938
939
940
941
942
943
944
945
946
947
948
949
950


































951
952
953
954
955
956
957
...
970
971
972
973
974
975
976
















977
978
979
980
981
982
983
984




985
986
987
988
989
990
991
...
993
994
995
996
997
998
999











1000
1001


































1002
1003
1004
1005
1006
1007
1008
....
1027
1028
1029
1030
1031
1032
1033












1034
1035
1036
1037
1038
1039
1040
....
1043
1044
1045
1046
1047
1048
1049











1050
1051
1052
1053
1054
1055
1056


1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079





















1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099






















1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
....
1138
1139
1140
1141
1142
1143
1144










1145
1146
1147
1148
1149
1150
1151
....
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
retaining the spelling, and 

=item *

retaining the case for constant names

=back






Wherever there was a predefined Lua metamethod, e. g., C<__add>, the 
appropriate function is bound to that name as well as the B<decNumber> 
package function name.

Where it seemed appropriate, functions are provided both as methods 
on decimal numbers, as well as functions in the C<decNumber> module.

=head2 Mutability

The decimal numbers created by Lua are not mutable. This decision 
was based on my judgment that the potential performance benefit, 
mainly lower memory consumption and less garbage collection, was 
outweighed by the safety and lack of surprise that immutability 
provides. This makes decimal numbers compatible with Lua numbers 
and strings, both of which are also not mutable.

=head2 Conversion

All the functions in the C<decNumber> module automatically convert 
their arguments from Lua numbers or strings as necessary to perform 
................................................................................
is included, but has been designed for WindowsXP.

=head2 Compliance Test

File: I<ldecNumberTestDriver.lua>

This is the big test. It uses dectest sources from IBM, and has 
over 25,000 test cases. The Lua file is a driver to execute the tests 
specified by dectest.

Since version 10 of the Lua C<decNumber> module, the results for this 
test are: 

C<For all 26205 tests, 26047 succeeded, 5 failed, 54 failed(conv), 69 skipped(#), 30 skipped(prec).>

This is as good as possible with the default configuration. What this means is:

	26047 succeeded	woot!
	5 failed	these 5 tests are know to fail in the decNumber C library;
             these edge cases are under reconsideration in the Decimal Number Specification
	54 failed(conv)	 the precision required for the operands is insufficient in the Lua wrapper
	69 skipped(#)	 the test is for NULL arguments or format conversions not supported
	30 skipped(prec) the test called for a  precision larger than provided in the Lua wrapper

=head1 REFERENCE

Here is a reference to the data types, constants, and functions in the 
C<decNumber> module. Many of these will refer to the decNumber C library 
User's Guide, I<decNumber.pdf>, for implementation details.

................................................................................
    decNumber.ROUND_CEILING     round towards +infinity
    decNumber.ROUND_UP          round away from 0
    decNumber.ROUND_HALF_UP     0.5 rounds up
    decNumber.ROUND_HALF_EVEN   0.5 rounds to nearest even
    decNumber.ROUND_HALF_DOWN   0.5 rounds down
    decNumber.ROUND_DOWN        round towards 0 (truncate)
    decNumber.ROUND_FLOOR       round towards -infinity


=head1 Status Flags

These numeric status flags are used with 
L<C<decctx:getstatus()>|/decctx:getstatus>

    decNumber.Conversion_syntax
................................................................................
    decNumber.IEEE_854_Invalid_operation
    decNumber.IEEE_854_Overflow
    decNumber.IEEE_854_Underflow
    decNumber.Errors       normally errors (results are qNaN, infinite, or 0)
    decNumber.NaNs         cause a result to become qNaN
    decNumber.Information  normally for information only (have finite results)






















=head1 Initialization Descriptors

These constants are used with 
L<C<decctx:setdefault(x)>|/decctx:setdefault>

    decNumber.INIT_BASE
    decNumber.INIT_DECIMAL32
................................................................................

Note that by binding the method C<__add> to this function, the 
Lua addition operator (C<+>) may be used with a C<decnum> on the 
left and a C<decarg> on the right.

Uses the C library function C<decNumberAdd()>.





















































=head2 C<decnum:divide>

    decnum:divide (decarg)
    decnum:__div (decarg)
    decNumber.divide (decarg, decarg)

Returns a decimal number that is the left (1st) argument divided 
................................................................................
The current context's rounding mode is used. 
See L<C<decnum:mod>|/decnum:mod>.

Uses the C library function C<decNumberDivideInteger()>, and then 
C<decNumberMultiply()> followed by C<decNumberCompare() decNumberIsZero()> 
to check if the remainder is zero, and C<decNumberSubtract()>.




































=head2 C<decnum:ln>

    decnum:ln ()
    decNumber.ln (decarg)

Returns a decimal number that is the natural logarithm (logarithm in 
base e) of the argument.
................................................................................
    decNumber.log10 (decarg)

Returns a decimal number that is the logarithm in base ten of the 
argument.

Uses the C library function C<decNumberLog10()>.


























=head2 C<decnum:max>

    decnum:max (decarg)
    decNumber.max (decarg, decarg)

Returns a decimal number that is the maximum of its arguments.

Uses the C library function C<decNumberMax()>.













=head2 C<decnum:min>

    decnum:min (decarg)
    decNumber.min (decarg, decarg)

Returns a decimal number that is the minimum of its arguments.

Uses the C library function C<decNumberMin()>.













=head2 C<decnum:minus>

    decnum:minus ()
    decnum:__unm ()
    decNumber.minus (decarg)

Returns a decimal number that is the result of subtracting 
................................................................................

Note that by binding the method C<__mul> to this function, the Lua 
multiplication operator (C<*>) may be used with a C<decnum> on the 
left and a C<decarg> on the right.

Uses the C library function C<decNumberMultiply()>.





















































=head2 C<ddecnum:normalize>

    decnum:normalize ()
    decNumber.normalize (decarg)

Returns a decimal number that is the result of adding the argument 
to 0, and putting the result in its simplest form. That is, a non-zero 
number which has any trailing zeros in the coefficient has those zeros 
................................................................................
has the value of the right (2nd) argument. See 
L<C<decnum:quantize>|/decnum:quantize>. 
The right (2nd) argument must be a whole number (before any rounding); 
that is, any digits in the fractional part of the number must be zero.

Uses the C library function C<decNumberRescale()>.





















=head2 C<decnum:samequantum>

    decnum:samequantum (decarg)
    decNumber.samequantum (decarg, decarg)

Returns the decimal number 1 when the exponents of the arguments are 
equal, or if they are both Infinities or they are both NaNs; in all 
other cases returns the decimal number 0. This function is used to 
test whether the exponents of two numbers are equal. The coefficients 
and signs of the arguments are ignored.

Uses the C library function C<decNumberSameQuantum()>.



































=head2 C<decnum:squareroot>

    decnum:squareroot ()
    decNumber.squareroot (decarg)

Returns a decimal number that is the square root of its argument, 
rounded if necessary using the digits setting in the decimal context 
................................................................................

Note that by binding the method C<__sub> to this function, the Lua 
subtraction operator (C<->) may be used with a C<decnum> on the left 
and a C<decarg> on the right.

Uses the C library function C<decNumberSubtract()>.

















=head2 C<decnum:tointegralvalue>

    decnum:tointegralvalue ()
    decNumber.tointegralvalue (decarg)

Returns a decimal number that is the argument with any fractional 
part removed, if necessary, using the rounding mode in the decimal 
context.





Uses the C library function C<decNumberToIntegralValue()>.

=head2 C<decnum:trim>

    decnum:trim ()
    decNumber.trim (decarg)
................................................................................
Returns a decimal number that is the argument with any insignificant 
trailing zeros removed. That is, if the number has any fractional 
trailing zeros they are removed by dividing the coefficient by the 
appropriate power of ten and adjusting the exponent accordingly.

Uses the C library function C<decNumberTrim()>.












B<Comparisons and Predicates>



































=head2 C<decnum:compare>

    decnum:compare (decarg)
    decNumber.compare (decarg, decarg)

Returns a decimal number that is the comparison of its arguments 
numerically. If the left (1st) argument is less than the right (2nd) 
................................................................................

    -NaN <  -sNaN < -Infinity < -finites < -0 < +0 < +finites < +Infinity < +sNaN < +NaN.

Also, C<1.000 < 1.0> (etc.) and NaNs are ordered by payload.

Uses the C library function C<decNumberCompareTotal()>.













=head2 C<decnum:eq>

    decnum:eq (decarg)
    decnum:__eq (decarg)
    decNumber.eq (<decarg>, <decarg>)

Returns a boolean that is true when the arguments are equal, false 
................................................................................
Note that by binding the method C<__eq> to this function, the Lua 
equality operators (C<==> and C<~=>) may be used with a C<decnum> 
on the left and a C<decnum> on the right.

Uses the C library functions C<decNumberCompare()> and 
C<decNumberIsZero()>.












=head2 C<decnum:iszero>

    decnum:iszero
    decNumber.iszero (decarg)

Returns a boolean that is true if the argument is zero, false 
otherwise



Uses the C library function C<decNumberIsZero()>.

=head2 C<decnum:isnegative>

    decnum:isnegative ()
    decNumber.isnegative (decarg)

Returns a boolean that is true if the argument is negative, false 
otherwise

Uses the C library function C<decNumberIsNegative()>.

=head2 C<decnum:isnan>

    decnum:isnan ()
    decNumber.isnan (decarg)

Returns a boolean that is true if the argument is a NaN (quiet or 
signaling), false otherwise

Uses the C library function C<decNumberIsNaN()>.






















=head2 C<decnum:isqnan>

    decnum:isqnan ()
    decNumber.isqnan (decarg)

Returns a boolean that is true if the argument is a quiet NaN, 
false otherwise

Uses the C library function C<decNumberIsQNaN()>.

=head2 C<decnum:issnan>

    decnum:issnan ()
    decNumber.issnan (decarg)

Returns a boolean that is true if the argument is a signaling NaN, 
false otherwise

Uses the C library function C<decNumberIsSNaN()>.























=head2 C<decnum:isinfinite>

    decnum:isinfinite ()
    decNumber.isinfinite (decarg)

Returns a boolean that is true if the argument is infinite, 
false otherwise.

Uses the C library function C<decNumberIsInfinite()>.

=head2 C<decnum:le>

    decnum:le (decarg)
    decnum:__le (decarg)
    decNumber.le (<decarg>, <decarg>)

................................................................................
that C<a E<lt>= b> is equivalent to C<not (b E<lt> a)> which in the 
presence of NaNs may or may not be what you want - if not, use 
L<C<decnum:compare>|/decnum:compare> directly.

Uses the C library functions C<decNumberCompare()> and 
C<decNumberIsNegative()>.











=head1 Operations on Random States

The following functions operate on random states.

The random number generator in the B<ldecNumber> package is based 
on a lagged Fibonacci generator ("LFIB4"). George Marsaglia has this 
to say about LFIB4:
................................................................................
number of decimal digits in the new random decimal number; the default 
is 12. If supplied, C<exponent> is the exponent of the new random 
decimal number; the default is C<-digits> so the new random decimal 
number is between zero (inclusive) and one (exclusive).

=head1 VERSION

This is B<ldecNumber> version 19.

=head1 CREDITS

B<ldecNumber> was developed by Doug Currie, Londonderry, NH, USA.

B<decNumber> was developed by Mike Cowlishaw at IBM.








>
>
>
>
>













|







 







|


|
|

|



|


|
|
|







 







>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









>
>
>
>
>
>
>
>
>
>
>
>









>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
|

|
|

|
<
>
>

|

|

|
|

|
|

|







|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






|









|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

|
|

|
|

|







 







>
>
>
>
>
>
>
>
>
>







 







|







134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
...
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
...
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
...
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
...
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
...
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
...
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
....
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
....
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
....
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
....
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
....
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412

1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
....
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
....
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
retaining the spelling, and 

=item *

retaining the case for constant names

=back

There are two cases where in following these rules the B<decNumber> 
names clash with Lua reserved words C<or> and C<and> -- in these 
cases the B<decNumber> functions are called "logical" operations,
so I called the functions C<lor> and C<land>.

Wherever there was a predefined Lua metamethod, e. g., C<__add>, the 
appropriate function is bound to that name as well as the B<decNumber> 
package function name.

Where it seemed appropriate, functions are provided both as methods 
on decimal numbers, as well as functions in the C<decNumber> module.

=head2 Mutability

The decimal numbers created by Lua are not mutable. This decision 
was based on my judgment that the potential performance benefit, 
mainly lower memory consumption and less garbage collection, was 
outweighed by the safety and lack of "surprise" that immutability 
provides. This makes decimal numbers compatible with Lua numbers 
and strings, both of which are also not mutable.

=head2 Conversion

All the functions in the C<decNumber> module automatically convert 
their arguments from Lua numbers or strings as necessary to perform 
................................................................................
is included, but has been designed for WindowsXP.

=head2 Compliance Test

File: I<ldecNumberTestDriver.lua>

This is the big test. It uses dectest sources from IBM, and has 
over 60,000 test cases. The Lua file is a driver to execute the tests 
specified by dectest.

As of version 21 of the Lua C<decNumber> module, the results for this 
test (dectest version 2.55) are: 

C<For all 60937 tests, 59951 succeeded, 5 failed, 301 failed(conv), 644 skipped(#), 36 skipped(prec).>

This is as good as possible with the default configuration. What this means is:

	59951 succeeded	woot!
	5 failed	these 5 tests are know to fail in the decNumber C library;
             these edge cases are under reconsideration in the Decimal Number Specification
	301 failed(conv)	 the precision required for the operands is insufficient in the Lua wrapper
	644 skipped(#)	 the test is for NULL arguments or format conversions not supported
	36 skipped(prec) the test called for a  precision larger than provided in the Lua wrapper

=head1 REFERENCE

Here is a reference to the data types, constants, and functions in the 
C<decNumber> module. Many of these will refer to the decNumber C library 
User's Guide, I<decNumber.pdf>, for implementation details.

................................................................................
    decNumber.ROUND_CEILING     round towards +infinity
    decNumber.ROUND_UP          round away from 0
    decNumber.ROUND_HALF_UP     0.5 rounds up
    decNumber.ROUND_HALF_EVEN   0.5 rounds to nearest even
    decNumber.ROUND_HALF_DOWN   0.5 rounds down
    decNumber.ROUND_DOWN        round towards 0 (truncate)
    decNumber.ROUND_FLOOR       round towards -infinity
    decNumber.ROUND_05UP        round for reround

=head1 Status Flags

These numeric status flags are used with 
L<C<decctx:getstatus()>|/decctx:getstatus>

    decNumber.Conversion_syntax
................................................................................
    decNumber.IEEE_854_Invalid_operation
    decNumber.IEEE_854_Overflow
    decNumber.IEEE_854_Underflow
    decNumber.Errors       normally errors (results are qNaN, infinite, or 0)
    decNumber.NaNs         cause a result to become qNaN
    decNumber.Information  normally for information only (have finite results)

=head1 Classifications

These numeric classifications for decNumbers are aligned with IEEE 754r 
and are returned by 
L<C<decnum:class()>|/decnum:class> 
Note that 'normal' and 'subnormal' are meaningful only with a decContext.

    decNumber.CLASS_SNAN
    decNumber.CLASS_QNAN
    decNumber.CLASS_NEG_INF
    decNumber.CLASS_NEG_NORMAL
    decNumber.CLASS_NEG_SUBNORMAL
    decNumber.CLASS_NEG_ZERO
    decNumber.CLASS_POS_ZERO
    decNumber.CLASS_POS_SUBNORMAL
    decNumber.CLASS_POS_NORMAL
    decNumber.CLASS_POS_INF

These classifications are also returned as string values from 
L<C<decnum:classasstring()>|/decnum:classasstring>

=head1 Initialization Descriptors

These constants are used with 
L<C<decctx:setdefault(x)>|/decctx:setdefault>

    decNumber.INIT_BASE
    decNumber.INIT_DECIMAL32
................................................................................

Note that by binding the method C<__add> to this function, the 
Lua addition operator (C<+>) may be used with a C<decnum> on the 
left and a C<decarg> on the right.

Uses the C library function C<decNumberAdd()>.

=head2 C<decnum:copy>

    decnum:copy ()
    decNumber.copy (decarg)

Returns a decimal number that is a copy of its argument. This is 
not too useful since C<decnum>s are immutable in B<ldecNumber>,
but it could be used as an alternative to 
L<C<decnum:tonumber>|/decnum:tonumber>
No error is possible from this function when its argument is a 
C<decnum>.

Uses the C library function C<decNumberCopy()>.

=head2 C<decnum:copyabs>

    decnum:copyabs ()
    decNumber.copyabs (decarg)

Returns a decimal number that is the absolute value of its argument.
This is the quiet C<abs> function described in IEEE 754r.
No error is possible from this function when its argument is a 
C<decnum>.

Uses the C library function C<decNumberCopyAbs()>.

=head2 C<decnum:copynegate>

    decnum:copynegate ()
    decNumber.copynegate (decarg)

Returns a decimal number that is the negation of its argument, in
other words it returns a copy of its argument with the sign inverted.
This is the quiet C<negate> function described in IEEE 754r.
No error is possible from this function when its argument is a 
C<decnum>.

Uses the C library function C<decNumberCopyNegate()>.

=head2 C<decnum:copysign>

    decnum:copysign (decarg)
    decNumber.copysign (decarg, decarg)

Returns a decimal number that is a copy of its first argument but 
with the sign of its second argument.
This is the quiet C<copysign> function described in IEEE 754r.
No error is possible from this function when its arguments are 
both C<decnum>s.

Uses the C library function C<decNumberCopySign()>.

=head2 C<decnum:divide>

    decnum:divide (decarg)
    decnum:__div (decarg)
    decNumber.divide (decarg, decarg)

Returns a decimal number that is the left (1st) argument divided 
................................................................................
The current context's rounding mode is used. 
See L<C<decnum:mod>|/decnum:mod>.

Uses the C library function C<decNumberDivideInteger()>, and then 
C<decNumberMultiply()> followed by C<decNumberCompare() decNumberIsZero()> 
to check if the remainder is zero, and C<decNumberSubtract()>.

=head2 C<decnum:fma>

    decnum:fma (decarg,decarg)
    decNumber.fma (decarg, decarg, decarg)

Returns a decimal number that is the result of multiplying the first
argument by the second argument and then adding the third argument to 
that intermediate result. It is equivalent to a multiplication followed 
by an addition except that the intermediate result is not rounded and 
will not cause overflow or underflow. That is, only the final result 
is rounded and checked.

Uses the C library function C<decNumberFMA()>.

=head2 C<decnum:invert>

    decnum:invert ()
    decNumber.invert (decarg)

Returns a decimal number that is the result of the digit-wise logical 
inversion of the argument (a 0 digit becomes 1 and vice versa).

Uses the C library function C<decNumberInvert()>.

=head2 C<decnum:land>

    decnum:land (decarg)
    decNumber.land (decarg, decarg)

Returns a decimal number that is the digit-wise logical and of the 
arguments. Note that all digits of the arguments must be 0 or 1 or
else this operation returns NaN,

Uses the C library function C<decNumberAnd()>.

=head2 C<decnum:ln>

    decnum:ln ()
    decNumber.ln (decarg)

Returns a decimal number that is the natural logarithm (logarithm in 
base e) of the argument.
................................................................................
    decNumber.log10 (decarg)

Returns a decimal number that is the logarithm in base ten of the 
argument.

Uses the C library function C<decNumberLog10()>.

=head2 C<decnum:logb>

    decnum:logb ()
    decNumber.logb (decarg)

Returns a decimal number that is the adjusted exponent of the 
argument, according to the rules for the C<logB> operation of the 
IEEE 754r proposal. This returns the exponent of the argument as 
though its decimal point had been moved to follow the first digit 
while keeping the same value. The result is not limited by 
C<emin> or C<emax>.

Uses the C library function C<decNumberLogB()>.

=head2 C<decnum:lor>

    decnum:lor (decarg)
    decNumber.lor (decarg, decarg)

Returns a decimal number that is the digit-wise logical inclusive or 
of the arguments. Note that all digits of the arguments must be 0 or 
1 or else this operation returns NaN,

Uses the C library function C<decNumberOr()>.

=head2 C<decnum:max>

    decnum:max (decarg)
    decNumber.max (decarg, decarg)

Returns a decimal number that is the maximum of its arguments.

Uses the C library function C<decNumberMax()>.

=head2 C<decnum:maxmag>

    decnum:maxmag (decarg)
    decNumber.maxmag (decarg, decarg)

Returns a decimal number that is the one of its arguments that 
has the maximum magnitude. It is identical to 
L<C<decnum:max>|/decnum:max> except that the signs of the operands 
are ignored and taken to be 0 (non-negative).

Uses the C library function C<decNumberMaxMag()>.

=head2 C<decnum:min>

    decnum:min (decarg)
    decNumber.min (decarg, decarg)

Returns a decimal number that is the minimum of its arguments.

Uses the C library function C<decNumberMin()>.

=head2 C<decnum:minmag>

    decnum:minmag (decarg)
    decNumber.minmag (decarg, decarg)

Returns a decimal number that is the one of its arguments that 
has the minimum magnitude. It is identical to 
L<C<decnum:min>|/decnum:min> except that the signs of the operands 
are ignored and taken to be 0 (non-negative).

Uses the C library function C<decNumberMinMag()>.

=head2 C<decnum:minus>

    decnum:minus ()
    decnum:__unm ()
    decNumber.minus (decarg)

Returns a decimal number that is the result of subtracting 
................................................................................

Note that by binding the method C<__mul> to this function, the Lua 
multiplication operator (C<*>) may be used with a C<decnum> on the 
left and a C<decarg> on the right.

Uses the C library function C<decNumberMultiply()>.

=head2 C<decnum:nextminus>

    decnum:nextminus ()
    decNumber.nextminus (decarg)

Returns a decimal number that is the closest value to the argument 
in the direction of -Infinity. This is computed as though by 
subtracting an infinitesimal amount from the argument 
using C<ROUND_FLOOR>, except that no flags are set as long as the 
argument is a C<decnum> (unless the argument is a signaling NaN).

This function is a generalization of the IEEE 754 C<nextDown> 
operation.

Uses the C library function C<decNumberNextMinus()>.

=head2 C<decnum:nextplus>

    decnum:nextplus ()
    decNumber.nextplus (decarg)

Returns a decimal number that is the closest value to the argument 
in the direction of +Infinity. This is computed as though by 
adding an infinitesimal amount from the argument 
using C<ROUND_CEILING>, except that no flags are set as long as the 
argument is a C<decnum> (unless the argument is a signaling NaN).

This function is a generalization of the IEEE 754 C<nextUp> 
operation.

Uses the C library function C<decNumberNextPlus()>.

=head2 C<decnum:nexttoward>

    decnum:nexttoward (decarg)
    decNumber.nexttoward (decarg, decarg)

Returns a decimal number that is the closest value to the first 
argument in the direction of the second argument. This is computed 
as though by adding or subtracting an infinitesimal amount to the 
first argument using either C<ROUND_CEILING> or C<ROUND_FLOOR>
depending on whether the second argument is larger or smaller than
the first argument. If the arguments are numerically equal, then 
the result is a copy of the first argument with the sign of the 
second argument. Flags are set as usual for an addition or 
subtraction (no flags are set in the equals case). 

This function is a generalization of the IEEE 754 C<nextAfter> 
operation.

Uses the C library function C<decNumberNextToward()>.

=head2 C<decnum:normalize>

    decnum:normalize ()
    decNumber.normalize (decarg)

Returns a decimal number that is the result of adding the argument 
to 0, and putting the result in its simplest form. That is, a non-zero 
number which has any trailing zeros in the coefficient has those zeros 
................................................................................
has the value of the right (2nd) argument. See 
L<C<decnum:quantize>|/decnum:quantize>. 
The right (2nd) argument must be a whole number (before any rounding); 
that is, any digits in the fractional part of the number must be zero.

Uses the C library function C<decNumberRescale()>.

=head2 C<decnum:rotate>

    decnum:rotate (decarg)
    decNumber.rotate (decarg, decarg)

Returns a decimal number that is the first argument with the digits 
of its coefficient rotated to the left (if the second argument is 
positive) or to the right (if the second argument is negative) 
without adjusting the exponent or the sign.

If the first argument has fewer digits than context C<digits> the 
coefficient is padded with zeros on the left before the
rotate. Any leading zeros in the result are ignored, as usual.

The second argument is the count of digits to rotate; it must be an 
integer (that is, it must have an exponent of 0) and must be in the 
range C<-digits> through C<+digits> in the current context.

Uses the C library function C<decNumberRotate()>.

=head2 C<decnum:samequantum>

    decnum:samequantum (decarg)
    decNumber.samequantum (decarg, decarg)

Returns the decimal number 1 when the exponents of the arguments are 
equal, or if they are both Infinities or they are both NaNs; in all 
other cases returns the decimal number 0. This function is used to 
test whether the exponents of two numbers are equal. The coefficients 
and signs of the arguments are ignored.

Uses the C library function C<decNumberSameQuantum()>.

=head2 C<decnum:scaleb>

    decnum:scaleb (decarg)
    decNumber.scaleb (decarg, decarg)

This function returns the result of multiplying the first argument by
ten raised to the power of the second argument. It is used to adjust 
(scale) the exponent of a number, using the rules of the C<scaleB> 
operation in the IEEE 754r proposal. The second argument must be an 
integer (that is, it must have an exponent of 0) and it must also be 
in the range C<-n> through C<+n>, where C<n> is 
C<2 * (context.emax + context.digits)>.

Uses the C library function C<decNumberScaleB()>.

=head2 C<decnum:shift>

    decnum:shift (decarg)
    decNumber.shift (decarg, decarg)

Returns a decimal number that is the first argument with the digits 
of its coefficient shifted to the left (if the second argument is 
positive) or to the right (if the second argument is negative) 
without adjusting the exponent or the sign.

The coefficient is padded with zeros on the left or right, as 
necessary. Any leading zeros in the result are ignored, as usual.

The second argument is the count of digits to shift; it must be an 
integer (that is, it must have an exponent of 0) and must be in the 
range C<-digits> through C<+digits> in the current context.

Uses the C library function C<decNumberShift()>.

=head2 C<decnum:squareroot>

    decnum:squareroot ()
    decNumber.squareroot (decarg)

Returns a decimal number that is the square root of its argument, 
rounded if necessary using the digits setting in the decimal context 
................................................................................

Note that by binding the method C<__sub> to this function, the Lua 
subtraction operator (C<->) may be used with a C<decnum> on the left 
and a C<decarg> on the right.

Uses the C library function C<decNumberSubtract()>.

=head2 C<decnum:tointegralexact>

    decnum:tointegralexact ()
    decNumber.tointegralexact (decarg)

Returns a decimal number that is the argument with any fractional 
part removed, if necessary, using the rounding mode in the decimal 
context.

The C<Inexact> flag is set if the result is numerically different 
from the argument. Other than that, no flags are set as long as the 
argument is a C<decnum> (unless the argument is a signaling NaN).
The result may have a positive exponent.

Uses the C library function C<decNumberToIntegralExact()>.

=head2 C<decnum:tointegralvalue>

    decnum:tointegralvalue ()
    decNumber.tointegralvalue (decarg)

Returns a decimal number that is the argument with any fractional 
part removed, if necessary, using the rounding mode in the decimal 
context.

No flags, not even C<Inexact>, are set as long as the 
argument is a C<decnum> (unless the argument is a signaling NaN).
The result may have a positive exponent.

Uses the C library function C<decNumberToIntegralValue()>.

=head2 C<decnum:trim>

    decnum:trim ()
    decNumber.trim (decarg)
................................................................................
Returns a decimal number that is the argument with any insignificant 
trailing zeros removed. That is, if the number has any fractional 
trailing zeros they are removed by dividing the coefficient by the 
appropriate power of ten and adjusting the exponent accordingly.

Uses the C library function C<decNumberTrim()>.

=head2 C<decnum:xor>

    decnum:xor (decarg)
    decNumber.xor (decarg, decarg)

Returns a decimal number that is the digit-wise logical exclusive or 
of the arguments. Note that all digits of the arguments must be 0 or 
1 or else this operation returns NaN,

Uses the C library function C<decNumberXor()>.

B<Comparisons and Predicates>

=head2 C<decnum:class>

    decnum:class ()
    decNumber.class (decarg)

Returns the class of a decNumber. No error is possible. The class is
one of the decNumber L</Classifications>.

Uses the C library function C<decNumberClass()>.

=head2 C<decnum:classasstring>

    decnum:classasstring ()
    decNumber.classasstring (decarg)

Returns the class of a decNumber as a string. No error is 
possible. The class is one of "-Infinity", "-Normal",  
"-Subnormal", "-Zero", "+Zero", "+Subnormal",
"+Normal", "+Infinity", "NaN", "sNaN", or "Invalid"

Uses the C library functions C<decNumberClass()> and 
C<decNumberClassToString()>.

=head2 C<classtostring>

    decNumber.classtostring (enum)

Converts the L</Classifications> of a decNumber to a string. 
No error is possible. The class is one of "-Infinity", 
"-Normal", "-Subnormal", "-Zero", "+Zero", "+Subnormal",
"+Normal", "+Infinity", "NaN", "sNaN", or "Invalid".

Uses the C library function C<decNumberClassToString()>.

=head2 C<decnum:compare>

    decnum:compare (decarg)
    decNumber.compare (decarg, decarg)

Returns a decimal number that is the comparison of its arguments 
numerically. If the left (1st) argument is less than the right (2nd) 
................................................................................

    -NaN <  -sNaN < -Infinity < -finites < -0 < +0 < +finites < +Infinity < +sNaN < +NaN.

Also, C<1.000 < 1.0> (etc.) and NaNs are ordered by payload.

Uses the C library function C<decNumberCompareTotal()>.

=head2 C<decnum:comparetotalmag>

    decnum:comparetotalmag (decarg)
    decNumber.comparetotalmag (decarg, decarg)

Returns a decimal number that is the comparison of the magnitude of 
its arguments using the IEEE 754r proposed ordering. It is identical 
to L<C<decnum:comparetotal>|/decnum:comparetotal> above except that 
the signs of the operands are ignored and taken to be 0 (non-negative).

Uses the C library function C<decNumberCompareTotalMag()>.

=head2 C<decnum:eq>

    decnum:eq (decarg)
    decnum:__eq (decarg)
    decNumber.eq (<decarg>, <decarg>)

Returns a boolean that is true when the arguments are equal, false 
................................................................................
Note that by binding the method C<__eq> to this function, the Lua 
equality operators (C<==> and C<~=>) may be used with a C<decnum> 
on the left and a C<decnum> on the right.

Uses the C library functions C<decNumberCompare()> and 
C<decNumberIsZero()>.

=head2 C<decnum:iscanonical>

    decnum:iscanonical ()
    decNumber.iscanonical (decarg)

Returns true always, because decNumbers always have canonical 
encodings (the function is provided for compatibility with the
IEEE 754r operation C<isCanonical>). No error is possible.

Uses the C library function C<decNumberIsCanonical()>.

=head2 C<decnum:isfinite>

    decnum:isfinite ()
    decNumber.isfinite (decarg)

Returns a boolean that is true if the argument is finite, false 

otherwise (that is, the argument is an infinity or a NaN).
No error is possible.

Uses the C library function C<decNumberIsFinite()>.

=head2 C<decnum:isinfinite>

    decnum:isinfinite ()
    decNumber.isinfinite (decarg)

Returns a boolean that is true if the argument is infinite, 
false otherwise. No error is possible.

Uses the C library function C<decNumberIsInfinite()>.

=head2 C<decnum:isnan>

    decnum:isnan ()
    decNumber.isnan (decarg)

Returns a boolean that is true if the argument is a NaN (quiet or 
signaling), false otherwise. No error is possible.

Uses the C library function C<decNumberIsNaN()>.

=head2 C<decnum:isnegative>

    decnum:isnegative ()
    decNumber.isnegative (decarg)

Returns a boolean that is true if the argument is is normal (that 
is, finite, non-zero, and not subnormal), false otherwise.
No error is possible.
 
Uses the C library function C<decNumberIsNegative()>.

=head2 C<decnum:isnormal>

    decnum:isnormal ()
    decNumber.isnormal (decarg)

Returns a boolean that is true if the argument is negative, false 
otherwise.  No error is possible.

Uses the C library function C<decNumberIsNormal()>.

=head2 C<decnum:isqnan>

    decnum:isqnan ()
    decNumber.isqnan (decarg)

Returns a boolean that is true if the argument is a quiet NaN, 
false otherwise. No error is possible.

Uses the C library function C<decNumberIsQNaN()>.

=head2 C<decnum:issnan>

    decnum:issnan ()
    decNumber.issnan (decarg)

Returns a boolean that is true if the argument is a signaling NaN, 
false otherwise. No error is possible.

Uses the C library function C<decNumberIsSNaN()>.

=head2 C<decnum:isspecial>

    decnum:isspecial ()
    decNumber.isspecial (decarg)

Returns a boolean that is true if the argument has a special 
value (Infinity or NaN), false otherwise; it is the inversion of 
L<C<decnum:isfinite>|/decnum:isfinite>. No error is possible.

Uses the C library function C<decNumberIsSpecial()>.

=head2 C<decnum:issubnormal>

    decnum:issubnormal ()
    decNumber.issubnormal (decarg)

Returns a boolean that is true if the argument is subnormal (that 
is, finite, non-zero, and not in the range of normal values), false 
otherwise. No error is possible.

Uses the C library function C<decNumberIsSubnormal()>.

=head2 C<decnum:iszero>

    decnum:iszero
    decNumber.iszero (decarg)

Returns a boolean that is true if the argument is zero, false 
otherwise. No error is possible.

Uses the C library function C<decNumberIsZero()>.

=head2 C<decnum:le>

    decnum:le (decarg)
    decnum:__le (decarg)
    decNumber.le (<decarg>, <decarg>)

................................................................................
that C<a E<lt>= b> is equivalent to C<not (b E<lt> a)> which in the 
presence of NaNs may or may not be what you want - if not, use 
L<C<decnum:compare>|/decnum:compare> directly.

Uses the C library functions C<decNumberCompare()> and 
C<decNumberIsNegative()>.

=head2 C<decnum:radix>

    decnum:radix ()
    decNumber.radix (decarg)

Returns the radix (number base) used by the decNumber package. This
always returns 10. No error is possible..

Uses the C library function C<decNumberRadix()>.

=head1 Operations on Random States

The following functions operate on random states.

The random number generator in the B<ldecNumber> package is based 
on a lagged Fibonacci generator ("LFIB4"). George Marsaglia has this 
to say about LFIB4:
................................................................................
number of decimal digits in the new random decimal number; the default 
is 12. If supplied, C<exponent> is the exponent of the new random 
decimal number; the default is C<-digits> so the new random decimal 
number is between zero (inclusive) and one (exclusive).

=head1 VERSION

This is B<ldecNumber> version 21.

=head1 CREDITS

B<ldecNumber> was developed by Doug Currie, Londonderry, NH, USA.

B<decNumber> was developed by Mike Cowlishaw at IBM.

Changes to ldecNumber.c.

310
311
312
313
314
315
316





317
318
319
320
321
322
323
...
337
338
339
340
341
342
343






















344
345
346
347
348
349
350
...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414




























415
416
417
418
419
420
421

422
423
424
425
426
427
428
429
430
431















432
433
434
435
436
437
438
...
443
444
445
446
447
448
449








































450
451
452
453
454
455
456
...
747
748
749
750
751
752
753

754
755
756
757
758
759
760
...
778
779
780
781
782
783
784












785
786
787
788
789
790
791
...
807
808
809
810
811
812
813










814
815
816
817
818
819
820
...
824
825
826
827
828
829
830
831













832
833
834
835
836
837
838
839
840





841





842
843
844
845
846
847
848
...
901
902
903
904
905
906
907










908
909
910
911
912
913
914
...
919
920
921
922
923
924
925













926
927
928
929
930
931
932
933
934










935
936
937
938
939
940
941
DN_OP1(dn_log10,  decNumberLog10)
DN_OP1(dn_abs,    decNumberAbs)
DN_OP1(dn_neg,    decNumberMinus)
DN_OP1(dn_norm,   decNumberNormalize)
DN_OP1(dn_plus,   decNumberPlus)
DN_OP1(dn_sqrt,   decNumberSquareRoot)
DN_OP1(dn_intval, decNumberToIntegralValue)






#define DN_OP2(name,fun) \
static int name (lua_State *L) \
{ \
    decContext *dc = ldn_get_context (L); \
    decNumber *dn1 = ldn_get (L, dc, 1); \
    decNumber *dn2 = ldn_get (L, dc, 2); \
................................................................................
DN_OP2(dn_divideinteger, decNumberDivideInteger)
DN_OP2(dn_max,           decNumberMax)
DN_OP2(dn_min,           decNumberMin)
DN_OP2(dn_quantize,      decNumberQuantize)
DN_OP2(dn_remainder,     decNumberRemainder)
DN_OP2(dn_remaindernear, decNumberRemainderNear)
DN_OP2(dn_rescale,       decNumberRescale)























/* mod -- needs to be fudged from remainder */

static int dn_mod (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
................................................................................
            // subtract one from result
            decNumberSubtract (dnr, dnr, &dnc_one, dc);
        }
    }
    return 1;
}

/* samequantum -- call needs no context */

static int dn_samequantum (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
    decNumber *dn2 = ldn_get (L, dc, 2);
    decNumber *dnr = ldn_make_decNumber (L);
    decNumberSameQuantum(dnr, dn1, dn2);
    return 1;
}

/* trim -- needs decNumberCopy */

static int dn_trim (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
    decNumber *dnr = ldn_make_decNumber (L);
    decNumberCopy (dnr, dn1);
    decNumberTrim (dnr);
    return 1;
}





























/* predicates */

#define DN_P1(name,pmac) \
static int name (lua_State *L) \
{ \
    decContext *dc = ldn_get_context (L); \
    decNumber *dn1 = ldn_get (L, dc, 1); \

    lua_pushboolean (L, pmac(dn1)); \
    return 1; \
}

DN_P1(dn_iszero, decNumberIsZero)
DN_P1(dn_isneg,  decNumberIsNegative)
DN_P1(dn_isnan,  decNumberIsNaN)
DN_P1(dn_isqnan, decNumberIsQNaN)
DN_P1(dn_issnan, decNumberIsSNaN)
DN_P1(dn_isinf,  decNumberIsInfinite)
















#define DN_PR2(name,fun,pmac) \
static int name (lua_State *L) \
{ \
    decNumber dnr; \
    decContext *dc = ldn_get_context (L); \
    decNumber *dn1 = ldn_get (L, dc, 1); \
................................................................................
}

#define decNumberIsNegativeOrZero(d) (decNumberIsNegative(d) || decNumberIsZero(d))

DN_PR2(dn_eq,decNumberCompare,decNumberIsZero)
DN_PR2(dn_lt,decNumberCompare,decNumberIsNegative)
DN_PR2(dn_le,decNumberCompare,decNumberIsNegativeOrZero)









































/* to string */

static int ldn_string (lua_State *L, int x, char *(*sf)(const decNumber *, char *))
{
    char buf[128];
    decContext *dc = ldn_get_context (L);
................................................................................
    DEC_(ROUND_CEILING)             /* round towards +infinity */
    DEC_(ROUND_UP)                  /* round away from 0 */
    DEC_(ROUND_HALF_UP)             /* 0.5 rounds up */
    DEC_(ROUND_HALF_EVEN)           /* 0.5 rounds to nearest even */
    DEC_(ROUND_HALF_DOWN)           /* 0.5 rounds down */
    DEC_(ROUND_DOWN)                /* round towards 0 (truncate) */
    DEC_(ROUND_FLOOR)               /* round towards -infinity */

    /* Trap-enabler and Status flags */
    DEC_(Conversion_syntax)
    DEC_(Division_by_zero)
    DEC_(Division_impossible)
    DEC_(Division_undefined)
    DEC_(Insufficient_storage)
    DEC_(Inexact)
................................................................................
    DEC_(NaNs)        /* flags which cause a result to become qNaN */
    DEC_(Information) /* flags which are normally for information only (have finite results) */
    /* Initialization descriptors, used by decContextDefault */
    DEC_(INIT_BASE)
    DEC_(INIT_DECIMAL32)
    DEC_(INIT_DECIMAL64)
    DEC_(INIT_DECIMAL128)












    /* compile time config */
    {"MAX_DIGITS", DECNUMDIGITS },
    /* terminator */
    { NULL, 0 }
};

#if LDN_ENABLE_RANDOM
................................................................................
    {"log10",           dn_log10  },
    {"abs",             dn_abs    },
    {"minus",           dn_neg    },
    {"normalize",       dn_norm   },
    {"plus",            dn_plus   },
    {"squareroot",      dn_sqrt   },
    {"tointegralvalue", dn_intval },











    {"add",             dn_add    },
    {"divide",          dn_div    },
    {"multiply",        dn_mul    },
    {"power",           dn_pow    },
    {"subtract",        dn_sub    },

................................................................................
    {"max",             dn_max           },
    {"min",             dn_min           },
    {"quantize",        dn_quantize      },
    {"remainder",       dn_remainder     },
    {"remaindernear",   dn_remaindernear },
    {"rescale",         dn_rescale       },
    {"samequantum",     dn_samequantum   },
    













    {"mod",             dn_mod           },
    {"floor",           dn_floor         },

    {"iszero",          dn_iszero        },
    {"isnegative",      dn_isneg         },
    {"isnan",           dn_isnan         },
    {"isqnan",          dn_isqnan        },
    {"issnan",          dn_issnan        },
    {"isinfinite",      dn_isinf         },





    





    {"trim",            dn_trim          },

    {"tostring",        dn_string        },
    {"toengstring",     dn_engstring     },

    { "__unm",      dn_neg    },
    { "__add",      dn_add    },
................................................................................
    {"log10",           dn_log10  },
    {"abs",             dn_abs    },
    {"minus",           dn_neg    },
    {"normalize",       dn_norm   },
    {"plus",            dn_plus   },
    {"squareroot",      dn_sqrt   },
    {"tointegralvalue", dn_intval },











    {"add",             dn_add    },
    {"divide",          dn_div    },
    {"multiply",        dn_mul    },
    {"power",           dn_pow    },
    {"subtract",        dn_sub    },

................................................................................
    {"min",             dn_min           },
    {"quantize",        dn_quantize      },
    {"remainder",       dn_remainder     },
    {"remaindernear",   dn_remaindernear },
    {"rescale",         dn_rescale       },
    {"samequantum",     dn_samequantum   },














    {"mod",             dn_mod           },
    {"floor",           dn_floor         },

    {"iszero",          dn_iszero        },
    {"isnegative",      dn_isneg         },
    {"isnan",           dn_isnan         },
    {"isqnan",          dn_isqnan        },
    {"issnan",          dn_issnan        },
    {"isinfinite",      dn_isinf         },










    
    {"trim",            dn_trim          },

    {"getcontext",      dn_get_context   },
    {"setcontext",      dn_set_context   },
    {"tonumber",        dn_todecnumber   },
    {"tostring",        dn_string        },







>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<
<












>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>







 







>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>









>
>
>
>
>
|
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>









>
>
>
>
>
>
>
>
>
>







310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
...
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
...
411
412
413
414
415
416
417












418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
...
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
...
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
...
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
...
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
...
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
....
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
....
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
DN_OP1(dn_log10,  decNumberLog10)
DN_OP1(dn_abs,    decNumberAbs)
DN_OP1(dn_neg,    decNumberMinus)
DN_OP1(dn_norm,   decNumberNormalize)
DN_OP1(dn_plus,   decNumberPlus)
DN_OP1(dn_sqrt,   decNumberSquareRoot)
DN_OP1(dn_intval, decNumberToIntegralValue)
DN_OP1(dn_invert, decNumberInvert)
DN_OP1(dn_logb,   decNumberLogB)
DN_OP1(dn_intxct, decNumberToIntegralExact)
DN_OP1(dn_intnmn, decNumberNextMinus)
DN_OP1(dn_intnpl, decNumberNextPlus)

#define DN_OP2(name,fun) \
static int name (lua_State *L) \
{ \
    decContext *dc = ldn_get_context (L); \
    decNumber *dn1 = ldn_get (L, dc, 1); \
    decNumber *dn2 = ldn_get (L, dc, 2); \
................................................................................
DN_OP2(dn_divideinteger, decNumberDivideInteger)
DN_OP2(dn_max,           decNumberMax)
DN_OP2(dn_min,           decNumberMin)
DN_OP2(dn_quantize,      decNumberQuantize)
DN_OP2(dn_remainder,     decNumberRemainder)
DN_OP2(dn_remaindernear, decNumberRemainderNear)
DN_OP2(dn_rescale,       decNumberRescale)

DN_OP2(dn_and,             decNumberAnd)
DN_OP2(dn_comparetotalmag, decNumberCompareTotalMag)
DN_OP2(dn_maxmag,          decNumberMaxMag)
DN_OP2(dn_minmag,          decNumberMinMag)
DN_OP2(dn_nexttoward,      decNumberNextToward)
DN_OP2(dn_or,              decNumberOr)
DN_OP2(dn_rotate,          decNumberRotate)
DN_OP2(dn_scaleb,          decNumberScaleB)
DN_OP2(dn_shift,           decNumberShift)
DN_OP2(dn_xor,             decNumberXor)

static int dn_fma (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
    decNumber *dn2 = ldn_get (L, dc, 2);
    decNumber *dn3 = ldn_get (L, dc, 3);
    decNumber *dnr = ldn_make_decNumber (L);
    decNumberFMA (dnr, dn1, dn2, dn3, dc);
    return 1;
}

/* mod -- needs to be fudged from remainder */

static int dn_mod (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
................................................................................
            // subtract one from result
            decNumberSubtract (dnr, dnr, &dnc_one, dc);
        }
    }
    return 1;
}













/* trim -- needs decNumberCopy */

static int dn_trim (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
    decNumber *dnr = ldn_make_decNumber (L);
    decNumberCopy (dnr, dn1);
    decNumberTrim (dnr);
    return 1;
}

#define DN_OP1nc(name,fun) \
static int name (lua_State *L) \
{ \
    decContext *dc = ldn_get_context (L); \
    decNumber *dn1 = ldn_get (L, dc, 1); \
    decNumber *dnr = ldn_make_decNumber (L); \
    fun(dnr, dn1); \
    return 1; \
}

DN_OP1nc(dn_copy, decNumberCopy)
DN_OP1nc(dn_copyabs, decNumberCopyAbs)
DN_OP1nc(dn_copynegate, decNumberCopyNegate)

#define DN_OP2nc(name,fun) \
static int name (lua_State *L) \
{ \
    decContext *dc = ldn_get_context (L); \
    decNumber *dn1 = ldn_get (L, dc, 1); \
    decNumber *dn2 = ldn_get (L, dc, 2); \
    decNumber *dnr = ldn_make_decNumber (L); \
    fun(dnr, dn1, dn2); \
    return 1; \
}

DN_OP2nc(dn_samequantum, decNumberSameQuantum)
DN_OP2nc(dn_copysign, decNumberCopySign)

/* predicates */

#define DN_P1(name,pmac) \
static int name (lua_State *L) \
{ \
    decContext *dc = ldn_get_context (L); \
    decNumber *dn1 = ldn_get (L, dc, 1); \
    dn1 = dn1; \
    lua_pushboolean (L, pmac(dn1)); \
    return 1; \
}

DN_P1(dn_iszero, decNumberIsZero)
DN_P1(dn_isneg,  decNumberIsNegative)
DN_P1(dn_isnan,  decNumberIsNaN)
DN_P1(dn_isqnan, decNumberIsQNaN)
DN_P1(dn_issnan, decNumberIsSNaN)
DN_P1(dn_isinf,  decNumberIsInfinite)
DN_P1(dn_iscncl, decNumberIsCanonical)
DN_P1(dn_isfini, decNumberIsFinite)
DN_P1(dn_isspec, decNumberIsSpecial)

#define DN_P1c(name,pmac) \
static int name (lua_State *L) \
{ \
    decContext *dc = ldn_get_context (L); \
    decNumber *dn1 = ldn_get (L, dc, 1); \
    lua_pushboolean (L, pmac(dn1,dc)); \
    return 1; \
}

DN_P1c(dn_isnorm, decNumberIsNormal)
DN_P1c(dn_issubn, decNumberIsSubnormal)

#define DN_PR2(name,fun,pmac) \
static int name (lua_State *L) \
{ \
    decNumber dnr; \
    decContext *dc = ldn_get_context (L); \
    decNumber *dn1 = ldn_get (L, dc, 1); \
................................................................................
}

#define decNumberIsNegativeOrZero(d) (decNumberIsNegative(d) || decNumberIsZero(d))

DN_PR2(dn_eq,decNumberCompare,decNumberIsZero)
DN_PR2(dn_lt,decNumberCompare,decNumberIsNegative)
DN_PR2(dn_le,decNumberCompare,decNumberIsNegativeOrZero)

/* classifiers */

static int dn_radix (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
    int r = decNumberRadix(dn1);
    dn1 = dn1;
    lua_pushinteger (L, r);
    return 1;
}

static int dn_class (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
    int decClass = decNumberClass(dn1,dc);
    lua_pushinteger (L, decClass);
    return 1;
}

static int dn_classtostring (lua_State *L)
{
    //decContext *dc = ldn_get_context (L);
    int decClass = luaL_checkint (L,1);
    const char * s = decNumberClassToString(decClass);
    lua_pushstring (L, s);
    return 1;
}

static int dn_classasstring (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
    int decClass = decNumberClass(dn1,dc);
    const char * s = decNumberClassToString(decClass);
    lua_pushstring (L, s);
    return 1;
}

/* to string */

static int ldn_string (lua_State *L, int x, char *(*sf)(const decNumber *, char *))
{
    char buf[128];
    decContext *dc = ldn_get_context (L);
................................................................................
    DEC_(ROUND_CEILING)             /* round towards +infinity */
    DEC_(ROUND_UP)                  /* round away from 0 */
    DEC_(ROUND_HALF_UP)             /* 0.5 rounds up */
    DEC_(ROUND_HALF_EVEN)           /* 0.5 rounds to nearest even */
    DEC_(ROUND_HALF_DOWN)           /* 0.5 rounds down */
    DEC_(ROUND_DOWN)                /* round towards 0 (truncate) */
    DEC_(ROUND_FLOOR)               /* round towards -infinity */
    DEC_(ROUND_05UP)                /* round for reround  */
    /* Trap-enabler and Status flags */
    DEC_(Conversion_syntax)
    DEC_(Division_by_zero)
    DEC_(Division_impossible)
    DEC_(Division_undefined)
    DEC_(Insufficient_storage)
    DEC_(Inexact)
................................................................................
    DEC_(NaNs)        /* flags which cause a result to become qNaN */
    DEC_(Information) /* flags which are normally for information only (have finite results) */
    /* Initialization descriptors, used by decContextDefault */
    DEC_(INIT_BASE)
    DEC_(INIT_DECIMAL32)
    DEC_(INIT_DECIMAL64)
    DEC_(INIT_DECIMAL128)
    /* Classifications for decNumbers, aligned with 754r (note that     */
    /* 'normal' and 'subnormal' are meaningful only with a decContext)  */
    DEC_(CLASS_SNAN)
    DEC_(CLASS_QNAN)
    DEC_(CLASS_NEG_INF)
    DEC_(CLASS_NEG_NORMAL)
    DEC_(CLASS_NEG_SUBNORMAL)
    DEC_(CLASS_NEG_ZERO)
    DEC_(CLASS_POS_ZERO)
    DEC_(CLASS_POS_SUBNORMAL)
    DEC_(CLASS_POS_NORMAL)
    DEC_(CLASS_POS_INF)
    /* compile time config */
    {"MAX_DIGITS", DECNUMDIGITS },
    /* terminator */
    { NULL, 0 }
};

#if LDN_ENABLE_RANDOM
................................................................................
    {"log10",           dn_log10  },
    {"abs",             dn_abs    },
    {"minus",           dn_neg    },
    {"normalize",       dn_norm   },
    {"plus",            dn_plus   },
    {"squareroot",      dn_sqrt   },
    {"tointegralvalue", dn_intval },
    {"invert",          dn_invert },
    {"logb",            dn_logb   },
    {"tointegralexact", dn_intxct },
    {"nextminus",       dn_intnmn },
    {"nextplus",        dn_intnpl },

    {"copy",            dn_copy       },
    {"copyabs",         dn_copyabs    },
    {"copynegate",      dn_copynegate },
    {"copysign",        dn_copysign   },

    {"add",             dn_add    },
    {"divide",          dn_div    },
    {"multiply",        dn_mul    },
    {"power",           dn_pow    },
    {"subtract",        dn_sub    },

................................................................................
    {"max",             dn_max           },
    {"min",             dn_min           },
    {"quantize",        dn_quantize      },
    {"remainder",       dn_remainder     },
    {"remaindernear",   dn_remaindernear },
    {"rescale",         dn_rescale       },
    {"samequantum",     dn_samequantum   },

    {"land",            dn_and           },
    {"comparetotalmag", dn_comparetotalmag },
    {"maxmag",          dn_maxmag        },
    {"minmag",          dn_minmag        },
    {"nexttoward",      dn_nexttoward    },
    {"lor",             dn_or            },
    {"rotate",          dn_rotate        },
    {"scaleb",          dn_scaleb        },
    {"shift",           dn_shift         },
    {"xor",             dn_xor           },
    
    {"fma",             dn_fma           },

    {"mod",             dn_mod           },
    {"floor",           dn_floor         },

    {"iszero",          dn_iszero        },
    {"isnegative",      dn_isneg         },
    {"isnan",           dn_isnan         },
    {"isqnan",          dn_isqnan        },
    {"issnan",          dn_issnan        },
    {"isinfinite",      dn_isinf         },
    {"isfinite",        dn_isfini        },
    {"iscanonical",     dn_iscncl        },
    {"isspecial",       dn_isspec        },
    {"isnormal",        dn_isnorm        },
    {"issubnormal",     dn_issubn        },

    {"radix",           dn_radix         },
    {"class",           dn_class         },
    {"classtostring",   dn_classtostring },
    {"classasstring",   dn_classasstring },

    {"trim",            dn_trim          },

    {"tostring",        dn_string        },
    {"toengstring",     dn_engstring     },

    { "__unm",      dn_neg    },
    { "__add",      dn_add    },
................................................................................
    {"log10",           dn_log10  },
    {"abs",             dn_abs    },
    {"minus",           dn_neg    },
    {"normalize",       dn_norm   },
    {"plus",            dn_plus   },
    {"squareroot",      dn_sqrt   },
    {"tointegralvalue", dn_intval },
    {"invert",          dn_invert },
    {"logb",            dn_logb   },
    {"tointegralexact", dn_intxct },
    {"nextminus",       dn_intnmn },
    {"nextplus",        dn_intnpl },

    {"copy",            dn_copy       },
    {"copyabs",         dn_copyabs    },
    {"copynegate",      dn_copynegate },
    {"copysign",        dn_copysign   },

    {"add",             dn_add    },
    {"divide",          dn_div    },
    {"multiply",        dn_mul    },
    {"power",           dn_pow    },
    {"subtract",        dn_sub    },

................................................................................
    {"min",             dn_min           },
    {"quantize",        dn_quantize      },
    {"remainder",       dn_remainder     },
    {"remaindernear",   dn_remaindernear },
    {"rescale",         dn_rescale       },
    {"samequantum",     dn_samequantum   },

    {"land",            dn_and           },
    {"comparetotalmag", dn_comparetotalmag },
    {"maxmag",          dn_maxmag        },
    {"minmag",          dn_minmag        },
    {"nexttoward",      dn_nexttoward    },
    {"lor",             dn_or            },
    {"rotate",          dn_rotate        },
    {"scaleb",          dn_scaleb        },
    {"shift",           dn_shift         },
    {"xor",             dn_xor           },

    {"fma",             dn_fma           },

    {"mod",             dn_mod           },
    {"floor",           dn_floor         },

    {"iszero",          dn_iszero        },
    {"isnegative",      dn_isneg         },
    {"isnan",           dn_isnan         },
    {"isqnan",          dn_isqnan        },
    {"issnan",          dn_issnan        },
    {"isinfinite",      dn_isinf         },
    {"isfinite",        dn_isfini        },
    {"iscanonical",     dn_iscncl        },
    {"isspecial",       dn_isspec        },
    {"isnormal",        dn_isnorm        },
    {"issubnormal",     dn_issubn        },

    {"radix",           dn_radix         },
    {"class",           dn_class         },
    {"classtostring",   dn_classtostring },
    {"classasstring",   dn_classasstring },
    
    {"trim",            dn_trim          },

    {"getcontext",      dn_get_context   },
    {"setcontext",      dn_set_context   },
    {"tonumber",        dn_todecnumber   },
    {"tostring",        dn_string        },

Changes to test/dectest/abs.decTest.

1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
------------------------------------------------------------------------
-- abs.decTest -- decimal absolute value                              --
-- Copyright (c) IBM Corporation, 1981, 2004.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.52

-- This set of tests primarily tests the existence of the operator.
-- Additon, subtraction, rounding, and more overflows are tested
-- elsewhere.

precision:   9
rounding:    half_up


|







 







|







1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
------------------------------------------------------------------------
-- abs.decTest -- decimal absolute value                              --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

-- This set of tests primarily tests the existence of the operator.
-- Additon, subtraction, rounding, and more overflows are tested
-- elsewhere.

precision:   9
rounding:    half_up

Changes to test/dectest/add.decTest.

1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
..
82
83
84
85
86
87
88

89
90
91
92
93
94
95
...
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
...
392
393
394
395
396
397
398

399
400
401
402
403
404
405
...
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
....
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
....
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
....
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
....
1661
1662
1663
1664
1665
1666
1667



1668
1669
1670
1671
1672
1673
1674
....
1913
1914
1915
1916
1917
1918
1919

1920
1921
1922
1923
1924
1925
1926
....
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
....
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
....
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602






























































































2603
2604
2605
------------------------------------------------------------------------
-- add.decTest -- decimal addition                                    --
-- Copyright (c) IBM Corporation, 1981, 2004.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.52

precision:   9
rounding:    half_up
maxExponent: 384
minexponent: -383
extended:    1

................................................................................
precision: 15
addx046 add '10000e+9'  '7' -> '10000000000007'
addx047 add '10000e+9'  '70' -> '10000000000070'
addx048 add '10000e+9'  '700' -> '10000000000700'
addx049 add '10000e+9'  '7000' -> '10000000007000'
addx050 add '10000e+9'  '70000' -> '10000000070000'
addx051 add '10000e+9'  '700000' -> '10000000700000'


-- examples from decarith
addx053 add '12' '7.00' -> '19.00'
addx054 add '1.3' '-1.07' -> '0.23'
addx055 add '1.3' '-1.30' -> '0.00'
addx056 add '1.3' '-2.07' -> '-0.77'
addx057 add '1E+2' '1E+4' -> '1.01E+4'
................................................................................
addx163 add '1.11'  '1E+12' -> '1000000000001.11'
addx164 add '-1'    '1E+12' -> '999999999999'
addx165 add '7E+12' '-1'    -> '6999999999999'
addx166 add '7E+12'  '1.11' -> '7000000000001.11'
addx167 add '1.11'  '7E+12' -> '7000000000001.11'
addx168 add '-1'    '7E+12' -> '6999999999999'

--            123456789012345      123456789012345      1 23456789012345
addx170 add '0.444444444444444'  '0.555555555555563' -> '1.00000000000001' Inexact Rounded
addx171 add '0.444444444444444'  '0.555555555555562' -> '1.00000000000001' Inexact Rounded
addx172 add '0.444444444444444'  '0.555555555555561' -> '1.00000000000001' Inexact Rounded
addx173 add '0.444444444444444'  '0.555555555555560' -> '1.00000000000000' Inexact Rounded
addx174 add '0.444444444444444'  '0.555555555555559' -> '1.00000000000000' Inexact Rounded
addx175 add '0.444444444444444'  '0.555555555555558' -> '1.00000000000000' Inexact Rounded
addx176 add '0.444444444444444'  '0.555555555555557' -> '1.00000000000000' Inexact Rounded
................................................................................
precision:   7
rounding:    half_up
maxExponent: 92
minexponent: -92
addx361  add 0E+50 10000E+1  -> 1.0000E+5
addx362  add 10000E+1 0E-50  -> 100000.0  Rounded
addx363  add 10000E+1 10000E-50  -> 100000.0  Rounded Inexact


-- a curiosity from JSR 13 testing
rounding:    half_down
precision:   10
addx370 add 99999999 81512 -> 100081511
precision:      6
addx371 add 99999999 81512 -> 1.00082E+8 Rounded Inexact
................................................................................
-- verify a query
precision:    16
maxExponent: +394
minExponent: -393
rounding:     down
addx561 add 1e-398 9.000000000000000E+384 -> 9.000000000000000E+384 Inexact Rounded
addx562 add      0 9.000000000000000E+384 -> 9.000000000000000E+384 Rounded
-- and using decimal64 bounds...
precision:    16
maxExponent: +384
minExponent: -383
rounding:     down
addx563 add 1e-388 9.000000000000000E+374 -> 9.000000000000000E+374 Inexact Rounded
addx564 add      0 9.000000000000000E+374 -> 9.000000000000000E+374 Rounded

................................................................................
addx574 subtract  1E-383  1E-384  ->   9E-384 Subnormal

-- Here we explore the boundary of rounding a subnormal to Nmin
addx575 subtract  1E-383  1E-398  ->   9.99999999999999E-384  Subnormal
addx576 subtract  1E-383  1E-398  ->   9.99999999999999E-384  Subnormal
addx577 subtract  1E-383  1E-399  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
addx578 subtract  1E-383  1E-400  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
--addx579 subtract  1E-383  1E-401  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
--addx580 subtract  1E-383  1E-402  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded

-- check overflow edge case
precision:   7
rounding:    half_up
maxExponent: 96
minExponent: -95
addx972 apply   9.999999E+96         -> 9.999999E+96
................................................................................
addx1469 add 1.123456789012345  0E-9   -> 1.123456789012345
addx1470 add 1.123456789012345  0E-10  -> 1.123456789012345
addx1471 add 1.123456789012345  0E-11  -> 1.123456789012345
addx1472 add 1.123456789012345  0E-12  -> 1.123456789012345
addx1473 add 1.123456789012345  0E-13  -> 1.123456789012345
addx1474 add 1.123456789012345  0E-14  -> 1.123456789012345
addx1475 add 1.123456789012345  0E-15  -> 1.123456789012345
-- next four flag Rounded becuase the 0 extends the result
addx1476 add 1.123456789012345  0E-16  -> 1.123456789012345 Rounded
addx1477 add 1.123456789012345  0E-17  -> 1.123456789012345 Rounded
addx1478 add 1.123456789012345  0E-18  -> 1.123456789012345 Rounded
addx1479 add 1.123456789012345  0E-19  -> 1.123456789012345 Rounded

-- sum of two opposite-sign operands is exactly 0 and floor => -0
precision:   16
................................................................................
addx1704  add 1E2       1E4       -> 1.01E+4
addx1705  subtract 130E-2  120E-2 -> 0.10
addx1706  subtract 130E-2  12E-1  -> 0.10
addx1707  subtract 130E-2  1E0    -> 0.30
addx1708  subtract 1E2     1E4    -> -9.9E+3

------------------------------------------------------------------------
-- Same as above, using decimal64default parameters                   --
------------------------------------------------------------------------
precision:   16
rounding:    half_even
maxExponent: 384
minexponent: -383

-- [first group are 'quick confidence check']
................................................................................
addx6063 add 1 '0.001' -> '1.001'
addx6064 add 1 '0.0001' -> '1.0001'
addx6065 add 1 '0.00001' -> '1.00001'
addx6066 add 1 '0.000001' -> '1.000001'
addx6067 add 1 '0.0000001' -> '1.0000001'
addx6068 add 1 '0.00000001' -> '1.00000001'




-- some funny zeros [in case of bad signum]
addx6070 add 1  0    -> 1
addx6071 add 1 0.    -> 1
addx6072 add 1  .0   -> 1.0
addx6073 add 1 0.0   -> 1.0
addx6074 add 1 0.00  -> 1.00
addx6075 add  0  1   -> 1
................................................................................
addx6350 add 1E+16   7    ->   1.000000000000001E+16  Inexact Rounded
addx6351 add 1E+17   7    ->   1.000000000000000E+17  Inexact Rounded

-- tryzeros cases
addx6361  add 0E+50 10000E+1  -> 1.0000E+5
addx6362  add 10000E+1 0E-50  -> 100000.0000000000  Rounded
addx6363  add 10000E+1 10000E-50  -> 100000.0000000000  Rounded Inexact


-- ulp replacement tests
addx6400 add   1   77e-14      ->  1.00000000000077
addx6401 add   1   77e-15      ->  1.000000000000077
addx6402 add   1   77e-16      ->  1.000000000000008 Inexact Rounded
addx6403 add   1   77e-17      ->  1.000000000000001 Inexact Rounded
addx6404 add   1   77e-18      ->  1.000000000000000 Inexact Rounded
................................................................................
addx6574 subtract  1E-383  1E-384  ->   9E-384 Subnormal

-- Here we explore the boundary of rounding a subnormal to Nmin
addx6575 subtract  1E-383  1E-398  ->   9.99999999999999E-384  Subnormal
addx6576 subtract  1E-383  1E-398  ->   9.99999999999999E-384  Subnormal
addx6577 subtract  1E-383  1E-399  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
addx6578 subtract  1E-383  1E-400  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
--addx6579 subtract  1E-383  1E-401  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
--addx6580 subtract  1E-383  1E-402  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded

-- check overflow edge case
--               1234567890123456
addx6972 apply   9.999999999999999E+384         -> 9.999999999999999E+384
addx6973 add     9.999999999999999E+384  1      -> 9.999999999999999E+384 Inexact Rounded
addx6974 add      9999999999999999E+369  1      -> 9.999999999999999E+384 Inexact Rounded
addx6975 add      9999999999999999E+369  1E+369  -> Infinity Overflow Inexact Rounded
................................................................................
addx61469 add 1.123456789012345  0E-9   -> 1.123456789012345
addx61470 add 1.123456789012345  0E-10  -> 1.123456789012345
addx61471 add 1.123456789012345  0E-11  -> 1.123456789012345
addx61472 add 1.123456789012345  0E-12  -> 1.123456789012345
addx61473 add 1.123456789012345  0E-13  -> 1.123456789012345
addx61474 add 1.123456789012345  0E-14  -> 1.123456789012345
addx61475 add 1.123456789012345  0E-15  -> 1.123456789012345
-- next four flag Rounded becuase the 0 extends the result
addx61476 add 1.123456789012345  0E-16  -> 1.123456789012345 Rounded
addx61477 add 1.123456789012345  0E-17  -> 1.123456789012345 Rounded
addx61478 add 1.123456789012345  0E-18  -> 1.123456789012345 Rounded
addx61479 add 1.123456789012345  0E-19  -> 1.123456789012345 Rounded

-- sum of two opposite-sign operands is exactly 0 and floor => -0
rounding:    half_up
................................................................................
-- some exact zeros from non-zeros
addx61635 add  1E-401   1E-401 ->  0E-398 Subnormal Inexact Rounded Underflow Clamped
addx61636 add -1E-401   1E-401 -> -0E-398 Clamped  -- *
addx61637 add  1E-401  -1E-401 -> -0E-398 Clamped  -- *
addx61638 add -1E-401  -1E-401 -> -1E-398 Subnormal Inexact Rounded Underflow

-- Examples from SQL proposal (Krishna Kulkarni)
addx61701  add 130E-2    120E-2    -> 2.50
addx61702  add 130E-2    12E-1     -> 2.50
addx61703  add 130E-2    1E0       -> 2.30
addx61704  add 1E2       1E4       -> 1.01E+4
addx61705  subtract 130E-2  120E-2 -> 0.10
addx61706  subtract 130E-2  12E-1  -> 0.10
addx61707  subtract 130E-2  1E0    -> 0.30
addx61708  subtract 1E2     1E4    -> -9.9E+3































































































-- Null tests
addx9990 add 10  # -> NaN Invalid_operation
addx9991 add  # 10 -> NaN Invalid_operation
|

|







 







|







 







>







 







|







 







>







 







|







 







|
|







 







|







 







|







 







>
>
>







 







>







 







|
|







 







|







 







|
|
|
|
|
|
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
..
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
...
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
....
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
....
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
....
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
....
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
....
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
....
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
....
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
....
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
------/cancell----------------------------------------------------------
-- add.decTest -- decimal addition                                    --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

precision:   9
rounding:    half_up
maxExponent: 384
minexponent: -383
extended:    1

................................................................................
precision: 15
addx046 add '10000e+9'  '7' -> '10000000000007'
addx047 add '10000e+9'  '70' -> '10000000000070'
addx048 add '10000e+9'  '700' -> '10000000000700'
addx049 add '10000e+9'  '7000' -> '10000000007000'
addx050 add '10000e+9'  '70000' -> '10000000070000'
addx051 add '10000e+9'  '700000' -> '10000000700000'
addx052 add '10000e+9'  '7000000' -> '10000007000000'

-- examples from decarith
addx053 add '12' '7.00' -> '19.00'
addx054 add '1.3' '-1.07' -> '0.23'
addx055 add '1.3' '-1.30' -> '0.00'
addx056 add '1.3' '-2.07' -> '-0.77'
addx057 add '1E+2' '1E+4' -> '1.01E+4'
................................................................................
addx163 add '1.11'  '1E+12' -> '1000000000001.11'
addx164 add '-1'    '1E+12' -> '999999999999'
addx165 add '7E+12' '-1'    -> '6999999999999'
addx166 add '7E+12'  '1.11' -> '7000000000001.11'
addx167 add '1.11'  '7E+12' -> '7000000000001.11'
addx168 add '-1'    '7E+12' -> '6999999999999'

--             123456789012345      123456789012345      1 23456789012345
addx170 add '0.444444444444444'  '0.555555555555563' -> '1.00000000000001' Inexact Rounded
addx171 add '0.444444444444444'  '0.555555555555562' -> '1.00000000000001' Inexact Rounded
addx172 add '0.444444444444444'  '0.555555555555561' -> '1.00000000000001' Inexact Rounded
addx173 add '0.444444444444444'  '0.555555555555560' -> '1.00000000000000' Inexact Rounded
addx174 add '0.444444444444444'  '0.555555555555559' -> '1.00000000000000' Inexact Rounded
addx175 add '0.444444444444444'  '0.555555555555558' -> '1.00000000000000' Inexact Rounded
addx176 add '0.444444444444444'  '0.555555555555557' -> '1.00000000000000' Inexact Rounded
................................................................................
precision:   7
rounding:    half_up
maxExponent: 92
minexponent: -92
addx361  add 0E+50 10000E+1  -> 1.0000E+5
addx362  add 10000E+1 0E-50  -> 100000.0  Rounded
addx363  add 10000E+1 10000E-50  -> 100000.0  Rounded Inexact
addx364  add 9.999999E+92 -9.999999E+92 -> 0E+86

-- a curiosity from JSR 13 testing
rounding:    half_down
precision:   10
addx370 add 99999999 81512 -> 100081511
precision:      6
addx371 add 99999999 81512 -> 1.00082E+8 Rounded Inexact
................................................................................
-- verify a query
precision:    16
maxExponent: +394
minExponent: -393
rounding:     down
addx561 add 1e-398 9.000000000000000E+384 -> 9.000000000000000E+384 Inexact Rounded
addx562 add      0 9.000000000000000E+384 -> 9.000000000000000E+384 Rounded
-- and using decimal64 bounds (see also ddadd.decTest)
precision:    16
maxExponent: +384
minExponent: -383
rounding:     down
addx563 add 1e-388 9.000000000000000E+374 -> 9.000000000000000E+374 Inexact Rounded
addx564 add      0 9.000000000000000E+374 -> 9.000000000000000E+374 Rounded

................................................................................
addx574 subtract  1E-383  1E-384  ->   9E-384 Subnormal

-- Here we explore the boundary of rounding a subnormal to Nmin
addx575 subtract  1E-383  1E-398  ->   9.99999999999999E-384  Subnormal
addx576 subtract  1E-383  1E-398  ->   9.99999999999999E-384  Subnormal
addx577 subtract  1E-383  1E-399  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
addx578 subtract  1E-383  1E-400  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
addx579 subtract  1E-383  1E-401  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
addx580 subtract  1E-383  1E-402  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded

-- check overflow edge case
precision:   7
rounding:    half_up
maxExponent: 96
minExponent: -95
addx972 apply   9.999999E+96         -> 9.999999E+96
................................................................................
addx1469 add 1.123456789012345  0E-9   -> 1.123456789012345
addx1470 add 1.123456789012345  0E-10  -> 1.123456789012345
addx1471 add 1.123456789012345  0E-11  -> 1.123456789012345
addx1472 add 1.123456789012345  0E-12  -> 1.123456789012345
addx1473 add 1.123456789012345  0E-13  -> 1.123456789012345
addx1474 add 1.123456789012345  0E-14  -> 1.123456789012345
addx1475 add 1.123456789012345  0E-15  -> 1.123456789012345
-- next four flag Rounded because the 0 extends the result
addx1476 add 1.123456789012345  0E-16  -> 1.123456789012345 Rounded
addx1477 add 1.123456789012345  0E-17  -> 1.123456789012345 Rounded
addx1478 add 1.123456789012345  0E-18  -> 1.123456789012345 Rounded
addx1479 add 1.123456789012345  0E-19  -> 1.123456789012345 Rounded

-- sum of two opposite-sign operands is exactly 0 and floor => -0
precision:   16
................................................................................
addx1704  add 1E2       1E4       -> 1.01E+4
addx1705  subtract 130E-2  120E-2 -> 0.10
addx1706  subtract 130E-2  12E-1  -> 0.10
addx1707  subtract 130E-2  1E0    -> 0.30
addx1708  subtract 1E2     1E4    -> -9.9E+3

------------------------------------------------------------------------
-- Same as above, using decimal64 default parameters                  --
------------------------------------------------------------------------
precision:   16
rounding:    half_even
maxExponent: 384
minexponent: -383

-- [first group are 'quick confidence check']
................................................................................
addx6063 add 1 '0.001' -> '1.001'
addx6064 add 1 '0.0001' -> '1.0001'
addx6065 add 1 '0.00001' -> '1.00001'
addx6066 add 1 '0.000001' -> '1.000001'
addx6067 add 1 '0.0000001' -> '1.0000001'
addx6068 add 1 '0.00000001' -> '1.00000001'

-- cancellation to integer
addx6069 add 99999999999999123456789 -99999999999999E+9 -> 123456789

-- some funny zeros [in case of bad signum]
addx6070 add 1  0    -> 1
addx6071 add 1 0.    -> 1
addx6072 add 1  .0   -> 1.0
addx6073 add 1 0.0   -> 1.0
addx6074 add 1 0.00  -> 1.00
addx6075 add  0  1   -> 1
................................................................................
addx6350 add 1E+16   7    ->   1.000000000000001E+16  Inexact Rounded
addx6351 add 1E+17   7    ->   1.000000000000000E+17  Inexact Rounded

-- tryzeros cases
addx6361  add 0E+50 10000E+1  -> 1.0000E+5
addx6362  add 10000E+1 0E-50  -> 100000.0000000000  Rounded
addx6363  add 10000E+1 10000E-50  -> 100000.0000000000  Rounded Inexact
addx6364  add 12.34    0e-398  -> 12.34000000000000  Rounded

-- ulp replacement tests
addx6400 add   1   77e-14      ->  1.00000000000077
addx6401 add   1   77e-15      ->  1.000000000000077
addx6402 add   1   77e-16      ->  1.000000000000008 Inexact Rounded
addx6403 add   1   77e-17      ->  1.000000000000001 Inexact Rounded
addx6404 add   1   77e-18      ->  1.000000000000000 Inexact Rounded
................................................................................
addx6574 subtract  1E-383  1E-384  ->   9E-384 Subnormal

-- Here we explore the boundary of rounding a subnormal to Nmin
addx6575 subtract  1E-383  1E-398  ->   9.99999999999999E-384  Subnormal
addx6576 subtract  1E-383  1E-398  ->   9.99999999999999E-384  Subnormal
addx6577 subtract  1E-383  1E-399  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
addx6578 subtract  1E-383  1E-400  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
addx6579 subtract  1E-383  1E-401  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded
addx6580 subtract  1E-383  1E-402  ->   1.000000000000000E-383 Underflow Inexact Subnormal Rounded

-- check overflow edge case
--               1234567890123456
addx6972 apply   9.999999999999999E+384         -> 9.999999999999999E+384
addx6973 add     9.999999999999999E+384  1      -> 9.999999999999999E+384 Inexact Rounded
addx6974 add      9999999999999999E+369  1      -> 9.999999999999999E+384 Inexact Rounded
addx6975 add      9999999999999999E+369  1E+369  -> Infinity Overflow Inexact Rounded
................................................................................
addx61469 add 1.123456789012345  0E-9   -> 1.123456789012345
addx61470 add 1.123456789012345  0E-10  -> 1.123456789012345
addx61471 add 1.123456789012345  0E-11  -> 1.123456789012345
addx61472 add 1.123456789012345  0E-12  -> 1.123456789012345
addx61473 add 1.123456789012345  0E-13  -> 1.123456789012345
addx61474 add 1.123456789012345  0E-14  -> 1.123456789012345
addx61475 add 1.123456789012345  0E-15  -> 1.123456789012345
-- next four flag Rounded because the 0 extends the result
addx61476 add 1.123456789012345  0E-16  -> 1.123456789012345 Rounded
addx61477 add 1.123456789012345  0E-17  -> 1.123456789012345 Rounded
addx61478 add 1.123456789012345  0E-18  -> 1.123456789012345 Rounded
addx61479 add 1.123456789012345  0E-19  -> 1.123456789012345 Rounded

-- sum of two opposite-sign operands is exactly 0 and floor => -0
rounding:    half_up
................................................................................
-- some exact zeros from non-zeros
addx61635 add  1E-401   1E-401 ->  0E-398 Subnormal Inexact Rounded Underflow Clamped
addx61636 add -1E-401   1E-401 -> -0E-398 Clamped  -- *
addx61637 add  1E-401  -1E-401 -> -0E-398 Clamped  -- *
addx61638 add -1E-401  -1E-401 -> -1E-398 Subnormal Inexact Rounded Underflow

-- Examples from SQL proposal (Krishna Kulkarni)
addx61701 add 130E-2    120E-2    -> 2.50
addx61702 add 130E-2    12E-1     -> 2.50
addx61703 add 130E-2    1E0       -> 2.30
addx61704 add 1E2       1E4       -> 1.01E+4
addx61705 subtract 130E-2  120E-2 -> 0.10
addx61706 subtract 130E-2  12E-1  -> 0.10
addx61707 subtract 130E-2  1E0    -> 0.30
addx61708 subtract 1E2     1E4    -> -9.9E+3

-- Gappy coefficients; check residue handling even with full coefficient gap
rounding: half_even

addx62001 add 1234567890123456 1      -> 1234567890123457
addx62002 add 1234567890123456 0.6    -> 1234567890123457  Inexact Rounded
addx62003 add 1234567890123456 0.06   -> 1234567890123456  Inexact Rounded
addx62004 add 1234567890123456 6E-3   -> 1234567890123456  Inexact Rounded
addx62005 add 1234567890123456 6E-4   -> 1234567890123456  Inexact Rounded
addx62006 add 1234567890123456 6E-5   -> 1234567890123456  Inexact Rounded
addx62007 add 1234567890123456 6E-6   -> 1234567890123456  Inexact Rounded
addx62008 add 1234567890123456 6E-7   -> 1234567890123456  Inexact Rounded
addx62009 add 1234567890123456 6E-8   -> 1234567890123456  Inexact Rounded
addx62010 add 1234567890123456 6E-9   -> 1234567890123456  Inexact Rounded
addx62011 add 1234567890123456 6E-10  -> 1234567890123456  Inexact Rounded
addx62012 add 1234567890123456 6E-11  -> 1234567890123456  Inexact Rounded
addx62013 add 1234567890123456 6E-12  -> 1234567890123456  Inexact Rounded
addx62014 add 1234567890123456 6E-13  -> 1234567890123456  Inexact Rounded
addx62015 add 1234567890123456 6E-14  -> 1234567890123456  Inexact Rounded
addx62016 add 1234567890123456 6E-15  -> 1234567890123456  Inexact Rounded
addx62017 add 1234567890123456 6E-16  -> 1234567890123456  Inexact Rounded
addx62018 add 1234567890123456 6E-17  -> 1234567890123456  Inexact Rounded
addx62019 add 1234567890123456 6E-18  -> 1234567890123456  Inexact Rounded
addx62020 add 1234567890123456 6E-19  -> 1234567890123456  Inexact Rounded
addx62021 add 1234567890123456 6E-20  -> 1234567890123456  Inexact Rounded

-- widening second argument at gap
addx62030 add 12345678 1                       -> 12345679
addx62031 add 12345678 0.1                     -> 12345678.1
addx62032 add 12345678 0.12                    -> 12345678.12
addx62033 add 12345678 0.123                   -> 12345678.123
addx62034 add 12345678 0.1234                  -> 12345678.1234
addx62035 add 12345678 0.12345                 -> 12345678.12345
addx62036 add 12345678 0.123456                -> 12345678.123456
addx62037 add 12345678 0.1234567               -> 12345678.1234567
addx62038 add 12345678 0.12345678              -> 12345678.12345678
addx62039 add 12345678 0.123456789             -> 12345678.12345679 Inexact Rounded
addx62040 add 12345678 0.123456785             -> 12345678.12345678 Inexact Rounded
addx62041 add 12345678 0.1234567850            -> 12345678.12345678 Inexact Rounded
addx62042 add 12345678 0.1234567851            -> 12345678.12345679 Inexact Rounded
addx62043 add 12345678 0.12345678501           -> 12345678.12345679 Inexact Rounded
addx62044 add 12345678 0.123456785001          -> 12345678.12345679 Inexact Rounded
addx62045 add 12345678 0.1234567850001         -> 12345678.12345679 Inexact Rounded
addx62046 add 12345678 0.12345678500001        -> 12345678.12345679 Inexact Rounded
addx62047 add 12345678 0.123456785000001       -> 12345678.12345679 Inexact Rounded
addx62048 add 12345678 0.1234567850000001      -> 12345678.12345679 Inexact Rounded
addx62049 add 12345678 0.1234567850000000      -> 12345678.12345678 Inexact Rounded
--                               90123456
rounding: half_even
addx62050 add 12345678 0.0234567750000000      -> 12345678.02345678 Inexact Rounded
addx62051 add 12345678 0.0034567750000000      -> 12345678.00345678 Inexact Rounded
addx62052 add 12345678 0.0004567750000000      -> 12345678.00045678 Inexact Rounded
addx62053 add 12345678 0.0000567750000000      -> 12345678.00005678 Inexact Rounded
addx62054 add 12345678 0.0000067750000000      -> 12345678.00000678 Inexact Rounded
addx62055 add 12345678 0.0000007750000000      -> 12345678.00000078 Inexact Rounded
addx62056 add 12345678 0.0000000750000000      -> 12345678.00000008 Inexact Rounded
addx62057 add 12345678 0.0000000050000000      -> 12345678.00000000 Inexact Rounded
addx62060 add 12345678 0.0234567750000001      -> 12345678.02345678 Inexact Rounded
addx62061 add 12345678 0.0034567750000001      -> 12345678.00345678 Inexact Rounded
addx62062 add 12345678 0.0004567750000001      -> 12345678.00045678 Inexact Rounded
addx62063 add 12345678 0.0000567750000001      -> 12345678.00005678 Inexact Rounded
addx62064 add 12345678 0.0000067750000001      -> 12345678.00000678 Inexact Rounded
addx62065 add 12345678 0.0000007750000001      -> 12345678.00000078 Inexact Rounded
addx62066 add 12345678 0.0000000750000001      -> 12345678.00000008 Inexact Rounded
addx62067 add 12345678 0.0000000050000001      -> 12345678.00000001 Inexact Rounded
-- far-out residues (full coefficient gap is 16+15 digits)
rounding: up
addx62070 add 12345678 1E-8                    -> 12345678.00000001
addx62071 add 12345678 1E-9                    -> 12345678.00000001 Inexact Rounded
addx62072 add 12345678 1E-10                   -> 12345678.00000001 Inexact Rounded
addx62073 add 12345678 1E-11                   -> 12345678.00000001 Inexact Rounded
addx62074 add 12345678 1E-12                   -> 12345678.00000001 Inexact Rounded
addx62075 add 12345678 1E-13                   -> 12345678.00000001 Inexact Rounded
addx62076 add 12345678 1E-14                   -> 12345678.00000001 Inexact Rounded
addx62077 add 12345678 1E-15                   -> 12345678.00000001 Inexact Rounded
addx62078 add 12345678 1E-16                   -> 12345678.00000001 Inexact Rounded
addx62079 add 12345678 1E-17                   -> 12345678.00000001 Inexact Rounded
addx62080 add 12345678 1E-18                   -> 12345678.00000001 Inexact Rounded
addx62081 add 12345678 1E-19                   -> 12345678.00000001 Inexact Rounded
addx62082 add 12345678 1E-20                   -> 12345678.00000001 Inexact Rounded
addx62083 add 12345678 1E-25                   -> 12345678.00000001 Inexact Rounded
addx62084 add 12345678 1E-30                   -> 12345678.00000001 Inexact Rounded
addx62085 add 12345678 1E-31                   -> 12345678.00000001 Inexact Rounded
addx62086 add 12345678 1E-32                   -> 12345678.00000001 Inexact Rounded
addx62087 add 12345678 1E-33                   -> 12345678.00000001 Inexact Rounded
addx62088 add 12345678 1E-34                   -> 12345678.00000001 Inexact Rounded
addx62089 add 12345678 1E-35                   -> 12345678.00000001 Inexact Rounded

-- payload decapitate
precision: 5
addx62100 add      11  sNaN123456789 ->  NaN56789  Invalid_operation
addx62101 add     -11 -sNaN123456789 -> -NaN56789  Invalid_operation
addx62102 add      11   NaN123456789 ->  NaN56789
addx62103 add     -11  -NaN123456789 -> -NaN56789

-- Null tests
addx9990 add 10  # -> NaN Invalid_operation
addx9991 add  # 10 -> NaN Invalid_operation

Added test/dectest/and.decTest.





































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
------------------------------------------------------------------------
-- and.decTest -- digitwise logical AND                               --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
-- particular, achieving the same results as the tests here is not    --
-- a guarantee that an implementation complies with any Standard      --
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

extended:    1
precision:   9
rounding:    half_up
maxExponent: 999
minExponent: -999

-- Sanity check (truth table)
andx001 and             0    0 ->    0
andx002 and             0    1 ->    0
andx003 and             1    0 ->    0
andx004 and             1    1 ->    1
andx005 and          1100 1010 -> 1000
andx006 and          1111   10 ->   10
andx007 and          1111 1010 -> 1010

-- and at msd and msd-1
andx010 and 000000000 000000000 ->           0
andx011 and 000000000 100000000 ->           0
andx012 and 100000000 000000000 ->           0
andx013 and 100000000 100000000 ->   100000000
andx014 and 000000000 000000000 ->           0
andx015 and 000000000 010000000 ->           0
andx016 and 010000000 000000000 ->           0
andx017 and 010000000 010000000 ->    10000000

-- Various lengths
--          123456789     123456789      123456789
andx021 and 111111111     111111111  ->  111111111
andx022 and 111111111111  111111111  ->  111111111
andx023 and 111111111111   11111111  ->   11111111
andx024 and 111111111      11111111  ->   11111111
andx025 and 111111111       1111111  ->    1111111
andx026 and 111111111111     111111  ->     111111
andx027 and 111111111111      11111  ->      11111
andx028 and 111111111111       1111  ->       1111
andx029 and 111111111111        111  ->        111
andx031 and 111111111111         11  ->         11
andx032 and 111111111111          1  ->          1
andx033 and 111111111111 1111111111  ->  111111111
andx034 and 11111111111 11111111111  ->  111111111
andx035 and 1111111111 111111111111  ->  111111111
andx036 and 111111111 1111111111111  ->  111111111

andx040 and 111111111  111111111111  ->  111111111
andx041 and  11111111  111111111111  ->   11111111
andx042 and  11111111     111111111  ->   11111111
andx043 and   1111111     111111111  ->    1111111
andx044 and    111111     111111111  ->     111111
andx045 and     11111     111111111  ->      11111
andx046 and      1111     111111111  ->       1111
andx047 and       111     111111111  ->        111
andx048 and        11     111111111  ->         11
andx049 and         1     111111111  ->          1

andx050 and 1111111111  1  ->  1
andx051 and  111111111  1  ->  1
andx052 and   11111111  1  ->  1
andx053 and    1111111  1  ->  1
andx054 and     111111  1  ->  1
andx055 and      11111  1  ->  1
andx056 and       1111  1  ->  1
andx057 and        111  1  ->  1
andx058 and         11  1  ->  1
andx059 and          1  1  ->  1

andx060 and 1111111111  0  ->  0
andx061 and  111111111  0  ->  0
andx062 and   11111111  0  ->  0
andx063 and    1111111  0  ->  0
andx064 and     111111  0  ->  0
andx065 and      11111  0  ->  0
andx066 and       1111  0  ->  0
andx067 and        111  0  ->  0
andx068 and         11  0  ->  0
andx069 and          1  0  ->  0

andx070 and 1  1111111111  ->  1
andx071 and 1   111111111  ->  1
andx072 and 1    11111111  ->  1
andx073 and 1     1111111  ->  1
andx074 and 1      111111  ->  1
andx075 and 1       11111  ->  1
andx076 and 1        1111  ->  1
andx077 and 1         111  ->  1
andx078 and 1          11  ->  1
andx079 and 1           1  ->  1

andx080 and 0  1111111111  ->  0
andx081 and 0   111111111  ->  0
andx082 and 0    11111111  ->  0
andx083 and 0     1111111  ->  0
andx084 and 0      111111  ->  0
andx085 and 0       11111  ->  0
andx086 and 0        1111  ->  0
andx087 and 0         111  ->  0
andx088 and 0          11  ->  0
andx089 and 0           1  ->  0

andx090 and 011111111  111111111  ->   11111111
andx091 and 101111111  111111111  ->  101111111
andx092 and 110111111  111111111  ->  110111111
andx093 and 111011111  111111111  ->  111011111
andx094 and 111101111  111111111  ->  111101111
andx095 and 111110111  111111111  ->  111110111
andx096 and 111111011  111111111  ->  111111011
andx097 and 111111101  111111111  ->  111111101
andx098 and 111111110  111111111  ->  111111110

andx100 and 111111111  011111111  ->   11111111
andx101 and 111111111  101111111  ->  101111111
andx102 and 111111111  110111111  ->  110111111
andx103 and 111111111  111011111  ->  111011111
andx104 and 111111111  111101111  ->  111101111
andx105 and 111111111  111110111  ->  111110111
andx106 and 111111111  111111011  ->  111111011
andx107 and 111111111  111111101  ->  111111101
andx108 and 111111111  111111110  ->  111111110

-- non-0/1 should not be accepted, nor should signs
andx220 and 111111112  111111111  ->  NaN Invalid_operation
andx221 and 333333333  333333333  ->  NaN Invalid_operation
andx222 and 555555555  555555555  ->  NaN Invalid_operation
andx223 and 777777777  777777777  ->  NaN Invalid_operation
andx224 and 999999999  999999999  ->  NaN Invalid_operation
andx225 and 222222222  999999999  ->  NaN Invalid_operation
andx226 and 444444444  999999999  ->  NaN Invalid_operation
andx227 and 666666666  999999999  ->  NaN Invalid_operation
andx228 and 888888888  999999999  ->  NaN Invalid_operation
andx229 and 999999999  222222222  ->  NaN Invalid_operation
andx230 and 999999999  444444444  ->  NaN Invalid_operation
andx231 and 999999999  666666666  ->  NaN Invalid_operation
andx232 and 999999999  888888888  ->  NaN Invalid_operation
-- a few randoms
andx240 and  567468689 -934981942 ->  NaN Invalid_operation
andx241 and  567367689  934981942 ->  NaN Invalid_operation
andx242 and -631917772 -706014634 ->  NaN Invalid_operation
andx243 and -756253257  138579234 ->  NaN Invalid_operation
andx244 and  835590149  567435400 ->  NaN Invalid_operation
-- test MSD
andx250 and  200000000 100000000 ->  NaN Invalid_operation
andx251 and  700000000 100000000 ->  NaN Invalid_operation
andx252 and  800000000 100000000 ->  NaN Invalid_operation
andx253 and  900000000 100000000 ->  NaN Invalid_operation
andx254 and  200000000 000000000 ->  NaN Invalid_operation
andx255 and  700000000 000000000 ->  NaN Invalid_operation
andx256 and  800000000 000000000 ->  NaN Invalid_operation
andx257 and  900000000 000000000 ->  NaN Invalid_operation
andx258 and  100000000 200000000 ->  NaN Invalid_operation
andx259 and  100000000 700000000 ->  NaN Invalid_operation
andx260 and  100000000 800000000 ->  NaN Invalid_operation
andx261 and  100000000 900000000 ->  NaN Invalid_operation
andx262 and  000000000 200000000 ->  NaN Invalid_operation
andx263 and  000000000 700000000 ->  NaN Invalid_operation
andx264 and  000000000 800000000 ->  NaN Invalid_operation
andx265 and  000000000 900000000 ->  NaN Invalid_operation
-- test MSD-1
andx270 and  020000000 100000000 ->  NaN Invalid_operation
andx271 and  070100000 100000000 ->  NaN Invalid_operation
andx272 and  080010000 100000001 ->  NaN Invalid_operation
andx273 and  090001000 100000010 ->  NaN Invalid_operation
andx274 and  100000100 020010100 ->  NaN Invalid_operation
andx275 and  100000000 070001000 ->  NaN Invalid_operation
andx276 and  100000010 080010100 ->  NaN Invalid_operation
andx277 and  100000000 090000010 ->  NaN Invalid_operation
-- test LSD
andx280 and  001000002 100000000 ->  NaN Invalid_operation
andx281 and  000000007 100000000 ->  NaN Invalid_operation
andx282 and  000000008 100000000 ->  NaN Invalid_operation
andx283 and  000000009 100000000 ->  NaN Invalid_operation
andx284 and  100000000 000100002 ->  NaN Invalid_operation
andx285 and  100100000 001000007 ->  NaN Invalid_operation
andx286 and  100010000 010000008 ->  NaN Invalid_operation
andx287 and  100001000 100000009 ->  NaN Invalid_operation
-- test Middie
andx288 and  001020000 100000000 ->  NaN Invalid_operation
andx289 and  000070001 100000000 ->  NaN Invalid_operation
andx290 and  000080000 100010000 ->  NaN Invalid_operation
andx291 and  000090000 100001000 ->  NaN Invalid_operation
andx292 and  100000010 000020100 ->  NaN Invalid_operation
andx293 and  100100000 000070010 ->  NaN Invalid_operation
andx294 and  100010100 000080001 ->  NaN Invalid_operation
andx295 and  100001000 000090000 ->  NaN Invalid_operation
-- signs
andx296 and -100001000 -000000000 ->  NaN Invalid_operation
andx297 and -100001000  000010000 ->  NaN Invalid_operation
andx298 and  100001000 -000000000 ->  NaN Invalid_operation
andx299 and  100001000  000011000 ->  1000

-- Nmax, Nmin, Ntiny
andx331 and  2   9.99999999E+999     -> NaN Invalid_operation
andx332 and  3   1E-999              -> NaN Invalid_operation
andx333 and  4   1.00000000E-999     -> NaN Invalid_operation
andx334 and  5   1E-1007             -> NaN Invalid_operation
andx335 and  6   -1E-1007            -> NaN Invalid_operation
andx336 and  7   -1.00000000E-999    -> NaN Invalid_operation
andx337 and  8   -1E-999             -> NaN Invalid_operation
andx338 and  9   -9.99999999E+999    -> NaN Invalid_operation
andx341 and  9.99999999E+999     -18 -> NaN Invalid_operation
andx342 and  1E-999               01 -> NaN Invalid_operation
andx343 and  1.00000000E-999     -18 -> NaN Invalid_operation
andx344 and  1E-1007              18 -> NaN Invalid_operation
andx345 and  -1E-1007            -10 -> NaN Invalid_operation
andx346 and  -1.00000000E-999     18 -> NaN Invalid_operation
andx347 and  -1E-999              10 -> NaN Invalid_operation
andx348 and  -9.99999999E+999    -18 -> NaN Invalid_operation

-- A few other non-integers
andx361 and  1.0                  1  -> NaN Invalid_operation
andx362 and  1E+1                 1  -> NaN Invalid_operation
andx363 and  0.0                  1  -> NaN Invalid_operation
andx364 and  0E+1                 1  -> NaN Invalid_operation
andx365 and  9.9                  1  -> NaN Invalid_operation
andx366 and  9E+1                 1  -> NaN Invalid_operation
andx371 and  0 1.0                   -> NaN Invalid_operation
andx372 and  0 1E+1                  -> NaN Invalid_operation
andx373 and  0 0.0                   -> NaN Invalid_operation
andx374 and  0 0E+1                  -> NaN Invalid_operation
andx375 and  0 9.9                   -> NaN Invalid_operation
andx376 and  0 9E+1                  -> NaN Invalid_operation

-- All Specials are in error
andx780 and -Inf  -Inf   -> NaN Invalid_operation
andx781 and -Inf  -1000  -> NaN Invalid_operation
andx782 and -Inf  -1     -> NaN Invalid_operation
andx783 and -Inf  -0     -> NaN Invalid_operation
andx784 and -Inf   0     -> NaN Invalid_operation
andx785 and -Inf   1     -> NaN Invalid_operation
andx786 and -Inf   1000  -> NaN Invalid_operation
andx787 and -1000 -Inf   -> NaN Invalid_operation
andx788 and -Inf  -Inf   -> NaN Invalid_operation
andx789 and -1    -Inf   -> NaN Invalid_operation
andx790 and -0    -Inf   -> NaN Invalid_operation
andx791 and  0    -Inf   -> NaN Invalid_operation
andx792 and  1    -Inf   -> NaN Invalid_operation
andx793 and  1000 -Inf   -> NaN Invalid_operation
andx794 and  Inf  -Inf   -> NaN Invalid_operation

andx800 and  Inf  -Inf   -> NaN Invalid_operation
andx801 and  Inf  -1000  -> NaN Invalid_operation
andx802 and  Inf  -1     -> NaN Invalid_operation
andx803 and  Inf  -0     -> NaN Invalid_operation
andx804 and  Inf   0     -> NaN Invalid_operation
andx805 and  Inf   1     -> NaN Invalid_operation
andx806 and  Inf   1000  -> NaN Invalid_operation
andx807 and  Inf   Inf   -> NaN Invalid_operation
andx808 and -1000  Inf   -> NaN Invalid_operation
andx809 and -Inf   Inf   -> NaN Invalid_operation
andx810 and -1     Inf   -> NaN Invalid_operation
andx811 and -0     Inf   -> NaN Invalid_operation
andx812 and  0     Inf   -> NaN Invalid_operation
andx813 and  1     Inf   -> NaN Invalid_operation
andx814 and  1000  Inf   -> NaN Invalid_operation
andx815 and  Inf   Inf   -> NaN Invalid_operation

andx821 and  NaN -Inf    -> NaN Invalid_operation
andx822 and  NaN -1000   -> NaN Invalid_operation
andx823 and  NaN -1      -> NaN Invalid_operation
andx824 and  NaN -0      -> NaN Invalid_operation
andx825 and  NaN  0      -> NaN Invalid_operation
andx826 and  NaN  1      -> NaN Invalid_operation
andx827 and  NaN  1000   -> NaN Invalid_operation
andx828 and  NaN  Inf    -> NaN Invalid_operation
andx829 and  NaN  NaN    -> NaN Invalid_operation
andx830 and -Inf  NaN    -> NaN Invalid_operation
andx831 and -1000 NaN    -> NaN Invalid_operation
andx832 and -1    NaN    -> NaN Invalid_operation
andx833 and -0    NaN    -> NaN Invalid_operation
andx834 and  0    NaN    -> NaN Invalid_operation
andx835 and  1    NaN    -> NaN Invalid_operation
andx836 and  1000 NaN    -> NaN Invalid_operation
andx837 and  Inf  NaN    -> NaN Invalid_operation

andx841 and  sNaN -Inf   ->  NaN  Invalid_operation
andx842 and  sNaN -1000  ->  NaN  Invalid_operation
andx843 and  sNaN -1     ->  NaN  Invalid_operation
andx844 and  sNaN -0     ->  NaN  Invalid_operation
andx845 and  sNaN  0     ->  NaN  Invalid_operation
andx846 and  sNaN  1     ->  NaN  Invalid_operation
andx847 and  sNaN  1000  ->  NaN  Invalid_operation
andx848 and  sNaN  NaN   ->  NaN  Invalid_operation
andx849 and  sNaN sNaN   ->  NaN  Invalid_operation
andx850 and  NaN  sNaN   ->  NaN  Invalid_operation
andx851 and -Inf  sNaN   ->  NaN  Invalid_operation
andx852 and -1000 sNaN   ->  NaN  Invalid_operation
andx853 and -1    sNaN   ->  NaN  Invalid_operation
andx854 and -0    sNaN   ->  NaN  Invalid_operation
andx855 and  0    sNaN   ->  NaN  Invalid_operation
andx856 and  1    sNaN   ->  NaN  Invalid_operation
andx857 and  1000 sNaN   ->  NaN  Invalid_operation
andx858 and  Inf  sNaN   ->  NaN  Invalid_operation
andx859 and  NaN  sNaN   ->  NaN  Invalid_operation

-- propagating NaNs
andx861 and  NaN1   -Inf    -> NaN Invalid_operation
andx862 and +NaN2   -1000   -> NaN Invalid_operation
andx863 and  NaN3    1000   -> NaN Invalid_operation
andx864 and  NaN4    Inf    -> NaN Invalid_operation
andx865 and  NaN5   +NaN6   -> NaN Invalid_operation
andx866 and -Inf     NaN7   -> NaN Invalid_operation
andx867 and -1000    NaN8   -> NaN Invalid_operation
andx868 and  1000    NaN9   -> NaN Invalid_operation
andx869 and  Inf    +NaN10  -> NaN Invalid_operation
andx871 and  sNaN11  -Inf   -> NaN Invalid_operation
andx872 and  sNaN12  -1000  -> NaN Invalid_operation
andx873 and  sNaN13   1000  -> NaN Invalid_operation
andx874 and  sNaN14   NaN17 -> NaN Invalid_operation
andx875 and  sNaN15  sNaN18 -> NaN Invalid_operation
andx876 and  NaN16   sNaN19 -> NaN Invalid_operation
andx877 and -Inf    +sNaN20 -> NaN Invalid_operation
andx878 and -1000    sNaN21 -> NaN Invalid_operation
andx879 and  1000    sNaN22 -> NaN Invalid_operation
andx880 and  Inf     sNaN23 -> NaN Invalid_operation
andx881 and +NaN25  +sNaN24 -> NaN Invalid_operation
andx882 and -NaN26    NaN28 -> NaN Invalid_operation
andx883 and -sNaN27  sNaN29 -> NaN Invalid_operation
andx884 and  1000    -NaN30 -> NaN Invalid_operation
andx885 and  1000   -sNaN31 -> NaN Invalid_operation

Changes to test/dectest/base.decTest.

1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
..
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110






















111
112
113
114
115
116
117
...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
...
414
415
416
417
418
419
420
















421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
...
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
...
494
495
496
497
498
499
500


















501
502
503
504
505
506
507
...
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594

595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
...
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
...
886
887
888
889
890
891
892
























































893
894
895
896
897
898
899
....
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305



1306




1307


1308



1309




1310



1311






------------------------------------------------------------------------
-- base.decTest -- base decimal <--> string conversions               --
-- Copyright (c) IBM Corporation, 1981, 2003.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.52


-- This file tests base conversions from string to a decimal number
-- and back to a string (in either Scientific or Engineering form)

-- Note that unlike other operations the operand is subject to rounding
-- to conform to emax and precision settings (that is, numbers will
-- conform to rules and exponent will be in permitted range).

precision:   15
rounding:    half_up
maxExponent: 999999999
minExponent: -999999999
extended:    1

basx001 toSci       0 -> 0
basx002 toSci       1 -> 1
basx003 toSci     1.0 -> 1.0
basx004 toSci    1.00 -> 1.00
basx005 toSci      10 -> 10
basx006 toSci    1000 -> 1000
................................................................................
basx036 toSci '0.0000000123456789'  -> '1.23456789E-8'

basx037 toSci '0.123456789012344'   -> '0.123456789012344'
basx038 toSci '0.123456789012345'   -> '0.123456789012345'

-- String [many more examples are implicitly tested elsewhere]
-- strings without E cannot generate E in result
basx100 toSci "12"        -> '12'
basx101 toSci "-76"       -> '-76'
basx102 toSci "12.76"     -> '12.76'
basx103 toSci "+12.76"    -> '12.76'
basx104 toSci "012.76"    -> '12.76'
basx105 toSci "+0.003"    -> '0.003'
basx106 toSci "17."       -> '17'
basx107 toSci ".5"        -> '0.5'
basx108 toSci "044"       -> '44'
basx109 toSci "0044"      -> '44'
basx110 toSci "0.0005"      -> '0.0005'
basx111 toSci "00.00005"    -> '0.00005'
basx112 toSci "0.000005"    -> '0.000005'
basx113 toSci "0.0000050"   -> '0.0000050'
basx114 toSci "0.0000005"   -> '5E-7'
basx115 toSci "0.00000005"  -> '5E-8'
basx116 toSci "12345678.543210" -> '12345678.543210'
basx117 toSci "2345678.543210" -> '2345678.543210'
basx118 toSci "345678.543210" -> '345678.543210'
basx119 toSci "0345678.54321" -> '345678.54321'
basx120 toSci "345678.5432" -> '345678.5432'
basx121 toSci "+345678.5432" -> '345678.5432'
basx122 toSci "+0345678.5432" -> '345678.5432'
basx123 toSci "+00345678.5432" -> '345678.5432'
basx124 toSci "-345678.5432"  -> '-345678.5432'
basx125 toSci "-0345678.5432"  -> '-345678.5432'
basx126 toSci "-00345678.5432"  -> '-345678.5432'
-- examples
basx127 toSci "5E-6"        -> '0.000005'
basx128 toSci "50E-7"       -> '0.0000050'
basx129 toSci "5E-7"        -> '5E-7'


-- [No exotics as no Unicode]























-- Numbers with E
basx130 toSci "0.000E-1"  -> '0.0000'
basx131 toSci "0.000E-2"  -> '0.00000'
basx132 toSci "0.000E-3"  -> '0.000000'
basx133 toSci "0.000E-4"  -> '0E-7'
basx134 toSci "0.00E-2"   -> '0.0000'
basx135 toSci "0.00E-3"   -> '0.00000'
................................................................................
basx258 toSci "0.1265E+1"  -> '1.265'
basx259 toSci "0.1265E+2"  -> '12.65'
basx260 toSci "0.1265E+3"  -> '126.5'
basx261 toSci "0.1265E+4"  -> '1265'
basx262 toSci "0.1265E+8"  -> '1.265E+7'
basx263 toSci "0.1265E+20" -> '1.265E+19'

basx270 toSci "0.09e999"  -> '9E+997'
basx271 toSci "0.9e999"   -> '9E+998'
basx272 toSci "9e999"     -> '9E+999'
basx273 toSci "9.9e999"   -> '9.9E+999'
basx274 toSci "9.99e999"  -> '9.99E+999'
basx275 toSci "9.99e-999" -> '9.99E-999'
basx276 toSci "9.9e-999"  -> '9.9E-999'
basx277 toSci "9e-999"    -> '9E-999'
basx279 toSci "99e-999"   -> '9.9E-998'
basx280 toSci "999e-999"  -> '9.99E-997'
basx281 toSci '0.9e-998'  -> '9E-999'
basx282 toSci '0.09e-997' -> '9E-999'
basx283 toSci '0.1e1000'  -> '1E+999'
basx284 toSci '10e-1000'  -> '1.0E-999'

-- some more negative zeros [systematic tests below]
basx290 toSci "-0.000E-1"  -> '-0.0000'
basx291 toSci "-0.000E-2"  -> '-0.00000'
basx292 toSci "-0.000E-3"  -> '-0.000000'
basx293 toSci "-0.000E-4"  -> '-0E-7'
basx294 toSci "-0.00E-2"   -> '-0.0000'
basx295 toSci "-0.00E-3"   -> '-0.00000'
................................................................................
basx470  toSci 1000000003000 -> 1.00000000E+12   Rounded Inexact
basx471  toEng 1000000003000 -> 1.00000000E+12   Rounded Inexact
basx472  toSci 1000000005000 -> 1.00000001E+12   Rounded Inexact
basx473  toEng 1000000005000 -> 1.00000001E+12   Rounded Inexact
basx474  toSci 1000000009000 -> 1.00000001E+12   Rounded Inexact
basx475  toEng 1000000009000 -> 1.00000001E+12   Rounded Inexact

















-- check rounding modes heeded
precision: 5
rounding:  ceiling
bsrx401  toSci  1.23450    ->  1.2345  Rounded
bsrx402  toSci  1.234549   ->  1.2346  Rounded Inexact
bsrx403  toSci  1.234550   ->  1.2346  Rounded Inexact
bsrx404  toSci  1.234551   ->  1.2346  Rounded Inexact
rounding:  down
bsrx405  toSci  1.23450    ->  1.2345  Rounded
bsrx406  toSci  1.234549   ->  1.2345  Rounded Inexact
bsrx407  toSci  1.234550   ->  1.2345  Rounded Inexact
bsrx408  toSci  1.234551   ->  1.2345  Rounded Inexact
rounding:  floor
bsrx410  toSci  1.23450    ->  1.2345  Rounded
bsrx411  toSci  1.234549   ->  1.2345  Rounded Inexact
bsrx412  toSci  1.234550   ->  1.2345  Rounded Inexact
bsrx413  toSci  1.234551   ->  1.2345  Rounded Inexact
rounding:  half_down
bsrx415  toSci  1.23450    ->  1.2345  Rounded
................................................................................
bsrx435  toSci  1.234551   ->  1.2346  Rounded Inexact
-- negatives
rounding:  ceiling
bsrx501  toSci -1.23450    -> -1.2345  Rounded
bsrx502  toSci -1.234549   -> -1.2345  Rounded Inexact
bsrx503  toSci -1.234550   -> -1.2345  Rounded Inexact
bsrx504  toSci -1.234551   -> -1.2345  Rounded Inexact
rounding:  down
bsrx505  toSci -1.23450    -> -1.2345  Rounded
bsrx506  toSci -1.234549   -> -1.2345  Rounded Inexact
bsrx507  toSci -1.234550   -> -1.2345  Rounded Inexact
bsrx508  toSci -1.234551   -> -1.2345  Rounded Inexact
rounding:  floor
bsrx510  toSci -1.23450    -> -1.2345  Rounded
bsrx511  toSci -1.234549   -> -1.2346  Rounded Inexact
bsrx512  toSci -1.234550   -> -1.2346  Rounded Inexact
bsrx513  toSci -1.234551   -> -1.2346  Rounded Inexact
rounding:  half_down
bsrx515  toSci -1.23450    -> -1.2345  Rounded
................................................................................
rounding:  half_up
bsrx531  toSci -1.23450    -> -1.2345  Rounded
bsrx532  toSci -1.234549   -> -1.2345  Rounded Inexact
bsrx533  toSci -1.234550   -> -1.2346  Rounded Inexact
bsrx534  toSci -1.234650   -> -1.2347  Rounded Inexact
bsrx535  toSci -1.234551   -> -1.2346  Rounded Inexact



















rounding:  half_up
precision: 9

-- The 'baddies' tests from DiagBigDecimal, plus some new ones
basx500 toSci '1..2'            -> NaN Conversion_syntax
basx501 toSci '.'               -> NaN Conversion_syntax
basx502 toSci '..'              -> NaN Conversion_syntax
................................................................................
basx570 toSci "9Inf"            -> NaN Conversion_syntax
basx571 toSci "-0Inf"           -> NaN Conversion_syntax
basx572 toSci "-9Inf"           -> NaN Conversion_syntax
basx573 toSci "-sNa"            -> NaN Conversion_syntax
basx574 toSci "xNaN"            -> NaN Conversion_syntax
basx575 toSci "0sNaN"           -> NaN Conversion_syntax

-- subnormals and overflows
basx576 toSci '99e999999999'       -> Infinity Overflow  Inexact Rounded
basx577 toSci '999e999999999'      -> Infinity Overflow  Inexact Rounded
basx578 toSci '0.9e-999999999'     -> 9E-1000000000 Subnormal
basx579 toSci '0.09e-999999999'    -> 9E-1000000001 Subnormal
basx580 toSci '0.1e1000000000'     -> 1E+999999999
basx581 toSci '10e-1000000000'     -> 1.0E-999999999
basx582 toSci '0.9e9999999999'     -> Infinity Overflow  Inexact Rounded
basx583 toSci '99e-9999999999'     -> 0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx584 toSci '111e9999999999'     -> Infinity Overflow  Inexact Rounded
basx585 toSci '1111e-9999999999'   -> 0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx586 toSci '1111e-99999999999'  -> 0E-1000000007 Underflow Subnormal Inexact Rounded Clamped

basx587 toSci '7e1000000000'       -> Infinity Overflow  Inexact Rounded
-- negatives the same
basx588 toSci '-99e999999999'      -> -Infinity Overflow  Inexact Rounded
basx589 toSci '-999e999999999'     -> -Infinity Overflow  Inexact Rounded
basx590 toSci '-0.9e-999999999'    -> -9E-1000000000 Subnormal
basx591 toSci '-0.09e-999999999'   -> -9E-1000000001 Subnormal
basx592 toSci '-0.1e1000000000'    -> -1E+999999999
basx593 toSci '-10e-1000000000'    -> -1.0E-999999999
basx594 toSci '-0.9e9999999999'    -> -Infinity Overflow  Inexact Rounded
basx595 toSci '-99e-9999999999'    -> -0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx596 toSci '-111e9999999999'    -> -Infinity Overflow  Inexact Rounded
basx597 toSci '-1111e-9999999999'  -> -0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx598 toSci '-1111e-99999999999' -> -0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx599 toSci '-7e1000000000'      -> -Infinity Overflow  Inexact Rounded

-- Zeros
basx601 toSci 0.000000000       -> 0E-9
basx602 toSci 0.00000000        -> 0E-8
basx603 toSci 0.0000000         -> 0E-7
basx604 toSci 0.000000          -> 0.000000
basx605 toSci 0.00000           -> 0.00000
................................................................................
basx684 toSci      00.          ->  0
basx685 toSci       0.          ->  0
basx686 toSci  +00000.          ->  0
basx687 toSci  -00000.          -> -0
basx688 toSci  +0.              ->  0
basx689 toSci  -0.              -> -0

-- some baddies with dots and Es
basx690 toSci  'e+1'            ->  NaN Conversion_syntax
basx691 toSci  '.e+1'           ->  NaN Conversion_syntax
basx692 toSci  '+.e+1'          ->  NaN Conversion_syntax
basx693 toSci  '-.e+'           ->  NaN Conversion_syntax
basx694 toSci  '-.e'            ->  NaN Conversion_syntax
basx695 toSci  'E+1'            ->  NaN Conversion_syntax
basx696 toSci  '.E+1'           ->  NaN Conversion_syntax
basx697 toSci  '+.E+1'          ->  NaN Conversion_syntax
basx698 toSci  '-.E+'           ->  NaN Conversion_syntax
basx699 toSci  '-.E'            ->  NaN Conversion_syntax

-- Specials
precision: 4
basx700 toSci "NaN"             -> NaN
basx701 toSci "nan"             -> NaN
basx702 toSci "nAn"             -> NaN
basx703 toSci "NAN"             -> NaN
basx704 toSci "+NaN"            -> NaN
................................................................................
basx873 toEng  0.00E-3          -> 0.00000
basx874 toEng  0.00E-4          -> 0.000000
basx875 toEng  0.00E-5          -> 0.0E-6
basx876 toEng  0.00E-6          -> 0.00E-6
basx877 toEng  0.00E-7          -> 0E-9
basx878 toEng  0.00E-8          -> 0.0E-9
basx879 toEng  0.00E-9          -> 0.00E-9

























































-- Giga exponent initial tests
maxExponent: 999999999
minExponent: -999999999

basx951 toSci '99e999'          -> '9.9E+1000'
basx952 toSci '999e999'         -> '9.99E+1001'
................................................................................
precision:   5
maxexponent: 79
minexponent: -79
basx1041 toSci     1.52444E-80  ->  1.524E-80 Inexact Rounded Subnormal Underflow
basx1042 toSci     1.52445E-80  ->  1.524E-80 Inexact Rounded Subnormal Underflow
basx1043 toSci     1.52446E-80  ->  1.524E-80 Inexact Rounded Subnormal Underflow

-- clamped zeros [see also clamped.decTest]
precision:   34
maxExponent: 6144
minExponent: -6143

basx1061 apply   0e+10000  ->  0E+6144 Clamped
basx1062 apply   0e-10000  ->  0E-6176 Clamped
basx1063 apply  -0e+10000  -> -0E+6144 Clamped
basx1064 apply  -0e-10000  -> -0E-6176 Clamped


































|







 







|
>








|

|
|
<







 







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|

<


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







|

|
|
|







 







|

|
|
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
|
|
|
|
|
|
|
|
|
|
<
>
|
|
|
|
|
<
<
<
<
<
<
<
<
<







 







<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|









>
>
>

>
>
>
>

>
>

>
>
>

>
>
>
>

>
>
>

>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

34
35
36
37
38
39
40
..
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
...
242
243
244
245
246
247
248















249
250
251
252
253
254
255
...
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
...
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
...
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633

634
635
636
637
638
639









640
641
642
643
644
645
646
...
724
725
726
727
728
729
730












731
732
733
734
735
736
737
...
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
....
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
------------------------------------------------------------------------
-- base.decTest -- base decimal <--> string conversions               --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55
extended:    1

-- This file tests base conversions from string to a decimal number
-- and back to a string (in either Scientific or Engineering form)

-- Note that unlike other operations the operand is subject to rounding
-- to conform to emax and precision settings (that is, numbers will
-- conform to rules and exponent will be in permitted range).

precision:   16
rounding:    half_up
maxExponent: 384
minExponent: -383


basx001 toSci       0 -> 0
basx002 toSci       1 -> 1
basx003 toSci     1.0 -> 1.0
basx004 toSci    1.00 -> 1.00
basx005 toSci      10 -> 10
basx006 toSci    1000 -> 1000
................................................................................
basx036 toSci '0.0000000123456789'  -> '1.23456789E-8'

basx037 toSci '0.123456789012344'   -> '0.123456789012344'
basx038 toSci '0.123456789012345'   -> '0.123456789012345'

-- String [many more examples are implicitly tested elsewhere]
-- strings without E cannot generate E in result
basx040 toSci "12"        -> '12'
basx041 toSci "-76"       -> '-76'
basx042 toSci "12.76"     -> '12.76'
basx043 toSci "+12.76"    -> '12.76'
basx044 toSci "012.76"    -> '12.76'
basx045 toSci "+0.003"    -> '0.003'
basx046 toSci "17."       -> '17'
basx047 toSci ".5"        -> '0.5'
basx048 toSci "044"       -> '44'
basx049 toSci "0044"      -> '44'
basx050 toSci "0.0005"      -> '0.0005'
basx051 toSci "00.00005"    -> '0.00005'
basx052 toSci "0.000005"    -> '0.000005'
basx053 toSci "0.0000050"   -> '0.0000050'
basx054 toSci "0.0000005"   -> '5E-7'
basx055 toSci "0.00000005"  -> '5E-8'
basx056 toSci "12345678.543210" -> '12345678.543210'
basx057 toSci "2345678.543210" -> '2345678.543210'
basx058 toSci "345678.543210" -> '345678.543210'
basx059 toSci "0345678.54321" -> '345678.54321'
basx060 toSci "345678.5432" -> '345678.5432'
basx061 toSci "+345678.5432" -> '345678.5432'
basx062 toSci "+0345678.5432" -> '345678.5432'
basx063 toSci "+00345678.5432" -> '345678.5432'
basx064 toSci "-345678.5432"  -> '-345678.5432'
basx065 toSci "-0345678.5432"  -> '-345678.5432'
basx066 toSci "-00345678.5432"  -> '-345678.5432'
-- examples
basx067 toSci "5E-6"        -> '0.000005'
basx068 toSci "50E-7"       -> '0.0000050'
basx069 toSci "5E-7"        -> '5E-7'


-- [No exotics as no Unicode]

-- rounded with dots in all (including edge) places
basx071 toSci  .1234567890123456123  -> 0.1234567890123456 Inexact Rounded
basx072 toSci  1.234567890123456123  -> 1.234567890123456 Inexact Rounded
basx073 toSci  12.34567890123456123  -> 12.34567890123456 Inexact Rounded
basx074 toSci  123.4567890123456123  -> 123.4567890123456 Inexact Rounded
basx075 toSci  1234.567890123456123  -> 1234.567890123456 Inexact Rounded
basx076 toSci  12345.67890123456123  -> 12345.67890123456 Inexact Rounded
basx077 toSci  123456.7890123456123  -> 123456.7890123456 Inexact Rounded
basx078 toSci  1234567.890123456123  -> 1234567.890123456 Inexact Rounded
basx079 toSci  12345678.90123456123  -> 12345678.90123456 Inexact Rounded
basx080 toSci  123456789.0123456123  -> 123456789.0123456 Inexact Rounded
basx081 toSci  1234567890.123456123  -> 1234567890.123456 Inexact Rounded
basx082 toSci  12345678901.23456123  -> 12345678901.23456 Inexact Rounded
basx083 toSci  123456789012.3456123  -> 123456789012.3456 Inexact Rounded
basx084 toSci  1234567890123.456123  -> 1234567890123.456 Inexact Rounded
basx085 toSci  12345678901234.56123  -> 12345678901234.56 Inexact Rounded
basx086 toSci  123456789012345.6123  -> 123456789012345.6 Inexact Rounded
basx087 toSci  1234567890123456.123  -> 1234567890123456  Inexact Rounded
basx088 toSci  12345678901234561.23  -> 1.234567890123456E+16 Inexact Rounded
basx089 toSci  123456789012345612.3  -> 1.234567890123456E+17 Inexact Rounded
basx090 toSci  1234567890123456123.  -> 1.234567890123456E+18 Inexact Rounded

-- Numbers with E
basx130 toSci "0.000E-1"  -> '0.0000'
basx131 toSci "0.000E-2"  -> '0.00000'
basx132 toSci "0.000E-3"  -> '0.000000'
basx133 toSci "0.000E-4"  -> '0E-7'
basx134 toSci "0.00E-2"   -> '0.0000'
basx135 toSci "0.00E-3"   -> '0.00000'
................................................................................
basx258 toSci "0.1265E+1"  -> '1.265'
basx259 toSci "0.1265E+2"  -> '12.65'
basx260 toSci "0.1265E+3"  -> '126.5'
basx261 toSci "0.1265E+4"  -> '1265'
basx262 toSci "0.1265E+8"  -> '1.265E+7'
basx263 toSci "0.1265E+20" -> '1.265E+19'
















-- some more negative zeros [systematic tests below]
basx290 toSci "-0.000E-1"  -> '-0.0000'
basx291 toSci "-0.000E-2"  -> '-0.00000'
basx292 toSci "-0.000E-3"  -> '-0.000000'
basx293 toSci "-0.000E-4"  -> '-0E-7'
basx294 toSci "-0.00E-2"   -> '-0.0000'
basx295 toSci "-0.00E-3"   -> '-0.00000'
................................................................................
basx470  toSci 1000000003000 -> 1.00000000E+12   Rounded Inexact
basx471  toEng 1000000003000 -> 1.00000000E+12   Rounded Inexact
basx472  toSci 1000000005000 -> 1.00000001E+12   Rounded Inexact
basx473  toEng 1000000005000 -> 1.00000001E+12   Rounded Inexact
basx474  toSci 1000000009000 -> 1.00000001E+12   Rounded Inexact
basx475  toEng 1000000009000 -> 1.00000001E+12   Rounded Inexact

-- all-nines rounding
precision: 9
rounding:  half_up
basx270  toSci 999999999          ->   999999999
basx271  toSci 9999999990         ->   9.99999999E+9      Rounded
basx272  toSci 9999999991         ->   9.99999999E+9      Rounded Inexact
basx273  toSci 9999999992         ->   9.99999999E+9      Rounded Inexact
basx274  toSci 9999999993         ->   9.99999999E+9      Rounded Inexact
basx275  toSci 9999999994         ->   9.99999999E+9      Rounded Inexact
basx276  toSci 9999999995         ->   1.00000000E+10     Rounded Inexact
basx277  toSci 9999999996         ->   1.00000000E+10     Rounded Inexact
basx278  toSci 9999999997         ->   1.00000000E+10     Rounded Inexact
basx279  toSci 9999999998         ->   1.00000000E+10     Rounded Inexact
basx280  toSci 9999999999         ->   1.00000000E+10     Rounded Inexact
basx281  toSci 9999999999999999   ->   1.00000000E+16     Rounded Inexact

-- check rounding modes heeded
precision: 5
rounding:  ceiling
bsrx401  toSci  1.23450    ->  1.2345  Rounded
bsrx402  toSci  1.234549   ->  1.2346  Rounded Inexact
bsrx403  toSci  1.234550   ->  1.2346  Rounded Inexact
bsrx404  toSci  1.234551   ->  1.2346  Rounded Inexact
rounding:  up
bsrx405  toSci  1.23450    ->  1.2345  Rounded
bsrx406  toSci  1.234549   ->  1.2346  Rounded Inexact
bsrx407  toSci  1.234550   ->  1.2346  Rounded Inexact
bsrx408  toSci  1.234551   ->  1.2346  Rounded Inexact
rounding:  floor
bsrx410  toSci  1.23450    ->  1.2345  Rounded
bsrx411  toSci  1.234549   ->  1.2345  Rounded Inexact
bsrx412  toSci  1.234550   ->  1.2345  Rounded Inexact
bsrx413  toSci  1.234551   ->  1.2345  Rounded Inexact
rounding:  half_down
bsrx415  toSci  1.23450    ->  1.2345  Rounded
................................................................................
bsrx435  toSci  1.234551   ->  1.2346  Rounded Inexact
-- negatives
rounding:  ceiling
bsrx501  toSci -1.23450    -> -1.2345  Rounded
bsrx502  toSci -1.234549   -> -1.2345  Rounded Inexact
bsrx503  toSci -1.234550   -> -1.2345  Rounded Inexact
bsrx504  toSci -1.234551   -> -1.2345  Rounded Inexact
rounding:  up
bsrx505  toSci -1.23450    -> -1.2345  Rounded
bsrx506  toSci -1.234549   -> -1.2346  Rounded Inexact
bsrx507  toSci -1.234550   -> -1.2346  Rounded Inexact
bsrx508  toSci -1.234551   -> -1.2346  Rounded Inexact
rounding:  floor
bsrx510  toSci -1.23450    -> -1.2345  Rounded
bsrx511  toSci -1.234549   -> -1.2346  Rounded Inexact
bsrx512  toSci -1.234550   -> -1.2346  Rounded Inexact
bsrx513  toSci -1.234551   -> -1.2346  Rounded Inexact
rounding:  half_down
bsrx515  toSci -1.23450    -> -1.2345  Rounded
................................................................................
rounding:  half_up
bsrx531  toSci -1.23450    -> -1.2345  Rounded
bsrx532  toSci -1.234549   -> -1.2345  Rounded Inexact
bsrx533  toSci -1.234550   -> -1.2346  Rounded Inexact
bsrx534  toSci -1.234650   -> -1.2347  Rounded Inexact
bsrx535  toSci -1.234551   -> -1.2346  Rounded Inexact

-- a few larger exponents
maxExponent: 999999999
minExponent: -999999999
basx480 toSci "0.09e999"  -> '9E+997'
basx481 toSci "0.9e999"   -> '9E+998'
basx482 toSci "9e999"     -> '9E+999'
basx483 toSci "9.9e999"   -> '9.9E+999'
basx484 toSci "9.99e999"  -> '9.99E+999'
basx485 toSci "9.99e-999" -> '9.99E-999'
basx486 toSci "9.9e-999"  -> '9.9E-999'
basx487 toSci "9e-999"    -> '9E-999'
basx489 toSci "99e-999"   -> '9.9E-998'
basx490 toSci "999e-999"  -> '9.99E-997'
basx491 toSci '0.9e-998'  -> '9E-999'
basx492 toSci '0.09e-997' -> '9E-999'
basx493 toSci '0.1e1000'  -> '1E+999'
basx494 toSci '10e-1000'  -> '1.0E-999'

rounding:  half_up
precision: 9

-- The 'baddies' tests from DiagBigDecimal, plus some new ones
basx500 toSci '1..2'            -> NaN Conversion_syntax
basx501 toSci '.'               -> NaN Conversion_syntax
basx502 toSci '..'              -> NaN Conversion_syntax
................................................................................
basx570 toSci "9Inf"            -> NaN Conversion_syntax
basx571 toSci "-0Inf"           -> NaN Conversion_syntax
basx572 toSci "-9Inf"           -> NaN Conversion_syntax
basx573 toSci "-sNa"            -> NaN Conversion_syntax
basx574 toSci "xNaN"            -> NaN Conversion_syntax
basx575 toSci "0sNaN"           -> NaN Conversion_syntax

-- some baddies with dots and Es and dots and specials
basx576 toSci  'e+1'            ->  NaN Conversion_syntax
basx577 toSci  '.e+1'           ->  NaN Conversion_syntax
basx578 toSci  '+.e+1'          ->  NaN Conversion_syntax
basx579 toSci  '-.e+'           ->  NaN Conversion_syntax
basx580 toSci  '-.e'            ->  NaN Conversion_syntax
basx581 toSci  'E+1'            ->  NaN Conversion_syntax
basx582 toSci  '.E+1'           ->  NaN Conversion_syntax
basx583 toSci  '+.E+1'          ->  NaN Conversion_syntax
basx584 toSci  '-.E+'           ->  NaN Conversion_syntax
basx585 toSci  '-.E'            ->  NaN Conversion_syntax


basx586 toSci  '.NaN'           ->  NaN Conversion_syntax
basx587 toSci  '-.NaN'          ->  NaN Conversion_syntax
basx588 toSci  '+.sNaN'         ->  NaN Conversion_syntax
basx589 toSci  '+.Inf'          ->  NaN Conversion_syntax
basx590 toSci  '.Infinity'      ->  NaN Conversion_syntax










-- Zeros
basx601 toSci 0.000000000       -> 0E-9
basx602 toSci 0.00000000        -> 0E-8
basx603 toSci 0.0000000         -> 0E-7
basx604 toSci 0.000000          -> 0.000000
basx605 toSci 0.00000           -> 0.00000
................................................................................
basx684 toSci      00.          ->  0
basx685 toSci       0.          ->  0
basx686 toSci  +00000.          ->  0
basx687 toSci  -00000.          -> -0
basx688 toSci  +0.              ->  0
basx689 toSci  -0.              -> -0













-- Specials
precision: 4
basx700 toSci "NaN"             -> NaN
basx701 toSci "nan"             -> NaN
basx702 toSci "nAn"             -> NaN
basx703 toSci "NAN"             -> NaN
basx704 toSci "+NaN"            -> NaN
................................................................................
basx873 toEng  0.00E-3          -> 0.00000
basx874 toEng  0.00E-4          -> 0.000000
basx875 toEng  0.00E-5          -> 0.0E-6
basx876 toEng  0.00E-6          -> 0.00E-6
basx877 toEng  0.00E-7          -> 0E-9
basx878 toEng  0.00E-8          -> 0.0E-9
basx879 toEng  0.00E-9          -> 0.00E-9


rounding:  half_up
precision: 9
-- subnormals and overflows
basx906 toSci '99e999999999'       -> Infinity Overflow  Inexact Rounded
basx907 toSci '999e999999999'      -> Infinity Overflow  Inexact Rounded
basx908 toSci '0.9e-999999999'     -> 9E-1000000000 Subnormal
basx909 toSci '0.09e-999999999'    -> 9E-1000000001 Subnormal
basx910 toSci '0.1e1000000000'     -> 1E+999999999
basx911 toSci '10e-1000000000'     -> 1.0E-999999999
basx912 toSci '0.9e9999999999'     -> Infinity Overflow  Inexact Rounded
basx913 toSci '99e-9999999999'     -> 0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx914 toSci '111e9999999999'     -> Infinity Overflow  Inexact Rounded
basx915 toSci '1111e-9999999999'   -> 0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx916 toSci '1111e-99999999999'  -> 0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx917 toSci '7e1000000000'       -> Infinity Overflow  Inexact Rounded
-- negatives the same
basx918 toSci '-99e999999999'      -> -Infinity Overflow  Inexact Rounded
basx919 toSci '-999e999999999'     -> -Infinity Overflow  Inexact Rounded
basx920 toSci '-0.9e-999999999'    -> -9E-1000000000 Subnormal
basx921 toSci '-0.09e-999999999'   -> -9E-1000000001 Subnormal
basx922 toSci '-0.1e1000000000'    -> -1E+999999999
basx923 toSci '-10e-1000000000'    -> -1.0E-999999999
basx924 toSci '-0.9e9999999999'    -> -Infinity Overflow  Inexact Rounded
basx925 toSci '-99e-9999999999'    -> -0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx926 toSci '-111e9999999999'    -> -Infinity Overflow  Inexact Rounded
basx927 toSci '-1111e-9999999999'  -> -0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx928 toSci '-1111e-99999999999' -> -0E-1000000007 Underflow Subnormal Inexact Rounded Clamped
basx929 toSci '-7e1000000000'      -> -Infinity Overflow  Inexact Rounded

rounding:  ceiling
basx930 toSci  '7e1000000000'      ->  Infinity Overflow  Inexact Rounded
basx931 toSci '-7e1000000000'      -> -9.99999999E+999999999 Overflow  Inexact Rounded
rounding:  up
basx932 toSci  '7e1000000000'      ->  Infinity Overflow  Inexact Rounded
basx933 toSci '-7e1000000000'      -> -Infinity Overflow  Inexact Rounded
rounding:  down
basx934 toSci  '7e1000000000'      ->  9.99999999E+999999999 Overflow  Inexact Rounded
basx935 toSci '-7e1000000000'      -> -9.99999999E+999999999 Overflow  Inexact Rounded
rounding:  floor
basx936 toSci  '7e1000000000'      ->  9.99999999E+999999999 Overflow  Inexact Rounded
basx937 toSci '-7e1000000000'      -> -Infinity Overflow  Inexact Rounded

rounding:  half_up
basx938 toSci  '7e1000000000'      ->  Infinity Overflow  Inexact Rounded
basx939 toSci '-7e1000000000'      -> -Infinity Overflow  Inexact Rounded
rounding:  half_even
basx940 toSci  '7e1000000000'      ->  Infinity Overflow  Inexact Rounded
basx941 toSci '-7e1000000000'      -> -Infinity Overflow  Inexact Rounded
rounding:  half_down
basx942 toSci  '7e1000000000'      ->  Infinity Overflow  Inexact Rounded
basx943 toSci '-7e1000000000'      -> -Infinity Overflow  Inexact Rounded

rounding:  half_even


-- Giga exponent initial tests
maxExponent: 999999999
minExponent: -999999999

basx951 toSci '99e999'          -> '9.9E+1000'
basx952 toSci '999e999'         -> '9.99E+1001'
................................................................................
precision:   5
maxexponent: 79
minexponent: -79
basx1041 toSci     1.52444E-80  ->  1.524E-80 Inexact Rounded Subnormal Underflow
basx1042 toSci     1.52445E-80  ->  1.524E-80 Inexact Rounded Subnormal Underflow
basx1043 toSci     1.52446E-80  ->  1.524E-80 Inexact Rounded Subnormal Underflow

-- clamped zeros [see also clamp.decTest]
precision:   34
maxExponent: 6144
minExponent: -6143

basx1061 apply   0e+10000  ->  0E+6144 Clamped
basx1062 apply   0e-10000  ->  0E-6176 Clamped
basx1063 apply  -0e+10000  -> -0E+6144 Clamped
basx1064 apply  -0e-10000  -> -0E-6176 Clamped

precision:   16
maxExponent: 384
minExponent: -383

basx1065 apply   0e+10000  ->  0E+384  Clamped
basx1066 apply   0e-10000  ->  0E-398  Clamped
basx1067 apply  -0e+10000  -> -0E+384  Clamped
basx1068 apply  -0e-10000  -> -0E-398  Clamped

-- same with IEEE clamping
clamp:       1

precision:   34
maxExponent: 6144
minExponent: -6143

basx1071 apply   0e+10000  ->  0E+6111 Clamped
basx1072 apply   0e-10000  ->  0E-6176 Clamped
basx1073 apply  -0e+10000  -> -0E+6111 Clamped
basx1074 apply  -0e-10000  -> -0E-6176 Clamped

precision:   16
maxExponent: 384
minExponent: -383

basx1075 apply   0e+10000  ->  0E+369  Clamped
basx1076 apply   0e-10000  ->  0E-398  Clamped
basx1077 apply  -0e+10000  -> -0E+369  Clamped
basx1078 apply  -0e-10000  -> -0E-398  Clamped


Changes to test/dectest/clamp.decTest.

1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
------------------------------------------------------------------------
-- clamp.decTest -- clamped exponent tests (format-independent)       --
-- Copyright (c) IBM Corporation, 2000, 2003.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.52

-- This set of tests uses the same limits as the 8-byte concrete
-- representation, but applies clamping without using format-specific
-- conversions.

extended:    1
precision:   16


|







 







|







1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
------------------------------------------------------------------------
-- clamp.decTest -- clamped exponent tests (format-independent)       --
-- Copyright (c) IBM Corporation, 2000, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

-- This set of tests uses the same limits as the 8-byte concrete
-- representation, but applies clamping without using format-specific
-- conversions.

extended:    1
precision:   16

Added test/dectest/class.decTest.







































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
------------------------------------------------------------------------
-- class.decTest -- Class operations                                  --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
-- particular, achieving the same results as the tests here is not    --
-- a guarantee that an implementation complies with any Standard      --
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

-- [New 2006.11.27]

precision:   9
maxExponent: 999
minExponent: -999
extended:    1
clamp:       1
rounding:    half_even

clasx001  class    0                        -> +Zero
clasx002  class    0.00                     -> +Zero
clasx003  class    0E+5                     -> +Zero
clasx004  class    1E-1007                  -> +Subnormal
clasx005  class  0.1E-999                   -> +Subnormal
clasx006  class  0.99999999E-999            -> +Subnormal
clasx007  class  1.00000000E-999            -> +Normal
clasx008  class   1E-999                    -> +Normal
clasx009  class   1E-100                    -> +Normal
clasx010  class   1E-10                     -> +Normal
clasx012  class   1E-1                      -> +Normal
clasx013  class   1                         -> +Normal
clasx014  class   2.50                      -> +Normal
clasx015  class   100.100                   -> +Normal
clasx016  class   1E+30                     -> +Normal
clasx017  class   1E+999                    -> +Normal
clasx018  class   9.99999999E+999           -> +Normal
clasx019  class   Inf                       -> +Infinity

clasx021  class   -0                        -> -Zero
clasx022  class   -0.00                     -> -Zero
clasx023  class   -0E+5                     -> -Zero
clasx024  class   -1E-1007                  -> -Subnormal
clasx025  class  -0.1E-999                  -> -Subnormal
clasx026  class  -0.99999999E-999           -> -Subnormal
clasx027  class  -1.00000000E-999           -> -Normal
clasx028  class  -1E-999                    -> -Normal
clasx029  class  -1E-100                    -> -Normal
clasx030  class  -1E-10                     -> -Normal
clasx032  class  -1E-1                      -> -Normal
clasx033  class  -1                         -> -Normal
clasx034  class  -2.50                      -> -Normal
clasx035  class  -100.100                   -> -Normal
clasx036  class  -1E+30                     -> -Normal
clasx037  class  -1E+999                    -> -Normal
clasx038  class  -9.99999999E+999           -> -Normal
clasx039  class  -Inf                       -> -Infinity

clasx041  class   NaN                       -> NaN
clasx042  class  -NaN                       -> NaN
clasx043  class  +NaN12345                  -> NaN
clasx044  class   sNaN                      -> sNaN
clasx045  class  -sNaN                      -> sNaN
clasx046  class  +sNaN12345                 -> sNaN


-- decimal64 bounds

precision:   16
maxExponent: 384
minExponent: -383
clamp:       1
rounding:    half_even

clasx201  class    0                        -> +Zero
clasx202  class    0.00                     -> +Zero
clasx203  class    0E+5                     -> +Zero
clasx204  class    1E-396                   -> +Subnormal
clasx205  class  0.1E-383                   -> +Subnormal
clasx206  class  0.999999999999999E-383     -> +Subnormal
clasx207  class  1.000000000000000E-383     -> +Normal
clasx208  class   1E-383                    -> +Normal
clasx209  class   1E-100                    -> +Normal
clasx210  class   1E-10                     -> +Normal
clasx212  class   1E-1                      -> +Normal
clasx213  class   1                         -> +Normal
clasx214  class   2.50                      -> +Normal
clasx215  class   100.100                   -> +Normal
clasx216  class   1E+30                     -> +Normal
clasx217  class   1E+384                    -> +Normal
clasx218  class   9.999999999999999E+384    -> +Normal
clasx219  class   Inf                       -> +Infinity

clasx221  class   -0                        -> -Zero
clasx222  class   -0.00                     -> -Zero
clasx223  class   -0E+5                     -> -Zero
clasx224  class   -1E-396                   -> -Subnormal
clasx225  class  -0.1E-383                  -> -Subnormal
clasx226  class  -0.999999999999999E-383    -> -Subnormal
clasx227  class  -1.000000000000000E-383    -> -Normal
clasx228  class  -1E-383                    -> -Normal
clasx229  class  -1E-100                    -> -Normal
clasx230  class  -1E-10                     -> -Normal
clasx232  class  -1E-1                      -> -Normal
clasx233  class  -1                         -> -Normal
clasx234  class  -2.50                      -> -Normal
clasx235  class  -100.100                   -> -Normal
clasx236  class  -1E+30                     -> -Normal
clasx237  class  -1E+384                    -> -Normal
clasx238  class  -9.999999999999999E+384    -> -Normal
clasx239  class  -Inf                       -> -Infinity

clasx241  class   NaN                       -> NaN
clasx242  class  -NaN                       -> NaN
clasx243  class  +NaN12345                  -> NaN
clasx244  class   sNaN                      -> sNaN
clasx245  class  -sNaN                      -> sNaN
clasx246  class  +sNaN12345                 -> sNaN



Changes to test/dectest/compare.decTest.

1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
...
261
262
263
264
265
266
267















268
269
270
271
272
273
274
...
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
...
708
709
710
711
712
713
714


























715
716
717
------------------------------------------------------------------------
-- compare.decTest -- decimal comparison                              --
-- Copyright (c) IBM Corporation, 1981, 2003.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.52

-- Note that we cannot assume add/subtract tests cover paths adequately,
-- here, because the code might be quite different (comparison cannot
-- overflow or underflow, so actual subtractions are not necessary).

extended: 1

................................................................................
comx083 compare   2.0   0.0  -> 1
comx085 compare   2.0   1.0  -> 1
comx086 compare   2.0   2.0  -> 0

-- now some cases which might overflow if subtract were used
maxexponent: 999999999
minexponent: -999999999
comx090 compare  9.99999999E+999999999 9.99999999E+999999999  -> 0
comx091 compare -9.99999999E+999999999 9.99999999E+999999999  -> -1
comx092 compare  9.99999999E+999999999 -9.99999999E+999999999 -> 1
comx093 compare -9.99999999E+999999999 -9.99999999E+999999999 -> 0

-- some differing length/exponent cases
comx100 compare   7.0    7.0    -> 0
comx101 compare   7.0    7      -> 0
comx102 compare   7      7.0    -> 0
comx103 compare   7E+0   7.0    -> 0
comx104 compare   70E-1  7.0    -> 0
................................................................................
comx445 compare   -.8E+1  -9      -> 1
comx446 compare   -80E-1  -9      -> 1
comx447 compare   -8.0    -9E+0   -> 1
comx448 compare   -8.0    -90E-1  -> 1
comx449 compare   -8      -.9E+1  -> 1
comx450 compare   -8      -90E-1  -> 1

















-- testcases that subtract to lots of zeros at boundaries [pgr]
precision: 40
comx470 compare 123.4560000000000000E789 123.456E789 -> 0
comx471 compare 123.456000000000000E-89 123.456E-89 -> 0
comx472 compare 123.45600000000000E789 123.456E789 -> 0
comx473 compare 123.4560000000000E-89 123.456E-89 -> 0
................................................................................
comx565 compare    1E+9   1       ->  1
comx566 compare    1E+10  1       ->  1
comx567 compare    1E+11  1       ->  1
comx568 compare    1E+12  1       ->  1
comx569 compare    1E+13  1       ->  1
comx570 compare    1E+14  1       ->  1
comx571 compare    1E+15  1       ->  1
-- similar with an useful coefficient, one side only
comx580 compare  0.000000987654321     1E-15    -> 1
comx581 compare  0.000000987654321     1E-14    -> 1
comx582 compare  0.000000987654321     1E-13    -> 1
comx583 compare  0.000000987654321     1E-12    -> 1
comx584 compare  0.000000987654321     1E-11    -> 1
comx585 compare  0.000000987654321     1E-10    -> 1
comx586 compare  0.000000987654321     1E-9     -> 1
................................................................................
comx903 compare -1e+777777777  1e+411111111 -> -1
comx904 compare -1e+777777777 -1e+411111111 -> -1
comx905 compare  1e-777777777  1e-411111111 -> -1
comx906 compare  1e-777777777 -1e-411111111 ->  1
comx907 compare -1e-777777777  1e-411111111 -> -1
comx908 compare -1e-777777777 -1e-411111111 ->  1



























-- Null tests
comx990 compare 10  # -> NaN Invalid_operation
comx991 compare  # 10 -> NaN Invalid_operation

|
|







 







|







 







|
|
|
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
...
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
...
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
------------------------------------------------------------------------
-- compare.decTest -- decimal comparison that allows quiet NaNs       --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

-- Note that we cannot assume add/subtract tests cover paths adequately,
-- here, because the code might be quite different (comparison cannot
-- overflow or underflow, so actual subtractions are not necessary).

extended: 1

................................................................................
comx083 compare   2.0   0.0  -> 1
comx085 compare   2.0   1.0  -> 1
comx086 compare   2.0   2.0  -> 0

-- now some cases which might overflow if subtract were used
maxexponent: 999999999
minexponent: -999999999
comx095 compare  9.99999999E+999999999 9.99999999E+999999999  -> 0
comx096 compare -9.99999999E+999999999 9.99999999E+999999999  -> -1
comx097 compare  9.99999999E+999999999 -9.99999999E+999999999 -> 1
comx098 compare -9.99999999E+999999999 -9.99999999E+999999999 -> 0

-- some differing length/exponent cases
comx100 compare   7.0    7.0    -> 0
comx101 compare   7.0    7      -> 0
comx102 compare   7      7.0    -> 0
comx103 compare   7E+0   7.0    -> 0
comx104 compare   70E-1  7.0    -> 0
................................................................................
comx445 compare   -.8E+1  -9      -> 1
comx446 compare   -80E-1  -9      -> 1
comx447 compare   -8.0    -9E+0   -> 1
comx448 compare   -8.0    -90E-1  -> 1
comx449 compare   -8      -.9E+1  -> 1
comx450 compare   -8      -90E-1  -> 1

-- misalignment traps for little-endian
comx451 compare      1.0       0.1  -> 1
comx452 compare      0.1       1.0  -> -1
comx453 compare     10.0       0.1  -> 1
comx454 compare      0.1      10.0  -> -1
comx455 compare      100       1.0  -> 1
comx456 compare      1.0       100  -> -1
comx457 compare     1000      10.0  -> 1
comx458 compare     10.0      1000  -> -1
comx459 compare    10000     100.0  -> 1
comx460 compare    100.0     10000  -> -1
comx461 compare   100000    1000.0  -> 1
comx462 compare   1000.0    100000  -> -1
comx463 compare  1000000   10000.0  -> 1
comx464 compare  10000.0   1000000  -> -1

-- testcases that subtract to lots of zeros at boundaries [pgr]
precision: 40
comx470 compare 123.4560000000000000E789 123.456E789 -> 0
comx471 compare 123.456000000000000E-89 123.456E-89 -> 0
comx472 compare 123.45600000000000E789 123.456E789 -> 0
comx473 compare 123.4560000000000E-89 123.456E-89 -> 0
................................................................................
comx565 compare    1E+9   1       ->  1
comx566 compare    1E+10  1       ->  1
comx567 compare    1E+11  1       ->  1
comx568 compare    1E+12  1       ->  1
comx569 compare    1E+13  1       ->  1
comx570 compare    1E+14  1       ->  1
comx571 compare    1E+15  1       ->  1
-- similar with a useful coefficient, one side only
comx580 compare  0.000000987654321     1E-15    -> 1
comx581 compare  0.000000987654321     1E-14    -> 1
comx582 compare  0.000000987654321     1E-13    -> 1
comx583 compare  0.000000987654321     1E-12    -> 1
comx584 compare  0.000000987654321     1E-11    -> 1
comx585 compare  0.000000987654321     1E-10    -> 1
comx586 compare  0.000000987654321     1E-9     -> 1
................................................................................
comx903 compare -1e+777777777  1e+411111111 -> -1
comx904 compare -1e+777777777 -1e+411111111 -> -1
comx905 compare  1e-777777777  1e-411111111 -> -1
comx906 compare  1e-777777777 -1e-411111111 ->  1
comx907 compare -1e-777777777  1e-411111111 -> -1
comx908 compare -1e-777777777 -1e-411111111 ->  1

-- spread zeros
comx910 compare   0E-383  0       ->  0
comx911 compare   0E-383 -0       ->  0
comx912 compare  -0E-383  0       ->  0
comx913 compare  -0E-383 -0       ->  0
comx914 compare   0E-383  0E+384  ->  0
comx915 compare   0E-383 -0E+384  ->  0
comx916 compare  -0E-383  0E+384  ->  0
comx917 compare  -0E-383 -0E+384  ->  0
comx918 compare   0       0E+384  ->  0
comx919 compare   0      -0E+384  ->  0
comx920 compare  -0       0E+384  ->  0
comx921 compare  -0      -0E+384  ->  0
comx930 compare   0E+384  0       ->  0
comx931 compare   0E+384 -0       ->  0
comx932 compare  -0E+384  0       ->  0
comx933 compare  -0E+384 -0       ->  0
comx934 compare   0E+384  0E-383  ->  0
comx935 compare   0E+384 -0E-383  ->  0
comx936 compare  -0E+384  0E-383  ->  0
comx937 compare  -0E+384 -0E-383  ->  0
comx938 compare   0       0E-383  ->  0
comx939 compare   0      -0E-383  ->  0
comx940 compare  -0       0E-383  ->  0
comx941 compare  -0      -0E-383  ->  0

-- Null tests
comx990 compare 10  # -> NaN Invalid_operation
comx991 compare  # 10 -> NaN Invalid_operation

Changes to test/dectest/comparetotal.decTest.

1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
114
115
116
117
118
119
120








121
122
123
124
125
126
127
...
754
755
756
757
758
759
760



























761
762
763
------------------------------------------------------------------------
-- comparetotal.decTest -- decimal comparison using total ordering    --
-- Copyright (c) IBM Corporation, 1981, 2006.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.52

-- Note that we cannot assume add/subtract tests cover paths adequately,
-- here, because the code might be quite different (comparison cannot
-- overflow or underflow, so actual subtractions are not necessary).
-- Similarly, comparetotal will have some radically different paths
-- than compare.

................................................................................
maxexponent: 999999999
minexponent: -999999999
cotx090 comparetotal  9.99999999E+999999999 9.99999999E+999999999  -> 0
cotx091 comparetotal -9.99999999E+999999999 9.99999999E+999999999  -> -1
cotx092 comparetotal  9.99999999E+999999999 -9.99999999E+999999999 -> 1
cotx093 comparetotal -9.99999999E+999999999 -9.99999999E+999999999 -> 0









-- some differing length/exponent cases
-- in this first group, compare would compare all equal
cotx100 comparetotal   7.0    7.0    -> 0
cotx101 comparetotal   7.0    7      -> -1
cotx102 comparetotal   7      7.0    -> 1
cotx103 comparetotal   7E+0   7.0    -> 1
cotx104 comparetotal   70E-1  7.0    -> 0
................................................................................
cotx1103 comparetotal -1e+777777777  1e+411111111 -> -1
cotx1104 comparetotal -1e+777777777 -1e+411111111 -> -1
cotx1105 comparetotal  1e-777777777  1e-411111111 -> -1
cotx1106 comparetotal  1e-777777777 -1e-411111111 ->  1
cotx1107 comparetotal -1e-777777777  1e-411111111 -> -1
cotx1108 comparetotal -1e-777777777 -1e-411111111 ->  1




























-- Null tests
cotx9990 comparetotal 10  # -> NaN Invalid_operation
cotx9991 comparetotal  # 10 -> NaN Invalid_operation


|







 







|







 







>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



1
2
3
4
5
6
7
8
9
10
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
...
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
------------------------------------------------------------------------
-- comparetotal.decTest -- decimal comparison using total ordering    --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
................................................................................
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

-- Note that we cannot assume add/subtract tests cover paths adequately,
-- here, because the code might be quite different (comparison cannot
-- overflow or underflow, so actual subtractions are not necessary).
-- Similarly, comparetotal will have some radically different paths
-- than compare.

................................................................................
maxexponent: 999999999
minexponent: -999999999
cotx090 comparetotal  9.99999999E+999999999 9.99999999E+999999999  -> 0
cotx091 comparetotal -9.99999999E+999999999 9.99999999E+999999999  -> -1
cotx092 comparetotal  9.99999999E+999999999 -9.99999999E+999999999 -> 1
cotx093 comparetotal -9.99999999E+999999999 -9.99999999E+999999999 -> 0

-- Examples
cotx094 comparetotal  12.73  127.9  -> -1
cotx095 comparetotal  -127   12     -> -1
cotx096 comparetotal  12.30  12.3   -> -1
cotx097 comparetotal  12.30  12.30  ->  0
cotx098 comparetotal  12.3   12.300 ->  1
cotx099 comparetotal  12.3   NaN    -> -1

-- some differing length/exponent cases
-- in this first group, compare would compare all equal
cotx100 comparetotal   7.0    7.0    -> 0
cotx101 comparetotal   7.0    7      -> -1
cotx102 comparetotal   7      7.0    -> 1
cotx103 comparetotal   7E+0   7.0    -> 1
cotx104 comparetotal   70E-1  7.0    -> 0
................................................................................
cotx1103 comparetotal -1e+777777777  1e+411111111 -> -1
cotx1104 comparetotal -1e+777777777 -1e+411111111 -> -1
cotx1105 comparetotal  1e-777777777  1e-411111111 -> -1
cotx1106 comparetotal  1e-777777777 -1e-411111111 ->  1
cotx1107 comparetotal -1e-777777777  1e-411111111 -> -1
cotx1108 comparetotal -1e-777777777 -1e-411111111 ->  1

-- spread zeros
cotx1110 comparetotal   0E-383  0       -> -1
cotx1111 comparetotal   0E-383 -0       ->  1
cotx1112 comparetotal  -0E-383  0       -> -1
cotx1113 comparetotal  -0E-383 -0       ->  1
cotx1114 comparetotal   0E-383  0E+384  -> -1
cotx1115 comparetotal   0E-383 -0E+384  ->  1
cotx1116 comparetotal  -0E-383  0E+384  -> -1
cotx1117 comparetotal  -0E-383 -0E+384  ->  1
cotx1118 comparetotal   0       0E+384  -> -1
cotx1119 comparetotal   0      -0E+384  ->  1
cotx1120 comparetotal  -0       0E+384  -> -1
cotx1121 comparetotal  -0      -0E+384  ->  1

cotx1130 comparetotal   0E+384  0       ->  1
cotx1131 comparetotal   0E+384 -0       ->  1
cotx1132 comparetotal  -0E+384  0       -> -1
cotx1133 comparetotal  -0E+384 -0       -> -1
cotx1134 comparetotal   0E+384  0E-383  ->  1
cotx1135 comparetotal   0E+384 -0E-383  ->  1
cotx1136 comparetotal  -0E+384  0E-383  -> -1
cotx1137 comparetotal  -0E+384 -0E-383  -> -1
cotx1138 comparetotal   0       0E-383  ->  1
cotx1139 comparetotal   0      -0E-383  ->  1
cotx1140 comparetotal  -0       0E-383  -> -1
cotx1141 comparetotal  -0      -0E-383  -> -1

-- Null tests
cotx9990 comparetotal 10  # -> NaN Invalid_operation
cotx9991 comparetotal  # 10 -> NaN Invalid_operation

Added test/dectest/comparetotmag.decTest.













































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
------------------------------------------------------------------------
-- comparetotmag.decTest -- decimal comparison, abs. total ordering   --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
-- particular, achieving the same results as the tests here is not    --
-- a guarantee that an implementation complies with any Standard      --
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

-- Note that it cannot be assumed that add/subtract tests cover paths
-- for this operation adequately, here, because the code might be
-- quite different (comparison cannot overflow or underflow, so
-- actual subtractions are not necessary). Similarly, comparetotal
-- will have some radically different paths than compare.

extended:    1
precision:   16
rounding:    half_up
maxExponent: 384
minExponent: -383

-- sanity checks
ctmx001 comparetotmag  -2  -2   ->   0
ctmx002 comparetotmag  -2  -1   ->   1
ctmx003 comparetotmag  -2   0   ->   1
ctmx004 comparetotmag  -2   1   ->   1
ctmx005 comparetotmag  -2   2   ->   0
ctmx006 comparetotmag  -1  -2   ->  -1
ctmx007 comparetotmag  -1  -1   ->   0
ctmx008 comparetotmag  -1   0   ->   1
ctmx009 comparetotmag  -1   1   ->   0
ctmx010 comparetotmag  -1   2   ->  -1
ctmx011 comparetotmag   0  -2   ->  -1
ctmx012 comparetotmag   0  -1   ->  -1
ctmx013 comparetotmag   0   0   ->   0
ctmx014 comparetotmag   0   1   ->  -1
ctmx015 comparetotmag   0   2   ->  -1
ctmx016 comparetotmag   1  -2   ->  -1
ctmx017 comparetotmag   1  -1   ->   0
ctmx018 comparetotmag   1   0   ->   1
ctmx019 comparetotmag   1   1   ->   0
ctmx020 comparetotmag   1   2   ->  -1
ctmx021 comparetotmag   2  -2   ->   0
ctmx022 comparetotmag   2  -1   ->   1
ctmx023 comparetotmag   2   0   ->   1
ctmx025 comparetotmag   2   1   ->   1
ctmx026 comparetotmag   2   2   ->   0

ctmx031 comparetotmag  -20  -20   ->   0
ctmx032 comparetotmag  -20  -10   ->   1
ctmx033 comparetotmag  -20   00   ->   1
ctmx034 comparetotmag  -20   10   ->   1
ctmx035 comparetotmag  -20   20   ->   0
ctmx036 comparetotmag  -10  -20   ->  -1
ctmx037 comparetotmag  -10  -10   ->   0
ctmx038 comparetotmag  -10   00   ->   1
ctmx039 comparetotmag  -10   10   ->   0
ctmx040 comparetotmag  -10   20   ->  -1
ctmx041 comparetotmag   00  -20   ->  -1
ctmx042 comparetotmag   00  -10   ->  -1
ctmx043 comparetotmag   00   00   ->   0
ctmx044 comparetotmag   00   10   ->  -1
ctmx045 comparetotmag   00   20   ->  -1
ctmx046 comparetotmag   10  -20   ->  -1
ctmx047 comparetotmag   10  -10   ->   0
ctmx048 comparetotmag   10   00   ->   1
ctmx049 comparetotmag   10   10   ->   0
ctmx050 comparetotmag   10   20   ->  -1
ctmx051 comparetotmag   20  -20   ->   0
ctmx052 comparetotmag   20  -10   ->   1
ctmx053 comparetotmag   20   00   ->   1
ctmx055 comparetotmag   20   10   ->   1
ctmx056 comparetotmag   20   20   ->   0

ctmx061 comparetotmag  -2.0  -2.0   ->   0
ctmx062 comparetotmag  -2.0  -1.0   ->   1
ctmx063 comparetotmag  -2.0   0.0   ->   1
ctmx064 comparetotmag  -2.0   1.0   ->   1
ctmx065 comparetotmag  -2.0   2.0   ->   0
ctmx066 comparetotmag  -1.0  -2.0   ->  -1
ctmx067 comparetotmag  -1.0  -1.0   ->   0
ctmx068 comparetotmag  -1.0   0.0   ->   1
ctmx069 comparetotmag  -1.0   1.0   ->   0
ctmx070 comparetotmag  -1.0   2.0   ->  -1
ctmx071 comparetotmag   0.0  -2.0   ->  -1
ctmx072 comparetotmag   0.0  -1.0   ->  -1
ctmx073 comparetotmag   0.0   0.0   ->   0
ctmx074 comparetotmag   0.0   1.0   ->  -1
ctmx075 comparetotmag   0.0   2.0   ->  -1
ctmx076 comparetotmag   1.0  -2.0   ->  -1
ctmx077 comparetotmag   1.0  -1.0   ->   0
ctmx078 comparetotmag   1.0   0.0   ->   1
ctmx079 comparetotmag   1.0   1.0   ->   0
ctmx080 comparetotmag   1.0   2.0   ->  -1
ctmx081 comparetotmag   2.0  -2.0   ->   0
ctmx082 comparetotmag   2.0  -1.0   ->   1
ctmx083 comparetotmag   2.0   0.0   ->   1
ctmx085 comparetotmag   2.0   1.0   ->   1
ctmx086 comparetotmag   2.0   2.0   ->   0

-- now some cases which might overflow if subtract were used
maxexponent: 999999999
minexponent: -999999999
ctmx090 comparetotmag  9.99999999E+999999999 9.99999999E+999999999   ->   0
ctmx091 comparetotmag -9.99999999E+999999999 9.99999999E+999999999   ->   0
ctmx092 comparetotmag  9.99999999E+999999999 -9.99999999E+999999999  ->   0
ctmx093 comparetotmag -9.99999999E+999999999 -9.99999999E+999999999  ->   0

-- some differing length/exponent cases
-- in this first group, compare would compare all equal
ctmx100 comparetotmag   7.0    7.0     ->   0
ctmx101 comparetotmag   7.0    7       ->  -1
ctmx102 comparetotmag   7      7.0     ->   1
ctmx103 comparetotmag   7E+0   7.0     ->   1
ctmx104 comparetotmag   70E-1  7.0     ->   0
ctmx105 comparetotmag   0.7E+1 7       ->   0
ctmx106 comparetotmag   70E-1  7       ->  -1
ctmx107 comparetotmag   7.0    7E+0    ->  -1
ctmx108 comparetotmag   7.0    70E-1   ->   0
ctmx109 comparetotmag   7      0.7E+1  ->   0
ctmx110 comparetotmag   7      70E-1   ->   1

ctmx120 comparetotmag   8.0    7.0     ->   1
ctmx121 comparetotmag   8.0    7       ->   1
ctmx122 comparetotmag   8      7.0     ->   1
ctmx123 comparetotmag   8E+0   7.0     ->   1
ctmx124 comparetotmag   80E-1  7.0     ->   1
ctmx125 comparetotmag   0.8E+1 7       ->   1
ctmx126 comparetotmag   80E-1  7       ->   1
ctmx127 comparetotmag   8.0    7E+0    ->   1
ctmx128 comparetotmag   8.0    70E-1   ->   1
ctmx129 comparetotmag   8      0.7E+1   ->   1
ctmx130 comparetotmag   8      70E-1   ->   1

ctmx140 comparetotmag   8.0    9.0     ->  -1
ctmx141 comparetotmag   8.0    9       ->  -1
ctmx142 comparetotmag   8      9.0     ->  -1
ctmx143 comparetotmag   8E+0   9.0     ->  -1
ctmx144 comparetotmag   80E-1  9.0     ->  -1
ctmx145 comparetotmag   0.8E+1 9       ->  -1
ctmx146 comparetotmag   80E-1  9       ->  -1
ctmx147 comparetotmag   8.0    9E+0    ->  -1
ctmx148 comparetotmag   8.0    90E-1   ->  -1
ctmx149 comparetotmag   8      0.9E+1  ->  -1
ctmx150 comparetotmag   8      90E-1   ->  -1

-- and again, with sign changes -+ ..
ctmx200 comparetotmag  -7.0    7.0     ->   0
ctmx201 comparetotmag  -7.0    7       ->  -1
ctmx202 comparetotmag  -7      7.0     ->   1
ctmx203 comparetotmag  -7E+0   7.0     ->   1
ctmx204 comparetotmag  -70E-1  7.0     ->   0
ctmx205 comparetotmag  -0.7E+1 7       ->   0
ctmx206 comparetotmag  -70E-1  7       ->  -1
ctmx207 comparetotmag  -7.0    7E+0    ->  -1
ctmx208 comparetotmag  -7.0    70E-1   ->   0
ctmx209 comparetotmag  -7      0.7E+1  ->   0
ctmx210 comparetotmag  -7      70E-1   ->   1

ctmx220 comparetotmag  -8.0    7.0     ->   1
ctmx221 comparetotmag  -8.0    7       ->   1
ctmx222 comparetotmag  -8      7.0     ->   1
ctmx223 comparetotmag  -8E+0   7.0     ->   1
ctmx224 comparetotmag  -80E-1  7.0     ->   1
ctmx225 comparetotmag  -0.8E+1 7       ->   1
ctmx226 comparetotmag  -80E-1  7       ->   1
ctmx227 comparetotmag  -8.0    7E+0    ->   1
ctmx228 comparetotmag  -8.0    70E-1   ->   1
ctmx229 comparetotmag  -8      0.7E+1  ->   1
ctmx230 comparetotmag  -8      70E-1   ->   1

ctmx240 comparetotmag  -8.0    9.0     ->  -1
ctmx241 comparetotmag  -8.0    9       ->  -1
ctmx242 comparetotmag  -8      9.0     ->  -1
ctmx243 comparetotmag  -8E+0   9.0     ->  -1
ctmx244 comparetotmag  -80E-1  9.0     ->  -1
ctmx245 comparetotmag  -0.8E+1 9       ->  -1
ctmx246 comparetotmag  -80E-1  9       ->  -1
ctmx247 comparetotmag  -8.0    9E+0    ->  -1
ctmx248 comparetotmag  -8.0    90E-1   ->  -1
ctmx249 comparetotmag  -8      0.9E+1  ->  -1
ctmx250 comparetotmag  -8      90E-1   ->  -1

-- and again, with sign changes +- ..
ctmx300 comparetotmag   7.0    -7.0     ->   0
ctmx301 comparetotmag   7.0    -7       ->  -1
ctmx302 comparetotmag   7      -7.0     ->   1
ctmx303 comparetotmag   7E+0   -7.0     ->   1
ctmx304 comparetotmag   70E-1  -7.0     ->   0
ctmx305 comparetotmag   .7E+1  -7       ->   0
ctmx306 comparetotmag   70E-1  -7       ->  -1
ctmx307 comparetotmag   7.0    -7E+0    ->  -1
ctmx308 comparetotmag   7.0    -70E-1   ->   0
ctmx309 comparetotmag   7      -.7E+1   ->   0
ctmx310 comparetotmag   7      -70E-1   ->   1

ctmx320 comparetotmag   8.0    -7.0     ->   1
ctmx321 comparetotmag   8.0    -7       ->   1
ctmx322 comparetotmag   8      -7.0     ->   1
ctmx323 comparetotmag   8E+0   -7.0     ->   1
ctmx324 comparetotmag   80E-1  -7.0     ->   1
ctmx325 comparetotmag   .8E+1  -7       ->   1
ctmx326 comparetotmag   80E-1  -7       ->   1
ctmx327 comparetotmag   8.0    -7E+0    ->   1
ctmx328 comparetotmag   8.0    -70E-1   ->   1
ctmx329 comparetotmag   8      -.7E+1   ->   1
ctmx330 comparetotmag   8      -70E-1   ->   1

ctmx340 comparetotmag   8.0    -9.0     ->  -1
ctmx341 comparetotmag   8.0    -9       ->  -1
ctmx342 comparetotmag   8      -9.0     ->  -1
ctmx343 comparetotmag   8E+0   -9.0     ->  -1
ctmx344 comparetotmag   80E-1  -9.0     ->  -1
ctmx345 comparetotmag   .8E+1  -9       ->  -1
ctmx346 comparetotmag   80E-1  -9       ->  -1
ctmx347 comparetotmag   8.0    -9E+0    ->  -1
ctmx348 comparetotmag   8.0    -90E-1   ->  -1
ctmx349 comparetotmag   8      -.9E+1   ->  -1
ctmx350 comparetotmag   8      -90E-1   ->  -1

-- and again, with sign changes -- ..
ctmx400 comparetotmag   -7.0    -7.0     ->   0
ctmx401 comparetotmag   -7.0    -7       ->  -1
ctmx402 comparetotmag   -7      -7.0     ->   1
ctmx403 comparetotmag   -7E+0   -7.0     ->   1
ctmx404 comparetotmag   -70E-1  -7.0     ->   0
ctmx405 comparetotmag   -.7E+1  -7       ->   0
ctmx406 comparetotmag   -70E-1  -7       ->  -1
ctmx407 comparetotmag   -7.0    -7E+0    ->  -1
ctmx408 comparetotmag   -7.0    -70E-1   ->   0
ctmx409 comparetotmag   -7      -.7E+1   ->   0
ctmx410 comparetotmag   -7      -70E-1   ->   1

ctmx420 comparetotmag   -8.0    -7.0     ->   1
ctmx421 comparetotmag   -8.0    -7       ->   1
ctmx422 comparetotmag   -8      -7.0     ->   1
ctmx423 comparetotmag   -8E+0   -7.0     ->   1
ctmx424 comparetotmag   -80E-1  -7.0     ->   1
ctmx425 comparetotmag   -.8E+1  -7       ->   1
ctmx426 comparetotmag   -80E-1  -7       ->   1
ctmx427 comparetotmag   -8.0    -7E+0    ->   1
ctmx428 comparetotmag   -8.0    -70E-1   ->   1
ctmx429 comparetotmag   -8      -.7E+1   ->   1
ctmx430 comparetotmag   -8      -70E-1   ->   1

ctmx440 comparetotmag   -8.0    -9.0     ->  -1
ctmx441 comparetotmag   -8.0    -9       ->  -1
ctmx442 comparetotmag   -8      -9.0     ->  -1
ctmx443 comparetotmag   -8E+0   -9.0     ->  -1
ctmx444 comparetotmag   -80E-1  -9.0     ->  -1
ctmx445 comparetotmag   -.8E+1  -9       ->  -1
ctmx446 comparetotmag   -80E-1  -9       ->  -1
ctmx447 comparetotmag   -8.0    -9E+0    ->  -1
ctmx448 comparetotmag   -8.0    -90E-1   ->  -1
ctmx449 comparetotmag   -8      -.9E+1   ->  -1
ctmx450 comparetotmag   -8      -90E-1   ->  -1


-- testcases that subtract to lots of zeros at boundaries [pgr]
precision: 40
ctmx470 comparetotmag 123.4560000000000000E789 123.456E789  ->  -1
ctmx471 comparetotmag 123.456000000000000E-89 123.456E-89  ->  -1
ctmx472 comparetotmag 123.45600000000000E789 123.456E789  ->  -1
ctmx473 comparetotmag 123.4560000000000E-89 123.456E-89  ->  -1
ctmx474 comparetotmag 123.456000000000E789 123.456E789  ->  -1
ctmx475 comparetotmag 123.45600000000E-89 123.456E-89  ->  -1
ctmx476 comparetotmag 123.4560000000E789 123.456E789  ->  -1
ctmx477 comparetotmag 123.456000000E-89 123.456E-89  ->  -1
ctmx478 comparetotmag 123.45600000E789 123.456E789  ->  -1
ctmx479 comparetotmag 123.4560000E-89 123.456E-89  ->  -1
ctmx480 comparetotmag 123.456000E789 123.456E789  ->  -1
ctmx481 comparetotmag 123.45600E-89 123.456E-89  ->  -1
ctmx482 comparetotmag 123.4560E789 123.456E789  ->  -1
ctmx483 comparetotmag 123.456E-89 123.456E-89  ->   0
ctmx484 comparetotmag 123.456E-89 123.4560000000000000E-89  ->   1
ctmx485 comparetotmag 123.456E789 123.456000000000000E789  ->   1
ctmx486 comparetotmag 123.456E-89 123.45600000000000E-89  ->   1
ctmx487 comparetotmag 123.456E789 123.4560000000000E789  ->   1
ctmx488 comparetotmag 123.456E-89 123.456000000000E-89  ->   1
ctmx489 comparetotmag 123.456E789 123.45600000000E789  ->   1
ctmx490 comparetotmag 123.456E-89 123.4560000000E-89  ->   1
ctmx491 comparetotmag 123.456E789 123.456000000E789  ->   1
ctmx492 comparetotmag 123.456E-89 123.45600000E-89  ->   1
ctmx493 comparetotmag 123.456E789 123.4560000E789  ->   1
ctmx494 comparetotmag 123.456E-89 123.456000E-89  ->   1
ctmx495 comparetotmag 123.456E789 123.45600E789  ->   1
ctmx496 comparetotmag 123.456E-89 123.4560E-89  ->   1
ctmx497 comparetotmag 123.456E789 123.456E789  ->   0

-- wide-ranging, around precision; signs equal
precision: 9
ctmx500 comparetotmag    1     1E-15     ->   1
ctmx501 comparetotmag    1     1E-14     ->   1
ctmx502 comparetotmag    1     1E-13     ->   1
ctmx503 comparetotmag    1     1E-12     ->   1
ctmx504 comparetotmag    1     1E-11     ->   1
ctmx505 comparetotmag    1     1E-10     ->   1
ctmx506 comparetotmag    1     1E-9      ->   1
ctmx507 comparetotmag    1     1E-8      ->   1
ctmx508 comparetotmag    1     1E-7      ->   1
ctmx509 comparetotmag    1     1E-6      ->   1
ctmx510 comparetotmag    1     1E-5      ->   1
ctmx511 comparetotmag    1     1E-4      ->   1
ctmx512 comparetotmag    1     1E-3      ->   1
ctmx513 comparetotmag    1     1E-2      ->   1
ctmx514 comparetotmag    1     1E-1      ->   1
ctmx515 comparetotmag    1     1E-0      ->   0
ctmx516 comparetotmag    1     1E+1      ->  -1
ctmx517 comparetotmag    1     1E+2      ->  -1
ctmx518 comparetotmag    1     1E+3      ->  -1
ctmx519 comparetotmag    1     1E+4      ->  -1
ctmx521 comparetotmag    1     1E+5      ->  -1
ctmx522 comparetotmag    1     1E+6      ->  -1
ctmx523 comparetotmag    1     1E+7      ->  -1
ctmx524 comparetotmag    1     1E+8      ->  -1
ctmx525 comparetotmag    1     1E+9      ->  -1
ctmx526 comparetotmag    1     1E+10     ->  -1
ctmx527 comparetotmag    1     1E+11     ->  -1
ctmx528 comparetotmag    1     1E+12     ->  -1
ctmx529 comparetotmag    1     1E+13     ->  -1
ctmx530 comparetotmag    1     1E+14     ->  -1
ctmx531 comparetotmag    1     1E+15     ->  -1
-- LR swap
ctmx540 comparetotmag    1E-15  1        ->  -1
ctmx541 comparetotmag    1E-14  1        ->  -1
ctmx542 comparetotmag    1E-13  1        ->  -1
ctmx543 comparetotmag    1E-12  1        ->  -1
ctmx544 comparetotmag    1E-11  1        ->  -1
ctmx545 comparetotmag    1E-10  1        ->  -1
ctmx546 comparetotmag    1E-9   1        ->  -1
ctmx547 comparetotmag    1E-8   1        ->  -1
ctmx548 comparetotmag    1E-7   1        ->  -1
ctmx549 comparetotmag    1E-6   1        ->  -1
ctmx550 comparetotmag    1E-5   1        ->  -1
ctmx551 comparetotmag    1E-4   1        ->  -1
ctmx552 comparetotmag    1E-3   1        ->  -1
ctmx553 comparetotmag    1E-2   1        ->  -1
ctmx554 comparetotmag    1E-1   1        ->  -1
ctmx555 comparetotmag    1E-0   1        ->   0
ctmx556 comparetotmag    1E+1   1        ->   1
ctmx557 comparetotmag    1E+2   1        ->   1
ctmx558 comparetotmag    1E+3   1        ->   1
ctmx559 comparetotmag    1E+4   1        ->   1
ctmx561 comparetotmag    1E+5   1        ->   1
ctmx562 comparetotmag    1E+6   1        ->   1
ctmx563 comparetotmag    1E+7   1        ->   1
ctmx564 comparetotmag    1E+8   1        ->   1
ctmx565 comparetotmag    1E+9   1        ->   1
ctmx566 comparetotmag    1E+10  1        ->   1
ctmx567 comparetotmag    1E+11  1        ->   1
ctmx568 comparetotmag    1E+12  1        ->   1
ctmx569 comparetotmag    1E+13  1        ->   1
ctmx570 comparetotmag    1E+14  1        ->   1
ctmx571 comparetotmag    1E+15  1        ->   1
-- similar with an useful coefficient, one side only
ctmx580 comparetotmag  0.000000987654321     1E-15     ->   1
ctmx581 comparetotmag  0.000000987654321     1E-14     ->   1
ctmx582 comparetotmag  0.000000987654321     1E-13     ->   1
ctmx583 comparetotmag  0.000000987654321     1E-12     ->   1
ctmx584 comparetotmag  0.000000987654321     1E-11     ->   1
ctmx585 comparetotmag  0.000000987654321     1E-10     ->   1
ctmx586 comparetotmag  0.000000987654321     1E-9      ->   1
ctmx587 comparetotmag  0.000000987654321     1E-8      ->   1
ctmx588 comparetotmag  0.000000987654321     1E-7      ->   1
ctmx589 comparetotmag  0.000000987654321     1E-6      ->  -1
ctmx590 comparetotmag  0.000000987654321     1E-5      ->  -1
ctmx591 comparetotmag  0.000000987654321     1E-4      ->  -1
ctmx592 comparetotmag  0.000000987654321     1E-3      ->  -1
ctmx593 comparetotmag  0.000000987654321     1E-2      ->  -1
ctmx594 comparetotmag  0.000000987654321     1E-1      ->  -1
ctmx595 comparetotmag  0.000000987654321     1E-0      ->  -1
ctmx596 comparetotmag  0.000000987654321     1E+1      ->  -1
ctmx597 comparetotmag  0.000000987654321     1E+2      ->  -1
ctmx598 comparetotmag  0.000000987654321     1E+3      ->  -1
ctmx599 comparetotmag  0.000000987654321     1E+4      ->  -1

-- check some unit-y traps
precision: 20
ctmx600 comparetotmag   12            12.2345  ->  -1
ctmx601 comparetotmag   12.0          12.2345  ->  -1
ctmx602 comparetotmag   12.00         12.2345  ->  -1
ctmx603 comparetotmag   12.000        12.2345  ->  -1
ctmx604 comparetotmag   12.0000       12.2345  ->  -1
ctmx605 comparetotmag   12.00000      12.2345  ->  -1
ctmx606 comparetotmag   12.000000     12.2345  ->  -1
ctmx607 comparetotmag   12.0000000    12.2345  ->  -1
ctmx608 comparetotmag   12.00000000   12.2345  ->  -1
ctmx609 comparetotmag   12.000000000  12.2345  ->  -1
ctmx610 comparetotmag   12.1234 12             ->   1
ctmx611 comparetotmag   12.1234 12.0           ->   1
ctmx612 comparetotmag   12.1234 12.00          ->   1
ctmx613 comparetotmag   12.1234 12.000         ->   1
ctmx614 comparetotmag   12.1234 12.0000        ->   1
ctmx615 comparetotmag   12.1234 12.00000       ->   1
ctmx616 comparetotmag   12.1234 12.000000      ->   1
ctmx617 comparetotmag   12.1234 12.0000000     ->   1
ctmx618 comparetotmag   12.1234 12.00000000    ->   1
ctmx619 comparetotmag   12.1234 12.000000000   ->   1
ctmx620 comparetotmag  -12           -12.2345  ->  -1
ctmx621 comparetotmag  -12.0         -12.2345  ->  -1
ctmx622 comparetotmag  -12.00        -12.2345  ->  -1
ctmx623 comparetotmag  -12.000       -12.2345  ->  -1
ctmx624 comparetotmag  -12.0000      -12.2345  ->  -1
ctmx625 comparetotmag  -12.00000     -12.2345  ->  -1
ctmx626 comparetotmag  -12.000000    -12.2345  ->  -1
ctmx627 comparetotmag  -12.0000000   -12.2345  ->  -1
ctmx628 comparetotmag  -12.00000000  -12.2345  ->  -1
ctmx629 comparetotmag  -12.000000000 -12.2345  ->  -1
ctmx630 comparetotmag  -12.1234 -12            ->   1
ctmx631 comparetotmag  -12.1234 -12.0          ->   1
ctmx632 comparetotmag  -12.1234 -12.00         ->   1
ctmx633 comparetotmag  -12.1234 -12.000        ->   1
ctmx634 comparetotmag  -12.1234 -12.0000       ->   1
ctmx635 comparetotmag  -12.1234 -12.00000      ->   1
ctmx636 comparetotmag  -12.1234 -12.000000     ->   1
ctmx637 comparetotmag  -12.1234 -12.0000000    ->   1
ctmx638 comparetotmag  -12.1234 -12.00000000   ->   1
ctmx639 comparetotmag  -12.1234 -12.000000000  ->   1
precision: 9

-- extended zeros
ctmx640 comparetotmag   0     0    ->   0
ctmx641 comparetotmag   0    -0    ->   0
ctmx642 comparetotmag   0    -0.0  ->   1
ctmx643 comparetotmag   0     0.0  ->   1
ctmx644 comparetotmag  -0     0    ->   0
ctmx645 comparetotmag  -0    -0    ->   0
ctmx646 comparetotmag  -0    -0.0  ->   1
ctmx647 comparetotmag  -0     0.0  ->   1
ctmx648 comparetotmag   0.0   0    ->  -1
ctmx649 comparetotmag   0.0  -0    ->  -1
ctmx650 comparetotmag   0.0  -0.0  ->   0
ctmx651 comparetotmag   0.0   0.0  ->   0
ctmx652 comparetotmag  -0.0   0    ->  -1
ctmx653 comparetotmag  -0.0  -0    ->  -1
ctmx654 comparetotmag  -0.0  -0.0  ->   0
ctmx655 comparetotmag  -0.0   0.0  ->   0

ctmx656 comparetotmag  -0E1   0.0  ->   1
ctmx657 comparetotmag  -0E2   0.0  ->   1
ctmx658 comparetotmag   0E1   0.0  ->   1
ctmx659 comparetotmag   0E2   0.0  ->   1
ctmx660 comparetotmag  -0E1   0    ->   1
ctmx661 comparetotmag  -0E2   0    ->   1
ctmx662 comparetotmag   0E1   0    ->   1
ctmx663 comparetotmag   0E2   0    ->   1
ctmx664 comparetotmag  -0E1  -0E1  ->   0
ctmx665 comparetotmag  -0E2  -0E1  ->   1
ctmx666 comparetotmag   0E1  -0E1  ->   0
ctmx667 comparetotmag   0E2  -0E1  ->   1
ctmx668 comparetotmag  -0E1  -0E2  ->  -1
ctmx669 comparetotmag  -0E2  -0E2  ->   0
ctmx670 comparetotmag   0E1  -0E2  ->  -1
ctmx671 comparetotmag   0E2  -0E2  ->   0
ctmx672 comparetotmag  -0E1   0E1  ->   0
ctmx673 comparetotmag  -0E2   0E1  ->   1
ctmx674 comparetotmag   0E1   0E1  ->   0
ctmx675 comparetotmag   0E2   0E1  ->   1
ctmx676 comparetotmag  -0E1   0E2  ->  -1
ctmx677 comparetotmag  -0E2   0E2  ->   0
ctmx678 comparetotmag   0E1   0E2  ->  -1
ctmx679 comparetotmag   0E2   0E2  ->   0

-- trailing zeros; unit-y
precision: 20
ctmx680 comparetotmag   12    12            ->   0
ctmx681 comparetotmag   12    12.0          ->   1
ctmx682 comparetotmag   12    12.00         ->   1
ctmx683 comparetotmag   12    12.000        ->   1
ctmx684 comparetotmag   12    12.0000       ->   1
ctmx685 comparetotmag   12    12.00000      ->   1
ctmx686 comparetotmag   12    12.000000     ->   1
ctmx687 comparetotmag   12    12.0000000    ->   1
ctmx688 comparetotmag   12    12.00000000   ->   1
ctmx689 comparetotmag   12    12.000000000  ->   1
ctmx690 comparetotmag   12              12  ->   0
ctmx691 comparetotmag   12.0            12  ->  -1
ctmx692 comparetotmag   12.00           12  ->  -1
ctmx693 comparetotmag   12.000          12  ->  -1
ctmx694 comparetotmag   12.0000         12  ->  -1
ctmx695 comparetotmag   12.00000        12  ->  -1
ctmx696 comparetotmag   12.000000       12  ->  -1
ctmx697 comparetotmag   12.0000000      12  ->  -1
ctmx698 comparetotmag   12.00000000     12  ->  -1
ctmx699 comparetotmag   12.000000000    12  ->  -1

-- long operand checks
maxexponent: 999
minexponent: -999
precision: 9
ctmx701 comparetotmag 12345678000  1  ->   1
ctmx702 comparetotmag 1 12345678000   ->  -1
ctmx703 comparetotmag 1234567800   1  ->   1
ctmx704 comparetotmag 1 1234567800    ->  -1
ctmx705 comparetotmag 1234567890   1  ->   1
ctmx706 comparetotmag 1 1234567890    ->  -1
ctmx707 comparetotmag 1234567891   1  ->   1
ctmx708 comparetotmag 1 1234567891    ->  -1
ctmx709 comparetotmag 12345678901  1  ->   1
ctmx710 comparetotmag 1 12345678901   ->  -1
ctmx711 comparetotmag 1234567896   1  ->   1
ctmx712 comparetotmag 1 1234567896    ->  -1
ctmx713 comparetotmag -1234567891  1  ->   1
ctmx714 comparetotmag 1 -1234567891   ->  -1
ctmx715 comparetotmag -12345678901 1  ->   1
ctmx716 comparetotmag 1 -12345678901  ->  -1
ctmx717 comparetotmag -1234567896  1  ->   1
ctmx718 comparetotmag 1 -1234567896   ->  -1

precision: 15
-- same with plenty of precision
ctmx721 comparetotmag 12345678000 1  ->   1
ctmx722 comparetotmag 1 12345678000  ->  -1
ctmx723 comparetotmag 1234567800  1  ->   1
ctmx724 comparetotmag 1 1234567800   ->  -1
ctmx725 comparetotmag 1234567890  1  ->   1
ctmx726 comparetotmag 1 1234567890   ->  -1
ctmx727 comparetotmag 1234567891  1  ->   1
ctmx728 comparetotmag 1 1234567891   ->  -1
ctmx729 comparetotmag 12345678901 1  ->   1
ctmx730 comparetotmag 1 12345678901  ->  -1
ctmx731 comparetotmag 1234567896  1  ->   1
ctmx732 comparetotmag 1 1234567896   ->  -1

-- residue cases
precision: 5
ctmx740 comparetotmag  1  0.9999999   ->   1
ctmx741 comparetotmag  1  0.999999    ->   1
ctmx742 comparetotmag  1  0.99999     ->   1
ctmx743 comparetotmag  1  1.0000      ->   1
ctmx744 comparetotmag  1  1.00001     ->  -1
ctmx745 comparetotmag  1  1.000001    ->  -1
ctmx746 comparetotmag  1  1.0000001   ->  -1
ctmx750 comparetotmag  0.9999999  1   ->  -1
ctmx751 comparetotmag  0.999999   1   ->  -1
ctmx752 comparetotmag  0.99999    1   ->  -1
ctmx753 comparetotmag  1.0000     1   ->  -1
ctmx754 comparetotmag  1.00001    1   ->   1
ctmx755 comparetotmag  1.000001   1   ->   1
ctmx756 comparetotmag  1.0000001  1   ->   1

-- a selection of longies
ctmx760 comparetotmag -36852134.84194296250843579428931 -5830629.8347085025808756560357940  ->   1
ctmx761 comparetotmag -36852134.84194296250843579428931 -36852134.84194296250843579428931   ->   0
ctmx762 comparetotmag -36852134.94194296250843579428931 -36852134.84194296250843579428931   ->   1
ctmx763 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
-- precisions above or below the difference should have no effect
precision:   11
ctmx764 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
precision:   10
ctmx765 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
precision:    9
ctmx766 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
precision:    8
ctmx767 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
precision:    7
ctmx768 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
precision:    6
ctmx769 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
precision:    5
ctmx770 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
precision:    4
ctmx771 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
precision:    3
ctmx772 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
precision:    2
ctmx773 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1
precision:    1
ctmx774 comparetotmag -36852134.84194296250843579428931 -36852134.94194296250843579428931   ->  -1

-- Specials
precision:   9
ctmx780 comparetotmag  Inf  -Inf   ->  0
ctmx781 comparetotmag  Inf  -1000  ->  1
ctmx782 comparetotmag  Inf  -1     ->  1
ctmx783 comparetotmag  Inf  -0     ->  1
ctmx784 comparetotmag  Inf   0     ->  1
ctmx785 comparetotmag  Inf   1     ->  1
ctmx786 comparetotmag  Inf   1000  ->  1
ctmx787 comparetotmag  Inf   Inf   ->  0
ctmx788 comparetotmag -1000  Inf   -> -1
ctmx789 comparetotmag -Inf   Inf   ->  0
ctmx790 comparetotmag -1     Inf   -> -1
ctmx791 comparetotmag -0     Inf   -> -1
ctmx792 comparetotmag  0     Inf   -> -1
ctmx793 comparetotmag  1     Inf   -> -1
ctmx794 comparetotmag  1000  Inf   -> -1
ctmx795 comparetotmag  Inf   Inf   ->  0

ctmx800 comparetotmag -Inf  -Inf   ->  0
ctmx801 comparetotmag -Inf  -1000  ->  1
ctmx802 comparetotmag -Inf  -1     ->  1
ctmx803 comparetotmag -Inf  -0     ->  1
ctmx804 comparetotmag -Inf   0     ->  1
ctmx805 comparetotmag -Inf   1     ->  1
ctmx806 comparetotmag -Inf   1000  ->  1
ctmx807 comparetotmag -Inf   Inf   ->  0
ctmx808 comparetotmag -Inf  -Inf   ->  0
ctmx809 comparetotmag -1000 -Inf   -> -1
ctmx810 comparetotmag -1    -Inf   -> -1
ctmx811 comparetotmag -0    -Inf   -> -1
ctmx812 comparetotmag  0    -Inf   -> -1
ctmx813 comparetotmag  1    -Inf   -> -1
ctmx814 comparetotmag  1000 -Inf   -> -1
ctmx815 comparetotmag  Inf  -Inf   ->  0

ctmx821 comparetotmag  NaN -Inf    ->  1
ctmx822 comparetotmag  NaN -1000   ->  1
ctmx823 comparetotmag  NaN -1      ->  1
ctmx824 comparetotmag  NaN -0      ->  1
ctmx825 comparetotmag  NaN  0      ->  1
ctmx826 comparetotmag  NaN  1      ->  1
ctmx827 comparetotmag  NaN  1000   ->  1
ctmx828 comparetotmag  NaN  Inf    ->  1
ctmx829 comparetotmag  NaN  NaN    ->  0
ctmx830 comparetotmag -Inf  NaN    ->  -1
ctmx831 comparetotmag -1000 NaN    ->  -1
ctmx832 comparetotmag -1    NaN    ->  -1
ctmx833 comparetotmag -0    NaN    ->  -1
ctmx834 comparetotmag  0    NaN    ->  -1
ctmx835 comparetotmag  1    NaN    ->  -1
ctmx836 comparetotmag  1000 NaN    ->  -1
ctmx837 comparetotmag  Inf  NaN    ->  -1
ctmx838 comparetotmag -NaN -NaN    ->  0
ctmx839 comparetotmag +NaN -NaN    ->  0
ctmx840 comparetotmag -NaN +NaN    ->  0

ctmx841 comparetotmag  sNaN -sNaN  ->  0
ctmx842 comparetotmag  sNaN -NaN   ->  -1
ctmx843 comparetotmag  sNaN -Inf   ->  1
ctmx844 comparetotmag  sNaN -1000  ->  1
ctmx845 comparetotmag  sNaN -1     ->  1
ctmx846 comparetotmag  sNaN -0     ->  1
ctmx847 comparetotmag  sNaN  0     ->  1
ctmx848 comparetotmag  sNaN  1     ->  1
ctmx849 comparetotmag  sNaN  1000  ->  1
ctmx850 comparetotmag  sNaN  NaN   ->  -1
ctmx851 comparetotmag  sNaN sNaN   ->  0

ctmx852 comparetotmag -sNaN sNaN   ->  0
ctmx853 comparetotmag -NaN  sNaN   ->  1
ctmx854 comparetotmag -Inf  sNaN   ->  -1
ctmx855 comparetotmag -1000 sNaN   ->  -1
ctmx856 comparetotmag -1    sNaN   ->  -1
ctmx857 comparetotmag -0    sNaN   ->  -1
ctmx858 comparetotmag  0    sNaN   ->  -1
ctmx859 comparetotmag  1    sNaN   ->  -1
ctmx860 comparetotmag  1000 sNaN   ->  -1
ctmx861 comparetotmag  Inf  sNaN   ->  -1
ctmx862 comparetotmag  NaN  sNaN   ->  1
ctmx863 comparetotmag  sNaN sNaN   ->  0

ctmx871 comparetotmag  -sNaN -sNaN  ->  0
ctmx872 comparetotmag  -sNaN -NaN   ->  -1
ctmx873 comparetotmag  -sNaN -Inf   ->  1
ctmx874 comparetotmag  -sNaN -1000  ->  1
ctmx875 comparetotmag  -sNaN -1     ->  1
ctmx876 comparetotmag  -sNaN -0     ->  1
ctmx877 comparetotmag  -sNaN  0     ->  1
ctmx878 comparetotmag  -sNaN  1     ->  1
ctmx879 comparetotmag  -sNaN  1000  ->  1
ctmx880 comparetotmag  -sNaN  NaN   ->  -1
ctmx881 comparetotmag  -sNaN sNaN   ->  0

ctmx882 comparetotmag -sNaN -sNaN   ->  0
ctmx883 comparetotmag -NaN  -sNaN   ->  1
ctmx884 comparetotmag -Inf  -sNaN   ->  -1
ctmx885 comparetotmag -1000 -sNaN   ->  -1
ctmx886 comparetotmag -1    -sNaN   ->  -1
ctmx887 comparetotmag -0    -sNaN   ->  -1
ctmx888 comparetotmag  0    -sNaN   ->  -1
ctmx889 comparetotmag  1    -sNaN   ->  -1
ctmx890 comparetotmag  1000 -sNaN   ->  -1
ctmx891 comparetotmag  Inf  -sNaN   ->  -1
ctmx892 comparetotmag  NaN  -sNaN   ->  1
ctmx893 comparetotmag  sNaN -sNaN   ->  0

-- NaNs with payload
ctmx960 comparetotmag  NaN9 -Inf   ->  1
ctmx961 comparetotmag  NaN8  999   ->  1
ctmx962 comparetotmag  NaN77 Inf   ->  1
ctmx963 comparetotmag -NaN67 NaN5  ->  1
ctmx964 comparetotmag -Inf  -NaN4  ->  -1
ctmx965 comparetotmag -999  -NaN33 ->  -1
ctmx966 comparetotmag  Inf   NaN2  ->  -1

ctmx970 comparetotmag -NaN41 -NaN42 -> -1
ctmx971 comparetotmag +NaN41 -NaN42 -> -1
ctmx972 comparetotmag -NaN41 +NaN42 -> -1
ctmx973 comparetotmag +NaN41 +NaN42 -> -1
ctmx974 comparetotmag -NaN42 -NaN01 ->  1
ctmx975 comparetotmag +NaN42 -NaN01 ->  1
ctmx976 comparetotmag -NaN42 +NaN01 ->  1
ctmx977 comparetotmag +NaN42 +NaN01 ->  1

ctmx980 comparetotmag -sNaN771 -sNaN772 -> -1
ctmx981 comparetotmag +sNaN771 -sNaN772 -> -1
ctmx982 comparetotmag -sNaN771 +sNaN772 -> -1
ctmx983 comparetotmag +sNaN771 +sNaN772 -> -1
ctmx984 comparetotmag -sNaN772 -sNaN771 ->  1
ctmx985 comparetotmag +sNaN772 -sNaN771 ->  1
ctmx986 comparetotmag -sNaN772 +sNaN771 ->  1
ctmx987 comparetotmag +sNaN772 +sNaN771 ->  1

ctmx991 comparetotmag -sNaN99 -Inf    ->  1
ctmx992 comparetotmag  sNaN98 -11     ->  1
ctmx993 comparetotmag  sNaN97  NaN    -> -1
ctmx994 comparetotmag  sNaN16 sNaN94  -> -1
ctmx995 comparetotmag  NaN85  sNaN83  ->  1
ctmx996 comparetotmag -Inf    sNaN92  -> -1
ctmx997 comparetotmag  088    sNaN81  -> -1
ctmx998 comparetotmag  Inf    sNaN90  -> -1
ctmx999 comparetotmag  NaN   -sNaN89  ->  1

-- overflow and underflow tests .. subnormal results now allowed
maxExponent: 999999999
minexponent: -999999999
ctmx1080 comparetotmag +1.23456789012345E-0 9E+999999999  ->  -1
ctmx1081 comparetotmag 9E+999999999 +1.23456789012345E-0  ->   1
ctmx1082 comparetotmag +0.100 9E-999999999                ->   1
ctmx1083 comparetotmag 9E-999999999 +0.100                ->  -1
ctmx1085 comparetotmag -1.23456789012345E-0 9E+999999999  ->  -1
ctmx1086 comparetotmag 9E+999999999 -1.23456789012345E-0  ->   1
ctmx1087 comparetotmag -0.100 9E-999999999                ->   1
ctmx1088 comparetotmag 9E-999999999 -0.100                ->  -1

ctmx1089 comparetotmag 1e-599999999 1e-400000001    ->  -1
ctmx1090 comparetotmag 1e-599999999 1e-400000000    ->  -1
ctmx1091 comparetotmag 1e-600000000 1e-400000000    ->  -1
ctmx1092 comparetotmag 9e-999999998 0.01            ->  -1
ctmx1093 comparetotmag 9e-999999998 0.1             ->  -1
ctmx1094 comparetotmag 0.01 9e-999999998            ->   1
ctmx1095 comparetotmag 1e599999999 1e400000001      ->   1
ctmx1096 comparetotmag 1e599999999 1e400000000      ->   1
ctmx1097 comparetotmag 1e600000000 1e400000000      ->   1
ctmx1098 comparetotmag 9e999999998 100              ->   1
ctmx1099 comparetotmag 9e999999998 10               ->   1
ctmx1100 comparetotmag 100  9e999999998             ->  -1
-- signs
ctmx1101 comparetotmag  1e+777777777  1e+411111111  ->   1
ctmx1102 comparetotmag  1e+777777777 -1e+411111111  ->   1
ctmx1103 comparetotmag -1e+777777777  1e+411111111  ->   1
ctmx1104 comparetotmag -1e+777777777 -1e+411111111  ->   1
ctmx1105 comparetotmag  1e-777777777  1e-411111111  ->  -1
ctmx1106 comparetotmag  1e-777777777 -1e-411111111  ->  -1
ctmx1107 comparetotmag -1e-777777777  1e-411111111  ->  -1
ctmx1108 comparetotmag -1e-777777777 -1e-411111111  ->  -1

-- spread zeros
ctmx1110 comparetotmag   0E-383  0        ->  -1
ctmx1111 comparetotmag   0E-383 -0        ->  -1
ctmx1112 comparetotmag  -0E-383  0        ->  -1
ctmx1113 comparetotmag  -0E-383 -0        ->  -1
ctmx1114 comparetotmag   0E-383  0E+384   ->  -1
ctmx1115 comparetotmag   0E-383 -0E+384   ->  -1
ctmx1116 comparetotmag  -0E-383  0E+384   ->  -1
ctmx1117 comparetotmag  -0E-383 -0E+384   ->  -1
ctmx1118 comparetotmag   0       0E+384   ->  -1
ctmx1119 comparetotmag   0      -0E+384   ->  -1
ctmx1120 comparetotmag  -0       0E+384   ->  -1
ctmx1121 comparetotmag  -0      -0E+384   ->  -1

ctmx1130 comparetotmag   0E+384  0        ->   1
ctmx1131 comparetotmag   0E+384 -0        ->   1
ctmx1132 comparetotmag  -0E+384  0        ->   1
ctmx1133 comparetotmag  -0E+384 -0        ->   1
ctmx1134 comparetotmag   0E+384  0E-383   ->   1
ctmx1135 comparetotmag   0E+384 -0E-383   ->   1
ctmx1136 comparetotmag  -0E+384  0E-383   ->   1
ctmx1137 comparetotmag  -0E+384 -0E-383   ->   1
ctmx1138 comparetotmag   0       0E-383   ->   1
ctmx1139 comparetotmag   0      -0E-383   ->   1
ctmx1140 comparetotmag  -0       0E-383   ->   1
ctmx1141 comparetotmag  -0      -0E-383   ->   1

-- Null tests
ctmx9990 comparetotmag 10  # -> NaN Invalid_operation
ctmx9991 comparetotmag  # 10 -> NaN Invalid_operation

Added test/dectest/copy.decTest.













































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
------------------------------------------------------------------------
-- copy.decTest -- quiet copy                                         --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
-- particular, achieving the same results as the tests here is not    --
-- a guarantee that an implementation complies with any Standard      --
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

extended:    1
precision:   9
rounding:    half_up
maxExponent: 999
minExponent: -999

-- Sanity check
cpyx001 copy       +7.50  -> 7.50

-- Infinities
cpyx011 copy  Infinity    -> Infinity
cpyx012 copy  -Infinity   -> -Infinity

-- NaNs, 0 payload
cpyx021 copy         NaN  -> NaN
cpyx022 copy        -NaN  -> -NaN
cpyx023 copy        sNaN  -> sNaN
cpyx024 copy       -sNaN  -> -sNaN

-- NaNs, non-0 payload
cpyx031 copy       NaN10  -> NaN10
cpyx032 copy      -NaN10  -> -NaN10
cpyx033 copy      sNaN10  -> sNaN10
cpyx034 copy     -sNaN10  -> -sNaN10
cpyx035 copy       NaN7   -> NaN7
cpyx036 copy      -NaN7   -> -NaN7
cpyx037 copy      sNaN101 -> sNaN101
cpyx038 copy     -sNaN101 -> -sNaN101

-- finites
cpyx101 copy          7   -> 7
cpyx102 copy         -7   -> -7
cpyx103 copy         75   -> 75
cpyx104 copy        -75   -> -75
cpyx105 copy       7.50   -> 7.50
cpyx106 copy      -7.50   -> -7.50
cpyx107 copy       7.500  -> 7.500
cpyx108 copy      -7.500  -> -7.500

-- zeros
cpyx111 copy          0   -> 0
cpyx112 copy         -0   -> -0
cpyx113 copy       0E+4   -> 0E+4
cpyx114 copy      -0E+4   -> -0E+4
cpyx115 copy     0.0000   -> 0.0000
cpyx116 copy    -0.0000   -> -0.0000
cpyx117 copy      0E-141  -> 0E-141
cpyx118 copy     -0E-141  -> -0E-141

-- full coefficients, alternating bits
cpyx121 copy   268268268        -> 268268268
cpyx122 copy  -268268268        -> -268268268
cpyx123 copy   134134134        -> 134134134
cpyx124 copy  -134134134        -> -134134134

-- Nmax, Nmin, Ntiny
cpyx131 copy  9.99999999E+999   -> 9.99999999E+999
cpyx132 copy  1E-999            -> 1E-999
cpyx133 copy  1.00000000E-999   -> 1.00000000E-999
cpyx134 copy  1E-1007           -> 1E-1007

cpyx135 copy  -1E-1007          -> -1E-1007
cpyx136 copy  -1.00000000E-999  -> -1.00000000E-999
cpyx137 copy  -1E-999           -> -1E-999
cpyx138 copy  -9.99999999E+999  -> -9.99999999E+999

Added test/dectest/copyabs.decTest.













































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
------------------------------------------------------------------------
-- copyAbs.decTest -- quiet copy and set sign to zero                 --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
-- particular, achieving the same results as the tests here is not    --
-- a guarantee that an implementation complies with any Standard      --
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

extended:    1
precision:   9
rounding:    half_up
maxExponent: 999
minExponent: -999

-- Sanity check
cpax001 copyabs       +7.50  -> 7.50

-- Infinities
cpax011 copyabs  Infinity    -> Infinity
cpax012 copyabs  -Infinity   -> Infinity

-- NaNs, 0 payload
cpax021 copyabs         NaN  -> NaN
cpax022 copyabs        -NaN  -> NaN
cpax023 copyabs        sNaN  -> sNaN
cpax024 copyabs       -sNaN  -> sNaN

-- NaNs, non-0 payload
cpax031 copyabs       NaN10  -> NaN10
cpax032 copyabs      -NaN15  -> NaN15
cpax033 copyabs      sNaN15  -> sNaN15
cpax034 copyabs     -sNaN10  -> sNaN10
cpax035 copyabs       NaN7   -> NaN7
cpax036 copyabs      -NaN7   -> NaN7
cpax037 copyabs      sNaN101 -> sNaN101
cpax038 copyabs     -sNaN101 -> sNaN101

-- finites
cpax101 copyabs          7   -> 7
cpax102 copyabs         -7   -> 7
cpax103 copyabs         75   -> 75
cpax104 copyabs        -75   -> 75
cpax105 copyabs       7.10   -> 7.10
cpax106 copyabs      -7.10   -> 7.10
cpax107 copyabs       7.500  -> 7.500
cpax108 copyabs      -7.500  -> 7.500

-- zeros
cpax111 copyabs          0   -> 0
cpax112 copyabs         -0   -> 0
cpax113 copyabs       0E+6   -> 0E+6
cpax114 copyabs      -0E+6   -> 0E+6
cpax115 copyabs     0.0000   -> 0.0000
cpax116 copyabs    -0.0000   -> 0.0000
cpax117 copyabs      0E-141  -> 0E-141
cpax118 copyabs     -0E-141  -> 0E-141

-- full coefficients, alternating bits
cpax121 copyabs   268268268        -> 268268268
cpax122 copyabs  -268268268        -> 268268268
cpax123 copyabs   134134134        -> 134134134
cpax124 copyabs  -134134134        -> 134134134

-- Nmax, Nmin, Ntiny
cpax131 copyabs  9.99999999E+999   -> 9.99999999E+999
cpax132 copyabs  1E-999            -> 1E-999
cpax133 copyabs  1.00000000E-999   -> 1.00000000E-999
cpax134 copyabs  1E-1007           -> 1E-1007

cpax135 copyabs  -1E-1007          -> 1E-1007
cpax136 copyabs  -1.00000000E-999  -> 1.00000000E-999
cpax137 copyabs  -1E-999           -> 1E-999
cpax199 copyabs  -9.99999999E+999  -> 9.99999999E+999

Added test/dectest/copynegate.decTest.













































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
------------------------------------------------------------------------
-- copyNegate.decTest -- quiet copy and negate                        --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
-- particular, achieving the same results as the tests here is not    --
-- a guarantee that an implementation complies with any Standard      --
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

extended:    1
precision:   9
rounding:    half_up
maxExponent: 999
minExponent: -999

-- Sanity check
cpnx001 copynegate       +7.50  -> -7.50

-- Infinities
cpnx011 copynegate  Infinity    -> -Infinity
cpnx012 copynegate  -Infinity   -> Infinity

-- NaNs, 0 payload
cpnx021 copynegate         NaN  -> -NaN
cpnx022 copynegate        -NaN  -> NaN
cpnx023 copynegate        sNaN  -> -sNaN
cpnx024 copynegate       -sNaN  -> sNaN

-- NaNs, non-0 payload
cpnx031 copynegate       NaN13  -> -NaN13
cpnx032 copynegate      -NaN13  -> NaN13
cpnx033 copynegate      sNaN13  -> -sNaN13
cpnx034 copynegate     -sNaN13  -> sNaN13
cpnx035 copynegate       NaN70  -> -NaN70
cpnx036 copynegate      -NaN70  -> NaN70
cpnx037 copynegate      sNaN101 -> -sNaN101
cpnx038 copynegate     -sNaN101 -> sNaN101

-- finites
cpnx101 copynegate          7   -> -7
cpnx102 copynegate         -7   -> 7
cpnx103 copynegate         75   -> -75
cpnx104 copynegate        -75   -> 75
cpnx105 copynegate       7.50   -> -7.50
cpnx106 copynegate      -7.50   -> 7.50
cpnx107 copynegate       7.500  -> -7.500
cpnx108 copynegate      -7.500  -> 7.500

-- zeros
cpnx111 copynegate          0   -> -0
cpnx112 copynegate         -0   -> 0
cpnx113 copynegate       0E+4   -> -0E+4
cpnx114 copynegate      -0E+4   -> 0E+4
cpnx115 copynegate     0.0000   -> -0.0000
cpnx116 copynegate    -0.0000   -> 0.0000
cpnx117 copynegate      0E-141  -> -0E-141
cpnx118 copynegate     -0E-141  -> 0E-141

-- full coefficients, alternating bits
cpnx121 copynegate  268268268         -> -268268268
cpnx122 copynegate  -268268268        -> 268268268
cpnx123 copynegate  134134134         -> -134134134
cpnx124 copynegate  -134134134        -> 134134134

-- Nmax, Nmin, Ntiny
cpnx131 copynegate  9.99999999E+999   -> -9.99999999E+999
cpnx132 copynegate  1E-999                     -> -1E-999
cpnx133 copynegate  1.00000000E-999   -> -1.00000000E-999
cpnx134 copynegate  1E-1007                    -> -1E-1007

cpnx135 copynegate  -1E-1007                   -> 1E-1007
cpnx136 copynegate  -1.00000000E-999  -> 1.00000000E-999
cpnx137 copynegate  -1E-999                    -> 1E-999
cpnx138 copynegate  -9.99999999E+999  -> 9.99999999E+999

Added test/dectest/copysign.decTest.



































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
------------------------------------------------------------------------
-- copysign.decTest -- quiet copy with sign from rhs                  --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
-- particular, achieving the same results as the tests here is not    --
-- a guarantee that an implementation complies with any Standard      --
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

extended:    1
precision:   9
rounding:    half_up
maxExponent: 999
minExponent: -999

-- Sanity check, and examples from decArith
cpsx001 copysign   +7.50       11  -> 7.50
cpsx002 copysign   '1.50'   '7.33' -> 1.50
cpsx003 copysign  '-1.50'   '7.33' -> 1.50
cpsx004 copysign   '1.50'  '-7.33' -> -1.50
cpsx005 copysign  '-1.50'  '-7.33' -> -1.50

-- Infinities
cpsx011 copysign  Infinity       11 -> Infinity
cpsx012 copysign  -Infinity      11 -> Infinity

-- NaNs, 0 payload
cpsx021 copysign         NaN     11 -> NaN
cpsx022 copysign        -NaN     11 -> NaN
cpsx023 copysign        sNaN     11 -> sNaN
cpsx024 copysign       -sNaN     11 -> sNaN

-- NaNs, non-0 payload
cpsx031 copysign       NaN10     11 -> NaN10
cpsx032 copysign      -NaN10     11 -> NaN10
cpsx033 copysign      sNaN10     11 -> sNaN10
cpsx034 copysign     -sNaN10     11 -> sNaN10
cpsx035 copysign       NaN7      11 -> NaN7
cpsx036 copysign      -NaN7      11 -> NaN7
cpsx037 copysign      sNaN101    11 -> sNaN101
cpsx038 copysign     -sNaN101    11 -> sNaN101

-- finites
cpsx101 copysign          7      11 -> 7
cpsx102 copysign         -7      11 -> 7
cpsx103 copysign         75      11 -> 75
cpsx104 copysign        -75      11 -> 75
cpsx105 copysign       7.50      11 -> 7.50
cpsx106 copysign      -7.50      11 -> 7.50
cpsx107 copysign       7.500     11 -> 7.500
cpsx108 copysign      -7.500     11 -> 7.500

-- zeros
cpsx111 copysign          0      11 -> 0
cpsx112 copysign         -0      11 -> 0
cpsx113 copysign       0E+4      11 -> 0E+4
cpsx114 copysign      -0E+4      11 -> 0E+4
cpsx115 copysign     0.0000      11 -> 0.0000
cpsx116 copysign    -0.0000      11 -> 0.0000
cpsx117 copysign      0E-141     11 -> 0E-141
cpsx118 copysign     -0E-141     11 -> 0E-141

-- full coefficients, alternating bits
cpsx121 copysign   268268268           11 -> 268268268
cpsx122 copysign  -268268268           11 -> 268268268
cpsx123 copysign   134134134           11 -> 134134134
cpsx124 copysign  -134134134           11 -> 134134134

-- Nmax, Nmin, Ntiny
cpsx131 copysign  9.99999999E+999      11 -> 9.99999999E+999
cpsx132 copysign  1E-999               11 -> 1E-999
cpsx133 copysign  1.00000000E-999      11 -> 1.00000000E-999
cpsx134 copysign  1E-1007              11 -> 1E-1007

cpsx135 copysign  -1E-1007             11 -> 1E-1007
cpsx136 copysign  -1.00000000E-999     11 -> 1.00000000E-999
cpsx137 copysign  -1E-999              11 -> 1E-999
cpsx138 copysign  -9.99999999E+999     11 -> 9.99999999E+999

-- repeat with negative RHS

-- Infinities
cpsx211 copysign  Infinity       -34 -> -Infinity
cpsx212 copysign  -Infinity      -34 -> -Infinity

-- NaNs, 0 payload
cpsx221 copysign         NaN     -34 -> -NaN
cpsx222 copysign        -NaN     -34 -> -NaN
cpsx223 copysign        sNaN     -34 -> -sNaN
cpsx224 copysign       -sNaN     -34 -> -sNaN

-- NaNs, non-0 payload
cpsx231 copysign       NaN10     -34 -> -NaN10
cpsx232 copysign      -NaN10     -34 -> -NaN10
cpsx233 copysign      sNaN10     -34 -> -sNaN10
cpsx234 copysign     -sNaN10     -34 -> -sNaN10
cpsx235 copysign       NaN7      -34 -> -NaN7
cpsx236 copysign      -NaN7      -34 -> -NaN7
cpsx237 copysign      sNaN101    -34 -> -sNaN101
cpsx238 copysign     -sNaN101    -34 -> -sNaN101

-- finites
cpsx301 copysign          7      -34 -> -7
cpsx302 copysign         -7      -34 -> -7
cpsx303 copysign         75      -34 -> -75
cpsx304 copysign        -75      -34 -> -75
cpsx305 copysign       7.50      -34 -> -7.50
cpsx306 copysign      -7.50      -34 -> -7.50
cpsx307 copysign       7.500     -34 -> -7.500
cpsx308 copysign      -7.500     -34 -> -7.500

-- zeros
cpsx311 copysign          0      -34 -> -0
cpsx312 copysign         -0      -34 -> -0
cpsx313 copysign       0E+4      -34 -> -0E+4
cpsx314 copysign      -0E+4      -34 -> -0E+4
cpsx315 copysign     0.0000      -34 -> -0.0000
cpsx316 copysign    -0.0000      -34 -> -0.0000
cpsx317 copysign      0E-141     -34 -> -0E-141
cpsx318 copysign     -0E-141     -34 -> -0E-141

-- full coefficients, alternating bits
cpsx321 copysign   268268268          -18 -> -268268268
cpsx322 copysign  -268268268          -18 -> -268268268
cpsx323 copysign   134134134          -18 -> -134134134
cpsx324 copysign  -134134134          -18 -> -134134134

-- Nmax, Nmin, Ntiny
cpsx331 copysign  9.99999999E+999     -18 -> -9.99999999E+999
cpsx332 copysign  1E-999              -18 -> -1E-999
cpsx333 copysign  1.00000000E-999     -18 -> -1.00000000E-999
cpsx334 copysign  1E-1007             -18 -> -1E-1007

cpsx335 copysign  -1E-1007            -18 -> -1E-1007
cpsx336 copysign  -1.00000000E-999    -18 -> -1.00000000E-999
cpsx337 copysign  -1E-999             -18 -> -1E-999
cpsx338 copysign  -9.99999999E+999    -18 -> -9.99999999E+999

-- Other kinds of RHS
cpsx401 copysign          701    -34 -> -701
cpsx402 copysign         -720    -34 -> -720
cpsx403 copysign          701    -0  -> -701
cpsx404 copysign         -720    -0  -> -720
cpsx405 copysign          701    +0  ->  701
cpsx406 copysign         -720    +0  ->  720
cpsx407 copysign          701    +34 ->  701
cpsx408 copysign         -720    +34 ->  720

cpsx413 copysign          701    -Inf  -> -701
cpsx414 copysign         -720    -Inf  -> -720
cpsx415 copysign          701    +Inf  ->  701
cpsx416 copysign         -720    +Inf  ->  720

cpsx420 copysign          701    -NaN  -> -701
cpsx421 copysign         -720    -NaN  -> -720
cpsx422 copysign          701    +NaN  ->  701
cpsx423 copysign         -720    +NaN  ->  720
cpsx425 copysign         -720    +NaN8 ->  720

cpsx426 copysign          701    -sNaN  -> -701
cpsx427 copysign         -720    -sNaN  -> -720
cpsx428 copysign          701    +sNaN  ->  701
cpsx429 copysign         -720    +sNaN  ->  720
cpsx430 copysign         -720    +sNaN3 ->  720

Added test/dectest/ddAbs.decTest.





























































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
------------------------------------------------------------------------
-- ddAbs.decTest -- decDouble absolute value, heeding sNaN            --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
-- particular, achieving the same results as the tests here is not    --
-- a guarantee that an implementation complies with any Standard      --
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.55

precision:   16
maxExponent: 384
minExponent: -383
extended:    1
clamp:       1
rounding:    half_even

ddabs001 abs '1'      -> '1'
ddabs002 abs '-1'     -> '1'
ddabs003 abs '1.00'   -> '1.00'
ddabs004 abs '-1.00'  -> '1.00'
ddabs005 abs '0'      -> '0'
ddabs006 abs '0.00'   -> '0.00'
ddabs007 abs '00.0'   -> '0.0'
ddabs008 abs '00.00'  -> '0.00'
ddabs009 abs '00'     -> '0'

ddabs010 abs '-2'     -> '2'
ddabs011 abs '2'      -> '2'
ddabs012 abs '-2.00'  -> '2.00'
ddabs013 abs '2.00'   -> '2.00'
ddabs014 abs '-0'     -> '0'
ddabs015 abs '-0.00'  -> '0.00'
ddabs016 abs '-00.0'  -> '0.0'
ddabs017 abs '-00.00' -> '0.00'
ddabs018 abs '-00'    -> '0'

ddabs020 abs '-2000000' -> '2000000'
ddabs021 abs '2000000'  -> '2000000'

ddabs030 abs '+0.1'            -> '0.1'
ddabs031 abs '-0.1'            -> '0.1'
ddabs032 abs '+0.01'           -> '0.01'
ddabs033 abs '-0.01'           -> '0.01'
ddabs034 abs '+0.001'          -> '0.001'
ddabs035 abs '-0.001'          -> '0.001'
ddabs036 abs '+0.000001'       -> '0.000001'
ddabs037 abs '-0.000001'       -> '0.000001'
ddabs038 abs '+0.000000000001' -> '1E-12'
ddabs039 abs '-0.000000000001' -> '1E-12'

-- examples from decArith
ddabs040 abs '2.1'     ->  '2.1'
ddabs041 abs '-100'    ->  '100'
ddabs042 abs '101.5'   ->  '101.5'
ddabs043 abs '-101.5'  ->  '101.5'

-- more fixed, potential LHS swaps/overlays if done by subtract 0
ddabs060 abs '-56267E-10'  -> '0.0000056267'
ddabs061 abs '-56267E-5'   -> '0.56267'
ddabs062 abs '-56267E-2'   -> '562.67'
ddabs063 abs '-56267E-1'   -> '5626.7'
ddabs065 abs '-56267E-0'   -> '56267'

-- subnormals and underflow

-- long operand tests
ddabs321 abs 1234567890123456  -> 1234567890123456
ddabs322 abs 12345678000  -> 12345678000
ddabs323 abs 1234567800   -> 1234567800
ddabs324 abs 1234567890   -> 1234567890
ddabs325 abs 1234567891   -> 1234567891
ddabs326 abs 12345678901  -> 12345678901
ddabs327 abs 1234567896   -> 1234567896

-- zeros
ddabs111 abs          0   -> 0
ddabs112 abs         -0   -> 0
ddabs113 abs       0E+6   -> 0E+6
ddabs114 abs      -0E+6   -> 0E+6
ddabs115 abs     0.0000   -> 0.0000
ddabs116 abs    -0.0000   -> 0.0000
ddabs117 abs      0E-141  -> 0E-141
ddabs118 abs     -0E-141  -> 0E-141

-- full coefficients, alternating bits
ddabs121 abs  2682682682682682         -> 2682682682682682
ddabs122 abs  -2682682682682682        -> 2682682682682682
ddabs123 abs  1341341341341341         -> 1341341341341341
ddabs124 abs  -1341341341341341        -> 1341341341341341

-- Nmax, Nmin, Ntiny
ddabs131 abs  9.999999999999999E+384   -> 9.999999999999999E+384
ddabs132 abs  1E-383                   -> 1E-383
ddabs133 abs  1.000000000000000E-383   -> 1.000000000000000E-383
ddabs134 abs  1E-398                   -> 1E-398 Subnormal

ddabs135 abs  -1E-398                  -> 1E-398 Subnormal
ddabs136 abs  -1.000000000000000E-383  -> 1.000000000000000E-383
ddabs137 abs  -1E-383                  -> 1E-383
ddabs138 abs  -9.999999999999999E+384  -> 9.999999999999999E+384

-- specials
ddabs520 abs 'Inf'    -> 'Infinity'
ddabs521 abs '-Inf'   -> 'Infinity'
ddabs522 abs   NaN    ->  NaN
ddabs523 abs  sNaN    ->  NaN   Invalid_operation
ddabs524 abs   NaN22  ->  NaN22
ddabs525 abs  sNaN33  ->  NaN33 Invalid_operation
ddabs526 abs  -NaN22  -> -NaN22
ddabs527 abs -sNaN33  -> -NaN33 Invalid_operation

-- Null tests
ddabs900 abs  # -> NaN Invalid_operation

Added test/dectest/ddAdd.decTest.





















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>