Kùzu C++ API
Loading...
Searching...
No Matches
int128_t.h
Go to the documentation of this file.
1// =========================================================================================
2// This int128 implementtaion got
3
4// =========================================================================================
5#pragma once
6
7#include <cstdint>
8#include <functional>
9#include <stdexcept>
10#include <string>
11
12#include "api.h"
13
14namespace kuzu {
15namespace common {
16
18
19// System representation for int128_t.
20struct int128_t {
21 uint64_t low;
22 int64_t high;
23
24 int128_t() = default;
25 int128_t(int64_t value); // NOLINT: Allow implicit conversion from numeric values
26 int128_t(int32_t value); // NOLINT: Allow implicit conversion from numeric values
27 int128_t(int16_t value); // NOLINT: Allow implicit conversion from numeric values
28 int128_t(int8_t value); // NOLINT: Allow implicit conversion from numeric values
29 int128_t(uint64_t value); // NOLINT: Allow implicit conversion from numeric values
30 int128_t(uint32_t value); // NOLINT: Allow implicit conversion from numeric values
31 int128_t(uint16_t value); // NOLINT: Allow implicit conversion from numeric values
32 int128_t(uint8_t value); // NOLINT: Allow implicit conversion from numeric values
33 int128_t(double value); // NOLINT: Allow implicit conversion from numeric values
34 int128_t(float value); // NOLINT: Allow implicit conversion from numeric values
35
36 constexpr int128_t(uint64_t low, int64_t high) : low(low), high(high) {}
37
38 constexpr int128_t(const int128_t&) = default;
39 constexpr int128_t(int128_t&&) = default;
40 int128_t& operator=(const int128_t&) = default;
42
44
45 // inplace arithmetic operators
50
51 // cast operators
52 explicit operator int64_t() const;
53 explicit operator int32_t() const;
54 explicit operator int16_t() const;
55 explicit operator int8_t() const;
56 explicit operator uint64_t() const;
57 explicit operator uint32_t() const;
58 explicit operator uint16_t() const;
59 explicit operator uint8_t() const;
60 explicit operator double() const;
61 explicit operator float() const;
62};
63
64// arithmetic operators
74KUZU_API int128_t operator<<(const int128_t& lhs, int amount);
75KUZU_API int128_t operator>>(const int128_t& lhs, int amount);
76
77// comparison operators
78KUZU_API bool operator==(const int128_t& lhs, const int128_t& rhs);
79KUZU_API bool operator!=(const int128_t& lhs, const int128_t& rhs);
80KUZU_API bool operator>(const int128_t& lhs, const int128_t& rhs);
81KUZU_API bool operator>=(const int128_t& lhs, const int128_t& rhs);
82KUZU_API bool operator<(const int128_t& lhs, const int128_t& rhs);
83KUZU_API bool operator<=(const int128_t& lhs, const int128_t& rhs);
84
85class Int128_t {
86public:
87 static std::string ToString(int128_t input);
88
89 template<class T>
90 static bool tryCast(int128_t input, T& result);
91
92 template<class T>
93 static T Cast(int128_t input) {
94 T result;
95 tryCast(input, result);
96 return result;
97 }
98
99 template<class T>
100 static bool tryCastTo(T value, int128_t& result);
101
102 template<class T>
103 static int128_t castTo(T value) {
104 int128_t result{};
105 if (!tryCastTo(value, result)) {
106 throw std::overflow_error("INT128 is out of range");
107 }
108 return result;
109 }
110
111 // negate
112 static void negateInPlace(int128_t& input) {
113 if (input.high == INT64_MIN && input.low == 0) {
114 throw std::overflow_error("INT128 is out of range: cannot negate INT128_MIN");
115 }
116 input.low = UINT64_MAX + 1 - input.low;
117 input.high = -input.high - 1 + (input.low == 0);
118 }
119
120 static int128_t negate(int128_t input) {
121 negateInPlace(input);
122 return input;
123 }
124
125 static bool tryMultiply(int128_t lhs, int128_t rhs, int128_t& result);
126
127 static int128_t Add(int128_t lhs, int128_t rhs);
128 static int128_t Sub(int128_t lhs, int128_t rhs);
129 static int128_t Mul(int128_t lhs, int128_t rhs);
130 static int128_t Div(int128_t lhs, int128_t rhs);
131 static int128_t Mod(int128_t lhs, int128_t rhs);
132 static int128_t Xor(int128_t lhs, int128_t rhs);
133 static int128_t LeftShift(int128_t lhs, int amount);
134 static int128_t RightShift(int128_t lhs, int amount);
138
139 static int128_t divMod(int128_t lhs, int128_t rhs, int128_t& remainder);
140 static int128_t divModPositive(int128_t lhs, uint64_t rhs, uint64_t& remainder);
141
142 static bool addInPlace(int128_t& lhs, int128_t rhs);
143 static bool subInPlace(int128_t& lhs, int128_t rhs);
144
145 // comparison operators
146 static bool Equals(int128_t lhs, int128_t rhs) {
147 return lhs.low == rhs.low && lhs.high == rhs.high;
148 }
149
150 static bool notEquals(int128_t lhs, int128_t rhs) {
151 return lhs.low != rhs.low || lhs.high != rhs.high;
152 }
153
154 static bool greaterThan(int128_t lhs, int128_t rhs) {
155 return (lhs.high > rhs.high) || (lhs.high == rhs.high && lhs.low > rhs.low);
156 }
157
158 static bool greaterThanOrEquals(int128_t lhs, int128_t rhs) {
159 return (lhs.high > rhs.high) || (lhs.high == rhs.high && lhs.low >= rhs.low);
160 }
161
162 static bool lessThan(int128_t lhs, int128_t rhs) {
163 return (lhs.high < rhs.high) || (lhs.high == rhs.high && lhs.low < rhs.low);
164 }
165
166 static bool lessThanOrEquals(int128_t lhs, int128_t rhs) {
167 return (lhs.high < rhs.high) || (lhs.high == rhs.high && lhs.low <= rhs.low);
168 }
169 static const int128_t powerOf10[40];
170};
171
172template<>
173bool Int128_t::tryCast(int128_t input, int8_t& result);
174template<>
175bool Int128_t::tryCast(int128_t input, int16_t& result);
176template<>
177bool Int128_t::tryCast(int128_t input, int32_t& result);
178template<>
179bool Int128_t::tryCast(int128_t input, int64_t& result);
180template<>
181bool Int128_t::tryCast(int128_t input, uint8_t& result);
182template<>
183bool Int128_t::tryCast(int128_t input, uint16_t& result);
184template<>
185bool Int128_t::tryCast(int128_t input, uint32_t& result);
186template<>
187bool Int128_t::tryCast(int128_t input, uint64_t& result);
188template<>
189bool Int128_t::tryCast(int128_t input, float& result);
190template<>
191bool Int128_t::tryCast(int128_t input, double& result);
192template<>
193bool Int128_t::tryCast(int128_t input, long double& result);
194
195template<>
196bool Int128_t::tryCastTo(int8_t value, int128_t& result);
197template<>
198bool Int128_t::tryCastTo(int16_t value, int128_t& result);
199template<>
200bool Int128_t::tryCastTo(int32_t value, int128_t& result);
201template<>
202bool Int128_t::tryCastTo(int64_t value, int128_t& result);
203template<>
204bool Int128_t::tryCastTo(uint8_t value, int128_t& result);
205template<>
206bool Int128_t::tryCastTo(uint16_t value, int128_t& result);
207template<>
208bool Int128_t::tryCastTo(uint32_t value, int128_t& result);
209template<>
210bool Int128_t::tryCastTo(uint64_t value, int128_t& result);
211template<>
213template<>
214bool Int128_t::tryCastTo(float value, int128_t& result);
215template<>
216bool Int128_t::tryCastTo(double value, int128_t& result);
217template<>
218bool Int128_t::tryCastTo(long double value, int128_t& result);
219
220// TODO: const char to int128
221
222} // namespace common
223} // namespace kuzu
224
225template<>
226struct std::hash<kuzu::common::int128_t> {
227 std::size_t operator()(const kuzu::common::int128_t& v) const noexcept;
228};
#define KUZU_API
Definition api.h:25
Definition int128_t.h:85
static int128_t Mul(int128_t lhs, int128_t rhs)
static int128_t LeftShift(int128_t lhs, int amount)
static bool tryCastTo(T value, int128_t &result)
static int128_t castTo(T value)
Definition int128_t.h:103
static T Cast(int128_t input)
Definition int128_t.h:93
static int128_t BinaryOr(int128_t lhs, int128_t rhs)
static int128_t negate(int128_t input)
Definition int128_t.h:120
static int128_t divMod(int128_t lhs, int128_t rhs, int128_t &remainder)
static int128_t BinaryAnd(int128_t lhs, int128_t rhs)
static int128_t Add(int128_t lhs, int128_t rhs)
static int128_t Sub(int128_t lhs, int128_t rhs)
static bool notEquals(int128_t lhs, int128_t rhs)
Definition int128_t.h:150
static std::string ToString(int128_t input)
static bool tryMultiply(int128_t lhs, int128_t rhs, int128_t &result)
static bool lessThanOrEquals(int128_t lhs, int128_t rhs)
Definition int128_t.h:166
static int128_t RightShift(int128_t lhs, int amount)
static int128_t Xor(int128_t lhs, int128_t rhs)
static bool addInPlace(int128_t &lhs, int128_t rhs)
static bool greaterThan(int128_t lhs, int128_t rhs)
Definition int128_t.h:154
static void negateInPlace(int128_t &input)
Definition int128_t.h:112
static bool tryCast(int128_t input, T &result)
static bool Equals(int128_t lhs, int128_t rhs)
Definition int128_t.h:146
static int128_t Mod(int128_t lhs, int128_t rhs)
static bool subInPlace(int128_t &lhs, int128_t rhs)
static int128_t divModPositive(int128_t lhs, uint64_t rhs, uint64_t &remainder)
static bool lessThan(int128_t lhs, int128_t rhs)
Definition int128_t.h:162
static int128_t Div(int128_t lhs, int128_t rhs)
static bool greaterThanOrEquals(int128_t lhs, int128_t rhs)
Definition int128_t.h:158
static const int128_t powerOf10[40]
Definition int128_t.h:169
static int128_t BinaryNot(int128_t val)
date_t operator+(int64_t i, const date_t date)
Definition date_t.h:48
KUZU_API bool operator>=(const int128_t &lhs, const int128_t &rhs)
KUZU_API int128_t operator-(const int128_t &lhs, const int128_t &rhs)
KUZU_API bool operator<=(const int128_t &lhs, const int128_t &rhs)
KUZU_API int128_t operator/(const int128_t &lhs, const int128_t &rhs)
KUZU_API int128_t operator~(const int128_t &val)
KUZU_API bool operator!=(const int128_t &lhs, const int128_t &rhs)
KUZU_API int128_t operator>>(const int128_t &lhs, int amount)
KUZU_API int128_t operator|(const int128_t &lhs, const int128_t &rhs)
KUZU_API bool operator<(const int128_t &lhs, const int128_t &rhs)
KUZU_API int128_t operator*(const int128_t &lhs, const int128_t &rhs)
KUZU_API bool operator>(const int128_t &lhs, const int128_t &rhs)
KUZU_API int128_t operator&(const int128_t &lhs, const int128_t &rhs)
KUZU_API bool operator==(const int128_t &lhs, const int128_t &rhs)
KUZU_API int128_t operator<<(const int128_t &lhs, int amount)
KUZU_API int128_t operator%(const int128_t &lhs, const int128_t &rhs)
KUZU_API int128_t operator^(const int128_t &lhs, const int128_t &rhs)
Definition array_utils.h:7
Definition int128_t.h:20
int128_t operator-() const
int128_t(int32_t value)
int128_t(uint32_t value)
int128_t & operator=(int128_t &&)=default
int128_t(double value)
int128_t & operator|=(const int128_t &rhs)
int128_t(int16_t value)
int128_t(uint8_t value)
int128_t(uint16_t value)
int128_t(int8_t value)
constexpr int128_t(uint64_t low, int64_t high)
Definition int128_t.h:36
int128_t(uint64_t value)
int128_t & operator&=(const int128_t &rhs)
uint64_t low
Definition int128_t.h:21
int128_t & operator=(const int128_t &)=default
constexpr int128_t(int128_t &&)=default
int64_t high
Definition int128_t.h:22
constexpr int128_t(const int128_t &)=default
int128_t(int64_t value)
int128_t & operator+=(const int128_t &rhs)
int128_t & operator*=(const int128_t &rhs)
std::size_t operator()(const kuzu::common::int128_t &v) const noexcept