xref: /qemu/include/libdecnumber/decContext.h (revision 2a6a4076)
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 #ifndef DECCONTEXT_H
54 #define DECCONTEXT_H
55 
56   #define DECCNAME     "decContext"			/* Short name */
57   #define DECCFULLNAME "Decimal Context Descriptor"   /* Verbose name */
58   #define DECCAUTHOR   "Mike Cowlishaw"		      /* Who to blame */
59 
60 
61   /* Extended flags setting -- set this to 0 to use only IEEE flags   */
62   #define DECEXTFLAG 1		   /* 1=enable extended flags	      */
63 
64   /* Conditional code flag -- set this to 0 for best performance      */
65   #define DECSUBSET  0		   /* 1=enable subset arithmetic      */
66 
67   /* Context for operations, with associated constants		      */
68   enum rounding {
69     DEC_ROUND_CEILING,		   /* round towards +infinity	      */
70     DEC_ROUND_UP,		   /* round away from 0		      */
71     DEC_ROUND_HALF_UP,		   /* 0.5 rounds up		      */
72     DEC_ROUND_HALF_EVEN,	   /* 0.5 rounds to nearest even      */
73     DEC_ROUND_HALF_DOWN,	   /* 0.5 rounds down		      */
74     DEC_ROUND_DOWN,		   /* round towards 0 (truncate)      */
75     DEC_ROUND_FLOOR,		   /* round towards -infinity	      */
76     DEC_ROUND_05UP,		   /* round for reround		      */
77     DEC_ROUND_MAX		   /* enum must be less than this     */
78     };
79   #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
80 
81   typedef struct {
82     int32_t  digits;		   /* working precision		      */
83     int32_t  emax;		   /* maximum positive exponent	      */
84     int32_t  emin;		   /* minimum negative exponent	      */
85     enum     rounding round;	   /* rounding mode		      */
86     uint32_t traps;		   /* trap-enabler flags	      */
87     uint32_t status;		   /* status flags		      */
88     uint8_t  clamp;		   /* flag: apply IEEE exponent clamp */
89     #if DECSUBSET
90     uint8_t  extended;		   /* flag: special-values allowed    */
91     #endif
92     } decContext;
93 
94   /* Maxima and Minima for context settings			      */
95   #define DEC_MAX_DIGITS 999999999
96   #define DEC_MIN_DIGITS	 1
97   #define DEC_MAX_EMAX	 999999999
98   #define DEC_MIN_EMAX		 0
99   #define DEC_MAX_EMIN		 0
100   #define DEC_MIN_EMIN	-999999999
101   #define DEC_MAX_MATH	    999999 /* max emax, etc., for math funcs. */
102 
103   /* Classifications for decimal numbers, aligned with 754r (note     */
104   /* that 'normal' and 'subnormal' are meaningful only with a	      */
105   /* decContext or a fixed size format).			      */
106   enum decClass {
107     DEC_CLASS_SNAN,
108     DEC_CLASS_QNAN,
109     DEC_CLASS_NEG_INF,
110     DEC_CLASS_NEG_NORMAL,
111     DEC_CLASS_NEG_SUBNORMAL,
112     DEC_CLASS_NEG_ZERO,
113     DEC_CLASS_POS_ZERO,
114     DEC_CLASS_POS_SUBNORMAL,
115     DEC_CLASS_POS_NORMAL,
116     DEC_CLASS_POS_INF
117     };
118   /* Strings for the decClasses */
119   #define DEC_ClassString_SN  "sNaN"
120   #define DEC_ClassString_QN  "NaN"
121   #define DEC_ClassString_NI  "-Infinity"
122   #define DEC_ClassString_NN  "-Normal"
123   #define DEC_ClassString_NS  "-Subnormal"
124   #define DEC_ClassString_NZ  "-Zero"
125   #define DEC_ClassString_PZ  "+Zero"
126   #define DEC_ClassString_PS  "+Subnormal"
127   #define DEC_ClassString_PN  "+Normal"
128   #define DEC_ClassString_PI  "+Infinity"
129   #define DEC_ClassString_UN  "Invalid"
130 
131   /* Trap-enabler and Status flags (exceptional conditions), and      */
132   /* their names.  The top byte is reserved for internal use	      */
133   #if DECEXTFLAG
134     /* Extended flags */
135     #define DEC_Conversion_syntax    0x00000001
136     #define DEC_Division_by_zero     0x00000002
137     #define DEC_Division_impossible  0x00000004
138     #define DEC_Division_undefined   0x00000008
139     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]	*/
140     #define DEC_Inexact		     0x00000020
141     #define DEC_Invalid_context	     0x00000040
142     #define DEC_Invalid_operation    0x00000080
143     #if DECSUBSET
144     #define DEC_Lost_digits	     0x00000100
145     #endif
146     #define DEC_Overflow	     0x00000200
147     #define DEC_Clamped		     0x00000400
148     #define DEC_Rounded		     0x00000800
149     #define DEC_Subnormal	     0x00001000
150     #define DEC_Underflow	     0x00002000
151   #else
152     /* IEEE flags only */
153     #define DEC_Conversion_syntax    0x00000010
154     #define DEC_Division_by_zero     0x00000002
155     #define DEC_Division_impossible  0x00000010
156     #define DEC_Division_undefined   0x00000010
157     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]	*/
158     #define DEC_Inexact		     0x00000001
159     #define DEC_Invalid_context	     0x00000010
160     #define DEC_Invalid_operation    0x00000010
161     #if DECSUBSET
162     #define DEC_Lost_digits	     0x00000000
163     #endif
164     #define DEC_Overflow	     0x00000008
165     #define DEC_Clamped		     0x00000000
166     #define DEC_Rounded		     0x00000000
167     #define DEC_Subnormal	     0x00000000
168     #define DEC_Underflow	     0x00000004
169   #endif
170 
171   /* IEEE 854 groupings for the flags				      */
172   /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal    */
173   /* are not in IEEE 854]					      */
174   #define DEC_IEEE_854_Division_by_zero	 (DEC_Division_by_zero)
175   #if DECSUBSET
176   #define DEC_IEEE_854_Inexact		 (DEC_Inexact | DEC_Lost_digits)
177   #else
178   #define DEC_IEEE_854_Inexact		 (DEC_Inexact)
179   #endif
180   #define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax |     \
181 					  DEC_Division_impossible |   \
182 					  DEC_Division_undefined |    \
183 					  DEC_Insufficient_storage |  \
184 					  DEC_Invalid_context |	      \
185 					  DEC_Invalid_operation)
186   #define DEC_IEEE_854_Overflow		 (DEC_Overflow)
187   #define DEC_IEEE_854_Underflow	 (DEC_Underflow)
188 
189   /* flags which are normally errors (result is qNaN, infinite, or 0) */
190   #define DEC_Errors (DEC_IEEE_854_Division_by_zero |		      \
191 		      DEC_IEEE_854_Invalid_operation |		      \
192 		      DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
193   /* flags which cause a result to become qNaN			      */
194   #define DEC_NaNs    DEC_IEEE_854_Invalid_operation
195 
196   /* flags which are normally for information only (finite results)   */
197   #if DECSUBSET
198   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact    \
199 			  | DEC_Lost_digits)
200   #else
201   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
202   #endif
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 
238 
239   extern decContext  * decContextClearStatus(decContext *, uint32_t);
240   extern decContext  * decContextDefault(decContext *, int32_t);
241   extern enum rounding decContextGetRounding(decContext *);
242   extern uint32_t      decContextGetStatus(decContext *);
243   extern decContext  * decContextRestoreStatus(decContext *, uint32_t, uint32_t);
244   extern uint32_t      decContextSaveStatus(decContext *, uint32_t);
245   extern decContext  * decContextSetRounding(decContext *, enum rounding);
246   extern decContext  * decContextSetStatus(decContext *, uint32_t);
247   extern decContext  * decContextSetStatusFromString(decContext *, const char *);
248   extern decContext  * decContextSetStatusFromStringQuiet(decContext *, const char *);
249   extern decContext  * decContextSetStatusQuiet(decContext *, uint32_t);
250   extern const char  * decContextStatusToString(const decContext *);
251   extern uint32_t      decContextTestSavedStatus(uint32_t, uint32_t);
252   extern uint32_t      decContextTestStatus(decContext *, uint32_t);
253   extern decContext  * decContextZeroStatus(decContext *);
254 
255 #endif
256