]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/gcclibs/libdecnumber/decRound.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / gcclibs / 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
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    In addition to the permissions in the GNU General Public License,
12    the Free Software Foundation gives you unlimited permission to link
13    the compiled version of this file into combinations with other
14    programs, and to distribute those combinations without any
15    restriction coming from the use of this file.  (The General Public
16    License restrictions do apply in other respects; for example, they
17    cover modification of the file, and distribution when not linked
18    into a combine executable.)
19
20    GCC is distributed in the hope that it will be useful, but WITHOUT
21    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
23    License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with GCC; see the file COPYING.  If not, write to the Free
27    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
28    02110-1301, USA.  */
29
30 #include "config.h"
31 #include "decContext.h"
32
33 #define FE_DEC_DOWNWARD 0
34 #define FE_DEC_TONEAREST 1
35 #define FE_DEC_TONEARESTFROMZERO 2
36 #define FE_DEC_TOWARDZERO 3
37 #define FE_DEC_UPWARD 4
38 #define FE_DEC_MAX 5
39
40 extern void __dfp_set_round (int);
41 extern int __dfp_get_round (void);
42 extern enum rounding __decGetRound (void);
43
44 /* FIXME: these should be in thread-local storage for runtime support.  */
45 static enum rounding __dfp_rounding_mode = DEC_ROUND_HALF_EVEN;
46
47 /* Set the decNumber rounding mode from the FE_DEC_* value in MODE.  */ 
48
49 void
50 __dfp_set_round (int mode)
51 {
52   switch (mode)
53     {
54     case FE_DEC_DOWNWARD:
55       __dfp_rounding_mode = DEC_ROUND_FLOOR; break;
56     case FE_DEC_TONEAREST:
57       __dfp_rounding_mode = DEC_ROUND_HALF_EVEN; break;
58     case FE_DEC_TONEARESTFROMZERO:
59       __dfp_rounding_mode = DEC_ROUND_HALF_UP; break;
60     case FE_DEC_TOWARDZERO:
61       __dfp_rounding_mode = DEC_ROUND_DOWN; break;
62     case FE_DEC_UPWARD:
63       __dfp_rounding_mode = DEC_ROUND_CEILING; break;
64     default:
65      /* We can't use assert in libgcc, so just return the default mode.  */
66       __dfp_rounding_mode = DEC_ROUND_HALF_EVEN; break;
67     }
68 }
69
70 /* Return the decNumber rounding mode as an FE_DEC_* value.  */
71
72 int
73 __dfp_get_round (void)
74 {
75   int mode;
76
77   switch (__dfp_rounding_mode)
78     {
79     case DEC_ROUND_FLOOR:
80       mode = FE_DEC_DOWNWARD; break;
81     case DEC_ROUND_HALF_EVEN:
82       mode = FE_DEC_TONEAREST; break;
83     case DEC_ROUND_HALF_UP:
84       mode = FE_DEC_TONEARESTFROMZERO; break;
85     case DEC_ROUND_DOWN:
86       mode = FE_DEC_TOWARDZERO; break;
87     case DEC_ROUND_CEILING:
88       mode = FE_DEC_UPWARD; break;
89     default:
90       /* We shouldn't get here, but can't use assert in libgcc.  */
91       mode = -1;
92     }
93   return mode;
94 }
95
96 /* Return the decNumber version of the current rounding mode.  */
97
98 enum rounding
99 __decGetRound (void)
100 {
101   return __dfp_rounding_mode;
102 }