blob: 898859f3029cb8adedf840e811c692fb07b6a3f7 [file] [log] [blame]
Samuel Huang06f1ae92018-03-13 18:19:34 +00001// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef COMPONENTS_ZUCCHINI_ALGORITHM_H_
6#define COMPONENTS_ZUCCHINI_ALGORITHM_H_
7
8#include <stddef.h>
9
10#include <algorithm>
11#include <type_traits>
12#include <vector>
13
14#include "base/logging.h"
15
16// Collection of simple utilities used in for low-level computation.
17
18namespace zucchini {
19
20// Safely determines whether |[begin, begin + size)| is in |[0, bound)|. Note:
21// The special case |[bound, bound)| is not considered to be in |[0, bound)|.
22template <typename T>
23bool RangeIsBounded(T begin, T size, size_t bound) {
24 static_assert(std::is_unsigned<T>::value, "Value type must be unsigned.");
25 return begin < bound && size <= bound - begin;
26}
27
28// Safely determines whether |value| lies in |[begin, begin + size)|. Works
29// properly even if |begin + size| overflows -- although such ranges are
30// considered pathological, and should fail validation elsewhere.
31template <typename T>
32bool RangeCovers(T begin, T size, T value) {
33 static_assert(std::is_unsigned<T>::value, "Value type must be unsigned.");
34 return begin <= value && value - begin < size;
35}
36
37// Returns the integer in inclusive range |[lo, hi]| that's closest to |value|.
38// This departs from the usual usage of semi-inclusive ranges, but is useful
39// because (1) sentinels can use this, (2) a valid output always exists. It is
40// assumed that |lo <= hi|.
41template <class T>
42T InclusiveClamp(T value, T lo, T hi) {
43 static_assert(std::is_unsigned<T>::value, "Value type must be unsigned.");
44 DCHECK_LE(lo, hi);
45 return value <= lo ? lo : (value >= hi ? hi : value);
46}
47
48// Returns the minimum multiple of |m| that's no less than |x|. Assumes |m > 0|
49// and |x| is sufficiently small so that no overflow occurs.
50template <class T>
Samuel Huang607ce602018-06-13 17:47:39 +000051constexpr T AlignCeil(T x, T m) {
Samuel Huang06f1ae92018-03-13 18:19:34 +000052 static_assert(std::is_unsigned<T>::value, "Value type must be unsigned.");
53 return T((x + m - 1) / m) * m;
54}
55
Samuel Huang156a6f22019-03-15 21:08:23 +000056// Specialized alignment helpers that returns the increment to |pos| to get the
57// next n-aligned value, where n is in {2, 4}. This is useful for aligning
58// iterators relative to a base iterator using:
59// it += IncrementForAlignCeil2(it - base);
60template <class T>
61inline int IncrementForAlignCeil2(T pos) {
62 return static_cast<int>(pos & 1); // Optimized from (-pos) & 1.
63}
64
65template <class T>
66inline int IncrementForAlignCeil4(T pos) {
67 return static_cast<int>((-pos) & 3);
68}
69
Samuel Huang06f1ae92018-03-13 18:19:34 +000070// Sorts values in |container| and removes duplicates.
71template <class T>
72void SortAndUniquify(std::vector<T>* container) {
73 std::sort(container->begin(), container->end());
74 container->erase(std::unique(container->begin(), container->end()),
75 container->end());
76 container->shrink_to_fit();
77}
78
Samuel Huang607ce602018-06-13 17:47:39 +000079// Extracts a single bit at |pos| from integer |v|.
80template <int pos, typename T>
81constexpr T GetBit(T v) {
82 return (v >> pos) & 1;
83}
84
85// Extracts bits in inclusive range [|lo|, |hi|] from integer |v|, and returns
86// the sign-extend result. For example, let the (MSB-first) bits in a 32-bit int
87// |v| be:
88// xxxxxxxx xxxxxSii iiiiiiii iyyyyyyy,
89// hi^ lo^ => lo = 7, hi = 18
90// To extract "Sii iiiiiiii i", calling
91// GetSignedBits<7, 18>(v);
92// produces the sign-extended result:
93// SSSSSSSS SSSSSSSS SSSSSiii iiiiiiii.
94template <int lo, int hi, typename T>
95constexpr typename std::make_signed<T>::type GetSignedBits(T v) {
96 constexpr int kNumBits = sizeof(T) * 8;
97 using SignedType = typename std::make_signed<T>::type;
98 // Assumes 0 <= |lo| <= |hi| < |kNumBits|.
99 // How this works:
100 // (1) Shift-left by |kNumBits - 1 - hi| to clear "left" bits.
101 // (2) Shift-right by |kNumBits - 1 - hi + lo| to clear "right" bits. The
102 // input is casted to a signed type to perform sign-extension.
103 return static_cast<SignedType>(v << (kNumBits - 1 - hi)) >>
104 (kNumBits - 1 - hi + lo);
105}
106
107// Similar to GetSignedBits(), but returns the zero-extended result. For the
108// above example, calling
109// GetUnsignedBits<7, 18>(v);
110// results in:
111// 00000000 00000000 0000Siii iiiiiiii.
112template <int lo, int hi, typename T>
113constexpr typename std::make_unsigned<T>::type GetUnsignedBits(T v) {
114 constexpr int kNumBits = sizeof(T) * 8;
115 using UnsignedType = typename std::make_unsigned<T>::type;
116 return static_cast<UnsignedType>(v << (kNumBits - 1 - hi)) >>
117 (kNumBits - 1 - hi + lo);
118}
119
Samuel Huang06f1ae92018-03-13 18:19:34 +0000120// Copies bits at |pos| in |v| to all higher bits, and returns the result as the
121// same int type as |v|.
122template <typename T>
123constexpr T SignExtend(int pos, T v) {
124 int kNumBits = sizeof(T) * 8;
125 int kShift = kNumBits - 1 - pos;
126 return static_cast<typename std::make_signed<T>::type>(v << kShift) >> kShift;
127}
128
129// Optimized version where |pos| becomes a template parameter.
130template <int pos, typename T>
131constexpr T SignExtend(T v) {
132 constexpr int kNumBits = sizeof(T) * 8;
133 constexpr int kShift = kNumBits - 1 - pos;
134 return static_cast<typename std::make_signed<T>::type>(v << kShift) >> kShift;
135}
136
Samuel Huang607ce602018-06-13 17:47:39 +0000137// Determines whether |v|, if interpreted as a signed integer, is representable
138// using |digs| bits. |1 <= digs <= sizeof(T)| is assumed.
139template <int digs, typename T>
140constexpr bool SignedFit(T v) {
141 return v == SignExtend<digs - 1, T>(v);
142}
143
Samuel Huang06f1ae92018-03-13 18:19:34 +0000144} // namespace zucchini
145
146#endif // COMPONENTS_ZUCCHINI_ALGORITHM_H_