decode: Use uint16_t for trie

This commit is contained in:
Alexis Engelke
2020-06-17 16:42:07 +02:00
parent 1fedc069b6
commit f4b41a7e80
2 changed files with 12 additions and 11 deletions

View File

@@ -14,7 +14,7 @@
#define UNLIKELY(x) __builtin_expect((x), 0) #define UNLIKELY(x) __builtin_expect((x), 0)
#define FD_DECODE_TABLE_DATA #define FD_DECODE_TABLE_DATA
static __attribute__((aligned(16))) const uint8_t _decode_table[] = { static __attribute__((aligned(16))) const uint16_t _decode_table[] = {
#include <fadec-decode-table.inc> #include <fadec-decode-table.inc>
}; };
#undef FD_DECODE_TABLE_DATA #undef FD_DECODE_TABLE_DATA
@@ -44,7 +44,7 @@ typedef enum DecodeMode DecodeMode;
#define ENTRY_UNPACK(table,kind,entry) do { \ #define ENTRY_UNPACK(table,kind,entry) do { \
uint16_t entry_copy = entry; \ uint16_t entry_copy = entry; \
table = &((uint16_t*) _decode_table)[(entry_copy & ~7) >> 1]; \ table = &_decode_table[(entry_copy & ~7) >> 1]; \
kind = entry_copy & ENTRY_MASK; \ kind = entry_copy & ENTRY_MASK; \
} while (0) } while (0)
@@ -323,11 +323,11 @@ fd_decode(const uint8_t* buffer, size_t len_sz, int mode_int, uintptr_t address,
// Ensure that we can actually handle the decode request // Ensure that we can actually handle the decode request
#if defined(ARCH_386) #if defined(ARCH_386)
if (mode == DECODE_32) if (mode == DECODE_32)
table = &((uint16_t*) _decode_table)[FD_TABLE_OFFSET_32 >> 1]; table = &_decode_table[FD_TABLE_OFFSET_32];
#endif #endif
#if defined(ARCH_X86_64) #if defined(ARCH_X86_64)
if (mode == DECODE_64) if (mode == DECODE_64)
table = &((uint16_t*) _decode_table)[FD_TABLE_OFFSET_64 >> 1]; table = &_decode_table[FD_TABLE_OFFSET_64];
#endif #endif
if (UNLIKELY(table == NULL)) if (UNLIKELY(table == NULL))

View File

@@ -336,12 +336,12 @@ class Table:
for name, entry in self.data.items(): for name, entry in self.data.items():
self.annotations[current] = "%s(%d)" % (name, entry.kind.value) self.annotations[current] = "%s(%d)" % (name, entry.kind.value)
self.offsets[name] = current self.offsets[name] = current
current += (2*entry.encode_length + 7) & ~7 current += (entry.encode_length + 3) & ~3
if current >= 0x10000: if current >= 0x8000:
raise Exception("maximum table size exceeded: {:x}".format(current)) raise Exception("maximum table size exceeded: {:x}".format(current))
def encode_item(self, name): def encode_item(self, name):
return self.offsets[name] | self.data[name].kind.value return (self.offsets[name] << 1) | self.data[name].kind.value
def compile(self): def compile(self):
self.calc_offsets() self.calc_offsets()
@@ -349,7 +349,7 @@ class Table:
data = () data = ()
for off, entry in ordered: for off, entry in ordered:
data += (0,) * (off//2 - len(data)) + entry.encode(self.encode_item) data += (0,) * (off - len(data)) + entry.encode(self.encode_item)
data = struct.pack("<%dH"%len(data), *data) data = struct.pack("<%dH"%len(data), *data)
@@ -358,12 +358,13 @@ class Table:
return data, self.annotations, [self.offsets[k] for k in self.roots] return data, self.annotations, [self.offsets[k] for k in self.roots]
def wrap(string): def wrap(string):
return "\n".join(string[i:i+80] for i in range(0, len(string), 80)) return "\n".join(string[i:i+56] for i in range(0, len(string), 56))
def bytes_to_table(data, notes): def bytes_to_table(data, notes):
hexdata = ",".join("0x{:02x}".format(byte) for byte in data) hexdata = ",".join("0x{:04x}".format(int.from_bytes(data[i:i+2], "little"))
for i in range(0, len(data), 2))
offs = [0] + sorted(notes.keys()) + [len(data)] offs = [0] + sorted(notes.keys()) + [len(data)]
return "\n".join(wrap(hexdata[p*5:c*5]) + "\n//%04x "%c + notes.get(c, "") return "\n".join(wrap(hexdata[p*7:c*7]) + "\n//%04x "%c + notes.get(c, "")
for p, c in zip(offs, offs[1:])) for p, c in zip(offs, offs[1:]))
template = """// Auto-generated file -- do not modify! template = """// Auto-generated file -- do not modify!