[llvm-branch-commits] [mlir] [mlir][arith] Add support for `fptosi`, `fptoui` to `ArithToAPFloat` (PR #169277)

Maksim Levental via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 24 08:55:30 PST 2025


================
@@ -101,4 +102,17 @@ _mlir_apfloat_convert(int32_t inSemantics, int32_t outSemantics, uint64_t a) {
   llvm::APInt result = val.bitcastToAPInt();
   return result.getZExtValue();
 }
+
+MLIR_APFLOAT_WRAPPERS_EXPORT uint64_t _mlir_apfloat_convert_to_int(
+    int32_t semantics, int32_t resultWidth, bool isUnsigned, uint64_t a) {
+  const llvm::fltSemantics &sem = llvm::APFloatBase::EnumToSemantics(
+      static_cast<llvm::APFloatBase::Semantics>(semantics));
+  unsigned inputWidth = llvm::APFloatBase::semanticsSizeInBits(sem);
+  llvm::APFloat val(sem, llvm::APInt(inputWidth, a));
+  llvm::APSInt result(resultWidth, isUnsigned);
+  bool isExact;
+  // TODO: Custom rounding modes are not supported yet.
+  val.convertToInteger(result, llvm::RoundingMode::NearestTiesToEven, &isExact);
+  return result.getZExtValue();
----------------
makslevental wrote:

n00b q: shouldn't this be either a `sext` or a `zext` depending on whether we want `fptosi` or `fptoui` semantics?

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


More information about the llvm-branch-commits mailing list