1 /* ------------------------------------------------------------------ */
2 /* Decimal Context module header                                      */
3 /* ------------------------------------------------------------------ */
4 /* Copyright (c) IBM Corporation, 2000, 2010.  All rights reserved.   */
5 /*                                                                    */
6 /* This software is made available under the terms of the             */
7 /* ICU License -- ICU 1.8.1 and later.                                */
8 /*                                                                    */
9 /* The description and User's Guide ("The decNumber C Library") for   */
10 /* this software is called decNumber.pdf.  This document is           */
11 /* available, together with arithmetic and format specifications,     */
12 /* testcases, and Web links, on the General Decimal Arithmetic page.  */
13 /*                                                                    */
14 /* Please send comments, suggestions, and corrections to the author:  */
15 /*   mfc@uk.ibm.com                                                   */
16 /*   Mike Cowlishaw, IBM Fellow                                       */
17 /*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
18 /* ------------------------------------------------------------------ */
19 /*                                                                    */
20 /* Context variables must always have valid values:                   */
21 /*                                                                    */
22 /*  status   -- [any bits may be cleared, but not set, by user]       */
23 /*  round    -- must be one of the enumerated rounding modes          */
24 /*                                                                    */
25 /* The following variables are implied for fixed size formats (i.e.,  */
26 /* they are ignored) but should still be set correctly in case used   */
27 /* with decNumber functions:                                          */
28 /*                                                                    */
29 /*  clamp    -- must be either 0 or 1                                 */
30 /*  digits   -- must be in the range 1 through 999999999              */
31 /*  emax     -- must be in the range 0 through 999999999              */
32 /*  emin     -- must be in the range 0 through -999999999             */
33 /*  extended -- must be either 0 or 1 [present only if DECSUBSET]     */
34 /*  traps    -- only defined bits may be set                          */
35 /*                                                                    */
36 /* ------------------------------------------------------------------ */
37 
38 #if !defined(DECCONTEXT)
39   #define DECCONTEXT
40   #define DECCNAME     "decContext"                     /* Short name */
41   #define DECCFULLNAME "Decimal Context Descriptor"   /* Verbose name */
42   #define DECCAUTHOR   "Mike Cowlishaw"               /* Who to blame */
43 
44   #if !defined(int32_t)
45     #include <stdint.h>            /* C99 standard integers           */
46   #endif
47   #include <stdio.h>               /* for printf, etc.                */
48   #include <signal.h>              /* for traps                       */
49 
50   /* Extended flags setting -- set this to 0 to use only IEEE flags   */
51   #if !defined(DECEXTFLAG)
52   #define DECEXTFLAG 1             /* 1=enable extended flags         */
53   #endif
54 
55   /* Conditional code flag -- set this to 0 for best performance      */
56   #if !defined(DECSUBSET)
57   #define DECSUBSET  0             /* 1=enable subset arithmetic      */
58   #endif
59 
60   /* Context for operations, with associated constants                */
61   enum rounding {
62     DEC_ROUND_CEILING,             /* round towards +infinity         */
63     DEC_ROUND_UP,                  /* round away from 0               */
64     DEC_ROUND_HALF_UP,             /* 0.5 rounds up                   */
65     DEC_ROUND_HALF_EVEN,           /* 0.5 rounds to nearest even      */
66     DEC_ROUND_HALF_DOWN,           /* 0.5 rounds down                 */
67     DEC_ROUND_DOWN,                /* round towards 0 (truncate)      */
68     DEC_ROUND_FLOOR,               /* round towards -infinity         */
69     DEC_ROUND_05UP,                /* round for reround               */
70     DEC_ROUND_MAX                  /* enum must be less than this     */
71     };
72   #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
73 
74   typedef struct {
75     int32_t  digits;               /* working precision               */
76     int32_t  emax;                 /* maximum positive exponent       */
77     int32_t  emin;                 /* minimum negative exponent       */
78     enum     rounding round;       /* rounding mode                   */
79     uint32_t traps;                /* trap-enabler flags              */
80     uint32_t status;               /* status flags                    */
81     uint8_t  clamp;                /* flag: apply IEEE exponent clamp */
82     #if DECSUBSET
83     uint8_t  extended;             /* flag: special-values allowed    */
84     #endif
85     } decContext;
86 
87   /* Maxima and Minima for context settings                           */
88   #define DEC_MAX_DIGITS 999999999
89   #define DEC_MIN_DIGITS         1
90   #define DEC_MAX_EMAX   999999999
91   #define DEC_MIN_EMAX           0
92   #define DEC_MAX_EMIN           0
93   #define DEC_MIN_EMIN  -999999999
94   #define DEC_MAX_MATH      999999 /* max emax, etc., for math funcs. */
95 
96   /* Classifications for decimal numbers, aligned with 754 (note that */
97   /* 'normal' and 'subnormal' are meaningful only with a decContext   */
98   /* or a fixed size format).                                         */
99   enum decClass {
100     DEC_CLASS_SNAN,
101     DEC_CLASS_QNAN,
102     DEC_CLASS_NEG_INF,
103     DEC_CLASS_NEG_NORMAL,
104     DEC_CLASS_NEG_SUBNORMAL,
105     DEC_CLASS_NEG_ZERO,
106     DEC_CLASS_POS_ZERO,
107     DEC_CLASS_POS_SUBNORMAL,
108     DEC_CLASS_POS_NORMAL,
109     DEC_CLASS_POS_INF
110     };
111   /* Strings for the decClasses */
112   #define DEC_ClassString_SN  "sNaN"
113   #define DEC_ClassString_QN  "NaN"
114   #define DEC_ClassString_NI  "-Infinity"
115   #define DEC_ClassString_NN  "-Normal"
116   #define DEC_ClassString_NS  "-Subnormal"
117   #define DEC_ClassString_NZ  "-Zero"
118   #define DEC_ClassString_PZ  "+Zero"
119   #define DEC_ClassString_PS  "+Subnormal"
120   #define DEC_ClassString_PN  "+Normal"
121   #define DEC_ClassString_PI  "+Infinity"
122   #define DEC_ClassString_UN  "Invalid"
123 
124   /* Trap-enabler and Status flags (exceptional conditions), and      */
125   /* their names.  The top byte is reserved for internal use          */
126   #if DECEXTFLAG
127     /* Extended flags */
128     #define DEC_Conversion_syntax    0x00000001
129     #define DEC_Division_by_zero     0x00000002
130     #define DEC_Division_impossible  0x00000004
131     #define DEC_Division_undefined   0x00000008
132     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]  */
133     #define DEC_Inexact              0x00000020
134     #define DEC_Invalid_context      0x00000040
135     #define DEC_Invalid_operation    0x00000080
136     #if DECSUBSET
137     #define DEC_Lost_digits          0x00000100
138     #endif
139     #define DEC_Overflow             0x00000200
140     #define DEC_Clamped              0x00000400
141     #define DEC_Rounded              0x00000800
142     #define DEC_Subnormal            0x00001000
143     #define DEC_Underflow            0x00002000
144   #else
145     /* IEEE flags only */
146     #define DEC_Conversion_syntax    0x00000010
147     #define DEC_Division_by_zero     0x00000002
148     #define DEC_Division_impossible  0x00000010
149     #define DEC_Division_undefined   0x00000010
150     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]  */
151     #define DEC_Inexact              0x00000001
152     #define DEC_Invalid_context      0x00000010
153     #define DEC_Invalid_operation    0x00000010
154     #if DECSUBSET
155     #define DEC_Lost_digits          0x00000000
156     #endif
157     #define DEC_Overflow             0x00000008
158     #define DEC_Clamped              0x00000000
159     #define DEC_Rounded              0x00000000
160     #define DEC_Subnormal            0x00000000
161     #define DEC_Underflow            0x00000004
162   #endif
163 
164   /* IEEE 754 groupings for the flags                                 */
165   /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal    */
166   /* are not in IEEE 754]                                             */
167   #define DEC_IEEE_754_Division_by_zero  (DEC_Division_by_zero)
168   #if DECSUBSET
169   #define DEC_IEEE_754_Inexact           (DEC_Inexact | DEC_Lost_digits)
170   #else
171   #define DEC_IEEE_754_Inexact           (DEC_Inexact)
172   #endif
173   #define DEC_IEEE_754_Invalid_operation (DEC_Conversion_syntax |     \
174                                           DEC_Division_impossible |   \
175                                           DEC_Division_undefined |    \
176                                           DEC_Insufficient_storage |  \
177                                           DEC_Invalid_context |       \
178                                           DEC_Invalid_operation)
179   #define DEC_IEEE_754_Overflow          (DEC_Overflow)
180   #define DEC_IEEE_754_Underflow         (DEC_Underflow)
181 
182   /* flags which are normally errors (result is qNaN, infinite, or 0) */
183   #define DEC_Errors (DEC_IEEE_754_Division_by_zero |                 \
184                       DEC_IEEE_754_Invalid_operation |                \
185                       DEC_IEEE_754_Overflow | DEC_IEEE_754_Underflow)
186   /* flags which cause a result to become qNaN                        */
187   #define DEC_NaNs    DEC_IEEE_754_Invalid_operation
188 
189   /* flags which are normally for information only (finite results)   */
190   #if DECSUBSET
191   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact    \
192                           | DEC_Lost_digits)
193   #else
194   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
195   #endif
196 
197   /* IEEE 854 names (for compatibility with older decNumber versions) */
198   #define DEC_IEEE_854_Division_by_zero  DEC_IEEE_754_Division_by_zero
199   #define DEC_IEEE_854_Inexact           DEC_IEEE_754_Inexact
200   #define DEC_IEEE_854_Invalid_operation DEC_IEEE_754_Invalid_operation
201   #define DEC_IEEE_854_Overflow          DEC_IEEE_754_Overflow
202   #define DEC_IEEE_854_Underflow         DEC_IEEE_754_Underflow
203 
204   /* Name strings for the exceptional conditions                      */
205   #define DEC_Condition_CS "Conversion syntax"
206   #define DEC_Condition_DZ "Division by zero"
207   #define DEC_Condition_DI "Division impossible"
208   #define DEC_Condition_DU "Division undefined"
209   #define DEC_Condition_IE "Inexact"
210   #define DEC_Condition_IS "Insufficient storage"
211   #define DEC_Condition_IC "Invalid context"
212   #define DEC_Condition_IO "Invalid operation"
213   #if DECSUBSET
214   #define DEC_Condition_LD "Lost digits"
215   #endif
216   #define DEC_Condition_OV "Overflow"
217   #define DEC_Condition_PA "Clamped"
218   #define DEC_Condition_RO "Rounded"
219   #define DEC_Condition_SU "Subnormal"
220   #define DEC_Condition_UN "Underflow"
221   #define DEC_Condition_ZE "No status"
222   #define DEC_Condition_MU "Multiple status"
223   #define DEC_Condition_Length 21  /* length of the longest string,   */
224                                    /* including terminator            */
225 
226   /* Initialization descriptors, used by decContextDefault            */
227   #define DEC_INIT_BASE         0
228   #define DEC_INIT_DECIMAL32   32
229   #define DEC_INIT_DECIMAL64   64
230   #define DEC_INIT_DECIMAL128 128
231   /* Synonyms */
232   #define DEC_INIT_DECSINGLE  DEC_INIT_DECIMAL32
233   #define DEC_INIT_DECDOUBLE  DEC_INIT_DECIMAL64
234   #define DEC_INIT_DECQUAD    DEC_INIT_DECIMAL128
235 
236   /* decContext routines                                              */
237   extern decContext  * decContextClearStatus(decContext *, uint32_t);
238   extern decContext  * decContextDefault(decContext *, int32_t);
239   extern enum rounding decContextGetRounding(decContext *);
240   extern uint32_t      decContextGetStatus(decContext *);
241   extern decContext  * decContextRestoreStatus(decContext *, uint32_t, uint32_t);
242   extern uint32_t      decContextSaveStatus(decContext *, uint32_t);
243   extern decContext  * decContextSetRounding(decContext *, enum rounding);
244   extern decContext  * decContextSetStatus(decContext *, uint32_t);
245   extern decContext  * decContextSetStatusFromString(decContext *, const char *);
246   extern decContext  * decContextSetStatusFromStringQuiet(decContext *, const char *);
247   extern decContext  * decContextSetStatusQuiet(decContext *, uint32_t);
248   extern const char  * decContextStatusToString(const decContext *);
249   extern int32_t       decContextTestEndian(uint8_t);
250   extern uint32_t      decContextTestSavedStatus(uint32_t, uint32_t);
251   extern uint32_t      decContextTestStatus(decContext *, uint32_t);
252   extern decContext  * decContextZeroStatus(decContext *);
253 
254 #endif
255