2023-03-28 23:55:57 +03:00
|
|
|
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
2019-10-16 11:19:03 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module math
|
2021-05-08 13:32:29 +03:00
|
|
|
|
2019-10-16 11:19:03 +03:00
|
|
|
// f32_bits returns the IEEE 754 binary representation of f,
|
|
|
|
// with the sign bit of f and the result in the same bit position.
|
|
|
|
// f32_bits(f32_from_bits(x)) == x.
|
|
|
|
pub fn f32_bits(f f32) u32 {
|
2021-05-08 13:32:29 +03:00
|
|
|
p := *unsafe { &u32(&f) }
|
2020-03-19 12:24:51 +03:00
|
|
|
return p
|
2019-10-16 11:19:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// f32_from_bits returns the floating-point number corresponding
|
|
|
|
// to the IEEE 754 binary representation b, with the sign bit of b
|
|
|
|
// and the result in the same bit position.
|
|
|
|
// f32_from_bits(f32_bits(x)) == x.
|
|
|
|
pub fn f32_from_bits(b u32) f32 {
|
2021-05-08 13:32:29 +03:00
|
|
|
p := *unsafe { &f32(&b) }
|
2020-03-19 12:24:51 +03:00
|
|
|
return p
|
2019-10-16 11:19:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// f64_bits returns the IEEE 754 binary representation of f,
|
|
|
|
// with the sign bit of f and the result in the same bit position,
|
|
|
|
// and f64_bits(f64_from_bits(x)) == x.
|
|
|
|
pub fn f64_bits(f f64) u64 {
|
2021-05-08 13:32:29 +03:00
|
|
|
p := *unsafe { &u64(&f) }
|
2020-03-19 12:24:51 +03:00
|
|
|
return p
|
2019-10-16 11:19:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// f64_from_bits returns the floating-point number corresponding
|
|
|
|
// to the IEEE 754 binary representation b, with the sign bit of b
|
|
|
|
// and the result in the same bit position.
|
|
|
|
// f64_from_bits(f64_bits(x)) == x.
|
|
|
|
pub fn f64_from_bits(b u64) f64 {
|
2021-05-08 13:32:29 +03:00
|
|
|
p := *unsafe { &f64(&b) }
|
2020-03-19 12:24:51 +03:00
|
|
|
return p
|
2019-10-16 11:19:03 +03:00
|
|
|
}
|
2021-10-08 17:44:55 +03:00
|
|
|
|
|
|
|
// with_set_low_word sets low word of `f` to `lo`
|
|
|
|
pub fn with_set_low_word(f f64, lo u32) f64 {
|
|
|
|
mut tmp := f64_bits(f)
|
|
|
|
tmp &= 0xffffffff_00000000
|
|
|
|
tmp |= u64(lo)
|
|
|
|
return f64_from_bits(tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// with_set_high_word sets high word of `f` to `lo`
|
|
|
|
pub fn with_set_high_word(f f64, hi u32) f64 {
|
|
|
|
mut tmp := f64_bits(f)
|
|
|
|
tmp &= 0x00000000_ffffffff
|
|
|
|
tmp |= u64(hi) << 32
|
|
|
|
return f64_from_bits(tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_high_word returns high part of the word of `f`.
|
|
|
|
pub fn get_high_word(f f64) u32 {
|
|
|
|
return u32(f64_bits(f) >> 32)
|
|
|
|
}
|