@@ -191,12 +191,11 @@ template <bool Signed> class IntegralAP final {
191
191
}
192
192
193
193
static bool add (IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
194
- return CheckAddUB (A, B, OpBits, R);
194
+ return CheckAddSubUB<std::plus> (A, B, OpBits, R);
195
195
}
196
196
197
197
static bool sub (IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
198
- // / FIXME: Gotta check if the result fits into OpBits bits.
199
- return CheckSubUB (A, B, R);
198
+ return CheckAddSubUB<std::minus>(A, B, OpBits, R);
200
199
}
201
200
202
201
static bool mul (IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
@@ -264,28 +263,21 @@ template <bool Signed> class IntegralAP final {
264
263
}
265
264
266
265
private:
267
- static bool CheckAddUB (const IntegralAP &A, const IntegralAP &B,
268
- unsigned BitWidth, IntegralAP *R) {
269
- if (!A.isSigned ()) {
270
- R->V = A.V + B.V ;
266
+ template <template <typename T> class Op >
267
+ static bool CheckAddSubUB (const IntegralAP &A, const IntegralAP &B,
268
+ unsigned BitWidth, IntegralAP *R) {
269
+ if constexpr (!Signed) {
270
+ R->V = Op<APInt>{}(A.V , B.V );
271
271
return false ;
272
272
}
273
273
274
- const APSInt &LHS = APSInt (A.V , A.isSigned ());
275
- const APSInt &RHS = APSInt (B.V , B.isSigned ());
276
-
277
- APSInt Value (LHS.extend (BitWidth) + RHS.extend (BitWidth), false );
274
+ const APSInt &LHS = A.toAPSInt ();
275
+ const APSInt &RHS = B.toAPSInt ();
276
+ APSInt Value = Op<APSInt>{}(LHS.extend (BitWidth), RHS.extend (BitWidth));
278
277
APSInt Result = Value.trunc (LHS.getBitWidth ());
279
- if (Result.extend (BitWidth) != Value)
280
- return true ;
281
-
282
278
R->V = Result;
283
- return false ;
284
- }
285
- static bool CheckSubUB (const IntegralAP &A, const IntegralAP &B,
286
- IntegralAP *R) {
287
- R->V = A.V - B.V ;
288
- return false ; // Success!
279
+
280
+ return Result.extend (BitWidth) != Value;
289
281
}
290
282
};
291
283
0 commit comments