Skip to content

Commit 0ec3ed0

Browse files
authored
Merge pull request #22 from Alexhuszagh/endian
Removed endian-dependent codepaths.
2 parents 871c06f + d316634 commit 0ec3ed0

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/float.rs

+11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub trait Float:
4141
const MAX_MANTISSA_FAST_PATH: u64 = 2_u64 << Self::MANTISSA_EXPLICIT_BITS;
4242

4343
fn from_u64(v: u64) -> Self;
44+
fn from_u64_bits(v: u64) -> Self;
4445
fn pow10_fast_path(exponent: usize) -> Self;
4546
}
4647

@@ -69,6 +70,11 @@ impl Float for f32 {
6970
v as _
7071
}
7172

73+
#[inline]
74+
fn from_u64_bits(v: u64) -> Self {
75+
f32::from_bits((v & 0xFFFFFFFF) as u32)
76+
}
77+
7278
#[inline]
7379
fn pow10_fast_path(exponent: usize) -> Self {
7480
#[allow(clippy::use_self)]
@@ -104,6 +110,11 @@ impl Float for f64 {
104110
v as _
105111
}
106112

113+
#[inline]
114+
fn from_u64_bits(v: u64) -> Self {
115+
f64::from_bits(v)
116+
}
117+
107118
#[inline]
108119
fn pow10_fast_path(exponent: usize) -> Self {
109120
#[allow(clippy::use_self)]

src/parse.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use core::mem;
2-
31
use crate::binary::compute_float;
42
use crate::float::Float;
53
use crate::number::{parse_inf_nan, parse_number};
@@ -32,13 +30,5 @@ pub fn parse_float<F: Float>(s: &[u8]) -> Option<(F, usize)> {
3230
if num.negative {
3331
word |= 1_u64 << F::SIGN_INDEX;
3432
}
35-
let value = unsafe {
36-
if cfg!(target_endian = "big") && mem::size_of::<F>() == 4 {
37-
*(&word as *const _ as *const F).add(1)
38-
} else {
39-
*(&word as *const _ as *const F)
40-
}
41-
};
42-
43-
Some((value, rest))
33+
Some((F::from_u64_bits(word), rest))
4434
}

0 commit comments

Comments
 (0)