| 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. |
| 4 | module math |
| 5 | |
| 6 | union U32_F32 { |
| 7 | u u32 |
| 8 | f f32 |
| 9 | } |
| 10 | |
| 11 | // f32_bits returns the IEEE 754 binary representation of f, |
| 12 | // with the sign bit of f and the result in the same bit position. |
| 13 | // f32_bits(f32_from_bits(x)) == x. |
| 14 | @[inline] |
| 15 | pub fn f32_bits(f f32) u32 { |
| 16 | $if tinyc { |
| 17 | return *unsafe { &u32(&f) } // this is faster for tcc, but causes `error: dereferencing type-punned pointer will break strict-aliasing rules` on gcc, with -cstrict |
| 18 | } |
| 19 | return unsafe { |
| 20 | U32_F32{ |
| 21 | f: f |
| 22 | }.u |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // f32_from_bits returns the floating-point number corresponding |
| 27 | // to the IEEE 754 binary representation b, with the sign bit of b |
| 28 | // and the result in the same bit position. |
| 29 | // f32_from_bits(f32_bits(x)) == x. |
| 30 | @[inline] |
| 31 | pub fn f32_from_bits(b u32) f32 { |
| 32 | $if tinyc { |
| 33 | return *unsafe { &f32(&b) } |
| 34 | } |
| 35 | return unsafe { |
| 36 | U32_F32{ |
| 37 | u: b |
| 38 | }.f |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | union U64_F64 { |
| 43 | u u64 |
| 44 | f f64 |
| 45 | } |
| 46 | |
| 47 | // f64_bits returns the IEEE 754 binary representation of f, |
| 48 | // with the sign bit of f and the result in the same bit position, |
| 49 | // and f64_bits(f64_from_bits(x)) == x. |
| 50 | @[inline] |
| 51 | pub fn f64_bits(f f64) u64 { |
| 52 | $if tinyc { |
| 53 | return *unsafe { &u64(&f) } |
| 54 | } |
| 55 | return unsafe { |
| 56 | U64_F64{ |
| 57 | f: f |
| 58 | }.u |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // f64_from_bits returns the floating-point number corresponding |
| 63 | // to the IEEE 754 binary representation b, with the sign bit of b |
| 64 | // and the result in the same bit position. |
| 65 | // f64_from_bits(f64_bits(x)) == x. |
| 66 | @[inline] |
| 67 | pub fn f64_from_bits(b u64) f64 { |
| 68 | $if tinyc { |
| 69 | return *unsafe { &f64(&b) } |
| 70 | } |
| 71 | return unsafe { |
| 72 | U64_F64{ |
| 73 | u: b |
| 74 | }.f |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // with_set_low_word sets low word of `f` to `lo` |
| 79 | @[inline] |
| 80 | pub fn with_set_low_word(f f64, lo u32) f64 { |
| 81 | mut tmp := f64_bits(f) |
| 82 | tmp &= 0xffffffff_00000000 |
| 83 | tmp |= u64(lo) |
| 84 | return f64_from_bits(tmp) |
| 85 | } |
| 86 | |
| 87 | // with_set_high_word sets high word of `f` to `lo` |
| 88 | @[inline] |
| 89 | pub fn with_set_high_word(f f64, hi u32) f64 { |
| 90 | mut tmp := f64_bits(f) |
| 91 | tmp &= 0x00000000_ffffffff |
| 92 | tmp |= u64(hi) << 32 |
| 93 | return f64_from_bits(tmp) |
| 94 | } |
| 95 | |
| 96 | // get_high_word returns high part of the word of `f`. |
| 97 | @[inline] |
| 98 | pub fn get_high_word(f f64) u32 { |
| 99 | return u32(f64_bits(f) >> 32) |
| 100 | } |
| 101 | |