12#include <cuda_runtime.h>
29#if FSS_ENABLE_V1_COMPAT
37template <
typename T, T mod = 0>
38 requires((std::is_unsigned_v<T> &&
sizeof(T) <= 8) ||
39 (std::is_same_v<T, __uint128_t> && mod > 0 && mod <= static_cast<__uint128_t>(UINT64_MAX)))
42template <
typename T, T mod = 0>
43 requires(std::is_unsigned_v<T> &&
sizeof(T) <= 8)
51 if constexpr (mod == 0)
return {
static_cast<T
>(val_ + rhs.val_)};
53 if (val_ >= mod - rhs.val_)
return {
static_cast<T
>(val_ + rhs.val_ - mod)};
54 else return {
static_cast<T
>(val_ + rhs.val_)};
59 if constexpr (mod == 0)
return {
static_cast<T
>(-val_)};
61 if (val_ == 0)
return {0};
62 else return {
static_cast<T
>(mod - val_)};
66 __host__ __device__
Uint() : val_(0) {}
69 __host__ __device__
static Uint From(int4 buf) {
70 assert((buf.w & 1) == 0);
73 if constexpr (
sizeof(T) < 4) value = buf.x & ((1 << 8 *
sizeof(T)) - 1);
75 else if constexpr (
sizeof(T) == 4) value =
static_cast<unsigned int>(buf.x);
76 else if constexpr (
sizeof(T) == 8)
77 value =
static_cast<T
>(
static_cast<unsigned int>(buf.x)) |
static_cast<T
>(
static_cast<unsigned int>(buf.y)) << 32;
78 else __builtin_unreachable();
80 if constexpr (mod > 0) value %= mod;
86 __host__ __device__ int4
Into()
const {
87 int4 buf = {0, 0, 0, 0};
88 if constexpr (
sizeof(T) <= 4) buf.x =
static_cast<int>(val_);
89 else if constexpr (
sizeof(T) == 8) {
90 buf.x =
static_cast<int>(val_ & 0xffffffff);
91 buf.y =
static_cast<int>(val_ >> 32);
92 }
else __builtin_unreachable();
100 __host__ __device__ T
Value()
const {
return val_; }
105 __host__ __device__
Uint(T v) : val_(v) {}
112#if FSS_ENABLE_V1_COMPAT
131template <__u
int128_t mod>
132 requires(mod > 0 && mod <= static_cast<__uint128_t>(UINT64_MAX))
133class [[deprecated(
"use fss::group::Uint128<fss::group::Uint128Modulus{.low = mod, .high = 0}> instead")]]
134 Uint<__uint128_t, mod> {
139 __host__ __device__ Uint operator+(Uint rhs)
const {
return Uint(impl_ + rhs.impl_); }
142 __host__ __device__ Uint operator-()
const {
return Uint(-impl_); }
145 __host__ __device__ Uint() : impl_() {}
148 __host__ __device__
static Uint From(int4 buf) {
return Uint(Underlying::From(buf)); }
151 __host__ __device__ int4 Into()
const {
return impl_.Into(); }
155 __host__ __device__ __uint128_t Value()
const {
return impl_.Value(); }
158 using Underlying = Uint128<Uint128Modulus{
static_cast<uint64_t
>(mod), 0}>;
162 __host__ __device__
explicit Uint(Underlying impl) : impl_(impl) {}
Unsigned integers with arithmetic addition and optional modulo as a group.
Definition uint.cuh:44
Uint operator+(Uint rhs) const
Return the group sum.
Definition uint.cuh:50
Uint operator-() const
Return the additive inverse.
Definition uint.cuh:58
int4 Into() const
Encode this group element as a clamped block.
Definition uint.cuh:86
Uint()
Construct the additive identity.
Definition uint.cuh:66
static Uint From(int4 buf)
Convert a clamped block to a group element, reducing modulo mod when set.
Definition uint.cuh:69
T Value() const
Return a copy of the underlying unsigned integer value.
Definition uint.cuh:100
Single build-time switch for the deprecated v1 Cw-form compatibility entries.
Group interface.
Definition group.cuh:49