1 /* Inline functions operating on enum Rounding_Dir values.
2 Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3 Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4
5 This file is part of the Parma Polyhedra Library (PPL).
6
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23
24 #ifndef PPL_Rounding_Dir_inlines_hh
25 #define PPL_Rounding_Dir_inlines_hh 1
26
27 #include "assertions.hh"
28
29 namespace Parma_Polyhedra_Library {
30
31 /*! \ingroup PPL_CXX_interface */
32 inline Rounding_Dir
operator &(Rounding_Dir x,Rounding_Dir y)33 operator&(Rounding_Dir x, Rounding_Dir y) {
34 const unsigned res = static_cast<unsigned>(x) & static_cast<unsigned>(y);
35 return static_cast<Rounding_Dir>(res);
36 }
37
38 /*! \ingroup PPL_CXX_interface */
39 inline Rounding_Dir
operator |(Rounding_Dir x,Rounding_Dir y)40 operator|(Rounding_Dir x, Rounding_Dir y) {
41 const unsigned res = static_cast<unsigned>(x) | static_cast<unsigned>(y);
42 return static_cast<Rounding_Dir>(res);
43 }
44
45 /*! \ingroup PPL_CXX_interface */
46 inline Rounding_Dir
round_dir(Rounding_Dir dir)47 round_dir(Rounding_Dir dir) {
48 return dir & ROUND_DIR_MASK;
49 }
50
51 /*! \ingroup PPL_CXX_interface */
52 inline bool
round_down(Rounding_Dir dir)53 round_down(Rounding_Dir dir) {
54 return round_dir(dir) == ROUND_DOWN;
55 }
56
57 /*! \ingroup PPL_CXX_interface */
58 inline bool
round_up(Rounding_Dir dir)59 round_up(Rounding_Dir dir) {
60 return round_dir(dir) == ROUND_UP;
61 }
62
63 /*! \ingroup PPL_CXX_interface */
64 inline bool
round_ignore(Rounding_Dir dir)65 round_ignore(Rounding_Dir dir) {
66 return round_dir(dir) == ROUND_IGNORE;
67 }
68
69 /*! \ingroup PPL_CXX_interface */
70 inline bool
round_not_needed(Rounding_Dir dir)71 round_not_needed(Rounding_Dir dir) {
72 return round_dir(dir) == ROUND_NOT_NEEDED;
73 }
74
75 /*! \ingroup PPL_CXX_interface */
76 inline bool
round_not_requested(Rounding_Dir dir)77 round_not_requested(Rounding_Dir dir) {
78 return round_dir(dir) == ROUND_IGNORE || round_dir(dir) == ROUND_NOT_NEEDED;
79 }
80
81 /*! \ingroup PPL_CXX_interface */
82 inline bool
round_direct(Rounding_Dir dir)83 round_direct(Rounding_Dir dir) {
84 return round_dir(dir) == ROUND_DIRECT;
85 }
86
87 /*! \ingroup PPL_CXX_interface */
88 inline bool
round_inverse(Rounding_Dir dir)89 round_inverse(Rounding_Dir dir) {
90 return round_dir(dir) == ROUND_INVERSE;
91 }
92
93 /*! \ingroup PPL_CXX_interface */
94 inline bool
round_strict_relation(Rounding_Dir dir)95 round_strict_relation(Rounding_Dir dir) {
96 return (dir & ROUND_STRICT_RELATION) == ROUND_STRICT_RELATION;
97 }
98
99 #if PPL_CAN_CONTROL_FPU
100
101 /*! \ingroup PPL_CXX_interface */
102 inline fpu_rounding_direction_type
round_fpu_dir(Rounding_Dir dir)103 round_fpu_dir(Rounding_Dir dir) {
104 switch (round_dir(dir)) {
105 case ROUND_UP:
106 return static_cast<fpu_rounding_direction_type>(PPL_FPU_UPWARD);
107 case ROUND_DOWN:
108 return static_cast<fpu_rounding_direction_type>(PPL_FPU_DOWNWARD);
109 case ROUND_IGNORE: // Fall through.
110 default:
111 PPL_UNREACHABLE;
112 return static_cast<fpu_rounding_direction_type>(PPL_FPU_UPWARD);
113 }
114 }
115
116 #undef PPL_FPU_DOWNWARD
117 #undef PPL_FPU_TONEAREST
118 #undef PPL_FPU_TOWARDZERO
119 #undef PPL_FPU_UPWARD
120
121 #endif
122
123 /*! \ingroup PPL_CXX_interface */
124 inline Rounding_Dir
inverse(Rounding_Dir dir)125 inverse(Rounding_Dir dir) {
126 switch (round_dir(dir)) {
127 case ROUND_UP:
128 return ROUND_DOWN | (dir & ROUND_STRICT_RELATION);
129 case ROUND_DOWN:
130 return ROUND_UP | (dir & ROUND_STRICT_RELATION);
131 case ROUND_IGNORE:
132 return dir;
133 default:
134 PPL_UNREACHABLE;
135 return dir;
136 }
137 }
138
139 } // namespace Parma_Polyhedra_Library
140
141 #endif // !defined(PPL_Rounding_Dir_inlines_hh)
142