forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.j
205 lines (166 loc) · 8.97 KB
/
float.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
## conversions to floating-point ##
convert(::Type{Float32}, x::Bool) = boxf32(sitofp32(unbox8(x)))
convert(::Type{Float32}, x::Char) = boxf32(uitofp32(unbox32(x)))
convert(::Type{Float32}, x::Int8) = boxf32(sitofp32(unbox8(x)))
convert(::Type{Float32}, x::Int16) = boxf32(sitofp32(unbox16(x)))
convert(::Type{Float32}, x::Int32) = boxf32(sitofp32(unbox32(x)))
convert(::Type{Float32}, x::Int64) = boxf32(sitofp32(unbox64(x)))
convert(::Type{Float32}, x::Uint8) = boxf32(uitofp32(unbox8(x)))
convert(::Type{Float32}, x::Uint16) = boxf32(uitofp32(unbox16(x)))
convert(::Type{Float32}, x::Uint32) = boxf32(uitofp32(unbox32(x)))
convert(::Type{Float32}, x::Uint64) = boxf32(uitofp32(unbox64(x)))
convert(::Type{Float32}, x::Float64) = boxf32(fptrunc32(unbox64(x)))
convert(::Type{Float64}, x::Bool) = boxf64(sitofp64(unbox8(x)))
convert(::Type{Float64}, x::Char) = boxf64(uitofp64(unbox32(x)))
convert(::Type{Float64}, x::Int8) = boxf64(sitofp64(unbox8(x)))
convert(::Type{Float64}, x::Int16) = boxf64(sitofp64(unbox16(x)))
convert(::Type{Float64}, x::Int32) = boxf64(sitofp64(unbox32(x)))
convert(::Type{Float64}, x::Int64) = boxf64(sitofp64(unbox64(x)))
convert(::Type{Float64}, x::Uint8) = boxf64(uitofp64(unbox8(x)))
convert(::Type{Float64}, x::Uint16) = boxf64(uitofp64(unbox16(x)))
convert(::Type{Float64}, x::Uint32) = boxf64(uitofp64(unbox32(x)))
convert(::Type{Float64}, x::Uint64) = boxf64(uitofp64(unbox64(x)))
convert(::Type{Float64}, x::Float32) = boxf64(fpext64(unbox32(x)))
convert(::Type{Float}, x::Bool) = convert(Float32, x)
convert(::Type{Float}, x::Char) = convert(Float32, x)
convert(::Type{Float}, x::Int8) = convert(Float32, x)
convert(::Type{Float}, x::Int16) = convert(Float32, x)
convert(::Type{Float}, x::Int32) = convert(Float64, x)
convert(::Type{Float}, x::Int64) = convert(Float64, x) # LOSSY
convert(::Type{Float}, x::Uint8) = convert(Float32, x)
convert(::Type{Float}, x::Uint16) = convert(Float32, x)
convert(::Type{Float}, x::Uint32) = convert(Float64, x)
convert(::Type{Float}, x::Uint64) = convert(Float64, x) # LOSSY
float32(x) = convert(Float32, x)
float64(x) = convert(Float64, x)
float(x) = convert(Float, x)
## conversions from floating-point ##
if WORD_SIZE == 64
iround(x::Float32) = iround(float64(x))
itrunc(x::Float32) = itrunc(float64(x))
else
iround(x::Float32) = boxsi32(fpsiround32(unbox32(x)))
itrunc(x::Float32) = boxsi32(fptosi32(unbox32(x)))
end
iround(x::Float64) = boxsi64(fpsiround64(unbox64(x)))
itrunc(x::Float64) = boxsi64(fptosi64(unbox64(x)))
iceil(x::Float) = itrunc(ceil(x)) # TODO: fast primitive for iceil
ifloor(x::Float) = itrunc(floor(x)) # TOOD: fast primitive for ifloor
convert(::Type{Integer}, x::Float) = iround(x)
convert(::Type{Int32}, x::Float) = int32(iround(x))
convert(::Type{Int64}, x::Float) = int64(iround(x))
## floating point promotions ##
promote_rule(::Type{Float64}, ::Type{Float32} ) = Float64
promote_rule(::Type{Float32}, ::Type{Int8} ) = Float32
promote_rule(::Type{Float32}, ::Type{Int16}) = Float32
promote_rule(::Type{Float32}, ::Type{Int32}) = Float64
promote_rule(::Type{Float32}, ::Type{Int64}) = Float64 # TODO: should be Float80
promote_rule(::Type{Float64}, ::Type{Int8} ) = Float64
promote_rule(::Type{Float64}, ::Type{Int16}) = Float64
promote_rule(::Type{Float64}, ::Type{Int32}) = Float64
promote_rule(::Type{Float64}, ::Type{Int64}) = Float64 # TODO: should be Float80
promote_rule(::Type{Float32}, ::Type{Uint8} ) = Float32
promote_rule(::Type{Float32}, ::Type{Uint16}) = Float32
promote_rule(::Type{Float32}, ::Type{Uint32}) = Float64
promote_rule(::Type{Float32}, ::Type{Uint64}) = Float64 # TODO: should be Float80
promote_rule(::Type{Float64}, ::Type{Uint8} ) = Float64
promote_rule(::Type{Float64}, ::Type{Uint16}) = Float64
promote_rule(::Type{Float64}, ::Type{Uint32}) = Float64
promote_rule(::Type{Float64}, ::Type{Uint64}) = Float64 # TODO: should be Float80
promote_rule(::Type{Float32}, ::Type{Char}) = Float32
promote_rule(::Type{Float64}, ::Type{Char}) = Float64
## floating point arithmetic ##
-(x::Float32) = boxf32(neg_float(unbox32(x)))
-(x::Float64) = boxf64(neg_float(unbox64(x)))
+(x::Float32, y::Float32) = boxf32(add_float(unbox32(x), unbox32(y)))
+(x::Float64, y::Float64) = boxf64(add_float(unbox64(x), unbox64(y)))
-(x::Float32, y::Float32) = boxf32(sub_float(unbox32(x), unbox32(y)))
-(x::Float64, y::Float64) = boxf64(sub_float(unbox64(x), unbox64(y)))
*(x::Float32, y::Float32) = boxf32(mul_float(unbox32(x), unbox32(y)))
*(x::Float64, y::Float64) = boxf64(mul_float(unbox64(x), unbox64(y)))
/(x::Float32, y::Float32) = boxf32(div_float(unbox32(x), unbox32(y)))
/(x::Float64, y::Float64) = boxf64(div_float(unbox64(x), unbox64(y)))
# TODO: faster floating point div?
# TODO: faster floating point fld?
# TODO: faster floating point mod?
rem(x::Float32, y::Float32) = boxf32(rem_float(unbox32(x), unbox32(y)))
rem(x::Float64, y::Float64) = boxf64(rem_float(unbox64(x), unbox64(y)))
## floating point comparisons ##
==(x::Float32, y::Float32) = eq_float(unbox32(x),unbox32(y))
==(x::Float64, y::Float64) = eq_float(unbox64(x),unbox64(y))
!=(x::Float32, y::Float32) = ne_float(unbox32(x),unbox32(y))
!=(x::Float64, y::Float64) = ne_float(unbox64(x),unbox64(y))
< (x::Float32, y::Float32) = lt_float(unbox32(x),unbox32(y))
< (x::Float64, y::Float64) = lt_float(unbox64(x),unbox64(y))
<=(x::Float32, y::Float32) = le_float(unbox32(x),unbox32(y))
<=(x::Float64, y::Float64) = le_float(unbox64(x),unbox64(y))
isequal(x::Float32, y::Float32) = fpiseq32(unbox32(x),unbox32(y))
isequal(x::Float64, y::Float64) = fpiseq64(unbox64(x),unbox64(y))
isless (x::Float32, y::Float32) = fpislt32(unbox32(x),unbox32(y))
isless (x::Float64, y::Float64) = fpislt64(unbox64(x),unbox64(y))
==(x::Float64, y::Int64 ) = eqfsi64(unbox64(x),unbox64(y))
==(x::Float64, y::Uint64 ) = eqfui64(unbox64(x),unbox64(y))
==(x::Int64 , y::Float64) = eqfsi64(unbox64(y),unbox64(x))
==(x::Uint64 , y::Float64) = eqfui64(unbox64(y),unbox64(x))
==(x::Float32, y::Int64 ) = eqfsi64(unbox64(float64(x)),unbox64(y))
==(x::Float32, y::Uint64 ) = eqfui64(unbox64(float64(x)),unbox64(y))
==(x::Int64 , y::Float32) = eqfsi64(unbox64(float64(y)),unbox64(x))
==(x::Uint64 , y::Float32) = eqfui64(unbox64(float64(y)),unbox64(x))
< (x::Float64, y::Int64 ) = ltfsi64(unbox64(x),unbox64(y))
< (x::Float64, y::Uint64 ) = ltfui64(unbox64(x),unbox64(y))
< (x::Int64 , y::Float64) = ltsif64(unbox64(x),unbox64(y))
< (x::Uint64 , y::Float64) = ltuif64(unbox64(x),unbox64(y))
< (x::Float32, y::Int64 ) = ltfsi64(unbox64(float64(x)),unbox64(y))
< (x::Float32, y::Uint64 ) = ltfui64(unbox64(float64(x)),unbox64(y))
< (x::Int64 , y::Float32) = ltsif64(unbox64(x),unbox64(float64(y)))
< (x::Uint64 , y::Float32) = ltuif64(unbox64(x),unbox64(float64(y)))
<=(x::Float64, y::Int64 ) = lefsi64(unbox64(x),unbox64(y))
<=(x::Float64, y::Uint64 ) = lefui64(unbox64(x),unbox64(y))
<=(x::Int64 , y::Float64) = lesif64(unbox64(x),unbox64(y))
<=(x::Uint64 , y::Float64) = leuif64(unbox64(x),unbox64(y))
<=(x::Float32, y::Int64 ) = lefsi64(unbox64(float64(x)),unbox64(y))
<=(x::Float32, y::Uint64 ) = lefui64(unbox64(float64(x)),unbox64(y))
<=(x::Int64 , y::Float32) = lesif64(unbox64(x),unbox64(float64(y)))
<=(x::Uint64 , y::Float32) = leuif64(unbox64(x),unbox64(float64(y)))
## floating point traits ##
const Inf32 = boxf32(unbox32(0x7f800000))
const NaN32 = boxf32(unbox32(0x7fc00000))
const Inf = boxf64(unbox64(0x7ff0000000000000))
const NaN = boxf64(unbox64(0x7ff8000000000000))
@eval begin
inf(::Type{Float32}) = $Inf32
nan(::Type{Float32}) = $NaN32
inf(::Type{Float64}) = $Inf
nan(::Type{Float64}) = $NaN
inf{T<:Float}(x::T) = inf(T)
nan{T<:Float}(x::T) = nan(T)
isdenormal(x::Float32) = (abs(x) < $boxf32(unbox32(0x00800000)))
isdenormal(x::Float64) = (abs(x) < $boxf64(unbox64(0x0010000000000000)))
typemin(::Type{Float32}) = $(-Inf32)
typemax(::Type{Float32}) = $(Inf32)
typemin(::Type{Float64}) = $(-Inf)
typemax(::Type{Float64}) = $(Inf)
typemin{T<:Real}(x::T) = typemin(T)
typemax{T<:Real}(x::T) = typemax(T)
realmin(::Type{Float32}) = $boxf32(unbox32(0x00800000))
realmin(::Type{Float64}) = $boxf64(unbox64(0x0010000000000000))
realmax(::Type{Float32}) = $boxf32(unbox32(0x7f7fffff))
realmax(::Type{Float64}) = $boxf64(unbox64(0x7fefffffffffffff))
realmin{T<:Float}(x::T) = realmin(T)
realmax{T<:Float}(x::T) = realmax(T)
realmin() = realmin(Float64)
realmax() = realmax(Float64)
nextfloat(x::Float32, i::Integer) = boxf32(add_int(unbox32(x),unbox32(int32(i))))
nextfloat(x::Float64, i::Integer) = boxf64(add_int(unbox64(x),unbox64(int64(i))))
nextfloat(x::Float) = nextfloat(x,1)
prevfloat(x::Float) = nextfloat(x,-1)
eps(x::Float) = isfinite(x) ? abs(nextfloat(x)-x) : nan(x)
eps(::Type{Float32}) = $boxf32(unbox32(0x34000000))
eps(::Type{Float64}) = $boxf64(unbox64(0x3cb0000000000000))
eps() = eps(Float64)
end
sizeof(::Type{Float32}) = 4
sizeof(::Type{Float64}) = 8
## mathematical constants ##
const e = 2.71828182845904523536
const pi = 3.14159265358979323846