-
Notifications
You must be signed in to change notification settings - Fork 11
/
single.py
65 lines (63 loc) · 1.07 KB
/
single.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
#!/usr/bin/python
import sys
#This program converts a number to a single-precision float to insert into assembly source code
def dbify(l):
s='.db $'
for i in l:
t=hex(i)[2:].upper()
if len(t)==1:
t='0'+t
s+=t[-2:]+",$"
return s[0:-2]
def tofloat(x,n=3):
if x.lower() == "nan":
return [0,0,32,0]
x=float(x)
if x==0:
return [0,0,0,0]
sign=0
if x<0:
sign=1
x=-x
if x==float('inf'):
return [255,255,64+(sign<<7),0]
exp=0
while x<1:
exp-=1
x+=x
while x>=2:
exp+=1
x/=2
if exp>127:
return [255,255,64+(sign<<7),0] #infinity
if exp<-127:
return [0,0,0,0] #zero
l=[exp+128]
x+=sign-1
x*=128
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-1) and (l[k]==256):
l[k]=0
k+=1
l[k]+=1
if k!=n-1:
return l
if l[k]&127!=0:
return l
#Here, we have [0,0,128 or 256,???]
l[k] -= 128
l[k+1] += 1
if l[k+1] == 256:
l[k] |= 64
l[k+1] = 0
return l
for i in sys.argv[1:]:
print(dbify(tofloat(i))+" ;"+i)