[Mlir-commits] [mlir] [MLIR][Math] Add erfc to math dialect (PR #126439)

Benoit Jacob llvmlistbot at llvm.org
Fri Feb 14 07:42:15 PST 2025


================
@@ -318,6 +318,24 @@ OpFoldResult math::ErfOp::fold(FoldAdaptor adaptor) {
       });
 }
 
+//===----------------------------------------------------------------------===//
+// ErfcOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::ErfcOp::fold(FoldAdaptor adaptor) {
+  return constFoldUnaryOpConditional<FloatAttr>(
+      adaptor.getOperands(), [](const APFloat &a) -> std::optional<APFloat> {
+        switch (a.getSizeInBits(a.getSemantics())) {
+        case 64:
+          return APFloat(erfc(a.convertToDouble()));
+        case 32:
----------------
bjacob wrote:

Sorry, I meant something more specific to the code being added here.

Instead of switching on the size in bits, switch on the semantics itself.

Reading APFloat.h, I see that there is:

```
  static Semantics SemanticsToEnum(const llvm::fltSemantics &Sem);
```

so here you could do:

```c++
switch (APFloat::SemanticsToEnum(a.getSemantics)) {
```

and have your `case:` statement based on enumerators. 

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


More information about the Mlir-commits mailing list