| 1 | // Copyright (C) 2025 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QAUDIO_ALIGNMENT_SUPPORT_P_H |
| 5 | #define QAUDIO_ALIGNMENT_SUPPORT_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtCore/qglobal.h> |
| 19 | |
| 20 | QT_BEGIN_NAMESPACE |
| 21 | |
| 22 | namespace QtMultimediaPrivate { |
| 23 | |
| 24 | template <typename IntType> |
| 25 | inline constexpr bool isPowerOfTwo(IntType arg) |
| 26 | { |
| 27 | return arg > 0 && (arg & (arg - 1)) == 0; |
| 28 | } |
| 29 | |
| 30 | template <typename Type> |
| 31 | constexpr Type alignUp(Type arg, size_t alignment) |
| 32 | { |
| 33 | if constexpr (std::is_pointer_v<Type>) { |
| 34 | return Type(alignUp(arg: std::intptr_t(arg), alignment)); |
| 35 | } else { |
| 36 | Q_ASSERT(isPowerOfTwo(alignment)); |
| 37 | return Type((arg + (Type(alignment) - 1)) & ~Type(alignment - 1)); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | template <typename Type> |
| 42 | constexpr Type alignDown(Type arg, size_t alignment) |
| 43 | { |
| 44 | if constexpr (std::is_pointer_v<Type>) { |
| 45 | return Type(alignDown(arg: std::intptr_t(arg), alignment)); |
| 46 | } else { |
| 47 | Q_ASSERT(isPowerOfTwo(alignment)); |
| 48 | return arg & ~Type(alignment - 1); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | template <typename IntType> |
| 53 | constexpr bool isAligned(IntType arg, size_t alignment) |
| 54 | { |
| 55 | if constexpr (std::is_pointer_v<IntType>) { |
| 56 | return isAligned(arg: std::intptr_t(arg), alignment); |
| 57 | } else { |
| 58 | Q_ASSERT(isPowerOfTwo(alignment)); |
| 59 | return (arg & (IntType(alignment) - 1)) == 0; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | } // namespace QtMultimediaPrivate |
| 64 | |
| 65 | QT_END_NAMESPACE |
| 66 | |
| 67 | #endif // QAUDIO_ALIGNMENT_SUPPORT_P_H |
| 68 | |