diff --git a/runtime/helpers/aligned_memory.h b/runtime/helpers/aligned_memory.h index c9b8e94099..1dbb919a35 100644 --- a/runtime/helpers/aligned_memory.h +++ b/runtime/helpers/aligned_memory.h @@ -93,9 +93,13 @@ inline size_t alignSizeWholePage(const void *ptr, size_t size) { } template -inline bool isAligned(T ptr) { - auto p = (uintptr_t)ptr; - return (p % alignment) == 0; +inline constexpr bool isAligned(T val) { + return (static_cast(val) % alignment) == 0; +} + +template +inline bool isAligned(T *ptr) { + return ((reinterpret_cast(ptr)) % alignment) == 0; } template diff --git a/unit_tests/helpers/aligned_memory_tests.cpp b/unit_tests/helpers/aligned_memory_tests.cpp index 3624bb1b41..42864114a5 100644 --- a/unit_tests/helpers/aligned_memory_tests.cpp +++ b/unit_tests/helpers/aligned_memory_tests.cpp @@ -264,3 +264,18 @@ TYPED_TEST(IsAlignedTests, aligned) { auto ptr3 = reinterpret_cast(reinterpret_cast(ptr) & ~((alignof(TypeParam) >> 1) - 1)); EXPECT_FALSE(isAligned(ptr3)); } + +TEST(IsAligned, nonPointerType) { + EXPECT_TRUE(isAligned<3>(0)); + EXPECT_FALSE(isAligned<3>(1)); + EXPECT_FALSE(isAligned<3>(2)); + EXPECT_TRUE(isAligned<3>(3)); + EXPECT_FALSE(isAligned<3>(4)); + EXPECT_FALSE(isAligned<3>(5)); + EXPECT_TRUE(isAligned<3>(6)); +} + +TEST(IsAligned, supportsConstexprEvaluation) { + static_assert(false == isAligned<3>(2), ""); + static_assert(true == isAligned<3>(3), ""); +}