-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.py
38 lines (33 loc) · 1.06 KB
/
utils.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
import struct
import numpy as np
EPS = 0.0000001
ST = 1
def dec_to_2s(number, width):
if number > 0:
result = bin(number)
pad_l = width - (len(result) - 2)
if pad_l < 0:
return "Error not enough bits"
else:
return "0" * pad_l + result[2:]
elif number == 0:
return "0" * width
else:
return dec_to_2s(number + 2 ** width, width)
def float_to_hex(number):
s = struct.pack('>f',number)
bits = struct.unpack('>l',s)[0]
return hex(int(dec_to_2s(bits,32),2)).upper().replace("X","f")
def half_to_hex(number):
s = hex(np.float16(number).view('H'))[2:].zfill(4)
return "0x" + s + s
def hex_to_bin(hex_string):
scale = 16 ## equals to hexadecimal
num_of_bits = 16
return bin(int(hex_string, scale))[2:].zfill(num_of_bits)
def bin_to_half(bin_string):
sign = int(bin_string[0])
exponent = int(bin_string[1:6],2)
mantissa = int(bin_string[6:],2)
factor = 10 ** np.ceil(np.log10(mantissa))
return (-1) ** sign * 2 ** (exponent - 15) * (1 + mantissa /factor)