240
241
242
243
244
245
246
247
248
249
250
251
252
253
...
265
266
267
268
269
270
271
272
273
274
275
276
277
278
...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
}
}
n.name = string.format("stat<%s>", ty.name)
n.stat_basetype = ty
return n
end)
lib.enum = function(tbl)
local ty = uint8
if #tbl >= 2^32 then ty = uint64 -- hey, can't be too safe
elseif #tbl >= 2^16 then ty = uint32
elseif #tbl >= 2^8 then ty = uint16 end
local o = { t = ty, members = tbl }
local strings = {}
for i, name in ipairs(tbl) do
................................................................................
local o = {}
for i, name in ipairs(tbl) do o[name] = i end
local struct set { _store: uint8[bytes] }
local struct bit { _v: intptr _set: &set}
terra set:clear() for i=0,bytes do self._store[i] = 0 end end
terra set:fill() for i=0,bytes do self._store[i] = 0xFF end end
set.members = tbl
set.name = string.format('set<%s>', table.concat(tbl, '|'))
set.metamethods.__entrymissing = macro(function(val, obj)
if o[val] == nil then error('value ' .. val .. ' not in set') end
return `bit { _v=[o[val] - 1], _set = &(obj) }
end)
terra set:sz()
var ct: intptr = 0
................................................................................
terra set:setbit(i: intptr, val: bool)
if val then
self._store[i/8] = self._store[i/8] or (1 << (i % 8))
else
self._store[i/8] = self._store[i/8] and not (1 << (i % 8))
end
end
set.bits = {}
set.idvmap = {}
for i,v in ipairs(tbl) do
set.idvmap[v] = i
set.bits[v] = quote var b: set b:clear() b:setbit([i-1], true) in b end
end
set.metamethods.__add = macro(function(self,other)
local new = symbol(set)
local q = quote var [new] new:clear() end
for i = 0, bytes - 1 do
q = quote [q]
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
<
|
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
...
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
...
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
}
}
n.name = string.format("stat<%s>", ty.name)
n.stat_basetype = ty
return n
end)
lib.enum = function(tbl)
if type(tbl) == 'string' then -- shorthand syntax
local t = {}
for w in tbl:gmatch('(%g+)') do t[#t+1] = w end
tbl = t
end
local ty = uint8
if #tbl >= 2^32 then ty = uint64 -- hey, can't be too safe
elseif #tbl >= 2^16 then ty = uint32
elseif #tbl >= 2^8 then ty = uint16 end
local o = { t = ty, members = tbl }
local strings = {}
for i, name in ipairs(tbl) do
................................................................................
local o = {}
for i, name in ipairs(tbl) do o[name] = i end
local struct set { _store: uint8[bytes] }
local struct bit { _v: intptr _set: &set}
terra set:clear() for i=0,bytes do self._store[i] = 0 end end
terra set:fill() for i=0,bytes do self._store[i] = 0xFF end end
set.members = tbl
set.idvmap = o
set.null = quote var s: set s:clear() in s end
set.name = string.format('set<%s>', table.concat(tbl, '|'))
set.metamethods.__entrymissing = macro(function(val, obj)
if o[val] == nil then error('value ' .. val .. ' not in set') end
return `bit { _v=[o[val] - 1], _set = &(obj) }
end)
terra set:sz()
var ct: intptr = 0
................................................................................
terra set:setbit(i: intptr, val: bool)
if val then
self._store[i/8] = self._store[i/8] or (1 << (i % 8))
else
self._store[i/8] = self._store[i/8] and not (1 << (i % 8))
end
end
local qksetexp = function(self,idx,rhs)
local ch,bit
if terralib.isconstant(idx) then
ch = math.floor(idx/8)
bit = idx%8
else
ch = `[idx]/8
bit = `[idx]%8
end
if terralib.isconstant(rhs) then
local b = rhs:asvalue()
if b == true then return quote
self._store[ch] = self._store[ch] or (1 << bit)
end elseif b == false then return quote
self._store[ch] = self._store[ch] and not (1 << bit)
end end
else
return quote self:setbit([ch], rhs) end
end
end
set.metamethods.__update = macro(qksetexp)
set.metamethods.__setentry = macro(function(field,self,rhs)
return `self:setbit([o[field]-1],rhs)
--return qksetexp(self, o[field], rhs)
end)
set.bits = {}
for i,v in ipairs(tbl) do
set.bits[v] = quote var b: set b:clear() b:setbit([i-1], true) in b end
end
set.metamethods.__add = macro(function(self,other)
local new = symbol(set)
local q = quote var [new] new:clear() end
for i = 0, bytes - 1 do
q = quote [q]
|