[Mlir-commits] [mlir] [mlir][math] Add Polynomial Approximation for acos, asin op (PR #90962)
Han-Chung Wang
llvmlistbot at llvm.org
Tue May 7 09:36:36 PDT 2024
================
@@ -821,6 +821,153 @@ Log1pApproximation::matchAndRewrite(math::Log1pOp op,
return success();
}
+//----------------------------------------------------------------------------//
+// Asin approximation.
+//----------------------------------------------------------------------------//
+
+// Approximates asin(x).
+// This approximation is based on the following stackoverflow post:
+// https://stackoverflow.com/a/42683455
+namespace {
+struct AsinPolynomialApproximation : public OpRewritePattern<math::AsinOp> {
+public:
+ using OpRewritePattern::OpRewritePattern;
+
+ LogicalResult matchAndRewrite(math::AsinOp op,
+ PatternRewriter &rewriter) const final;
+};
+} // namespace
+LogicalResult
+AsinPolynomialApproximation::matchAndRewrite(math::AsinOp op,
+ PatternRewriter &rewriter) const {
+ Value operand = op.getOperand();
+ Type elementType = getElementTypeOrSelf(operand);
+
+ if (!(elementType.isF32() || elementType.isF16()))
+ return rewriter.notifyMatchFailure(op,
+ "only f32 and f16 type is supported.");
+ VectorShape shape = vectorShape(operand);
+
+ ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
+ auto bcast = [&](Value value) -> Value {
+ return broadcast(builder, value, shape);
+ };
+
+ auto fma = [&](Value a, Value b, Value c) -> Value {
+ return builder.create<math::FmaOp>(a, b, c);
+ };
+
+ auto mul = [&](Value a, Value b) -> Value {
+ return builder.create<arith::MulFOp>(a, b);
+ };
+
+ Value s = mul(operand, operand);
+ Value q = mul(s, s);
+ Value r = bcast(floatCst(builder, 5.5579749017470502e-2, elementType));
+ Value t = bcast(floatCst(builder, -6.2027913464120114e-2, elementType));
+
+ r = fma(r, q, bcast(floatCst(builder, 5.4224464349245036e-2, elementType)));
----------------
hanhanW wrote:
Thanks, can you add it to the PR description?
https://github.com/llvm/llvm-project/pull/90962
More information about the Mlir-commits
mailing list