Skip to content

Commit 98a3bfa

Browse files
committed
add baseline signi/signf ops
1 parent b6ea04a commit 98a3bfa

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

mlir/include/mlir/Dialect/Math/IR/MathOps.td

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,45 @@ def Math_CoshOp : Math_FloatUnaryOp<"cosh"> {
437437
let hasFolder = 1;
438438
}
439439

440+
//===----------------------------------------------------------------------===//
441+
// SignIOp
442+
//===----------------------------------------------------------------------===//
443+
444+
def Math_SignIOp : Math_IntegerUnaryOp<"signi"> {
445+
let summary = "ternary-valued sign of an integer";
446+
let description = [{
447+
The `signi` operation computes -1 if the operand is negative, 0 if it is
448+
zero, and 1 if it is positive.
449+
450+
Example:
451+
452+
```mlir
453+
%a = math.signi %b : i64
454+
```
455+
}];
456+
let hasFolder = 1;
457+
}
458+
459+
460+
//===----------------------------------------------------------------------===//
461+
// SignFOp
462+
//===----------------------------------------------------------------------===//
463+
464+
def Math_SignFOp : Math_FloatUnaryOp<"signf"> {
465+
let summary = "ternary-valued sign of a floating point value";
466+
let description = [{
467+
The `signf` operation computes -1.0 if the operand is negative, 0.0 if it
468+
is zero, 1.0 if it is positive, and `NaN` if the operand is `NaN`.
469+
470+
Example:
471+
472+
```mlir
473+
%a = math.signf %b : f32
474+
```
475+
}];
476+
let hasFolder = 1;
477+
}
478+
440479
//===----------------------------------------------------------------------===//
441480
// SinOp
442481
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/Math/IR/MathOps.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,34 @@ OpFoldResult math::SinOp::fold(FoldAdaptor adaptor) {
266266
});
267267
}
268268

269+
//===----------------------------------------------------------------------===//
270+
// SignIOp folder
271+
//===----------------------------------------------------------------------===//
272+
273+
OpFoldResult math::SignIOp::fold(FoldAdaptor adaptor) {
274+
return constFoldUnaryOp<IntegerAttr>(
275+
adaptor.getOperands(), [](const APInt &a) {
276+
return a.isNegative() ? APInt(a.getBitWidth(), -1)
277+
: (a.isZero() ? APInt(a.getBitWidth(), 0)
278+
: APInt(a.getBitWidth(), 1));
279+
});
280+
}
281+
282+
//===----------------------------------------------------------------------===//
283+
// SignFOp folder
284+
//===----------------------------------------------------------------------===//
285+
286+
OpFoldResult math::SignFOp::fold(FoldAdaptor adaptor) {
287+
return constFoldUnaryOp<FloatAttr>(
288+
adaptor.getOperands(), [](const APFloat &a) {
289+
if (a.isNaN())
290+
return APFloat::getNaN(a.getSemantics());
291+
return a.isNegative() ? APFloat(a.getSemantics(), -1)
292+
: (a.isZero() ? APFloat(a.getSemantics(), 0)
293+
: APFloat(a.getSemantics(), 1));
294+
});
295+
}
296+
269297
//===----------------------------------------------------------------------===//
270298
// SinhOp folder
271299
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)