Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes unsigned range souffle-lang/souffle#2182 #2184

Merged
merged 2 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/include/souffle/utility/EvaluatorUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace souffle::evaluator {

template <typename A, typename F /* Tuple<RamDomain,1> -> void */>
void runRange(A from, A to, A step, F&& go) {
void runRange(const A from, const A to, const A step, F&& go) {
#define GO(x) go(Tuple<RamDomain, 1>{ramBitCast(x)})
if (0 < step) {
for (auto x = from; x < to; x += step) {
Expand All @@ -42,8 +42,26 @@ void runRange(A from, A to, A step, F&& go) {
}

template <typename A, typename F /* Tuple<RamDomain,1> -> void */>
void runRange(A from, A to, F&& go) {
return runRange(from, to, A(from <= to ? 1 : -1), std::forward<F>(go));
void runRangeBackward(const A from, const A to, F&& func) {
assert(from > to);
if (from > to) {
for (auto x = from; x > to; --x) {
func(Tuple<RamDomain, 1>{ramBitCast(x)});
}
}
}

template <typename A, typename F /* Tuple<RamDomain,1> -> void */>
void runRange(const A from, const A to, F&& go) {
if constexpr (std::is_unsigned<A>()) {
if (from <= to) {
runRange(from, to, static_cast<A>(1U), std::forward<F>(go));
} else {
runRangeBackward(from, to, std::forward<F>(go));
}
} else {
return runRange(from, to, A(from <= to ? 1 : -1), std::forward<F>(go));
}
}

template <typename A>
Expand Down
4 changes: 4 additions & 0 deletions tests/evaluation/rangeop/out10.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-0.25
-0.5
-0.75
-1
Empty file.
9 changes: 9 additions & 0 deletions tests/evaluation/rangeop/out5.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
1
2
3
4
5
6
7
8
9
9 changes: 9 additions & 0 deletions tests/evaluation/rangeop/out6.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
1
2
3
4
5
6
7
8
9
8 changes: 8 additions & 0 deletions tests/evaluation/rangeop/out7.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1.25
2.25
3.25
4.25
5.25
6.25
7.25
8.25
13 changes: 13 additions & 0 deletions tests/evaluation/rangeop/out8.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-0.5
-1
-1.5
-2
-2.5
-3
-3.5
-4
-4.5
0
0.5
1
1.5
10 changes: 10 additions & 0 deletions tests/evaluation/rangeop/out9.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-0.5
-1.5
-2.5
-3.5
-4.5
0.5
1.5
2.5
3.5
4.5
31 changes: 31 additions & 0 deletions tests/evaluation/rangeop/rangeop.dl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ out1(X) :- X = range(1, 10).
.output out2()
out2(X) :- X = range(9, 0, -1).

// Range with contradicting step
.decl out2b(X:number)
.output out2b()
out2b(X) :- X = range(9, 0, 1).

// Range appearing twice with the same endpoints
.decl out3(X:number, Y:number)
.output out3()
Expand All @@ -25,3 +30,29 @@ out3(X,Y) :- X = range(1,3), Y = range(1,3).
.decl out4(X:number, Y:number)
.output out4()
out4(X,Y) :- X = range(0,3), Y = range(0,3), X < Y, X + Y = 3.

// Unsigned range
.decl out5(X:unsigned)
.output out5
out5(X) :- X = range(1, 10).

.decl out6(X:unsigned)
.output out6
out6(X) :- X = range(9, 0).

// Float range
.decl out7(X:float)
.output out7
out7(X) :- X = range(1.25, 9.25).

.decl out8(X:float)
.output out8
out8(X) :- X = range(1.5, -4.6, -0.5).

.decl out9(X:float)
.output out9
out9(X) :- X = range(4.5, -4.6).

.decl out10(X:float)
.output out10
out10(X) :- X = range(-1, 0, 0.25).
4 changes: 4 additions & 0 deletions tests/semantic/range/b_u.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2
3
4
5