v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
bounds.h
Go to the documentation of this file.
1// Copyright 2019 the V8 project 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 V8_BASE_BOUNDS_H_
6#define V8_BASE_BOUNDS_H_
7
8#include "include/v8config.h"
9#include "src/base/macros.h"
10
11namespace v8 {
12namespace base {
13
14// Checks if value is in range [lower_limit, higher_limit] using a single
15// branch.
16template <typename T, typename U>
17 requires((std::is_integral_v<T> || std::is_enum_v<T>) &&
18 (std::is_integral_v<U> || std::is_enum_v<U>)) &&
19 (sizeof(U) <= sizeof(T))
20inline constexpr bool IsInRange(T value, U lower_limit, U higher_limit) {
21 DCHECK_LE(lower_limit, higher_limit);
22 using unsigned_T = typename std::make_unsigned<T>::type;
23 // Use static_cast to support enum classes.
24 return static_cast<unsigned_T>(static_cast<unsigned_T>(value) -
25 static_cast<unsigned_T>(lower_limit)) <=
26 static_cast<unsigned_T>(static_cast<unsigned_T>(higher_limit) -
27 static_cast<unsigned_T>(lower_limit));
28}
29
30// Like IsInRange but for the half-open range [lower_limit, higher_limit).
31template <typename T, typename U>
32 requires((std::is_integral_v<T> || std::is_enum_v<T>) &&
33 (std::is_integral_v<U> || std::is_enum_v<U>)) &&
34 (sizeof(U) <= sizeof(T))
35inline constexpr bool IsInHalfOpenRange(T value, U lower_limit,
36 U higher_limit) {
37 DCHECK_LE(lower_limit, higher_limit);
38 using unsigned_T = typename std::make_unsigned<T>::type;
39 // Use static_cast to support enum classes.
40 return static_cast<unsigned_T>(static_cast<unsigned_T>(value) -
41 static_cast<unsigned_T>(lower_limit)) <
42 static_cast<unsigned_T>(static_cast<unsigned_T>(higher_limit) -
43 static_cast<unsigned_T>(lower_limit));
44}
45
46// Checks if [index, index+length) is in range [0, max). Note that this check
47// works even if {index+length} would wrap around.
48template <typename T>
49inline constexpr bool IsInBounds(T index, T length, T max)
50 requires std::is_unsigned<T>::value
51{
52 return length <= max && index <= (max - length);
53}
54
55// Checks if [index, index+length) is in range [0, max). If not, {length} is
56// clamped to its valid range. Note that this check works even if
57// {index+length} would wrap around.
58template <typename T>
59inline bool ClampToBounds(T index, T* length, T max) {
60 if (index > max) {
61 *length = 0;
62 return false;
63 }
64 T avail = max - index;
65 bool oob = *length > avail;
66 if (oob) *length = avail;
67 return !oob;
68}
69
70} // namespace base
71} // namespace v8
72
73#endif // V8_BASE_BOUNDS_H_
OptionalOpIndex index
bool ClampToBounds(T index, T *length, T max)
Definition bounds.h:59
constexpr bool IsInHalfOpenRange(T value, U lower_limit, U higher_limit)
Definition bounds.h:35
constexpr bool IsInBounds(T index, T length, T max)
Definition bounds.h:49
constexpr bool IsInRange(T value, U lower_limit, U higher_limit)
Definition bounds.h:20
#define DCHECK_LE(v1, v2)
Definition logging.h:490
std::unique_ptr< ValueMirror > value