Skip to content

Commit

Permalink
v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marzer committed Aug 3, 2023
1 parent ad85977 commit a1e8c99
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 33 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Changelog

## v0.4.0

- made all macros overridable
- minor refactors

## v0.3.0

- fixed incorrect `__builtin_assume_aligned` check

<br>

## v0.2.0

- removed `constexpr` where it would be non-portable
- made use of extra bits (e.g. on AMD64) opt-in via `MZ_TAGGED_PTR_BITS`
- made use of `assert()` configurable via `MZ_ASSERT()`

<br>

## v0.1.0

- Initial release 🎉&#xFE0F;
149 changes: 121 additions & 28 deletions include/mz/tagged_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define MZ_TAGGED_PTR_HPP

#define MZ_TAGGED_PTR_VERSION_MAJOR 0
#define MZ_TAGGED_PTR_VERSION_MINOR 3
#define MZ_TAGGED_PTR_VERSION_MINOR 4
#define MZ_TAGGED_PTR_VERSION_PATCH 0

#ifndef MZ_CPP
Expand Down Expand Up @@ -281,26 +281,47 @@
#endif
#endif

// clang-format off
#ifndef MZ_PURE_GETTER
#define MZ_INLINE_GETTER MZ_NODISCARD MZ_ALWAYS_INLINE
#ifndef MZ_PURE
#ifdef NDEBUG
#define MZ_PURE MZ_DECLSPEC(noalias) MZ_ATTR(pure)
#define MZ_CONST MZ_DECLSPEC(noalias) MZ_ATTR(const)
#define MZ_PURE_GETTER MZ_NODISCARD MZ_PURE
#define MZ_CONST_GETTER MZ_NODISCARD MZ_CONST
#define MZ_PURE_INLINE_GETTER MZ_NODISCARD MZ_ALWAYS_INLINE MZ_PURE
#define MZ_CONST_INLINE_GETTER MZ_NODISCARD MZ_ALWAYS_INLINE MZ_CONST
#define MZ_PURE MZ_DECLSPEC(noalias) MZ_ATTR(pure)
#else
#define MZ_PURE
#endif
#endif
#ifndef MZ_CONST
#ifdef NDEBUG
#define MZ_CONST MZ_DECLSPEC(noalias) MZ_ATTR(const)
#else
#define MZ_CONST
#define MZ_PURE_GETTER MZ_NODISCARD
#define MZ_CONST_GETTER MZ_NODISCARD
#define MZ_PURE_INLINE_GETTER MZ_NODISCARD MZ_ALWAYS_INLINE
#define MZ_CONST_INLINE_GETTER MZ_NODISCARD MZ_ALWAYS_INLINE
#endif
#endif
// clang-format on
#ifndef MZ_INLINE_GETTER
#define MZ_INLINE_GETTER \
MZ_NODISCARD \
MZ_ALWAYS_INLINE
#endif
#ifndef MZ_PURE_GETTER
#define MZ_PURE_GETTER \
MZ_NODISCARD \
MZ_PURE
#endif
#ifndef MZ_PURE_INLINE_GETTER
#define MZ_PURE_INLINE_GETTER \
MZ_NODISCARD \
MZ_ALWAYS_INLINE \
MZ_PURE
#endif
#ifndef MZ_CONST_GETTER
#define MZ_CONST_GETTER \
MZ_NODISCARD \
MZ_CONST
#endif
#ifndef MZ_CONST_INLINE_GETTER
#define MZ_CONST_INLINE_GETTER \
MZ_NODISCARD \
MZ_ALWAYS_INLINE \
MZ_CONST
#endif

#ifndef MZ_TRIVIAL_ABI
#if MZ_CLANG || MZ_HAS_ATTR(__trivial_abi__)
Expand Down Expand Up @@ -351,8 +372,10 @@

#ifndef MZ_ENABLE_IF
#if !MZ_DOXYGEN
#define MZ_ENABLE_IF(...) , typename std::enable_if<!!(__VA_ARGS__), int>::type = 0
#define MZ_ENABLE_IF_T(T, ...) std::enable_if_t<!!(__VA_ARGS__), T>
#define MZ_ENABLE_IF(...) , MZ_ENABLE_IF_T(int, __VA_ARGS__) = 0
#else
#define MZ_ENABLE_IF_T(T, ...)
#define MZ_ENABLE_IF(...)
#endif
#endif
Expand All @@ -375,6 +398,24 @@
#endif
#endif

#ifndef MZ_HAS_IF_CONSTEVAL
#if defined(__cpp_if_consteval) && __cpp_if_consteval >= 202106
#define MZ_HAS_IF_CONSTEVAL 1
#else
#define MZ_HAS_IF_CONSTEVAL 0
#endif
#endif

#ifndef MZ_IF_CONSTEVAL
#if MZ_HAS_IF_CONSTEVAL
#define MZ_IF_CONSTEVAL if consteval
#define MZ_IF_RUNTIME if !consteval
#else
#define MZ_IF_CONSTEVAL if (::mz::is_constant_evaluated())
#define MZ_IF_RUNTIME if (!::mz::is_constant_evaluated())
#endif
#endif

