| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import pandas as pd
- import math
- def dec_mantissa(bin_mantissa):
- res = 0
- for k in range(len(bin_mantissa)):
- res = res + (int(bin_mantissa[k]) * (2 ** (-1 - k)))
- return res
- def hex2dec(hex):
- binary = "{0:08b}".format(int(hex, 16))
- if len(binary) < 16:
- while len(binary) < 16:
- binary = "0" + binary
- sign = 1
- else:
- sign = -1 ** int(binary[0])
- exp = 2 ** (int(binary[1:6], 2) - 16)
- mantissa = 1 + dec_mantissa(binary[6:])
- return [binary, sign * exp * mantissa]
- # df = pd.read_csv("fp16_test.hex")
- # bins = []
- # decs = []
- # for i in range(df.shape[0]):
- # hexs = df.iloc[i][0].split()
- # bins.append([])
- # decs.append([])
- # for j in range(4):
- # bins[i].append("{0:08b}".format(int(hexs[j], 16)))
- # exp = 2 ** (int(bins[i][j][1:6], 2) - 16)
- # sign = -1 ** int(bins[i][j][0])
- # mantissa = 1 + dec_mantissa(bins[i][j][6:])
- # decs[i].append(sign * exp * mantissa)
- input_1 = "bbeb"
- input_2 = "58fe"
- result = "d8f1"
- calc_res = "d9f1"
- print("input 1 :", input_1, ", ", hex2dec(input_1)[0], ", ", hex2dec(input_1)[1])
- print("input 2 :", input_2, ", ", hex2dec(input_2)[0], ", ", hex2dec(input_2)[1])
- print("result :", result, ", ", hex2dec(result)[0], ", ", hex2dec(result)[1])
- print("calc res:", calc_res, ", ", hex2dec(calc_res)[0], ", ", hex2dec(calc_res)[1])
|