dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
libgmp.cpp
1
7#include "libgmp.h"
8
9namespace std {
10
11// https://en.cppreference.com/w/cpp/utility/hash/operator()
12size_t hash<mpq_class>::operator()(const mpq_class &val) const {
13 mp_limb_t result = 2166136261;
14 size_t num_size = mpz_size(val.get_num_mpz_t());
15 size_t den_size = mpz_size(val.get_den_mpz_t());
16 const mp_limb_t *num_limbs = mpz_limbs_read(val.get_num_mpz_t());
17 const mp_limb_t *den_limbs = mpz_limbs_read(val.get_den_mpz_t());
18 for (size_t i = 0; i < num_size; i++) {
19 result = (result * 16777619) ^ num_limbs[i];
20 }
21 for (size_t i = 0; i < den_size; i++) {
22 result = (result * 16777619) ^ den_limbs[i];
23 }
24 return static_cast<size_t>(result);
25}
26
27} // namespace std
28
29namespace dlinear {
30
31std::strong_ordering operator<=>(const mpq_class &lhs, const mpq_t &rhs) {
32 const mpq_class &rhs_class = gmp::ToMpqClass(rhs);
33 return lhs < rhs_class ? std::strong_ordering::less
34 : lhs > rhs_class ? std::strong_ordering::greater
35 : std::strong_ordering::equal;
36}
37std::strong_ordering operator<=>(const mpq_t &lhs, const mpq_class &rhs) {
38 const mpq_class &lhs_class = gmp::ToMpqClass(lhs);
39 return lhs_class < rhs ? std::strong_ordering::less
40 : lhs_class > rhs ? std::strong_ordering::greater
41 : std::strong_ordering::equal;
42}
43
44namespace gmp {
45
46mpz_class floor(const mpq_class &val) {
47 // This rounds towards zero
48 mpz_class t{val};
49 if (t == val || val > 0) {
50 return t;
51 } else {
52 // val is negative and non-integer, so it was rounded upwards
53 return t - 1;
54 }
55}
56
57mpz_class ceil(const mpq_class &val) {
58 // This rounds towards zero
59 mpz_class t{val};
60 if (t == val || val < 0) {
61 return t;
62 } else {
63 // val is positive and non-integer, so it was rounded downwards
64 return t + 1;
65 }
66}
67
68} // namespace gmp
69
70} // namespace dlinear
Global namespace for the dlinear library.
STL namespace.