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