lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

>> SIMD is probably done better through Dynasm as some extension library
> Well, this is not on my horizon, yet. But a generic mechanism
to embed machine code from the Lua side would be pretty nice.
Better than coding in C and/or writing C inline assembler (which
is hard to use and non-portable across compilers).

A quick idea (which I haven't really thought about at length, and
perhaps LuaJIT goes some way to solving already) could be to somehow be
able to bind to/access binary objects (e.g. C structs). Perhaps you
could specify the data you want to expose:

C Style:
	vec3 = luajit.expose("struct { float x,y,z; };")

Or Lua style:
	vec3 = luajit.create_struct("vector") : 
				add("float","x") : 
				add("float","y") : 

You might have special type names so that specific instructions could be
issued on platforms that allowed those optimizations, e.g. type
"__vector" which would be beneficial for platforms that allowed you to
do SIMD instructions on vector types. E.g.

	vec3 = luajit.create_struct("vector") : add("__vector",

	-- usage example
	function vec3_diff(a,b)
		local diff = vec3:construct()
		diff = a - b
		return diff

This way you might be able to both allow manipulation of binary data
from Lua and also to allow binding to C side structures for fast
manipulation. In the example I gave before (
there is a Lisp accessor method "->", e.g. "(-> myvec x)". Currently
you'd have to write binding code (perhaps a function which takes a
vector and returns the required scalar value, "get_vec_x"). I've found
in the past that a significant amount of time is spent in the binding
(e.g. type checking) of larger projects which expose a lot of
functionality from C. Whilst Lua is very fast a large amount of time is
"wasted" in the binding (doing type checking because of the dynamic
nature of the language). If the binding can be broken down (e.g. compile
time type checking and appropriate asm code generation) there could be
very significant gains.