Add fixed point multiplication tests

This commit is contained in:
Madeline Busig 2024-08-29 22:07:08 -07:00
parent 89424a914e
commit b7decb3923
3 changed files with 111 additions and 1 deletions

View File

@ -9,7 +9,7 @@ message("Compiling MTL for ${CMAKE_SYSTEM_PROCESSOR}")
# build system to automatically re-run CMake if the glob changes, solving the
# major issue with globbing. May have a performance impact, but it should be
# negligible compared to the time spent building.
file(GLOB mtl_sources_common LIST_DIRECTORIES false CONFIGURE_DEPENDS src/*.cpp src/*.c src/*.s)
file(GLOB mtl_sources_common LIST_DIRECTORIES false CONFIGURE_DEPENDS src/*.cpp src/*.c src/*.s src/tests/*.cpp)
file(GLOB mtl_sources_armv4t LIST_DIRECTORIES false CONFIGURE_DEPENDS src/armv4t/*.cpp src/armv4t/*.c src/armv4t/*.s)
file(GLOB mtl_sources_gba LIST_DIRECTORIES false CONFIGURE_DEPENDS src/gba/*.cpp src/gba/*.c src/gba/*.s)

View File

@ -0,0 +1,41 @@
#pragma once
#include "mtl/test.hpp"
#include "mtl/fixed.hpp"
namespace mtl {
namespace test {
class fixed_suite : public suite<fixed_suite> {
public:
fixed_suite() {
add_test(&construction, "construction");
add_test(&addition, "addition");
add_test(&subtraction, "subtraction");
add_test(&multiplication, "multiplication");
add_test(&mult_overflow, "mult_overflow");
add_test(&division, "division");
}
virtual string_view name() {
return "fixed_suite";
}
static bool construction() {
return fixed(7).raw() == 7 * 64;
}
// All tests are placed in IWRAM in ARM mode.
GBA_IWRAM ARM_MODE static bool addition();
GBA_IWRAM ARM_MODE static bool subtraction();
GBA_IWRAM ARM_MODE static bool multiplication();
GBA_IWRAM ARM_MODE static bool mult_overflow();
GBA_IWRAM ARM_MODE static bool division();
};
} // namespace test
} // namespace mtl

69
src/tests/fixed.cpp Normal file
View File

@ -0,0 +1,69 @@
#include "mtl/tests/fixed.hpp"
#include "mtl/target.hpp"
#include "mtl/fixed.hpp"
TARGET_ARM_MODE
namespace mtl {
namespace test{
bool fixed_suite::addition() {
struct {
NOINLINE fixed operator()(fixed x, fixed y) {
return x + y;
}
} f;
return f(3, 4) == fixed(7);
}
bool fixed_suite::subtraction() {
struct {
NOINLINE fixed operator()(fixed x, fixed y) {
return x - y;
}
} f;
return f(8, 3) == fixed(5);
}
bool fixed_suite::multiplication() {
struct {
NOINLINE fixed operator()(fixed x, fixed y) {
return x * y;
}
} f;
return f(3, 4) == fixed(12);
}
bool fixed_suite::mult_overflow() {
// This tests fixed point multiplication where the intermediate result
// (x.raw * y.raw) overflows int32 max still works. (IE. int64 multiplication
// is used internally)
struct {
NOINLINE fixed operator()(fixed x, fixed y) {
return x * y;
}
} f;
return f(1048575, 8) == fixed(8388600);
}
bool fixed_suite::division() {
struct {
NOINLINE fixed operator()(fixed x, fixed y) {
return x / y;
}
} f;
return f(73, 13) == fixed(73.0 / 13.0);
}
}
}
TARGET_END_MODE