diff options
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | calc.c | 20 | ||||
| -rw-r--r-- | itoa_gen.py | 17 |
3 files changed, 21 insertions, 19 deletions
@@ -7,6 +7,9 @@ build: calc.c lut bench: build ./calc 10.0.0.0/8 2 +bigbench: build + ./calc 10.0.0.0/6 2 + lut: itoa_gen.py python3 ./itoa_gen.py @@ -32,20 +32,20 @@ uint32_t subnetmask_for_netspace(unsigned int desired_netspace) { void write_ip(char **chunk_ptr, uint32_t ip) { // local ptr so we don't have to deref every time char *ptr = *chunk_ptr; - LutEntry e = {.bits = itoa_lut[(ip >> 24) & 0xff]}; - WRITE_STR(ptr, e.fields.chars, e.fields.len) + LutEntry e = itoa_lut[(ip >> 24) & 0xff]; + WRITE_STR(ptr, e.chars, e.len) ptr[0] = '.'; ptr += 1; - e.bits = itoa_lut[(ip >> 16) & 0xff]; - WRITE_STR(ptr, e.fields.chars, e.fields.len) + e = itoa_lut[(ip >> 16) & 0xff]; + WRITE_STR(ptr, e.chars, e.len) ptr[0] = '.'; ptr += 1; - e.bits = itoa_lut[(ip >> 8) & 0xff]; - WRITE_STR(ptr, e.fields.chars, e.fields.len) + e = itoa_lut[(ip >> 8) & 0xff]; + WRITE_STR(ptr, e.chars, e.len) ptr[0] = '.'; ptr += 1; - e.bits = itoa_lut[ip & 0xff]; - WRITE_STR(ptr, e.fields.chars, e.fields.len) + e = itoa_lut[ip & 0xff]; + WRITE_STR(ptr, e.chars, e.len) *chunk_ptr = ptr; } @@ -106,8 +106,8 @@ int main(int argc, char** argv) { write_ip(&chunk_ptr, broadcast); WRITE_STR(chunk_ptr, "\",\n", 3) WRITE_STR(chunk_ptr, "\t\t\"Nbr\": \"", 10) - LutEntry e = {.bits = itoa_lut[ips_per_subnet - 2]}; - WRITE_STR(chunk_ptr, e.fields.chars, e.fields.len); + LutEntry e = itoa_lut[ips_per_subnet - 2]; + WRITE_STR(chunk_ptr, e.chars, e.len); if (i+1 == req.count) { WRITE_STR(chunk_ptr, "\"\n\t}\n", 5) }else { diff --git a/itoa_gen.py b/itoa_gen.py index 0437c62..2c62ed5 100644 --- a/itoa_gen.py +++ b/itoa_gen.py @@ -1,15 +1,14 @@ with open("itoa_lut.h", "w") as f: f.write("#include <stdint.h>\n") - f.write("typedef union { struct { char chars[3]; uint8_t len; } fields; uint32_t bits; } LutEntry;\n") - f.write("const uint32_t itoa_lut[256] = {") + f.write("typedef struct { char chars[3]; uint8_t len; } LutEntry;\n") + f.write("const LutEntry itoa_lut[256] = {\n") out = [] for i in range(0, 256): running=len(str(i)) - for j in reversed(range(3)): - running = running << 8 - if j < len(str(i)): - running += ord(str(i)[j]) - out.append(hex(running)) - f.write(", ".join(out)) - f.write("};") + s = str(i) + while len(s) < 3: + s=s+chr(69) + out.append("{{"+",".join(map(lambda x: f"'{x}'", s))+"}, "+str(len(str(i)))+"}") + f.write(",\n".join(out)) + f.write("\n};") |
