summaryrefslogtreecommitdiff
path: root/lib/ultralib/tools/util.py
blob: b6c615596daaff2e2896067c2c51a437b602d06b (plain)
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

import struct

def enumerate_stepped(l, start=0, step=1):
    p = start
    for e in l:
        yield p, e
        p += step

def back_align(x, al):
    return x - (x % al)

def forward_align(x, al):
    return (x + (al - 1)) & ~(al - 1)

def as_double(b):
    return struct.unpack(">d", b)[0]

def as_dword(b):
    return struct.unpack(">Q", b)[0]

def as_float(b):
    return struct.unpack(">f", b)[0]

def as_word(b):
    return struct.unpack(">I", b)[0]

def as_hword(b):
    return struct.unpack(">H", b)[0]

def as_double_list(b):
    return [i[0] for i in struct.iter_unpack(">d", b)]

def as_dword_list(b):
    return [i[0] for i in struct.iter_unpack(">Q", b)]

def as_float_list(b):
    return [i[0] for i in struct.iter_unpack(">f", b)]

def as_word_list(b):
    return [i[0] for i in struct.iter_unpack(">I", b)]

def as_hword_list(b):
    return [h[0] for h in struct.iter_unpack(">H", b)]