xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/quadmath-rounding-mode.h (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* GCC Quad-Precision Math Library
2*181254a7Smrg    Copyright (C) 2012 Free Software Foundation, Inc.
3*181254a7Smrg 
4*181254a7Smrg This file is part of the libquadmath library.
5*181254a7Smrg Libquadmath is free software; you can redistribute it and/or
6*181254a7Smrg modify it under the terms of the GNU Library General Public
7*181254a7Smrg License as published by the Free Software Foundation; either
8*181254a7Smrg version 2 of the License, or (at your option) any later version.
9*181254a7Smrg 
10*181254a7Smrg Libquadmath is distributed in the hope that it will be useful,
11*181254a7Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
12*181254a7Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*181254a7Smrg Library General Public License for more details.
14*181254a7Smrg 
15*181254a7Smrg You should have received a copy of the GNU Library General Public
16*181254a7Smrg License along with libquadmath; see the file COPYING.LIB.  If
17*181254a7Smrg not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18*181254a7Smrg Boston, MA 02110-1301, USA.  */
19*181254a7Smrg 
20*181254a7Smrg 
21*181254a7Smrg #ifndef QUADMATH_ROUNDING_MODE_H
22*181254a7Smrg #define QUADMATH_ROUNDING_MODE_H
23*181254a7Smrg 
24*181254a7Smrg #include <stdbool.h>
25*181254a7Smrg 
26*181254a7Smrg 
27*181254a7Smrg #if defined(HAVE_FENV_H)
28*181254a7Smrg #  include <fenv.h>
29*181254a7Smrg #endif /* HAVE_FENV_H */
30*181254a7Smrg 
31*181254a7Smrg static inline int
get_rounding_mode(void)32*181254a7Smrg get_rounding_mode (void)
33*181254a7Smrg {
34*181254a7Smrg #if defined(HAVE_FENV_H) && (defined(FE_DOWNWARD) || defined(FE_TONEAREST) \
35*181254a7Smrg 			     || defined(FE_TOWARDZERO) || defined(FE_UPWARD))
36*181254a7Smrg   return fegetround ();
37*181254a7Smrg #else
38*181254a7Smrg   return 0;
39*181254a7Smrg #endif
40*181254a7Smrg }
41*181254a7Smrg 
42*181254a7Smrg static inline bool
round_away(bool negative,bool last_digit_odd,bool half_bit,bool more_bits,int mode)43*181254a7Smrg round_away (bool negative, bool last_digit_odd, bool half_bit, bool more_bits,
44*181254a7Smrg             int mode)
45*181254a7Smrg {
46*181254a7Smrg   switch (mode)
47*181254a7Smrg     {
48*181254a7Smrg #ifdef FE_DOWNWARD
49*181254a7Smrg     case FE_DOWNWARD:
50*181254a7Smrg       return negative && (half_bit || more_bits);
51*181254a7Smrg #endif
52*181254a7Smrg 
53*181254a7Smrg #ifdef FE_DOWNWARD
54*181254a7Smrg     case FE_TONEAREST:
55*181254a7Smrg       return half_bit && (last_digit_odd || more_bits);
56*181254a7Smrg #endif
57*181254a7Smrg 
58*181254a7Smrg #ifdef FE_TOWARDZERO
59*181254a7Smrg     case FE_TOWARDZERO:
60*181254a7Smrg       return false;
61*181254a7Smrg #endif
62*181254a7Smrg 
63*181254a7Smrg 
64*181254a7Smrg #ifdef FE_UPWARD
65*181254a7Smrg     case FE_UPWARD:
66*181254a7Smrg       return !negative && (half_bit || more_bits);
67*181254a7Smrg #endif
68*181254a7Smrg 
69*181254a7Smrg     default:
70*181254a7Smrg       return false;
71*181254a7Smrg     }
72*181254a7Smrg }
73*181254a7Smrg 
74*181254a7Smrg #endif /* QUADMATH_ROUNDING_MODE_H  */
75