diff --git a/clang/include/clang/Basic/FixedPoint.h b/clang/include/clang/Basic/FixedPoint.h --- a/clang/include/clang/Basic/FixedPoint.h +++ b/clang/include/clang/Basic/FixedPoint.h @@ -130,6 +130,7 @@ APFixedPoint add(const APFixedPoint &Other, bool *Overflow = nullptr) const; APFixedPoint sub(const APFixedPoint &Other, bool *Overflow = nullptr) const; APFixedPoint mul(const APFixedPoint &Other, bool *Overflow = nullptr) const; + APFixedPoint div(const APFixedPoint &Other, bool *Overflow = nullptr) const; /// Perform a unary negation (-X) on this fixed point type, taking into /// account saturation if applicable. diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -12948,6 +12948,15 @@ return false; return Success(Result, E); } + case BO_Div: { + bool AddOverflow, ConversionOverflow; + APFixedPoint Result = LHSFX.div(RHSFX, &AddOverflow) + .convert(ResultFXSema, &ConversionOverflow); + if ((AddOverflow || ConversionOverflow) && + !HandleOverflow(Info, E, Result, E->getType())) + return false; + return Success(Result, E); + } default: return false; } diff --git a/clang/lib/Basic/FixedPoint.cpp b/clang/lib/Basic/FixedPoint.cpp --- a/clang/lib/Basic/FixedPoint.cpp +++ b/clang/lib/Basic/FixedPoint.cpp @@ -254,6 +254,61 @@ CommonFXSema); } +APFixedPoint APFixedPoint::div(const APFixedPoint &Other, + bool *Overflow) const { + auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics()); + APFixedPoint ConvertedThis = convert(CommonFXSema); + APFixedPoint ConvertedOther = Other.convert(CommonFXSema); + llvm::APSInt ThisVal = ConvertedThis.getValue(); + llvm::APSInt OtherVal = ConvertedOther.getValue(); + bool Overflowed = false; + + // Widen the LHS and RHS so we can perform a full division. + unsigned Wide = CommonFXSema.getWidth() * 2; + if (CommonFXSema.isSigned()) { + ThisVal = ThisVal.sextOrSelf(Wide); + OtherVal = OtherVal.sextOrSelf(Wide); + } else { + ThisVal = ThisVal.zextOrSelf(Wide); + OtherVal = OtherVal.zextOrSelf(Wide); + } + + // Upscale to compensate for the loss of precision from division, and + // perform the full division. + ThisVal = ThisVal.shl(CommonFXSema.getScale()); + llvm::APSInt Result; + if (CommonFXSema.isSigned()) { + llvm::APInt Rem; + llvm::APInt::sdivrem(ThisVal, OtherVal, Result, Rem); + // If the quotient is negative and the remainder is nonzero, round + // towards negative infinity by subtracting epsilon from the result. + if (Result.isNegative() && !Rem.isNullValue()) + Result = Result - 1; + } else + Result = ThisVal.udiv(OtherVal); + Result.setIsSigned(CommonFXSema.isSigned()); + + // If our result lies outside of the representative range of the common + // semantic, we either have overflow or saturation. + llvm::APSInt Max = APFixedPoint::getMax(CommonFXSema).getValue() + .extOrTrunc(Wide); + llvm::APSInt Min = APFixedPoint::getMin(CommonFXSema).getValue() + .extOrTrunc(Wide); + if (CommonFXSema.isSaturated()) { + if (Result < Min) + Result = Min; + else if (Result > Max) + Result = Max; + } else + Overflowed = Result < Min || Result > Max; + + if (Overflow) + *Overflow = Overflowed; + + return APFixedPoint(Result.sextOrTrunc(CommonFXSema.getWidth()), + CommonFXSema); +} + void APFixedPoint::toString(llvm::SmallVectorImpl &Str) const { llvm::APSInt Val = getValue(); unsigned Scale = getScale(); diff --git a/clang/test/Frontend/fixed_point_div.c b/clang/test/Frontend/fixed_point_div.c --- a/clang/test/Frontend/fixed_point_div.c +++ b/clang/test/Frontend/fixed_point_div.c @@ -1,6 +1,70 @@ // RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,SIGNED // RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -fpadding-on-unsigned-fixed-point -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,UNSIGNED +// Division between different fixed point types +short _Accum sa_const = 1.0hk / 2.0hk; // CHECK-DAG: @sa_const = {{.*}}global i16 64, align 2 +_Accum a_const = 1.0hk / 2.0k; // CHECK-DAG: @a_const = {{.*}}global i32 16384, align 4 +long _Accum la_const = 1.0hk / 2.0lk; // CHECK-DAG: @la_const = {{.*}}global i64 1073741824, align 8 +short _Accum sa_const2 = 0.5hr / 2.0hk; // CHECK-DAG: @sa_const2 = {{.*}}global i16 32, align 2 +short _Accum sa_const3 = 0.5r / 2.0hk; // CHECK-DAG: @sa_const3 = {{.*}}global i16 32, align 2 +short _Accum sa_const4 = 0.5lr / 2.0hk; // CHECK-DAG: @sa_const4 = {{.*}}global i16 32, align 2 +short _Accum sa_const5 = 2.0hk / 0.5lr; // CHECK-DAG: @sa_const5 = {{.*}}global i16 512, align 2 + +// Unsigned division +unsigned short _Accum usa_const = 3.0uhk / 2.0uhk; +// CHECK-SIGNED-DAG: @usa_const = {{.*}}global i16 192, align 2 +// CHECK-UNSIGNED-DAG: @usa_const = {{.*}}global i16 384, align 2 + +// Unsigned / signed +short _Accum sa_const6 = 1.0uhk / 2.0hk; +// CHECK-DAG: @sa_const6 = {{.*}}global i16 64, align 2 + +// Division with negative number +short _Accum sa_const7 = 0.5hr / (-2.0hk); +// CHECK-DAG: @sa_const7 = {{.*}}global i16 -32, align 2 + +// Int division +unsigned short _Accum usa_const2 = 2 / 0.5uhk; +// CHECK-SIGNED-DAG: @usa_const2 = {{.*}}global i16 512, align 2 +// CHECK-UNSIGNED-DAG: @usa_const2 = {{.*}}global i16 1024, align 2 +short _Accum sa_const8 = 2 / (-0.5hk); // CHECK-DAG: @sa_const8 = {{.*}}global i16 -512, align 2 +short _Accum sa_const9 = 256 / 2.0hk; // CHECK-DAG: @sa_const9 = {{.*}}global i16 16384, align 2 +long _Fract lf_const = 0.5lr / -1; // CHECK-DAG: @lf_const = {{.*}}global i32 -1073741824, align 4 + +// Saturated division +_Sat short _Accum sat_sa_const = (_Sat short _Accum)128.0hk / (-0.25hk); +// CHECK-DAG: @sat_sa_const = {{.*}}global i16 -32768, align 2 +_Sat unsigned short _Accum sat_usa_const = (_Sat unsigned short _Accum)128.0uhk / (0.25uhk); +// CHECK-SIGNED-DAG: @sat_usa_const = {{.*}}global i16 65535, align 2 +// CHECK-UNSIGNED-DAG: @sat_usa_const = {{.*}}global i16 32767, align 2 +_Sat short _Accum sat_sa_const2 = (_Sat short _Accum)-128.0hk / (-0.0125hr); +// CHECK-DAG: @sat_sa_const2 = {{.*}}global i16 32767, align 2 +_Sat unsigned short _Accum sat_usa_const2 = (_Sat unsigned short _Accum)128.0uhk / (-128); +// CHECK-SIGNED-DAG: @sat_usa_const2 = {{.*}}global i16 65535, align 2 +// CHECK-UNSIGNED-DAG: @sat_usa_const2 = {{.*}}global i16 32767, align 2 +_Sat unsigned short _Accum sat_usa_const3 = (_Sat unsigned short _Accum)0.5uhk / -1; +// CHECK-DAG: @sat_usa_const3 = {{.*}}global i16 0, align 2 +_Sat short _Accum sat_sa_const3 = (_Sat short _Accum)-128.0hk / 128; +// CHECK-DAG: @sat_sa_const3 = {{.*}}global i16 -128, align 2 +_Sat short _Accum sat_sa_const4 = (_Sat short _Accum)-25.7hk / 0.1lk; +// CHECK-DAG: @sat_sa_const4 = {{.*}}global i16 -32768, align 2 + +// Some more cases +short _Accum sa_const10 = 255.9921875hk / 255.9921875hk; +// CHECK-DAG: @sa_const10 = {{.*}}global i16 128, align 2 +short _Accum sat_sa_const5 = (_Sat short _Accum)(-255.0hk - 1.0hk) / 0.0078125hk; +// CHECK-DAG: @sat_sa_const5 = {{.*}}global i16 -32768, align 2 +_Sat short _Accum sat_sa_const6 = (_Sat short _Accum)(-255.0hk - 1.0hk) / -0.0078125hk; +// CHECK-DAG: @sat_sa_const6 = {{.*}}global i16 32767, align 2 +short _Accum sa_const12 = 255.9921875hk / -1.0hk; +// CHECK-DAG: @sa_const12 = {{.*}}global i16 -32767, align 2 +_Sat short _Accum sat_sa_const7 = (_Sat short _Accum)(-255.0hk - 1.0hk) / -1.0hk; +// CHECK-DAG: @sat_sa_const7 = {{.*}}global i16 32767, align 2 +short _Accum sa_const13 = 0.0234375hk / 2.0hk; +// CHECK-DAG: @sa_const13 = {{.*}}global i16 1, align 2 +short _Accum sa_const14 = -0.0234375hk / 2.0hk; +// CHECK-DAG: @sa_const14 = {{.*}}global i16 -2, align 2 + void SignedDivision() { // CHECK-LABEL: SignedDivision short _Accum sa;