Skip to content

Commit 1095215

Browse files
authored
improve detection of fake "better" programs (#159)
1 parent 2a0f2b1 commit 1095215

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ To install or update LODA, please follow the [installation instructions](https:/
55
### Enhancements
66

77
* Improve handling of loops with constant number of iterations
8+
* Improve detection of fake "better" programs
89

910
# v22.6.19
1011

src/finder.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,12 @@ std::pair<std::string, steps_t> Finder::isOptimizedBetter(
313313
}
314314
result.second = optimized_steps;
315315

316-
// check if the first non-decreasing term is beyond the know sequence terms
317-
if (tmp.get_first_non_decreasing_term() >=
318-
static_cast<int64_t>(terms.size())) {
319-
return result; // bad
316+
// check if the first decreasing/non-increasing term is beyond the known
317+
// sequence terms => fake "better" program
318+
const int64_t s = terms.size();
319+
if (tmp.get_first_delta_lt(Number::ZERO) >= s || // decreasing
320+
tmp.get_first_delta_lt(Number::ONE) >= s) { // non-increasing
321+
return result; // => fake "better" program
320322
}
321323

322324
// evaluate existing program for same number of terms

src/include/sequence.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Sequence : public std::vector<Number> {
1717

1818
bool is_linear(size_t start) const;
1919

20-
int64_t get_first_non_decreasing_term() const;
20+
int64_t get_first_delta_lt(const Number &d) const;
2121

2222
bool align(const Sequence &s, int64_t max_offset);
2323

src/sequence.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ bool Sequence::is_linear(size_t start) const {
3838
return true;
3939
}
4040

41-
int64_t Sequence::get_first_non_decreasing_term() const {
41+
int64_t Sequence::get_first_delta_lt(const Number &d) const {
4242
for (size_t i = 1; i < size(); i++) {
43-
if (!((*this)[i - 1] < (*this)[i])) {
43+
const auto delta = Semantics::sub((*this)[i], (*this)[i - 1]);
44+
if (delta < d) {
4445
return i;
4546
}
4647
}

0 commit comments

Comments
 (0)