afonso360 opened issue #4463:
Feature
We are missing a
frem x, y
instruction providing the floating-point remainder of x/y.Benefit
This is a strange omission to our instruction set. We have
srem
andurem
for integers and the rest of the commonly available floating point operations but no remainder.Some of our users are already lowering it to libcall's themselves:
Implementation
Currently the idea would be to implement this as a libcall on all 3 backends, this seems to be what llvm does (on x86 and aarch64 at least) and should be fairly easy to implement.
I'd be available to work on this.
Alternatives
We can ignore this and let implementers lower it to a libcall themselves, which is what is already happening in practice.
sunfishcode commented on issue #4463:
I can give some background on why
frem
was not originally included."Remainder" isn't a natural operation for floating-point values. With integers, division rounds to the nearest integer toward zero, so there is an inherent quantity which is left over from division, which we call the remainder. In floating point, division rounds to the nearest representable value in the floating point type, so there is no representable quantity which is left over from division.
Consequently, it's somewhat ambiguous what "remainder" for floating point even means. There isn't a single common definition in common use. The thing LLVM calls "frem" is ends up being different from the IEEE 754
remainder
operation and the Cremainder
function. It does have uses, but they're uncommon in practice, and far less common than operations likesin
orcos
, for example. It isn't present in any modern hardware. It will probably always be implemented as a library call, and it will always have subtle behavior.I witnessed the existence of
frem
in LLVM contribute to a high-level source language deciding to support%
on floating-point values in the language, and considered that to have been a mistake.
afonso360 commented on issue #4463:
Thanks for clarifying! I didn't know there was such a disagreement about the implementation of the remainder.
afonso360 closed issue #4463:
Feature
We are missing a
frem x, y
instruction providing the floating-point remainder of x/y.Benefit
This is a strange omission to our instruction set. We have
srem
andurem
for integers and the rest of the commonly available floating point operations but no remainder.Some of our users are already lowering it to libcall's themselves:
Implementation
Currently the idea would be to implement this as a libcall on all 3 backends, this seems to be what llvm does (on x86 and aarch64 at least) and should be fairly easy to implement.
I'd be available to work on this.
Alternatives
We can ignore this and let implementers lower it to a libcall themselves, which is what is already happening in practice.
Last updated: Dec 23 2024 at 12:05 UTC