diff --git a/CMakeLists.txt b/CMakeLists.txt index b35e62f..e395b7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.5) include(cmake/color.cmake) -set(BLT_VERSION 0.16.2) +set(BLT_VERSION 0.16.3) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/math/fixed_point.h b/include/blt/math/fixed_point.h index e409d0c..ee93fa0 100644 --- a/include/blt/math/fixed_point.h +++ b/include/blt/math/fixed_point.h @@ -22,6 +22,8 @@ #include #include +#define BLT_DEBUG_NO_INLINE BLT_ATTRIB_NO_INLINE + namespace blt { struct fp64 @@ -50,38 +52,106 @@ namespace blt return fp; } - BLT_ATTRIB_NO_INLINE friend fp64 operator+(fp64 left, fp64 right) + static fp64 from_f64(f64 d) + { + fp64 fp; + fp.v = static_cast(d * (1ul << 32ul)); + return fp; + } + + static fp64 from_f32(f32 f) + { + return from_f64(static_cast(f)); + } + + BLT_DEBUG_NO_INLINE friend fp64 operator+(fp64 left, fp64 right) { return fp64(left.v + right.v); } - BLT_ATTRIB_NO_INLINE friend fp64 operator-(fp64 left, fp64 right) + BLT_DEBUG_NO_INLINE friend fp64 operator-(fp64 left, fp64 right) { return fp64(left.v - right.v); } - BLT_ATTRIB_NO_INLINE friend fp64 operator*(fp64 left, fp64 right) + BLT_DEBUG_NO_INLINE friend fp64 operator*(fp64 left, fp64 right) { auto lhs = static_cast(left.v); auto rhs = static_cast(right.v); return fp64(static_cast((lhs * rhs) >> 32)); } - BLT_ATTRIB_NO_INLINE friend fp64 operator/(fp64 left, fp64 right) + BLT_DEBUG_NO_INLINE friend fp64 operator/(fp64 left, fp64 right) { - auto lhs = static_cast(left.v); + auto lhs = static_cast(left.v) << 32; auto rhs = static_cast(right.v); - return fp64(static_cast((lhs / rhs) << 32)); + + return fp64(static_cast(lhs / rhs)); } - [[nodiscard]] u64 as_u64() const + [[nodiscard]] inline u64 as_u64() const { return v >> 32; } - [[nodiscard]] i64 as_i64() const + [[nodiscard]] inline i64 as_i64() const { - return static_cast(v >> 32); + return static_cast(as_u64()); + } + + [[nodiscard]] inline u32 as_u32() const + { + return static_cast(as_u64()); + } + + [[nodiscard]] inline i32 as_i32() const + { + return static_cast(as_u64()); + } + + [[nodiscard]] inline f64 as_f64() const + { + return static_cast(v) / static_cast(1ul << 32ul); + } + + [[nodiscard]] inline f32 as_f32() const + { + return static_cast(as_f64()); + } + + inline explicit operator u64() const + { + return as_u64(); + } + + inline explicit operator i64() const + { + return as_i64(); + } + + inline explicit operator u32() const + { + return as_u32(); + } + + inline explicit operator i32() const + { + return as_i32(); + } + + inline explicit operator f32() const + { + return as_f32(); + }; + + inline explicit operator f64() const + { + return as_f64(); + } + + [[nodiscard]] u64 raw() const + { + return v; } }; } diff --git a/include/blt/std/types.h b/include/blt/std/types.h index 1bbc4a1..034b6f0 100644 --- a/include/blt/std/types.h +++ b/include/blt/std/types.h @@ -37,6 +37,8 @@ namespace blt using u64 = std::uint64_t; using size_t = std::size_t; + using f32 = float; + using f64 = double; #ifndef NO_BLT_NAMESPACE_ON_TYPES } #endif diff --git a/tests/src/math_tests.cpp b/tests/src/math_tests.cpp index b8ad067..3a78316 100644 --- a/tests/src/math_tests.cpp +++ b/tests/src/math_tests.cpp @@ -19,6 +19,7 @@ #include #include #include +#include namespace blt::test { @@ -27,8 +28,13 @@ namespace blt::test fp64 uv = fp64::from_u64(32); fp64 iv = fp64::from_i64(16); - std::cout << uv.as_i64() << " : " << uv.as_u64() << std::endl; - std::cout << iv.as_i64() << " : " << iv.as_u64() << std::endl; + fp64 fv = fp64::from_f32(53.4234234); + fp64 pi = fp64::from_f64(M_PI); + + std::cout << "[" << static_cast(uv) << "]: " << uv.as_i64() << " : " << uv.as_u64() << " : " << uv.as_f64() << std::endl; + std::cout << "[" << static_cast(iv) << "]: " << iv.as_i64() << " : " << iv.as_u64() << " : " << iv.as_f64() << std::endl; + std::cout << "[" << static_cast(fv) << "]: " << fv.as_i64() << " : " << fv.as_u64() << " : " << fv.as_f64() << std::endl; + std::cout << "[" << static_cast(pi) << "]: " << pi.as_i64() << " : " << pi.as_u64() << " : " << pi.as_f64() << std::endl; std::cout << (uv * iv).as_i64() << std::endl; std::cout << (uv * iv).as_u64() << std::endl;