55 lines
1.2 KiB
Python
55 lines
1.2 KiB
Python
from math import log2, ceil, floor
|
|
|
|
BW = 5
|
|
BE = 3
|
|
EO = 1
|
|
|
|
BM = BW-BE
|
|
BO = BM-EO # BW-BE-EO
|
|
EXP_MAX = 2**BE-1
|
|
R_MIN = ufp_to_r(EXP_MAX, 1, BW, BE, EO)
|
|
|
|
|
|
def r_to_ufp(r, BW, BE, EO):
|
|
BM = BW-BE
|
|
BO = BM-EO # BW-BE-EO
|
|
EXP_MAX = 2**BE-1
|
|
R_MIN = ufp_to_r(EXP_MAX, 1, BW, BE, EO)
|
|
|
|
if r<R_MIN: # Small values
|
|
ee = EXP_MAX
|
|
mm = 0
|
|
return ee, mm
|
|
|
|
aa = floor(BM - log2(r))
|
|
aa = max(min(aa, EXP_MAX+BO), BO)
|
|
ee = aa - BO
|
|
|
|
mm = round(r*2**aa)
|
|
return ee, mm
|
|
|
|
|
|
def ufp_to_r(ee, mm, BW, BE, EO):
|
|
BM = BW-BE
|
|
BO = BM-EO # BW-BE-EO
|
|
|
|
r = mm * 2**(-BO-ee)
|
|
return r
|
|
|
|
|
|
for mm in range(2**BM):
|
|
for ee in range(2**BE):
|
|
r_exp = ufp_to_r(ee, mm, BW, BE, EO)
|
|
ee_exp, mm_exp = r_to_ufp(r_exp, BW, BE, EO)
|
|
r_exp2 = ufp_to_r(ee_exp, mm_exp, BW, BE, EO)
|
|
#print("r={} r_exp={} mm={} ee={}".format(r, r_exp, mm_exp, ee_exp))
|
|
|
|
print("mm={} ee={} r={} ".format(mm, ee, r_exp))
|
|
print("mm={} ee={} r={} ".format(mm_exp, ee_exp, r_exp2))
|
|
print("---------------------------------------------------")
|
|
|
|
|
|
r=1.5625e-2
|
|
#r=3.1249999999999997e-2
|
|
ee, mm = r_to_ufp(r, BW, BE, EO)
|
|
r2 = ufp_to_r(ee, mm, BW, BE, EO)
|