forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prim.j
141 lines (124 loc) · 3.89 KB
/
prim.j
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
typealias Nullable{T} Union(T,())
typealias Index Int32
typealias Size Int32
typealias String Array{Uint8,1}
(<:)(T::Type, S::Type) = subtype(T,S)
(:>)(T::Type, S::Type) = subtype(S,T)
ref(t::Tuple, i::Index) = tupleref(t, i)
length(t::Tuple) = tuplelen(t)
!(x::Bool) = eq_int(unbox8(x),trunc8(unbox32(0)))
!(x) = false
!=(x, y) = !(x == y)
# bootstrapping versions of operators needed by for loops
(-)(x::Int32) = boxsi32(neg_int(unbox32(x)))
(+)(x::Int32, y::Int32) = boxsi32(add_int(unbox32(x), unbox32(y)))
(-)(x::Int32, y::Int32) = boxsi32(sub_int(unbox32(x), unbox32(y)))
(*)(x::Int32, y::Int32) = boxsi32(mul_int(unbox32(x), unbox32(y)))
div(x::Int32, y::Int32) = boxsi32(sdiv_int(unbox32(x), unbox32(y)))
< (x::Int32, y::Int32) = slt_int(unbox32(x),unbox32(y))
==(x::Int32, y::Int32) = eq_int(unbox32(x),unbox32(y))
<=(x::Int32, y::Int32) = slt_int(unbox32(x),unbox32(y)) || eq_int(unbox32(x),unbox32(y))
# fallback definitions for emulating N-arg operators with 2-arg definitions
(*)() = 1
(*)(x::Tensor) = x
(*)(a,b,c) = (*)((*)(a,b),c)
(*)(a,b,c,d) = (*)((*)((*)(a,b),c),d)
(*)(a,b,c,d,e) = (*)((*)((*)((*)(a,b),c),d),e)
function (*)(x1, x2, x3, xs...)
accum = (*)((*)(x1,x2),x3)
for x = xs
accum = accum * x
end
accum
end
(+)() = 0
(+)(x::Tensor) = x
(+)(a,b,c) = (+)((+)(a,b),c)
(+)(a,b,c,d) = (+)((+)((+)(a,b),c),d)
(+)(a,b,c,d,e) = (+)((+)((+)((+)(a,b),c),d),e)
function (+)(x1, x2, x3, xs...)
accum = (+)((+)(x1,x2),x3)
for x = xs
accum = accum + x
end
accum
end
# arithmetic promotion
(+)(x::Number...) = (+)(promote(x...)...)
(*)(x::Number...) = (*)(promote(x...)...)
(-)(x::Number, y::Number) = (-)(promote(x,y)...)
(/)(x::Number, y::Number) = (/)(promote(x,y)...)
(<)(x::Number, y::Number) = (<)(promote(x,y)...)
(>)(x::Number, y::Number) = (>)(promote(x,y)...)
(<=)(x::Number, y::Number) = (<=)(promote(x,y)...)
(>=)(x::Number, y::Number) = (>=)(promote(x,y)...)
(==)(x::Number, y::Number) = (==)(promote(x,y)...)
(./)(x, y) = x/y
(.*)(x, y) = x*y
# iterating over tuples
start(t::Tuple) = 1
done(t::Tuple, i) = (i > length(t))
next(t::Tuple, i) = (t[i], i+1)
# map on tuples
# 0 argument function
map(f) = f()
# 1 argument function
map(f, t::()) = ()
map(f, t::(Any,)) = (f(t[1]),)
map(f, t::(Any, Any)) = (f(t[1]), f(t[2]))
map(f, t::(Any, Any, Any)) = (f(t[1]), f(t[2]), f(t[3]))
map(f, t::(Any, Any, Any, Any)) = (f(t[1]), f(t[2]), f(t[3]), f(t[4]))
map(f, t::Tuple) = maptuple(f, t...)
maptuple(f) = ()
maptuple(f, first, rest...) = tuple(f(first), maptuple(f, rest...)...)
# 2 argument function
map(f, t::(), s::()) = ()
map(f, t::(Any,), s::(Any,)) = (f(t[1],s[1]),)
map(f, t::(Any,Any), s::(Any,Any)) = (f(t[1],s[1]), f(t[2],s[2]))
# n argument function
function map(f, ts::Tuple...)
function _map(f, ts, i)
if i > length(ts[1])
return ()
end
return tuple(f(map(x->x[i],ts)...), _map(f,ts,i+1)...)
end
return _map(f, ts, 1)
end
ref(t::Tuple, r::Range) = accumtuple(t, r, start(r))
function accumtuple(t::Tuple, r::Range, i, elts...)
if (done(r, i))
return elts
end
accumtuple(t, r, i+r.step, elts..., t[i])
end
ref(t::Tuple, r::RangeFrom) = t[Range(r.start,r.step,length(t))]
ref(t::Tuple, r::RangeTo) = t[Range(1,r.step,r.stop)]
ref(t::Tuple, r::RangeBy) = t[Range(1,r.step,length(t))]
function ==(t1::Tuple, t2::Tuple)
if length(t1) != length(t2)
return false
end
for i = 1:length(t1)
if t1[i] != t2[i]
return false
end
end
return true
end
function append(t1::Tuple, ts::Tuple...)
if (length(ts)==0)
return t1
end
return tuple(t1..., append(ts...)...)
end
print(x...) = for i=x; print(i); end
expr(hd::Symbol, args...) = Expr(hd, args, Any)
function cell_literal(xs...)
n = length(xs)
a = Array(Any,n)
for i=1:n
arrayset(a,i,xs[i])
end
a
end