# compiler: fix \`@intFromFloat\` safety check · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/6ffa285fc35651cb7f4b738a0f6e84718b821fd8)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 6ffa285fc35651cb7f4b738a0f6e84718b821fd8

Requested commit: 6ffa285fc35651cb7f4b738a0f6e84718b821fd8

Commit: 6ffa285fc35651cb7f4b738a0f6e84718b821fd8

Tree: 5c1307e2cfc9d6becd2bbcbdcd64979ca5dab5d9

Author: mlugg

Committer: Jacob Young

## Message

```
compiler: fix `@intFromFloat` safety check

This safety check was completely broken; it triggered unchecked illegal
behavior *in order to implement the safety check*. You definitely can't
do that! Instead, we must explicitly check the boundaries. This is a
tiny bit fiddly, because we need to make sure we do floating-point
rounding in the correct direction, and also handle the fact that the
operation truncates so the boundary works differently for min vs max.

Instead of implementing this safety check in Sema, there are now
dedicated AIR instructions for safety-checked intfromfloat (two
instructions; which one is used depends on the float mode). Currently,
no backend directly implements them; instead, a `Legalize.Feature` is
added which expands the safety check, and this feature is enabled for
all backends we currently test, including the LLVM backend.

The `u0` case is still handled in Sema, because Sema needs to check for
that anyway due to the comptime-known result. The old safety check here
was also completely broken and has therefore been rewritten. In that
case, we just check for 'abs(input) < 1.0'.

I've added a bunch of test coverage for the boundary cases of
`@intFromFloat`, both for successes (in `test/behavior/cast.zig`) and
failures (in `test/cases/safety/`).

Resolves: #24161

```

## Parents

- [6b41beb370ce62b4381396e0543e6eb7f70b18cf](https://git.cafe/gitcafe/zig/commit/6b41beb370ce62b4381396e0543e6eb7f70b18cf?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/6ffa285fc35651cb7f4b738a0f6e84718b821fd8?format=markdown)
