more fixed point numbers now with explict conversions!
parent
de5d363fe8
commit
646fd60212
|
@ -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)
|
||||
|
|
|
@ -22,6 +22,8 @@
|
|||
#include <blt/std/types.h>
|
||||
#include <blt/std/utility.h>
|
||||
|
||||
#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<u64>(d * (1ul << 32ul));
|
||||
return fp;
|
||||
}
|
||||
|
||||
static fp64 from_f32(f32 f)
|
||||
{
|
||||
return from_f64(static_cast<double>(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<unsigned __int128>(left.v);
|
||||
auto rhs = static_cast<unsigned __int128>(right.v);
|
||||
return fp64(static_cast<u64>((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<unsigned __int128>(left.v);
|
||||
auto lhs = static_cast<unsigned __int128>(left.v) << 32;
|
||||
auto rhs = static_cast<unsigned __int128>(right.v);
|
||||
return fp64(static_cast<u64>((lhs / rhs) << 32));
|
||||
|
||||
return fp64(static_cast<u64>(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<i64>(v >> 32);
|
||||
return static_cast<i64>(as_u64());
|
||||
}
|
||||
|
||||
[[nodiscard]] inline u32 as_u32() const
|
||||
{
|
||||
return static_cast<u32>(as_u64());
|
||||
}
|
||||
|
||||
[[nodiscard]] inline i32 as_i32() const
|
||||
{
|
||||
return static_cast<i32>(as_u64());
|
||||
}
|
||||
|
||||
[[nodiscard]] inline f64 as_f64() const
|
||||
{
|
||||
return static_cast<f64>(v) / static_cast<f64>(1ul << 32ul);
|
||||
}
|
||||
|
||||
[[nodiscard]] inline f32 as_f32() const
|
||||
{
|
||||
return static_cast<f32>(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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include <blt/std/logging.h>
|
||||
#include <iostream>
|
||||
#include <blt_tests.h>
|
||||
#include <cmath>
|
||||
|
||||
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<u64>(uv) << "]: " << uv.as_i64() << " : " << uv.as_u64() << " : " << uv.as_f64() << std::endl;
|
||||
std::cout << "[" << static_cast<u64>(iv) << "]: " << iv.as_i64() << " : " << iv.as_u64() << " : " << iv.as_f64() << std::endl;
|
||||
std::cout << "[" << static_cast<u64>(fv) << "]: " << fv.as_i64() << " : " << fv.as_u64() << " : " << fv.as_f64() << std::endl;
|
||||
std::cout << "[" << static_cast<u64>(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;
|
||||
|
|
Loading…
Reference in New Issue