1 | // Copyright (c) 2019-2023 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. |
4 | module math |
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] |
10 | pub 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] |
20 | pub 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] |
29 | pub 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] |
39 | pub fn f64_from_bits(b u64) f64 { |
40 | p := *unsafe { &f64(&b) } |
41 | return p |
42 | } |
43 | |
44 | // with_set_low_word sets low word of `f` to `lo` |
45 | [inline] |
46 | pub fn with_set_low_word(f f64, lo u32) f64 { |
47 | mut tmp := f64_bits(f) |
48 | tmp &= 0xffffffff_00000000 |
49 | tmp |= u64(lo) |
50 | return f64_from_bits(tmp) |
51 | } |
52 | |
53 | // with_set_high_word sets high word of `f` to `lo` |
54 | [inline] |
55 | pub fn with_set_high_word(f f64, hi u32) f64 { |
56 | mut tmp := f64_bits(f) |
57 | tmp &= 0x00000000_ffffffff |
58 | tmp |= u64(hi) << 32 |
59 | return f64_from_bits(tmp) |
60 | } |
61 | |
62 | // get_high_word returns high part of the word of `f`. |
63 | [inline] |
64 | pub fn get_high_word(f f64) u32 { |
65 | return u32(f64_bits(f) >> 32) |
66 | } |