cuDDL
Loading...
Searching...
No Matches
register.cuh
Go to the documentation of this file.
1#pragma once
2
3#include <cuda/std/algorithm>
4#include <cuda/std/cstdint>
5#include <cuda/std/limits>
6
8
9namespace cuddl {
10
12template <uint8_t ExponentBits, uint8_t MantissaBits>
14 static_assert(ExponentBits > 0U);
15 static_assert(MantissaBits > 0U);
16 static_assert(ExponentBits + MantissaBits == 16U);
17
18 static constexpr uint8_t exponent_bits = ExponentBits;
19 static constexpr uint8_t mantissa_bits = MantissaBits;
20};
21
23
24namespace detail {
25
28constexpr uint32_t mantissa_mask = (1U << mantissa_bits) - 1U;
29
31constexpr uint32_t restore_shift_base = 63U - mantissa_bits;
32constexpr uint32_t max_nlz = restore_shift_base;
33
42template <typename Layout = default_register_layout>
43__host__ __device__ inline uint16_t score(uint64_t hash) noexcept {
44 constexpr auto layout_mantissa_mask = (1U << Layout::mantissa_bits) - 1U;
45 constexpr auto layout_restore_shift_base = 63U - Layout::mantissa_bits;
46 constexpr auto layout_max_nlz =
47 cuda::std::min(layout_restore_shift_base, (1U << Layout::exponent_bits) - 1U);
48#ifdef __CUDA_ARCH__
49 auto const nlz = static_cast<uint32_t>(__clzll(hash | 1ULL));
50#else
51 auto const nlz = static_cast<uint32_t>(__builtin_clzll(hash | 1ULL));
52#endif
53 auto const capped = nlz > layout_max_nlz ? layout_max_nlz : nlz;
54 auto const shift = layout_restore_shift_base - capped;
55 auto const mantissa = static_cast<uint32_t>((hash >> shift) & layout_mantissa_mask);
56 auto const inverted = static_cast<uint32_t>(mantissa ^ layout_mantissa_mask);
57 auto const raw = static_cast<uint16_t>((capped << Layout::mantissa_bits) | inverted);
58 return raw == 0U ? static_cast<uint16_t>(1U) : raw;
59}
60
67template <typename Layout = default_register_layout>
68__host__ __device__ constexpr uint64_t restore(uint16_t stored) noexcept {
69 constexpr auto layout_mantissa_mask = (1U << Layout::mantissa_bits) - 1U;
70 constexpr auto layout_restore_shift_base = 63U - Layout::mantissa_bits;
71 auto const nlz = static_cast<uint32_t>(stored >> Layout::mantissa_bits);
72 auto const lowbits = static_cast<uint32_t>((~stored) & layout_mantissa_mask);
73 auto const mantissa = (1U << Layout::mantissa_bits) | lowbits;
74 auto const shift = layout_restore_shift_base - nlz;
75 return static_cast<uint64_t>(mantissa) << shift;
76}
77
86template <typename Layout = default_register_layout>
87__host__ __device__ constexpr uint64_t restore_midpoint(uint16_t stored) noexcept {
88 constexpr auto layout_mantissa_mask = (1U << Layout::mantissa_bits) - 1U;
89 constexpr auto layout_restore_shift_base = 63U - Layout::mantissa_bits;
90 auto const nlz = static_cast<uint32_t>(stored >> Layout::mantissa_bits);
91 auto const lowbits = static_cast<uint32_t>((~stored) & layout_mantissa_mask);
92 auto const mantissa = (1U << Layout::mantissa_bits) | lowbits;
93 auto const shift = layout_restore_shift_base - nlz;
94 auto const lower = static_cast<uint64_t>(mantissa) << shift;
95 // `(1 << shift) >> 1` is 2^(shift-1) for ordinary tiers and exactly zero for the
96 // clamped top tier (`shift == 0`), so the midpoint needs no branch.
97 return lower + ((1ULL << shift) >> 1U);
98}
99
100} // namespace detail
101} // namespace cuddl
auto const shift
Definition kernels.cuh:442
constexpr uint32_t max_nlz
Definition register.cuh:32
constexpr uint32_t restore_shift_base
Default restoration constants retained for source compatibility.
Definition register.cuh:31
__host__ __device__ constexpr uint64_t restore_midpoint(uint16_t stored) noexcept
Reconstructs the interval midpoint for restore.
Definition register.cuh:87
__host__ __device__ constexpr uint64_t restore(uint16_t stored) noexcept
Reconstructs the approximate original hash magnitude from a stored score.
Definition register.cuh:68
constexpr uint32_t mantissa_mask
Definition register.cuh:28
__host__ __device__ uint16_t score(uint64_t hash) noexcept
Computes the 16-bit DDL rarity score for hash.
Definition register.cuh:43
constexpr uint8_t mantissa_bits
Default score layout retained for source compatibility.
Definition register.cuh:27
Batch operations over contiguous device rows.
Definition a48.hpp:14
Compile-time split of a 16-bit DDL score into exponent and mantissa fields.
Definition register.cuh:13
static constexpr uint8_t mantissa_bits
Definition register.cuh:19
static constexpr uint8_t exponent_bits
Definition register.cuh:18