10#ifndef EIGEN_CXX11_TENSOR_TENSOR_UINT128_H
11#define EIGEN_CXX11_TENSOR_TENSOR_UINT128_H
14#include "./InternalHeaderCheck.h"
21 static const uint64_t value = n;
22 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
operator uint64_t()
const {
return n; }
24 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static_val() {}
27 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static_val(
const T& v) {
28 EIGEN_UNUSED_VARIABLE(v);
33template <
typename HIGH = u
int64_t,
typename LOW = u
int64_t>
38 template <
typename OTHER_HIGH,
typename OTHER_LOW>
39 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE TensorUInt128(
const TensorUInt128<OTHER_HIGH, OTHER_LOW>& other)
40 : high(other.high), low(other.low) {
41 EIGEN_STATIC_ASSERT(
sizeof(OTHER_HIGH) <=
sizeof(HIGH), YOU_MADE_A_PROGRAMMING_MISTAKE);
42 EIGEN_STATIC_ASSERT(
sizeof(OTHER_LOW) <=
sizeof(LOW), YOU_MADE_A_PROGRAMMING_MISTAKE);
45 template <
typename OTHER_HIGH,
typename OTHER_LOW>
46 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE TensorUInt128& operator=(
const TensorUInt128<OTHER_HIGH, OTHER_LOW>& other) {
47 EIGEN_STATIC_ASSERT(
sizeof(OTHER_HIGH) <=
sizeof(HIGH), YOU_MADE_A_PROGRAMMING_MISTAKE);
48 EIGEN_STATIC_ASSERT(
sizeof(OTHER_LOW) <=
sizeof(LOW), YOU_MADE_A_PROGRAMMING_MISTAKE);
55 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
explicit TensorUInt128(
const T& x) : high(0), low(x) {
57 (
static_cast<std::conditional_t<sizeof(T) == 8, uint64_t, uint32_t
>>(x) <= NumTraits<uint64_t>::highest()));
61 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE TensorUInt128(HIGH y, LOW x) : high(y), low(x) {}
63 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
operator LOW()
const {
return low; }
64 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE LOW lower()
const {
return low; }
65 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE HIGH upper()
const {
return high; }
68template <
typename HL,
typename LL,
typename HR,
typename LR>
69EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
bool operator==(
const TensorUInt128<HL, LL>& lhs,
70 const TensorUInt128<HR, LR>& rhs) {
71 return (lhs.high == rhs.high) && (lhs.low == rhs.low);
74template <
typename HL,
typename LL,
typename HR,
typename LR>
75EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
bool operator!=(
const TensorUInt128<HL, LL>& lhs,
76 const TensorUInt128<HR, LR>& rhs) {
77 return (lhs.high != rhs.high) || (lhs.low != rhs.low);
80template <
typename HL,
typename LL,
typename HR,
typename LR>
81EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
bool operator>=(
const TensorUInt128<HL, LL>& lhs,
82 const TensorUInt128<HR, LR>& rhs) {
83 if (lhs.high != rhs.high) {
84 return lhs.high > rhs.high;
86 return lhs.low >= rhs.low;
89template <
typename HL,
typename LL,
typename HR,
typename LR>
90EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
bool operator<(
const TensorUInt128<HL, LL>& lhs,
91 const TensorUInt128<HR, LR>& rhs) {
92 if (lhs.high != rhs.high) {
93 return lhs.high < rhs.high;
95 return lhs.low < rhs.low;
98template <
typename HL,
typename LL,
typename HR,
typename LR>
99EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE TensorUInt128<uint64_t, uint64_t> operator+(
const TensorUInt128<HL, LL>& lhs,
100 const TensorUInt128<HR, LR>& rhs) {
101 TensorUInt128<uint64_t, uint64_t> result(lhs.high + rhs.high, lhs.low + rhs.low);
102 if (result.low < rhs.low) {
108template <
typename HL,
typename LL,
typename HR,
typename LR>
109EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE TensorUInt128<uint64_t, uint64_t> operator-(
const TensorUInt128<HL, LL>& lhs,
110 const TensorUInt128<HR, LR>& rhs) {
111 TensorUInt128<uint64_t, uint64_t> result(lhs.high - rhs.high, lhs.low - rhs.low);
112 if (result.low > lhs.low) {
118template <
typename HL,
typename LL,
typename HR,
typename LR>
119static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorUInt128<uint64_t, uint64_t> operator*(
120 const TensorUInt128<HL, LL>& lhs,
const TensorUInt128<HR, LR>& rhs) {
132 const uint64_t LOW = 0x00000000FFFFFFFFLL;
133 const uint64_t HIGH = 0xFFFFFFFF00000000LL;
135 uint64_t d = lhs.low & LOW;
136 uint64_t c = (lhs.low & HIGH) >> 32LL;
137 uint64_t b = lhs.high & LOW;
138 uint64_t a = (lhs.high & HIGH) >> 32LL;
140 uint64_t h = rhs.low & LOW;
141 uint64_t g = (rhs.low & HIGH) >> 32LL;
142 uint64_t f = rhs.high & LOW;
143 uint64_t e = (rhs.high & HIGH) >> 32LL;
146 uint64_t acc = d * h;
147 uint64_t low = acc & LOW;
151 uint64_t acc2 = acc + c * h;
159 low |= (acc << 32LL);
163 acc2 = (acc >> 32LL) | (carry << 32LL);
178 uint64_t high = acc & LOW;
181 acc2 = (acc >> 32LL) | (carry << 32LL);
187 high |= (acc2 << 32LL);
189 return TensorUInt128<uint64_t, uint64_t>(high, low);
192template <
typename HL,
typename LL,
typename HR,
typename LR>
193static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorUInt128<uint64_t, uint64_t> operator/(
194 const TensorUInt128<HL, LL>& lhs,
const TensorUInt128<HR, LR>& rhs) {
195 if (rhs == TensorUInt128<static_val<0>, static_val<1>>(1)) {
196 return TensorUInt128<uint64_t, uint64_t>(lhs.high, lhs.low);
197 }
else if (lhs < rhs) {
198 return TensorUInt128<uint64_t, uint64_t>(0);
201 TensorUInt128<uint64_t, uint64_t> power2(1);
202 TensorUInt128<uint64_t, uint64_t> d(rhs);
203 TensorUInt128<uint64_t, uint64_t> tmp(lhs - d);
207 power2 = power2 + power2;
210 tmp = TensorUInt128<uint64_t, uint64_t>(lhs.high, lhs.low);
211 TensorUInt128<uint64_t, uint64_t> result(0);
212 while (power2 != TensorUInt128<static_val<0>, static_val<0>>(0)) {
215 result = result + power2;
218 power2 = TensorUInt128<uint64_t, uint64_t>(power2.high >> 1, (power2.low >> 1) | (power2.high << 63));
219 d = TensorUInt128<uint64_t, uint64_t>(d.high >> 1, (d.low >> 1) | (d.high << 63));
Namespace containing all symbols from the Eigen library.