Skip to content

Commit fbf43b0

Browse files
authored
[libc] Only declare float128 math functions in the generated math.h if float128 type is supported. (#81010)
1 parent 1389260 commit fbf43b0

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

libc/include/math.h.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <__llvm-libc-common.h>
1313
#include <llvm-libc-macros/math-macros.h>
14+
#include <llvm-libc-types/float128.h>
15+
1416

1517
%%public_api()
1618

libc/spec/spec.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ class FunctionSpec<string name, RetValSpec return, list<ArgSpec> args> {
176176
list<ArgSpec> Args = args;
177177
}
178178

179+
class GuardedFunctionSpec<string name, RetValSpec return, list<ArgSpec> args, string guard_macro> : FunctionSpec<name, return, args> {
180+
string Guard = guard_macro;
181+
}
182+
179183
class ObjectSpec<string name, string type> {
180184
string Name = name;
181185
string Type = type;

libc/spec/stdc.td

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,17 @@ def StdC : StandardSpec<"stdc"> {
359359
FunctionSpec<"copysign", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
360360
FunctionSpec<"copysignf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
361361
FunctionSpec<"copysignl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
362-
FunctionSpec<"copysignf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>]>,
362+
GuardedFunctionSpec<"copysignf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
363363

364364
FunctionSpec<"ceil", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
365365
FunctionSpec<"ceilf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
366366
FunctionSpec<"ceill", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
367-
FunctionSpec<"ceilf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>]>,
367+
GuardedFunctionSpec<"ceilf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
368368

369369
FunctionSpec<"fabs", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
370370
FunctionSpec<"fabsf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
371371
FunctionSpec<"fabsl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
372-
FunctionSpec<"fabsf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>]>,
372+
GuardedFunctionSpec<"fabsf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
373373

374374
FunctionSpec<"fdim", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
375375
FunctionSpec<"fdimf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
@@ -378,17 +378,17 @@ def StdC : StandardSpec<"stdc"> {
378378
FunctionSpec<"floor", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
379379
FunctionSpec<"floorf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
380380
FunctionSpec<"floorl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
381-
FunctionSpec<"floorf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>]>,
381+
GuardedFunctionSpec<"floorf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
382382

383383
FunctionSpec<"fmin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
384384
FunctionSpec<"fminf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
385385
FunctionSpec<"fminl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
386-
FunctionSpec<"fminf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>]>,
386+
GuardedFunctionSpec<"fminf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
387387

388388
FunctionSpec<"fmax", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
389389
FunctionSpec<"fmaxf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
390390
FunctionSpec<"fmaxl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
391-
FunctionSpec<"fmaxf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>]>,
391+
GuardedFunctionSpec<"fmaxf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
392392

393393
FunctionSpec<"fma", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
394394
FunctionSpec<"fmaf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>, ArgSpec<FloatType>]>,
@@ -461,7 +461,7 @@ def StdC : StandardSpec<"stdc"> {
461461
FunctionSpec<"round", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
462462
FunctionSpec<"roundf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
463463
FunctionSpec<"roundl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
464-
FunctionSpec<"roundf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>]>,
464+
GuardedFunctionSpec<"roundf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
465465

466466
FunctionSpec<"lround", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
467467
FunctionSpec<"lroundf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
@@ -486,12 +486,12 @@ def StdC : StandardSpec<"stdc"> {
486486
FunctionSpec<"sqrt", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
487487
FunctionSpec<"sqrtf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
488488
FunctionSpec<"sqrtl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
489-
FunctionSpec<"sqrtf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>]>,
489+
GuardedFunctionSpec<"sqrtf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
490490

491491
FunctionSpec<"trunc", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
492492
FunctionSpec<"truncf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
493493
FunctionSpec<"truncl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
494-
FunctionSpec<"truncf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>]>,
494+
GuardedFunctionSpec<"truncf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_COMPILER_HAS_FLOAT128">,
495495

496496
FunctionSpec<"nearbyint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
497497
FunctionSpec<"nearbyintf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

libc/utils/HdrGen/PublicAPICommand.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ void writeAPIFromIndex(APIIndexer &G,
102102
llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
103103
llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");
104104

105+
// TODO: https://github.com/llvm/llvm-project/issues/81208
106+
// Ideally, we should group functions based on their guarding macros.
107+
bool Guarded =
108+
(FunctionSpec->getType()->getAsString() == "GuardedFunctionSpec");
109+
110+
if (Guarded)
111+
OS << "#ifdef " << FunctionSpec->getValueAsString("Guard") << "\n";
112+
105113
OS << G.getTypeAsString(ReturnType) << " " << Name << "(";
106114

107115
auto ArgsList = FunctionSpec->getValueAsListOfDefs("Args");
@@ -112,7 +120,12 @@ void writeAPIFromIndex(APIIndexer &G,
112120
OS << ", ";
113121
}
114122

115-
OS << ") __NOEXCEPT;\n\n";
123+
OS << ") __NOEXCEPT;\n";
124+
125+
if (Guarded)
126+
OS << "#endif // " << FunctionSpec->getValueAsString("Guard") << "\n";
127+
128+
OS << "\n";
116129
}
117130

118131
// Make another pass over entrypoints to emit object declarations.

0 commit comments

Comments
 (0)