From 86e6b2a2a35f46550439262978d52bdeb93c6dc6 Mon Sep 17 00:00:00 2001 From: batsumaru <> Date: Sun, 9 Aug 2026 10:54:32 +0900 Subject: Initial commit, fixed point addition --- .gitignore | 5 +++++ include/fixed_point.h | 20 ++++++++++++++++++++ meson.build | 29 +++++++++++++++++++++++++++++ src/fixed_point.cpp | 9 +++++++++ subprojects/catch2.wrap | 6 ++++++ tests/test_fixed_point.cpp | 9 +++++++++ 6 files changed, 78 insertions(+) create mode 100644 .gitignore create mode 100644 include/fixed_point.h create mode 100644 meson.build create mode 100644 src/fixed_point.cpp create mode 100644 subprojects/catch2.wrap create mode 100644 tests/test_fixed_point.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6634468 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/build +/.cache + +/subprojects/* +!/subprojects/*.wrap diff --git a/include/fixed_point.h b/include/fixed_point.h new file mode 100644 index 0000000..254bc91 --- /dev/null +++ b/include/fixed_point.h @@ -0,0 +1,20 @@ +#ifndef FIXED_POINT_H +#define FIXED_POINT_H + +#include +#include + +struct fp_t { + std::int32_t raw; + + static constexpr fp_t from_raw(std::int32_t r) { return fp_t{r}; } + static constexpr fp_t from_int(std::int32_t i) { return fp_t{i << 16}; } + + fp_t operator+(const fp_t& other) const; + bool operator==(const fp_t& other) const; +}; + +static_assert(std::is_trivially_copyable_v, "fp_t must remain trivially copyable for rollback determinism"); +static_assert(sizeof(fp_t) == 4, "fp_t must stay 4 bytes — check for unexpected padding"); + +#endif // !FIXED_POINT_H diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..4aa7ece --- /dev/null +++ b/meson.build @@ -0,0 +1,29 @@ +project('fg-engine', 'cpp', + default_options: ['warning_level=3', 'cpp_std=none']) + +cpp = meson.get_compiler('cpp') + +if cpp.get_id() in ['msvc', 'clang-cl'] + add_project_arguments('/std:c++latest', language: 'cpp') +else + add_project_arguments('-std=c++23', language: 'cpp') +endif + +sim_core = static_library('sim_core', + sources: files('src/fixed_point.cpp'), + include_directories: 'include', +) + +sim_core_dep = declare_dependency( + link_with: sim_core, + include_directories: 'include', +) + +catch2_dep = dependency('catch2-with-main') + +test_exe = executable('fg-tests', + sources: files('tests/test_fixed_point.cpp'), + dependencies: [sim_core_dep, catch2_dep], +) + +test('unit tests', test_exe) diff --git a/src/fixed_point.cpp b/src/fixed_point.cpp new file mode 100644 index 0000000..3e67e77 --- /dev/null +++ b/src/fixed_point.cpp @@ -0,0 +1,9 @@ +#include "fixed_point.h" + +fp_t fp_t::operator+(const fp_t& other) const { + return {raw + other.raw}; +} + +bool fp_t::operator==(const fp_t& other) const { + return raw == other.raw; +} diff --git a/subprojects/catch2.wrap b/subprojects/catch2.wrap new file mode 100644 index 0000000..ae29cd2 --- /dev/null +++ b/subprojects/catch2.wrap @@ -0,0 +1,6 @@ +[wrap-git] +url = https://github.com/catchorg/Catch2 +revision = v3.15.3 + +[provide] +catch2-with-main = catch2_with_main_dep diff --git a/tests/test_fixed_point.cpp b/tests/test_fixed_point.cpp new file mode 100644 index 0000000..8cb12d2 --- /dev/null +++ b/tests/test_fixed_point.cpp @@ -0,0 +1,9 @@ +#include +#include "fixed_point.h" + +TEST_CASE("fp_t addition is commutative") { + fp_t a = fp_t::from_int(1); + fp_t b = fp_t::from_int(2); + CHECK(a + b == b + a); + REQUIRE(a.raw != 0); +} -- cgit v1.3