* libdecnumber: Import decNumber sources from the dfp-branch.
[gcc.git] / libdecnumber / decRound.c
1 /* Temporary support for a libc-like fp environment for decimal float.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 #include "decContext.h"
22
23 #define FE_DEC_DOWNWARD 0
24 #define FE_DEC_TONEAREST 1
25 #define FE_DEC_TONEARESTFROMZERO 2
26 #define FE_DEC_TOWARDZERO 3
27 #define FE_DEC_UPWARD 4
28 #define FE_DEC_MAX 5
29
30 extern void __dfp_set_round (int);
31 extern int __dfp_get_round (void);
32 extern enum rounding __decGetRound (void);
33
34 /* FIXME: these should be in thread-local storage for runtime support. */
35 static enum rounding __dfp_rounding_mode = DEC_ROUND_HALF_EVEN;
36
37 /* Set the decNumber rounding mode from the FE_DEC_* value in MODE. */
38
39 void
40 __dfp_set_round (int mode)
41 {
42 switch (mode)
43 {
44 case FE_DEC_DOWNWARD:
45 __dfp_rounding_mode = DEC_ROUND_FLOOR; break;
46 case FE_DEC_TONEAREST:
47 __dfp_rounding_mode = DEC_ROUND_HALF_EVEN; break;
48 case FE_DEC_TONEARESTFROMZERO:
49 __dfp_rounding_mode = DEC_ROUND_HALF_UP; break;
50 case FE_DEC_TOWARDZERO:
51 __dfp_rounding_mode = DEC_ROUND_DOWN; break;
52 case FE_DEC_UPWARD:
53 __dfp_rounding_mode = DEC_ROUND_CEILING; break;
54 default:
55 /* We can't use assert in libgcc, so just return the default mode. */
56 __dfp_rounding_mode = DEC_ROUND_HALF_EVEN; break;
57 }
58 }
59
60 /* Return the decNumber rounding mode as an FE_DEC_* value. */
61
62 int
63 __dfp_get_round (void)
64 {
65 int mode;
66
67 switch (__dfp_rounding_mode)
68 {
69 case DEC_ROUND_FLOOR:
70 mode = FE_DEC_DOWNWARD; break;
71 case DEC_ROUND_HALF_EVEN:
72 mode = FE_DEC_TONEAREST; break;
73 case DEC_ROUND_HALF_UP:
74 mode = FE_DEC_TONEARESTFROMZERO; break;
75 case DEC_ROUND_DOWN:
76 mode = FE_DEC_TOWARDZERO; break;
77 case DEC_ROUND_CEILING:
78 mode = FE_DEC_UPWARD; break;
79 default:
80 /* We shouldn't get here, but can't use assert in libgcc. */
81 mode = -1;
82 }
83 return mode;
84 }
85
86 /* Return the decNumber version of the current rounding mode. */
87
88 enum rounding
89 __decGetRound (void)
90 {
91 return __dfp_rounding_mode;
92 }