@@ -32,6 +32,12 @@ template <unsigned Bits, bool Signed> class Integral;
32
32
class Boolean ;
33
33
34
34
template <bool Signed> class IntegralAP final {
35
+ private:
36
+ template <typename T> static T truncateCast (const APSInt &V) {
37
+ return std::is_signed_v<T> ? V.trunc (sizeof (T) * 8 ).getSExtValue ()
38
+ : V.trunc (sizeof (T) * 8 ).getZExtValue ();
39
+ }
40
+
35
41
public:
36
42
APSInt V;
37
43
@@ -55,14 +61,14 @@ template <bool Signed> class IntegralAP final {
55
61
bool operator <=(IntegralAP RHS) const { return V <= RHS.V ; }
56
62
57
63
explicit operator bool () const { return !V.isZero (); }
58
- explicit operator int8_t () const { return V. getSExtValue ( ); }
59
- explicit operator uint8_t () const { return V. getZExtValue ( ); }
60
- explicit operator int16_t () const { return V. getSExtValue ( ); }
61
- explicit operator uint16_t () const { return V. getZExtValue ( ); }
62
- explicit operator int32_t () const { return V. getSExtValue ( ); }
63
- explicit operator uint32_t () const { return V. getZExtValue ( ); }
64
- explicit operator int64_t () const { return V. getSExtValue ( ); }
65
- explicit operator uint64_t () const { return V. getZExtValue ( ); }
64
+ explicit operator int8_t () const { return truncateCast< int8_t >(V ); }
65
+ explicit operator uint8_t () const { return truncateCast< uint8_t >(V ); }
66
+ explicit operator int16_t () const { return truncateCast< int16_t >(V ); }
67
+ explicit operator uint16_t () const { return truncateCast< uint16_t >(V ); }
68
+ explicit operator int32_t () const { return truncateCast< int32_t >(V ); }
69
+ explicit operator uint32_t () const { return truncateCast< uint32_t >(V ); }
70
+ explicit operator int64_t () const { return truncateCast< int64_t >(V ); }
71
+ explicit operator uint64_t () const { return truncateCast< uint64_t >(V ); }
66
72
67
73
template <typename T> static IntegralAP from (T Value, unsigned NumBits = 0 ) {
68
74
assert (NumBits > 0 );
0 commit comments