pxfm/
ceil.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 ceilf(x: f32) -> f32 {
33    // If x is infinity NaN or zero, return it.
34    if !x.is_normal() {
35        return x;
36    }
37
38    let is_neg = x.is_sign_negative();
39    let exponent = get_exponent_f32(x);
40
41    // If the exponent is greater than the most negative mantissa
42    // exponent, then x is already an integer.
43    const FRACTION_LENGTH: u32 = 23;
44    if exponent >= FRACTION_LENGTH as i32 {
45        return x;
46    }
47
48    if exponent <= -1 {
49        return if is_neg { -0.0 } else { 1.0 };
50    }
51
52    let trim_size = (FRACTION_LENGTH as i32).wrapping_sub(exponent);
53    let x_u = x.to_bits();
54    let trunc_u = x_u
55        .wrapping_shr(trim_size as u32)
56        .wrapping_shl(trim_size as u32);
57
58    // If x is already an integer, return it.
59    if trunc_u == x_u {
60        return x;
61    }
62
63    let trunc_value = f32::from_bits(trunc_u);
64
65    // If x is negative, the ceil operation is equivalent to the trunc operation.
66    if is_neg {
67        return trunc_value;
68    }
69
70    trunc_value + 1.0
71}
72
73#[inline]
74pub const fn ceil(x: f64) -> f64 {
75    // If x is infinity NaN or zero, return it.
76    if !x.is_normal() {
77        return x;
78    }
79
80    let is_neg = x.is_sign_negative();
81    let exponent = get_exponent_f64(x);
82
83    // If the exponent is greater than the most negative mantissa
84    // exponent, then x is already an integer.
85    const FRACTION_LENGTH: u64 = 52;
86    if exponent >= FRACTION_LENGTH as i64 {
87        return x;
88    }
89
90    if exponent <= -1 {
91        return if is_neg { -0.0 } else { 1.0 };
92    }
93
94    let trim_size = (FRACTION_LENGTH as i64).wrapping_sub(exponent);
95    let x_u = x.to_bits();
96    let trunc_u = x_u
97        .wrapping_shr(trim_size as u32)
98        .wrapping_shl(trim_size as u32);
99
100    // If x is already an integer, return it.
101    if trunc_u == x_u {
102        return x;
103    }
104
105    let trunc_value = f64::from_bits(trunc_u);
106
107    // If x is negative, the ceil operation is equivalent to the trunc operation.
108    if is_neg {
109        return trunc_value;
110    }
111
112    trunc_value + 1.0
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn test_ceilf() {
121        assert_eq!(ceilf(0.0), 0.0);
122        assert_eq!(ceilf(10.0), 10.0);
123        assert_eq!(ceilf(10.1), 11.0);
124        assert_eq!(ceilf(-9.0), -9.0);
125        assert_eq!(ceilf(-9.5), -9.0);
126    }
127
128    #[test]
129    fn test_ceil() {
130        assert_eq!(ceil(0.0), 0.0);
131        assert_eq!(ceil(10.0), 10.0);
132        assert_eq!(ceil(10.1), 11.0);
133        assert_eq!(ceil(-9.0), -9.0);
134        assert_eq!(ceil(-9.5), -9.0);
135    }
136}