# compiler-rt: fix f80 ceil/floor optimization · gitcafe/zig

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

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

Visibility: public

Requested revision: 938efe4aab09b5806cd3e9903619b95a5f122f36

Requested commit: 938efe4aab09b5806cd3e9903619b95a5f122f36

Commit: 938efe4aab09b5806cd3e9903619b95a5f122f36

Tree: cbd969a877bc2590ca212d4b5ba17eb3c711e636

Author: David Rubin

Committer: Andrew Kelley

## Message

```
compiler-rt: fix f80 ceil/floor optimization

Our implementation did the classic add-sub rounding trick `(y = x +/- C =+ C - x)`
with `C = 1 / eps(T) = 2^(mantissa - 1)`. This approach only works for values whose
magnitude is below the rounding capacity of the constant. For a 64-bit mantissa
(like f80 has), `C = 2^63` only rounds for `|x| < 2^63`. Before we allowed this to
be ran on `e < bias + 64` aka `|x| < 2^64`. And because it isn't large enough,
we lose a bit to rounding.

For reference, the musl implementation does the same thing, using `mantissa - 1`:
https://git.musl-libc.org/cgit/musl/tree/src/math/ceill.c#n18
where `LDBL_MANT_DIG` is 64 for `long double` on x86.

This commit also combines the floor and ceil implementations into one generic one.

```

## Parents

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

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