Skip to content

Commit 88d4213

Browse files
committed
add annotations to transmutes to satify nightly clippy
1 parent 2841ec4 commit 88d4213

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66
## [Unreleased]
77
### Fixed
88
- Missing macro import causing build failure on `no_std` + `alloc` feature set. Fixes [#107].
9+
- Clippy warning on nightly rust.
910

1011
## [2.4.0] - 2024-02-25 <a name="2.4.0"></a>
1112
### Added

src/bfloat/convert.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::mem;
55
pub(crate) const fn f32_to_bf16(value: f32) -> u16 {
66
// TODO: Replace mem::transmute with to_bits() once to_bits is const-stabilized
77
// Convert to raw bytes
8-
let x: u32 = unsafe { mem::transmute(value) };
8+
let x: u32 = unsafe { mem::transmute::<f32, u32>(value) };
99

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

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

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

112112
let half_sign = (i & 0x8000u16) as u64;
@@ -117,11 +117,15 @@ pub(crate) const fn bf16_to_f64(i: u16) -> f64 {
117117
if half_exp == 0x7F80u64 {
118118
// Check for signed infinity if mantissa is zero
119119
if half_man == 0 {
120-
return unsafe { mem::transmute((half_sign << 48) | 0x7FF0_0000_0000_0000u64) };
120+
return unsafe {
121+
mem::transmute::<u64, f64>((half_sign << 48) | 0x7FF0_0000_0000_0000u64)
122+
};
121123
} else {
122124
// NaN, keep current mantissa but also set most significiant mantissa bit
123125
return unsafe {
124-
mem::transmute((half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 45))
126+
mem::transmute::<u64, f64>(
127+
(half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 45),
128+
)
125129
};
126130
}
127131
}
@@ -139,10 +143,10 @@ pub(crate) const fn bf16_to_f64(i: u16) -> f64 {
139143
// Rebias and adjust exponent
140144
let exp = ((1023 - 127 - e) as u64) << 52;
141145
let man = (half_man << (46 + e)) & 0xF_FFFF_FFFF_FFFFu64;
142-
return unsafe { mem::transmute(sign | exp | man) };
146+
return unsafe { mem::transmute::<u64, f64>(sign | exp | man) };
143147
}
144148
// Rebias exponent for a normalized normal
145149
let exp = ((unbiased_exp + 1023) as u64) << 52;
146150
let man = (half_man & 0x007Fu64) << 45;
147-
unsafe { mem::transmute(sign | exp | man) }
151+
unsafe { mem::transmute::<u64, f64>(sign | exp | man) }
148152
}

src/binary16/arch.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ fn convert_chunked_slice_4<S: Copy + Default, D: Copy>(
482482
pub(crate) const fn f32_to_f16_fallback(value: f32) -> u16 {
483483
// TODO: Replace mem::transmute with to_bits() once to_bits is const-stabilized
484484
// Convert to raw bytes
485-
let x: u32 = unsafe { mem::transmute(value) };
485+
let x: u32 = unsafe { mem::transmute::<f32, u32>(value) };
486486

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

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

618618
let half_sign = (i & 0x8000u16) as u32;
@@ -623,11 +623,11 @@ pub(crate) const fn f16_to_f32_fallback(i: u16) -> f32 {
623623
if half_exp == 0x7C00u32 {
624624
// Check for signed infinity if mantissa is zero
625625
if half_man == 0 {
626-
return unsafe { mem::transmute((half_sign << 16) | 0x7F80_0000u32) };
626+
return unsafe { mem::transmute::<u32, f32>((half_sign << 16) | 0x7F80_0000u32) };
627627
} else {
628628
// NaN, keep current mantissa but also set most significiant mantissa bit
629629
return unsafe {
630-
mem::transmute((half_sign << 16) | 0x7FC0_0000u32 | (half_man << 13))
630+
mem::transmute::<u32, f32>((half_sign << 16) | 0x7FC0_0000u32 | (half_man << 13))
631631
};
632632
}
633633
}
@@ -645,21 +645,21 @@ pub(crate) const fn f16_to_f32_fallback(i: u16) -> f32 {
645645
// Rebias and adjust exponent
646646
let exp = (127 - 15 - e) << 23;
647647
let man = (half_man << (14 + e)) & 0x7F_FF_FFu32;
648-
return unsafe { mem::transmute(sign | exp | man) };
648+
return unsafe { mem::transmute::<u32, f32>(sign | exp | man) };
649649
}
650650

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

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

665665
let half_sign = (i & 0x8000u16) as u64;
@@ -670,11 +670,15 @@ pub(crate) const fn f16_to_f64_fallback(i: u16) -> f64 {
670670
if half_exp == 0x7C00u64 {
671671
// Check for signed infinity if mantissa is zero
672672
if half_man == 0 {
673-
return unsafe { mem::transmute((half_sign << 48) | 0x7FF0_0000_0000_0000u64) };
673+
return unsafe {
674+
mem::transmute::<u64, f64>((half_sign << 48) | 0x7FF0_0000_0000_0000u64)
675+
};
674676
} else {
675677
// NaN, keep current mantissa but also set most significiant mantissa bit
676678
return unsafe {
677-
mem::transmute((half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 42))
679+
mem::transmute::<u64, f64>(
680+
(half_sign << 48) | 0x7FF8_0000_0000_0000u64 | (half_man << 42),
681+
)
678682
};
679683
}
680684
}
@@ -692,13 +696,13 @@ pub(crate) const fn f16_to_f64_fallback(i: u16) -> f64 {
692696
// Rebias and adjust exponent
693697
let exp = ((1023 - 15 - e) as u64) << 52;
694698
let man = (half_man << (43 + e)) & 0xF_FFFF_FFFF_FFFFu64;
695-
return unsafe { mem::transmute(sign | exp | man) };
699+
return unsafe { mem::transmute::<u64, f64>(sign | exp | man) };
696700
}
697701

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

704708
#[inline]

0 commit comments

Comments
 (0)