[PATCH] D73607: [X86] Custom lower ISD::FROUND with SSE4.1 to avoid a libcall.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 29 00:05:12 PST 2020


craig.topper created this revision.
craig.topper added reviewers: spatel, RKSimon.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
craig.topper added reviewers: andrew.w.kaylor, efriedma, uweigand, kpn, cameron.mcinally.

ISD::FROUND is defined to round to nearest with ties rounding
away from 0. This mode isn't supported in hardware on X86.

But as long as we aren't compiling with trapping math, we can
emulate this with floor(X + copysign(nextafter(0.5, 0.0), X)).

We have to use nextafter to avoid some corner cases that adding
0.5 would have. For example, if X is nextafter(0.5, 0.0) it should
round to 0.0, but adding 0.5 would need one extra bit of mantissa
than can be stored so it rounds to 1.0. Adding nextafter(0.5, 0.0)
instead will just increase the exponent by 1 and leave the mantissa
as all 1s. This would be nextafter(1.0, 0.0) which will floor to 0.0.

Techically this requires -fno-trapping-math which isn't our default.
But if we care about exceptions we should be using constrained
intrinsics. Constrained intrinsics would use STRICT_FROUND which
won't go through this code.

Fixes PR42195.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73607

Files:
  llvm/lib/Target/X86/X86ISelLowering.cpp
  llvm/test/CodeGen/X86/extractelement-fp.ll
  llvm/test/CodeGen/X86/fp-round.ll
  llvm/test/CodeGen/X86/vec-libcalls.ll
  llvm/test/CodeGen/X86/vec_round.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73607.241058.patch
Type: text/x-patch
Size: 33664 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200129/26e806a2/attachment-0001.bin>


More information about the llvm-commits mailing list