forked from YosysHQ/icestorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicecompr.py
executable file
·135 lines (112 loc) · 3.7 KB
/
icecompr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
def make_int_bits(value, nbits):
bits = list()
for i in range(nbits-1, -1, -1):
bits.append((value & (1 << i)) != 0)
return bits
def ice_compress_bits(inbits):
outbits = list()
outbits += make_int_bits(0x49434543, 32)
outbits += make_int_bits(0x4f4d5052, 32)
deltas = list()
numzeros = 0
for bit in inbits:
if bit:
deltas.append(numzeros)
numzeros = 0
else:
numzeros += 1
i = 0
while i < len(deltas):
raw_len = 0
compr_len = 0
best_compr_raw_diff = -1
best_compr_raw_idx = -1
best_compr_raw_len = -1
for j in range(len(deltas) - i):
delta = deltas[i+j]
raw_len += delta + 1
if delta < 4:
compr_len += 3
elif delta < 32:
compr_len += 7
elif delta < 256:
compr_len += 11
else:
compr_len += 26
if compr_len - raw_len < max(best_compr_raw_diff - 4, 0) or raw_len > 64:
break
if compr_len - raw_len > best_compr_raw_diff:
best_compr_raw_diff = compr_len - raw_len
best_compr_raw_idx = j
best_compr_raw_len = raw_len
if best_compr_raw_diff > 9:
outbits.append(False)
outbits.append(False)
outbits.append(False)
outbits.append(True)
outbits += make_int_bits(best_compr_raw_len-1, 6)
for j in range(0, best_compr_raw_idx+1):
delta = deltas[i+j]
for k in range(delta):
outbits.append(False)
if j < best_compr_raw_idx:
outbits.append(True)
i += best_compr_raw_idx + 1
continue
delta = deltas[i]
i += 1
if delta < 4:
outbits.append(True)
outbits += make_int_bits(delta, 2)
elif delta < 32:
outbits.append(False)
outbits.append(True)
outbits += make_int_bits(delta, 5)
elif delta < 256:
outbits.append(False)
outbits.append(False)
outbits.append(True)
outbits += make_int_bits(delta, 8)
else:
outbits.append(False)
outbits.append(False)
outbits.append(False)
outbits.append(False)
outbits.append(True)
outbits += make_int_bits(delta, 23)
outbits.append(False)
outbits.append(False)
outbits.append(False)
outbits.append(False)
outbits.append(False)
outbits += make_int_bits(numzeros, 23)
return outbits
def ice_compress_bytes(inbytes):
inbits = list()
for byte in inbytes:
for i in range(7, -1, -1):
inbits.append((byte & (1 << i)) != 0)
outbits = ice_compress_bits(inbits)
outbytes = list()
for i in range(0, len(outbits), 8):
byte = 0
for k in range(i, min(i+8, len(outbits))):
if outbits[k]: byte |= 1 << (7-(k-i))
outbytes.append(byte)
return bytes(outbytes)
# ------------------------------------------------------
# Usage example:
# python3 icecompr.py < example_8k.bin > example_8k.compr
if __name__ == '__main__':
import sys
inbytes = sys.stdin.buffer.read()
outbytes = ice_compress_bytes(inbytes)
sys.stdout.buffer.write(outbytes)