PandA-2024.02
APInt.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2020-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
43 #ifndef APINT_HPP
44 #define APINT_HPP
45 
46 #include <iostream>
47 #include <string>
48 #include <type_traits>
49 
50 #include <boost/multiprecision/cpp_int.hpp>
51 #include <boost/multiprecision/cpp_int/literals.hpp>
52 
53 class APInt
54 {
55  public:
56  using backend = boost::multiprecision::backends::cpp_int_backend<4096, 4096, boost::multiprecision::signed_magnitude,
57  boost::multiprecision::unchecked, void>;
58  using number = boost::multiprecision::number<backend>;
59  using bw_t = uint16_t;
60 
61  private:
63 
64  public:
65  APInt();
66 
67 #pragma GCC diagnostic push
68 #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
70  APInt(T val) : _data(val)
71  {
72  }
73 
74  APInt(const number& v) : _data(v)
75  {
76  }
77 
78  APInt(const std::string& str) : _data(boost::lexical_cast<number>(str))
79  {
80  }
81 #pragma GCC diagnostic pop
82 
83  friend bool operator<(const APInt& lhs, const APInt& rhs);
84  friend bool operator>(const APInt& lhs, const APInt& rhs);
85  friend bool operator<=(const APInt& lhs, const APInt& rhs);
86  friend bool operator>=(const APInt& lhs, const APInt& rhs);
87  friend bool operator==(const APInt& lhs, const APInt& rhs);
88  friend bool operator!=(const APInt& lhs, const APInt& rhs);
89  explicit operator bool() const;
90 
91  /*
92  * Binary operators
93  */
94  friend APInt operator+(const APInt& lhs, const APInt& rhs);
95  friend APInt operator-(const APInt& lhs, const APInt& rhs);
96  friend APInt operator*(const APInt& lhs, const APInt& rhs);
97  friend APInt operator/(const APInt& lhs, const APInt& rhs);
98  friend APInt operator%(const APInt& lhs, const APInt& rhs);
99  friend APInt operator&(const APInt& lhs, const APInt& rhs);
100  friend APInt operator|(const APInt& lhs, const APInt& rhs);
101  friend APInt operator^(const APInt& lhs, const APInt& rhs);
102  friend APInt operator<<(const APInt& lhs, const APInt& rhs);
103  friend APInt operator>>(const APInt& lhs, const APInt& rhs);
104  APInt& operator+=(const APInt& rhs);
105  APInt& operator-=(const APInt& rhs);
106  APInt& operator*=(const APInt& rhs);
107  APInt& operator/=(const APInt& rhs);
108  APInt& operator%=(const APInt& rhs);
109  APInt& operator&=(const APInt& rhs);
110  APInt& operator|=(const APInt& rhs);
111  APInt& operator^=(const APInt& rhs);
112  APInt& operator<<=(const APInt& rhs);
113  APInt& operator>>=(const APInt& rhs);
114 
115  /*
116  * Unary operators
117  */
118  APInt abs() const;
119  APInt operator-() const;
120  APInt operator~() const;
121  APInt operator++(int);
122  APInt operator--(int);
123  APInt& operator++();
124  APInt& operator--();
125 
126  /*
127  * Bitwise helpers
128  */
129  void bit_set(bw_t i);
130  void bit_clr(bw_t i);
131  bool bit_tst(bw_t i) const;
132  bool sign() const;
133  APInt& extOrTrunc(bw_t bw, bool sign);
134  APInt extOrTrunc(bw_t bw, bool sign) const;
135  bw_t trailingZeros(bw_t bw) const;
136  bw_t trailingOnes(bw_t bw) const;
137  bw_t leadingZeros(bw_t bw) const;
138  bw_t leadingOnes(bw_t bw) const;
139  bw_t minBitwidth(bool sign) const;
140 
142  explicit operator T() const
143  {
144  using U = typename std::make_unsigned<T>::type;
145  return static_cast<T>(static_cast<U>(_data & std::numeric_limits<U>::max()));
146  }
147 
148  static APInt getMaxValue(bw_t bw);
149  static APInt getMinValue(bw_t bw);
150  static APInt getSignedMaxValue(bw_t bw);
151  static APInt getSignedMinValue(bw_t bw);
152 
153  friend std::ostream& operator<<(std::ostream& str, const APInt& v);
154  friend std::istream& operator>>(std::istream& str, APInt& v);
155 };
156 
157 std::ostream& operator<<(std::ostream& str, const APInt& v);
158 std::istream& operator>>(std::istream& str, APInt& v);
159 
160 template <char... STR>
161 constexpr APInt::number operator"" _apint()
162 {
163  typedef typename boost::multiprecision::literals::detail::make_packed_value_from_str<STR...>::type pt;
165 }
166 
167 #endif
APInt & extOrTrunc(bw_t bw, bool sign)
Definition: APInt.cpp:285
static APInt getSignedMinValue(bw_t bw)
Definition: APInt.cpp:434
APInt & operator--()
Definition: APInt.cpp:250
This algorithm is to find coloring of a graph Algorithm: Let G = (V,E) be a graph with vertices v_1...
APInt & operator|=(const APInt &rhs)
Definition: APInt.cpp:183
bool bit_tst(bw_t i) const
Definition: APInt.cpp:265
friend bool operator>=(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:74
APInt & operator*=(const APInt &rhs)
Definition: APInt.cpp:159
static APInt getSignedMaxValue(bw_t bw)
Definition: APInt.cpp:429
boost::multiprecision::backends::cpp_int_backend< 4096, 4096, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void > backend
Definition: APInt.hpp:57
bw_t leadingOnes(bw_t bw) const
Definition: APInt.cpp:382
APInt & operator++()
Definition: APInt.cpp:245
friend APInt operator*(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:107
APInt & operator<<=(const APInt &rhs)
Definition: APInt.cpp:195
friend APInt operator/(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:112
APInt(const number &v)
Definition: APInt.hpp:74
bw_t trailingZeros(bw_t bw) const
Definition: APInt.cpp:309
APInt & operator-=(const APInt &rhs)
Definition: APInt.cpp:153
bool
Definition: bellmanford.c:29
#define STR(s)
Macro which performs a lexical_cast to a string.
#define max
Definition: backprop.h:17
bw_t minBitwidth(bool sign) const
Definition: APInt.cpp:395
APInt & operator &=(const APInt &rhs)
friend APInt operator>>(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:142
APInt & operator+=(const APInt &rhs)
Definition: APInt.cpp:147
APInt(const std::string &str)
Definition: APInt.hpp:78
friend bool operator==(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:79
APInt operator~() const
Definition: APInt.cpp:224
void bit_set(bw_t i)
Definition: APInt.cpp:255
friend bool operator!=(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:84
friend bool operator<=(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:69
static APInt getMaxValue(bw_t bw)
Definition: APInt.cpp:419
APInt & operator%=(const APInt &rhs)
Definition: APInt.cpp:171
boost::multiprecision::number< backend > number
Definition: APInt.hpp:58
friend bool operator<(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:59
uint16_t bw_t
Definition: APInt.hpp:59
friend APInt operator &(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:122
bw_t leadingZeros(bw_t bw) const
Definition: APInt.cpp:335
APInt & operator/=(const APInt &rhs)
Definition: APInt.cpp:165
Definition: APInt.hpp:53
bool sign() const
Definition: APInt.cpp:270
bw_t trailingOnes(bw_t bw) const
Definition: APInt.cpp:322
void bit_clr(bw_t i)
Definition: APInt.cpp:260
friend APInt operator<<(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:137
APInt operator-() const
Definition: APInt.cpp:217
APInt(T val)
Definition: APInt.hpp:70
APInt abs() const
Definition: APInt.cpp:210
static APInt getMinValue(bw_t bw)
Definition: APInt.cpp:424
char str[25]
Definition: fixedptc.c:8
APInt & operator^=(const APInt &rhs)
Definition: APInt.cpp:189
friend APInt operator|(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:127
APInt()
Definition: APInt.cpp:54
APInt & operator>>=(const APInt &rhs)
Definition: APInt.cpp:201
friend APInt operator^(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:132
friend APInt operator+(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:97
number _data
Definition: APInt.hpp:62
friend APInt operator%(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:117
friend bool operator>(const APInt &lhs, const APInt &rhs)
Definition: APInt.cpp:64

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13