1 /* Decimal context header module for the decNumber C Library.
2    Copyright (C) 2005, 2007 Free Software Foundation, Inc.
3    Contributed by IBM Corporation.  Author Mike Cowlishaw.
4 
5    This file is part of GCC.
6 
7    GCC is free software; you can redistribute it and/or modify it under
8    the terms of the GNU General Public License as published by the Free
9    Software Foundation; either version 2, or (at your option) any later
10    version.
11 
12    In addition to the permissions in the GNU General Public License,
13    the Free Software Foundation gives you unlimited permission to link
14    the compiled version of this file into combinations with other
15    programs, and to distribute those combinations without any
16    restriction coming from the use of this file.  (The General Public
17    License restrictions do apply in other respects; for example, they
18    cover modification of the file, and distribution when not linked
19    into a combine executable.)
20 
21    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22    WARRANTY; without even the implied warranty of MERCHANTABILITY or
23    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24    for more details.
25 
26    You should have received a copy of the GNU General Public License
27    along with GCC; see the file COPYING.  If not, write to the Free
28    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29    02110-1301, USA.  */
30 
31 /* ------------------------------------------------------------------ */
32 /* Decimal Context module header				      */
33 /* ------------------------------------------------------------------ */
34 /*								      */
35 /* Context variables must always have valid values:		      */
36 /*								      */
37 /*  status   -- [any bits may be cleared, but not set, by user]	      */
38 /*  round    -- must be one of the enumerated rounding modes	      */
39 /*								      */
40 /* The following variables are implied for fixed size formats (i.e.,  */
41 /* they are ignored) but should still be set correctly in case used   */
42 /* with decNumber functions:					      */
43 /*								      */
44 /*  clamp    -- must be either 0 or 1				      */
45 /*  digits   -- must be in the range 1 through 999999999	      */
46 /*  emax     -- must be in the range 0 through 999999999	      */
47 /*  emin     -- must be in the range 0 through -999999999	      */
48 /*  extended -- must be either 0 or 1 [present only if DECSUBSET]     */
49 /*  traps    -- only defined bits may be set			      */
50 /*								      */
51 /* ------------------------------------------------------------------ */
52 
53 #if !defined(DECCONTEXT)
54   #define DECCONTEXT
55   #define DECCNAME     "decContext"			/* Short name */
56   #define DECCFULLNAME "Decimal Context Descriptor"   /* Verbose name */
57   #define DECCAUTHOR   "Mike Cowlishaw"		      /* Who to blame */
58 
59 
60   /* Extended flags setting -- set this to 0 to use only IEEE flags   */
61   #define DECEXTFLAG 1		   /* 1=enable extended flags	      */
62 
63   /* Conditional code flag -- set this to 0 for best performance      */
64   #define DECSUBSET  0		   /* 1=enable subset arithmetic      */
65 
66   /* Context for operations, with associated constants		      */
67   enum rounding {
68     DEC_ROUND_CEILING,		   /* round towards +infinity	      */
69     DEC_ROUND_UP,		   /* round away from 0		      */
70     DEC_ROUND_HALF_UP,		   /* 0.5 rounds up		      */
71     DEC_ROUND_HALF_EVEN,	   /* 0.5 rounds to nearest even      */
72     DEC_ROUND_HALF_DOWN,	   /* 0.5 rounds down		      */
73     DEC_ROUND_DOWN,		   /* round towards 0 (truncate)      */
74     DEC_ROUND_FLOOR,		   /* round towards -infinity	      */
75     DEC_ROUND_05UP,		   /* round for reround		      */
76     DEC_ROUND_MAX		   /* enum must be less than this     */
77     };
78   #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
79 
80   typedef struct {
81     int32_t  digits;		   /* working precision		      */
82     int32_t  emax;		   /* maximum positive exponent	      */
83     int32_t  emin;		   /* minimum negative exponent	      */
84     enum     rounding round;	   /* rounding mode		      */
85     uint32_t traps;		   /* trap-enabler flags	      */
86     uint32_t status;		   /* status flags		      */
87     uint8_t  clamp;		   /* flag: apply IEEE exponent clamp */
88     #if DECSUBSET
89     uint8_t  extended;		   /* flag: special-values allowed    */
90     #endif
91     } decContext;
92 
93   /* Maxima and Minima for context settings			      */
94   #define DEC_MAX_DIGITS 999999999
95   #define DEC_MIN_DIGITS	 1
96   #define DEC_MAX_EMAX	 999999999
97   #define DEC_MIN_EMAX		 0
98   #define DEC_MAX_EMIN		 0
99   #define DEC_MIN_EMIN	-999999999
100   #define DEC_MAX_MATH	    999999 /* max emax, etc., for math funcs. */
101 
102   /* Classifications for decimal numbers, aligned with 754r (note     */
103   /* that 'normal' and 'subnormal' are meaningful only with a	      */
104   /* decContext or a fixed size format).			      */
105   enum decClass {
106     DEC_CLASS_SNAN,
107     DEC_CLASS_QNAN,
108     DEC_CLASS_NEG_INF,
109     DEC_CLASS_NEG_NORMAL,
110     DEC_CLASS_NEG_SUBNORMAL,
111     DEC_CLASS_NEG_ZERO,
112     DEC_CLASS_POS_ZERO,
113     DEC_CLASS_POS_SUBNORMAL,
114     DEC_CLASS_POS_NORMAL,
115     DEC_CLASS_POS_INF
116     };
117   /* Strings for the decClasses */
118   #define DEC_ClassString_SN  "sNaN"
119   #define DEC_ClassString_QN  "NaN"
120   #define DEC_ClassString_NI  "-Infinity"
121   #define DEC_ClassString_NN  "-Normal"
122   #define DEC_ClassString_NS  "-Subnormal"
123   #define DEC_ClassString_NZ  "-Zero"
124   #define DEC_ClassString_PZ  "+Zero"
125   #define DEC_ClassString_PS  "+Subnormal"
126   #define DEC_ClassString_PN  "+Normal"
127   #define DEC_ClassString_PI  "+Infinity"
128   #define DEC_ClassString_UN  "Invalid"
129 
130   /* Trap-enabler and Status flags (exceptional conditions), and      */
131   /* their names.  The top byte is reserved for internal use	      */
132   #if DECEXTFLAG
133     /* Extended flags */
134     #define DEC_Conversion_syntax    0x00000001
135     #define DEC_Division_by_zero     0x00000002
136     #define DEC_Division_impossible  0x00000004
137     #define DEC_Division_undefined   0x00000008
138     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]	*/
139     #define DEC_Inexact		     0x00000020
140     #define DEC_Invalid_context	     0x00000040
141     #define DEC_Invalid_operation    0x00000080
142     #if DECSUBSET
143     #define DEC_Lost_digits	     0x00000100
144     #endif
145     #define DEC_Overflow	     0x00000200
146     #define DEC_Clamped		     0x00000400
147     #define DEC_Rounded		     0x00000800
148     #define DEC_Subnormal	     0x00001000
149     #define DEC_Underflow	     0x00002000
150   #else
151     /* IEEE flags only */
152     #define DEC_Conversion_syntax    0x00000010
153     #define DEC_Division_by_zero     0x00000002
154     #define DEC_Division_impossible  0x00000010
155     #define DEC_Division_undefined   0x00000010
156     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]	*/
157     #define DEC_Inexact		     0x00000001
158     #define DEC_Invalid_context	     0x00000010
159     #define DEC_Invalid_operation    0x00000010
160     #if DECSUBSET
161     #define DEC_Lost_digits	     0x00000000
162     #endif
163     #define DEC_Overflow	     0x00000008
164     #define DEC_Clamped		     0x00000000
165     #define DEC_Rounded		     0x00000000
166     #define DEC_Subnormal	     0x00000000
167     #define DEC_Underflow	     0x00000004
168   #endif
169 
170   /* IEEE 854 groupings for the flags				      */
171   /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal    */
172   /* are not in IEEE 854]					      */
173   #define DEC_IEEE_854_Division_by_zero	 (DEC_Division_by_zero)
174   #if DECSUBSET
175   #define DEC_IEEE_854_Inexact		 (DEC_Inexact | DEC_Lost_digits)
176   #else
177   #define DEC_IEEE_854_Inexact		 (DEC_Inexact)
178   #endif
179   #define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax |     \
180 					  DEC_Division_impossible |   \
181 					  DEC_Division_undefined |    \
182 					  DEC_Insufficient_storage |  \
183 					  DEC_Invalid_context |	      \
184 					  DEC_Invalid_operation)
185   #define DEC_IEEE_854_Overflow		 (DEC_Overflow)
186   #define DEC_IEEE_854_Underflow	 (DEC_Underflow)
187 
188   /* flags which are normally errors (result is qNaN, infinite, or 0) */
189   #define DEC_Errors (DEC_IEEE_854_Division_by_zero |		      \
190 		      DEC_IEEE_854_Invalid_operation |		      \
191 		      DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
192   /* flags which cause a result to become qNaN			      */
193   #define DEC_NaNs    DEC_IEEE_854_Invalid_operation
194 
195   /* flags which are normally for information only (finite results)   */
196   #if DECSUBSET
197   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact    \
198 			  | DEC_Lost_digits)
199   #else
200   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
201   #endif
202 
203   /* Name strings for the exceptional conditions		      */
204   #define DEC_Condition_CS "Conversion syntax"
205   #define DEC_Condition_DZ "Division by zero"
206   #define DEC_Condition_DI "Division impossible"
207   #define DEC_Condition_DU "Division undefined"
208   #define DEC_Condition_IE "Inexact"
209   #define DEC_Condition_IS "Insufficient storage"
210   #define DEC_Condition_IC "Invalid context"
211   #define DEC_Condition_IO "Invalid operation"
212   #if DECSUBSET
213   #define DEC_Condition_LD "Lost digits"
214   #endif
215   #define DEC_Condition_OV "Overflow"
216   #define DEC_Condition_PA "Clamped"
217   #define DEC_Condition_RO "Rounded"
218   #define DEC_Condition_SU "Subnormal"
219   #define DEC_Condition_UN "Underflow"
220   #define DEC_Condition_ZE "No status"
221   #define DEC_Condition_MU "Multiple status"
222   #define DEC_Condition_Length 21  /* length of the longest string,   */
223 				   /* including terminator	      */
224 
225   /* Initialization descriptors, used by decContextDefault	      */
226   #define DEC_INIT_BASE		0
227   #define DEC_INIT_DECIMAL32   32
228   #define DEC_INIT_DECIMAL64   64
229   #define DEC_INIT_DECIMAL128 128
230   /* Synonyms */
231   #define DEC_INIT_DECSINGLE  DEC_INIT_DECIMAL32
232   #define DEC_INIT_DECDOUBLE  DEC_INIT_DECIMAL64
233   #define DEC_INIT_DECQUAD    DEC_INIT_DECIMAL128
234 
235   /* decContext routines					      */
236 
237 
238   extern decContext  * decContextClearStatus(decContext *, uint32_t);
239   extern decContext  * decContextDefault(decContext *, int32_t);
240   extern enum rounding decContextGetRounding(decContext *);
241   extern uint32_t      decContextGetStatus(decContext *);
242   extern decContext  * decContextRestoreStatus(decContext *, uint32_t, uint32_t);
243   extern uint32_t      decContextSaveStatus(decContext *, uint32_t);
244   extern decContext  * decContextSetRounding(decContext *, enum rounding);
245   extern decContext  * decContextSetStatus(decContext *, uint32_t);
246   extern decContext  * decContextSetStatusFromString(decContext *, const char *);
247   extern decContext  * decContextSetStatusFromStringQuiet(decContext *, const char *);
248   extern decContext  * decContextSetStatusQuiet(decContext *, uint32_t);
249   extern const char  * decContextStatusToString(const decContext *);
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