Skip to content

Commit

Permalink
add annotations to transmutes to satify nightly clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
starkat99 committed Apr 6, 2024
1 parent 2841ec4 commit 88d4213
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
### Fixed
- Missing macro import causing build failure on `no_std` + `alloc` feature set. Fixes [#107].
- Clippy warning on nightly rust.

## [2.4.0] - 2024-02-25 <a name="2.4.0"></a>
### Added
Expand Down
22 changes: 13 additions & 9 deletions src/bfloat/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::mem;
pub(crate) const fn f32_to_bf16(value: f32) -> u16 {
// TODO: Replace mem::transmute with to_bits() once to_bits is const-stabilized
// Convert to raw bytes
let x: u32 = unsafe { mem::transmute(value) };
let x: u32 = unsafe { mem::transmute::<f32, u32>(value) };

// check for NaN
if x & 0x7FFF_FFFFu32 > 0x7F80_0000u32 {
Expand All @@ -27,7 +27,7 @@ pub(crate) const fn f64_to_bf16(value: f64) -> u16 {
// TODO: Replace mem::transmute with to_bits() once to_bits is const-stabilized
// Convert to raw bytes, truncating the last 32-bits of mantissa; that precision will always
// be lost on half-precision.
let val: u64 = unsafe { mem::transmute(value) };
let val: u64 = unsafe { mem::transmute::<f64, u64>(value) };
let x = (val >> 32) as u32;

// Extract IEEE754 components
Expand Down Expand Up @@ -95,9 +95,9 @@ pub(crate) const fn bf16_to_f32(i: u16) -> f32 {
// TODO: Replace mem::transmute with from_bits() once from_bits is const-stabilized
// If NaN, keep current mantissa but also set most significiant mantissa bit
if i & 0x7FFFu16 > 0x7F80u16 {
unsafe { mem::transmute((i as u32 | 0x0040u32) << 16) }
unsafe { mem::transmute::<u32, f32>((i as u32 | 0x0040u32) << 16) }
} else {
unsafe { mem::transmute((i as u32) << 16) }
unsafe { mem::transmute::<u32, f32>((i as u32) << 16) }
}
}

Expand All @@ -106,7 +106,7 @@ pub(crate) const fn bf16_to_f64(i: u16) -> f64 {
// TODO: Replace mem::transmute with from_bits() once from_bits is const-stabilized
// Check for signed zero
if i & 0x7FFFu16 == 0 {
return unsafe { mem::transmute((i as u64) << 48) };
return unsafe { mem::transmute::<u64, f64>((i as u64) << 48) };
}

let half_sign = (i & 0x8000u16) as u64;
Expand All @@ -117,11 +117,15 @@ pub(crate) const fn bf16_to_f64(i: u16) -> f64 {
if half_exp == 0x7F80u64 {
// Check for signed infinity if mantissa is zero
if half_man == 0 {
return unsafe { mem::transmute((half_sign << 48) | 0x7FF0_0000_0000_0000u64) };
return unsafe {
mem::transmute::<u64, f64>((half_sign << 48) | 0x7FF0_0000_0000_0000u64)
};
} else {
// NaN, keep current mantissa but also set most significiant mantissa bit
return unsafe {
mem::transmute((half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 45))
mem::transmute::<u64, f64>(
(half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 45),
)
};
}
}
Expand All @@ -139,10 +143,10 @@ pub(crate) const fn bf16_to_f64(i: u16) -> f64 {
// Rebias and adjust exponent
let exp = ((1023 - 127 - e) as u64) << 52;
let man = (half_man << (46 + e)) & 0xF_FFFF_FFFF_FFFFu64;
return unsafe { mem::transmute(sign | exp | man) };
return unsafe { mem::transmute::<u64, f64>(sign | exp | man) };
}
// Rebias exponent for a normalized normal
let exp = ((unbiased_exp + 1023) as u64) << 52;
let man = (half_man & 0x007Fu64) << 45;
unsafe { mem::transmute(sign | exp | man) }
unsafe { mem::transmute::<u64, f64>(sign | exp | man) }
}
28 changes: 16 additions & 12 deletions src/binary16/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ fn convert_chunked_slice_4<S: Copy + Default, D: Copy>(
pub(crate) const fn f32_to_f16_fallback(value: f32) -> u16 {
// TODO: Replace mem::transmute with to_bits() once to_bits is const-stabilized
// Convert to raw bytes
let x: u32 = unsafe { mem::transmute(value) };
let x: u32 = unsafe { mem::transmute::<f32, u32>(value) };

// Extract IEEE754 components
let sign = x & 0x8000_0000u32;
Expand Down Expand Up @@ -544,7 +544,7 @@ pub(crate) const fn f64_to_f16_fallback(value: f64) -> u16 {
// Convert to raw bytes, truncating the last 32-bits of mantissa; that precision will always
// be lost on half-precision.
// TODO: Replace mem::transmute with to_bits() once to_bits is const-stabilized
let val: u64 = unsafe { mem::transmute(value) };
let val: u64 = unsafe { mem::transmute::<f64, u64>(value) };
let x = (val >> 32) as u32;

// Extract IEEE754 components
Expand Down Expand Up @@ -612,7 +612,7 @@ pub(crate) const fn f16_to_f32_fallback(i: u16) -> f32 {
// Check for signed zero
// TODO: Replace mem::transmute with from_bits() once from_bits is const-stabilized
if i & 0x7FFFu16 == 0 {
return unsafe { mem::transmute((i as u32) << 16) };
return unsafe { mem::transmute::<u32, f32>((i as u32) << 16) };
}

let half_sign = (i & 0x8000u16) as u32;
Expand All @@ -623,11 +623,11 @@ pub(crate) const fn f16_to_f32_fallback(i: u16) -> f32 {
if half_exp == 0x7C00u32 {
// Check for signed infinity if mantissa is zero
if half_man == 0 {
return unsafe { mem::transmute((half_sign << 16) | 0x7F80_0000u32) };
return unsafe { mem::transmute::<u32, f32>((half_sign << 16) | 0x7F80_0000u32) };
} else {
// NaN, keep current mantissa but also set most significiant mantissa bit
return unsafe {
mem::transmute((half_sign << 16) | 0x7FC0_0000u32 | (half_man << 13))
mem::transmute::<u32, f32>((half_sign << 16) | 0x7FC0_0000u32 | (half_man << 13))
};
}
}
Expand All @@ -645,21 +645,21 @@ pub(crate) const fn f16_to_f32_fallback(i: u16) -> f32 {
// Rebias and adjust exponent
let exp = (127 - 15 - e) << 23;
let man = (half_man << (14 + e)) & 0x7F_FF_FFu32;
return unsafe { mem::transmute(sign | exp | man) };
return unsafe { mem::transmute::<u32, f32>(sign | exp | man) };
}

// Rebias exponent for a normalized normal
let exp = ((unbiased_exp + 127) as u32) << 23;
let man = (half_man & 0x03FFu32) << 13;
unsafe { mem::transmute(sign | exp | man) }
unsafe { mem::transmute::<u32, f32>(sign | exp | man) }
}

#[inline]
pub(crate) const fn f16_to_f64_fallback(i: u16) -> f64 {
// Check for signed zero
// TODO: Replace mem::transmute with from_bits() once from_bits is const-stabilized
if i & 0x7FFFu16 == 0 {
return unsafe { mem::transmute((i as u64) << 48) };
return unsafe { mem::transmute::<u64, f64>((i as u64) << 48) };
}

let half_sign = (i & 0x8000u16) as u64;
Expand All @@ -670,11 +670,15 @@ pub(crate) const fn f16_to_f64_fallback(i: u16) -> f64 {
if half_exp == 0x7C00u64 {
// Check for signed infinity if mantissa is zero
if half_man == 0 {
return unsafe { mem::transmute((half_sign << 48) | 0x7FF0_0000_0000_0000u64) };
return unsafe {
mem::transmute::<u64, f64>((half_sign << 48) | 0x7FF0_0000_0000_0000u64)
};
} else {
// NaN, keep current mantissa but also set most significiant mantissa bit
return unsafe {
mem::transmute((half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 42))
mem::transmute::<u64, f64>(
(half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 42),
)
};
}
}
Expand All @@ -692,13 +696,13 @@ pub(crate) const fn f16_to_f64_fallback(i: u16) -> f64 {
// Rebias and adjust exponent
let exp = ((1023 - 15 - e) as u64) << 52;
let man = (half_man << (43 + e)) & 0xF_FFFF_FFFF_FFFFu64;
return unsafe { mem::transmute(sign | exp | man) };
return unsafe { mem::transmute::<u64, f64>(sign | exp | man) };
}

// Rebias exponent for a normalized normal
let exp = ((unbiased_exp + 1023) as u64) << 52;
let man = (half_man & 0x03FFu64) << 42;
unsafe { mem::transmute(sign | exp | man) }
unsafe { mem::transmute::<u64, f64>(sign | exp | man) }
}

#[inline]
Expand Down

0 comments on commit 88d4213

Please sign in to comment.