Skip to content

Commit 96a89e6

Browse files
Merge #218
218: Implement is_sign_* and signum methods in terms of bitcasts. r=cuviper a=ElectronicRU # Rationale [rust-gpu](/EmbarkStudios/rust-gpu) project uses num-traits directly and via glam. However, integer_decode() function is a bit poisonous for it - it immediately requires both Int16 and Int64 capabilities. This PR reimplements corresponding functions in terms of libm's copysign. (For some reason, rust's libm is missing signbit - it can be done with a bitcast though, maybe that's actually a better implementation avenue). Co-authored-by: Alex S <[email protected]>
2 parents 92298b2 + cfab8ed commit 96a89e6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/float.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,17 @@ impl FloatCore for f32 {
781781
}
782782
}
783783

784+
#[inline]
785+
#[cfg(not(feature = "std"))]
786+
fn is_sign_negative(self) -> bool {
787+
const SIGN_MASK: u32 = 0x80000000;
788+
789+
// Safety: this identical to the implementation of f32::to_bits(),
790+
// which is only available starting at Rust 1.20
791+
let bits: u32 = unsafe { mem::transmute(self) };
792+
bits & SIGN_MASK != 0
793+
}
794+
784795
#[inline]
785796
#[cfg(not(feature = "std"))]
786797
fn to_degrees(self) -> Self {
@@ -872,6 +883,17 @@ impl FloatCore for f64 {
872883
}
873884
}
874885

886+
#[inline]
887+
#[cfg(not(feature = "std"))]
888+
fn is_sign_negative(self) -> bool {
889+
const SIGN_MASK: u64 = 0x8000000000000000;
890+
891+
// Safety: this identical to the implementation of f64::to_bits(),
892+
// which is only available starting at Rust 1.20
893+
let bits: u64 = unsafe { mem::transmute(self) };
894+
bits & SIGN_MASK != 0
895+
}
896+
875897
#[inline]
876898
#[cfg(not(feature = "std"))]
877899
fn to_degrees(self) -> Self {

0 commit comments

Comments
 (0)