• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef BERBERIS_BASE_BIT_UTIL_H_
18 #define BERBERIS_BASE_BIT_UTIL_H_
19 
20 #include <cstdint>
21 #include <cstring>
22 #include <type_traits>
23 
24 #include "berberis/base/checks.h"
25 
26 namespace berberis {
27 
28 template <typename T>
IsPowerOf2(T x)29 constexpr bool IsPowerOf2(T x) {
30   static_assert(std::is_integral_v<T>, "IsPowerOf2: T must be integral");
31   DCHECK(x != 0);
32   return (x & (x - 1)) == 0;
33 }
34 
35 template <typename T>
AlignDown(T x,size_t align)36 constexpr T AlignDown(T x, size_t align) {
37   static_assert(std::is_integral_v<T>, "AlignDown: T must be integral");
38   DCHECK(IsPowerOf2(align));
39   return x & ~(align - 1);
40 }
41 
42 template <typename T>
AlignUp(T x,size_t align)43 constexpr T AlignUp(T x, size_t align) {
44   return AlignDown(x + align - 1, align);
45 }
46 
47 template <typename T>
IsAligned(T x,size_t align)48 constexpr bool IsAligned(T x, size_t align) {
49   return AlignDown(x, align) == x;
50 }
51 
52 // Helper to align pointers.
53 template <typename T>
AlignDown(T * p,size_t align)54 constexpr T* AlignDown(T* p, size_t align) {
55   return reinterpret_cast<T*>(AlignDown(reinterpret_cast<uintptr_t>(p), align));
56 }
57 
58 // Helper to align pointers.
59 template <typename T>
AlignUp(T * p,size_t align)60 constexpr T* AlignUp(T* p, size_t align) {
61   return reinterpret_cast<T*>(AlignUp(reinterpret_cast<uintptr_t>(p), align));
62 }
63 
64 // Helper to align pointers.
65 template <typename T>
IsAligned(T * p,size_t align)66 constexpr bool IsAligned(T* p, size_t align) {
67   return IsAligned(reinterpret_cast<uintptr_t>(p), align);
68 }
69 
70 template <typename T>
BitUtilLog2(T x)71 constexpr T BitUtilLog2(T x) {
72   static_assert(std::is_integral_v<T>, "Log2: T must be integral");
73   DCHECK(IsPowerOf2(x));
74   return x == 1 ? 0 : BitUtilLog2(x >> 1) + 1;
75 }
76 
77 // Verify that argument value fits into a target.
78 template <typename ResultType, typename ArgumentType>
IsInRange(ArgumentType x)79 inline bool IsInRange(ArgumentType x) {
80   // Note: conversion from wider integer type into narrow integer type is always
81   // defined.  Conversion to unsigned produces well-defined result while conversion
82   // to signed type produces implementation-defined result but in both cases value
83   // is guaranteed to be unchanged if it can be represented in the destination type
84   // and is *some* valid value if it's unrepesentable.
85   //
86   // Quote from the standard (including "note" in the standard):
87   //   If the destination type is unsigned, the resulting value is the least unsigned
88   // integer congruent to the source integer (modulo 2ⁿ where n is the number of bits
89   // used to represent the unsigned type). [ Note: In a two’s complement representation,
90   // this conversion is conceptual and there is no change in the bit pattern (if there
91   // is no truncation). — end note ]
92   //   If the destination type is signed, the value is unchanged if it can be represented
93   // in the destination type; otherwise, the value is implementation-defined.
94 
95   return static_cast<ResultType>(x) == x;
96 }
97 
98 // bit_cast<Dest, Source> is a well-defined equivalent of address-casting:
99 //   *reinterpret_cast<Dest*>(&source)
100 // See chromium base/macros.h for details.
101 template <class Dest, class Source>
bit_cast(const Source & source)102 inline Dest bit_cast(const Source& source) {
103   static_assert(sizeof(Dest) == sizeof(Source),
104                 "bit_cast: source and destination must be of same size");
105   static_assert(std::is_trivially_copyable_v<Dest>,
106                 "bit_cast: destination must be trivially copyable");
107   static_assert(std::is_trivially_copyable_v<Source>,
108                 "bit_cast: source must be trivially copyable");
109   Dest dest;
110   memcpy(&dest, &source, sizeof(dest));
111   return dest;
112 }
113 
114 }  // namespace berberis
115 
116 #endif  // BERBERIS_BASE_BIT_UTIL_H_
117