fix: Add checks to align functions

Signed-off-by: Filip Hazubski <filip.hazubski@intel.com>
This commit is contained in:
Filip Hazubski 2024-08-08 10:00:15 +00:00 committed by Compute-Runtime-Automation
parent be4570fe10
commit 54b5adb767
1 changed files with 6 additions and 1 deletions

View File

@ -1,11 +1,12 @@
/* /*
* Copyright (C) 2018-2023 Intel Corporation * Copyright (C) 2018-2024 Intel Corporation
* *
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
* *
*/ */
#pragma once #pragma once
#include "shared/source/helpers/basic_math.h"
#include "shared/source/helpers/constants.h" #include "shared/source/helpers/constants.h"
#include "shared/source/helpers/debug_helpers.h" #include "shared/source/helpers/debug_helpers.h"
#include "shared/source/utilities/logger.h" #include "shared/source/utilities/logger.h"
@ -21,6 +22,7 @@
template <typename T, typename TNoRef = typename std::remove_reference<T>::type> template <typename T, typename TNoRef = typename std::remove_reference<T>::type>
constexpr inline TNoRef alignUp(T before, size_t alignment) { constexpr inline TNoRef alignUp(T before, size_t alignment) {
UNRECOVERABLE_IF(!Math::isPow2(alignment));
TNoRef mask = static_cast<TNoRef>(alignment - 1); TNoRef mask = static_cast<TNoRef>(alignment - 1);
return (before + mask) & ~mask; return (before + mask) & ~mask;
} }
@ -41,6 +43,7 @@ constexpr inline T *alignUp(T *ptrBefore, size_t alignment) {
template <typename T, typename TNoRef = typename std::remove_reference<T>::type> template <typename T, typename TNoRef = typename std::remove_reference<T>::type>
constexpr inline TNoRef alignDown(T before, size_t alignment) { constexpr inline TNoRef alignDown(T before, size_t alignment) {
UNRECOVERABLE_IF(!Math::isPow2(alignment));
TNoRef mask = static_cast<TNoRef>(alignment - 1); TNoRef mask = static_cast<TNoRef>(alignment - 1);
return before & ~mask; return before & ~mask;
} }
@ -105,11 +108,13 @@ inline bool isAligned(T *ptr) {
template <typename T1, typename T2> template <typename T1, typename T2>
inline bool isAligned(T1 ptr, T2 alignment) { inline bool isAligned(T1 ptr, T2 alignment) {
UNRECOVERABLE_IF(!Math::isPow2(alignment));
return ((static_cast<size_t>(ptr)) & (static_cast<size_t>(alignment) - 1u)) == 0; return ((static_cast<size_t>(ptr)) & (static_cast<size_t>(alignment) - 1u)) == 0;
} }
template <typename T> template <typename T>
inline bool isAligned(T *ptr) { inline bool isAligned(T *ptr) {
// alignment requirement (returned by alignof) is always a power of 2
return (reinterpret_cast<uintptr_t>(ptr) & (alignof(T) - 1)) == 0; return (reinterpret_cast<uintptr_t>(ptr) & (alignof(T) - 1)) == 0;
} }
inline auto allocateAlignedMemory(size_t bytes, size_t alignment) { inline auto allocateAlignedMemory(size_t bytes, size_t alignment) {