[PATCH] D46926: [Fixed Point Arithmetic] Conversion between Fixed Point and Floating Point Numbers

2018-06-21 Thread Leonard Chan via Phabricator via cfe-commits
leonardchan abandoned this revision.
leonardchan added a comment.

The logic in this patch is moved to https://reviews.llvm.org/D48456


Repository:
  rC Clang

https://reviews.llvm.org/D46926



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D46926: [Fixed Point Arithmetic] Conversion between Fixed Point and Floating Point Numbers

2018-05-22 Thread Steve Canon via Phabricator via cfe-commits
scanon requested changes to this revision.
scanon added a comment.
This revision now requires changes to proceed.

IIRC the optimization of divide-by-power-of-two --> multiply-by-inverse does 
not occur at -O0, so it would be better to multiply by 2^(-fbits) instead.


Repository:
  rC Clang

https://reviews.llvm.org/D46926



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D46926: [Fixed Point Arithmetic] Conversion between Fixed Point and Floating Point Numbers

2018-05-21 Thread Leonard Chan via Phabricator via cfe-commits
leonardchan updated this revision to Diff 147881.
leonardchan added a comment.

formatting


Repository:
  rC Clang

https://reviews.llvm.org/D46926

Files:
  include/clang/AST/OperationKinds.def
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/AST/Expr.cpp
  lib/AST/ExprConstant.cpp
  lib/CodeGen/CGExpr.cpp
  lib/CodeGen/CGExprAgg.cpp
  lib/CodeGen/CGExprComplex.cpp
  lib/CodeGen/CGExprConstant.cpp
  lib/CodeGen/CGExprScalar.cpp
  lib/Edit/RewriteObjCFoundationAPI.cpp
  lib/Sema/SemaCast.cpp
  lib/Sema/SemaExpr.cpp
  lib/StaticAnalyzer/Core/ExprEngineC.cpp
  test/Frontend/fixed_point_validation.c

Index: test/Frontend/fixed_point_validation.c
===
--- test/Frontend/fixed_point_validation.c
+++ test/Frontend/fixed_point_validation.c
@@ -30,6 +30,7 @@
 // Run simple validation tests
 
 #define assert(b) if (!(b)) { return 1; }
+#define abs(x) x < 0 ? -x : x
 
 int main(){
   short _Accum s_accum = 0.0hk;
@@ -264,4 +265,18 @@
   assert(5.0hk >> 2 == 1.25hk);
   assert(-5.0hk >> 2 == -1.25k);
   assert(0.0hr >> 2 == 0);
+
+  / Float conversions ***/
+
+  float f = (float)2.5k;
+  assert(f > 2.4999 && f < 2.5001);  // High precision since the fractional
+ // value can be evenly represented.
+  assert((float)2.333hk != 2.333f);
+
+  float base = 2.333f;
+  float saccum_diff = abs(base - 2.333hk);
+  float accum_diff = abs(base - 2.333k);
+  float laccum_diff = abs(base - 2.333lk);
+  assert(accum_diff < saccum_diff);
+  assert(laccum_diff < accum_diff);
 }
Index: lib/StaticAnalyzer/Core/ExprEngineC.cpp
===
--- lib/StaticAnalyzer/Core/ExprEngineC.cpp
+++ lib/StaticAnalyzer/Core/ExprEngineC.cpp
@@ -326,6 +326,8 @@
   case CK_IntegralToFixedPoint:
 llvm_unreachable(
 "ExprEngine::VisitCast CK_IntegralToFixedPoint");  // TODO
+  case CK_FixedPointToFloating:
+llvm_unreachable("Unimplemented logic for CK_FixedPointToFloating");
   case CK_LValueToRValue:
 llvm_unreachable("LValueToRValue casts handled earlier.");
   case CK_ToVoid:
Index: lib/Sema/SemaExpr.cpp
===
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -1029,6 +1029,21 @@
   return result;
 }
 
+/// \brief Handle arithmetic conversion from fixed point to floating.  Helper
+/// function of UsualArithmeticConversions()
+static QualType handleFixedPointToFloatConversion(Sema ,
+  ExprResult ,
+  ExprResult ,
+  QualType FloatTy,
+  QualType FixedPointTy) {
+  assert(FloatTy->isFloatingType());
+  assert(FixedPointTy->isFixedPointType());
+
+  FixedPointExpr = S.ImpCastExprToType(FixedPointExpr.get(), FloatTy,
+   CK_FixedPointToFloating);
+  return FloatTy;
+}
+
 /// \brief Handle arithmethic conversion with floating point types.  Helper
 /// function of UsualArithmeticConversions()
 static QualType handleFloatConversion(Sema , ExprResult ,
@@ -1057,11 +1072,20 @@
 if (LHSType->isHalfType() && !S.getLangOpts().NativeHalfType)
   LHSType = S.Context.FloatTy;
 
+if (RHSType->isFixedPointType()) {
+  return handleFixedPointToFloatConversion(S, LHS, RHS, LHSType, RHSType);
+}
+
 return handleIntToFloatConversion(S, LHS, RHS, LHSType, RHSType,
   /*convertFloat=*/!IsCompAssign,
   /*convertInt=*/ true);
   }
   assert(RHSFloat);
+
+  if (LHSType->isFixedPointType()) {
+return handleFixedPointToFloatConversion(S, RHS, LHS, RHSType, LHSType);
+  }
+
   return handleIntToFloatConversion(S, RHS, LHS, RHSType, LHSType,
 /*convertInt=*/ true,
 /*convertFloat=*/!IsCompAssign);
@@ -1218,6 +1242,7 @@
   CK_IntegralRealToComplex);
   return ComplexType;
 }
+
 /// \brief Handle arithmetic conversion from integer to fixed point.  Helper
 /// function of UsualArithmeticConversions()
 static QualType handleIntToFixedPointConversion(Sema ,
@@ -6041,9 +6066,20 @@
 }
 llvm_unreachable("Should have returned before this");
 
-  case Type::STK_FixedPoint:
-llvm_unreachable(
-"Sema::PrepareScalarCast from STK_FixedPoint to anything");  // TODO
+  case Type::STK_FixedPoint: {
+switch (DestTy->getScalarTypeKind()) {
+  default:
+llvm_unreachable("Unable to convert from fixed point type");
+  case Type::STK_Integral:
+llvm_unreachable(
+"Unimplemented scalar cast from fixed point to int");  // TODO
+  case Type::STK_Floating:
+return CK_FixedPointToFloating;
+  case