Skip to content

Commit

Permalink
Enable sqrt* intrinsics (#3000)
Browse files Browse the repository at this point in the history
CBMC's sqrt* implementations were fixed in
diffblue/cbmc#8195.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
  • Loading branch information
tautschnig authored Aug 2, 2024
1 parent 343ed8c commit e305471
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/src/rust-feature-support/intrinsics.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ sinf32 | Partial | Results are overapproximated; [this test](https://github.com/
sinf64 | Partial | Results are overapproximated; [this test](https://github.com/model-checking/kani/blob/main/tests/kani/Intrinsics/Math/Trigonometry/sinf64.rs) explains how |
size_of | Yes | |
size_of_val | Yes | |
sqrtf32 | No | |
sqrtf64 | No | |
sqrtf32 | Partial | Results are overapproximated |
sqrtf64 | Partial | Results are overapproximated |
sub_with_overflow | Yes | |
transmute | Partial | Doesn't check [all UB conditions](https://doc.rust-lang.org/nomicon/transmutes.html) |
truncf32 | Yes | |
Expand Down
4 changes: 2 additions & 2 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ impl<'tcx> GotocCtx<'tcx> {
"simd_xor" => codegen_intrinsic_binop!(bitxor),
"size_of" => unreachable!(),
"size_of_val" => codegen_size_align!(size),
"sqrtf32" => unstable_codegen!(codegen_simple_intrinsic!(Sqrtf)),
"sqrtf64" => unstable_codegen!(codegen_simple_intrinsic!(Sqrt)),
"sqrtf32" => codegen_simple_intrinsic!(Sqrtf),
"sqrtf64" => codegen_simple_intrinsic!(Sqrt),
"sub_with_overflow" => self.codegen_op_with_overflow(
BinaryOperator::OverflowResultMinus,
fargs,
Expand Down
24 changes: 24 additions & 0 deletions tests/kani/Intrinsics/Math/Arith/sqrt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

#[kani::proof]
fn verify_sqrt32() {
let positive = 4.0_f32;
let negative_zero = -0.0_f32;

let abs_difference = (positive.sqrt() - 2.0).abs();

assert!(abs_difference <= f32::EPSILON);
assert!(negative_zero.sqrt() == negative_zero);
}

#[kani::proof]
fn verify_sqrt64() {
let positive = 4.0_f64;
let negative_zero = -0.0_f64;

let abs_difference = (positive.sqrt() - 2.0).abs();

assert!(abs_difference <= 1e-10);
assert!(negative_zero.sqrt() == negative_zero);
}

0 comments on commit e305471

Please sign in to comment.