1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright 2012 John Maddock.
3 // Copyright 2012 Phil Endecott
4 // Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #include <boost/multiprecision/cpp_int.hpp>
9 #include "arithmetic_backend.hpp"
10 #include <boost/chrono.hpp>
11 #include <boost/random/mersenne_twister.hpp>
12 #include <boost/random/uniform_int_distribution.hpp>
17 template <class Clock>
20 typedef typename Clock::duration duration;
23 m_start = Clock::now();
27 return Clock::now() - m_start;
31 m_start = Clock::now();
35 typename Clock::time_point m_start;
38 // Custom 128-bit maths used for exact calculation of the Delaunay test.
39 // Only the few operators actually needed here are implemented.
47 int128_t(int32_t i) : high(i >> 31), low(static_cast<int64_t>(i)) {}
48 int128_t(uint32_t i) : high(0), low(i) {}
49 int128_t(int64_t i) : high(i >> 63), low(i) {}
50 int128_t(uint64_t i) : high(0), low(i) {}
53 inline int128_t operator<<(int128_t val, int amt)
56 r.low = val.low << amt;
57 r.high = val.low >> (64 - amt);
58 r.high |= val.high << amt;
62 inline int128_t& operator+=(int128_t& l, int128_t r)
65 bool carry = l.low < r.low;
72 inline int128_t operator-(int128_t val)
82 inline int128_t operator+(int128_t l, int128_t r)
88 inline bool operator<(int128_t l, int128_t r)
91 return l.high < r.high;
95 inline int128_t mult_64x64_to_128(int64_t a, int64_t b)
97 // Make life simple by dealing only with positive numbers:
110 // Divide input into 32-bit halves:
111 uint32_t ah = a >> 32;
112 uint32_t al = a & 0xffffffff;
113 uint32_t bh = b >> 32;
114 uint32_t bl = b & 0xffffffff;
116 // Long multiplication, with 64-bit temporaries:
127 uint64_t t1 = static_cast<uint64_t>(al) * bl;
128 uint64_t t2 = static_cast<uint64_t>(ah) * bl;
129 uint64_t t3 = static_cast<uint64_t>(al) * bh;
130 uint64_t t4 = static_cast<uint64_t>(ah) * bh;
134 r += int128_t(t2) << 32;
135 r += int128_t(t3) << 32;
143 template <class R, class T>
144 BOOST_FORCEINLINE void mul_2n(R& r, const T& a, const T& b)
150 template <class B, boost::multiprecision::expression_template_option ET, class T>
151 BOOST_FORCEINLINE void mul_2n(boost::multiprecision::number<B, ET>& r, const T& a, const T& b)
156 BOOST_FORCEINLINE void mul_2n(int128_t& r, const boost::int64_t& a, const boost::int64_t& b)
158 r = mult_64x64_to_128(a, b);
161 template <class Traits>
162 inline bool delaunay_test(int32_t ax, int32_t ay, int32_t bx, int32_t by,
163 int32_t cx, int32_t cy, int32_t dx, int32_t dy)
165 // Test whether the quadrilateral ABCD's diagonal AC should be flipped to BD.
166 // This is the Cline & Renka method.
167 // Flip if the sum of the angles ABC and CDA is greater than 180 degrees.
168 // Equivalently, flip if sin(ABC + CDA) < 0.
169 // Trig identity: cos(ABC) * sin(CDA) + sin(ABC) * cos(CDA) < 0
170 // We can use scalar and vector products to find sin and cos, and simplify
171 // to the following code.
172 // Numerical robustness is important. This code addresses it by performing
173 // exact calculations with large integer types.
175 // NOTE: This routine is limited to inputs with up to 30 BIT PRECISION, which
176 // is to say all inputs must be in the range [INT_MIN/2, INT_MAX/2].
178 typedef typename Traits::i64_t i64;
179 typedef typename Traits::i128_t i128;
182 mul_2n(cos_abc, (ax - bx), (cx - bx)); // subtraction yields 31-bit values, multiplied to give 62-bit values
183 mul_2n(t, (ay - by), (cy - by));
184 cos_abc += t; // addition yields 63 bit value, leaving one left for the sign
187 mul_2n(cos_cda, (cx - dx), (ax - dx));
188 mul_2n(t, (cy - dy), (ay - dy));
191 if (cos_abc >= 0 && cos_cda >= 0)
193 if (cos_abc < 0 && cos_cda < 0)
197 mul_2n(sin_abc, (ax - bx), (cy - by));
198 mul_2n(t, (cx - bx), (ay - by));
202 mul_2n(sin_cda, (cx - dx), (ay - dy));
203 mul_2n(t, (ax - dx), (cy - dy));
207 mul_2n(sin_sum, sin_abc, cos_cda); // 63-bit inputs multiplied to 126-bit output
208 mul_2n(t128, cos_abc, sin_cda);
209 sin_sum += t128; // Addition yields 127 bit result, leaving one bit for the sign
216 int32_t ax, ay, bx, by, cx, cy, dx, dy;
219 typedef std::vector<dt_dat> data_t;
222 template <class Traits>
223 void do_calc(const char* name)
225 std::cout << "Running calculations for: " << name << std::endl;
227 stopwatch<boost::chrono::high_resolution_clock> w;
229 boost::uint64_t flips = 0;
230 boost::uint64_t calcs = 0;
232 for (int j = 0; j < 1000; ++j)
234 for (data_t::const_iterator i = data.begin(); i != data.end(); ++i)
236 const dt_dat& d = *i;
237 bool flip = delaunay_test<Traits>(d.ax, d.ay, d.bx, d.by, d.cx, d.cy, d.dx, d.dy);
243 double t = boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
245 std::cout << "Number of calculations = " << calcs << std::endl;
246 std::cout << "Number of flips = " << flips << std::endl;
247 std::cout << "Total execution time = " << t << std::endl;
248 std::cout << "Time per calculation = " << t / calcs << std::endl
252 template <class I64, class I128>
259 dt_dat generate_quadrilateral()
261 static boost::random::mt19937 gen;
262 static boost::random::uniform_int_distribution<> dist(INT_MIN / 2, INT_MAX / 2);
266 result.ax = dist(gen);
267 result.ay = dist(gen);
268 result.bx = boost::random::uniform_int_distribution<>(result.ax, INT_MAX / 2)(gen); // bx is to the right of ax.
269 result.by = dist(gen);
270 result.cx = dist(gen);
271 result.cy = boost::random::uniform_int_distribution<>(result.cx > result.bx ? result.by : result.ay, INT_MAX / 2)(gen); // cy is below at least one of ay and by.
272 result.dx = boost::random::uniform_int_distribution<>(result.cx, INT_MAX / 2)(gen); // dx is to the right of cx.
273 result.dy = boost::random::uniform_int_distribution<>(result.cx > result.bx ? result.by : result.ay, INT_MAX / 2)(gen); // cy is below at least one of ay and by.
278 static void load_data()
280 for (unsigned i = 0; i < 100000; ++i)
281 data.push_back(generate_quadrilateral());
286 using namespace boost::multiprecision;
287 std::cout << "loading data...\n";
290 std::cout << "calculating...\n";
292 do_calc<test_traits<boost::int64_t, boost::int64_t> >("int64_t, int64_t");
293 do_calc<test_traits<number<arithmetic_backend<boost::int64_t>, et_off>, number<arithmetic_backend<boost::int64_t>, et_off> > >("arithmetic_backend<int64_t>, arithmetic_backend<int64_t>");
294 do_calc<test_traits<boost::int64_t, number<arithmetic_backend<boost::int64_t>, et_off> > >("int64_t, arithmetic_backend<int64_t>");
295 do_calc<test_traits<number<cpp_int_backend<64, 64, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void>, et_off>, number<cpp_int_backend<64, 64, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void>, et_off> > >("multiprecision::int64_t, multiprecision::int64_t");
297 do_calc<test_traits<boost::int64_t, ::int128_t> >("int64_t, int128_t");
298 do_calc<test_traits<boost::int64_t, boost::multiprecision::int128_t> >("int64_t, boost::multiprecision::int128_t");
299 do_calc<test_traits<boost::int64_t, number<cpp_int_backend<128, 128, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void>, et_on> > >("int64_t, int128_t (ET)");
300 do_calc<test_traits<number<cpp_int_backend<64, 64, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void>, et_off>, boost::multiprecision::int128_t> >("multiprecision::int64_t, multiprecision::int128_t");
302 do_calc<test_traits<boost::int64_t, cpp_int> >("int64_t, cpp_int");
303 do_calc<test_traits<boost::int64_t, number<cpp_int_backend<>, et_off> > >("int64_t, cpp_int (no ET's)");
304 do_calc<test_traits<boost::int64_t, number<cpp_int_backend<128> > > >("int64_t, cpp_int(128-bit cache)");
305 do_calc<test_traits<boost::int64_t, number<cpp_int_backend<128>, et_off> > >("int64_t, cpp_int (128-bit Cache no ET's)");