Skip to content

Commit 8692109

Browse files
committed
WIP tests for NaN with rounding functions
1 parent b68c7ee commit 8692109

8 files changed

Lines changed: 79 additions & 0 deletions

File tree

libm/src/math/ceil.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ mod tests {
6262
(-1.0, -1.0, Status::OK),
6363
(<$f>::INFINITY, <$f>::INFINITY, Status::OK),
6464
(<$f>::NEG_INFINITY, <$f>::NEG_INFINITY, Status::OK),
65+
(<$f>::NAN, <$f>::NAN, Status::OK),
66+
(<$f>::NEG_NAN, <$f>::NEG_NAN, Status::OK),
67+
// FIXME(snan): these should technically quiet the sNaN and return INVALID.
68+
(<$f>::SNAN, <$f>::SNAN, Status::OK),
69+
(<$f>::NEG_SNAN, <$f>::NEG_SNAN, Status::OK),
6570
// with rounding
6671
(0.1, 1.0, Status::INEXACT),
6772
(-0.1, -0.0, Status::INEXACT),

libm/src/math/floor.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ mod tests {
6262
(-1.0, -1.0, Status::OK),
6363
(<$f>::INFINITY, <$f>::INFINITY, Status::OK),
6464
(<$f>::NEG_INFINITY, <$f>::NEG_INFINITY, Status::OK),
65+
(<$f>::NAN, <$f>::NAN, Status::OK),
66+
(<$f>::NEG_NAN, <$f>::NEG_NAN, Status::OK),
67+
// FIXME(snan): these should technically quiet the sNaN and return INVALID.
68+
(<$f>::SNAN, <$f>::SNAN, Status::OK),
69+
(<$f>::NEG_SNAN, <$f>::NEG_SNAN, Status::OK),
6570
// with rounding
6671
(0.1, 0.0, Status::INEXACT),
6772
(-0.1, -1.0, Status::INEXACT),

libm/src/math/generic/round.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
use super::{copysign, trunc_status};
22
use crate::support::{Float, MinInt};
3+
use crate::support::Hex;
4+
5+
extern crate std;
6+
use std::dbg;
37

48
#[inline]
59
pub fn round<F: Float>(x: F) -> F {
610
let f0p5 = F::from_parts(false, F::EXP_BIAS - 1, F::Int::ZERO); // 0.5
711
let f0p25 = F::from_parts(false, F::EXP_BIAS - 2, F::Int::ZERO); // 0.25
812

13+
dbg!(Hex(x));
14+
dbg!(Hex(copysign(f0p5 - f0p25 * F::EPSILON, x)));
15+
dbg!(Hex(x + copysign(f0p5 - f0p25 * F::EPSILON, x)));
16+
917
trunc_status(x + copysign(f0p5 - f0p25 * F::EPSILON, x)).val
1018
}

libm/src/math/generic/trunc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ use crate::support::{Float, FpResult, Int, IntTy, MinInt, Status};
66
#[inline]
77
pub fn trunc_status<F: Float>(x: F) -> FpResult<F> {
88
let xi: F::Int = x.to_bits();
9+
extern crate std;
10+
use std::dbg;
911
let e: i32 = x.exp_unbiased();
12+
dbg!(e);
13+
dbg!(F::SIG_BITS);
1014

1115
// The represented value has no fractional part, so no truncation is needed
1216
if e >= F::SIG_BITS as i32 {

libm/src/math/rint.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ mod tests {
6666
(-1.0, -1.0, Status::OK),
6767
(<$f>::INFINITY, <$f>::INFINITY, Status::OK),
6868
(<$f>::NEG_INFINITY, <$f>::NEG_INFINITY, Status::OK),
69+
// FIXME: these should return OK status
70+
(<$f>::NAN, <$f>::NAN, Status::INEXACT),
71+
(<$f>::NEG_NAN, <$f>::NEG_NAN, Status::INEXACT),
72+
// FIXME(snan): these should technically quiet the sNaN and return INVALID.
73+
(<$f>::SNAN, <$f>::SNAN, Status::INEXACT),
74+
(<$f>::NEG_SNAN, <$f>::NEG_SNAN, Status::INEXACT),
6975
// with rounding
7076
(0.1, 0.0, Status::INEXACT),
7177
(-0.1, -0.0, Status::INEXACT),

libm/src/math/round.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ mod tests {
6666
let val = f(x);
6767
assert_biteq!(val, exp_res, "round({x:?}) {}", Hex(x));
6868
}
69+
70+
assert!(f(F::NAN).is_qnan());
71+
assert!(f(F::NEG_NAN).is_qnan());
72+
assert!(f(F::SNAN).is_qnan());
73+
assert!(f(F::NEG_SNAN).is_qnan());
6974
}
7075

7176
#[test]

libm/src/math/trunc.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ mod tests {
6161
(-1.0, -1.0, Status::OK),
6262
(<$f>::INFINITY, <$f>::INFINITY, Status::OK),
6363
(<$f>::NEG_INFINITY, <$f>::NEG_INFINITY, Status::OK),
64+
(<$f>::NAN, <$f>::NAN, Status::OK),
65+
(<$f>::NEG_NAN, <$f>::NEG_NAN, Status::OK),
66+
// FIXME(snan): these should technically quiet the sNaN and return INVALID.
67+
(<$f>::SNAN, <$f>::SNAN, Status::OK),
68+
(<$f>::NEG_SNAN, <$f>::NEG_SNAN, Status::OK),
6469
// with rounding
6570
(0.1, 0.0, Status::INEXACT),
6671
(-0.1, -0.0, Status::INEXACT),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
thread 'rustc' panicked at /rustc-dev/bcded331651b60a0383b3ff51db4f24c4495ac53/compiler/rustc_middle/src/query/on_disk_cache.rs:663:9:
2+
cannot decode `AttrId` with `CacheDecoder`
3+
stack backtrace:
4+
0: 0x77d8101b906b - <std[7d30a76dffadf049]::backtrace::Backtrace>::create
5+
1: 0x77d8101b8fb5 - <std[7d30a76dffadf049]::backtrace::Backtrace>::force_capture
6+
2: 0x77d80f1d3c8b - std[7d30a76dffadf049]::panicking::update_hook::<alloc[e0b931b7d0af5c50]::boxed::Box<rustc_driver_impl[5448826ccd3e97a1]::install_ice_hook::{closure#1}>>::{closure#0}
7+
3: 0x77d8101cbb62 - std[7d30a76dffadf049]::panicking::panic_with_hook
8+
4: 0x77d8101ae60a - std[7d30a76dffadf049]::panicking::panic_handler::{closure#0}
9+
5: 0x77d8101a2b39 - std[7d30a76dffadf049]::sys::backtrace::__rust_end_short_backtrace::<std[7d30a76dffadf049]::panicking::panic_handler::{closure#0}, !>
10+
6: 0x77d8101b003d - __rustc[a638a912b495cae0]::rust_begin_unwind
11+
7: 0x77d80cca951c - core[623525b5c18656f3]::panicking::panic_fmt
12+
8: 0x77d811d4b22d - <rustc_middle[6f95cc988acd69b2]::query::on_disk_cache::OnDiskCache>::load_side_effect
13+
9: 0x77d811d4c78d - <rustc_query_impl[44740c00700bfba1]::dep_kind_vtables::non_query::SideEffect::{closure#0} as core[623525b5c18656f3]::ops::function::FnOnce<(rustc_middle[6f95cc988acd69b2]::ty::context::TyCtxt, rustc_middle[6f95cc988acd69b2]::dep_graph::dep_node::DepNode, rustc_middle[6f95cc988acd69b2]::dep_graph::serialized::SerializedDepNodeIndex)>>::call_once
14+
10: 0x77d8108b8cb4 - <rustc_middle[6f95cc988acd69b2]::dep_graph::graph::DepGraphData>::try_mark_previous_green
15+
11: 0x77d8108b5e69 - <rustc_middle[6f95cc988acd69b2]::dep_graph::graph::DepGraph>::try_mark_green
16+
12: 0x77d8108b5cae - rustc_query_impl[44740c00700bfba1]::execution::ensure_can_skip_execution::<rustc_middle[6f95cc988acd69b2]::query::caches::DefaultCache<rustc_span[5cb0e44dc3869b0b]::def_id::LocalModDefId, rustc_middle[6f95cc988acd69b2]::query::erase::ErasedData<[u8; 0usize]>>>
17+
13: 0x77d8118023b4 - rustc_query_impl[44740c00700bfba1]::query_impl::check_mod_deathness::execute_query_incr::__rust_end_short_backtrace
18+
14: 0x77d811801e63 - rustc_interface[aed5607d16a7f406]::passes::analysis::{closure#0}::{closure#0}::{closure#1}
19+
15: 0x77d811800962 - rustc_data_structures[94965839176f1595]::sync::parallel::par_fns
20+
16: 0x77d8118008fc - rustc_interface[aed5607d16a7f406]::passes::analysis::{closure#0}::{closure#0}
21+
17: 0x77d811800962 - rustc_data_structures[94965839176f1595]::sync::parallel::par_fns
22+
18: 0x77d8108b9ca3 - rustc_interface[aed5607d16a7f406]::passes::analysis
23+
19: 0x77d811dadabe - rustc_query_impl[44740c00700bfba1]::execution::try_execute_query::<rustc_middle[6f95cc988acd69b2]::query::caches::SingleCache<rustc_middle[6f95cc988acd69b2]::query::erase::ErasedData<[u8; 0usize]>>, true>
24+
20: 0x77d811dad42a - rustc_query_impl[44740c00700bfba1]::query_impl::analysis::execute_query_incr::__rust_end_short_backtrace
25+
21: 0x77d811a8309e - rustc_interface[aed5607d16a7f406]::interface::run_compiler::<(), rustc_driver_impl[5448826ccd3e97a1]::run_compiler::{closure#0}>::{closure#1}
26+
22: 0x77d811a386fe - std[7d30a76dffadf049]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[aed5607d16a7f406]::util::run_in_thread_with_globals<rustc_interface[aed5607d16a7f406]::util::run_in_thread_pool_with_globals<rustc_interface[aed5607d16a7f406]::interface::run_compiler<(), rustc_driver_impl[5448826ccd3e97a1]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
27+
23: 0x77d811a38fa0 - <std[7d30a76dffadf049]::thread::lifecycle::spawn_unchecked<rustc_interface[aed5607d16a7f406]::util::run_in_thread_with_globals<rustc_interface[aed5607d16a7f406]::util::run_in_thread_pool_with_globals<rustc_interface[aed5607d16a7f406]::interface::run_compiler<(), rustc_driver_impl[5448826ccd3e97a1]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[623525b5c18656f3]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
28+
24: 0x77d811a39eac - <std[7d30a76dffadf049]::sys::thread::unix::Thread>::new::thread_start
29+
25: 0x77d80b2a3d64 - start_thread
30+
at ./nptl/pthread_create.c:448:8
31+
26: 0x77d80b3373fc - __GI___clone3
32+
at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:78:0
33+
27: 0x0 - <unknown>
34+
35+
36+
rustc version: 1.96.0-nightly (bcded3316 2026-04-06)
37+
platform: x86_64-unknown-linux-gnu
38+
39+
query stack during panic:
40+
#0 [analysis] running analysis passes on crate `libm`
41+
end of query stack

0 commit comments

Comments
 (0)