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   #include <stdint.h>
60   #include <stdio.h>		   /* for printf, etc.		      */
61   #include <signal.h>		   /* for traps			      */
62 
63   /* Extended flags setting -- set this to 0 to use only IEEE flags   */
64   #define DECEXTFLAG 1		   /* 1=enable extended flags	      */
65 
66   /* Conditional code flag -- set this to 0 for best performance      */
67   #define DECSUBSET  0		   /* 1=enable subset arithmetic      */
68 
69   /* Context for operations, with associated constants		      */
70   enum rounding {
71     DEC_ROUND_CEILING,		   /* round towards +infinity	      */
72     DEC_ROUND_UP,		   /* round away from 0		      */
73     DEC_ROUND_HALF_UP,		   /* 0.5 rounds up		      */
74     DEC_ROUND_HALF_EVEN,	   /* 0.5 rounds to nearest even      */
75     DEC_ROUND_HALF_DOWN,	   /* 0.5 rounds down		      */
76     DEC_ROUND_DOWN,		   /* round towards 0 (truncate)      */
77     DEC_ROUND_FLOOR,		   /* round towards -infinity	      */
78     DEC_ROUND_05UP,		   /* round for reround		      */
79     DEC_ROUND_MAX		   /* enum must be less than this     */
80     };
81   #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
82 
83   typedef struct {
84     int32_t  digits;		   /* working precision		      */
85     int32_t  emax;		   /* maximum positive exponent	      */
86     int32_t  emin;		   /* minimum negative exponent	      */
87     enum     rounding round;	   /* rounding mode		      */
88     uint32_t traps;		   /* trap-enabler flags	      */
89     uint32_t status;		   /* status flags		      */
90     uint8_t  clamp;		   /* flag: apply IEEE exponent clamp */
91     #if DECSUBSET
92     uint8_t  extended;		   /* flag: special-values allowed    */
93     #endif
94     } decContext;
95 
96   /* Maxima and Minima for context settings			      */
97   #define DEC_MAX_DIGITS 999999999
98   #define DEC_MIN_DIGITS	 1
99   #define DEC_MAX_EMAX	 999999999
100   #define DEC_MIN_EMAX		 0
101   #define DEC_MAX_EMIN		 0
102   #define DEC_MIN_EMIN	-999999999
103   #define DEC_MAX_MATH	    999999 /* max emax, etc., for math funcs. */
104 
105   /* Classifications for decimal numbers, aligned with 754r (note     */
106   /* that 'normal' and 'subnormal' are meaningful only with a	      */
107   /* decContext or a fixed size format).			      */
108   enum decClass {
109     DEC_CLASS_SNAN,
110     DEC_CLASS_QNAN,
111     DEC_CLASS_NEG_INF,
112     DEC_CLASS_NEG_NORMAL,
113     DEC_CLASS_NEG_SUBNORMAL,
114     DEC_CLASS_NEG_ZERO,
115     DEC_CLASS_POS_ZERO,
116     DEC_CLASS_POS_SUBNORMAL,
117     DEC_CLASS_POS_NORMAL,
118     DEC_CLASS_POS_INF
119     };
120   /* Strings for the decClasses */
121   #define DEC_ClassString_SN  "sNaN"
122   #define DEC_ClassString_QN  "NaN"
123   #define DEC_ClassString_NI  "-Infinity"
124   #define DEC_ClassString_NN  "-Normal"
125   #define DEC_ClassString_NS  "-Subnormal"
126   #define DEC_ClassString_NZ  "-Zero"
127   #define DEC_ClassString_PZ  "+Zero"
128   #define DEC_ClassString_PS  "+Subnormal"
129   #define DEC_ClassString_PN  "+Normal"
130   #define DEC_ClassString_PI  "+Infinity"
131   #define DEC_ClassString_UN  "Invalid"
132 
133   /* Trap-enabler and Status flags (exceptional conditions), and      */
134   /* their names.  The top byte is reserved for internal use	      */
135   #if DECEXTFLAG
136     /* Extended flags */
137     #define DEC_Conversion_syntax    0x00000001
138     #define DEC_Division_by_zero     0x00000002
139     #define DEC_Division_impossible  0x00000004
140     #define DEC_Division_undefined   0x00000008
141     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]	*/
142     #define DEC_Inexact		     0x00000020
143     #define DEC_Invalid_context	     0x00000040
144     #define DEC_Invalid_operation    0x00000080
145     #if DECSUBSET
146     #define DEC_Lost_digits	     0x00000100
147     #endif
148     #define DEC_Overflow	     0x00000200
149     #define DEC_Clamped		     0x00000400
150     #define DEC_Rounded		     0x00000800
151     #define DEC_Subnormal	     0x00001000
152     #define DEC_Underflow	     0x00002000
153   #else
154     /* IEEE flags only */
155     #define DEC_Conversion_syntax    0x00000010
156     #define DEC_Division_by_zero     0x00000002
157     #define DEC_Division_impossible  0x00000010
158     #define DEC_Division_undefined   0x00000010
159     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]	*/
160     #define DEC_Inexact		     0x00000001
161     #define DEC_Invalid_context	     0x00000010
162     #define DEC_Invalid_operation    0x00000010
163     #if DECSUBSET
164     #define DEC_Lost_digits	     0x00000000
165     #endif
166     #define DEC_Overflow	     0x00000008
167     #define DEC_Clamped		     0x00000000
168     #define DEC_Rounded		     0x00000000
169     #define DEC_Subnormal	     0x00000000
170     #define DEC_Underflow	     0x00000004
171   #endif
172 
173   /* IEEE 854 groupings for the flags				      */
174   /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal    */
175   /* are not in IEEE 854]					      */
176   #define DEC_IEEE_854_Division_by_zero	 (DEC_Division_by_zero)
177   #if DECSUBSET
178   #define DEC_IEEE_854_Inexact		 (DEC_Inexact | DEC_Lost_digits)
179   #else
180   #define DEC_IEEE_854_Inexact		 (DEC_Inexact)
181   #endif
182   #define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax |     \
183 					  DEC_Division_impossible |   \
184 					  DEC_Division_undefined |    \
185 					  DEC_Insufficient_storage |  \
186 					  DEC_Invalid_context |	      \
187 					  DEC_Invalid_operation)
188   #define DEC_IEEE_854_Overflow		 (DEC_Overflow)
189   #define DEC_IEEE_854_Underflow	 (DEC_Underflow)
190 
191   /* flags which are normally errors (result is qNaN, infinite, or 0) */
192   #define DEC_Errors (DEC_IEEE_854_Division_by_zero |		      \
193 		      DEC_IEEE_854_Invalid_operation |		      \
194 		      DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
195   /* flags which cause a result to become qNaN			      */
196   #define DEC_NaNs    DEC_IEEE_854_Invalid_operation
197 
198   /* flags which are normally for information only (finite results)   */
199   #if DECSUBSET
200   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact    \
201 			  | DEC_Lost_digits)
202   #else
203   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
204   #endif
205 
206   /* Name strings for the exceptional conditions		      */
207   #define DEC_Condition_CS "Conversion syntax"
208   #define DEC_Condition_DZ "Division by zero"
209   #define DEC_Condition_DI "Division impossible"
210   #define DEC_Condition_DU "Division undefined"
211   #define DEC_Condition_IE "Inexact"
212   #define DEC_Condition_IS "Insufficient storage"
213   #define DEC_Condition_IC "Invalid context"
214   #define DEC_Condition_IO "Invalid operation"
215   #if DECSUBSET
216   #define DEC_Condition_LD "Lost digits"
217   #endif
218   #define DEC_Condition_OV "Overflow"
219   #define DEC_Condition_PA "Clamped"
220   #define DEC_Condition_RO "Rounded"
221   #define DEC_Condition_SU "Subnormal"
222   #define DEC_Condition_UN "Underflow"
223   #define DEC_Condition_ZE "No status"
224   #define DEC_Condition_MU "Multiple status"
225   #define DEC_Condition_Length 21  /* length of the longest string,   */
226 				   /* including terminator	      */
227 
228   /* Initialization descriptors, used by decContextDefault	      */
229   #define DEC_INIT_BASE		0
230   #define DEC_INIT_DECIMAL32   32
231   #define DEC_INIT_DECIMAL64   64
232   #define DEC_INIT_DECIMAL128 128
233   /* Synonyms */
234   #define DEC_INIT_DECSINGLE  DEC_INIT_DECIMAL32
235   #define DEC_INIT_DECDOUBLE  DEC_INIT_DECIMAL64
236   #define DEC_INIT_DECQUAD    DEC_INIT_DECIMAL128
237 
238   /* decContext routines					      */
239 
240 
241   extern decContext  * decContextClearStatus(decContext *, uint32_t);
242   extern decContext  * decContextDefault(decContext *, int32_t);
243   extern enum rounding decContextGetRounding(decContext *);
244   extern uint32_t      decContextGetStatus(decContext *);
245   extern decContext  * decContextRestoreStatus(decContext *, uint32_t, uint32_t);
246   extern uint32_t      decContextSaveStatus(decContext *, uint32_t);
247   extern decContext  * decContextSetRounding(decContext *, enum rounding);
248   extern decContext  * decContextSetStatus(decContext *, uint32_t);
249   extern decContext  * decContextSetStatusFromString(decContext *, const char *);
250   extern decContext  * decContextSetStatusFromStringQuiet(decContext *, const char *);
251   extern decContext  * decContextSetStatusQuiet(decContext *, uint32_t);
252   extern const char  * decContextStatusToString(const decContext *);
253   extern uint32_t      decContextTestSavedStatus(uint32_t, uint32_t);
254   extern uint32_t      decContextTestStatus(decContext *, uint32_t);
255   extern decContext  * decContextZeroStatus(decContext *);
256 
257 #endif
258