Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions mlir/include/mlir/Dialect/Math/IR/MathOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,45 @@ def Math_CoshOp : Math_FloatUnaryOp<"cosh"> {
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
// SignIOp
//===----------------------------------------------------------------------===//

def Math_SignIOp : Math_IntegerUnaryOp<"signi"> {
let summary = "ternary-valued sign of an integer";
let description = [{
The `signi` operation computes -1 if the operand is negative, 0 if it is
zero, and 1 if it is positive.

Example:

```mlir
%a = math.signi %b : i64
```
}];
let hasFolder = 1;
}


//===----------------------------------------------------------------------===//
// SignFOp
//===----------------------------------------------------------------------===//

def Math_SignFOp : Math_FloatUnaryOp<"signf"> {
let summary = "ternary-valued sign of a floating point value";
let description = [{
The `signf` operation computes -1.0 if the operand is negative, 0.0 if it
is zero, 1.0 if it is positive, and `NaN` if the operand is `NaN`.

Example:

```mlir
%a = math.signf %b : f32
```
}];
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
// SinOp
//===----------------------------------------------------------------------===//
Expand Down
28 changes: 28 additions & 0 deletions mlir/lib/Dialect/Math/IR/MathOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,34 @@ OpFoldResult math::SinOp::fold(FoldAdaptor adaptor) {
});
}

//===----------------------------------------------------------------------===//
// SignIOp folder
//===----------------------------------------------------------------------===//

OpFoldResult math::SignIOp::fold(FoldAdaptor adaptor) {
return constFoldUnaryOp<IntegerAttr>(
adaptor.getOperands(), [](const APInt &a) {
return a.isNegative() ? APInt(a.getBitWidth(), -1)
: (a.isZero() ? APInt(a.getBitWidth(), 0)
: APInt(a.getBitWidth(), 1));
});
}

//===----------------------------------------------------------------------===//
// SignFOp folder
//===----------------------------------------------------------------------===//

OpFoldResult math::SignFOp::fold(FoldAdaptor adaptor) {
return constFoldUnaryOp<FloatAttr>(
adaptor.getOperands(), [](const APFloat &a) {
if (a.isNaN())
return APFloat::getNaN(a.getSemantics());
return a.isNegative() ? APFloat(a.getSemantics(), -1)
: (a.isZero() ? APFloat(a.getSemantics(), 0)
: APFloat(a.getSemantics(), 1));
});
}

//===----------------------------------------------------------------------===//
// SinhOp folder
//===----------------------------------------------------------------------===//
Expand Down