Skip to content

Commit

Permalink
shaders_test: FloatEx tests
Browse files Browse the repository at this point in the history
  • Loading branch information
valdok committed Jul 10, 2024
1 parent 6a05c04 commit 820eb3d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bvm/Shaders/dummy/contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,25 @@ BEAM_EXPORT void Method_15(Dummy::TestFarCallFlags& r)
Env::SaveVar_T((uint8_t) 1, (uint8_t) 2);
}
}

template <typename TMethod>
void TestFloatOp(TMethod& r)
{
switch (r.m_Op)
{
case 0: r.m_Arg1 += r.m_Arg2; break;
case 1: r.m_Arg1 -= r.m_Arg2; break;
case 2: r.m_Arg1 *= r.m_Arg2; break;
case 3: r.m_Arg1 /= r.m_Arg2; break;
}
}

BEAM_EXPORT void Method_16(Dummy::TestFloat1& r)
{
TestFloatOp(r);
}

BEAM_EXPORT void Method_17(Dummy::TestFloat2& r)
{
TestFloatOp(r);
}
16 changes: 16 additions & 0 deletions bvm/Shaders/dummy/contract.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ namespace Dummy
uint8_t m_TryWrite;
};

struct TestFloat1
{
static const uint32_t s_iMethod = 16;
MultiPrecision::Float m_Arg1;
MultiPrecision::Float m_Arg2;
uint8_t m_Op;
};

struct TestFloat2
{
static const uint32_t s_iMethod = 17;
MultiPrecision::FloatEx m_Arg1;
MultiPrecision::FloatEx m_Arg2;
uint8_t m_Op;
};

#pragma pack (pop)

}
Binary file modified bvm/Shaders/dummy/contract.wasm
Binary file not shown.
73 changes: 73 additions & 0 deletions bvm/unittest/shaders_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ namespace Shaders {
}
template <bool bToShader> void Convert(Dummy::FindVarTest& x) {
}
template <bool bToShader> void Convert(Dummy::TestFloat1& x) {
}
template <bool bToShader> void Convert(Dummy::TestFloat2& x) {
}

template <bool bToShader> void Convert(Roulette::Params& x) {
}
Expand Down Expand Up @@ -2717,6 +2721,75 @@ namespace bvm2 {
verify_test(RunGuarded_T(cid, args.s_iMethod, args));
}

{
Shaders::Dummy::TestFloat1 args1;
ZeroObject(args1);
Shaders::Dummy::TestFloat2 args2;
ZeroObject(args2);

for (uint32_t i = 0; i < 100; i++)
{
uintBigFor<uint64_t>::Type val;
uint64_t a, b, c;
ECC::GenRandom(val);
val.Export(a);
ECC::GenRandom(val);
val.Export(b);

if (a < b)
std::swap(a, b);

args1.m_Arg1 = a;
args1.m_Arg2 = b;
args1.m_Op = 1; // subtract

verify_test(args1.m_Arg1 >= args1.m_Arg2);

verify_test(RunGuarded_T(cid, args1.s_iMethod, args1));

verify_test(args1.m_Arg1.RoundDown(c));
verify_test(c == a - b);

args1.m_Arg2 = args1.m_Arg1;
verify_test(args1.m_Arg1 == args1.m_Arg2);
verify_test(RunGuarded_T(cid, args1.s_iMethod, args1)); // must be 0

verify_test(args1.m_Arg1.IsZero());
verify_test(args1.m_Arg1.RoundDown(c));
verify_test(!c);

args2.m_Arg1 = a;
args2.m_Arg2 = b;
args2.m_Op = 1; // subtract

verify_test(args2.m_Arg1 >= args2.m_Arg2);

verify_test(RunGuarded_T(cid, args2.s_iMethod, args2));

verify_test(!args2.m_Arg1.IsNaN() && !args2.m_Arg1.IsNegative());
verify_test(args2.m_Arg1.RoundDown(c));
verify_test(c == a - b);

// check negative subtraction. Make sure result fits in int64_t (for comparison)
b >>= 1;
a >>= 1;
args2.m_Arg1 = b;
args2.m_Arg2 = a;

verify_test(args2.m_Arg1 <= args2.m_Arg2);

verify_test(RunGuarded_T(cid, args2.s_iMethod, args2));

verify_test(!args2.m_Arg1.IsNaN() && !args2.m_Arg1.IsPositive());
args2.m_Arg1.RoundDown(c);
verify_test(!c); // underflow, truncated to 0

int64_t d;
verify_test(args2.m_Arg1.RoundDown(d));
verify_test(d == static_cast<int64_t>(b - a));
}
}

verify_test(ContractDestroy_T(cid, zero));
}

Expand Down

0 comments on commit 820eb3d

Please sign in to comment.