Lua 5.3 alignment issue

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua 5.3 alignment issue

Gé Weijers
I ran into an issue interfacing code that uses multimedia extensions
(Intel SSE2) with Lua 5.3 on 64-bit Linux and macOS.

In short: some instructions loading/storing 128-bit multimedia
registers expect 16-byte alignment. malloc will provide memory aligned
to work with that, and the C compiler will align the non-portable
__m128i type at at 16-byte boundary in structures.

Trying to use these SSE2 instructions with Lua is a bit difficult,
because lua_newuserdata does not return a pointer with the same
alignment constraints as malloc, I've found that the pointer returned
is consistently aligned on an 8-byte boundary, but not on a 16-byte
one.

Digging into the Lua implementation I found that defining
LUAI_USER_ALIGNMENT_T can override the standard alignment of the
pointer returned by lua_newuserdata. Is this 'officially' supported?
(I know this introduces memory overhead on all userdata structures).

It's not overly difficult to write code that works with an unmodified
Lua interpreter, but shouldn't lua_newuserdata return pointers that
can be used in the same way as 'malloc' generated pointers?


Thanks,



Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.3 alignment issue

Roberto Ierusalimschy
> [...]
>
> Digging into the Lua implementation I found that defining
> LUAI_USER_ALIGNMENT_T can override the standard alignment of the
> pointer returned by lua_newuserdata. Is this 'officially' supported?

Yes. It exists exactly to allow that kind of correction.


> It's not overly difficult to write code that works with an unmodified
> Lua interpreter, but shouldn't lua_newuserdata return pointers that
> can be used in the same way as 'malloc' generated pointers?

I don't think it is possible to implement that in a portalbe manner
in ANSI C, unless we have zero overhead. (For instance, in your case,
you have to know about a '__m128i' type.) That is why we have the macro
LUAI_USER_ALIGNMENT_T.

-- Roberto