mirrored from https://chromium.googlesource.com/v8/v8.git
/
bounds.h
54 lines (46 loc) · 1.72 KB
/
bounds.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_BASE_BOUNDS_H_
#define V8_BASE_BOUNDS_H_
#include "include/v8config.h"
#include "src/base/macros.h"
namespace v8 {
namespace base {
// Checks if value is in range [lower_limit, higher_limit] using a single
// branch.
template <typename T, typename U>
inline constexpr bool IsInRange(T value, U lower_limit, U higher_limit) {
#if V8_HAS_CXX14_CONSTEXPR
DCHECK_LE(lower_limit, higher_limit);
#endif
STATIC_ASSERT(sizeof(U) <= sizeof(T));
using unsigned_T = typename std::make_unsigned<T>::type;
// Use static_cast to support enum classes.
return static_cast<unsigned_T>(static_cast<unsigned_T>(value) -
static_cast<unsigned_T>(lower_limit)) <=
static_cast<unsigned_T>(static_cast<unsigned_T>(higher_limit) -
static_cast<unsigned_T>(lower_limit));
}
// Checks if [index, index+length) is in range [0, max). Note that this check
// works even if {index+length} would wrap around.
inline constexpr bool IsInBounds(size_t index, size_t length, size_t max) {
return length <= max && index <= (max - length);
}
// Checks if [index, index+length) is in range [0, max). If not, {length} is
// clamped to its valid range. Note that this check works even if
// {index+length} would wrap around.
template <typename T>
inline bool ClampToBounds(T index, T* length, T max) {
if (index > max) {
*length = 0;
return false;
}
T avail = max - index;
bool oob = *length > avail;
if (oob) *length = avail;
return !oob;
}
} // namespace base
} // namespace v8
#endif // V8_BASE_BOUNDS_H_