xref: /openbsd-src/gnu/gcc/libdecnumber/decContext.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Decimal Context module header for the decNumber C Library
2*404b540aSrobert    Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3*404b540aSrobert    Contributed by IBM Corporation.  Author Mike Cowlishaw.
4*404b540aSrobert 
5*404b540aSrobert    This file is part of GCC.
6*404b540aSrobert 
7*404b540aSrobert    GCC is free software; you can redistribute it and/or modify it under
8*404b540aSrobert    the terms of the GNU General Public License as published by the Free
9*404b540aSrobert    Software Foundation; either version 2, or (at your option) any later
10*404b540aSrobert    version.
11*404b540aSrobert 
12*404b540aSrobert    In addition to the permissions in the GNU General Public License,
13*404b540aSrobert    the Free Software Foundation gives you unlimited permission to link
14*404b540aSrobert    the compiled version of this file into combinations with other
15*404b540aSrobert    programs, and to distribute those combinations without any
16*404b540aSrobert    restriction coming from the use of this file.  (The General Public
17*404b540aSrobert    License restrictions do apply in other respects; for example, they
18*404b540aSrobert    cover modification of the file, and distribution when not linked
19*404b540aSrobert    into a combine executable.)
20*404b540aSrobert 
21*404b540aSrobert    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22*404b540aSrobert    WARRANTY; without even the implied warranty of MERCHANTABILITY or
23*404b540aSrobert    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24*404b540aSrobert    for more details.
25*404b540aSrobert 
26*404b540aSrobert    You should have received a copy of the GNU General Public License
27*404b540aSrobert    along with GCC; see the file COPYING.  If not, write to the Free
28*404b540aSrobert    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29*404b540aSrobert    02110-1301, USA.  */
30*404b540aSrobert 
31*404b540aSrobert /* ------------------------------------------------------------------ */
32*404b540aSrobert /*                                                                    */
33*404b540aSrobert /* Context must always be set correctly:                              */
34*404b540aSrobert /*                                                                    */
35*404b540aSrobert /*  digits   -- must be in the range 1 through 999999999              */
36*404b540aSrobert /*  emax     -- must be in the range 0 through 999999999              */
37*404b540aSrobert /*  emin     -- must be in the range 0 through -999999999             */
38*404b540aSrobert /*  round    -- must be one of the enumerated rounding modes          */
39*404b540aSrobert /*  traps    -- only defined bits may be set                          */
40*404b540aSrobert /*  status   -- [any bits may be cleared, but not set, by user]       */
41*404b540aSrobert /*  clamp    -- must be either 0 or 1                                 */
42*404b540aSrobert /*  extended -- must be either 0 or 1 [present only if DECSUBSET]     */
43*404b540aSrobert /*                                                                    */
44*404b540aSrobert /* ------------------------------------------------------------------ */
45*404b540aSrobert 
46*404b540aSrobert #if !defined(DECCONTEXT)
47*404b540aSrobert #define DECCONTEXT
48*404b540aSrobert #define DECCNAME     "decContext"	/* Short name */
49*404b540aSrobert #define DECCFULLNAME "Decimal Context Descriptor"	/* Verbose name */
50*404b540aSrobert #define DECCAUTHOR   "Mike Cowlishaw"	/* Who to blame */
51*404b540aSrobert 
52*404b540aSrobert #include "gstdint.h"		/* C99 standard integers */
53*404b540aSrobert #include <signal.h>		/* for traps */
54*404b540aSrobert 
55*404b540aSrobert 
56*404b540aSrobert   /* Conditional code flag -- set this to 0 for best performance */
57*404b540aSrobert #define DECSUBSET 0		/* 1 to enable subset arithmetic */
58*404b540aSrobert 
59*404b540aSrobert   /* Context for operations, with associated constants */
60*404b540aSrobert enum rounding
61*404b540aSrobert {
62*404b540aSrobert   DEC_ROUND_CEILING,		/* round towards +infinity */
63*404b540aSrobert   DEC_ROUND_UP,			/* round away from 0 */
64*404b540aSrobert   DEC_ROUND_HALF_UP,		/* 0.5 rounds up */
65*404b540aSrobert   DEC_ROUND_HALF_EVEN,		/* 0.5 rounds to nearest even */
66*404b540aSrobert   DEC_ROUND_HALF_DOWN,		/* 0.5 rounds down */
67*404b540aSrobert   DEC_ROUND_DOWN,		/* round towards 0 (truncate) */
68*404b540aSrobert   DEC_ROUND_FLOOR,		/* round towards -infinity */
69*404b540aSrobert   DEC_ROUND_MAX			/* enum must be less than this */
70*404b540aSrobert };
71*404b540aSrobert 
72*404b540aSrobert typedef struct
73*404b540aSrobert {
74*404b540aSrobert   int32_t digits;		/* working precision */
75*404b540aSrobert   int32_t emax;			/* maximum positive exponent */
76*404b540aSrobert   int32_t emin;			/* minimum negative exponent */
77*404b540aSrobert   enum rounding round;		/* rounding mode */
78*404b540aSrobert   uint32_t traps;		/* trap-enabler flags */
79*404b540aSrobert   uint32_t status;		/* status flags */
80*404b540aSrobert   uint8_t clamp;		/* flag: apply IEEE exponent clamp */
81*404b540aSrobert #if DECSUBSET
82*404b540aSrobert   uint8_t extended;		/* flag: special-values allowed */
83*404b540aSrobert #endif
84*404b540aSrobert } decContext;
85*404b540aSrobert 
86*404b540aSrobert   /* Maxima and Minima */
87*404b540aSrobert #define DEC_MAX_DIGITS 999999999
88*404b540aSrobert #define DEC_MIN_DIGITS         1
89*404b540aSrobert #define DEC_MAX_EMAX   999999999
90*404b540aSrobert #define DEC_MIN_EMAX           0
91*404b540aSrobert #define DEC_MAX_EMIN           0
92*404b540aSrobert #define DEC_MIN_EMIN  -999999999
93*404b540aSrobert 
94*404b540aSrobert   /* Trap-enabler and Status flags (exceptional conditions), and their names */
95*404b540aSrobert   /* Top byte is reserved for internal use */
96*404b540aSrobert #define DEC_Conversion_syntax    0x00000001
97*404b540aSrobert #define DEC_Division_by_zero     0x00000002
98*404b540aSrobert #define DEC_Division_impossible  0x00000004
99*404b540aSrobert #define DEC_Division_undefined   0x00000008
100*404b540aSrobert #define DEC_Insufficient_storage 0x00000010	/* [used if malloc fails] */
101*404b540aSrobert #define DEC_Inexact              0x00000020
102*404b540aSrobert #define DEC_Invalid_context      0x00000040
103*404b540aSrobert #define DEC_Invalid_operation    0x00000080
104*404b540aSrobert #if DECSUBSET
105*404b540aSrobert #define DEC_Lost_digits          0x00000100
106*404b540aSrobert #endif
107*404b540aSrobert #define DEC_Overflow             0x00000200
108*404b540aSrobert #define DEC_Clamped              0x00000400
109*404b540aSrobert #define DEC_Rounded              0x00000800
110*404b540aSrobert #define DEC_Subnormal            0x00001000
111*404b540aSrobert #define DEC_Underflow            0x00002000
112*404b540aSrobert 
113*404b540aSrobert   /* IEEE 854 groupings for the flags */
114*404b540aSrobert   /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal are */
115*404b540aSrobert   /* not in IEEE 854] */
116*404b540aSrobert #define DEC_IEEE_854_Division_by_zero  (DEC_Division_by_zero)
117*404b540aSrobert #if DECSUBSET
118*404b540aSrobert #define DEC_IEEE_854_Inexact           (DEC_Inexact | DEC_Lost_digits)
119*404b540aSrobert #else
120*404b540aSrobert #define DEC_IEEE_854_Inexact           (DEC_Inexact)
121*404b540aSrobert #endif
122*404b540aSrobert #define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax |     \
123*404b540aSrobert                                           DEC_Division_impossible |   \
124*404b540aSrobert                                           DEC_Division_undefined |    \
125*404b540aSrobert                                           DEC_Insufficient_storage |  \
126*404b540aSrobert                                           DEC_Invalid_context |       \
127*404b540aSrobert                                           DEC_Invalid_operation)
128*404b540aSrobert #define DEC_IEEE_854_Overflow          (DEC_Overflow)
129*404b540aSrobert #define DEC_IEEE_854_Underflow         (DEC_Underflow)
130*404b540aSrobert 
131*404b540aSrobert   /* flags which are normally errors (results are qNaN, infinite, or 0) */
132*404b540aSrobert #define DEC_Errors (DEC_IEEE_854_Division_by_zero |                 \
133*404b540aSrobert                       DEC_IEEE_854_Invalid_operation |                \
134*404b540aSrobert                       DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
135*404b540aSrobert   /* flags which cause a result to become qNaN */
136*404b540aSrobert #define DEC_NaNs    DEC_IEEE_854_Invalid_operation
137*404b540aSrobert 
138*404b540aSrobert   /* flags which are normally for information only (have finite results) */
139*404b540aSrobert #if DECSUBSET
140*404b540aSrobert #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact     \
141*404b540aSrobert                           | DEC_Lost_digits)
142*404b540aSrobert #else
143*404b540aSrobert #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
144*404b540aSrobert #endif
145*404b540aSrobert 
146*404b540aSrobert   /* name strings for the exceptional conditions */
147*404b540aSrobert 
148*404b540aSrobert #define DEC_Condition_CS "Conversion syntax"
149*404b540aSrobert #define DEC_Condition_DZ "Division by zero"
150*404b540aSrobert #define DEC_Condition_DI "Division impossible"
151*404b540aSrobert #define DEC_Condition_DU "Division undefined"
152*404b540aSrobert #define DEC_Condition_IE "Inexact"
153*404b540aSrobert #define DEC_Condition_IS "Insufficient storage"
154*404b540aSrobert #define DEC_Condition_IC "Invalid context"
155*404b540aSrobert #define DEC_Condition_IO "Invalid operation"
156*404b540aSrobert #if DECSUBSET
157*404b540aSrobert #define DEC_Condition_LD "Lost digits"
158*404b540aSrobert #endif
159*404b540aSrobert #define DEC_Condition_OV "Overflow"
160*404b540aSrobert #define DEC_Condition_PA "Clamped"
161*404b540aSrobert #define DEC_Condition_RO "Rounded"
162*404b540aSrobert #define DEC_Condition_SU "Subnormal"
163*404b540aSrobert #define DEC_Condition_UN "Underflow"
164*404b540aSrobert #define DEC_Condition_ZE "No status"
165*404b540aSrobert #define DEC_Condition_MU "Multiple status"
166*404b540aSrobert #define DEC_Condition_Length 21	/* length of the longest string, */
167*404b540aSrobert 				   /* including terminator */
168*404b540aSrobert 
169*404b540aSrobert   /* Initialization descriptors, used by decContextDefault */
170*404b540aSrobert #define DEC_INIT_BASE         0
171*404b540aSrobert #define DEC_INIT_DECIMAL32   32
172*404b540aSrobert #define DEC_INIT_DECIMAL64   64
173*404b540aSrobert #define DEC_INIT_DECIMAL128 128
174*404b540aSrobert 
175*404b540aSrobert   /* decContext routines */
176*404b540aSrobert #ifdef IN_LIBGCC2
177*404b540aSrobert #define decContextDefault __decContextDefault
178*404b540aSrobert #define decContextSetStatus __decContextSetStatus
179*404b540aSrobert #define decContextStatusToString __decContextStatusToString
180*404b540aSrobert #define decContextSetStatusFromString __decContextSetStatusFromString
181*404b540aSrobert #endif
182*404b540aSrobert decContext *decContextDefault (decContext *, int32_t);
183*404b540aSrobert decContext *decContextSetStatus (decContext *, uint32_t);
184*404b540aSrobert const char *decContextStatusToString (const decContext *);
185*404b540aSrobert decContext *decContextSetStatusFromString (decContext *, const char *);
186*404b540aSrobert 
187*404b540aSrobert #endif
188