diff --git a/clang/lib/AST/Interp/InterpBuiltin.cpp b/clang/lib/AST/Interp/InterpBuiltin.cpp --- a/clang/lib/AST/Interp/InterpBuiltin.cpp +++ b/clang/lib/AST/Interp/InterpBuiltin.cpp @@ -173,6 +173,26 @@ return true; } +static bool interp__builtin_fmax(InterpState &S, CodePtr OpPC, + const InterpFrame *Frame, + const Function *Func) { + const Floating &LHS = getParam(Frame, 0); + const Floating &RHS = getParam(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(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. @@ -341,6 +361,15 @@ return Ret(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(S, OpPC, Dummy); + break; + case Builtin::BI__builtin_isnan: if (interp__builtin_isnan(S, OpPC, Frame, F)) return Ret(S, OpPC, Dummy); diff --git a/clang/test/Sema/constant-builtins-fmax.cpp b/clang/test/Sema/constant-builtins-fmax.cpp --- a/clang/test/Sema/constant-builtins-fmax.cpp +++ b/clang/test/Sema/constant-builtins-fmax.cpp @@ -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("");