-
Emulate rounding to the nearest integer. · f7b75889Nicolas Capens authored
This implementation works by adding a large value which makes the fractional part no longer fit in the mantissa, and then subtracting it again. It matches nearbyint() for values up to 2^22, positive or negative. The 'magic number' of 0x00C00000 is derived by first observing that the integer values 0x00800000 to 0x00FFFFFF can be represented exactly in single-precision floating-point format but can't have a fractional part because there are 24 mantissa bits (the top one being hidden). So when adding 0x00800000 to for example 0.6, it forces the hardware to round it to the nearest representable integer, being 0x00800001. Subtracting 0x00800000 again gives us 1.0. This works for rounding any value from 0.0 to 0x007FFFFF. However, it doesn't work for negative values, because the intermediate result would be less than 0x00800000 and thus leave some room for fractional bits in the mantissa. The solution is to use 0x00C00000 instead so the range gets split between positive and negative values. Note that values greater than the upper bound will still round to integers, but not the nearest ones, while values less than the lower bound can result in fractional values. Bug b/37495485 Change-Id: I1aed2d831269fcf21b8d3313856a9b9756a532ef Reviewed-on: https://swiftshader-review.googlesource.com/9488Reviewed-by:
Nicolas Capens <capn@google.com> Reviewed-by:
Corentin Wallez <cwallez@google.com> Tested-by:
Nicolas Capens <capn@google.com>
f7b75889
×