Skip to content

Commit

Permalink
lex operation: largest exponent (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckrause authored Oct 20, 2024
1 parent 94e96a3 commit e272450
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 24 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ To install or update LODA, please follow the [installation instructions](https:/

## [Unreleased]

## v24.10.20

### Features

* Added `lex` operation (largest exponent)

## v24.9.15

### Bugfixes

* Fix optimizer bug
* Fix program folding bug
* Fix optimizer bugs
* Fix program folding bugs

## v24.9.7

Expand Down
12 changes: 4 additions & 8 deletions src/cmd/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,15 +588,11 @@ void Test::programUtil() {
true);
}
p = parser.parse(ProgramUtil::getProgramPath(45));
auto h = ProgramUtil::hash(p);
size_t expected_hash = 12279585564253383018ULL;
if (h != expected_hash) {
Log::get().error("Unexpected program hash: " + std::to_string(h), true);
}
auto h1 = ProgramUtil::hash(p);
ProgramUtil::removeOps(p, Operation::Type::NOP);
h = ProgramUtil::hash(p);
if (h != expected_hash) {
Log::get().error("Unexpected program hash: " + std::to_string(h), true);
auto h2 = ProgramUtil::hash(p);
if (h2 != h1) {
Log::get().error("Unexpected program hash: " + std::to_string(h2), true);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/eval/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ Number Interpreter::calc(const Operation::Type type, const Number& target,
case Operation::Type::GCD: {
return Semantics::gcd(target, source);
}
case Operation::Type::LEX: {
return Semantics::lex(target, source);
}
case Operation::Type::BIN: {
return Semantics::bin(target, source);
}
Expand Down
21 changes: 21 additions & 0 deletions src/eval/semantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,27 @@ Number Semantics::gcd(const Number& a, const Number& b) {
return aa;
}

Number Semantics::lex(const Number& a, const Number& b) {
if (a == Number::INF || b == Number::INF) {
return Number::INF;
}
if (b == Number::ZERO || b == Number::ONE) {
return Number::ZERO;
}
auto r = Number::ZERO;
auto aa = abs(a);
auto bb = abs(b);
while (true) {
auto aaa = dif(aa, bb);
if (aaa == aa) {
break;
}
aa = aaa;
r += Number::ONE;
}
return r;
}

Number Semantics::bin(const Number& nn, const Number& kk) {
if (nn == Number::INF || kk == Number::INF) {
return Number::INF;
Expand Down
2 changes: 2 additions & 0 deletions src/eval/semantics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Semantics {

static Number gcd(const Number& a, const Number& b);

static Number lex(const Number& a, const Number& b);

static Number bin(const Number& n, const Number& k);

static Number log(const Number& a, const Number& b);
Expand Down
22 changes: 13 additions & 9 deletions src/lang/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

#include <stdexcept>

const std::array<Operation::Type, 32> Operation::Types = {
const std::array<Operation::Type, 33> Operation::Types = {
Operation::Type::NOP, Operation::Type::MOV, Operation::Type::ADD,
Operation::Type::SUB, Operation::Type::TRN, Operation::Type::MUL,
Operation::Type::DIV, Operation::Type::DIF, Operation::Type::MOD,
Operation::Type::POW, Operation::Type::GCD, Operation::Type::BIN,
Operation::Type::LOG, Operation::Type::NRT, Operation::Type::DIS,
Operation::Type::DIR, Operation::Type::EQU, Operation::Type::NEQ,
Operation::Type::LEQ, Operation::Type::GEQ, Operation::Type::MIN,
Operation::Type::MAX, Operation::Type::BAN, Operation::Type::BOR,
Operation::Type::BXO, Operation::Type::LPB, Operation::Type::LPE,
Operation::Type::CLR, Operation::Type::SRT, Operation::Type::SEQ,
Operation::Type::PRG, Operation::Type::DBG,
Operation::Type::POW, Operation::Type::GCD, Operation::Type::LEX,
Operation::Type::BIN, Operation::Type::LOG, Operation::Type::NRT,
Operation::Type::DIS, Operation::Type::DIR, Operation::Type::EQU,
Operation::Type::NEQ, Operation::Type::LEQ, Operation::Type::GEQ,
Operation::Type::MIN, Operation::Type::MAX, Operation::Type::BAN,
Operation::Type::BOR, Operation::Type::BXO, Operation::Type::LPB,
Operation::Type::LPE, Operation::Type::CLR, Operation::Type::SRT,
Operation::Type::SEQ, Operation::Type::PRG, Operation::Type::DBG,
};

const Operation::Metadata& Operation::Metadata::get(Type t) {
Expand All @@ -39,6 +39,8 @@ const Operation::Metadata& Operation::Metadata::get(Type t) {
Operation::Type::POW, "pow", 2, true, true, true};
static Operation::Metadata gcd{
Operation::Type::GCD, "gcd", 2, true, true, true};
static Operation::Metadata lex{
Operation::Type::LEX, "lex", 2, true, true, true};
static Operation::Metadata bin{
Operation::Type::BIN, "bin", 2, true, true, true};
static Operation::Metadata log{
Expand Down Expand Up @@ -104,6 +106,8 @@ const Operation::Metadata& Operation::Metadata::get(Type t) {
return pow;
case Operation::Type::GCD:
return gcd;
case Operation::Type::LEX:
return lex;
case Operation::Type::BIN:
return bin;
case Operation::Type::LOG:
Expand Down
3 changes: 2 additions & 1 deletion src/lang/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Operation {
MOD, // modulo
POW, // power
GCD, // greatest common divisor
LEX, // largest exponent
BIN, // binomial coefficient
LOG, // logarithm
NRT, // n-th root
Expand All @@ -69,7 +70,7 @@ class Operation {
__COUNT // number of operation types
};

static const std::array<Type, 32> Types;
static const std::array<Type, 33> Types;

class Metadata {
public:
Expand Down
1 change: 1 addition & 0 deletions src/mine/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ void Generator::ensureMeaningfulLoops(Program &p) {
case Operation::Type::DIF:
case Operation::Type::MOD:
case Operation::Type::GCD:
case Operation::Type::LEX:
case Operation::Type::BIN:
case Operation::Type::EQU:
case Operation::Type::NEQ:
Expand Down
14 changes: 10 additions & 4 deletions src/mine/iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ bool Iterator::inc(Operation& op) {
return true;

case Operation::Type::GCD:
op.type = Operation::Type::LEX;
return true;

case Operation::Type::LEX:
op.type = Operation::Type::BIN;
return true;

Expand Down Expand Up @@ -173,20 +177,22 @@ bool Iterator::shouldSkip(const Operation& op) {
op.type == Operation::Type::TRN || op.type == Operation::Type::MUL ||
op.type == Operation::Type::DIV || op.type == Operation::Type::DIF ||
op.type == Operation::Type::MOD || op.type == Operation::Type::GCD ||
op.type == Operation::Type::BIN || op.type == Operation::Type::EQU ||
op.type == Operation::Type::NEQ)) {
op.type == Operation::Type::LEX || op.type == Operation::Type::BIN ||
op.type == Operation::Type::EQU || op.type == Operation::Type::NEQ)) {
return true;
}
if (op.source == CONSTANT_ZERO &&
(op.type == Operation::Type::MUL || op.type == Operation::Type::DIV ||
op.type == Operation::Type::DIF || op.type == Operation::Type::MOD ||
op.type == Operation::Type::POW || op.type == Operation::Type::GCD ||
op.type == Operation::Type::BIN || op.type == Operation::Type::LPB)) {
op.type == Operation::Type::LEX || op.type == Operation::Type::BIN ||
op.type == Operation::Type::LPB)) {
return true;
}
if (op.source == CONSTANT_ONE &&
(op.type == Operation::Type::MOD || op.type == Operation::Type::POW ||
op.type == Operation::Type::GCD || op.type == Operation::Type::BIN)) {
op.type == Operation::Type::GCD || op.type == Operation::Type::LEX ||
op.type == Operation::Type::BIN)) {
return true;
}

Expand Down
59 changes: 59 additions & 0 deletions tests/semantics/lex.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Op1,Op2,Result
-4,-5,0
-4,-4,1
-4,-3,0
-4,-2,2
-4,-1,0
-4,0,0
-4,1,0
-4,2,2
-4,3,0
-4,4,1
-4,5,0
-4,6,0
-4,7,0
-4,8,0
0,-2,0
0,-1,0
0,0,0
0,1,0
0,2,0
1,-1,0
1,-1,0
1,0,0
1,1,0
1,2,0
2,-3,0
2,-2,1
2,-1,0
2,0,0
2,1,0
2,2,1
2,3,0
2,4,0
4,-5,0
4,-4,1
4,-3,0
4,-2,2
4,-1,0
4,0,0
4,1,0
4,2,2
4,3,0
4,4,1
4,5,0
9,1,0
9,2,0
9,3,2
9,4,0
9,5,0
9,9,1
9,10,0
16,2,4
16,4,2
36,2,2
36,3,2
36,4,1
36,5,0
36,6,2
36,7,0

0 comments on commit e272450

Please sign in to comment.