Annotate noexcept and constexpr with HEDLEY, and add interval containment, ChaCha, and the dyadic range tables.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Ryan Henry 2026-09-24 20:44:07 -06:00
parent 875f09fec1
commit 0d8a5a8131
97 changed files with 9212 additions and 1159 deletions

View file

@ -10,6 +10,8 @@
#ifndef LIBDPF_INCLUDE_GROTTO_CONSTANT_LUT_HPP__
#define LIBDPF_INCLUDE_GROTTO_CONSTANT_LUT_HPP__
#include "hedley/hedley.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
@ -57,11 +59,13 @@ struct constant_lut
/// `bounds[i + 1]` or one past `numeric_limits<Raw>::max()` for the last piece.
std::vector<std::int64_t> values;
HEDLEY_NO_THROW
std::size_t linear_parts() const noexcept { return values.size(); }
/// Pieces after joining the first and last when they carry the same value.
/// Those two meet across the signed wrap, which is how the paper counts
/// parts for `zero` and `nonzero` (2, not 3).
HEDLEY_NO_THROW
std::size_t wrapped_parts() const noexcept
{
if (values.size() >= 2 && values.front() == values.back())
@ -69,6 +73,7 @@ struct constant_lut
return values.size();
}
HEDLEY_NO_THROW
std::int64_t operator()(Raw x) const noexcept
{
const auto it = std::upper_bound(bounds.begin(), bounds.end(), x);
@ -86,6 +91,7 @@ namespace detail
using u128 = unsigned __int128;
template <typename Raw>
HEDLEY_NO_THROW
constexpr u128 magnitude(Raw raw) noexcept
{
if (raw >= 0)
@ -95,6 +101,7 @@ constexpr u128 magnitude(Raw raw) noexcept
return static_cast<u128>(-static_cast<__int128>(raw));
}
HEDLEY_NO_THROW
constexpr int floor_log2(u128 mag) noexcept
{
if (mag <= std::uint64_t(-1))
@ -102,6 +109,7 @@ constexpr int floor_log2(u128 mag) noexcept
return 127 - __builtin_clzll(static_cast<std::uint64_t>(mag >> 64));
}
HEDLEY_NO_THROW
constexpr bool shift_fits(u128 value, unsigned shift) noexcept
{
return shift < 128 && value <= (~u128{0} >> shift);
@ -131,6 +139,7 @@ inline constexpr std::uint64_t pow10[] = {
10000000000000000000ull,
};
HEDLEY_NO_THROW
inline bool magnitude_ge_pow10(u128 mag, int k, unsigned fractional_bits) noexcept
{
if (mag == 0 || fractional_bits >= 128)
@ -151,6 +160,7 @@ inline bool magnitude_ge_pow10(u128 mag, int k, unsigned fractional_bits) noexce
}
/// Smallest positive magnitude whose base-10 log is at least `k`.
HEDLEY_NO_THROW
inline u128 first_magnitude_at_least_pow10(int k, unsigned fractional_bits) noexcept
{
if (fractional_bits >= 128)
@ -253,6 +263,7 @@ struct sign_program
static constexpr std::int64_t canonical[] = { Neg, Zero, Pos };
template <typename Raw>
HEDLEY_NO_THROW
static std::int64_t eval(Raw raw, unsigned) noexcept
{
if (raw < 0)
@ -310,6 +321,7 @@ template <>
struct exact_lut<exact_constant::ilogb>
{
template <typename Raw>
HEDLEY_NO_THROW
static std::int64_t eval(Raw raw, unsigned fractional_bits) noexcept
{
const detail::u128 mag = detail::magnitude(raw);
@ -341,6 +353,7 @@ template <>
struct exact_lut<exact_constant::ceil_ilogb>
{
template <typename Raw>
HEDLEY_NO_THROW
static std::int64_t eval(Raw raw, unsigned fractional_bits) noexcept
{
const detail::u128 mag = detail::magnitude(raw);
@ -379,6 +392,7 @@ template <>
struct exact_lut<exact_constant::ilog10>
{
template <typename Raw>
HEDLEY_NO_THROW
static std::int64_t eval(Raw raw, unsigned fractional_bits) noexcept
{
const detail::u128 mag = detail::magnitude(raw);
@ -416,6 +430,7 @@ template <>
struct exact_lut<exact_constant::clz>
{
template <typename Raw>
HEDLEY_NO_THROW
static std::int64_t eval(Raw raw, unsigned fractional_bits) noexcept
{
if (raw < 0)
@ -452,6 +467,7 @@ template <>
struct exact_lut<exact_constant::clrsb>
{
template <typename Raw>
HEDLEY_NO_THROW
static std::int64_t eval(Raw raw, unsigned fractional_bits) noexcept
{
const detail::u128 mag = detail::magnitude(raw);
@ -559,6 +575,7 @@ enum class threshold_cmp
};
template <typename Raw>
HEDLEY_NO_THROW
std::int64_t evaluate_threshold(Raw raw, Raw bound, threshold_cmp kind) noexcept
{
switch (kind)