Skip to content

Commit 18f32ca

Browse files
authored
float: use safe code for floating point endian conversion
This makes use of the 'from_bits' routines.
1 parent 2e17045 commit 18f32ca

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

src/lib.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,10 +2054,7 @@ impl ByteOrder for BigEndian {
20542054
fn from_slice_f32(numbers: &mut [f32]) {
20552055
if cfg!(target_endian = "little") {
20562056
for n in numbers {
2057-
unsafe {
2058-
let int = *(n as *const f32 as *const u32);
2059-
*n = *(&int.to_be() as *const u32 as *const f32);
2060-
}
2057+
*n = f32::from_bits(n.to_bits().to_be());
20612058
}
20622059
}
20632060
}
@@ -2066,10 +2063,7 @@ impl ByteOrder for BigEndian {
20662063
fn from_slice_f64(numbers: &mut [f64]) {
20672064
if cfg!(target_endian = "little") {
20682065
for n in numbers {
2069-
unsafe {
2070-
let int = *(n as *const f64 as *const u64);
2071-
*n = *(&int.to_be() as *const u64 as *const f64);
2072-
}
2066+
*n = f64::from_bits(n.to_bits().to_be());
20732067
}
20742068
}
20752069
}
@@ -2232,10 +2226,7 @@ impl ByteOrder for LittleEndian {
22322226
fn from_slice_f32(numbers: &mut [f32]) {
22332227
if cfg!(target_endian = "big") {
22342228
for n in numbers {
2235-
unsafe {
2236-
let int = *(n as *const f32 as *const u32);
2237-
*n = *(&int.to_le() as *const u32 as *const f32);
2238-
}
2229+
*n = f32::from_bits(n.to_bits().to_le());
22392230
}
22402231
}
22412232
}
@@ -2244,10 +2235,7 @@ impl ByteOrder for LittleEndian {
22442235
fn from_slice_f64(numbers: &mut [f64]) {
22452236
if cfg!(target_endian = "big") {
22462237
for n in numbers {
2247-
unsafe {
2248-
let int = *(n as *const f64 as *const u64);
2249-
*n = *(&int.to_le() as *const u64 as *const f64);
2250-
}
2238+
*n = f64::from_bits(n.to_bits().to_le());
22512239
}
22522240
}
22532241
}

0 commit comments

Comments
 (0)