pxfm/
floor.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 4/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 */
29
30/// Round to integer towards minus infinity
31#[inline]
32pub const fn floorf(x: f32) -> f32 {
33    let mut i0 = x.to_bits() as i32;
34    let j0 = ((i0 >> 23) & 0xff) - 0x7f;
35    if j0 < 23 {
36        if j0 < 0 {
37            /* return 0 * sign (x) if |x| < 1  */
38            if i0 >= 0 {
39                i0 = 0;
40            } else if (i0 & 0x7fffffff) != 0 {
41                i0 = 0xbf800000u32 as i32;
42            }
43        } else {
44            let i = (0x007fffff) >> j0;
45            if (i0 & i) == 0 {
46                return x; /* x is integral  */
47            }
48            if i0 < 0 {
49                i0 = i0.wrapping_add((0x00800000) >> j0);
50            }
51            i0 &= !i;
52        }
53    } else {
54        return if j0 == 0x80 {
55            x + x /* inf or NaN  */
56        } else {
57            x /* x is integral  */
58        };
59    }
60    f32::from_bits(i0 as u32)
61}
62
63/// Floors value
64#[inline]
65pub const fn floor(x: f64) -> f64 {
66    let mut i0 = x.to_bits() as i64;
67    let j0 = (((i0 >> 52) & 0x7ff) as i32) - 0x3ff;
68    let mut x = x;
69    if j0 < 52 {
70        if j0 < 0 {
71            /* return 0 * sign (x) if |x| < 1  */
72            if i0 >= 0 {
73                i0 = 0;
74            } else if (i0 & 0x7fffffffffffffffi64) != 0 {
75                i0 = 0xbff0000000000000u64 as i64;
76            }
77        } else {
78            let i = 0x000fffffffffffffi64 >> j0;
79            if (i0 & i) == 0 {
80                return x; /* x is integral */
81            }
82            if i0 < 0 {
83                i0 = i0.wrapping_add((0x0010000000000000u64 >> j0) as i64);
84            }
85            i0 &= !i;
86        }
87        x = f64::from_bits(i0 as u64);
88    } else if j0 == 0x400 {
89        return x + x; /* inf or NaN */
90    }
91    x
92}
93
94pub(crate) trait FloorFinite {
95    fn floor_finite(self) -> Self;
96}
97
98impl FloorFinite for f32 {
99    #[inline]
100    fn floor_finite(self) -> Self {
101        #[cfg(any(
102            all(
103                any(target_arch = "x86", target_arch = "x86_64"),
104                target_feature = "sse4.1"
105            ),
106            target_arch = "aarch64"
107        ))]
108        {
109            self.floor()
110        }
111        #[cfg(not(any(
112            all(
113                any(target_arch = "x86", target_arch = "x86_64"),
114                target_feature = "sse4.1"
115            ),
116            target_arch = "aarch64"
117        )))]
118        {
119            floorf(self)
120        }
121    }
122}
123
124impl FloorFinite for f64 {
125    #[inline]
126    fn floor_finite(self) -> Self {
127        #[cfg(any(
128            all(
129                any(target_arch = "x86", target_arch = "x86_64"),
130                target_feature = "sse4.1"
131            ),
132            target_arch = "aarch64"
133        ))]
134        {
135            self.floor()
136        }
137        #[cfg(not(any(
138            all(
139                any(target_arch = "x86", target_arch = "x86_64"),
140                target_feature = "sse4.1"
141            ),
142            target_arch = "aarch64"
143        )))]
144        {
145            floor(self)
146        }
147    }
148}
149
150#[cfg(test)]
151mod tests {
152    use super::*;
153
154    #[test]
155    fn test_floorf() {
156        assert_eq!(floorf(-1.0), -1.0);
157        assert_eq!(floorf(1.0), 1.0);
158        assert_eq!(floorf(0.0), 0.0);
159        assert_eq!(floorf(0.324), 0.0);
160        assert_eq!(floorf(-0.324), -1.0);
161        assert_eq!(floorf(1.234211), 1.0);
162        assert_eq!(floorf(-1.234211), -2.0);
163        assert_eq!(floorf(f32::INFINITY), f32::INFINITY);
164        assert_eq!(floorf(f32::NEG_INFINITY), f32::NEG_INFINITY);
165        assert!(floorf(f32::NAN).is_nan());
166    }
167
168    #[test]
169    fn test_floor() {
170        assert_eq!(floor(-1.0), -1.0);
171        assert_eq!(floor(1.0), 1.0);
172        assert_eq!(floor(0.0), 0.0);
173        assert_eq!(floor(0.324), 0.0);
174        assert_eq!(floor(-0.324), -1.0);
175        assert_eq!(floor(1.234211), 1.0);
176        assert_eq!(floor(-1.234211), -2.0);
177        assert_eq!(floor(f64::INFINITY), f64::INFINITY);
178        assert_eq!(floor(f64::NEG_INFINITY), f64::NEG_INFINITY);
179        assert!(floor(f64::NAN).is_nan());
180    }
181}