[flang-commits] [flang] [flang] Fix for atand(Y, X), and implment atan2d(Y, X), atanpi(X), atanpi(Y, X), atan2pi(Y, X) (PR #79002)

Yi Wu via flang-commits flang-commits at lists.llvm.org
Wed Jan 31 02:44:35 PST 2024


================
@@ -2128,20 +2131,78 @@ IntrinsicLibrary::genAny(mlir::Type resultType,
   return readAndAddCleanUp(resultMutableBox, resultType, "ANY");
 }
 
+void static atanNoneZeroCheck(mlir::Value y, mlir::Value x,
+                              fir::FirOpBuilder &builder, mlir::Location loc) {
+  // When Y == 0 X must not be 0
+  mlir::Value zero = builder.createRealZeroConstant(loc, y.getType());
+  mlir::Value cmpYEq0 = builder.create<mlir::arith::CmpFOp>(
+      loc, mlir::arith::CmpFPredicate::UEQ, y, zero);
+  mlir::Value cmpXEq0 = builder.create<mlir::arith::CmpFOp>(
+      loc, mlir::arith::CmpFPredicate::UEQ, x, zero);
+  mlir::Value terminationCheck =
+      builder.create<mlir::arith::AndIOp>(loc, cmpYEq0, cmpXEq0);
+  builder.genIfThenElse(loc, terminationCheck)
+      .genThen([&]() {
+        fir::runtime::genReportFatalUserError(builder, loc,
+                                              "When Y == 0 X must not be 0");
+      })
+      .end();
+}
+
+// ATAND, ATAN2D
 mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
                                        llvm::ArrayRef<mlir::Value> args) {
-  assert(args.size() == 1);
+  // assert for: atand(X), atand(Y,X), atan2d(Y,X)
+  assert(args.size() >= 1 && args.size() <= 2);
+
   mlir::MLIRContext *context = builder.getContext();
-  mlir::FunctionType ftype =
-      mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
-  mlir::Value atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
+  mlir::Value atan;
+
+  // atand(Y,X) atan2d(Y,X) == atan2(Y,X) * 180/pi
+  if (args.size() == 2) {
+    mlir::Value y = fir::getBase(args[0]);
+    mlir::Value x = fir::getBase(args[1]);
+    atanNoneZeroCheck(y, x, builder, loc);
----------------
yi-wu-arm wrote:

Just checked in the math dialect:
https://github.com/llvm/llvm-project/blob/0217d2e089afba8ca33330713787521ba52a1056/mlir/lib/Dialect/Math/IR/MathOps.cpp#L159-L161
while in practice (when testing with both y and x equal to 0), it returns 0.
So its probably fine to remove the `atanNoneZeroCheck` here?

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


More information about the flang-commits mailing list