1169695Skan/* Decimal Context module header for the decNumber C Library
2169695Skan   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3169695Skan   Contributed by IBM Corporation.  Author Mike Cowlishaw.
4169695Skan
5169695Skan   This file is part of GCC.
6169695Skan
7169695Skan   GCC is free software; you can redistribute it and/or modify it under
8169695Skan   the terms of the GNU General Public License as published by the Free
9169695Skan   Software Foundation; either version 2, or (at your option) any later
10169695Skan   version.
11169695Skan
12169695Skan   In addition to the permissions in the GNU General Public License,
13169695Skan   the Free Software Foundation gives you unlimited permission to link
14169695Skan   the compiled version of this file into combinations with other
15169695Skan   programs, and to distribute those combinations without any
16169695Skan   restriction coming from the use of this file.  (The General Public
17169695Skan   License restrictions do apply in other respects; for example, they
18169695Skan   cover modification of the file, and distribution when not linked
19169695Skan   into a combine executable.)
20169695Skan
21169695Skan   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22169695Skan   WARRANTY; without even the implied warranty of MERCHANTABILITY or
23169695Skan   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24169695Skan   for more details.
25169695Skan
26169695Skan   You should have received a copy of the GNU General Public License
27169695Skan   along with GCC; see the file COPYING.  If not, write to the Free
28169695Skan   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29169695Skan   02110-1301, USA.  */
30169695Skan
31169695Skan/* ------------------------------------------------------------------ */
32169695Skan/*                                                                    */
33169695Skan/* Context must always be set correctly:                              */
34169695Skan/*                                                                    */
35169695Skan/*  digits   -- must be in the range 1 through 999999999              */
36169695Skan/*  emax     -- must be in the range 0 through 999999999              */
37169695Skan/*  emin     -- must be in the range 0 through -999999999             */
38169695Skan/*  round    -- must be one of the enumerated rounding modes          */
39169695Skan/*  traps    -- only defined bits may be set                          */
40169695Skan/*  status   -- [any bits may be cleared, but not set, by user]       */
41169695Skan/*  clamp    -- must be either 0 or 1                                 */
42169695Skan/*  extended -- must be either 0 or 1 [present only if DECSUBSET]     */
43169695Skan/*                                                                    */
44169695Skan/* ------------------------------------------------------------------ */
45169695Skan
46169695Skan#if !defined(DECCONTEXT)
47169695Skan#define DECCONTEXT
48169695Skan#define DECCNAME     "decContext"	/* Short name */
49169695Skan#define DECCFULLNAME "Decimal Context Descriptor"	/* Verbose name */
50169695Skan#define DECCAUTHOR   "Mike Cowlishaw"	/* Who to blame */
51169695Skan
52169695Skan#include "gstdint.h"		/* C99 standard integers */
53169695Skan#include <signal.h>		/* for traps */
54169695Skan
55169695Skan
56169695Skan  /* Conditional code flag -- set this to 0 for best performance */
57169695Skan#define DECSUBSET 0		/* 1 to enable subset arithmetic */
58169695Skan
59169695Skan  /* Context for operations, with associated constants */
60169695Skanenum rounding
61169695Skan{
62169695Skan  DEC_ROUND_CEILING,		/* round towards +infinity */
63169695Skan  DEC_ROUND_UP,			/* round away from 0 */
64169695Skan  DEC_ROUND_HALF_UP,		/* 0.5 rounds up */
65169695Skan  DEC_ROUND_HALF_EVEN,		/* 0.5 rounds to nearest even */
66169695Skan  DEC_ROUND_HALF_DOWN,		/* 0.5 rounds down */
67169695Skan  DEC_ROUND_DOWN,		/* round towards 0 (truncate) */
68169695Skan  DEC_ROUND_FLOOR,		/* round towards -infinity */
69169695Skan  DEC_ROUND_MAX			/* enum must be less than this */
70169695Skan};
71169695Skan
72169695Skantypedef struct
73169695Skan{
74169695Skan  int32_t digits;		/* working precision */
75169695Skan  int32_t emax;			/* maximum positive exponent */
76169695Skan  int32_t emin;			/* minimum negative exponent */
77169695Skan  enum rounding round;		/* rounding mode */
78169695Skan  uint32_t traps;		/* trap-enabler flags */
79169695Skan  uint32_t status;		/* status flags */
80169695Skan  uint8_t clamp;		/* flag: apply IEEE exponent clamp */
81169695Skan#if DECSUBSET
82169695Skan  uint8_t extended;		/* flag: special-values allowed */
83169695Skan#endif
84169695Skan} decContext;
85169695Skan
86169695Skan  /* Maxima and Minima */
87169695Skan#define DEC_MAX_DIGITS 999999999
88169695Skan#define DEC_MIN_DIGITS         1
89169695Skan#define DEC_MAX_EMAX   999999999
90169695Skan#define DEC_MIN_EMAX           0
91169695Skan#define DEC_MAX_EMIN           0
92169695Skan#define DEC_MIN_EMIN  -999999999
93169695Skan
94169695Skan  /* Trap-enabler and Status flags (exceptional conditions), and their names */
95169695Skan  /* Top byte is reserved for internal use */
96169695Skan#define DEC_Conversion_syntax    0x00000001
97169695Skan#define DEC_Division_by_zero     0x00000002
98169695Skan#define DEC_Division_impossible  0x00000004
99169695Skan#define DEC_Division_undefined   0x00000008
100169695Skan#define DEC_Insufficient_storage 0x00000010	/* [used if malloc fails] */
101169695Skan#define DEC_Inexact              0x00000020
102169695Skan#define DEC_Invalid_context      0x00000040
103169695Skan#define DEC_Invalid_operation    0x00000080
104169695Skan#if DECSUBSET
105169695Skan#define DEC_Lost_digits          0x00000100
106169695Skan#endif
107169695Skan#define DEC_Overflow             0x00000200
108169695Skan#define DEC_Clamped              0x00000400
109169695Skan#define DEC_Rounded              0x00000800
110169695Skan#define DEC_Subnormal            0x00001000
111169695Skan#define DEC_Underflow            0x00002000
112169695Skan
113169695Skan  /* IEEE 854 groupings for the flags */
114169695Skan  /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal are */
115169695Skan  /* not in IEEE 854] */
116169695Skan#define DEC_IEEE_854_Division_by_zero  (DEC_Division_by_zero)
117169695Skan#if DECSUBSET
118169695Skan#define DEC_IEEE_854_Inexact           (DEC_Inexact | DEC_Lost_digits)
119169695Skan#else
120169695Skan#define DEC_IEEE_854_Inexact           (DEC_Inexact)
121169695Skan#endif
122169695Skan#define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax |     \
123169695Skan                                          DEC_Division_impossible |   \
124169695Skan                                          DEC_Division_undefined |    \
125169695Skan                                          DEC_Insufficient_storage |  \
126169695Skan                                          DEC_Invalid_context |       \
127169695Skan                                          DEC_Invalid_operation)
128169695Skan#define DEC_IEEE_854_Overflow          (DEC_Overflow)
129169695Skan#define DEC_IEEE_854_Underflow         (DEC_Underflow)
130169695Skan
131169695Skan  /* flags which are normally errors (results are qNaN, infinite, or 0) */
132169695Skan#define DEC_Errors (DEC_IEEE_854_Division_by_zero |                 \
133169695Skan                      DEC_IEEE_854_Invalid_operation |                \
134169695Skan                      DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
135169695Skan  /* flags which cause a result to become qNaN */
136169695Skan#define DEC_NaNs    DEC_IEEE_854_Invalid_operation
137169695Skan
138169695Skan  /* flags which are normally for information only (have finite results) */
139169695Skan#if DECSUBSET
140169695Skan#define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact     \
141169695Skan                          | DEC_Lost_digits)
142169695Skan#else
143169695Skan#define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
144169695Skan#endif
145169695Skan
146169695Skan  /* name strings for the exceptional conditions */
147169695Skan
148169695Skan#define DEC_Condition_CS "Conversion syntax"
149169695Skan#define DEC_Condition_DZ "Division by zero"
150169695Skan#define DEC_Condition_DI "Division impossible"
151169695Skan#define DEC_Condition_DU "Division undefined"
152169695Skan#define DEC_Condition_IE "Inexact"
153169695Skan#define DEC_Condition_IS "Insufficient storage"
154169695Skan#define DEC_Condition_IC "Invalid context"
155169695Skan#define DEC_Condition_IO "Invalid operation"
156169695Skan#if DECSUBSET
157169695Skan#define DEC_Condition_LD "Lost digits"
158169695Skan#endif
159169695Skan#define DEC_Condition_OV "Overflow"
160169695Skan#define DEC_Condition_PA "Clamped"
161169695Skan#define DEC_Condition_RO "Rounded"
162169695Skan#define DEC_Condition_SU "Subnormal"
163169695Skan#define DEC_Condition_UN "Underflow"
164169695Skan#define DEC_Condition_ZE "No status"
165169695Skan#define DEC_Condition_MU "Multiple status"
166169695Skan#define DEC_Condition_Length 21	/* length of the longest string, */
167169695Skan				   /* including terminator */
168169695Skan
169169695Skan  /* Initialization descriptors, used by decContextDefault */
170169695Skan#define DEC_INIT_BASE         0
171169695Skan#define DEC_INIT_DECIMAL32   32
172169695Skan#define DEC_INIT_DECIMAL64   64
173169695Skan#define DEC_INIT_DECIMAL128 128
174169695Skan
175169695Skan  /* decContext routines */
176169695Skan#ifdef IN_LIBGCC2
177169695Skan#define decContextDefault __decContextDefault
178169695Skan#define decContextSetStatus __decContextSetStatus
179169695Skan#define decContextStatusToString __decContextStatusToString
180169695Skan#define decContextSetStatusFromString __decContextSetStatusFromString
181169695Skan#endif
182169695SkandecContext *decContextDefault (decContext *, int32_t);
183169695SkandecContext *decContextSetStatus (decContext *, uint32_t);
184169695Skanconst char *decContextStatusToString (const decContext *);
185169695SkandecContext *decContextSetStatusFromString (decContext *, const char *);
186169695Skan
187169695Skan#endif
188