Skip to content

Commit

Permalink
popcount: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
XPhyro committed Mar 8, 2024
1 parent 318ad10 commit 50fb8c8
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/c/util/math/popcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
#include <sys/types.h>
#include <unistd.h>

#if !defined(BUILTIN) && (defined(__GNUC__) || defined(__clang__))
#define USE_BUILTIN
#endif

int main(int argc, char *argv[])
{
ssize_t nread, i;
size_t popcount;
char buf[PIPE_BUF];
#if defined(__GNUC__) || defined(__clang__)
#ifndef USE_BUILTIN
unsigned int c;
#else
char c;
Expand All @@ -18,7 +22,7 @@ int main(int argc, char *argv[])
while ((nread = read(STDIN_FILENO, buf, PIPE_BUF)) > 0) {
for (i = 0; i < nread; i++) {
c = buf[i];
#if defined(__GNUC__) || defined(__clang__)
#ifndef USE_BUILTIN
popcount += __builtin_popcount(c & 0xff);
#else
popcount += c & 0x01 + c & 0x02 + c & 0x04 + c & 0x08 + c & 0x10 + c & 0x20 + c &
Expand Down

0 comments on commit 50fb8c8

Please sign in to comment.