Skip to content

Commit b628a10

Browse files
adpaco-awstedinski
authored andcommitted
Add tests for trigonometric intrinsics (rust-lang#1043)
* Add test for `sinf32` * Add test for `sinf64` * Add test for `cosf32` * Add test for `cosf64` * Minor fix
1 parent b45f72e commit b628a10

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that `cosf32` returns the expected results.
5+
// Note: The CBMC model for this function is an over-approximation that returns:
6+
// * A nondet. value between -1 and 1
7+
// * 1.0 if the argument is 0.0
8+
#![feature(core_intrinsics)]
9+
10+
fn fp_equals(value: f32, expected: f32) -> bool {
11+
let abs_diff = (value - expected).abs();
12+
abs_diff <= f32::EPSILON
13+
}
14+
15+
#[kani::proof]
16+
fn cosine_range() {
17+
let x: f32 = kani::any();
18+
kani::assume(x.is_finite());
19+
let cosine = unsafe { std::intrinsics::cosf32(x) };
20+
assert!(cosine < 1.0 || fp_equals(cosine, 1.0));
21+
assert!(cosine > -1.0 || fp_equals(cosine, -1.0));
22+
}
23+
24+
#[kani::proof]
25+
fn cosine_const() {
26+
let x = 0.0;
27+
let cosine = unsafe { std::intrinsics::cosf32(x) };
28+
assert!(fp_equals(cosine, 1.0));
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that `cosf64` returns the expected results.
5+
// Note: The CBMC model for this function is an over-approximation that returns:
6+
// * A nondet. value between -1 and 1
7+
// * 1.0 if the argument is 0.0
8+
#![feature(core_intrinsics)]
9+
10+
fn fp_equals(value: f64, expected: f64) -> bool {
11+
let abs_diff = (value - expected).abs();
12+
abs_diff <= f64::EPSILON
13+
}
14+
15+
#[kani::proof]
16+
fn cosine_range() {
17+
let x: f64 = kani::any();
18+
kani::assume(x.is_finite());
19+
let cosine = unsafe { std::intrinsics::cosf64(x) };
20+
assert!(cosine < 1.0 || fp_equals(cosine, 1.0));
21+
assert!(cosine > -1.0 || fp_equals(cosine, -1.0));
22+
}
23+
24+
#[kani::proof]
25+
fn cosine_const() {
26+
let x = 0.0;
27+
let cosine = unsafe { std::intrinsics::cosf64(x) };
28+
assert!(fp_equals(cosine, 1.0));
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that `sinf32` returns the expected results.
5+
// Note: The CBMC model for this function is an over-approximation that returns:
6+
// * A nondet. value between -1 and 1
7+
// * 0.0 if the argument is 0.0
8+
#![feature(core_intrinsics)]
9+
10+
fn fp_equals(value: f32, expected: f32) -> bool {
11+
let abs_diff = (value - expected).abs();
12+
abs_diff <= f32::EPSILON
13+
}
14+
15+
#[kani::proof]
16+
fn sine_range() {
17+
let x: f32 = kani::any();
18+
kani::assume(x.is_finite());
19+
let sine = unsafe { std::intrinsics::sinf32(x) };
20+
assert!(sine < 1.0 || fp_equals(sine, 1.0));
21+
assert!(sine > -1.0 || fp_equals(sine, -1.0));
22+
}
23+
24+
#[kani::proof]
25+
fn sine_const() {
26+
let x = 0.0;
27+
let sine = unsafe { std::intrinsics::sinf32(x) };
28+
assert!(fp_equals(sine, 0.0));
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that `sinf64` returns the expected results.
5+
// Note: The CBMC model for this function is an over-approximation that returns:
6+
// * A nondet. value between -1 and 1
7+
// * 0.0 if the argument is 0.0
8+
#![feature(core_intrinsics)]
9+
10+
fn fp_equals(value: f64, expected: f64) -> bool {
11+
let abs_diff = (value - expected).abs();
12+
abs_diff <= f64::EPSILON
13+
}
14+
15+
#[kani::proof]
16+
fn sine_range() {
17+
let x: f64 = kani::any();
18+
kani::assume(x.is_finite());
19+
let sine = unsafe { std::intrinsics::sinf64(x) };
20+
assert!(sine < 1.0 || fp_equals(sine, 1.0));
21+
assert!(sine > -1.0 || fp_equals(sine, -1.0));
22+
}
23+
24+
#[kani::proof]
25+
fn sine_const() {
26+
let x = 0.0;
27+
let sine = unsafe { std::intrinsics::sinf64(x) };
28+
assert!(fp_equals(sine, 0.0));
29+
}

0 commit comments

Comments
 (0)