https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125817

            Bug ID: 125817
           Summary: __builtin_popcountg() doesn't work for large _BitInt()
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: [email protected]
  Target Milestone: ---

>From the program below, __builtin_popcountg() only counts the low 32 bits of a
_BitInt(256).  Is there any limit in how large a bitint can be as input to this
builtin?  If so, I'd expect a diagnostic when passing a larger one.  If not
(most likely), then this seems to be just an implementation bug.


alx@devuan:~/tmp$ cat foo.c 
#include <stdcountof.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

union ln {
        unsigned _BitInt(256)  ln;
        unsigned _BitInt(1)    e[256];
};

[[gnu::noipa]]
void
print_ln(FILE *stream, size_t n, const union ln l[1])
{
        char  *p;
        char  txt[n + countof("\n")];


        p = txt;
        for (size_t i = 0; i < n; i++)
                *p++ = l->e[i] + '0';
        strcpy(p, "\n");
        fputs(txt, stream);
}

[[gnu::noipa]]
size_t
sum_ln(const union ln l[1])
{
        return __builtin_popcountg(l->ln);
}

int
main(void)
{
        union ln  l = {};

        l.e[5] = true;
        l.e[4] = true;
        l.e[2] = true;
        l.e[7] = true;
        l.e[20] = true;
        l.e[30] = true;
        l.e[60] = true;
        l.e[77] = true;

        printf("%zu\n", sum_ln(&l));
        print_ln(stdout, 128, &l);
}
alx@devuan:~/tmp$ gcc-16 -Wall -Wextra foo.c 
alx@devuan:~/tmp$ ./a.out 
6
00101101000000000000100000000010000000000000000000000000000010000000000000000100000000000000000000000000000000000000000000000000
alx@devuan:~/tmp$ ./a.out | tail -n1 | tr -d 0 | wc -L
8

Reply via email to