Skip to content

Commit

Permalink
Add popcount, clz, ctz builtin intrinsics (NVIDIA#3489)
Browse files Browse the repository at this point in the history
* add popcount, clz, ctz builtin instrinsics
* macro order and year update
* fix macro names
* skip c++11/14
  • Loading branch information
fbusato authored Jan 23, 2025
1 parent eb9c614 commit bc24165
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 1 deletion.
18 changes: 17 additions & 1 deletion libcudacxx/include/cuda/std/__cccl/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

Expand Down Expand Up @@ -138,6 +138,22 @@
# undef _CCCL_BUILTIN_BIT_CAST
#endif // clang < 10 || nvcc < 11.7

#if _CCCL_CHECK_BUILTIN(builtin_popcount) || _CCCL_COMPILER(GCC, <, 10) || _CCCL_COMPILER(CLANG) \
|| _CCCL_COMPILER(NVHPC)
# define _CCCL_BUILTIN_POPCOUNT(...) ::__builtin_popcount(__VA_ARGS__)
# define _CCCL_BUILTIN_POPCOUNTLL(...) ::__builtin_popcountll(__VA_ARGS__)
#endif // _CCCL_CHECK_BUILTIN(builtin_popcount)

#if _CCCL_CHECK_BUILTIN(builtin_clz) || _CCCL_COMPILER(GCC, <, 10) || _CCCL_COMPILER(CLANG) || _CCCL_COMPILER(NVHPC)
# define _CCCL_BUILTIN_CLZ(...) ::__builtin_clz(__VA_ARGS__)
# define _CCCL_BUILTIN_CLZLL(...) ::__builtin_clzll(__VA_ARGS__)
#endif // _CCCL_CHECK_BUILTIN(builtin_clz)

#if _CCCL_CHECK_BUILTIN(builtin_ctz) || _CCCL_COMPILER(GCC, <, 10) || _CCCL_COMPILER(CLANG) || _CCCL_COMPILER(NVHPC)
# define _CCCL_BUILTIN_CTZ(...) ::__builtin_ctz(__VA_ARGS__)
# define _CCCL_BUILTIN_CTZLL(...) ::__builtin_ctzll(__VA_ARGS__)
#endif // _CCCL_CHECK_BUILTIN(builtin_ctz)

#if _CCCL_CHECK_BUILTIN(builtin_bswap16)
# define _CCCL_BUILTIN_BSWAP16(...) __builtin_bswap16(__VA_ARGS__)
#endif // _CCCL_CHECK_BUILTIN(builtin_bswap16)
Expand Down
22 changes: 22 additions & 0 deletions libcudacxx/test/libcudacxx/std/cccl/builtin.bit.clz.fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14

#include <cuda/std/__cccl/builtin.h>

int main(int, char**)
{
#if !defined(_CCCL_BUILTIN_CLZ)
auto x = _CCCL_BUILTIN_CLZ(0b10101010);
unused(x);
#else
static_assert(false);
#endif
return 0;
}
22 changes: 22 additions & 0 deletions libcudacxx/test/libcudacxx/std/cccl/builtin.bit.clzll.fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14

#include <cuda/std/__cccl/builtin.h>

int main(int, char**)
{
#if !defined(_CCCL_BUILTIN_CLZLL)
auto x = _CCCL_BUILTIN_CLZLL(0b10101010);
unused(x);
#else
static_assert(false);
#endif
return 0;
}
22 changes: 22 additions & 0 deletions libcudacxx/test/libcudacxx/std/cccl/builtin.bit.ctz.fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14

#include <cuda/std/__cccl/builtin.h>

int main(int, char**)
{
#if !defined(_CCCL_BUILTIN_CTZ)
auto x = _CCCL_BUILTIN_CTZ(0b10101010);
unused(x);
#else
static_assert(false);
#endif
return 0;
}
22 changes: 22 additions & 0 deletions libcudacxx/test/libcudacxx/std/cccl/builtin.bit.ctzll.fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14

#include <cuda/std/__cccl/builtin.h>

int main(int, char**)
{
#if !defined(_CCCL_BUILTIN_CTZLL)
auto x = _CCCL_BUILTIN_CTZLL(0b10101010);
unused(x);
#else
static_assert(false);
#endif
return 0;
}
34 changes: 34 additions & 0 deletions libcudacxx/test/libcudacxx/std/cccl/builtin.bit.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14

#include <cuda/std/__cccl/builtin.h>

int main(int, char**)
{
#if defined(_CCCL_BUILTIN_POPCOUNT)
static_assert(_CCCL_BUILTIN_POPCOUNT(0b10101010) == 4);
#endif
#if defined(_CCCL_BUILTIN_POPCOUNTLL)
static_assert(_CCCL_BUILTIN_POPCOUNTLL(0b10101010) == 4);
#endif
#if defined(_CCCL_BUILTIN_CLZ)
static_assert(_CCCL_BUILTIN_CLZ(0b10101010) == 24);
#endif
#if defined(_CCCL_BUILTIN_CLZLL)
static_assert(_CCCL_BUILTIN_CLZLL(0b10101010) == 56);
#endif
#if defined(_CCCL_BUILTIN_CTZ)
static_assert(_CCCL_BUILTIN_CTZ(0b10101010) == 1);
#endif
#if defined(_CCCL_BUILTIN_CTZLL)
static_assert(_CCCL_BUILTIN_CTZLL(0b10101010) == 1);
#endif
return 0;
}
22 changes: 22 additions & 0 deletions libcudacxx/test/libcudacxx/std/cccl/builtin.bit.popc.fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14

#include <cuda/std/__cccl/builtin.h>

int main(int, char**)
{
#if !defined(_CCCL_BUILTIN_POPCOUNT)
auto x = _CCCL_BUILTIN_POPCOUNT(0b10101010);
unused(x);
#else
static_assert(false);
#endif
return 0;
}
22 changes: 22 additions & 0 deletions libcudacxx/test/libcudacxx/std/cccl/builtin.bit.popcll.fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14

#include <cuda/std/__cccl/builtin.h>

int main(int, char**)
{
#if !defined(_CCCL_BUILTIN_POPCOUNTLL)
auto x = _CCCL_BUILTIN_POPCOUNTLL(0b10101010);
unused(x);
#else
static_assert(false);
#endif
return 0;
}

0 comments on commit bc24165

Please sign in to comment.