-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsfloat.py
37 lines (30 loc) · 944 Bytes
/
sfloat.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
def twos_complement(value, bitWidth):
if value >= 2**bitWidth:
raise ValueError(
"Value: {} out of range of {}-bit value.". format(value, bitWidth))
else:
return value - int((value << 1) & 2**bitWidth)
def getExponent(value):
exponent = twos_complement(((value >> 12) & 0xF), 4)
return int(exponent)
def getMantissa(value):
mantissa = twos_complement((value & 0x0FFF), 12)
return int(mantissa)
def to_int(value):
# NaN
if (value == 0x07FF):
return float('nan')
# NRes (not at this resolution)
elif (value == 0x0800):
return float('nan')
# Positive infinity
elif (value == 0x07FE):
return float("inf")
# Negative infinity
elif (value == 0x0802):
return float("-inf")
# Reserved
elif (value == 0x0801):
return float("nan")
else:
return float(getMantissa(value) * pow(10, getExponent(value)))