Skip to content

fix saturating float-to-int cast#4573

Open
Rexicon226 wants to merge 1 commit intomodel-checking:mainfrom
Rexicon226:fix-float-cast
Open

fix saturating float-to-int cast#4573
Rexicon226 wants to merge 1 commit intomodel-checking:mainfrom
Rexicon226:fix-float-cast

Conversation

@Rexicon226
Copy link
Copy Markdown

The issue is that Kani computes int_max_as_float by casting u64::MAX to f64. But u64::MAX (2^64 - 1) isn't exactly representable in f64, so it rounds up to 2^64. The old code used a greater-than operation, which would mean a float value equal to exactly 2^64 fails this check, falls through the raw truncation path, and produces a wrong result instead of saturating to u64::MAX.

Instead we use greater-than-or-equal so that the boundary value 2^64 correctly saturates. This is safe for types where the max IS exactly representable.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@Rexicon226 Rexicon226 requested a review from a team as a code owner April 8, 2026 17:46
@github-actions github-actions bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Apr 8, 2026
Comment thread kani-compiler/src/codegen_cprover_gotoc/codegen/rvalue.rs
@Rexicon226 Rexicon226 force-pushed the fix-float-cast branch 2 times, most recently from 457b38e to c7bbcac Compare April 10, 2026 02:23
Comment thread tests/kani/Cast/float_to_int_saturating.rs
The issue is that Kani computes int_max_as_float by casting u64::MAX
to f64. But u64::MAX (2^64 - 1) isn't exactly representable in f64,
so it rounds *up* to 2^64. The old code used a greater-than operation,
which would mean a float value equal to exactly 2^64 fails this check,
falls through the raw truncation path, and produces a wrong result
instead of saturating to u64::MAX.

Instead we use greater-than-or-equal so that the boundary value
2^64 correctly saturates. This is safe for types where the max IS
exactly representable.
@Rexicon226
Copy link
Copy Markdown
Author

The remaining failures don't seem related to this PR?

@Rexicon226
Copy link
Copy Markdown
Author

@feliperodri sorry to bother, but any progress?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants