@@ -33,9 +33,9 @@ template <unsigned Bits, bool Signed> class Integral;
3333template <bool Signed> class IntegralAP final {
3434private:
3535 friend IntegralAP<!Signed>;
36- APSInt V;
36+ APInt V;
3737
38- template <typename T> static T truncateCast (const APSInt &V) {
38+ template <typename T> static T truncateCast (const APInt &V) {
3939 constexpr unsigned BitSize = sizeof (T) * 8 ;
4040 if (BitSize >= V.getBitWidth ())
4141 return std::is_signed_v<T> ? V.getSExtValue () : V.getZExtValue ();
@@ -48,23 +48,37 @@ template <bool Signed> class IntegralAP final {
4848 using AsUnsigned = IntegralAP<false >;
4949
5050 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)) {}
5453
5554 IntegralAP (APInt V) : V(V) {}
56- IntegralAP (APSInt V) : V(V) {}
5755 // / Arbitrary value for uninitialized variables.
58- IntegralAP () : V(APSInt::getMaxValue( 1024 , Signed) ) {}
56+ IntegralAP () : IntegralAP(- 1 , 1024 ) {}
5957
6058 IntegralAP operator -() const { return IntegralAP (-V); }
6159 IntegralAP operator -(const IntegralAP &Other) const {
6260 return IntegralAP (V - Other.V );
6361 }
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+ }
6882
6983 explicit operator bool () const { return !V.isZero (); }
7084 explicit operator int8_t () const { return truncateCast<int8_t >(V); }
@@ -78,42 +92,32 @@ template <bool Signed> class IntegralAP final {
7892
7993 template <typename T> static IntegralAP from (T Value, unsigned NumBits = 0 ) {
8094 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);
8396
8497 return IntegralAP<Signed>(Copy);
8598 }
8699
87100 template <bool InputSigned>
88101 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 );
96103 }
97104
98105 template <unsigned Bits, bool InputSigned>
99106 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);
103108
104- assert (Copy.isSigned () == Signed);
105109 return IntegralAP<Signed>(Copy);
106110 }
107111
108112 static IntegralAP zero (int32_t BitWidth) {
109- APSInt V = APSInt ( APInt (BitWidth, 0LL , Signed), ! Signed);
113+ APInt V = APInt (BitWidth, 0LL , Signed);
110114 return IntegralAP (V);
111115 }
112116
113117 constexpr unsigned bitWidth () const { return V.getBitWidth (); }
114118
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) ); }
117121
118122 bool isZero () const { return V.isZero (); }
119123 bool isPositive () const { return V.isNonNegative (); }
@@ -139,22 +143,38 @@ template <bool Signed> class IntegralAP final {
139143 }
140144
141145 IntegralAP<false > toUnsigned () const {
142- APSInt Copy = V;
143- Copy.setIsSigned (false );
146+ APInt Copy = V;
144147 return IntegralAP<false >(Copy);
145148 }
146149
147150 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;
149167 }
150168
151169 static bool increment (IntegralAP A, IntegralAP *R) {
170+ // FIXME: Implement.
152171 assert (false );
153- *R = IntegralAP (A.V + 1 );
172+ *R = IntegralAP (A.V - 1 );
154173 return false ;
155174 }
156175
157176 static bool decrement (IntegralAP A, IntegralAP *R) {
177+ // FIXME: Implement.
158178 assert (false );
159179 *R = IntegralAP (A.V - 1 );
160180 return false ;
@@ -170,48 +190,46 @@ template <bool Signed> class IntegralAP final {
170190 }
171191
172192 static bool mul (IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
193+ // FIXME: Implement.
173194 assert (false );
174- // return CheckMulUB(A.V, B.V, R->V);
175195 return false ;
176196 }
177197
178198 static bool rem (IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
199+ // FIXME: Implement.
179200 assert (false );
180- *R = IntegralAP (A.V % B.V );
181201 return false ;
182202 }
183203
184204 static bool div (IntegralAP A, IntegralAP B, unsigned OpBits, IntegralAP *R) {
205+ // FIXME: Implement.
185206 assert (false );
186- *R = IntegralAP (A.V / B.V );
187207 return false ;
188208 }
189209
190210 static bool bitAnd (IntegralAP A, IntegralAP B, unsigned OpBits,
191211 IntegralAP *R) {
212+ // FIXME: Implement.
192213 assert (false );
193- *R = IntegralAP (A.V & B.V );
194214 return false ;
195215 }
196216
197217 static bool bitOr (IntegralAP A, IntegralAP B, unsigned OpBits,
198218 IntegralAP *R) {
199219 assert (false );
200- *R = IntegralAP (A.V | B.V );
201220 return false ;
202221 }
203222
204223 static bool bitXor (IntegralAP A, IntegralAP B, unsigned OpBits,
205224 IntegralAP *R) {
225+ // FIXME: Implement.
206226 assert (false );
207- *R = IntegralAP (A.V ^ B.V );
208227 return false ;
209228 }
210229
211230 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 ();
215233 *R = IntegralAP (AI);
216234 return false ;
217235 }
@@ -223,12 +241,12 @@ template <bool Signed> class IntegralAP final {
223241
224242 static void shiftLeft (const IntegralAP A, const IntegralAP B, unsigned OpBits,
225243 IntegralAP *R) {
226- *R = IntegralAP (A.V << B.V .getZExtValue ());
244+ *R = IntegralAP (A.V . shl ( B.V .getZExtValue () ));
227245 }
228246
229247 static void shiftRight (const IntegralAP A, const IntegralAP B,
230248 unsigned OpBits, IntegralAP *R) {
231- *R = IntegralAP (A.V >> B.V .getZExtValue ());
249+ *R = IntegralAP (A.V . ashr ( B.V .getZExtValue () ));
232250 }
233251
234252private:
@@ -239,8 +257,8 @@ template <bool Signed> class IntegralAP final {
239257 return false ;
240258 }
241259
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 ()) ;
244262
245263 APSInt Value (LHS.extend (BitWidth) + RHS.extend (BitWidth), false );
246264 APSInt Result = Value.trunc (LHS.getBitWidth ());
0 commit comments