[Mlir-commits] [mlir] [MLIR][Math] Add erfc to math dialect (PR #126439)
Benoit Jacob
llvmlistbot at llvm.org
Thu Feb 13 17:36:27 PST 2025
================
@@ -1118,6 +1122,101 @@ ErfPolynomialApproximation::matchAndRewrite(math::ErfOp op,
return success();
}
+// Approximates erfc(x) with p((x - 2) / (x + 2)), where p is a 9 degree
+// polynomial.This approximation is based on the following stackoverflow post:
+// https://stackoverflow.com/questions/35966695/vectorizable-implementation-of-complementary-error-function-erfcf
+LogicalResult
+ErfcPolynomialApproximation::matchAndRewrite(math::ErfcOp op,
+ PatternRewriter &rewriter) const {
+ Value x = op.getOperand();
+ Type et = getElementTypeOrSelf(x);
+
+ if (!et.isF32())
+ return rewriter.notifyMatchFailure(op, "only f32 type is supported.");
+ std::optional<VectorShape> shape = vectorShape(x);
+
+ ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
+ auto bcast = [&](Value value) -> Value {
+ return broadcast(builder, value, shape);
+ };
+
+ Value trueValue = bcast(boolCst(builder, true));
+ Value zero = bcast(floatCst(builder, 0.0f, et));
+ Value one = bcast(floatCst(builder, 1.0f, et));
+ Value onehalf = bcast(floatCst(builder, 0.5f, et));
+ Value neg4 = bcast(floatCst(builder, -4.0f, et));
+ Value neg2 = bcast(floatCst(builder, -2.0f, et));
+ Value pos2 = bcast(floatCst(builder, 2.0f, et));
+ Value posInf = bcast(f32FromBits(builder, 0x7f800000u));
+ Value clampVal = bcast(floatCst(builder, 10.0546875f, et));
+
+ // Get abs(x)
+ Value isNegativeArg =
+ builder.create<arith::CmpFOp>(arith::CmpFPredicate::OLT, x, zero);
+ Value negArg = builder.create<arith::NegFOp>(x);
+ Value a = builder.create<arith::SelectOp>(isNegativeArg, negArg, x);
----------------
bjacob wrote:
Use `math.absf`.
https://github.com/llvm/llvm-project/pull/126439
More information about the Mlir-commits
mailing list