Skip to content

Commit

Permalink
float: use safe code for floating point endian conversion
Browse files Browse the repository at this point in the history
This makes use of the 'from_bits' routines.
  • Loading branch information
fintelia authored Apr 2, 2024
1 parent 2e17045 commit 18f32ca
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2054,10 +2054,7 @@ impl ByteOrder for BigEndian {
fn from_slice_f32(numbers: &mut [f32]) {
if cfg!(target_endian = "little") {
for n in numbers {
unsafe {
let int = *(n as *const f32 as *const u32);
*n = *(&int.to_be() as *const u32 as *const f32);
}
*n = f32::from_bits(n.to_bits().to_be());
}
}
}
Expand All @@ -2066,10 +2063,7 @@ impl ByteOrder for BigEndian {
fn from_slice_f64(numbers: &mut [f64]) {
if cfg!(target_endian = "little") {
for n in numbers {
unsafe {
let int = *(n as *const f64 as *const u64);
*n = *(&int.to_be() as *const u64 as *const f64);
}
*n = f64::from_bits(n.to_bits().to_be());
}
}
}
Expand Down Expand Up @@ -2232,10 +2226,7 @@ impl ByteOrder for LittleEndian {
fn from_slice_f32(numbers: &mut [f32]) {
if cfg!(target_endian = "big") {
for n in numbers {
unsafe {
let int = *(n as *const f32 as *const u32);
*n = *(&int.to_le() as *const u32 as *const f32);
}
*n = f32::from_bits(n.to_bits().to_le());
}
}
}
Expand All @@ -2244,10 +2235,7 @@ impl ByteOrder for LittleEndian {
fn from_slice_f64(numbers: &mut [f64]) {
if cfg!(target_endian = "big") {
for n in numbers {
unsafe {
let int = *(n as *const f64 as *const u64);
*n = *(&int.to_le() as *const u64 as *const f64);
}
*n = f64::from_bits(n.to_bits().to_le());
}
}
}
Expand Down

0 comments on commit 18f32ca

Please sign in to comment.