Skip to content

Commit

Permalink
[clang][Interp] Implement __builtin_fmax
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D155401
  • Loading branch information
tbaederr committed Aug 1, 2023
1 parent 7ef8793 commit 89e44e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions clang/lib/AST/Interp/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ static bool interp__builtin_fmin(InterpState &S, CodePtr OpPC,
return true;
}

static bool interp__builtin_fmax(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
const Function *Func) {
const Floating &LHS = getParam<Floating>(Frame, 0);
const Floating &RHS = getParam<Floating>(Frame, 1);

Floating Result;

// When comparing zeroes, return +0.0 if one of the zeroes is positive.
if (LHS.isZero() && RHS.isZero() && LHS.isNegative())
Result = RHS;
else if (LHS.isNan() || RHS > LHS)
Result = RHS;
else
Result = LHS;

S.Stk.push<Floating>(Result);
return true;
}

/// Defined as __builtin_isnan(...), to accommodate the fact that it can
/// take a float, double, long double, etc.
/// But for us, that's all a Floating anyway.
Expand Down Expand Up @@ -341,6 +361,15 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
return Ret<PT_Float>(S, OpPC, Dummy);
break;

case Builtin::BI__builtin_fmax:
case Builtin::BI__builtin_fmaxf:
case Builtin::BI__builtin_fmaxl:
case Builtin::BI__builtin_fmaxf16:
case Builtin::BI__builtin_fmaxf128:
if (interp__builtin_fmax(S, OpPC, Frame, F))
return Ret<PT_Float>(S, OpPC, Dummy);
break;

case Builtin::BI__builtin_isnan:
if (interp__builtin_isnan(S, OpPC, Frame, F))
return Ret<PT_Sint32>(S, OpPC, Dummy);
Expand Down
1 change: 1 addition & 0 deletions clang/test/Sema/constant-builtins-fmax.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
// expected-no-diagnostics

constexpr double NaN = __builtin_nan("");
Expand Down

0 comments on commit 89e44e3

Please sign in to comment.