mirror of
https://github.com/LuaJIT/LuaJIT.git
synced 2025-04-19 21:43:27 +00:00
Mirror of the LuaJIT git repository
![]() 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 |
||
---|---|---|
doc | ||
dynasm | ||
etc | ||
src | ||
.gitattributes | ||
.gitignore | ||
.relver | ||
COPYRIGHT | ||
Makefile | ||
README |
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