Skip to content

Commit

Permalink
add math::limits
Browse files Browse the repository at this point in the history
continue i12_ and u128 integrations
  • Loading branch information
HUD-Software committed Jun 18, 2024
1 parent 2b09fae commit 4e524d9
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 343 deletions.
203 changes: 39 additions & 164 deletions interface/core/i128.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#else
#include "i128/i128_portable.h"
#endif
#include "math.h"

namespace hud
{
Expand All @@ -21,83 +22,21 @@ namespace hud
class alignas(16) i128
: protected details::i128::i128_impl
{

private:
friend u128;
using super = details::i128::i128_impl;

public:
/** Default constructor. */
constexpr i128() = default;

/** Construct a i128 from low and high part. */
constexpr i128(i64 high, u64 low) noexcept
: super(low, high)
{
}

/** Constructor i128 from i32. */
constexpr i128(i32 value) noexcept
: super(value)
{
}

/** Constructor i128 from u32. */
constexpr i128(u32 value) noexcept
: super(value)
{
}

/** Constructor i128 from i64. */
constexpr i128(i64 value) noexcept
: super(value)
{
}
using super::super;

/** Constructor i128 from u64. */
constexpr i128(u64 value) noexcept
: super(value)
{
}

/** Constructor i128 from u128. */
/** Construct a i128 from u128. */
explicit constexpr i128(u128 value) noexcept;

/** Construct a i128 from f32. */
i128(f32 value) noexcept
: super(value)
{
}

/** Construct a i128 from f64. */
i128(f64 value) noexcept
: super(value)
{
}

#if HD_INTRINSIC_INT128_SUPPORTED
/** Construct a i128 from __int128. */
i128(__int128 value) noexcept
: super(value)
{
}

/** Construct a i128 from __int128. */
i128(unsigned __int128 value) noexcept
: super(value)
{
}
#endif

/** Retrieves the low part of the i128. */
[[nodiscard]] constexpr u64 low() const noexcept
{
return super::low();
}

/** Retrieves the high part of the i128. */
[[nodiscard]] constexpr i64 high() const noexcept
{
return super::high();
}
using super::high;
/** Retrieves the low part of the i128. */
using super::low;

/** Cast to bool. */
[[nodiscard]] constexpr explicit operator bool() const noexcept
Expand Down Expand Up @@ -225,92 +164,25 @@ namespace hud
return !(left == right);
}

static inline constexpr i128 i128_max = i128 {i64_max, u64_max};
static inline constexpr i128 i128_min = i128 {i64_min, 0u};

class alignas(16) u128
: protected details::i128::u128_impl
{
private:
friend i128;
using super = details::i128::u128_impl;

public:
/** Default constructor. */
constexpr u128() = default;
using super::super;

/** Construct a u128 from low and high part. */
constexpr u128(u64 high, u64 low) noexcept
: super(low, high)
{
}

/** Constructor u128 from i32. */
constexpr u128(i32 value) noexcept
: super(value)
{
}

/** Constructor u128 from u32. */
constexpr u128(u32 value) noexcept
: super(value)
{
}

/** Constructor u128 from i64. */
constexpr u128(i64 value) noexcept
: super(value)
{
}

/** Constructor u128 from u64. */
constexpr u128(u64 value) noexcept
: super(value)
{
}

/** Constructor u128 from u128. */
explicit constexpr u128(i128 value) noexcept
: super(value)
{
}

/** Construct a u128 from f32. */
u128(f32 value) noexcept
: super(value)
{
}

/** Construct a u128 from f64. */
u128(f64 value) noexcept
: super(value)
{
}

#if HD_INTRINSIC_INT128_SUPPORTED
/** Construct a i128 from __int128. */
u128(__int128 value) noexcept
: super(value)
: super(static_cast<super>(value))
{
}

/** Construct a i128 from __int128. */
u128(unsigned __int128 value) noexcept
: super(value)
{
}
#endif

/** Retrieves the low part of the u128. */
[[nodiscard]] constexpr u64 low() const noexcept
{
return super::low();
}

/** Retrieves the high part of the u128. */
[[nodiscard]] constexpr u64 high() const noexcept
{
return super::high();
}
/** Retrieves the high part of the i128. */
using super::high;
/** Retrieves the low part of the i128. */
using super::low;

/** Cast to bool. */
[[nodiscard]] constexpr explicit operator bool() const noexcept
Expand Down Expand Up @@ -439,25 +311,13 @@ namespace hud
return !(left == right);
}

static inline constexpr u128 u128_max = u128 {u64_max, u64_max};
static inline constexpr u128 u128_min = u128 {u64_min, 0u};

#if HD_INTRINSIC_INT128_SUPPORTED
namespace details::i128
/** Construct a i128 from u128. */
constexpr i128::i128(u128 value) noexcept
: super(static_cast<super>(value))
{
/** Construct a i128 from i128. */
constexpr i128_intrinsics::i128_intrinsics(hud::u128 value) noexcept
: value_(static_cast<__int128>(value))
{
}

constexpr u128_intrinsics::u128_intrinsics(hud::i128 value) noexcept
: value_(static_cast<unsigned __int128>(value))
{
}
}

} // namespace details::i128
#else
#if !HD_INTRINSIC_INT128_SUPPORTED
namespace details::i128
{
/** Construct a i128 from u128. */
Expand Down Expand Up @@ -486,13 +346,28 @@ namespace hud
{
}
} // namespace details::i128

