@@ -33,9 +33,9 @@ template <unsigned Bits, bool Signed> class Integral;
33
33
template <bool Signed> class IntegralAP final {
34
34
private:
35
35
friend IntegralAP<!Signed>;
36
- APSInt V;
36
+ APInt V;
37
37
38
- template <typename T> static T truncateCast (const APSInt &V) {
38
+ template <typename T> static T truncateCast (const APInt &V) {
39
39
constexpr unsigned BitSize = sizeof (T) * 8 ;
40
40
if (BitSize >= V.getBitWidth ())
41
41
return std::is_signed_v<T> ? V.getSExtValue () : V.getZExtValue ();
@@ -48,23 +48,37 @@ template <bool Signed> class IntegralAP final {
48
48
using AsUnsigned = IntegralAP<false >;
49
49
50
50
template <typename T>
51
- IntegralAP (T Value)
52
- : V(APInt(sizeof (T) * 8 , static_cast <uint64_t >(Value),
53
- std::is_signed_v<T>)) {}
51
+ IntegralAP (T Value, unsigned BitWidth)
52
+ : V(APInt(BitWidth, static_cast <uint64_t >(Value), Signed)) {}
54
53
55
54
IntegralAP (APInt V) : V(V) {}
56
- IntegralAP (APSInt V) : V(V) {}
57
55
// / Arbitrary value for uninitialized variables.
58
- IntegralAP () : V(APSInt::getMaxValue( 1024 , Signed) ) {}
56
+ IntegralAP () : IntegralAP(- 1 , 1024 ) {}
59
57
60
58
IntegralAP operator -() const { return IntegralAP (-V); }
61
59
IntegralAP operator -(const IntegralAP &Other) const {
62
60
return IntegralAP (V - Other.V );
63
61
}
64
- bool operator >(IntegralAP RHS) const { return V > RHS.V ; }
65
- bool operator >=(IntegralAP RHS) const { return V >= RHS.V ; }
66
- bool operator <(IntegralAP RHS) const { return V < RHS.V ; }
67
- bool operator <=(IntegralAP RHS) const { return V <= RHS.V ; }
62
+ bool operator >(const IntegralAP &RHS) const {
63
+ if constexpr (Signed)
64
+ return V.ugt (RHS.V );
65
+ return V.sgt (RHS.V );
66
+ }
67
+ bool operator >=(IntegralAP RHS) const {
68
+ if constexpr (Signed)
69
+ return V.uge (RHS.V );
70
+ return V.sge (RHS.V );
71
+ }
72
+ bool operator <(IntegralAP RHS) const {
73
+ if constexpr (Signed)
74
+ return V.slt (RHS.V );
75
+ return V.slt (RHS.V );
76
+ }
77
+ bool operator <=(IntegralAP RHS) const {
78
+ if constexpr (Signed)
79
+ return V.ult (RHS.V );
80
+ return V.ult (RHS.V );
81
+ }
68
82
69
83
explicit operator bool () const { return !V.isZero (); }
70
84
explicit operator int8_t () const { return truncateCast<int8_t >(V); }
@@ -78,42 +92,32 @@ template <bool Signed> class IntegralAP final {
78
92
79
93
template <typename T> static IntegralAP from (T Value, unsigned NumBits = 0 ) {
80
94
assert (NumBits > 0 );
81
- APSInt Copy =
82
- APSInt (APInt (NumBits, static_cast <uint64_t >(Value), Signed), !Signed);
95
+ APInt Copy = APInt (NumBits, static_cast <uint64_t >(Value), Signed);
83
96
84
97
return IntegralAP<Signed>(Copy);
85
98
}
86
99
87
100
template <bool InputSigned>
88
101
static IntegralAP from (IntegralAP<InputSigned> V, unsigned NumBits = 0 ) {
89
- if constexpr (Signed == InputSigned)
90
- return V;
91
-
92
- APSInt Copy = V.V ;
93
- Copy.setIsSigned (Signed);
94
-
95
- return IntegralAP<Signed>(Copy);
102
+ return IntegralAP<Signed>(V.V );
96
103
}
97
104
98
105
template <unsigned Bits, bool InputSigned>
99
106
static IntegralAP from (Integral<Bits, InputSigned> I, unsigned BitWidth) {
100
- APSInt Copy =
101
- APSInt (APInt (BitWidth, static_cast <uint64_t >(I), InputSigned), !Signed);
102
- Copy.setIsSigned (Signed);
107
+ APInt Copy = APInt (BitWidth, static_cast <uint64_t >(I), InputSigned);
103
108
104
- assert (Copy.isSigned () == Signed);
105
109
return IntegralAP<Signed>(Copy);
106
110
}
107
111
108
112
static IntegralAP zero (int32_t BitWidth) {
109
- APSInt V = APSInt ( APInt (BitWidth, 0LL , Signed), ! Signed);
113
+ APInt V = APInt (BitWidth, 0LL , Signed);
110
114
return IntegralAP (V);
111
115
}
112
116
113
117
constexpr unsigned bitWidth () const { return V.getBitWidth (); }
114
118
115
- APSInt toAPSInt (unsigned Bits = 0 ) const { return V ; }
116
- APValue toAPValue () const { return APValue (V ); }
119
+ APSInt toAPSInt (unsigned Bits = 0 ) const { return APSInt (V, Signed) ; }
120
+ APValue toAPValue () const { return APValue (APSInt (V, Signed) ); }
117
121
118
122
bool isZero () const { return V.isZero (); }
119
123
bool isPositive () const { return V.isNonNegative (); }
@@ -139,22 +143,38 @@ template <bool Signed> class IntegralAP final {
139
143
}
140
144
141
145
IntegralAP<false > toUnsigned () const {
142
- APSInt Copy = V;
143
- Copy.setIsSigned (false );
146
+ APInt Copy = V;
144
147
return IntegralAP<false >(Copy);
145
148
}
146
149
147
150
ComparisonCategoryResult compare (const IntegralAP &RHS) const {
148
- return Compare (V, RHS.V );
151
+ assert (Signed == RHS.isSigned ());
152
+ assert (bitWidth () == RHS.bitWidth ());
153
+ if constexpr (Signed) {
154
+ if (V.slt (RHS.V ))
155
+ return ComparisonCategoryResult::Less;
156
+ if (V.sgt (RHS.V ))
157
+ return ComparisonCategoryResult::Greater;
158
+ return ComparisonCategoryResult::Equal;
159
+ }
160
+
161
+ assert (!Signed);
162
+ if (V.ult (RHS.V ))
163
+ return ComparisonCategoryResult::Less;
164
+ if (V.ugt (RHS.V ))
165
+ return ComparisonCategoryResult::Greater;
166
+ return ComparisonCategoryResult::Equal;
149
167
}
150
168
151
169
static bool increment (IntegralAP A, IntegralAP *R) {
170
+ // FIXME: Implement.
152
171
assert (false );
153
- *R = IntegralAP (A.V + 1 );
172
+ *R = IntegralAP (A.V - 1 );
154
173
return false ;
155
174
}
156
175
157
176
static bool decrement (IntegralAP A, IntegralAP *R) {
177
+ // FIXME: Implement.
158
178
assert (false );
159
179
*R = IntegralAP (A.V - 1 );
160
180
return false ;
@@ -170,48 +190,46 @@ template <bool Signed> class IntegralAP final {
170
190
}
171
191
172
192
static bool mul (IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
193
+ // FIXME: Implement.
173
194
assert (false );
174
- // return CheckMulUB(A.V, B.V, R->V);
175
195
return false ;
176
196
}
177
197
178
198
static bool rem (IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
199
+ // FIXME: Implement.
179
200
assert (false );
180
- *R = IntegralAP (A.V % B.V );
181
201
return false ;
182
202
}
183
203
184
204
static bool div (IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
205
+ // FIXME: Implement.
185
206
assert (false );
186
- *R = IntegralAP (A.V / B.V );
187
207
return false ;
188
208
}
189
209
190
210
static bool bitAnd (IntegralAP A, IntegralAP B, unsigned OpBits,
191
211
IntegralAP *R) {
212
+ // FIXME: Implement.
192
213
assert (false );
193
- *R = IntegralAP (A.V & B.V );
194
214
return false ;
195
215
}
196
216
197
217
static bool bitOr (IntegralAP A, IntegralAP B, unsigned OpBits,
198
218
IntegralAP *R) {
199
219
assert (false );
200
- *R = IntegralAP (A.V | B.V );
201
220
return false ;
202
221
}
203
222
204
223
static bool bitXor (IntegralAP A, IntegralAP B, unsigned OpBits,
205
224
IntegralAP *R) {
225
+ // FIXME: Implement.
206
226
assert (false );
207
- *R = IntegralAP (A.V ^ B.V );
208
227
return false ;
209
228
}
210
229
211
230
static bool neg (const IntegralAP &A, IntegralAP *R) {
212
- APSInt AI = A.V ;
213
-
214
- AI.setIsSigned (Signed);
231
+ APInt AI = A.V ;
232
+ AI.negate ();
215
233
*R = IntegralAP (AI);
216
234
return false ;
217
235
}
@@ -223,12 +241,12 @@ template <bool Signed> class IntegralAP final {
223
241
224
242
static void shiftLeft (const IntegralAP A, const IntegralAP B, unsigned OpBits,
225
243
IntegralAP *R) {
226
- *R = IntegralAP (A.V << B.V .getZExtValue ());
244
+ *R = IntegralAP (A.V . shl ( B.V .getZExtValue () ));
227
245
}
228
246
229
247
static void shiftRight (const IntegralAP A, const IntegralAP B,
230
248
unsigned OpBits, IntegralAP *R) {
231
- *R = IntegralAP (A.V >> B.V .getZExtValue ());
249
+ *R = IntegralAP (A.V . ashr ( B.V .getZExtValue () ));
232
250
}
233
251
234
252
private:
@@ -239,8 +257,8 @@ template <bool Signed> class IntegralAP final {
239
257
return false ;
240
258
}
241
259
242
- const APSInt &LHS = A.V ;
243
- const APSInt &RHS = B.V ;
260
+ const APSInt &LHS = APSInt ( A.V , A. isSigned ()) ;
261
+ const APSInt &RHS = APSInt ( B.V , B. isSigned ()) ;
244
262
245
263
APSInt Value (LHS.extend (BitWidth) + RHS.extend (BitWidth), false );
246
264
APSInt Result = Value.trunc (LHS.getBitWidth ());
0 commit comments