Bit Intrinsics
Bit intrinsics are usually used for operating on masks, to compact data structures and binary encodings. alpaka exposes portable helpers such as popcount, ffs, and clz.
Bit-Manipulation Kernel
The example is only showing how to use the functions.
struct BitIntrinsicKernel { ALPAKA_FN_ACC void operator()( onAcc::concepts::Acc auto const& acc, concepts::IMdSpan auto popCounts, concepts::IMdSpan auto firstSetBits, concepts::IMdSpan auto leadingZeros, concepts::IDataSource auto const& input) const { for(auto [idx] : onAcc::makeIdxMap(acc, onAcc::worker::threadsInGrid, IdxRange{input.getExtents()})) { auto value = input[idx]; // number of bits set to 1 popCounts[idx] = popcount(value); // position of the least significant bit set to 1 firstSetBits[idx] = ffs(value); // number of most significant zero bits leadingZeros[idx] = clz(value); } } };
The three operations in this example are:
popcount(value): number of set bits, typically used to count the participating threads in a warp after the warp operationwarp::ballot()was called.ffs(value): position of the first set bit, using1as the first position and0for the zero value, typically used in memory allocators to find the next free slot within a chunk based storage.clz(value): number of leading zero bits, often used for hash tables and within allocators.
Complete Source File
191_intrinsics.cpp
1/* Copyright 2026 René Widera
2 * SPDX-License-Identifier: ISC
3 */
4
5#include "docsTest.hpp"
6
7#include <alpaka/alpaka.hpp>
8
9#include <catch2/catch_template_test_macros.hpp>
10#include <catch2/catch_test_macros.hpp>
11
12#include <array>
13#include <bit>
14#include <limits>
15
16using namespace alpaka;
17
18struct BitIntrinsicKernel
19{
20 ALPAKA_FN_ACC void operator()(
21 onAcc::concepts::Acc auto const& acc,
22 concepts::IMdSpan auto popCounts,
23 concepts::IMdSpan auto firstSetBits,
24 concepts::IMdSpan auto leadingZeros,
25 concepts::IDataSource auto const& input) const
26 {
27 for(auto [idx] : onAcc::makeIdxMap(acc, onAcc::worker::threadsInGrid, IdxRange{input.getExtents()}))
28 {
29 auto value = input[idx];
30 // number of bits set to 1
31 popCounts[idx] = popcount(value);
32 // position of the least significant bit set to 1
33 firstSetBits[idx] = ffs(value);
34 // number of most significant zero bits
35 leadingZeros[idx] = clz(value);
36 }
37 }
38};
39
40
41TEMPLATE_LIST_TEST_CASE("tutorial intrinsics", "[docs]", docs::test::TestBackends)
42{
43 auto cfg = TestType::makeDict();
44 auto exec = alpaka::getExecutor(cfg);
45
46 auto selector = onHost::makeDeviceSelector(cfg);
47 if(!selector.isAvailable())
48 return;
49 onHost::concepts::Device auto device = selector.makeDevice(0);
50 onHost::Queue queue = device.makeQueue(queueKind::blocking);
51
52 std::array<uint32_t, 4u> hostInput{0u, 1u, 0b1011'0000u, 0xFFFF'0000u};
53 std::array<int32_t, 4u> hostPopCount{};
54 std::array<int32_t, 4u> hostFfs{};
55 std::array<int32_t, 4u> hostClz{};
56
57 auto inputBuffer = onHost::allocLike(device, hostInput);
58 auto popCountBuffer = onHost::allocLike(device, hostPopCount);
59 auto ffsBuffer = onHost::allocLike(device, hostFfs);
60 auto clzBuffer = onHost::allocLike(device, hostClz);
61
62 onHost::memcpy(queue, inputBuffer, hostInput);
63
64 onHost::concepts::FrameSpec auto frameSpec = onHost::getFrameSpec(device, exec, inputBuffer.getExtents());
65 queue.enqueue(frameSpec, KernelBundle{BitIntrinsicKernel{}, popCountBuffer, ffsBuffer, clzBuffer, inputBuffer});
66
67 onHost::memcpy(queue, hostPopCount, popCountBuffer);
68 onHost::memcpy(queue, hostFfs, ffsBuffer);
69 onHost::memcpy(queue, hostClz, clzBuffer);
70 onHost::wait(queue);
71
72 for(size_t i = 0; i < hostInput.size(); ++i)
73 {
74 auto value = hostInput[i];
75 CHECK(hostPopCount[i] == std::popcount(value));
76 CHECK(hostFfs[i] == (value == 0u ? 0 : static_cast<int32_t>(std::countr_zero(value) + 1u)));
77 CHECK(
78 hostClz[i]
79 == (value == 0u ? std::numeric_limits<uint32_t>::digits : static_cast<int32_t>(std::countl_zero(value))));
80 }
81}