Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
while_float
  • Loading branch information
ShaharNaveh committed Jun 27, 2026
commit f7163ee4abf1be56da252cdc23f4092a6ef6af9a
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ unused_peekable = "warn"
unused_rounding = "warn"
use_self = "warn"
useless_let_if_seq = "warn"
while_float = "warn"

# pedantic lints to enforce gradually
assigning_clones = "warn"
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/float_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use num_traits::{Signed, ToPrimitive};

#[must_use]
pub const fn decompose_float(value: f64) -> (f64, i32) {
if 0.0 == value {
(0.0, 0i32)
if value == 0.0 {
(0.0, 0)
} else {
let bits = value.to_bits();
let exponent: i32 = ((bits >> 52) & 0x7ff) as i32 - 1022;
Expand Down
19 changes: 10 additions & 9 deletions crates/common/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ impl HashSecret {
let k1 = u64::from_le_bytes(right.try_into().unwrap());
Self { k0, k1 }
}
}

impl HashSecret {
pub fn hash_value<T: Hash + ?Sized>(&self, data: &T) -> PyHash {
fix_sentinel(mod_int(self.hash_one(data) as _))
}
Expand Down Expand Up @@ -94,7 +92,7 @@ pub const fn hash_pointer(value: usize) -> PyHash {

#[inline]
#[must_use]
pub fn hash_float(value: f64) -> Option<PyHash> {
pub const fn hash_float(value: f64) -> Option<PyHash> {
// cpython _Py_HashDouble
if !value.is_finite() {
return if value.is_infinite() {
Expand All @@ -111,6 +109,8 @@ pub fn hash_float(value: f64) -> Option<PyHash> {
let mut m = frexp.0;
let mut e = frexp.1;
let mut x: PyUHash = 0;

#[expect(clippy::while_float, reason = "keep this loop like CPython does it")]
while m != 0.0 {
x = ((x << 28) & MODULUS) | (x >> (BITS - 28));
m *= 268_435_456.0; // 2**28
Expand All @@ -137,13 +137,14 @@ pub fn hash_float(value: f64) -> Option<PyHash> {

#[must_use]
pub fn hash_bigint(value: &BigInt) -> PyHash {
let ret = match value.to_i64() {
Some(i) => mod_int(i),
None => (value % MODULUS).to_i64().unwrap_or_else(|| unsafe {
// SAFETY: MODULUS < i64::MAX, so value % MODULUS is guaranteed to be in the range of i64
core::hint::unreachable_unchecked()
}),
let ret = if let Some(v) = value.to_i64() {
mod_int(v)
} else {
// SAFETY:
// MODULUS < i64::MAX, so value % MODULUS is guaranteed to be in the range of i64
unsafe { (value % MODULUS).to_i64().unwrap_unchecked() }
};

fix_sentinel(ret)
}

Expand Down