@@ -33,17 +33,20 @@ class FixedPoint final {
3333 : V(APInt(0 , 0ULL , false ),
3434 llvm::FixedPointSemantics (0 , 0 , false , false , false )) {}
3535
36- static FixedPoint Zero (llvm::FixedPointSemantics Sem) {
36+ static FixedPoint zero (llvm::FixedPointSemantics Sem) {
3737 return FixedPoint (APInt (Sem.getWidth (), 0ULL , Sem.isSigned ()), Sem);
3838 }
3939
40- operator bool () const { return V.getBoolValue (); }
41- template <typename Ty, typename = std::enable_if_t <std::is_integral_v<Ty>>>
42- explicit operator Ty () const {
43- // FIXME
44- return 0 ;
40+ static FixedPoint from (const APSInt &I, llvm::FixedPointSemantics Sem,
41+ bool *Overflow) {
42+ return FixedPoint (llvm::APFixedPoint::getFromIntValue (I, Sem, Overflow));
43+ }
44+ static FixedPoint from (const llvm::APFloat &I, llvm::FixedPointSemantics Sem,
45+ bool *Overflow) {
46+ return FixedPoint (llvm::APFixedPoint::getFromFloatValue (I, Sem, Overflow));
4547 }
4648
49+ operator bool () const { return V.getBoolValue (); }
4750 void print (llvm::raw_ostream &OS) const { OS << V; }
4851
4952 APValue toAPValue (const ASTContext &) const { return APValue (V); }
@@ -55,9 +58,9 @@ class FixedPoint final {
5558 bool isNegative () const { return V.getValue ().isNegative (); }
5659 bool isPositive () const { return V.getValue ().isNonNegative (); }
5760 bool isMin () const {
58- return V.getValue () == APSInt::getMinValue (V.getSemantics ().getWidth (),
59- !V.getSemantics ().isSigned ());
61+ return V == llvm::APFixedPoint::getMin (V.getSemantics ());
6062 }
63+ bool isMinusOne () const { return V.isSigned () && V.getValue () == -1 ; }
6164
6265 FixedPoint truncate (unsigned BitWidth) const { return *this ; }
6366
@@ -70,14 +73,21 @@ class FixedPoint final {
7073 return V.convertToFloat (*Sem);
7174 }
7275
76+ llvm::APSInt toInt (unsigned BitWidth, bool Signed, bool *Overflow) const {
77+ return V.convertToInt (BitWidth, Signed, Overflow);
78+ }
79+
7380 std::string toDiagnosticString (const ASTContext &Ctx) const {
7481 return V.toString ();
7582 }
7683
7784 ComparisonCategoryResult compare (const FixedPoint &Other) const {
78- if (Other.V == V)
85+ int c = V.compare (Other.V );
86+ if (c == 0 )
7987 return ComparisonCategoryResult::Equal;
80- return ComparisonCategoryResult::Unordered;
88+ else if (c < 0 )
89+ return ComparisonCategoryResult::Less;
90+ return ComparisonCategoryResult::Greater;
8191 }
8292
8393 static bool neg (const FixedPoint &A, FixedPoint *R) {
@@ -94,16 +104,40 @@ class FixedPoint final {
94104 }
95105 static bool sub (const FixedPoint A, const FixedPoint B, unsigned Bits,
96106 FixedPoint *R) {
97- return true ;
107+ bool Overflow = false ;
108+ *R = FixedPoint (A.V .sub (B.V , &Overflow));
109+ return Overflow;
98110 }
99111 static bool mul (const FixedPoint A, const FixedPoint B, unsigned Bits,
100112 FixedPoint *R) {
101- return true ;
113+ bool Overflow = false ;
114+ *R = FixedPoint (A.V .mul (B.V , &Overflow));
115+ return Overflow;
102116 }
103117 static bool div (const FixedPoint A, const FixedPoint B, unsigned Bits,
104118 FixedPoint *R) {
119+ bool Overflow = false ;
120+ *R = FixedPoint (A.V .div (B.V , &Overflow));
121+ return Overflow;
122+ }
123+ static bool rem (const FixedPoint A, const FixedPoint B, unsigned Bits,
124+ FixedPoint *R) {
125+ llvm_unreachable (" Rem doesn't exist for fixed point values" );
126+ return true ;
127+ }
128+ static bool bitAnd (const FixedPoint A, const FixedPoint B, unsigned Bits,
129+ FixedPoint *R) {
105130 return true ;
106131 }
132+ static bool bitOr (const FixedPoint A, const FixedPoint B, unsigned Bits,
133+ FixedPoint *R) {
134+ return true ;
135+ }
136+ static bool bitXor (const FixedPoint A, const FixedPoint B, unsigned Bits,
137+ FixedPoint *R) {
138+ return true ;
139+ }
140+
107141 static bool increment (const FixedPoint &A, FixedPoint *R) { return true ; }
108142 static bool decrement (const FixedPoint &A, FixedPoint *R) { return true ; }
109143};
0 commit comments