pxfm/trunc.rs
1/*
2 * // Copyright (c) Radzivon Bartoshyk 6/2025. All rights reserved.
3 * //
4 * // Redistribution and use in source and binary forms, with or without modification,
5 * // are permitted provided that the following conditions are met:
6 * //
7 * // 1. Redistributions of source code must retain the above copyright notice, this
8 * // list of conditions and the following disclaimer.
9 * //
10 * // 2. Redistributions in binary form must reproduce the above copyright notice,
11 * // this list of conditions and the following disclaimer in the documentation
12 * // and/or other materials provided with the distribution.
13 * //
14 * // 3. Neither the name of the copyright holder nor the names of its
15 * // contributors may be used to endorse or promote products derived from
16 * // this software without specific prior written permission.
17 * //
18 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29use crate::bits::{get_exponent_f32, get_exponent_f64};
30
31#[inline]
32pub const fn truncf(x: f32) -> f32 {
33 // If x is infinity or NaN, return it.
34 // If it is zero also we should return it as is, but the logic
35 // later in this function takes care of it. But not doing a zero
36 // check, we improve the run time of non-zero values.
37 if x.is_infinite() || x.is_nan() {
38 return x;
39 }
40 const FRACTION_LENGTH: u32 = 23;
41 let exponent = get_exponent_f32(x);
42
43 // If the exponent is greater than the most negative mantissa
44 // exponent, then x is already an integer.
45 if exponent >= FRACTION_LENGTH as i32 {
46 return x;
47 }
48
49 // If the exponent is such that abs(x) is less than 1, then return 0.
50 if exponent <= -1 {
51 return if x.is_sign_negative() { -0.0 } else { 0.0 };
52 }
53 const FRACTION_MASK: u32 = (1 << FRACTION_LENGTH) - 1;
54 let trim_size = FRACTION_LENGTH as i32 - exponent;
55 let trunc_mantissa =
56 ((x.to_bits() & FRACTION_MASK) >> trim_size).wrapping_shl(trim_size as u32);
57
58 let prepared_bits = x.to_bits() & 0xFF800000;
59 f32::from_bits(prepared_bits | trunc_mantissa)
60}
61
62#[inline]
63pub const fn trunc(x: f64) -> f64 {
64 // If x is infinity or NaN, return it.
65 // If it is zero also we should return it as is, but the logic
66 // later in this function takes care of it. But not doing a zero
67 // check, we improve the run time of non-zero values.
68 if x.is_infinite() || x.is_nan() {
69 return x;
70 }
71 const FRACTION_LENGTH: u32 = 52;
72 let exponent = get_exponent_f64(x);
73
74 // If the exponent is greater than the most negative mantissa
75 // exponent, then x is already an integer.
76 if exponent >= FRACTION_LENGTH as i64 {
77 return x;
78 }
79
80 // If the exponent is such that abs(x) is less than 1, then return 0.
81 if exponent <= -1 {
82 return if x.is_sign_negative() { -0.0 } else { 0.0 };
83 }
84 const FRACTION_MASK: u64 = (1 << FRACTION_LENGTH) - 1;
85 let trim_size = FRACTION_LENGTH as i64 - exponent;
86 let trunc_mantissa =
87 ((x.to_bits() & FRACTION_MASK) >> trim_size).wrapping_shl(trim_size as u32);
88
89 let prepared_bits = x.to_bits() & 0xFFF0000000000000;
90 f64::from_bits(prepared_bits | trunc_mantissa)
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 #[test]
98 fn test_truncf() {
99 assert_eq!(truncf(-1.0), -1.0);
100 assert_eq!(truncf(1.0), 1.0);
101 assert_eq!(truncf(1.234211), 1.0);
102 assert_eq!(truncf(-1.234211), -1.0);
103 }
104
105 #[test]
106 fn test_trunc() {
107 assert_eq!(trunc(-1.0), -1.0);
108 assert_eq!(trunc(1.0), 1.0);
109 assert_eq!(trunc(1.234211), 1.0);
110 assert_eq!(trunc(-1.234211), -1.0);
111 }
112}