pbrt_r3/core/misc/
float.rs

1use crate::core::base::Float;
2
3#[inline]
4pub fn float_to_bits_32(f: f32) -> u32 {
5    f.to_bits()
6}
7
8#[inline]
9pub fn bits_to_float_32(ui: u32) -> f32 {
10    f32::from_bits(ui)
11}
12
13#[inline]
14pub fn float_to_bits_64(f: f64) -> u64 {
15    f.to_bits()
16}
17
18#[inline]
19pub fn bits_to_float_64(ui: u64) -> f64 {
20    f64::from_bits(ui)
21}
22
23pub fn next_float_down_32(x: f32) -> f32 {
24    let mut v = x;
25    if f32::is_infinite(v) && v < 0.0 {
26        return v;
27    }
28    if v == 0.0 {
29        v = -0.0;
30    }
31
32    let mut ui = float_to_bits_32(v);
33    if v > 0.0 {
34        ui = ui.saturating_sub(1);
35    } else {
36        ui = ui.saturating_add(1);
37    }
38    return bits_to_float_32(ui);
39}
40
41pub fn next_float_up_32(x: f32) -> f32 {
42    let mut v = x;
43    if f32::is_infinite(v) && v > 0.0 {
44        return v;
45    }
46    if v == -0.0 {
47        v = 0.0;
48    }
49    let mut ui = float_to_bits_32(v);
50    if v >= 0.0 {
51        ui = ui.saturating_add(1);
52    } else {
53        ui = ui.saturating_sub(1);
54    }
55    return bits_to_float_32(ui);
56}
57
58pub fn next_float_down_64(x: f64) -> f64 {
59    let mut v = x;
60    if f64::is_infinite(v) && v < 0.0 {
61        return v;
62    }
63    if v == 0.0 {
64        v = -0.0;
65    }
66
67    let mut ui = float_to_bits_64(v);
68    if v > 0.0 {
69        ui = ui.saturating_sub(1);
70    } else {
71        ui = ui.saturating_add(1);
72    }
73    return bits_to_float_64(ui);
74}
75
76pub fn next_float_up_64(x: f64) -> f64 {
77    let mut v = x;
78    if f64::is_infinite(v) && v > 0.0 {
79        return v;
80    }
81    if v == -0.0 {
82        v = 0.0;
83    }
84    let mut ui = float_to_bits_64(v);
85    if v >= 0.0 {
86        ui = ui.saturating_add(1);
87    } else {
88        ui = ui.saturating_sub(1);
89    }
90    return bits_to_float_64(ui);
91}
92
93pub fn next_float_down(x: Float) -> Float {
94    #[cfg(not(feature = "float-as-double"))]
95    return next_float_down_32(x);
96    #[cfg(feature = "float-as-double")]
97    return next_float_down_64(x);
98}
99
100pub fn next_float_up(x: Float) -> Float {
101    #[cfg(not(feature = "float-as-double"))]
102    return next_float_up_32(x);
103    #[cfg(feature = "float-as-double")]
104    return next_float_up_64(x);
105}