-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
46 lines (35 loc) · 996 Bytes
/
example.py
File metadata and controls
46 lines (35 loc) · 996 Bytes
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
import struct
def HalfToFloat(h):
s = int((h >> 15) & 0x00000001) # sign
e = int((h >> 10) & 0x0000001f) # exponent
f = int(h & 0x000003ff) # fraction
if e == 0:
if f == 0:
return int(s << 31)
else:
while not (f & 0x00000400):
f <<= 1
e -= 1
e += 1
f &= ~0x00000400
print (s,e,f)
elif e == 31:
if f == 0:
return int((s << 31) | 0x7f800000)
else:
return int((s << 31) | 0x7f800000 | (f << 13))
e = e + (127 -15)
f = f << 13
return int((s << 31) | (e << 23) | f)
if __name__ == "__main__":
# a half precision binary floating point string
FP16=b'\x00\x3c'
FP16=b'\x00\x36'
FP16=b'\x00\x3f'
v = struct.unpack('H', FP16)
x = HalfToFloat(v[0])
# hack to coerce to float
str = struct.pack('I',x)
f=struct.unpack('f', str)
# print the resulting floating point
print (f[0])