Mirror of the LuaJIT git repository
Go to file
Kacper Michajłow 6c91ac9cec Add -fno-strict-float-cast-overflow complation flag
LuaJIT checks whether a Lua number can be represented as an integer by
casting the number (`double`) to an `int32_t` and comparing the result
to the original. While this approach works in practice, it is
technically UB according to the C standard. Because it's UB, compilers
are free to make optimizations that may not preserve the original intent
of the code. Clang/LLVM, in particular, takes advantage of this.

Consider the following code:

```c
int32_t k = lj_num2int(n);
if (n == (lua_Number)k)
  ...
```

Clang short-circuit this to do basically

```c
if (n == trunc(n))
  ...
```

It can do this because it assumes that `n` is either representable as an
`int32_t` or the behavior is undefined, which allows it to compare the
truncated `n` directly without needing to convert `k` back to a
`double`.

We could implement a C-compliant saturating cast from floating point to
integer ourselves, but that awkward, and more useless code, especially
since most FPUs already handle such conversions natively with
saturation.

Alternatively, we could use intrinsics or inline asm to ensure the
correct saturating conversion is used.

But there's a simpler option: we can ask the compiler to handle this
correctly for us.

This commit adds the `-fno-strict-float-cast-overflow` compilation flag, which:
> With the ‘no-strict’ option, Clang will saturate towards the smallest
and largest representable integer values instead. NaNs will be converted
to zero.

GCC does not currently exploit this UB as aggressively, so it's not an
issue there. MSVC also appears unaffected, though it provides built-in
functions for saturating conversions, if someone wants to use them
directly
https://learn.microsoft.com/cpp/intrinsics/saturation-conversion-functions

This commit fixes assumptions that LuaJIT makes in the code.

Fixes: #1351
2025-04-07 22:11:56 +02:00
doc Windows: Clarify installation directory layout. 2025-03-09 16:10:22 +01:00
dynasm Merge branch 'master' into v2.1 2025-01-13 16:15:19 +01:00
etc Merge branch 'master' into v2.1 2025-01-13 16:15:19 +01:00
src Add -fno-strict-float-cast-overflow complation flag 2025-04-07 22:11:56 +02:00
.gitattributes Add .gitattributes to dynamically resolve .relver. 2023-08-22 15:30:27 +02:00
.gitignore RELEASE LuaJIT-2.0.0-beta1 2009-12-08 19:46:35 +01:00
.relver Add .gitattributes to dynamically resolve .relver. 2023-08-22 15:30:27 +02:00
COPYRIGHT Bump copyright date. 2025-01-13 15:59:10 +01:00
Makefile Use dylib extension for iOS installs, too. 2025-03-09 15:00:15 +01:00
README Merge branch 'master' into v2.1 2025-01-13 16:15:19 +01:00

README for LuaJIT 2.1
---------------------

LuaJIT is a Just-In-Time (JIT) compiler for the Lua programming language.

Project Homepage: https://luajit.org/

LuaJIT is Copyright (C) 2005-2025 Mike Pall.
LuaJIT is free software, released under the MIT license.
See full Copyright Notice in the COPYRIGHT file or in luajit.h.

Documentation for LuaJIT is available in HTML format.
Please point your favorite browser to:

 doc/luajit.html