summaryrefslogtreecommitdiff
path: root/itoa_gen.py
blob: 0437c622f5880cf39dcc61eb0ba3f8e8b1bb0eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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] = {")
    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("};")