Skip to content

Commit 790d986

Browse files
authored
[clang][bytecode] Implement __builtin_f{maximum,minimum}_num (#112335)
1 parent 0a17bdf commit 790d986

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -333,39 +333,48 @@ static bool interp__builtin_copysign(InterpState &S, CodePtr OpPC,
333333
}
334334

335335
static bool interp__builtin_fmin(InterpState &S, CodePtr OpPC,
336-
const InterpFrame *Frame, const Function *F) {
336+
const InterpFrame *Frame, const Function *F,
337+
bool IsNumBuiltin) {
337338
const Floating &LHS = getParam<Floating>(Frame, 0);
338339
const Floating &RHS = getParam<Floating>(Frame, 1);
339340

340341
Floating Result;
341342

342-
// When comparing zeroes, return -0.0 if one of the zeroes is negative.
343-
if (LHS.isZero() && RHS.isZero() && RHS.isNegative())
344-
Result = RHS;
345-
else if (LHS.isNan() || RHS < LHS)
346-
Result = RHS;
347-
else
348-
Result = LHS;
343+
if (IsNumBuiltin) {
344+
Result = llvm::minimumnum(LHS.getAPFloat(), RHS.getAPFloat());
345+
} else {
346+
// When comparing zeroes, return -0.0 if one of the zeroes is negative.
347+
if (LHS.isZero() && RHS.isZero() && RHS.isNegative())
348+
Result = RHS;
349+
else if (LHS.isNan() || RHS < LHS)
350+
Result = RHS;
351+
else
352+
Result = LHS;
353+
}
349354

350355
S.Stk.push<Floating>(Result);
351356
return true;
352357
}
353358

354359
static bool interp__builtin_fmax(InterpState &S, CodePtr OpPC,
355-
const InterpFrame *Frame,
356-
const Function *Func) {
360+
const InterpFrame *Frame, const Function *Func,
361+
bool IsNumBuiltin) {
357362
const Floating &LHS = getParam<Floating>(Frame, 0);
358363
const Floating &RHS = getParam<Floating>(Frame, 1);
359364

360365
Floating Result;
361366

362-
// When comparing zeroes, return +0.0 if one of the zeroes is positive.
363-
if (LHS.isZero() && RHS.isZero() && LHS.isNegative())
364-
Result = RHS;
365-
else if (LHS.isNan() || RHS > LHS)
366-
Result = RHS;
367-
else
368-
Result = LHS;
367+
if (IsNumBuiltin) {
368+
Result = llvm::maximumnum(LHS.getAPFloat(), RHS.getAPFloat());
369+
} else {
370+
// When comparing zeroes, return +0.0 if one of the zeroes is positive.
371+
if (LHS.isZero() && RHS.isZero() && LHS.isNegative())
372+
Result = RHS;
373+
else if (LHS.isNan() || RHS > LHS)
374+
Result = RHS;
375+
else
376+
Result = LHS;
377+
}
369378

370379
S.Stk.push<Floating>(Result);
371380
return true;
@@ -1701,7 +1710,16 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
17011710
case Builtin::BI__builtin_fminl:
17021711
case Builtin::BI__builtin_fminf16:
17031712
case Builtin::BI__builtin_fminf128:
1704-
if (!interp__builtin_fmin(S, OpPC, Frame, F))
1713+
if (!interp__builtin_fmin(S, OpPC, Frame, F, /*IsNumBuiltin=*/false))
1714+
return false;
1715+
break;
1716+
1717+
case Builtin::BI__builtin_fminimum_num:
1718+
case Builtin::BI__builtin_fminimum_numf:
1719+
case Builtin::BI__builtin_fminimum_numl:
1720+
case Builtin::BI__builtin_fminimum_numf16:
1721+
case Builtin::BI__builtin_fminimum_numf128:
1722+
if (!interp__builtin_fmin(S, OpPC, Frame, F, /*IsNumBuiltin=*/true))
17051723
return false;
17061724
break;
17071725

@@ -1710,7 +1728,16 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
17101728
case Builtin::BI__builtin_fmaxl:
17111729
case Builtin::BI__builtin_fmaxf16:
17121730
case Builtin::BI__builtin_fmaxf128:
1713-
if (!interp__builtin_fmax(S, OpPC, Frame, F))
1731+
if (!interp__builtin_fmax(S, OpPC, Frame, F, /*IsNumBuiltin=*/false))
1732+
return false;
1733+
break;
1734+
1735+
case Builtin::BI__builtin_fmaximum_num:
1736+
case Builtin::BI__builtin_fmaximum_numf:
1737+
case Builtin::BI__builtin_fmaximum_numl:
1738+
case Builtin::BI__builtin_fmaximum_numf16:
1739+
case Builtin::BI__builtin_fmaximum_numf128:
1740+
if (!interp__builtin_fmax(S, OpPC, Frame, F, /*IsNumBuiltin=*/true))
17141741
return false;
17151742
break;
17161743

clang/test/Sema/constant-builtins-fmaximum-num.cpp

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

55
constexpr double NaN = __builtin_nan("");

clang/test/Sema/constant-builtins-fminimum-num.cpp

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

55
constexpr double NaN = __builtin_nan("");

0 commit comments

Comments
 (0)