-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constant Arrays #152
Comments
Here is some example code to do this. I don't remember offhand why what you local N = 32 local ctable = terralib.constant(tbl) terra sintable(a : float) : float return ctable[idx] sintable:disas() print(sintable(0)) On Sat, Jan 16, 2016 at 4:00 PM, Brandon Bloom notifications@github.com
|
Thanks! I'll leave this ticket open in case you ever want to dig in to the nature of my problem. I've got to say, this project is fantastic. I've been writing some C, doing some gnarly text-template metaprogramming, and toying with Rust every now and again. I was able to pick up Lua in one evening and Terra the next, and was instantly productive with some pretty sophisticated metaprogramming. Thanks! |
I just wasted about 30 minutes figuring out how to make constant sized arrays of a specific base type - @brandonbloom's #152 (comment) taught me This is off-topic a bit, but if anyone is interested in a completely ridiculous way to create a sized array type, here you go: local function instfor(typ)
-- evil little C trick that translates nicely into Terra
return (`@([&typ](nil)))
end
local function sized_array_type(base_type, num_entries)
-- this is so ridiculous, but it works: returns `base_type[num_entries]` as a type.
local entries = {}
for i=1,num_entries do table.insert(entries, instfor(base_type)) end
return (`array([entries])):gettype()
end Terra is pretty neat (: |
Is there any way to make a constant array directly out of a Lua array without creating the temporary cdata buffer? |
On newer versions of Terra the following should work:
This is basically making a quote with the list of elements, and passing that to |
@elliottslaughter doesn't that still use unpack() and is thus limited to some 200 args? I had that problem before. |
Hm, that does seem to be the case. At least on my machine, the limit is 7999 args. Not sure why it's that exact value, but beyond that I get:
with the program:
However, I'm guessing this is just an implementation detail. I don't see anything in the above code that would indicate that it has to be implemented with |
In case anyone comes here looking for a workaround, I'm copying what @zdevito posted up earlier in the threat (cleaned up slightly for presentation): local N = 8000
local t = terralib.new(float[N])
for i = 1,N do
t[i-1] = i
end
local c = terralib.constant(t)
terra f(x : int)
return c[x]
end
print(f(0))
print(f(1))
print(f(2)) |
The documentation reads:
The constant function seems to work just fine for integers I've tried it on. However, I can't figure out how to make this work with arrays. I'd expect this to work:
Am I missing something?
The text was updated successfully, but these errors were encountered: