decContext.h revision 169696
1240116Smarcel/* Decimal Context module header for the decNumber C Library
2240116Smarcel   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3240116Smarcel   Contributed by IBM Corporation.  Author Mike Cowlishaw.
4240116Smarcel
5240116Smarcel   This file is part of GCC.
6240116Smarcel
7240116Smarcel   GCC is free software; you can redistribute it and/or modify it under
8240116Smarcel   the terms of the GNU General Public License as published by the Free
9240116Smarcel   Software Foundation; either version 2, or (at your option) any later
10240116Smarcel   version.
11240116Smarcel
12240116Smarcel   In addition to the permissions in the GNU General Public License,
13240116Smarcel   the Free Software Foundation gives you unlimited permission to link
14240116Smarcel   the compiled version of this file into combinations with other
15240116Smarcel   programs, and to distribute those combinations without any
16240116Smarcel   restriction coming from the use of this file.  (The General Public
17240116Smarcel   License restrictions do apply in other respects; for example, they
18240116Smarcel   cover modification of the file, and distribution when not linked
19240116Smarcel   into a combine executable.)
20240116Smarcel
21240116Smarcel   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22240116Smarcel   WARRANTY; without even the implied warranty of MERCHANTABILITY or
23240116Smarcel   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24240116Smarcel   for more details.
25240116Smarcel
26240116Smarcel   You should have received a copy of the GNU General Public License
27240116Smarcel   along with GCC; see the file COPYING.  If not, write to the Free
28240116Smarcel   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29240116Smarcel   02110-1301, USA.  */
30240116Smarcel
31240116Smarcel/* ------------------------------------------------------------------ */
32240116Smarcel/*                                                                    */
33240116Smarcel/* Context must always be set correctly:                              */
34240116Smarcel/*                                                                    */
35240116Smarcel/*  digits   -- must be in the range 1 through 999999999              */
36240116Smarcel/*  emax     -- must be in the range 0 through 999999999              */
37240116Smarcel/*  emin     -- must be in the range 0 through -999999999             */
38240116Smarcel/*  round    -- must be one of the enumerated rounding modes          */
39240116Smarcel/*  traps    -- only defined bits may be set                          */
40240116Smarcel/*  status   -- [any bits may be cleared, but not set, by user]       */
41240116Smarcel/*  clamp    -- must be either 0 or 1                                 */
42240116Smarcel/*  extended -- must be either 0 or 1 [present only if DECSUBSET]     */
43240116Smarcel/*                                                                    */
44240116Smarcel/* ------------------------------------------------------------------ */
45240116Smarcel
46240116Smarcel#if !defined(DECCONTEXT)
47240116Smarcel#define DECCONTEXT
48240116Smarcel#define DECCNAME     "decContext"	/* Short name */
49240116Smarcel#define DECCFULLNAME "Decimal Context Descriptor"	/* Verbose name */
50240116Smarcel#define DECCAUTHOR   "Mike Cowlishaw"	/* Who to blame */
51240116Smarcel
52240116Smarcel#include "gstdint.h"		/* C99 standard integers */
53240116Smarcel#include <signal.h>		/* for traps */
54240116Smarcel
55240116Smarcel
56240116Smarcel  /* Conditional code flag -- set this to 0 for best performance */
57240116Smarcel#define DECSUBSET 0		/* 1 to enable subset arithmetic */
58240116Smarcel
59240116Smarcel  /* Context for operations, with associated constants */
60240116Smarcelenum rounding
61240116Smarcel{
62240116Smarcel  DEC_ROUND_CEILING,		/* round towards +infinity */
63240116Smarcel  DEC_ROUND_UP,			/* round away from 0 */
64240116Smarcel  DEC_ROUND_HALF_UP,		/* 0.5 rounds up */
65240116Smarcel  DEC_ROUND_HALF_EVEN,		/* 0.5 rounds to nearest even */
66240116Smarcel  DEC_ROUND_HALF_DOWN,		/* 0.5 rounds down */
67240116Smarcel  DEC_ROUND_DOWN,		/* round towards 0 (truncate) */
68240116Smarcel  DEC_ROUND_FLOOR,		/* round towards -infinity */
69240116Smarcel  DEC_ROUND_MAX			/* enum must be less than this */
70240116Smarcel};
71240116Smarcel
72240116Smarceltypedef struct
73240116Smarcel{
74240116Smarcel  int32_t digits;		/* working precision */
75240116Smarcel  int32_t emax;			/* maximum positive exponent */
76240116Smarcel  int32_t emin;			/* minimum negative exponent */
77240116Smarcel  enum rounding round;		/* rounding mode */
78240116Smarcel  uint32_t traps;		/* trap-enabler flags */
79240116Smarcel  uint32_t status;		/* status flags */
80240116Smarcel  uint8_t clamp;		/* flag: apply IEEE exponent clamp */
81240116Smarcel#if DECSUBSET
82240116Smarcel  uint8_t extended;		/* flag: special-values allowed */
83240116Smarcel#endif
84240116Smarcel} decContext;
85240116Smarcel
86240116Smarcel  /* Maxima and Minima */
87240116Smarcel#define DEC_MAX_DIGITS 999999999
88240116Smarcel#define DEC_MIN_DIGITS         1
89240116Smarcel#define DEC_MAX_EMAX   999999999
90240116Smarcel#define DEC_MIN_EMAX           0
91240116Smarcel#define DEC_MAX_EMIN           0
92240116Smarcel#define DEC_MIN_EMIN  -999999999
93240116Smarcel
94240116Smarcel  /* Trap-enabler and Status flags (exceptional conditions), and their names */
95240116Smarcel  /* Top byte is reserved for internal use */
96240116Smarcel#define DEC_Conversion_syntax    0x00000001
97240116Smarcel#define DEC_Division_by_zero     0x00000002
98240116Smarcel#define DEC_Division_impossible  0x00000004
99240116Smarcel#define DEC_Division_undefined   0x00000008
100240116Smarcel#define DEC_Insufficient_storage 0x00000010	/* [used if malloc fails] */
101240116Smarcel#define DEC_Inexact              0x00000020
102240116Smarcel#define DEC_Invalid_context      0x00000040
103240116Smarcel#define DEC_Invalid_operation    0x00000080
104240116Smarcel#if DECSUBSET
105240116Smarcel#define DEC_Lost_digits          0x00000100
106240116Smarcel#endif
107240116Smarcel#define DEC_Overflow             0x00000200
108240116Smarcel#define DEC_Clamped              0x00000400
109240116Smarcel#define DEC_Rounded              0x00000800
110240116Smarcel#define DEC_Subnormal            0x00001000
111240116Smarcel#define DEC_Underflow            0x00002000
112240116Smarcel
113240116Smarcel  /* IEEE 854 groupings for the flags */
114240116Smarcel  /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal are */
115240116Smarcel  /* not in IEEE 854] */
116240116Smarcel#define DEC_IEEE_854_Division_by_zero  (DEC_Division_by_zero)
117240116Smarcel#if DECSUBSET
118240116Smarcel#define DEC_IEEE_854_Inexact           (DEC_Inexact | DEC_Lost_digits)
119240116Smarcel#else
120#define DEC_IEEE_854_Inexact           (DEC_Inexact)
121#endif
122#define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax |     \
123                                          DEC_Division_impossible |   \
124                                          DEC_Division_undefined |    \
125                                          DEC_Insufficient_storage |  \
126                                          DEC_Invalid_context |       \
127                                          DEC_Invalid_operation)
128#define DEC_IEEE_854_Overflow          (DEC_Overflow)
129#define DEC_IEEE_854_Underflow         (DEC_Underflow)
130
131  /* flags which are normally errors (results are qNaN, infinite, or 0) */
132#define DEC_Errors (DEC_IEEE_854_Division_by_zero |                 \
133                      DEC_IEEE_854_Invalid_operation |                \
134                      DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
135  /* flags which cause a result to become qNaN */
136#define DEC_NaNs    DEC_IEEE_854_Invalid_operation
137
138  /* flags which are normally for information only (have finite results) */
139#if DECSUBSET
140#define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact     \
141                          | DEC_Lost_digits)
142#else
143#define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
144#endif
145
146  /* name strings for the exceptional conditions */
147
148#define DEC_Condition_CS "Conversion syntax"
149#define DEC_Condition_DZ "Division by zero"
150#define DEC_Condition_DI "Division impossible"
151#define DEC_Condition_DU "Division undefined"
152#define DEC_Condition_IE "Inexact"
153#define DEC_Condition_IS "Insufficient storage"
154#define DEC_Condition_IC "Invalid context"
155#define DEC_Condition_IO "Invalid operation"
156#if DECSUBSET
157#define DEC_Condition_LD "Lost digits"
158#endif
159#define DEC_Condition_OV "Overflow"
160#define DEC_Condition_PA "Clamped"
161#define DEC_Condition_RO "Rounded"
162#define DEC_Condition_SU "Subnormal"
163#define DEC_Condition_UN "Underflow"
164#define DEC_Condition_ZE "No status"
165#define DEC_Condition_MU "Multiple status"
166#define DEC_Condition_Length 21	/* length of the longest string, */
167				   /* including terminator */
168
169  /* Initialization descriptors, used by decContextDefault */
170#define DEC_INIT_BASE         0
171#define DEC_INIT_DECIMAL32   32
172#define DEC_INIT_DECIMAL64   64
173#define DEC_INIT_DECIMAL128 128
174
175  /* decContext routines */
176#ifdef IN_LIBGCC2
177#define decContextDefault __decContextDefault
178#define decContextSetStatus __decContextSetStatus
179#define decContextStatusToString __decContextStatusToString
180#define decContextSetStatusFromString __decContextSetStatusFromString
181#endif
182decContext *decContextDefault (decContext *, int32_t);
183decContext *decContextSetStatus (decContext *, uint32_t);
184const char *decContextStatusToString (const decContext *);
185decContext *decContextSetStatusFromString (decContext *, const char *);
186
187#endif
188