Skip to content

Commit 94e8ec6

Browse files
committed
[AST] Add fixed-point division constant evaluation.
Reviewers: rjmccall, leonardchan, bjope Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D73187
1 parent 53f5c8b commit 94e8ec6

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

clang/include/clang/Basic/FixedPoint.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class APFixedPoint {
130130
APFixedPoint add(const APFixedPoint &Other, bool *Overflow = nullptr) const;
131131
APFixedPoint sub(const APFixedPoint &Other, bool *Overflow = nullptr) const;
132132
APFixedPoint mul(const APFixedPoint &Other, bool *Overflow = nullptr) const;
133+
APFixedPoint div(const APFixedPoint &Other, bool *Overflow = nullptr) const;
133134

134135
/// Perform a unary negation (-X) on this fixed point type, taking into
135136
/// account saturation if applicable.

clang/lib/AST/ExprConstant.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12948,6 +12948,15 @@ bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1294812948
return false;
1294912949
return Success(Result, E);
1295012950
}
12951+
case BO_Div: {
12952+
bool AddOverflow, ConversionOverflow;
12953+
APFixedPoint Result = LHSFX.div(RHSFX, &AddOverflow)
12954+
.convert(ResultFXSema, &ConversionOverflow);
12955+
if ((AddOverflow || ConversionOverflow) &&
12956+
!HandleOverflow(Info, E, Result, E->getType()))
12957+
return false;
12958+
return Success(Result, E);
12959+
}
1295112960
default:
1295212961
return false;
1295312962
}

clang/lib/Basic/FixedPoint.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,61 @@ APFixedPoint APFixedPoint::mul(const APFixedPoint &Other,
254254
CommonFXSema);
255255
}
256256

