]> granicus.if.org Git - clang/commitdiff
PR6515: Implement __builtin_signbit and friends.
authorEli Friedman <eli.friedman@gmail.com>
Sat, 6 Mar 2010 02:17:52 +0000 (02:17 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sat, 6 Mar 2010 02:17:52 +0000 (02:17 +0000)
I'm reasonably sure my implementation is correct, but it would be nice if
someone could double-check.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97864 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/Builtins.def
lib/CodeGen/CGBuiltin.cpp
test/CodeGen/builtins.c

index 9442ac328700907a2c3ddabc8af65f594acb4b5b..af233b81e0a5c7ae1a5ef2f7405dcfe1679612c5 100644 (file)
@@ -240,6 +240,11 @@ BUILTIN(__builtin_isinf_sign, "i.", "nc")
 BUILTIN(__builtin_isnan,      "i.", "nc")
 BUILTIN(__builtin_isnormal,   "i.", "nc")
 
+// FP signbit builtins
+BUILTIN(__builtin_signbit, "id", "nc")
+BUILTIN(__builtin_signbitf, "if", "nc")
+BUILTIN(__builtin_signbitl, "iLd", "nc")
+
 // Builtins for arithmetic.
 BUILTIN(__builtin_clz  , "iUi"  , "nc")
 BUILTIN(__builtin_clzl , "iULi" , "nc")
index 8703c381b2ecbea0a028e8b394158062a3aa7f63..706e4411fc793e6c55227dab60bc25c4af41d200 100644 (file)
@@ -682,6 +682,23 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
     Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1);
     return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp"));
   }
+
+  case Builtin::BI__builtin_signbit:
+  case Builtin::BI__builtin_signbitf:
+  case Builtin::BI__builtin_signbitl: {
+    LLVMContext &C = CGM.getLLVMContext();
+
+    Value *Arg = EmitScalarExpr(E->getArg(0));
+    const llvm::Type *ArgTy = Arg->getType();
+    if (ArgTy->isPPC_FP128Ty())
+      break; // FIXME: I'm not sure what the right implementation is here.
+    int ArgWidth = ArgTy->getPrimitiveSizeInBits();
+    const llvm::Type *ArgIntTy = llvm::IntegerType::get(C, ArgWidth);
+    Value *BCArg = Builder.CreateBitCast(Arg, ArgIntTy);
+    Value *ZeroCmp = llvm::Constant::getNullValue(ArgIntTy);
+    Value *Result = Builder.CreateICmpSLT(BCArg, ZeroCmp);
+    return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType())));
+  }
   }
 
   // If this is an alias for a libm function (e.g. __builtin_sin) turn it into
index 417ca7def5f2bc412b232a59bea494b0748a6dcd..a4424d77428f3a563facc2e591ec2d1c948bc403 100644 (file)
@@ -118,6 +118,7 @@ int main() {
   // V(clear_cache, (&N, &N+1));
   V(trap, ());
   R(extract_return_addr, (&N));
+  P(signbit, (1.0));
 
   return 0;
 }