Generated on Sat Feb 7 2015 02:01:16 for Gecode by doxygen 1.8.9.1
limits.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2005
8  *
9  * Last modified:
10  * $Date: 2013-02-14 16:29:11 +0100 (Thu, 14 Feb 2013) $ by $Author: schulte $
11  * $Revision: 13292 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Int {
39 
40  inline bool
42  return ((n >= min) && (n <= max));
43  }
44  inline bool
45  Limits::valid(long long int n) {
46  return ((n >= min) && (n <= max));
47  }
48 
49  inline void
50  Limits::check(int n, const char* l) {
51  if ((n < min) || (n > max))
52  throw OutOfLimits(l);
53  }
54  inline void
55  Limits::check(long long int n, const char* l) {
56  if ((n < min) || (n > max))
57  throw OutOfLimits(l);
58  }
59 
60  inline void
61  Limits::positive(int n, const char* l) {
62  if ((n <= 0) || (n > max))
63  throw OutOfLimits(l);
64  }
65  inline void
66  Limits::positive(long long int n, const char* l) {
67  if ((n <= 0.0) || (n > max))
68  throw OutOfLimits(l);
69  }
70 
71  inline void
72  Limits::nonnegative(int n, const char* l) {
73  if ((n < 0) || (n > max))
74  throw OutOfLimits(l);
75  }
76  inline void
77  Limits::nonnegative(long long int n, const char* l) {
78  if ((n < 0.0) || (n > max))
79  throw OutOfLimits(l);
80  }
81 
82  forceinline bool
83  Limits::overflow_add(int n, int m) {
84  long long int nm =
85  static_cast<long long int>(n) + static_cast<long long int>(m);
86  return (nm > INT_MAX) || (nm < INT_MIN+1);
87  }
88  forceinline bool
89  Limits::overflow_add(long long int n, long long int m) {
90  if (m < 0)
91  return n < LLONG_MIN + 1 - m;
92  else
93  return n > LLONG_MAX - m;
94  }
95 
96  forceinline bool
97  Limits::overflow_sub(int n, int m) {
98  long long int nm =
99  static_cast<long long int>(n) - static_cast<long long int>(m);
100  return (nm > INT_MAX) || (nm < INT_MIN+1);
101  }
102  forceinline bool
103  Limits::overflow_sub(long long int n, long long int m) {
104  if (m < 0)
105  return n > LLONG_MAX + m;
106  else
107  return n < LLONG_MIN + 1 + m;
108  }
109 
110  inline bool
111  Limits::overflow_mul(int n, int m) {
112  long long int nm =
113  static_cast<long long int>(n) * static_cast<long long int>(m);
114  return (nm > INT_MAX) || (nm < INT_MIN+1);
115  }
116 
117  inline bool
118  Limits::overflow_mul(long long int n, long long int m) {
119  // Check whether we can at least change the sign
120  if ((n == LLONG_MIN) || (m == LLONG_MIN))
121  return false;
122 
123  unsigned long long int un =
124  static_cast<unsigned long long int>(n < 0 ? -n : n);
125  unsigned long long int um =
126  static_cast<unsigned long long int>(m < 0 ? -m : m);
127 
128  const unsigned int k = CHAR_BIT * sizeof(int);
129 
130  unsigned long long int un_hi = un >> k;
131  unsigned long long int un_lo = un & ((1ULL << k) - 1ULL);
132  unsigned long long int um_hi = um >> k;
133  unsigned long long int um_lo = um & ((1ULL << k) - 1ULL);
134 
135  // This would mean that there is something larger than 2^64
136  if ((un_hi != 0ULL) && (um_hi != 0ULL))
137  return true;
138 
139  unsigned long long int unm_hi = 0ULL;
140 
141  // Now, either un_hi or m_hi can be different from zero
142  if (un_hi != 0ULL)
143  unm_hi = un_hi * um_lo;
144  else if (um_hi != 0ULL)
145  unm_hi = um_hi * un_lo;
146  else
147  return false;
148 
149  // Again, something larger than 2^64
150  if ((unm_hi >> k) != 0ULL)
151  return true;
152  unm_hi <<= k;
153 
154  unsigned long long int unm_lo = un_lo * um_lo;
155 
156  return unm_hi > static_cast<unsigned long long int>(LLONG_MAX) - unm_lo;
157  }
158 
159 }}
160 
161 // STATISTICS: int-var
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
Exception: Value out of limits
Definition: exception.hpp:48
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
void nonnegative(int n, const char *l)
Check whether n is in range and nonnegative, otherwise throw out of limits with information l...
Definition: limits.hpp:72
bool overflow_mul(int n, int m)
Check whether multiplying n and m would overflow.
Definition: limits.hpp:111
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
bool overflow_add(int n, int m)
Check whether adding n and m would overflow.
Definition: limits.hpp:83
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
bool overflow_sub(int n, int m)
Check whether subtracting m from n would overflow.
Definition: limits.hpp:97
#define forceinline
Definition: config.hpp:132
bool valid(int n)
Return whether n is in range.
Definition: limits.hpp:41
Gecode toplevel namespace
void check(int n, const char *l)
Check whether n is in range, otherwise throw out of limits with information l.
Definition: limits.hpp:50
void positive(int n, const char *l)
Check whether n is in range and strictly positive, otherwise throw out of limits with information l...
Definition: limits.hpp:61