From 88d42138ce1b3457d31e28f4307b73a6b2503260 Mon Sep 17 00:00:00 2001 From: Kathryn Long Date: Sat, 6 Apr 2024 15:52:06 -0400 Subject: [PATCH] add annotations to transmutes to satify nightly clippy --- CHANGELOG.md | 1 + src/bfloat/convert.rs | 22 +++++++++++++--------- src/binary16/arch.rs | 28 ++++++++++++++++------------ 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f86e73..3c09421 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ### Added diff --git a/src/bfloat/convert.rs b/src/bfloat/convert.rs index 8f258f5..04d0649 100644 --- a/src/bfloat/convert.rs +++ b/src/bfloat/convert.rs @@ -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::(value) }; // check for NaN if x & 0x7FFF_FFFFu32 > 0x7F80_0000u32 { @@ -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::(value) }; let x = (val >> 32) as u32; // Extract IEEE754 components @@ -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::((i as u32 | 0x0040u32) << 16) } } else { - unsafe { mem::transmute((i as u32) << 16) } + unsafe { mem::transmute::((i as u32) << 16) } } } @@ -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::((i as u64) << 48) }; } let half_sign = (i & 0x8000u16) as u64; @@ -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::((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::( + (half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 45), + ) }; } } @@ -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::(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::(sign | exp | man) } } diff --git a/src/binary16/arch.rs b/src/binary16/arch.rs index d33103e..4c003a0 100644 --- a/src/binary16/arch.rs +++ b/src/binary16/arch.rs @@ -482,7 +482,7 @@ fn convert_chunked_slice_4( 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::(value) }; // Extract IEEE754 components let sign = x & 0x8000_0000u32; @@ -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::(value) }; let x = (val >> 32) as u32; // Extract IEEE754 components @@ -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::((i as u32) << 16) }; } let half_sign = (i & 0x8000u16) as u32; @@ -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::((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::((half_sign << 16) | 0x7FC0_0000u32 | (half_man << 13)) }; } } @@ -645,13 +645,13 @@ 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::(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::(sign | exp | man) } } #[inline] @@ -659,7 +659,7 @@ 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::((i as u64) << 48) }; } let half_sign = (i & 0x8000u16) as u64; @@ -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::((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::( + (half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 42), + ) }; } } @@ -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::(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::(sign | exp | man) } } #[inline]