-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable powf*, exp*, log* intrinsics (#2996)
CBMC provides approximating implementations of these. Resolves: #2966
- Loading branch information
1 parent
129375f
commit f08a3e9
Showing
7 changed files
with
129 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// | ||
// This test will trigger use of the `expf32` and `expf64` intrinsics, which in turn invoke | ||
// functions modelled in CBMC's math library. These models use approximations as documented in | ||
// CBMC's source code: https://github.com/diffblue/cbmc/blob/develop/src/ansi-c/library/math.c. | ||
|
||
#[kani::proof] | ||
fn verify_exp32() { | ||
let two = 2.0_f32; | ||
let two_sq = std::f32::consts::E * std::f32::consts::E; | ||
let two_exp = two.exp(); | ||
|
||
assert!((two_sq - two_exp).abs() <= 0.192); | ||
} | ||
|
||
#[kani::proof] | ||
fn verify_exp64() { | ||
let two = 2.0_f64; | ||
let two_sq = std::f64::consts::E * std::f64::consts::E; | ||
let two_exp = two.exp(); | ||
|
||
assert!((two_sq - two_exp).abs() <= 0.192); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// This test will trigger use of the `exp2f32` and `exp2f64` intrinsics, which in turn invoke | ||
// functions modelled in CBMC's math library. These models use approximations as documented in | ||
// CBMC's source code: https://github.com/diffblue/cbmc/blob/develop/src/ansi-c/library/math.c. | ||
|
||
#[kani::proof] | ||
fn verify_exp2_32() { | ||
let two = 2.0_f32; | ||
let two_two = two.exp2(); | ||
|
||
assert!((two_two - 4.0).abs() <= 0.345); | ||
} | ||
|
||
#[kani::proof] | ||
fn verify_exp2_64() { | ||
let two = 2.0_f64; | ||
let two_two = two.exp2(); | ||
|
||
assert!((two_two - 4.0).abs() <= 0.345); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// This test will trigger use of the `logf32` and `logf64` intrinsics, which in turn invoke | ||
// functions modelled in CBMC's math library. These models use approximations as documented in | ||
// CBMC's source code: https://github.com/diffblue/cbmc/blob/develop/src/ansi-c/library/math.c. | ||
|
||
#[kani::proof] | ||
fn verify_logf32() { | ||
let e = std::f32::consts::E; | ||
let e_log = e.ln(); | ||
|
||
assert!((e_log - 1.0).abs() <= 0.058); | ||
} | ||
|
||
#[kani::proof] | ||
fn verify_logf64() { | ||
let e = std::f64::consts::E; | ||
let e_log = e.ln(); | ||
|
||
assert!((e_log - 1.0).abs() <= 0.058); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// This test will trigger use of the `powf32` intrinsic, which in turn invoke functions modelled in | ||
// CBMC's math library. These models use approximations as documented in CBMC's source code: | ||
// https://github.com/diffblue/cbmc/blob/develop/src/ansi-c/library/math.c. | ||
|
||
#[kani::proof] | ||
fn verify_pow() { | ||
let x: f32 = kani::any(); | ||
kani::assume(x.is_normal()); | ||
kani::assume(x > 1.0 && x < u16::MAX.into()); | ||
let x2 = x.powf(2.0); | ||
assert!(x2 >= 0.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// This test will trigger use of the `powf64` intrinsic, which in turn invoke functions modelled in | ||
// CBMC's math library. These models use approximations as documented in CBMC's source code: | ||
// https://github.com/diffblue/cbmc/blob/develop/src/ansi-c/library/math.c. | ||
|
||
pub fn f(a: u64) -> u64 { | ||
const C: f64 = 0.618; | ||
(a as f64).powf(C) as u64 | ||
} | ||
|
||
#[cfg(kani)] | ||
mod verification { | ||
use super::*; | ||
|
||
#[kani::proof] | ||
fn verify_f() { | ||
const LIMIT: u64 = 10; | ||
let x: u64 = kani::any(); | ||
let y: u64 = kani::any(); | ||
// outside these limits our approximation may yield spurious results | ||
kani::assume(x > LIMIT && x < LIMIT * 3); | ||
kani::assume(y > LIMIT && y < LIMIT * 3); | ||
kani::assume(x > y); | ||
let x_ = f(x); | ||
let y_ = f(y); | ||
assert!(x_ >= y_); | ||
} | ||
} |