summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--include/fixed_point.h20
-rw-r--r--meson.build29
-rw-r--r--src/fixed_point.cpp9
-rw-r--r--subprojects/catch2.wrap6
-rw-r--r--tests/test_fixed_point.cpp9
6 files changed, 78 insertions, 0 deletions
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 <cstdint>
+#include <type_traits>
+
+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>, "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 <catch2/catch_test_macros.hpp>
+#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);
+}