xref: /qemu/include/libdecnumber/decContext.h (revision ca3d87d4c84032f19478010b5604cac88b045c25)
172ac97cdSTom Musta /* Decimal context header module for the decNumber C Library.
272ac97cdSTom Musta    Copyright (C) 2005, 2007 Free Software Foundation, Inc.
372ac97cdSTom Musta    Contributed by IBM Corporation.  Author Mike Cowlishaw.
472ac97cdSTom Musta 
572ac97cdSTom Musta    This file is part of GCC.
672ac97cdSTom Musta 
772ac97cdSTom Musta    GCC is free software; you can redistribute it and/or modify it under
872ac97cdSTom Musta    the terms of the GNU General Public License as published by the Free
972ac97cdSTom Musta    Software Foundation; either version 2, or (at your option) any later
1072ac97cdSTom Musta    version.
1172ac97cdSTom Musta 
1272ac97cdSTom Musta    In addition to the permissions in the GNU General Public License,
1372ac97cdSTom Musta    the Free Software Foundation gives you unlimited permission to link
1472ac97cdSTom Musta    the compiled version of this file into combinations with other
1572ac97cdSTom Musta    programs, and to distribute those combinations without any
1672ac97cdSTom Musta    restriction coming from the use of this file.  (The General Public
1772ac97cdSTom Musta    License restrictions do apply in other respects; for example, they
1872ac97cdSTom Musta    cover modification of the file, and distribution when not linked
1972ac97cdSTom Musta    into a combine executable.)
2072ac97cdSTom Musta 
2172ac97cdSTom Musta    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
2272ac97cdSTom Musta    WARRANTY; without even the implied warranty of MERCHANTABILITY or
2372ac97cdSTom Musta    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2472ac97cdSTom Musta    for more details.
2572ac97cdSTom Musta 
2672ac97cdSTom Musta    You should have received a copy of the GNU General Public License
2772ac97cdSTom Musta    along with GCC; see the file COPYING.  If not, write to the Free
2872ac97cdSTom Musta    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2972ac97cdSTom Musta    02110-1301, USA.  */
3072ac97cdSTom Musta 
3172ac97cdSTom Musta /* ------------------------------------------------------------------ */
3272ac97cdSTom Musta /* Decimal Context module header				      */
3372ac97cdSTom Musta /* ------------------------------------------------------------------ */
3472ac97cdSTom Musta /*								      */
3572ac97cdSTom Musta /* Context variables must always have valid values:		      */
3672ac97cdSTom Musta /*								      */
3772ac97cdSTom Musta /*  status   -- [any bits may be cleared, but not set, by user]	      */
3872ac97cdSTom Musta /*  round    -- must be one of the enumerated rounding modes	      */
3972ac97cdSTom Musta /*								      */
4072ac97cdSTom Musta /* The following variables are implied for fixed size formats (i.e.,  */
4172ac97cdSTom Musta /* they are ignored) but should still be set correctly in case used   */
4272ac97cdSTom Musta /* with decNumber functions:					      */
4372ac97cdSTom Musta /*								      */
4472ac97cdSTom Musta /*  clamp    -- must be either 0 or 1				      */
4572ac97cdSTom Musta /*  digits   -- must be in the range 1 through 999999999	      */
4672ac97cdSTom Musta /*  emax     -- must be in the range 0 through 999999999	      */
4772ac97cdSTom Musta /*  emin     -- must be in the range 0 through -999999999	      */
4872ac97cdSTom Musta /*  extended -- must be either 0 or 1 [present only if DECSUBSET]     */
4972ac97cdSTom Musta /*  traps    -- only defined bits may be set			      */
5072ac97cdSTom Musta /*								      */
5172ac97cdSTom Musta /* ------------------------------------------------------------------ */
5272ac97cdSTom Musta 
53*2a6a4076SMarkus Armbruster #ifndef DECCONTEXT_H
54*2a6a4076SMarkus Armbruster #define DECCONTEXT_H
55*2a6a4076SMarkus Armbruster 
5672ac97cdSTom Musta   #define DECCNAME     "decContext"			/* Short name */
5772ac97cdSTom Musta   #define DECCFULLNAME "Decimal Context Descriptor"   /* Verbose name */
5872ac97cdSTom Musta   #define DECCAUTHOR   "Mike Cowlishaw"		      /* Who to blame */
5972ac97cdSTom Musta 
6072ac97cdSTom Musta 
6172ac97cdSTom Musta   /* Extended flags setting -- set this to 0 to use only IEEE flags   */
6272ac97cdSTom Musta   #define DECEXTFLAG 1		   /* 1=enable extended flags	      */
6372ac97cdSTom Musta 
6472ac97cdSTom Musta   /* Conditional code flag -- set this to 0 for best performance      */
6572ac97cdSTom Musta   #define DECSUBSET  0		   /* 1=enable subset arithmetic      */
6672ac97cdSTom Musta 
6772ac97cdSTom Musta   /* Context for operations, with associated constants		      */
6872ac97cdSTom Musta   enum rounding {
6972ac97cdSTom Musta     DEC_ROUND_CEILING,		   /* round towards +infinity	      */
7072ac97cdSTom Musta     DEC_ROUND_UP,		   /* round away from 0		      */
7172ac97cdSTom Musta     DEC_ROUND_HALF_UP,		   /* 0.5 rounds up		      */
7272ac97cdSTom Musta     DEC_ROUND_HALF_EVEN,	   /* 0.5 rounds to nearest even      */
7372ac97cdSTom Musta     DEC_ROUND_HALF_DOWN,	   /* 0.5 rounds down		      */
7472ac97cdSTom Musta     DEC_ROUND_DOWN,		   /* round towards 0 (truncate)      */
7572ac97cdSTom Musta     DEC_ROUND_FLOOR,		   /* round towards -infinity	      */
7672ac97cdSTom Musta     DEC_ROUND_05UP,		   /* round for reround		      */
7772ac97cdSTom Musta     DEC_ROUND_MAX		   /* enum must be less than this     */
7872ac97cdSTom Musta     };
7972ac97cdSTom Musta   #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
8072ac97cdSTom Musta 
8172ac97cdSTom Musta   typedef struct {
8272ac97cdSTom Musta     int32_t  digits;		   /* working precision		      */
8372ac97cdSTom Musta     int32_t  emax;		   /* maximum positive exponent	      */
8472ac97cdSTom Musta     int32_t  emin;		   /* minimum negative exponent	      */
8572ac97cdSTom Musta     enum     rounding round;	   /* rounding mode		      */
8672ac97cdSTom Musta     uint32_t traps;		   /* trap-enabler flags	      */
8772ac97cdSTom Musta     uint32_t status;		   /* status flags		      */
8872ac97cdSTom Musta     uint8_t  clamp;		   /* flag: apply IEEE exponent clamp */
8972ac97cdSTom Musta     #if DECSUBSET
9072ac97cdSTom Musta     uint8_t  extended;		   /* flag: special-values allowed    */
9172ac97cdSTom Musta     #endif
9272ac97cdSTom Musta     } decContext;
9372ac97cdSTom Musta 
9472ac97cdSTom Musta   /* Maxima and Minima for context settings			      */
9572ac97cdSTom Musta   #define DEC_MAX_DIGITS 999999999
9672ac97cdSTom Musta   #define DEC_MIN_DIGITS	 1
9772ac97cdSTom Musta   #define DEC_MAX_EMAX	 999999999
9872ac97cdSTom Musta   #define DEC_MIN_EMAX		 0
9972ac97cdSTom Musta   #define DEC_MAX_EMIN		 0
10072ac97cdSTom Musta   #define DEC_MIN_EMIN	-999999999
10172ac97cdSTom Musta   #define DEC_MAX_MATH	    999999 /* max emax, etc., for math funcs. */
10272ac97cdSTom Musta 
10372ac97cdSTom Musta   /* Classifications for decimal numbers, aligned with 754r (note     */
10472ac97cdSTom Musta   /* that 'normal' and 'subnormal' are meaningful only with a	      */
10572ac97cdSTom Musta   /* decContext or a fixed size format).			      */
10672ac97cdSTom Musta   enum decClass {
10772ac97cdSTom Musta     DEC_CLASS_SNAN,
10872ac97cdSTom Musta     DEC_CLASS_QNAN,
10972ac97cdSTom Musta     DEC_CLASS_NEG_INF,
11072ac97cdSTom Musta     DEC_CLASS_NEG_NORMAL,
11172ac97cdSTom Musta     DEC_CLASS_NEG_SUBNORMAL,
11272ac97cdSTom Musta     DEC_CLASS_NEG_ZERO,
11372ac97cdSTom Musta     DEC_CLASS_POS_ZERO,
11472ac97cdSTom Musta     DEC_CLASS_POS_SUBNORMAL,
11572ac97cdSTom Musta     DEC_CLASS_POS_NORMAL,
11672ac97cdSTom Musta     DEC_CLASS_POS_INF
11772ac97cdSTom Musta     };
11872ac97cdSTom Musta   /* Strings for the decClasses */
11972ac97cdSTom Musta   #define DEC_ClassString_SN  "sNaN"
12072ac97cdSTom Musta   #define DEC_ClassString_QN  "NaN"
12172ac97cdSTom Musta   #define DEC_ClassString_NI  "-Infinity"
12272ac97cdSTom Musta   #define DEC_ClassString_NN  "-Normal"
12372ac97cdSTom Musta   #define DEC_ClassString_NS  "-Subnormal"
12472ac97cdSTom Musta   #define DEC_ClassString_NZ  "-Zero"
12572ac97cdSTom Musta   #define DEC_ClassString_PZ  "+Zero"
12672ac97cdSTom Musta   #define DEC_ClassString_PS  "+Subnormal"
12772ac97cdSTom Musta   #define DEC_ClassString_PN  "+Normal"
12872ac97cdSTom Musta   #define DEC_ClassString_PI  "+Infinity"
12972ac97cdSTom Musta   #define DEC_ClassString_UN  "Invalid"
13072ac97cdSTom Musta 
13172ac97cdSTom Musta   /* Trap-enabler and Status flags (exceptional conditions), and      */
13272ac97cdSTom Musta   /* their names.  The top byte is reserved for internal use	      */
13372ac97cdSTom Musta   #if DECEXTFLAG
13472ac97cdSTom Musta     /* Extended flags */
13572ac97cdSTom Musta     #define DEC_Conversion_syntax    0x00000001
13672ac97cdSTom Musta     #define DEC_Division_by_zero     0x00000002
13772ac97cdSTom Musta     #define DEC_Division_impossible  0x00000004
13872ac97cdSTom Musta     #define DEC_Division_undefined   0x00000008
13972ac97cdSTom Musta     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]	*/
14072ac97cdSTom Musta     #define DEC_Inexact		     0x00000020
14172ac97cdSTom Musta     #define DEC_Invalid_context	     0x00000040
14272ac97cdSTom Musta     #define DEC_Invalid_operation    0x00000080
14372ac97cdSTom Musta     #if DECSUBSET
14472ac97cdSTom Musta     #define DEC_Lost_digits	     0x00000100
14572ac97cdSTom Musta     #endif
14672ac97cdSTom Musta     #define DEC_Overflow	     0x00000200
14772ac97cdSTom Musta     #define DEC_Clamped		     0x00000400
14872ac97cdSTom Musta     #define DEC_Rounded		     0x00000800
14972ac97cdSTom Musta     #define DEC_Subnormal	     0x00001000
15072ac97cdSTom Musta     #define DEC_Underflow	     0x00002000
15172ac97cdSTom Musta   #else
15272ac97cdSTom Musta     /* IEEE flags only */
15372ac97cdSTom Musta     #define DEC_Conversion_syntax    0x00000010
15472ac97cdSTom Musta     #define DEC_Division_by_zero     0x00000002
15572ac97cdSTom Musta     #define DEC_Division_impossible  0x00000010
15672ac97cdSTom Musta     #define DEC_Division_undefined   0x00000010
15772ac97cdSTom Musta     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]	*/
15872ac97cdSTom Musta     #define DEC_Inexact		     0x00000001
15972ac97cdSTom Musta     #define DEC_Invalid_context	     0x00000010
16072ac97cdSTom Musta     #define DEC_Invalid_operation    0x00000010
16172ac97cdSTom Musta     #if DECSUBSET
16272ac97cdSTom Musta     #define DEC_Lost_digits	     0x00000000
16372ac97cdSTom Musta     #endif
16472ac97cdSTom Musta     #define DEC_Overflow	     0x00000008
16572ac97cdSTom Musta     #define DEC_Clamped		     0x00000000
16672ac97cdSTom Musta     #define DEC_Rounded		     0x00000000
16772ac97cdSTom Musta     #define DEC_Subnormal	     0x00000000
16872ac97cdSTom Musta     #define DEC_Underflow	     0x00000004
16972ac97cdSTom Musta   #endif
17072ac97cdSTom Musta 
17172ac97cdSTom Musta   /* IEEE 854 groupings for the flags				      */
17272ac97cdSTom Musta   /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal    */
17372ac97cdSTom Musta   /* are not in IEEE 854]					      */
17472ac97cdSTom Musta   #define DEC_IEEE_854_Division_by_zero	 (DEC_Division_by_zero)
17572ac97cdSTom Musta   #if DECSUBSET
17672ac97cdSTom Musta   #define DEC_IEEE_854_Inexact		 (DEC_Inexact | DEC_Lost_digits)
17772ac97cdSTom Musta   #else
17872ac97cdSTom Musta   #define DEC_IEEE_854_Inexact		 (DEC_Inexact)
17972ac97cdSTom Musta   #endif
18072ac97cdSTom Musta   #define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax |     \
18172ac97cdSTom Musta 					  DEC_Division_impossible |   \
18272ac97cdSTom Musta 					  DEC_Division_undefined |    \
18372ac97cdSTom Musta 					  DEC_Insufficient_storage |  \
18472ac97cdSTom Musta 					  DEC_Invalid_context |	      \
18572ac97cdSTom Musta 					  DEC_Invalid_operation)
18672ac97cdSTom Musta   #define DEC_IEEE_854_Overflow		 (DEC_Overflow)
18772ac97cdSTom Musta   #define DEC_IEEE_854_Underflow	 (DEC_Underflow)
18872ac97cdSTom Musta 
18972ac97cdSTom Musta   /* flags which are normally errors (result is qNaN, infinite, or 0) */
19072ac97cdSTom Musta   #define DEC_Errors (DEC_IEEE_854_Division_by_zero |		      \
19172ac97cdSTom Musta 		      DEC_IEEE_854_Invalid_operation |		      \
19272ac97cdSTom Musta 		      DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
19372ac97cdSTom Musta   /* flags which cause a result to become qNaN			      */
19472ac97cdSTom Musta   #define DEC_NaNs    DEC_IEEE_854_Invalid_operation
19572ac97cdSTom Musta 
19672ac97cdSTom Musta   /* flags which are normally for information only (finite results)   */
19772ac97cdSTom Musta   #if DECSUBSET
19872ac97cdSTom Musta   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact    \
19972ac97cdSTom Musta 			  | DEC_Lost_digits)
20072ac97cdSTom Musta   #else
20172ac97cdSTom Musta   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
20272ac97cdSTom Musta   #endif
20372ac97cdSTom Musta 
20472ac97cdSTom Musta   /* Name strings for the exceptional conditions		      */
20572ac97cdSTom Musta   #define DEC_Condition_CS "Conversion syntax"
20672ac97cdSTom Musta   #define DEC_Condition_DZ "Division by zero"
20772ac97cdSTom Musta   #define DEC_Condition_DI "Division impossible"
20872ac97cdSTom Musta   #define DEC_Condition_DU "Division undefined"
20972ac97cdSTom Musta   #define DEC_Condition_IE "Inexact"
21072ac97cdSTom Musta   #define DEC_Condition_IS "Insufficient storage"
21172ac97cdSTom Musta   #define DEC_Condition_IC "Invalid context"
21272ac97cdSTom Musta   #define DEC_Condition_IO "Invalid operation"
21372ac97cdSTom Musta   #if DECSUBSET
21472ac97cdSTom Musta   #define DEC_Condition_LD "Lost digits"
21572ac97cdSTom Musta   #endif
21672ac97cdSTom Musta   #define DEC_Condition_OV "Overflow"
21772ac97cdSTom Musta   #define DEC_Condition_PA "Clamped"
21872ac97cdSTom Musta   #define DEC_Condition_RO "Rounded"
21972ac97cdSTom Musta   #define DEC_Condition_SU "Subnormal"
22072ac97cdSTom Musta   #define DEC_Condition_UN "Underflow"
22172ac97cdSTom Musta   #define DEC_Condition_ZE "No status"
22272ac97cdSTom Musta   #define DEC_Condition_MU "Multiple status"
22372ac97cdSTom Musta   #define DEC_Condition_Length 21  /* length of the longest string,   */
22472ac97cdSTom Musta 				   /* including terminator	      */
22572ac97cdSTom Musta 
22672ac97cdSTom Musta   /* Initialization descriptors, used by decContextDefault	      */
22772ac97cdSTom Musta   #define DEC_INIT_BASE		0
22872ac97cdSTom Musta   #define DEC_INIT_DECIMAL32   32
22972ac97cdSTom Musta   #define DEC_INIT_DECIMAL64   64
23072ac97cdSTom Musta   #define DEC_INIT_DECIMAL128 128
23172ac97cdSTom Musta   /* Synonyms */
23272ac97cdSTom Musta   #define DEC_INIT_DECSINGLE  DEC_INIT_DECIMAL32
23372ac97cdSTom Musta   #define DEC_INIT_DECDOUBLE  DEC_INIT_DECIMAL64
23472ac97cdSTom Musta   #define DEC_INIT_DECQUAD    DEC_INIT_DECIMAL128
23572ac97cdSTom Musta 
23672ac97cdSTom Musta   /* decContext routines					      */
23772ac97cdSTom Musta 
23872ac97cdSTom Musta 
23972ac97cdSTom Musta   extern decContext  * decContextClearStatus(decContext *, uint32_t);
24072ac97cdSTom Musta   extern decContext  * decContextDefault(decContext *, int32_t);
24172ac97cdSTom Musta   extern enum rounding decContextGetRounding(decContext *);
24272ac97cdSTom Musta   extern uint32_t      decContextGetStatus(decContext *);
24372ac97cdSTom Musta   extern decContext  * decContextRestoreStatus(decContext *, uint32_t, uint32_t);
24472ac97cdSTom Musta   extern uint32_t      decContextSaveStatus(decContext *, uint32_t);
24572ac97cdSTom Musta   extern decContext  * decContextSetRounding(decContext *, enum rounding);
24672ac97cdSTom Musta   extern decContext  * decContextSetStatus(decContext *, uint32_t);
24772ac97cdSTom Musta   extern decContext  * decContextSetStatusFromString(decContext *, const char *);
24872ac97cdSTom Musta   extern decContext  * decContextSetStatusFromStringQuiet(decContext *, const char *);
24972ac97cdSTom Musta   extern decContext  * decContextSetStatusQuiet(decContext *, uint32_t);
25072ac97cdSTom Musta   extern const char  * decContextStatusToString(const decContext *);
25172ac97cdSTom Musta   extern uint32_t      decContextTestSavedStatus(uint32_t, uint32_t);
25272ac97cdSTom Musta   extern uint32_t      decContextTestStatus(decContext *, uint32_t);
25372ac97cdSTom Musta   extern decContext  * decContextZeroStatus(decContext *);
25472ac97cdSTom Musta 
25572ac97cdSTom Musta #endif
256