1 /* C99 Floating point unit related functions.
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_fpu_c99_inlines_hh
25 #define PPL_fpu_c99_inlines_hh 1
26
27 #ifdef PPL_HAVE_FENV_H
28 #include <fenv.h>
29 #include <stdexcept>
30
31 #ifdef FE_TONEAREST
32 #define PPL_FPU_TONEAREST FE_TONEAREST
33 #endif
34 #ifdef FE_UPWARD
35 #define PPL_FPU_UPWARD FE_UPWARD
36 #endif
37 #ifdef FE_DOWNWARD
38 #define PPL_FPU_DOWNWARD FE_DOWNWARD
39 #endif
40 #ifdef FE_TOWARDZERO
41 #define PPL_FPU_TOWARDZERO FE_TOWARDZERO
42 #endif
43
44 namespace Parma_Polyhedra_Library {
45
46 inline void
fpu_initialize_control_functions()47 fpu_initialize_control_functions() {
48 const int old = fegetround();
49 if (fesetround(PPL_FPU_DOWNWARD) != 0
50 || fesetround(PPL_FPU_UPWARD) != 0
51 || fesetround(old) != 0) {
52 throw std::logic_error("PPL configuration error:"
53 " PPL_CAN_CONTROL_FPU evaluates to true,"
54 " but fesetround() returns nonzero.");
55 }
56 }
57
58 inline fpu_rounding_direction_type
fpu_get_rounding_direction()59 fpu_get_rounding_direction() {
60 return static_cast<fpu_rounding_direction_type>(fegetround());
61 }
62
63 inline void
fpu_set_rounding_direction(fpu_rounding_direction_type dir)64 fpu_set_rounding_direction(fpu_rounding_direction_type dir) {
65 fesetround(dir);
66 }
67
68 inline fpu_rounding_control_word_type
fpu_save_rounding_direction(fpu_rounding_direction_type dir)69 fpu_save_rounding_direction(fpu_rounding_direction_type dir) {
70 const fpu_rounding_control_word_type old
71 = static_cast<fpu_rounding_control_word_type>(fegetround());
72 fesetround(dir);
73 return old;
74 }
75
76 inline void
fpu_reset_inexact()77 fpu_reset_inexact() {
78 #if PPL_CXX_SUPPORTS_IEEE_INEXACT_FLAG
79 feclearexcept(FE_INEXACT);
80 #endif
81 }
82
83 inline void
fpu_restore_rounding_direction(fpu_rounding_control_word_type w)84 fpu_restore_rounding_direction(fpu_rounding_control_word_type w) {
85 fesetround(w);
86 }
87
88 inline int
fpu_check_inexact()89 fpu_check_inexact() {
90 #if PPL_CXX_SUPPORTS_IEEE_INEXACT_FLAG
91 return fetestexcept(FE_INEXACT) != 0 ? 1 : 0;
92 #else
93 return -1;
94 #endif
95 }
96
97 } // namespace Parma_Polyhedra_Library
98
99 #endif // !defined(PPL_HAVE_FENV_H)
100
101 #endif // !defined(PPL_fpu_c99_inlines_hh)
102