2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2020-02-23 07:05:14 +00:00
|
|
|
#include <vapours/common.hpp>
|
|
|
|
#include <vapours/assert.hpp>
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
namespace ams::util {
|
|
|
|
|
|
|
|
/* Utilities for alignment to power of two. */
|
2019-12-31 23:23:25 +00:00
|
|
|
template<typename T>
|
|
|
|
constexpr ALWAYS_INLINE bool IsPowerOfTwo(T value) {
|
|
|
|
using U = typename std::make_unsigned<T>::type;
|
|
|
|
return (static_cast<U>(value) & static_cast<U>(value - 1)) == 0;
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2019-12-31 23:23:25 +00:00
|
|
|
constexpr ALWAYS_INLINE T AlignUp(T value, size_t alignment) {
|
2019-12-09 11:57:37 +00:00
|
|
|
using U = typename std::make_unsigned<T>::type;
|
|
|
|
const U invmask = static_cast<U>(alignment - 1);
|
|
|
|
return static_cast<T>((value + invmask) & ~invmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-12-31 23:23:25 +00:00
|
|
|
constexpr ALWAYS_INLINE T AlignDown(T value, size_t alignment) {
|
2019-12-09 11:57:37 +00:00
|
|
|
using U = typename std::make_unsigned<T>::type;
|
|
|
|
const U invmask = static_cast<U>(alignment - 1);
|
|
|
|
return static_cast<T>(value & ~invmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-12-31 23:23:25 +00:00
|
|
|
constexpr ALWAYS_INLINE bool IsAligned(T value, size_t alignment) {
|
2019-12-09 11:57:37 +00:00
|
|
|
using U = typename std::make_unsigned<T>::type;
|
|
|
|
const U invmask = static_cast<U>(alignment - 1);
|
|
|
|
return (value & invmask) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-12-31 23:23:25 +00:00
|
|
|
constexpr ALWAYS_INLINE void *AlignUp<void *>(void *value, size_t alignment) {
|
2019-12-09 11:57:37 +00:00
|
|
|
return reinterpret_cast<void *>(AlignUp(reinterpret_cast<uintptr_t>(value), alignment));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-12-31 23:23:25 +00:00
|
|
|
constexpr ALWAYS_INLINE const void *AlignUp<const void *>(const void *value, size_t alignment) {
|
2019-12-09 11:57:37 +00:00
|
|
|
return reinterpret_cast<const void *>(AlignUp(reinterpret_cast<uintptr_t>(value), alignment));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-12-31 23:23:25 +00:00
|
|
|
constexpr ALWAYS_INLINE void *AlignDown<void *>(void *value, size_t alignment) {
|
2019-12-09 11:57:37 +00:00
|
|
|
return reinterpret_cast<void *>(AlignDown(reinterpret_cast<uintptr_t>(value), alignment));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-12-31 23:23:25 +00:00
|
|
|
constexpr ALWAYS_INLINE const void *AlignDown<const void *>(const void *value, size_t alignment) {
|
2019-12-09 11:57:37 +00:00
|
|
|
return reinterpret_cast<void *>(AlignDown(reinterpret_cast<uintptr_t>(value), alignment));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-12-31 23:23:25 +00:00
|
|
|
constexpr ALWAYS_INLINE bool IsAligned<void *>(void *value, size_t alignment) {
|
2019-12-09 11:57:37 +00:00
|
|
|
return IsAligned(reinterpret_cast<uintptr_t>(value), alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-12-31 23:23:25 +00:00
|
|
|
constexpr ALWAYS_INLINE bool IsAligned<const void *>(const void *value, size_t alignment) {
|
2019-12-09 11:57:37 +00:00
|
|
|
return IsAligned(reinterpret_cast<uintptr_t>(value), alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|