Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qaudio_alignment_support_p.h
Go to the documentation of this file.
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
20QT_BEGIN_NAMESPACE
21
22namespace QtMultimediaPrivate {
23
24template <typename IntType>
25inline constexpr bool isPowerOfTwo(IntType arg)
26{
27 return arg > 0 && (arg & (arg - 1)) == 0;
28}
29
30template <typename Type>
31constexpr Type alignUp(Type arg, size_t alignment)
32{
33 if constexpr (std::is_pointer_v<Type>) {
34 return Type(alignUp(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
41template <typename Type>
42constexpr Type alignDown(Type arg, size_t alignment)
43{
44 if constexpr (std::is_pointer_v<Type>) {
45 return Type(alignDown(std::intptr_t(arg), alignment));
46 } else {
47 Q_ASSERT(isPowerOfTwo(alignment));
48 return arg & ~Type(alignment - 1);
49 }
50}
51
52template <typename IntType>
53constexpr bool isAligned(IntType arg, size_t alignment)
54{
55 if constexpr (std::is_pointer_v<IntType>) {
56 return isAligned(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
65QT_END_NAMESPACE
66
67#endif // QAUDIO_ALIGNMENT_SUPPORT_P_H
constexpr Type alignUp(Type arg, size_t alignment)
constexpr bool isPowerOfTwo(IntType arg)
constexpr bool isAligned(IntType arg, size_t alignment)
constexpr Type alignDown(Type arg, size_t alignment)