Skip to content

Commit 89e44e3

Browse files
committed
[clang][Interp] Implement __builtin_fmax
Differential Revision: https://reviews.llvm.org/D155401
1 parent 7ef8793 commit 89e44e3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

clang/lib/AST/Interp/InterpBuiltin.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ static bool interp__builtin_fmin(InterpState &S, CodePtr OpPC,
173173
return true;
174174
}
175175

176+
static bool interp__builtin_fmax(InterpState &S, CodePtr OpPC,
177+
const InterpFrame *Frame,
178+
const Function *Func) {
179+
const Floating &LHS = getParam<Floating>(Frame, 0);
180+
const Floating &RHS = getParam<Floating>(Frame, 1);
181+
182+
Floating Result;
183+
184+
// When comparing zeroes, return +0.0 if one of the zeroes is positive.
185+
if (LHS.isZero() && RHS.isZero() && LHS.isNegative())
186+
Result = RHS;
187+
else if (LHS.isNan() || RHS > LHS)
188+
Result = RHS;
189+
else
190+
Result = LHS;
191+
192+
S.Stk.push<Floating>(Result);
193+
return true;
194+
}
195+
176196
/// Defined as __builtin_isnan(...), to accommodate the fact that it can
177197
/// take a float, double, long double, etc.
178198
/// But for us, that's all a Floating anyway.
@@ -341,6 +361,15 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
341361
return Ret<PT_Float>(S, OpPC, Dummy);
342362
break;
343363

364+
case Builtin::BI__builtin_fmax:
365+
case Builtin::BI__builtin_fmaxf:
366+
case Builtin::BI__builtin_fmaxl:
367+
case Builtin::BI__builtin_fmaxf16:
368+
case Builtin::BI__builtin_fmaxf128:
369+
if (interp__builtin_fmax(S, OpPC, Frame, F))
370+
return Ret<PT_Float>(S, OpPC, Dummy);
371+
break;
372+
344373
case Builtin::BI__builtin_isnan:
345374
if (interp__builtin_isnan(S, OpPC, Frame, F))
346375
return Ret<PT_Sint32>(S, OpPC, Dummy);

clang/test/Sema/constant-builtins-fmax.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
2+
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
23
// expected-no-diagnostics
34

45
constexpr double NaN = __builtin_nan("");

0 commit comments

Comments
 (0)