[clang] eccf7fc - [AST] Add fixed-point subtraction constant evaluation.
Bevin Hansson via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 26 04:41:55 PDT 2020
Author: Bevin Hansson
Date: 2020-06-26T13:38:11+02:00
New Revision: eccf7fc7b31a00823a0fe6c782ef312b3ba743c4
URL: https://github.com/llvm/llvm-project/commit/eccf7fc7b31a00823a0fe6c782ef312b3ba743c4
DIFF: https://github.com/llvm/llvm-project/commit/eccf7fc7b31a00823a0fe6c782ef312b3ba743c4.diff
LOG: [AST] Add fixed-point subtraction constant evaluation.
Reviewers: rjmccall, leonardchan
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D73185
Added:
Modified:
clang/include/clang/Basic/FixedPoint.h
clang/lib/AST/ExprConstant.cpp
clang/lib/Basic/FixedPoint.cpp
clang/test/Frontend/fixed_point_sub.c
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/FixedPoint.h b/clang/include/clang/Basic/FixedPoint.h
index 55465c604a7d..1f3251982325 100644
--- a/clang/include/clang/Basic/FixedPoint.h
+++ b/clang/include/clang/Basic/FixedPoint.h
@@ -28,8 +28,8 @@ class QualType;
/// The fixed point semantics work similarly to llvm::fltSemantics. The width
/// specifies the whole bit width of the underlying scaled integer (with padding
/// if any). The scale represents the number of fractional bits in this type.
-/// When HasUnsignedPadding is true and this type is signed, the first bit
-/// in the value this represents is treaded as padding.
+/// When HasUnsignedPadding is true and this type is unsigned, the first bit
+/// in the value this represents is treated as padding.
class FixedPointSemantics {
public:
FixedPointSemantics(unsigned Width, unsigned Scale, bool IsSigned,
@@ -125,9 +125,10 @@ class APFixedPoint {
// Perform binary operations on a fixed point type. The resulting fixed point
// value will be in the common, full precision semantics that can represent
- // the precision and ranges os both input values. See convert() for an
+ // the precision and ranges of both input values. See convert() for an
// explanation of the Overflow parameter.
APFixedPoint add(const APFixedPoint &Other, bool *Overflow = nullptr) const;
+ APFixedPoint sub(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
index afae020f3dd2..5cdeb3cd0e67 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -12924,7 +12924,16 @@ bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
case BO_Add: {
bool AddOverflow, ConversionOverflow;
APFixedPoint Result = LHSFX.add(RHSFX, &AddOverflow)
- .convert(ResultFXSema, &ConversionOverflow);
+ .convert(ResultFXSema, &ConversionOverflow);
+ if ((AddOverflow || ConversionOverflow) &&
+ !HandleOverflow(Info, E, Result, E->getType()))
+ return false;
+ return Success(Result, E);
+ }
+ case BO_Sub: {
+ bool AddOverflow, ConversionOverflow;
+ APFixedPoint Result = LHSFX.sub(RHSFX, &AddOverflow)
+ .convert(ResultFXSema, &ConversionOverflow);
if ((AddOverflow || ConversionOverflow) &&
!HandleOverflow(Info, E, Result, E->getType()))
return false;
diff --git a/clang/lib/Basic/FixedPoint.cpp b/clang/lib/Basic/FixedPoint.cpp
index 05600dfc6d21..29d532f49561 100644
--- a/clang/lib/Basic/FixedPoint.cpp
+++ b/clang/lib/Basic/FixedPoint.cpp
@@ -173,6 +173,30 @@ APFixedPoint APFixedPoint::add(const APFixedPoint &Other,
return APFixedPoint(Result, CommonFXSema);
}
+APFixedPoint APFixedPoint::sub(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;
+
+ llvm::APSInt Result;
+ if (CommonFXSema.isSaturated()) {
+ Result = CommonFXSema.isSigned() ? ThisVal.ssub_sat(OtherVal)
+ : ThisVal.usub_sat(OtherVal);
+ } else {
+ Result = ThisVal.isSigned() ? ThisVal.ssub_ov(OtherVal, Overflowed)
+ : ThisVal.usub_ov(OtherVal, Overflowed);
+ }
+
+ if (Overflow)
+ *Overflow = Overflowed;
+
+ return APFixedPoint(Result, CommonFXSema);
+}
+
void APFixedPoint::toString(llvm::SmallVectorImpl<char> &Str) const {
llvm::APSInt Val = getValue();
unsigned Scale = getScale();
diff --git a/clang/test/Frontend/fixed_point_sub.c b/clang/test/Frontend/fixed_point_sub.c
index 59b2e0a43aed..1e449537ae23 100644
--- a/clang/test/Frontend/fixed_point_sub.c
+++ b/clang/test/Frontend/fixed_point_sub.c
@@ -1,6 +1,55 @@
// RUN: %clang_cc1 -ffixed-point -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,SIGNED
// RUN: %clang_cc1 -ffixed-point -fpadding-on-unsigned-fixed-point -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,UNSIGNED
+// Subtraction between
diff erent fixed point types
+short _Accum sa_const = 1.0hk - 2.0hk; // CHECK-DAG: @sa_const = {{.*}}global i16 -128, align 2
+_Accum a_const = 1.0hk - 2.0k; // CHECK-DAG: @a_const = {{.*}}global i32 -32768, align 4
+long _Accum la_const = 1.0hk - 2.0lk; // CHECK-DAG: @la_const = {{.*}}global i64 -2147483648, align 8
+short _Accum sa_const2 = 0.5hr - 2.0hk; // CHECK-DAG: @sa_const2 = {{.*}}global i16 -192, align 2
+short _Accum sa_const3 = 0.5r - 2.0hk; // CHECK-DAG: @sa_const3 = {{.*}}global i16 -192, align 2
+short _Accum sa_const4 = 0.5lr - 2.0hk; // CHECK-DAG: @sa_const4 = {{.*}}global i16 -192, align 2
+short _Accum sa_const5 = 2.0hk - 0.5lr; // CHECK-DAG: @sa_const5 = {{.*}}global i16 192, align 2
+
+// Unsigned subtraction
+unsigned short _Accum usa_const = 3.0uhk - 2.0uhk;
+// CHECK-SIGNED-DAG: @usa_const = {{.*}}global i16 768, 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 -128, align 2
+
+// Subtraction with negative number
+short _Accum sa_const7 = 0.5hr - (-2.0hk);
+// CHECK-DAG: @sa_const7 = {{.*}}global i16 320, align 2
+
+// Int subtraction
+unsigned short _Accum usa_const2 = 2 - 0.5uhk;
+// CHECK-SIGNED-DAG: @usa_const2 = {{.*}}global i16 640, align 2
+// CHECK-UNSIGNED-DAG: @usa_const2 = {{.*}}global i16 320, align 2
+short _Accum sa_const8 = 2 - (-0.5hk); // CHECK-DAG: @sa_const8 = {{.*}}global i16 320, align 2
+short _Accum sa_const9 = 257 - 2.0hk; // CHECK-DAG: @sa_const9 = {{.*}}global i16 32640, align 2
+long _Fract lf_const = 0.5lr - 1; // CHECK-DAG: @lf_const = {{.*}}global i32 -1073741824, align 4
+
+// Saturated subtraction
+_Sat short _Accum sat_sa_const = (_Sat short _Accum)128.0hk - (-128.0hk);
+// CHECK-DAG: @sat_sa_const = {{.*}}global i16 32767, align 2
+_Sat unsigned short _Accum sat_usa_const = (_Sat unsigned short _Accum)128.0uhk - (-128.0uhk);
+// 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 - (-128);
+// 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 - 2;
+// 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 -32768, align 2
+_Sat short _Accum sat_sa_const4 = (_Sat short _Accum)-150.0hk - 130.0lk;
+// CHECK-DAG: @sat_sa_const4 = {{.*}}global i16 -32768, align 2
+
+
void SignedSubtraction() {
// CHECK-LABEL: SignedSubtraction
short _Accum sa;
More information about the cfe-commits
mailing list