1 /* Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
2    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
3 
4 This file is free software; as a special exception the author gives
5 unlimited permission to copy and/or distribute it, with or without
6 modifications, as long as this notice is preserved.
7 
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
10 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. */
12 
13 #ifndef PPL_Coefficient_types_hh
14 #define PPL_Coefficient_types_hh 1
15 
16 #include "Checked_Number_defs.hh"
17 
18 #ifdef PPL_GMP_INTEGERS
19 #include "GMP_Integer_types.hh"
20 #endif
21 
22 #if defined(PPL_CHECKED_INTEGERS) || defined(PPL_NATIVE_INTEGERS)
23 
24 namespace Parma_Polyhedra_Library {
25 
26 //! A policy for checked bounded integer coefficients.
27 /*! \ingroup PPL_CXX_interface */
28 struct Bounded_Integer_Coefficient_Policy {
29   //! Check for overflowed result.
30   const_bool_nodef(check_overflow, true);
31 
32   //! Do not check for attempts to add infinities with different sign.
33   const_bool_nodef(check_inf_add_inf, false);
34 
35   //! Do not check for attempts to subtract infinities with same sign.
36   const_bool_nodef(check_inf_sub_inf, false);
37 
38   //! Do not check for attempts to multiply infinities by zero.
39   const_bool_nodef(check_inf_mul_zero, false);
40 
41   //! Do not check for attempts to divide by zero.
42   const_bool_nodef(check_div_zero, false);
43 
44   //! Do not check for attempts to divide infinities.
45   const_bool_nodef(check_inf_div_inf, false);
46 
47   //! Do not check for attempts to compute remainder of infinities.
48   const_bool_nodef(check_inf_mod, false);
49 
50   //! Do not checks for attempts to take the square root of a negative number.
51   const_bool_nodef(check_sqrt_neg, false);
52 
53   //! Do not handle not-a-number special value.
54   const_bool_nodef(has_nan, false);
55 
56   //! Do not handle infinity special values.
57   const_bool_nodef(has_infinity, false);
58 
59   /*! \brief
60     The checked number can always be safely converted to the
61     underlying type \p T and vice-versa.
62   */
63   const_bool_nodef(convertible, true);
64 
65   //! Do not honor requests to check for FPU inexact results.
66   const_bool_nodef(fpu_check_inexact, false);
67 
68   //! Do not make extra checks to detect FPU NaN results.
69   const_bool_nodef(fpu_check_nan_result, true);
70 
71   /*! \brief
72     For constructors, by default use the same rounding used by
73     underlying type.
74   */
75   static const Rounding_Dir ROUND_DEFAULT_CONSTRUCTOR = ROUND_NATIVE;
76 
77   /*! \brief
78     For overloaded operators (operator+(), operator-(), ...), by
79     default use the same rounding used by the underlying type.
80   */
81   static const Rounding_Dir ROUND_DEFAULT_OPERATOR = ROUND_NATIVE;
82 
83   /*! \brief
84     For input functions, by default use the same rounding used by
85     the underlying type.
86   */
87   static const Rounding_Dir ROUND_DEFAULT_INPUT = ROUND_NATIVE;
88 
89   /*! \brief
90     For output functions, by default use the same rounding used by
91     the underlying type.
92   */
93   static const Rounding_Dir ROUND_DEFAULT_OUTPUT = ROUND_NATIVE;
94 
95   /*! \brief
96     For all other functions, by default use the same rounding used by
97     the underlying type.
98   */
99   static const Rounding_Dir ROUND_DEFAULT_FUNCTION = ROUND_NATIVE;
100 
101   /*! \brief
102     Handles \p r: called by all constructors, operators and functions that
103     do not return a Result value.
104   */
105   static void handle_result(Result r);
106 };
107 
108 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
109 //! Coefficient traits specialization for 8 bits checked integers.
110 /*! \ingroup PPL_CXX_interface */
111 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
112 template <typename Policy>
113 struct Coefficient_traits_template<Checked_Number<int8_t, Policy> > {
114   //! The type used for references to const 8 bit checked integers.
115   typedef Checked_Number<int8_t, Policy> const_reference;
116 };
117 
118 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
119 //! Coefficient traits specialization for 16 bits checked integers.
120 /*! \ingroup PPL_CXX_interface */
121 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
122 template <typename Policy>
123 struct Coefficient_traits_template<Checked_Number<int16_t, Policy> > {
124   //! The type used for references to const 16 bit checked integers.
125   typedef Checked_Number<int16_t, Policy> const_reference;
126 };
127 
128 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
129 //! Coefficient traits specialization for 32 bits checked integers.
130 /*! \ingroup PPL_CXX_interface */
131 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
132 template <typename Policy>
133 struct Coefficient_traits_template<Checked_Number<int32_t, Policy> > {
134   //! The type used for references to const 32 bit checked integers.
135   typedef Checked_Number<int32_t, Policy> const_reference;
136 };
137 
138 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
139 //! Coefficient traits specialization for 64 bits checked integers.
140 /*! \ingroup PPL_CXX_interface */
141 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
142 template <typename Policy>
143 struct Coefficient_traits_template<Checked_Number<int64_t, Policy> > {
144   //! The type used for references to const 64 bit checked integers.
145   typedef const Checked_Number<int64_t, Policy>& const_reference;
146 };
147 
148 } // namespace Parma_Polyhedra_Library
149 
150 #endif // defined(PPL_CHECKED_INTEGERS) || defined(PPL_NATIVE_INTEGERS)
151 
152 namespace Parma_Polyhedra_Library {
153 
154 //! An alias for easily naming the type of PPL coefficients.
155 /*! \ingroup PPL_CXX_interface
156   Objects of type Coefficient are used to implement the integral valued
157   coefficients occurring in linear expressions, constraints, generators,
158   intervals, bounding boxes and so on.  Depending on the chosen
159   configuration options (see file <CODE>README.configure</CODE>),
160   a Coefficient may actually be:
161     - The GMP_Integer type, which in turn is an alias for the
162       <CODE>mpz_class</CODE> type implemented by the C++ interface
163       of the GMP library (this is the default configuration).
164     - An instance of the Checked_Number class template: with the policy
165       Bounded_Integer_Coefficient_Policy, this implements overflow
166       detection on top of a native integral type (available template
167       instances include checked integers having 8, 16, 32 or 64 bits);
168       with the Checked_Number_Transparent_Policy, this is a wrapper
169       for native integral types with no overflow detection
170       (available template instances are as above).
171 */
172 typedef PPL_COEFFICIENT_TYPE Coefficient;
173 
174 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
175 //! An alias for easily naming the coefficient traits.
176 /*! \ingroup PPL_CXX_interface */
177 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
178 typedef Coefficient_traits_template<Coefficient> Coefficient_traits;
179 
180 } // namespace Parma_Polyhedra_Library
181 
182 #endif // !defined(PPL_Coefficient_types_hh)
183