v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
integer-literal.h
Go to the documentation of this file.
1// Copyright 2022 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_NUMBERS_INTEGER_LITERAL_H_
6#define V8_NUMBERS_INTEGER_LITERAL_H_
7
8#include <optional>
9
10#include "src/common/globals.h"
11
12namespace v8 {
13namespace internal {
14
16 public:
21
22 template <typename T>
23 explicit IntegerLiteral(T value) : IntegerLiteral(value, true) {}
24
25 bool is_negative() const { return negative_; }
26 uint64_t absolute_value() const { return absolute_value_; }
27
28 template <typename T>
29 bool IsRepresentableAs() const {
30 static_assert(std::is_integral<T>::value, "Integral type required");
31 static_assert(sizeof(T) <= sizeof(uint64_t),
32 "Types with more than 64 bits are not supported");
33 return Compare(IntegerLiteral(std::numeric_limits<T>::min(), false)) >= 0 &&
34 Compare(IntegerLiteral(std::numeric_limits<T>::max(), false)) <= 0;
35 }
36
37 template <typename T>
38 T To() const {
39 static_assert(std::is_integral<T>::value, "Integral type required");
41 uint64_t v = absolute_value_;
42 if (negative_) v = ~v + 1;
43 return static_cast<T>(v);
44 }
45
46 template <typename T>
47 std::optional<T> TryTo() const {
48 static_assert(std::is_integral<T>::value, "Integral type required");
49 if (!IsRepresentableAs<T>()) return std::nullopt;
50 return To<T>();
51 }
52
53 int Compare(const IntegerLiteral& other) const {
54 if (absolute_value_ == other.absolute_value_) {
55 if (absolute_value_ == 0 || negative_ == other.negative_) return 0;
56 return negative_ ? -1 : 1;
57 } else if (absolute_value_ < other.absolute_value_) {
58 return other.negative_ ? 1 : -1;
59 } else {
60 return negative_ ? -1 : 1;
61 }
62 }
63
64 std::string ToString() const;
65
66 private:
67 template <typename T>
68 explicit IntegerLiteral(T value, bool perform_dcheck) : negative_(false) {
69 static_assert(std::is_integral<T>::value, "Integral type required");
70 absolute_value_ = static_cast<uint64_t>(value);
71 if (value < T(0)) {
72 negative_ = true;
73 absolute_value_ = ~absolute_value_ + 1;
74 }
75 if (perform_dcheck) DCHECK_EQ(To<T>(), value);
76 }
77
80};
81
82inline bool operator==(const IntegerLiteral& x, const IntegerLiteral& y) {
83 return x.Compare(y) == 0;
84}
85
86inline bool operator!=(const IntegerLiteral& x, const IntegerLiteral& y) {
87 return x.Compare(y) != 0;
88}
89
90inline std::ostream& operator<<(std::ostream& stream,
91 const IntegerLiteral& literal) {
92 return stream << literal.ToString();
93}
94
96 const IntegerLiteral& y) {
97 DCHECK(!x.is_negative());
98 DCHECK(!y.is_negative());
99 return IntegerLiteral(false, x.absolute_value() | y.absolute_value());
100}
101
102IntegerLiteral operator<<(const IntegerLiteral& x, const IntegerLiteral& y);
103IntegerLiteral operator+(const IntegerLiteral& x, const IntegerLiteral& y);
104
105} // namespace internal
106} // namespace v8
107#endif // V8_NUMBERS_INTEGER_LITERAL_H_
#define T
int Compare(const IntegerLiteral &other) const
IntegerLiteral(bool negative, uint64_t absolute_value)
IntegerLiteral(T value, bool perform_dcheck)
std::optional< T > TryTo() const
int x
FunctionLiteral * literal
Definition liveedit.cc:294
bool operator!=(ExternalReference lhs, ExternalReference rhs)
IntegerLiteral operator+(const IntegerLiteral &x, const IntegerLiteral &y)
std::ostream & operator<<(std::ostream &os, AtomicMemoryOrder order)
IntegerLiteral operator|(const IntegerLiteral &x, const IntegerLiteral &y)
return value
Definition map-inl.h:893
bool operator==(ExternalReference lhs, ExternalReference rhs)
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485