Skip to content

Commit 487cfbe

Browse files
authored
[Clang] Implement constexpr support for __builtin_popcountg (#84318)
1 parent b408241 commit 487cfbe

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5378,6 +5378,7 @@ The following builtin intrinsics can be used in constant expressions:
53785378
* ``__builtin_popcount``
53795379
* ``__builtin_popcountl``
53805380
* ``__builtin_popcountll``
5381+
* ``__builtin_popcountg``
53815382
* ``__builtin_rotateleft8``
53825383
* ``__builtin_rotateleft16``
53835384
* ``__builtin_rotateleft32``

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ Non-comprehensive list of changes in this release
157157
- ``__builtin_addc``, ``__builtin_subc``, and the other sizes of those
158158
builtins are now constexpr and may be used in constant expressions.
159159

160+
- Added ``__builtin_popcountg`` as a type-generic alternative to
161+
``__builtin_popcount{,l,ll}`` with support for any unsigned integer type. Like
162+
the previous builtins, this new builtin is constexpr and may be used in
163+
constant expressions.
164+
160165
New Compiler Flags
161166
------------------
162167

clang/include/clang/Basic/Builtins.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ def Popcount : Builtin, BitInt_Long_LongLongTemplate {
706706

707707
def Popcountg : Builtin {
708708
let Spellings = ["__builtin_popcountg"];
709-
let Attributes = [NoThrow, Const, CustomTypeChecking];
709+
let Attributes = [NoThrow, Const, Constexpr, CustomTypeChecking];
710710
let Prototype = "int(...)";
711711
}
712712

clang/lib/AST/ExprConstant.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12483,6 +12483,7 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1248312483
case Builtin::BI__builtin_popcount:
1248412484
case Builtin::BI__builtin_popcountl:
1248512485
case Builtin::BI__builtin_popcountll:
12486+
case Builtin::BI__builtin_popcountg:
1248612487
case Builtin::BI__popcnt16: // Microsoft variants of popcount
1248712488
case Builtin::BI__popcnt:
1248812489
case Builtin::BI__popcnt64: {

clang/test/Sema/constant-builtins-2.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ char popcount7[__builtin_popcountl(~0L) == BITSIZE(long) ? 1 : -1];
237237
char popcount8[__builtin_popcountll(0LL) == 0 ? 1 : -1];
238238
char popcount9[__builtin_popcountll(0xF0F0LL) == 8 ? 1 : -1];
239239
char popcount10[__builtin_popcountll(~0LL) == BITSIZE(long long) ? 1 : -1];
240+
char popcount11[__builtin_popcountg(0U) == 0 ? 1 : -1];
241+
char popcount12[__builtin_popcountg(0xF0F0U) == 8 ? 1 : -1];
242+
char popcount13[__builtin_popcountg(~0U) == BITSIZE(int) ? 1 : -1];
243+
char popcount14[__builtin_popcountg(~0UL) == BITSIZE(long) ? 1 : -1];
244+
char popcount15[__builtin_popcountg(~0ULL) == BITSIZE(long long) ? 1 : -1];
245+
char popcount16[__builtin_popcountg(~(unsigned __int128)0) == BITSIZE(__int128) ? 1 : -1];
246+
char popcount17[__builtin_popcountg(~(unsigned _BitInt(128))0) == BITSIZE(_BitInt(128)) ? 1 : -1];
240247

241248
char parity1[__builtin_parity(0) == 0 ? 1 : -1];
242249
char parity2[__builtin_parity(0xb821) == 0 ? 1 : -1];

0 commit comments

Comments
 (0)