-
Notifications
You must be signed in to change notification settings - Fork 599
chore: numeric audit 0 #20491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: numeric audit 0 #20491
Changes from all commits
4240295
2bb46ba
4c2dd41
6f0e7de
086213e
26d4599
362403b
ea968e9
8128d74
5d63625
65f0a1b
6fa372f
e0ce568
32fa97c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,15 @@ | ||
| // === AUDIT STATUS === | ||
| // internal: { status: Planned, auditors: [], commit: } | ||
| // internal: { status: Complete, auditors: [Luke], commit: } | ||
| // external_1: { status: not started, auditors: [], commit: } | ||
| // external_2: { status: not started, auditors: [], commit: } | ||
| // ===================== | ||
|
|
||
| #pragma once | ||
| #include <array> | ||
| #include <cassert> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <limits> | ||
| namespace bb::numeric { | ||
|
|
||
| // from http://supertech.csail.mit.edu/papers/debruijn.pdf | ||
|
|
@@ -72,8 +74,17 @@ template <typename T> constexpr inline T get_lsb(const T in) | |
|
|
||
| template <typename T> constexpr inline T round_up_power_2(const T in) | ||
| { | ||
| if (in == 0) { | ||
| return 0; | ||
| } | ||
| auto lower_bound = T(1) << get_msb(in); | ||
| return (lower_bound == in || lower_bound == 1) ? in : lower_bound * 2; | ||
| if (lower_bound == in) { | ||
| return in; | ||
| } | ||
| // Overflow check: lower_bound is the highest power of 2 <= in, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably overly cautious but avoids wrap in lower_bound * 2 if input is > numeric_limits/2 |
||
| // so lower_bound * 2 would overflow if lower_bound is already the top bit. | ||
| assert(lower_bound <= (std::numeric_limits<T>::max() >> 1)); | ||
| return lower_bound * 2; | ||
| } | ||
|
|
||
| } // namespace bb::numeric | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // === AUDIT STATUS === | ||
| // internal: { status: Planned, auditors: [], commit: } | ||
| // internal: { status: Complete, auditors: [Luke], commit: } | ||
| // external_1: { status: not started, auditors: [], commit: } | ||
| // external_2: { status: not started, auditors: [], commit: } | ||
| // ===================== | ||
|
|
@@ -37,12 +37,15 @@ class alignas(32) uint128_t { | |
| return { static_cast<uint32_t>(a), static_cast<uint32_t>(a >> 32), 0, 0 }; | ||
| } | ||
|
|
||
| constexpr explicit operator uint64_t() { return (static_cast<uint64_t>(data[1]) << 32) + data[0]; } | ||
| constexpr explicit operator uint64_t() const { return (static_cast<uint64_t>(data[1]) << 32) + data[0]; } | ||
|
|
||
| constexpr uint128_t& operator=(const uint128_t& other) = default; | ||
| constexpr uint128_t& operator=(uint128_t&& other) = default; | ||
| constexpr ~uint128_t() = default; | ||
| explicit constexpr operator bool() const { return static_cast<bool>(data[0]); }; | ||
| explicit constexpr operator bool() const | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug: was only checking lowest limb. wasn't ever showing up anywhere because we don't have instances where we directly treat a u128 as a bool
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe worth documenting that it converts any non-zero value to 1? |
||
| { | ||
| return (data[0] != 0) || (data[1] != 0) || (data[2] != 0) || (data[3] != 0); | ||
| }; | ||
|
|
||
| template <std::integral T> explicit constexpr operator T() const { return static_cast<T>(data[0]); }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // === AUDIT STATUS === | ||
| // internal: { status: Planned, auditors: [], commit: } | ||
| // internal: { status: Complete, auditors: [Luke], commit: } | ||
| // external_1: { status: not started, auditors: [], commit: } | ||
| // external_2: { status: not started, auditors: [], commit: } | ||
| // ===================== | ||
|
|
@@ -109,9 +109,12 @@ class alignas(32) uint256_t { | |
| constexpr uint256_t& operator=(uint256_t&& other) noexcept = default; | ||
| constexpr ~uint256_t() noexcept = default; | ||
|
|
||
| explicit constexpr operator bool() const { return static_cast<bool>(data[0]); }; | ||
| explicit constexpr operator bool() const | ||
| { | ||
| return (data[0] != 0) || (data[1] != 0) || (data[2] != 0) || (data[3] != 0); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here |
||
| }; | ||
|
|
||
| constexpr explicit operator uint128_t() { return (static_cast<uint128_t>(data[1]) << 64) + data[0]; } | ||
| constexpr explicit operator uint128_t() const { return (static_cast<uint128_t>(data[1]) << 64) + data[0]; } | ||
| template <std::integral T> explicit constexpr operator T() const { return static_cast<T>(data[0]); }; | ||
|
|
||
| [[nodiscard]] constexpr bool get_bit(uint64_t bit_index) const; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its technically undefined behavior to call __builtin_clzll on 0 so adding this extra protection (matches what we do for u256 below)