AArch64, 48 44 bytes
Raw instructions (32-bit little endian hex):
1e2703e4 bc404400 0e205801 2e303821
0ea43c23 2ea31c02 2ea31c24 f1000421
54ffff21 1e260040 d65f03c0
Uncommented assembly:
.globl bittiest
bittiest:
fmov s4, #0
.Lloop:
ldr s0, [x0], #4
cnt v1.8b, v0.8b
uaddlv h1, v1.8b
cmge v3.2s, v1.2s, v4.2s
bit v2.8b, v0.8b, v3.8b
bit v4.8b, v1.8b, v3.8b
subs x1, x1, #1
bne .Lloop
fmov w0, s2
ret
Explanation
C function signature:
int32_t bittiest(int32_t *words, size_t len);
Pseudo-C:
int32_t bittiest(int32_t *words, size_t len)
{
int32_t maxcount = 0;
int32_t maxvalue;
do {
int32_t value = *words++;
int8_t counts[4] = popcount8x4((int8_t *)&value);
int32_t count = counts[0] + counts[1] + counts[2] + counts[3];
if (count >= maxcount) {
maxvalue = value;
maxcount = count;
}
} while (--len);
return maxvalue;
}
AArch64's population count instruction is in NEON (the SIMD/floating point instruction set), and it counts each byte individually. Therefore, it is a little awkward to work with scalars here so we do everything in NEON.
v4 is the max population count (v4, s4, h4, and d4 all refer to the same register). Set it to 0.
fmov s4, #0
Load the next int32 word into v0, and increment words (x0) by 4.
ldr s0, [x0], #4
Store the population count of each byte in v0 into the corresponding byte in v1.
cnt v1.8b, v0.8b
Add all of the 8-bit lanes in v1 together to get the full population count, and store into v1 again.
uaddlv h1, v1.8b
Compare the population count of this word to the maximum. If it is larger or equal, v3 will be all 1 bits (true), otherwise it will be all 0 bits (false).
cmge v3.2s, v1.2s, v4.2s
If v3 is true, set the max word (v2) to the current word.
max is not initialized on the first iteration, but it will always be set because the population count will always be >= 0.
bit v2.8b, v0.8b, v3.8b
Same, but for the new max population count.
bit v4.8b, v1.8b, v3.8b
Decrement len (x1), and loop if it is not zero
subs x1, x1, #1
bne .Lloop
End of loop: Move the maximum value from a NEON register to the return register (w0), and return.
fmov w0, s2
ret
11 instructions = 44 bytes
-2^31
and2^31-1
, and we output one whose 32-bit signed representation has the most 1's. \$\endgroup\$