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