__builtin_expect function isn't available in gcc below version 3

82 views
Skip to first unread message

Richard Beckmann

unread,
Feb 27, 2026, 11:15:16 PM (4 days ago) Feb 27
to lu...@googlegroups.com
Dear Lua team

When compiling Lua 5.4.7 for Haiku targeting x86 32 bit using gcc 2 I encounter that '__builtin_expect' function isn't available. Btw, this specific x86 32 bit target of Haiku uses gcc version 2. This issue is also affecting Lua 5.4.8 and Lua 5.5.0.

I checked out the gcc 2.95 manual at https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html and the gcc 3.0 manual at https://gcc.gnu.org/onlinedocs/gcc-3.0/gcc_5.html. I recognize that `__builtin_expect` is in the gcc 3.0 manual but not in the gcc 2.95 manual. From there I conclude that `__builtin_expect` is only available since gcc version 3.

Based on this conclusion, one of the Haiku developers Begasus and I created a patch. In the patchset of the Lua 5.4.7 compilation for Haiku at https://github.com/haikuports/haikuports/blob/master/dev-lang/lua/patches/lua-5.4.7.patchset
the patch is as follows.
```
diff --git a/src/luaconf.h b/src/luaconf.h
index 024aa7f..9e24be9 100644
--- a/src/luaconf.h
+++ b/src/luaconf.h
@@ -701,7 +701,7 @@
 */
 #if !defined(luai_likely)
 
-#if defined(__GNUC__) && !defined(LUA_NOBUILTIN)
+#if defined(__GNUC__) && ((__GNUC__) >= 3) && !defined(LUA_NOBUILTIN)
 #define luai_likely(x) (__builtin_expect(((x) != 0), 1))
 #define luai_unlikely(x) (__builtin_expect(((x) != 0), 0))
 #else
--
2.45.2
```

In this patch `((__GNUC__) >= 3)` is added to `defined(__GNUC__)` because only gcc 3 and above have support for `__builtin_expect`.

Sincerely,

Richard Beckmann

Sent with Proton Mail secure email.

Luiz Henrique de Figueiredo

unread,
Feb 28, 2026, 5:28:11 AM (3 days ago) Feb 28
to lu...@googlegroups.com
> When compiling Lua 5.4.7 for Haiku targeting x86 32 bit using gcc 2 I encounter that '__builtin_expect' function isn't available.

In this case, just compile Lua with -DLUA_NOBUILTIN.

Thanks for the report and patch.

Richard Beckmann

unread,
Feb 28, 2026, 10:09:33 PM (3 days ago) Feb 28
to lu...@googlegroups.com
> In this case, just compile Lua with -DLUA_NOBUILTIN.

Forgot to mention. I and the Haiku developer Begasus already tried `-DLUA_NOBUILTIN` and it wasn't good enough for us. `-DLUA_NOBUILTIN` works for compiling Lua but not when using headers to compile Lua/C modules with LuaRocks. LuaRocks doesn't issue `-DLUA_NOBUILTIN` when compiling Lua/C modules. So, to make building Lua/C modules with LuaRocks work as well, we were forced to patch the headers.



Sent with Proton Mail secure email.

> --
> You received this message because you are subscribed to the Google Groups "lua-l" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/lua-l/CABt16qmjBW5iUXk36QW2Vkmon-Hxc7kB%2B35T4%3DOhVbZOESsyvQ%40mail.gmail.com.
>

Christophe Delord

unread,
Mar 1, 2026, 3:12:01 AM (2 days ago) Mar 1
to lu...@googlegroups.com

Hello,

Le 01/03/2026 à 04:09, ‘Richard Beckmann’ via lua-l a écrit :

In this case, just compile Lua with -DLUA_NOBUILTIN.
Forgot to mention. I and the Haiku developer Begasus already tried `-DLUA_NOBUILTIN` and it wasn't good enough for us. `-DLUA_NOBUILTIN` works for compiling Lua but not when using headers to compile Lua/C modules with LuaRocks. LuaRocks doesn't issue `-DLUA_NOBUILTIN` when compiling Lua/C modules. So, to make building Lua/C modules with LuaRocks work as well, we were forced to patch the headers.

Did you try to set LUA_NOBUILTIN with CFLAGS? e.g.:


CFLAGS="-DLUA_NOBUILTIN" luarocks install ...

Sent with Proton Mail secure email.

On Saturday, February 28th, 2026 at 10:28 AM, Luiz Henrique de Figueiredo <l...@tecgraf.puc-rio.br> wrote:

When compiling Lua 5.4.7 for Haiku targeting x86 32 bit using gcc 2 I encounter that '__builtin_expect' function isn't available.
In this case, just compile Lua with -DLUA_NOBUILTIN.

Thanks for the report and patch.

--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CABt16qmjBW5iUXk36QW2Vkmon-Hxc7kB%2B35T4%3DOhVbZOESsyvQ%40mail.gmail.com.


      

Richard Beckmann

unread,
Mar 1, 2026, 6:37:07 PM (2 days ago) Mar 1
to lu...@googlegroups.com
> Did you try to set `LUA_NOBUILTIN` with `CFLAGS`? e.g.:
>
> CFLAGS="-DLUA_NOBUILTIN" luarocks install ...

No, I didn't. But manually setting `-DLUA_NOBUILTIN` with CFLAGS is not what I'm looking for. If `-DLUA_NOBUILTIN` would work for me, LuaRocks would automatically apply `-DLUA_NOBUILTIN` behind the scene when running `luarocks install <package name>`. Though, I don't know if LuaRocks can be configured to do that.

Anyway, the Haiku developer Begasus and I chose to patch the headers, which does work for us. Also in my opinion, it's the easiest solution. It doesn't require to put `-DLUA_NOBUILTIN` almost everywhere.

Sent with Proton Mail secure email.

Sewbacca

unread,
Mar 1, 2026, 7:26:04 PM (2 days ago) Mar 1
to lu...@googlegroups.com
For completion's sake: yes it can be configured to do that, run

luarocks config variables.CFLAGS "-DNO_BUILTIN"

On the tree that you are using.

~ Sewbacca

02.03.2026 00:37:09 'Richard Beckmann' via lua-l <lu...@googlegroups.com>:

Reply all
Reply to author
Forward
0 new messages