• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkAlign_DEFINED
9 #define SkAlign_DEFINED
10 
11 #include "include/private/base/SkAssert.h"
12 
13 #include <cstddef>
14 
SkAlign2(T x)15 template <typename T> static constexpr T SkAlign2(T x) { return (x + 1) >> 1 << 1; }
SkAlign4(T x)16 template <typename T> static constexpr T SkAlign4(T x) { return (x + 3) >> 2 << 2; }
SkAlign8(T x)17 template <typename T> static constexpr T SkAlign8(T x) { return (x + 7) >> 3 << 3; }
18 
SkIsAlign2(T x)19 template <typename T> static constexpr bool SkIsAlign2(T x) { return 0 == (x & 1); }
SkIsAlign4(T x)20 template <typename T> static constexpr bool SkIsAlign4(T x) { return 0 == (x & 3); }
SkIsAlign8(T x)21 template <typename T> static constexpr bool SkIsAlign8(T x) { return 0 == (x & 7); }
22 
SkAlignPtr(T x)23 template <typename T> static constexpr T SkAlignPtr(T x) {
24     return sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x);
25 }
SkIsAlignPtr(T x)26 template <typename T> static constexpr bool SkIsAlignPtr(T x) {
27     return sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x);
28 }
29 
30 /**
31  *  align up to a power of 2
32  */
SkAlignTo(size_t x,size_t alignment)33 static inline constexpr size_t SkAlignTo(size_t x, size_t alignment) {
34     // The same as alignment && SkIsPow2(value), w/o a dependency cycle.
35     SkASSERT(alignment && (alignment & (alignment - 1)) == 0);
36     return (x + alignment - 1) & ~(alignment - 1);
37 }
38 
39 #endif
40