21 : builder(builder), loc(loc) {}
24 const llvm::FixedPointSemantics &srcSema,
26 mlir::Type opTy = getAccommodatingFloatType(dstTy, srcSema);
30 builder.createCast(loc, cir::CastKind::int_to_float, src, opTy);
33 const llvm::fltSemantics &opSemantics =
34 mlir::cast<cir::FPTypeInterface>(opTy).getFloatSemantics();
35 llvm::APFloat scaleVal(
36 std::pow(2.0, -
static_cast<int>(srcSema.getScale())));
38 scaleVal.convert(opSemantics, llvm::APFloat::rmNearestTiesToEven,
42 cir::ConstantOp fpConst = builder.getConstFP(loc, opTy, scaleVal);
43 result = builder.createFMul(loc, result, fpConst);
46 result = builder.createFloatingCast(result, dstTy);
51 const llvm::FixedPointSemantics &dstSema) {
52 mlir::Value result = src;
53 mlir::Type opTy = getAccommodatingFloatType(src.getType(), dstSema);
55 if (opTy != src.getType())
56 result = builder.createFloatingCast(result, opTy);
60 const llvm::fltSemantics &opSemantics =
61 mlir::cast<cir::FPTypeInterface>(opTy).getFloatSemantics();
62 llvm::APFloat scaleVal(
std::pow(2.0, dstSema.getScale()));
64 scaleVal.convert(opSemantics, llvm::APFloat::rmNearestTiesToEven,
68 cir::ConstantOp fpConst = builder.getConstFP(loc, opTy, scaleVal);
69 result = builder.createFMul(loc, result, fpConst);
71 cir::IntType opIntTy = getOpIntType(dstSema);
72 if (dstSema.isSaturated()) {
73 result = builder.createFPToIntSat(loc, result, opIntTy);
74 if (dstSema.hasUnsignedPadding())
75 result = clampNegativeToZero(result);
78 builder.createCast(loc, cir::CastKind::float_to_int, result, opIntTy);
81 return builder.createIntCast(result, getIntType(dstSema));
85 const llvm::FixedPointSemantics &srcSema,
86 unsigned dstWidth,
bool dstIsSigned) {
89 llvm::FixedPointSemantics::GetIntegerSemantics(dstWidth, dstIsSigned),
94 const llvm::FixedPointSemantics &dstSema) {
96 if (mlir::isa<cir::BoolType>(src.getType())) {
99 src = builder.createBoolToInt(
100 src, cir::IntType::get(builder.getContext(), 1,
false));
102 srcWidth = mlir::cast<cir::IntType>(src.getType()).getWidth();
106 llvm::FixedPointSemantics::GetIntegerSemantics(srcWidth, srcIsSigned),
111 const llvm::FixedPointSemantics &srcSema,
112 const llvm::FixedPointSemantics &dstSema) {
113 return convert(src, srcSema, dstSema,
false);
117 const llvm::FixedPointSemantics &lhsSema,
119 const llvm::FixedPointSemantics &rhsSema) {
120 auto commonSema = getCommonBinopSemantic(lhsSema, rhsSema);
126 commonSema.isSaturated()
127 ? createSaturatingAddSub(wideLhs, wideRhs, commonSema,
129 : builder.createAdd(loc, wideLhs, wideRhs);
132 lhsSema.getCommonSemantics(rhsSema));
136 const llvm::FixedPointSemantics &lhsSema,
138 const llvm::FixedPointSemantics &rhsSema) {
139 auto commonSema = getCommonBinopSemantic(lhsSema, rhsSema);
145 commonSema.isSaturated()
146 ? createSaturatingAddSub(wideLhs, wideRhs, commonSema,
148 : builder.createSub(loc, wideLhs, wideRhs);
151 lhsSema.getCommonSemantics(rhsSema));
156 const llvm::FixedPointSemantics &
sema) {
157 return createSub(builder.getNullValue(value.getType(), loc),
sema, value,
162 const llvm::FixedPointSemantics &lhsSema,
164 const llvm::FixedPointSemantics &rhsSema) {
165 auto commonSema = getCommonBinopSemantic(lhsSema, rhsSema);
166 bool useSigned = commonSema.isSigned() || commonSema.hasUnsignedPadding();
172 cir::ConstantOp scale;
175 intrinId =
"smul.fix";
176 scale = builder.getSInt32(commonSema.getScale(), loc);
178 intrinId =
"umul.fix";
179 scale = builder.getUInt32(commonSema.getScale(), loc);
182 if (commonSema.isSaturated())
186 builder.emitIntrinsicCallOp(loc, intrinId, wideLhs.getType(),
187 mlir::ValueRange{wideLhs, wideRhs, scale});
190 lhsSema.getCommonSemantics(rhsSema));
194 const llvm::FixedPointSemantics &lhsSema,
196 const llvm::FixedPointSemantics &rhsSema) {
197 auto commonSema = getCommonBinopSemantic(lhsSema, rhsSema);
198 bool useSigned = commonSema.isSigned() || commonSema.hasUnsignedPadding();
204 cir::ConstantOp scale;
207 intrinId =
"sdiv.fix";
208 scale = builder.getSInt32(commonSema.getScale(), loc);
210 intrinId =
"udiv.fix";
211 scale = builder.getUInt32(commonSema.getScale(), loc);
214 if (commonSema.isSaturated())
218 builder.emitIntrinsicCallOp(loc, intrinId, wideLhs.getType(),
219 mlir::ValueRange{wideLhs, wideRhs, scale});
222 lhsSema.getCommonSemantics(rhsSema));
226 const llvm::FixedPointSemantics &lhsSema,
228 const llvm::FixedPointSemantics &rhsSema,
229 cir::CmpOpKind
kind) {
230 auto commonSema = getCommonBinopSemantic(lhsSema, rhsSema);
235 return builder.createCompare(loc,
kind, wideLhs, wideRhs);
239 const llvm::FixedPointSemantics &lhsSema,
242 if (lhsSema.isSaturated()) {
245 auto rhsIntTy = mlir::cast<cir::IntType>(rhs.getType());
246 auto rhsUnsignedTy = cir::IntType::get(
247 builder.getContext(), rhsIntTy.getWidth(),
false);
249 mlir::Value rhsUnsigned =
250 builder.createCast(cir::CastKind::integral, rhs, rhsUnsignedTy);
251 mlir::Value rhsResized = builder.createCast(cir::CastKind::integral,
252 rhsUnsigned, lhs.getType());
254 bool useSigned = lhsSema.isSigned() || lhsSema.hasUnsignedPadding();
255 result = builder.emitIntrinsicCallOp(
256 loc, useSigned ?
"sshl.sat" :
"ushl.sat", lhs.getType(),
257 mlir::ValueRange{lhs, rhsResized});
259 result = builder.createShiftLeft(loc, lhs, rhs);
265 mlir::Value
createShr(mlir::Value lhs, mlir::Value rhs) {
266 return builder.createShiftRight(loc, lhs, rhs);
270 mlir::Value convert(mlir::Value src,
const llvm::FixedPointSemantics &srcSema,
271 const llvm::FixedPointSemantics &dstSema,
273 unsigned srcWidth = srcSema.getWidth();
274 unsigned dstWidth = dstSema.getWidth();
275 unsigned srcScale = srcSema.getScale();
276 unsigned dstScale = dstSema.getScale();
277 bool srcIsSigned = srcSema.isSigned();
278 bool dstIsSigned = dstSema.isSigned();
280 mlir::Value result = src;
281 unsigned resultWidth = srcWidth;
284 if (dstScale < srcScale) {
288 if (dstIsInteger && srcIsSigned) {
289 mlir::Value zero = builder.
getNullValue(result.getType(), loc);
291 builder.
createCompare(loc, cir::CmpOpKind::lt, result, zero);
293 loc, result.getType(),
294 llvm::APInt::getLowBitsSet(srcWidth, srcScale));
295 mlir::Value rounded = builder.
createAdd(loc, result, lowBits);
301 cir::IntType dstIntTy = getIntType(dstSema);
303 if (!dstSema.isSaturated()) {
307 if (dstScale > srcScale)
311 if (dstScale > srcScale) {
313 resultWidth = std::max(srcWidth + dstScale - srcScale, dstWidth);
314 cir::IntType upscaledTy =
315 cir::IntType::get(builder.getContext(), resultWidth, srcIsSigned);
316 result = builder.createIntCast(result, upscaledTy);
317 result = builder.createShiftLeft(loc, result, dstScale - srcScale);
321 bool fewerIntBits = dstSema.getIntegralBits() < srcSema.getIntegralBits();
323 mlir::Value
max = builder.getConstAPInt(
324 loc, result.getType(),
325 llvm::APFixedPoint::getMax(dstSema).getValue().extOrTrunc(
328 mlir::Value tooHigh =
329 builder.createCompare(loc, cir::CmpOpKind::gt, result,
max);
330 result = builder.createSelect(loc, tooHigh,
max, result);
335 if (srcIsSigned && (fewerIntBits || !dstIsSigned)) {
336 mlir::Value
min = builder.getConstAPInt(
337 loc, result.getType(),
338 llvm::APFixedPoint::getMin(dstSema).getValue().extOrTrunc(
341 builder.createCompare(loc, cir::CmpOpKind::lt, result,
min);
342 result = builder.createSelect(loc, tooLow,
min, result);
347 result = builder.createIntCast(result, dstIntTy);
352 mlir::Type getAccommodatingFloatType(mlir::Type ty,
353 const llvm::FixedPointSemantics &sema) {
354 const llvm::fltSemantics *floatSema =
355 &mlir::cast<cir::FPTypeInterface>(ty).getFloatSemantics();
356 while (!sema.fitsInFloatSemantics(*floatSema))
357 floatSema = llvm::APFixedPoint::promoteFloatSemantics(floatSema);
358 cir::FPTypeInterface accommodating =
360 assert(accommodating &&
"no float type for semantics?");
361 return accommodating;
365 llvm::FixedPointSemantics
366 getCommonBinopSemantic(
const llvm::FixedPointSemantics &lhsSema,
367 const llvm::FixedPointSemantics &rhsSema) {
368 auto c = lhsSema.getCommonSemantics(rhsSema);
370 lhsSema.hasUnsignedPadding() && rhsSema.hasUnsignedPadding();
371 return llvm::FixedPointSemantics(
372 c.getWidth() +
static_cast<unsigned>(bothPadded && c.isSaturated()),
373 c.getScale(), c.isSigned(), c.isSaturated(), bothPadded);
377 cir::IntType getIntType(
const llvm::FixedPointSemantics &sema) {
378 return cir::IntType::get(builder.getContext(), sema.getWidth(),
385 cir::IntType getOpIntType(
const llvm::FixedPointSemantics &sema) {
386 return cir::IntType::get(builder.getContext(), sema.getWidth(),
387 sema.isSigned() || sema.hasUnsignedPadding());
391 mlir::Value clampNegativeToZero(mlir::Value value) {
392 mlir::Value zero = builder.getNullValue(value.getType(), loc);
394 builder.createCompare(loc, cir::CmpOpKind::lt, value, zero);
395 return builder.createSelect(loc, isNeg, zero, value);
398 mlir::Value createSaturatingAddSub(mlir::Value lhs, mlir::Value rhs,
399 const llvm::FixedPointSemantics &sema,
401 cir::IntType opTy = getOpIntType(sema);
402 mlir::Value opLhs = builder.createIntCast(lhs, opTy);
403 mlir::Value opRhs = builder.createIntCast(rhs, opTy);
404 mlir::Value result = isSub ? builder.createSubSat(loc, opLhs, opRhs)
405 : builder.createAddSat(loc, opLhs, opRhs);
406 if (isSub && sema.hasUnsignedPadding())
407 result = clampNegativeToZero(result);
408 return builder.createIntCast(result, lhs.getType());
411 CIRGenBuilderTy &builder;