-
Notifications
You must be signed in to change notification settings - Fork 0
/
f24.py
58 lines (53 loc) · 952 Bytes
/
f24.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
#!/usr/bin/python3
import sys
#This program converts a number to a 24-bit float to insert into assembly source code
def dbify(l):
s='.db $'
for i in reversed(l): # JFB: reversed for big endian
t=hex(i)[2:].upper()
if len(t)==1:
t='0'+t
s+=t[-2:]+",$"
return s[0:-2]
def tofloat(x,n=2):
if x.lower() == "nan":
return [255,255,127]
x=float(x)
if x==0:
return [0,0,0]
sign=0
if x<0:
sign=1
x=-x
if x==float('inf'):
return [0,0,127+(sign<<7)]
exp=0
while x<1:
exp-=1
x+=x
while x>=2:
exp+=1
x/=2
if exp>63:
return [0,0,127+(sign<<7)] #infinity
if exp<-62:
return [0,0,0] #zero
l=[exp+63+(sign<<7)]
x -= 1
x*=256
for k in range(n-1):
a=int(x)
x-=a
l=[a]+l
x*=256
#rounding
x+=.5
l=[int(x)]+l
k=0
while (k<n) and (l[k]==256):
l[k]=0
k+=1
l[k]+=1
return l
for i in sys.argv[1:]:
print(dbify(tofloat(i))+" ;"+i)