v2 / vlib / math / bits / unsafe_bits.v
42 lines · 38 sloc · 1.23 KB · 0321c3f544d70e2d789f2b2fa985d0f57e6f0c4f
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module bits
5
6// f32_bits returns the IEEE 754 binary representation of f,
7// with the sign bit of f and the result in the same bit position.
8// f32_bits(f32_from_bits(x)) == x.
9@[inline]
10pub fn f32_bits(f f32) u32 {
11 p := *unsafe { &u32(&f) }
12 return p
13}
14
15// f32_from_bits returns the floating-point number corresponding
16// to the IEEE 754 binary representation b, with the sign bit of b
17// and the result in the same bit position.
18// f32_from_bits(f32_bits(x)) == x.
19@[inline]
20pub fn f32_from_bits(b u32) f32 {
21 p := *unsafe { &f32(&b) }
22 return p
23}
24
25// f64_bits returns the IEEE 754 binary representation of f,
26// with the sign bit of f and the result in the same bit position,
27// and f64_bits(f64_from_bits(x)) == x.
28@[inline]
29pub fn f64_bits(f f64) u64 {
30 p := *unsafe { &u64(&f) }
31 return p
32}
33
34// f64_from_bits returns the floating-point number corresponding
35// to the IEEE 754 binary representation b, with the sign bit of b
36// and the result in the same bit position.
37// f64_from_bits(f64_bits(x)) == x.
38@[inline]
39pub fn f64_from_bits(b u64) f64 {
40 p := *unsafe { &f64(&b) }
41 return p
42}
43