[PATCH] D137719: [clang] Missed rounding mode use in constant evaluation

Serge Pavlov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 9 08:14:44 PST 2022


sepavloff created this revision.
sepavloff added reviewers: rjmccall, efriedma, aaron.ballman.
Herald added a project: All.
sepavloff requested review of this revision.
Herald added a project: clang.

Integer-to-float conversion was handled in constant evaluator with
default rounding mode. This change fixes the behavior and the conversion
is made using rounding mode stored in ImplicitCastExpr node.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137719

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/test/AST/const-fpfeatures.c


Index: clang/test/AST/const-fpfeatures.c
===================================================================
--- clang/test/AST/const-fpfeatures.c
+++ clang/test/AST/const-fpfeatures.c
@@ -13,6 +13,9 @@
 // CHECK: @F2u = {{.*}} float 0x3FF0000020000000
 // CHECK: @F3u = {{.*}} float 0x3FF0000020000000
 
+float FI1u = 0xFFFFFFFFU;
+// CHECK: @FI1u = {{.*}} float 0x41F0000000000000
+
 float _Complex C1u = C0;
 // CHECK: @C1u = {{.*}} { float, float } { float 0x3FF0000020000000, float 0x3FF0000020000000 }
 
@@ -27,5 +30,8 @@
 // CHECK: @F2d = {{.*}} float 1.000000e+00
 // CHECK: @F3d = {{.*}} float 1.000000e+00
 
+float FI1d = 0xFFFFFFFFU;
+// CHECK: @FI1d = {{.*}} float 0x41EFFFFFE0000000
+
 float _Complex C1d = C0;
 // CHECK: @C1d = {{.*}} { float, float } { float 1.000000e+00, float 1.000000e+00 }
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -2648,14 +2648,9 @@
                                  QualType SrcType, const APSInt &Value,
                                  QualType DestType, APFloat &Result) {
   Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1);
-  APFloat::opStatus St = Result.convertFromAPInt(Value, Value.isSigned(),
-       APFloat::rmNearestTiesToEven);
-  if (!Info.InConstantContext && St != llvm::APFloatBase::opOK &&
-      FPO.isFPConstrained()) {
-    Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict);
-    return false;
-  }
-  return true;
+  llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
+  APFloat::opStatus St = Result.convertFromAPInt(Value, Value.isSigned(), RM);
+  return checkFloatingPointResult(Info, E, St);
 }
 
 static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137719.474282.patch
Type: text/x-patch
Size: 1798 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221109/d3e0150a/attachment.bin>


More information about the cfe-commits mailing list