# Undefined reference to __builtin_ctzll

2 messages from 2014-08-13 to 2014-08-13. Participants: Радослав Йовчев, Erik Faye-Lund.
Thread: https://gitlist.dev/t/37342

## Радослав Йовчев, 2014-08-13 08:36

Subject: Undefined reference to __builtin_ctzll
Message-ID: <CAAC8vw4EzaEM13TWzEW9QUuc-qdZFhPfEA9z1=KjCE0Brzotjg@mail.gmail.com>
URL: https://gitlist.dev/e/CAAC8vw4EzaEM13TWzEW9QUuc-qdZFhPfEA9z1%3DKjCE0Brzotjg%40mail.gmail.com

```
Dear GIT community,


I found myself in situation where I had to install GIT on Debian 3.1
sarge.  It comes with GCC 3.3.5. I tried to built from source but the
libgcc was not providing the ctzll function, thus I decided to put an
implementation.


I do not know how to post and do a nice patch (and whether somebody
will care), but I guess, for reference I can post my solution. Just
appended in compat/strlcpy.c the following:


int __builtin_ctzll (long long x)
{
        int i;
        for (i = 0; i < 8 * sizeof (long long); ++i)
                if (x & ((long long) 1  << i))
                        break;
        return i;
}


I guess that some ifdef macro can be used to detect compiler version
or missing __builtin_ctzll.


With best regards and hopes that this can help somebody in similar situation,
Radoslav.

```

## Erik Faye-Lund, 2014-08-13 10:15

Subject: Re: Undefined reference to __builtin_ctzll
Message-ID: <CABPQNSZw4vwhF8m+TWrbuOSaV5ycRGm1oeCu42j96_T+f3xtTg@mail.gmail.com>
URL: https://gitlist.dev/e/CABPQNSZw4vwhF8m%2BTWrbuOSaV5ycRGm1oeCu42j96_T%2Bf3xtTg%40mail.gmail.com
In-Reply-To: <CAAC8vw4EzaEM13TWzEW9QUuc-qdZFhPfEA9z1=KjCE0Brzotjg@mail.gmail.com>

```
On Wed, Aug 13, 2014 at 10:36 AM, Радослав Йовчев <radoslav.tm@gmail.com> wrote:
> Dear GIT community,
>
>
> I found myself in situation where I had to install GIT on Debian 3.1
> sarge.  It comes with GCC 3.3.5. I tried to built from source but the
> libgcc was not providing the ctzll function, thus I decided to put an
> implementation.
>
>
> I do not know how to post and do a nice patch (and whether somebody
> will care), but I guess, for reference I can post my solution. Just
> appended in compat/strlcpy.c the following:
>
>
> int __builtin_ctzll (long long x)
> {
>         int i;
>         for (i = 0; i < 8 * sizeof (long long); ++i)
>                 if (x & ((long long) 1  << i))
>                         break;
>         return i;
> }
>
>
> I guess that some ifdef macro can be used to detect compiler version
> or missing __builtin_ctzll.

It seems __builtin_ctzll is only available in GCC 3.4.0 and beyond, so
I think a better fix is something like this:

diff --git a/ewah/ewok.h b/ewah/ewok.h
index 43adeb5..2700fa3 100644
--- a/ewah/ewok.h
+++ b/ewah/ewok.h
@@ -47,7 +47,7 @@ static inline uint32_t ewah_bit_popcount64(uint64_t x)
  return (x * 0x0101010101010101ULL) >> 56;
 }

-#ifdef __GNUC__
+#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ > 3
 #define ewah_bit_ctz64(x) __builtin_ctzll(x)
 #else
 static inline int ewah_bit_ctz64(uint64_t x)

```
