Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 459dd80

Browse files
committed
Change from_parts to take a u32 exponent rather than i32
Make things more consistent with other API that works with a bitwise representation of the exponent. That is, use `u32` when working with a bitwise (biased) representation, use `i32` when the bitwise representation has been adjusted for bias and ay be negative. Every place this has been used so far has an `as i32`, so this change makes things cleaner anyway.
1 parent 122ba48 commit 459dd80

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/math/generic/sqrt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ where
9292
}
9393

9494
// Normalize subnormals by multiplying by 1.0 << SIG_BITS (e.g. 0x1p52 for doubles).
95-
let scaled = x * F::from_parts(false, (F::SIG_BITS + F::EXP_BIAS) as i32, zero);
95+
let scaled = x * F::from_parts(false, F::SIG_BITS + F::EXP_BIAS, zero);
9696
ix = scaled.to_bits();
9797
match top {
9898
Exp::Shifted(ref mut v) => {

src/math/support/float_traits.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ pub trait Float:
131131
fn from_bits(a: Self::Int) -> Self;
132132

133133
/// Constructs a `Self` from its parts. Inputs are treated as bits and shifted into position.
134-
fn from_parts(negative: bool, exponent: i32, significand: Self::Int) -> Self {
134+
fn from_parts(negative: bool, exponent: u32, significand: Self::Int) -> Self {
135135
let sign = if negative { Self::Int::ONE } else { Self::Int::ZERO };
136136
Self::from_bits(
137137
(sign << (Self::BITS - 1))
138-
| (Self::Int::cast_from(exponent as u32 & Self::EXP_MAX) << Self::SIG_BITS)
138+
| (Self::Int::cast_from(exponent & Self::EXP_MAX) << Self::SIG_BITS)
139139
| (significand & Self::SIG_MASK),
140140
)
141141
}
@@ -282,7 +282,7 @@ mod tests {
282282
assert_eq!(f16::from_bits(0x1).exp_unbiased(), -15);
283283

284284
// `from_parts`
285-
assert_biteq!(f16::from_parts(true, f16::EXP_BIAS as i32, 0), -1.0f16);
285+
assert_biteq!(f16::from_parts(true, f16::EXP_BIAS, 0), -1.0f16);
286286
assert_biteq!(f16::from_parts(false, 0, 1), f16::from_bits(0x1));
287287
}
288288

@@ -304,8 +304,8 @@ mod tests {
304304
assert_eq!(f32::from_bits(0x1).exp_unbiased(), -127);
305305

306306
// `from_parts`
307-
assert_biteq!(f32::from_parts(true, f32::EXP_BIAS as i32, 0), -1.0f32);
308-
assert_biteq!(f32::from_parts(false, 10 + f32::EXP_BIAS as i32, 0), hf32!("0x1p10"));
307+
assert_biteq!(f32::from_parts(true, f32::EXP_BIAS, 0), -1.0f32);
308+
assert_biteq!(f32::from_parts(false, 10 + f32::EXP_BIAS, 0), hf32!("0x1p10"));
309309
assert_biteq!(f32::from_parts(false, 0, 1), f32::from_bits(0x1));
310310
}
311311

@@ -327,8 +327,8 @@ mod tests {
327327
assert_eq!(f64::from_bits(0x1).exp_unbiased(), -1023);
328328

329329
// `from_parts`
330-
assert_biteq!(f64::from_parts(true, f64::EXP_BIAS as i32, 0), -1.0f64);
331-
assert_biteq!(f64::from_parts(false, 10 + f64::EXP_BIAS as i32, 0), hf64!("0x1p10"));
330+
assert_biteq!(f64::from_parts(true, f64::EXP_BIAS, 0), -1.0f64);
331+
assert_biteq!(f64::from_parts(false, 10 + f64::EXP_BIAS, 0), hf64!("0x1p10"));
332332
assert_biteq!(f64::from_parts(false, 0, 1), f64::from_bits(0x1));
333333
}
334334

@@ -351,7 +351,7 @@ mod tests {
351351
assert_eq!(f128::from_bits(0x1).exp_unbiased(), -16383);
352352

353353
// `from_parts`
354-
assert_biteq!(f128::from_parts(true, f128::EXP_BIAS as i32, 0), -1.0f128);
354+
assert_biteq!(f128::from_parts(true, f128::EXP_BIAS, 0), -1.0f128);
355355
assert_biteq!(f128::from_parts(false, 0, 1), f128::from_bits(0x1));
356356
}
357357
}

0 commit comments

Comments
 (0)