Repository navigation
Expand file tree
/
Copy pathhgcal_encode.py
More file actions
59 lines (49 loc) · 1.91 KB
/
Copy pathhgcal_encode.py
File metadata and controls
59 lines (49 loc) · 1.91 KB
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
#!/usr/bin/env python
import numpy as np
import math
def encode(value, dropBits=1, expBits=4, mantBits=3, roundBits=False, asInt=False):
binCode=bin(value)[2:]
if len(binCode) <= (mantBits+dropBits):
if roundBits and dropBits>0:
value += 2**(dropBits-1)
value = value>>dropBits
binCode=bin(value)[2:]
mantissa = format(value, '#0%ib'%(mantBits+2))[2:]
exponent = '0'*expBits
elif len(binCode)==mantBits+dropBits+1:
if roundBits and dropBits>0:
value += 2**(dropBits-1)
value = value>>dropBits
binCode=bin(value)[2:]
exponent = '0001'
mantissa = binCode[1:1+mantBits]
else:
if roundBits:
vTemp = int(binCode,2) + int(2**(len(binCode)-2-mantBits))
binCode = bin(vTemp)[2:]
firstZero = len(binCode)-mantBits-dropBits
if firstZero<1:
print ("PROBLEM")
if firstZero<2**expBits:
exponent = format(firstZero, '#0%ib'%(expBits+2))[2:]
mantissa = binCode[1:1+mantBits]
else:
exponent = '1'*expBits
mantissa = '1'*mantBits
if asInt:
return int(exponent + mantissa,2)
else:
return exponent + mantissa
def decode(val,droppedBits=1,expBits=4,mantBits=3,edge=False,quarter=False):
exp=val>>mantBits
mant= val & (2**mantBits-1)
data = (((mant<<(exp-1)) if exp>0 else mant) + (0 if exp==0 else (1<<(exp+mantBits-1))))
data = data<<droppedBits
shift = max(exp-1,0)
if quarter:
if (droppedBits+shift)>1:
data += 1<<(shift+droppedBits-2)
elif not edge:
if (droppedBits+shift)>0:
data += 1<<(shift+droppedBits-1)
return data