#include <climits> // CHAR_BIT
#include <cstddef> // size_t
#include <cstring> // std::memcpy
Expand Down Expand Up @@ -446,6 +487,7 @@ namespace mz
using type = std::add_rvalue_reference_t<typename remove_enum_<T>::type>;
};
}

template <typename T>
using remove_enum = typename detail::remove_enum_<T>::type;

Expand Down Expand Up @@ -631,6 +673,45 @@ namespace mz

#endif // MZ_HAS_SNIPPET_HAS_SINGLE_BIT

#ifndef MZ_HAS_SNIPPET_IS_CONSTANT_EVALUATED
#define MZ_HAS_SNIPPET_IS_CONSTANT_EVALUATED

MZ_CONST_INLINE_GETTER
constexpr bool is_constant_evaluated() noexcept
{
#if MZ_HAS_IF_CONSTEVAL

if consteval
{
return true;
}
else
{
return false;
}

#elif MZ_CLANG >= 9 || MZ_GCC >= 9 || MZ_MSVC >= 1925 || MZ_HAS_BUILTIN(is_constant_evaluated)

return __builtin_is_constant_evaluated();

#elif defined(__cpp_lib_is_constant_evaluated) && __cpp_lib_is_constant_evaluated >= 201811

return std::is_constant_evaluated();

#else

return false;

#endif
}

namespace build
{
inline constexpr bool supports_is_constant_evaluated = is_constant_evaluated();
}

#endif // MZ_HAS_SNIPPET_IS_CONSTANT_EVALUATED

#ifndef MZ_HAS_SNIPPET_ASSUME_ALIGNED
#define MZ_HAS_SNIPPET_ASSUME_ALIGNED

Expand All @@ -642,35 +723,46 @@ namespace mz
static_assert(N > 0 && (N & (N - 1u)) == 0u, "assume_aligned() requires a power-of-two alignment value.");
static_assert(!std::is_function_v<T>, "assume_aligned may not be used on functions.");

MZ_ASSUME((reinterpret_cast<uintptr_t>(ptr) & (N - uintptr_t{ 1 })) == 0);

if constexpr (std::is_volatile_v<T>)
MZ_IF_CONSTEVAL
{
return static_cast<T*>(mz::assume_aligned<N>(const_cast<std::remove_volatile_t<T>*>(ptr)));
return ptr;
}
else
{
MZ_ASSUME((reinterpret_cast<uintptr_t>(ptr) & (N - uintptr_t{ 1 })) == 0);

if constexpr (std::is_volatile_v<T>)
{
return static_cast<T*>(mz::assume_aligned<N>(const_cast<std::remove_volatile_t<T>*>(ptr)));
}
else
{
#if MZ_CLANG || MZ_GCC || MZ_HAS_BUILTIN(__builtin_assume_aligned)

return static_cast<T*>(__builtin_assume_aligned(ptr, N));
return static_cast<T*>(__builtin_assume_aligned(ptr, N));

#elif MZ_MSVC

if constexpr (N < 16384)
return static_cast<T*>(__builtin_assume_aligned(ptr, N));
else
return ptr;
if constexpr (N < 16384)
return static_cast<T*>(__builtin_assume_aligned(ptr, N));
else
return ptr;

#elif MZ_ICC

__assume_aligned(ptr, N);
return ptr;
__assume_aligned(ptr, N);
return ptr;

#elif defined(__cpp_lib_assume_aligned)

return std::assume_aligned<N>(ptr);

#else

return ptr;
return ptr;

#endif
}
}
}

Expand Down Expand Up @@ -1238,6 +1330,7 @@ namespace mz

#else
#endif

MZ_CONST_INLINE_GETTER
static bool can_store_ptr(pointer value) noexcept
{
Expand Down
6 changes: 5 additions & 1 deletion include/mz/tagged_ptr.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define {% macros::prefix %}TAGGED_PTR_HPP

#define {% macros::prefix %}TAGGED_PTR_VERSION_MAJOR 0
#define {% macros::prefix %}TAGGED_PTR_VERSION_MINOR 3
#define {% macros::prefix %}TAGGED_PTR_VERSION_MINOR 4
#define {% macros::prefix %}TAGGED_PTR_VERSION_PATCH 0

//% preprocessor::cpp
Expand All @@ -31,6 +31,9 @@
//% preprocessor::hidden
//% preprocessor::sfinae

//% preprocessor::has_if_consteval
//% preprocessor::if_consteval

#include <climits> // CHAR_BIT
#include <cstddef> // size_t
#include <cstring> // std::memcpy
Expand Down Expand Up @@ -72,6 +75,7 @@ namespace {% namespaces::main %}
//% bit_floor guarded
//% bit_fill_right guarded
//% has_single_bit guarded
//% is_constant_evaluated guarded
//% assume_aligned guarded
}

Expand Down

0 comments on commit a1e8c99

Please sign in to comment.