257+
APFixedPoint APFixedPoint::div(const APFixedPoint &Other,
258+
bool *Overflow) const {
259+
auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics());
260+
APFixedPoint ConvertedThis = convert(CommonFXSema);
261+
APFixedPoint ConvertedOther = Other.convert(CommonFXSema);
262+
llvm::APSInt ThisVal = ConvertedThis.getValue();
263+
llvm::APSInt OtherVal = ConvertedOther.getValue();
264+
bool Overflowed = false;
265+
266+
// Widen the LHS and RHS so we can perform a full division.
267+
unsigned Wide = CommonFXSema.getWidth() * 2;
268+
if (CommonFXSema.isSigned()) {
269+
ThisVal = ThisVal.sextOrSelf(Wide);
270+
OtherVal = OtherVal.sextOrSelf(Wide);
271+
} else {
272+
ThisVal = ThisVal.zextOrSelf(Wide);
273+
OtherVal = OtherVal.zextOrSelf(Wide);
274+
}
275+
276+
// Upscale to compensate for the loss of precision from division, and
277+
// perform the full division.
278+
ThisVal = ThisVal.shl(CommonFXSema.getScale());
279+
llvm::APSInt Result;
280+
if (CommonFXSema.isSigned()) {
281+
llvm::APInt Rem;
282+
llvm::APInt::sdivrem(ThisVal, OtherVal, Result, Rem);
283+
// If the quotient is negative and the remainder is nonzero, round
284+
// towards negative infinity by subtracting epsilon from the result.
285+
if (Result.isNegative() && !Rem.isNullValue())
286+
Result = Result - 1;
287+
} else
288+
Result = ThisVal.udiv(OtherVal);
289+
Result.setIsSigned(CommonFXSema.isSigned());
290+
291+
// If our result lies outside of the representative range of the common
292+
// semantic, we either have overflow or saturation.
293+
llvm::APSInt Max = APFixedPoint::getMax(CommonFXSema).getValue()
294+
.extOrTrunc(Wide);
295+
llvm::APSInt Min = APFixedPoint::getMin(CommonFXSema).getValue()
296+
.extOrTrunc(Wide);
297+
if (CommonFXSema.isSaturated()) {
298+
if (Result < Min)
299+
Result = Min;
300+
else if (Result > Max)
301+
Result = Max;
302+
} else
303+
Overflowed = Result < Min || Result > Max;
304+
305+
if (Overflow)
306+
*Overflow = Overflowed;
307+
308+
return APFixedPoint(Result.sextOrTrunc(CommonFXSema.getWidth()),
309+
CommonFXSema);
310+
}
311+
257312
void APFixedPoint::toString(llvm::SmallVectorImpl<char> &Str) const {
258313
llvm::APSInt Val = getValue();
259314
unsigned Scale = getScale();

clang/test/Frontend/fixed_point_div.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,70 @@
11
// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,SIGNED
22
// 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
33

4+
// Division between different fixed point types
5+
short _Accum sa_const = 1.0hk / 2.0hk; // CHECK-DAG: @sa_const = {{.*}}global i16 64, align 2
6+
_Accum a_const = 1.0hk / 2.0k; // CHECK-DAG: @a_const = {{.*}}global i32 16384, align 4
7+
long _Accum la_const = 1.0hk / 2.0lk; // CHECK-DAG: @la_const = {{.*}}global i64 1073741824, align 8
8+
short _Accum sa_const2 = 0.5hr / 2.0hk; // CHECK-DAG: @sa_const2 = {{.*}}global i16 32, align 2
9+
short _Accum sa_const3 = 0.5r / 2.0hk; // CHECK-DAG: @sa_const3 = {{.*}}global i16 32, align 2
10+
short _Accum sa_const4 = 0.5lr / 2.0hk; // CHECK-DAG: @sa_const4 = {{.*}}global i16 32, align 2
11+
short _Accum sa_const5 = 2.0hk / 0.5lr; // CHECK-DAG: @sa_const5 = {{.*}}global i16 512, align 2
12+
13+
// Unsigned division
14+
unsigned short _Accum usa_const = 3.0uhk / 2.0uhk;
15+
// CHECK-SIGNED-DAG: @usa_const = {{.*}}global i16 192, align 2
16+
// CHECK-UNSIGNED-DAG: @usa_const = {{.*}}global i16 384, align 2
17+
18+
// Unsigned / signed
19+
short _Accum sa_const6 = 1.0uhk / 2.0hk;
20+
// CHECK-DAG: @sa_const6 = {{.*}}global i16 64, align 2
21+
22+
// Division with negative number
23+
short _Accum sa_const7 = 0.5hr / (-2.0hk);
24+
// CHECK-DAG: @sa_const7 = {{.*}}global i16 -32, align 2
25+
26+
// Int division
27+
unsigned short _Accum usa_const2 = 2 / 0.5uhk;
28+
// CHECK-SIGNED-DAG: @usa_const2 = {{.*}}global i16 512, align 2
29+
// CHECK-UNSIGNED-DAG: @usa_const2 = {{.*}}global i16 1024, align 2
30+
short _Accum sa_const8 = 2 / (-0.5hk); // CHECK-DAG: @sa_const8 = {{.*}}global i16 -512, align 2
31+
short _Accum sa_const9 = 256 / 2.0hk; // CHECK-DAG: @sa_const9 = {{.*}}global i16 16384, align 2
32+
long _Fract lf_const = 0.5lr / -1; // CHECK-DAG: @lf_const = {{.*}}global i32 -1073741824, align 4
33+
34+
// Saturated division
35+
_Sat short _Accum sat_sa_const = (_Sat short _Accum)128.0hk / (-0.25hk);
36+
// CHECK-DAG: @sat_sa_const = {{.*}}global i16 -32768, align 2
37+
_Sat unsigned short _Accum sat_usa_const = (_Sat unsigned short _Accum)128.0uhk / (0.25uhk);
38+
// CHECK-SIGNED-DAG: @sat_usa_const = {{.*}}global i16 65535, align 2
39+
// CHECK-UNSIGNED-DAG: @sat_usa_const = {{.*}}global i16 32767, align 2
40+
_Sat short _Accum sat_sa_const2 = (_Sat short _Accum)-128.0hk / (-0.0125hr);
41+
// CHECK-DAG: @sat_sa_const2 = {{.*}}global i16 32767, align 2
42+
_Sat unsigned short _Accum sat_usa_const2 = (_Sat unsigned short _Accum)128.0uhk / (-128);
43+
// CHECK-SIGNED-DAG: @sat_usa_const2 = {{.*}}global i16 65535, align 2
44+
// CHECK-UNSIGNED-DAG: @sat_usa_const2 = {{.*}}global i16 32767, align 2
45+
_Sat unsigned short _Accum sat_usa_const3 = (_Sat unsigned short _Accum)0.5uhk / -1;
46+
// CHECK-DAG: @sat_usa_const3 = {{.*}}global i16 0, align 2
47+
_Sat short _Accum sat_sa_const3 = (_Sat short _Accum)-128.0hk / 128;
48+
// CHECK-DAG: @sat_sa_const3 = {{.*}}global i16 -128, align 2
49+
_Sat short _Accum sat_sa_const4 = (_Sat short _Accum)-25.7hk / 0.1lk;
50+
// CHECK-DAG: @sat_sa_const4 = {{.*}}global i16 -32768, align 2
51+
52+
// Some more cases
53+
short _Accum sa_const10 = 255.9921875hk / 255.9921875hk;
54+
// CHECK-DAG: @sa_const10 = {{.*}}global i16 128, align 2
55+
short _Accum sat_sa_const5 = (_Sat short _Accum)(-255.0hk - 1.0hk) / 0.0078125hk;
56+
// CHECK-DAG: @sat_sa_const5 = {{.*}}global i16 -32768, align 2
57+
_Sat short _Accum sat_sa_const6 = (_Sat short _Accum)(-255.0hk - 1.0hk) / -0.0078125hk;
58+
// CHECK-DAG: @sat_sa_const6 = {{.*}}global i16 32767, align 2
59+
short _Accum sa_const12 = 255.9921875hk / -1.0hk;
60+
// CHECK-DAG: @sa_const12 = {{.*}}global i16 -32767, align 2
61+
_Sat short _Accum sat_sa_const7 = (_Sat short _Accum)(-255.0hk - 1.0hk) / -1.0hk;
62+
// CHECK-DAG: @sat_sa_const7 = {{.*}}global i16 32767, align 2
63+
short _Accum sa_const13 = 0.0234375hk / 2.0hk;
64+
// CHECK-DAG: @sa_const13 = {{.*}}global i16 1, align 2
65+
short _Accum sa_const14 = -0.0234375hk / 2.0hk;
66+
// CHECK-DAG: @sa_const14 = {{.*}}global i16 -2, align 2
67+
468
void SignedDivision() {
569
// CHECK-LABEL: SignedDivision
670
short _Accum sa;

0 commit comments

Comments
 (0)