cswork / libraryOpen practice workspace

Coding questions · Anthropic

Apply bitwise tricks for performance

This question evaluates proficiency with bitwise operations, branchless programming, low-level performance optimizations, and microarchitectural reasoning about pipeline stalls, instruction-level parallelism, and cache behavior.

For an integer-heavy inner loop, propose bit-level optimizations that reduce branches and memory traffic: e.g., population count usage, fast modulo by power-of-two with masks, branchless conditional updates via bitwise selects, and alignment checks. Provide concise code examples, explain correctness, and analyze the microarchitectural impact (pipeline stalls, ILP, cache behavior).

Apply bitwise tricks for performance

You are given a list of non-negative integers nums, a bucket_count that is guaranteed to be a power of two, an align value that is also a power of two, and an integer threshold.

Simulate the following integer-heavy inner loop and return the final scores array:

scores = [0] * bucket_count
for x in nums:
    bucket = x & (bucket_count - 1)
    ones = popcount(x)
    signed = ones if ones >= threshold else -ones
    delta = signed if x is aligned to align else -signed
    scores[bucket] += delta

Here, popcount(x) is the number of set bits in x, and x is aligned to align when x % align == 0.

This problem intentionally mirrors common bit-level performance tricks:

  • x & (bucket_count - 1) replaces modulo when bucket_count is a power of two.
  • x.bit_count() is a population count.
  • A branchless select can be written as b ^ ((a ^ b) & mask) with mask = -int(condition).
  • Alignment can be checked with (x & (align - 1)) == 0.

Why these tricks are correct: for powers of two, masking keeps exactly the low bits needed for modulo, and alignment means those same low bits are all zero. On real CPUs, these transformations avoid expensive division, reduce unpredictable branches that can cause pipeline stalls, expose more instruction-level parallelism, and keep the hot scores array cache-friendly, reducing memory traffic.

Constraints

  • 0 <= len(nums) <= 200000
  • 0 <= nums[i] < 2^63
  • 1 <= bucket_count <= 65536, and bucket_count is a power of two
  • 1 <= align <= 1048576, and align is a power of two
  • 0 <= threshold <= 63

Examples

Example 1

Input: ([5, 8, 3], 4, 4, 2)
Output: [-1, -2, 0, -2]

Explanation: 5 has popcount 2 and is unaligned, so bucket 1 gets -2. 8 has popcount 1, is aligned, and contributes -1 to bucket 0. 3 has popcount 2 and is unaligned, so bucket 3 gets -2.

Example 2

Input: ([4, 7, 12, 2], 8, 4, 2)
Output: [0, 0, 1, 0, 1, 0, 0, -3]

Explanation: 4 contributes -1 to bucket 4, 7 contributes -3 to bucket 7, 12 contributes +2 to bucket 4, and 2 is unaligned with popcount 1 so it flips sign and contributes +1 to bucket 2.

Hints

  1. Because bucket_count is a power of two, x % bucket_count can be replaced by a bit mask on the low bits.
  2. Think of each if as choosing between two values. A boolean can be turned into a mask and used for a branchless select.