Skip to content

Commit

Permalink
Improve u32_to_f32()
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Jan 26, 2025
1 parent 92d4021 commit 39e8629
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/tree/conversions.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
/// Convert [`u32`] fraction to [`f32`] (ranged 0 to 1).
pub(crate) fn u32_to_f32(fraction: u32) -> f32 {

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

function `u32_to_f32` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, nightly)

function `u32_to_f32` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, stable)

function `u32_to_f32` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, beta)

function `u32_to_f32` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, nightly)

function `u32_to_f32` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

function `u32_to_f32` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

function `u32_to_f32` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, nightly)

function `u32_to_f32` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, stable)

function `u32_to_f32` is never used
// Calculate leading zeros including inferred 1
let leading_zeros = fraction.leading_zeros();
// Remove leading zeros and extra bit to subtract from exponent
let (fraction, leading_zeros) = if leading_zeros >= 32 {
(0, 127)
} else {
(fraction << leading_zeros, leading_zeros + 1)
};
// Check if fraction is 0
let nonzero = u32::from(fraction != 0);
// Calculate leading zeros
let leading_zeros = nonzero * fraction.leading_zeros();
// Remove leading zeros to subtract from exponent
let fraction = fraction << leading_zeros;
// Truncate to 24-bit fraction
let fraction = fraction >> 8;
// Clear 24th bit
// Clear 24th (inferred 1) bit
let fraction = fraction & !(1 << 23);
// Calculate -127 bias exponent (plus inferred 1)
let exponent = (127 - leading_zeros) << 23;
// Calculate -127 bias exponent (minus inferred 1)
let exponent = (nonzero * 126 - leading_zeros) << 23;

// Scale up (u32 max is 2³² - 1, and we want 2³²)
f32::from_bits(exponent | fraction)
* f32::from_bits(0b111111100000000000000000000001)
}
Expand Down

0 comments on commit 39e8629

Please sign in to comment.