• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 namespace v8 {
12 namespace base {
13 
14 // Checks if value is in range [lower_limit, higher_limit] using a single
15 // branch.
16 template <typename T, typename U>
IsInRange(T value,U lower_limit,U higher_limit)17 inline constexpr bool IsInRange(T value, U lower_limit, U higher_limit) {
18   DCHECK_LE(lower_limit, higher_limit);
19   STATIC_ASSERT(sizeof(U) <= sizeof(T));
20   using unsigned_T = typename std::make_unsigned<T>::type;
21   // Use static_cast to support enum classes.
22   return static_cast<unsigned_T>(static_cast<unsigned_T>(value) -
23                                  static_cast<unsigned_T>(lower_limit)) <=
24          static_cast<unsigned_T>(static_cast<unsigned_T>(higher_limit) -
25                                  static_cast<unsigned_T>(lower_limit));
26 }
27 
28 // Checks if [index, index+length) is in range [0, max). Note that this check
29 // works even if {index+length} would wrap around.
30 template <typename T,
31           typename = typename std::enable_if<std::is_unsigned<T>::value>::type>
IsInBounds(T index,T length,T max)32 inline constexpr bool IsInBounds(T index, T length, T max) {
33   return length <= max && index <= (max - length);
34 }
35 
36 // Checks if [index, index+length) is in range [0, max). If not, {length} is
37 // clamped to its valid range. Note that this check works even if
38 // {index+length} would wrap around.
39 template <typename T>
ClampToBounds(T index,T * length,T max)40 inline bool ClampToBounds(T index, T* length, T max) {
41   if (index > max) {
42     *length = 0;
43     return false;
44   }
45   T avail = max - index;
46   bool oob = *length > avail;
47   if (oob) *length = avail;
48   return !oob;
49 }
50 
51 }  // namespace base
52 }  // namespace v8
53 
54 #endif  // V8_BASE_BOUNDS_H_
55