1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5inline UIntNative ALIGN_UP( UIntNative val, UIntNative alignment )
6{
7    // alignment must be a power of 2 for this implementation to work (need modulo otherwise)
8    ASSERT( 0 == (alignment & (alignment - 1)) );
9    UIntNative result = (val + (alignment - 1)) & ~(alignment - 1);
10    ASSERT( result >= val );      // check for overflow
11
12    return result;
13}
14
15template <typename T>
16inline T* ALIGN_UP(T* val, UIntNative alignment)
17{
18    return reinterpret_cast<T*>(ALIGN_UP(reinterpret_cast<UIntNative>(val), alignment));
19}
20
21inline UIntNative ALIGN_DOWN( UIntNative val, UIntNative alignment )
22{
23    // alignment must be a power of 2 for this implementation to work (need modulo otherwise)
24    ASSERT( 0 == (alignment & (alignment - 1)) );
25    UIntNative result = val & ~(alignment - 1);
26    return result;
27}
28
29template <typename T>
30inline T* ALIGN_DOWN(T* val, UIntNative alignment)
31{
32    return reinterpret_cast<T*>(ALIGN_DOWN(reinterpret_cast<UIntNative>(val), alignment));
33}
34
35inline bool IS_ALIGNED(UIntNative val, UIntNative alignment)
36{
37    ASSERT(0 == (alignment & (alignment - 1)));
38    return 0 == (val & (alignment - 1));
39}
40
41template <typename T>
42inline bool IS_ALIGNED(T* val, UIntNative alignment)
43{
44    ASSERT(0 == (alignment & (alignment - 1)));
45    return IS_ALIGNED(reinterpret_cast<UIntNative>(val), alignment);
46}
47
48