-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp.py
153 lines (133 loc) · 5.04 KB
/
fp.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File: fp.py
# Author: Samuel McFalls
# Description: See README.md
import argparse
from sys import exit
from math import log10, floor
args = None # Args available to all functions
# From http://stackoverflow.com/a/9147327/3287359
def twos_comp(val, bits):
"""compute the 2's compliment of int value val"""
if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
val = val - (1 << bits) # compute negative value
return val
# From http://stackoverflow.com/a/12946226/3287359
def bindigits(n, bits):
s = bin(n & int("1"*bits, 2))[2:]
return ("{0:0>%s}" % (bits)).format(s)
# From http://stackoverflow.com/a/13662978/3287359
def parse_bin(s):
t = s.split('.')
return int(t[0], 2) + int(t[1], 2) / 2.**len(t[1])
# From http://stackoverflow.com/a/3413529/3287359
def round_sig(x, sig=2):
if (x < 0):
x = -x
return -round(x, sig-int(floor(log10(x)))-1)
elif (x == 0):
return x
return round(x, sig-int(floor(log10(x)))-1)
def hexToDec(hexVal):
try:
binToDec(bin(int(hexVal, 16))[2:])
except:
print("Error: VALUE is not valid")
exit(1)
def binToDec(binVal):
if len(binVal) > 32:
print("Error: VALUE more than 32 bits")
exit(1)
if len(binVal) < 32:
binVal = binVal.zfill(32)
if (args.verbose):
print("\nPadded to " + binVal)
try:
sign = int(binVal[0]);
exponent = twos_comp(int(binVal[1:8], 2), 7)
mantissa = parse_bin("0." + binVal[8:])
except:
print("Error: VALUE is not valid")
exit(1)
if (args.verbose):
signStr = "+ Positive" if (binVal[0] == "0") else "- Negative"
print("\nSign: 1'b" + binVal[0] + " -> " + signStr)
print("Exponent: 7'b" + binVal[1:8] + " -> " + str(exponent))
print("Mantissa: 24'b" + binVal[8:] + " -> " + str(mantissa))
print("(-1) ^ (" + str(sign) + ") * 16 ^ (" + str(exponent) + ") * "
+ str(mantissa) + " =")
dec = ((-1) ** sign) * (16 ** exponent) * mantissa
try:
decSigFigs = round_sig(dec, 6); # Six significant figures
except:
if (args.verbose):
print("\nWarning: Significant Figure conversion failed")
print(dec)
return
print(decSigFigs)
def decToFP(decVal):
# Convert to float and check the sign
try:
decVal = float(decVal)
except:
print("Error: VALUE is not valid")
if (decVal < 0):
sign = 1
decVal = abs(decVal)
else:
sign = 0
# Determine the proper exponent
# Iterate through, starting with -64 and ending with +63
# It would be more efficient to start with 0 and check as in a binary
# search, but I won't prematurely optimize
prev = None
exponent = None
for exponent in range(-64, 64):
if decVal < 16 ** exponent:
if prev is not None:
if (decVal >= 16 ** prev):
break;
else:
break; # The value is less than 16^-64
# If nothing is found, we must be at or above 16^63
# Determine the mantissa
# Again, probably more effecient ways than using parse_bin, such as
# keeping a running total, but I won't prematurely optimize
mantissa = list("0.000000000000000000000000")
for bitNum in range(0, 24):
mantissa[bitNum + 2] = "1"
if (parse_bin("".join(mantissa)) * (16 ** exponent) > decVal):
mantissa[bitNum + 2] = "0"
expStr = bindigits(exponent, 7)
manStr = "".join(mantissa[2:])
manVal = parse_bin("".join(mantissa))
if (args.verbose):
signStr = "+ Positive" if (sign == 0) else "- Negative"
print("\nSign: " + signStr + " -> " + "1'b" + str(sign))
print("Exponent: " + str(exponent) + " -> " + "7'b" + expStr)
print("Mantissa: " + str(manVal) + " -> " + "24'b" + manStr)
print("(-1) ^ (" + str(sign) + ") * 16 ^ (" + str(exponent) + ") * "
+ str(manVal) + " =")
dec = ((-1) ** sign) * (16 ** exponent) * manVal
print(dec)
error = abs(abs(dec) - abs(decVal)) / abs(decVal)
print("This calculation has error of " + str(error) + "\n")
print("32'b" + str(sign) + expStr + manStr)
parser = argparse.ArgumentParser(description=
"Converts DD2 Format FP <-> Decimal")
parser.add_argument("value", metavar="VALUE", help="The value to convert from")
base = parser.add_mutually_exclusive_group(required=True)
base.add_argument("-x", "--hex", help="Specifies that VALUE is in hex",
action="store_true")
base.add_argument("-b", "--bin", help="Specifies that VALUE is in binary",
action="store_true")
base.add_argument("-d", "--dec", help="Specifies that VALUE is in decimal",
action="store_true")
parser.add_argument("-v", "--verbose", help="Enables more verbose output",
action="store_true")
args = parser.parse_args()
if (args.hex):
hexToDec(args.value)
elif (args.bin):
binToDec(args.value)
elif (args.dec):
decToFP(args.value)