Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkAlign.h
Go to the documentation of this file.
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
12
13#include <cstddef>
14
15template <typename T> static constexpr T SkAlign2(T x) { return (x + 1) >> 1 << 1; }
16template <typename T> static constexpr T SkAlign4(T x) { return (x + 3) >> 2 << 2; }
17template <typename T> static constexpr T SkAlign8(T x) { return (x + 7) >> 3 << 3; }
18
19template <typename T> static constexpr bool SkIsAlign2(T x) { return 0 == (x & 1); }
20template <typename T> static constexpr bool SkIsAlign4(T x) { return 0 == (x & 3); }
21template <typename T> static constexpr bool SkIsAlign8(T x) { return 0 == (x & 7); }
22
23template <typename T> static constexpr T SkAlignPtr(T x) {
24 return sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x);
25}
26template <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 */
33static 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
static constexpr bool SkIsAlign2(T x)
Definition SkAlign.h:19
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition SkAlign.h:33
static constexpr bool SkIsAlign8(T x)
Definition SkAlign.h:21
static constexpr bool SkIsAlign4(T x)
Definition SkAlign.h:20
static constexpr bool SkIsAlignPtr(T x)
Definition SkAlign.h:26
static constexpr T SkAlign2(T x)
Definition SkAlign.h:15
static constexpr T SkAlign4(T x)
Definition SkAlign.h:16
static constexpr T SkAlign8(T x)
Definition SkAlign.h:17
static constexpr T SkAlignPtr(T x)
Definition SkAlign.h:23
#define SkASSERT(cond)
Definition SkAssert.h:116
double x
#define T