[Mlir-commits] [mlir] [mlir][arith] Match folding of `arith.remf` to `llvm.frem` semantics (PR #96537)

Felix Schneider llvmlistbot at llvm.org
Tue Jun 25 06:47:08 PDT 2024


ubfx wrote:

> In SPIR-V, we have `OpFRem` and `OpFMod`, with the former matching the sign of the lhs operand, and the latter matching the sign of the rhs operand

Thank you for pointing me to this.

> I think this is consistent with the updated fold, but could you double-check that this is truly the case?

This seems to be correct to me:

`llvm.frem` by definition behaves like libm's fmod(), which is defined like this:
> These functions compute the floating-point remainder of dividing x by y.  The return value is x - n * y, where n is the quotient of x / y, rounded toward zero to an integer.

Rounding towards zero means that abs(n*y) <= abs(x) such that sgn(x-n*y) = sgn(x). This matches the behavior of OpFRem as well, i.e. "sign matches the sign of Operand 1". Finally, APFloat::mod() also references fmod() and behaves like this.
https://github.com/llvm/llvm-project/blob/main/llvm/lib/Support/APFloat.cpp#L2226

https://github.com/llvm/llvm-project/pull/96537


More information about the Mlir-commits mailing list