#endif

constexpr i128::i128(u128 value) noexcept
: super(value)
static inline constexpr i128 i128_max = i128 {i64_max, u64_max};
static inline constexpr i128 i128_min = i128 {i64_min, 0u};

static inline constexpr u128 u128_max = u128 {u64_max, u64_max};
static inline constexpr u128 u128_min = u128 {u64_min, 0u};

namespace math
{
}
template<> struct limits<i128>
{
static constexpr i128 min {hud::i128_min};
static constexpr i128 max {hud::i128_max};
};

template<> struct limits<u128>
{
static constexpr u128 min {hud::u128_min};
static constexpr u128 max {hud::u128_max};
};
} // namespace math

} // namespace hud

Expand Down
19 changes: 15 additions & 4 deletions interface/core/i128/i128_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

namespace hud
{
class i128;
class u128;
// class i128;
// class u128;

namespace details::i128
{
struct u128_intrinsics;

/**
* This function converts an unsigned 128-bit integer to a signed 128-bit integer
* while preserving the underlying binary representation. If the 128th bit (bit 127)
Expand Down Expand Up @@ -90,7 +92,7 @@ namespace hud
}

/** Construct a i128 from i128. */
explicit constexpr i128_intrinsics(hud::u128 value) noexcept;
explicit constexpr i128_intrinsics(u128_intrinsics value) noexcept;

/** Construct a i128 from f32. */
i128_intrinsics(f32 value) noexcept
Expand Down Expand Up @@ -271,7 +273,10 @@ namespace hud
}

/** Construct a i128 from i128. */
explicit constexpr u128_intrinsics(hud::i128 value) noexcept;
explicit constexpr u128_intrinsics(i128_intrinsics value) noexcept
: value_(static_cast<unsigned __int128>(value))
{
}

/** Construct a i128 from f32. */
constexpr u128_intrinsics(f32 value) noexcept
Expand Down Expand Up @@ -403,6 +408,12 @@ namespace hud
};

using u128_impl = u128_intrinsics;

constexpr i128_intrinsics::i128_intrinsics(u128_intrinsics value) noexcept
: value_(static_cast<__int128>(value))
{
}

} // namespace details::i128
} // namespace hud

Expand Down
27 changes: 24 additions & 3 deletions interface/core/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,29 @@

namespace hud
{

struct Math
namespace math
{
/**
* Defines limits of types
* @tparam type_t The type to define
*/
template<typename type_t>
struct limits;
#define hud_limits_for(type_t) \
template<> struct limits<type_t> \
{ \
static constexpr type_t min {hud::type_t##_min}; \
static constexpr type_t max {hud::type_t##_max}; \
};
hud_limits_for(i8);
hud_limits_for(u8);
hud_limits_for(i16);
hud_limits_for(u16);
hud_limits_for(i32);
hud_limits_for(u32);
hud_limits_for(i64);
hud_limits_for(u64);

/**
* Check wheter the given number is a power of two or not
* Requires type_t to be an integral type
Expand All @@ -17,6 +37,7 @@ namespace hud
template<typename type_t>
requires(is_integral_v<type_t>)
[[nodiscard]] static constexpr bool is_power_of_two(const type_t value) noexcept

{
return value && !(value & (value - 1));
}
Expand All @@ -33,7 +54,7 @@ namespace hud
{
return value >= min && value <= max;
}
};
}; // namespace math

} // namespace hud
#endif // HD_INC_CORE_MATH_H
5 changes: 3 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ set( src
tuple/tuple_swap.cpp
tuple/tuple_element.cpp
tuple/tuple_size.cpp
u128/u128_cast.cpp
u128/u128_constructors.cpp
u128/u128_operations.cpp
unique_pointer/array/unique_pointer_array_assignments.cpp
unique_pointer/array/unique_pointer_array_comparison.cpp
unique_pointer/array/unique_pointer_array_constructors.cpp
Expand Down Expand Up @@ -288,12 +291,10 @@ set( src
compilation.cpp
cstring.cpp
debugger.cpp
i128.cpp
main.cpp
precompiled.h
slice.cpp
types.cpp
u128.cpp
uuid.cpp
)

Expand Down
Loading

0 comments on commit 4e524d9

Please sign in to comment.