-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathruntests.jl
194 lines (160 loc) · 5.49 KB
/
runtests.jl
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
using Test, BFloat16s, Printf, Random
@info "Testing BFloat16s" BFloat16s.llvm_storage BFloat16s.llvm_arithmetic
@testset "comparisons" begin
@test BFloat16(1) < BFloat16(2)
@test BFloat16(1f0) < BFloat16(2f0)
@test BFloat16(1.0) < BFloat16(2.0)
@test BFloat16(1) <= BFloat16(2)
@test BFloat16(1f0) <= BFloat16(2f0)
@test BFloat16(1.0) <= BFloat16(2.0)
@test BFloat16(2) > BFloat16(1)
@test BFloat16(2f0) > BFloat16(1f0)
@test BFloat16(2.0) > BFloat16(1.0)
@test BFloat16(2) >= BFloat16(1)
@test BFloat16(2f0) >= BFloat16(1f0)
@test BFloat16(2.0) >= BFloat16(1.0)
@test BFloat16(2) != BFloat16(1)
@test BFloat16(2f0) != BFloat16(1f0)
@test BFloat16(2.0) != BFloat16(1.0)
@test iszero(BFloat16(0)) == true
@test iszero(BFloat16(3.45)) == false
end
@testset "conversions" begin
@test Float32(BFloat16(10)) == 1f1
@test Float64(BFloat16(10)) == 10.0
@test Int32(BFloat16(10)) == Int32(10)
@test Int64(BFloat16(10)) == Int64(10)
@test BFloat16(BigFloat(1)) == BFloat16(1)
@test BigFloat(BFloat16(1)) == BigFloat(1)
end
@testset "abi" begin
f() = BFloat16(1)
@test f() == BFloat16(1)
g(x) = x+BFloat16(1)
@test g(BFloat16(2)) == BFloat16(3)
end
@testset "functions" begin
@test abs(BFloat16(-10)) == BFloat16(10)
@test BFloat16(2) ^ BFloat16(4) == BFloat16(16)
@test eps(BFloat16) == BFloat16(0.0078125)
@test sqrt(BFloat16(4f0)) == BFloat16(2f0)
@test round(BFloat16(10.4), RoundUp) == BFloat16(11.0)
@test round(BFloat16(10.6), RoundDown) == BFloat16(10.0)
@test round(BFloat16(3.2), RoundNearest) == BFloat16(3.0)
@test round(BFloat16(4.8), RoundNearest) == BFloat16(5.0)
end
@testset "arithmetic" begin
@test BFloat16(0.2) * BFloat16(5.0) == BFloat16(1.0)
@test BFloat16(1.0) / BFloat16(5.0) == BFloat16(0.2)
@test inv(BFloat16(5.0)) == BFloat16(0.2)
@test zero(BFloat16) == BFloat16(0.0f0)
@test one(BFloat16) == BFloat16(1.0)
@test BFloat16(2.0) ^ -2 == BFloat16(0.25)
end
@testset "printf" begin
for (fmt, val) in (("%7.2f", " 1.23"),
("%-7.2f", "1.23 "),
("%07.2f", "0001.23"),
("%.0f", "1"),
("%#.0f", "1."),
("%.3e", "1.234e+00"),
("%.3E", "1.234E+00"),
("%.2a", "0x1.3cp+0"),
("%.2A", "0X1.3CP+0")),
num in (BFloat16(1.234),)
@eval @test @sprintf($fmt, $num) == $val
end
@test (@sprintf "%f" BFloat16(Inf)) == "Inf"
@test (@sprintf "%f" BFloat16(NaN)) == "NaN"
@test (@sprintf "%.0e" BFloat16(3e2)) == "3e+02"
@test (@sprintf "%#.0e" BFloat16(3e2)) == "3.e+02"
for (fmt, val) in (("%10.5g", " 123.5"),
("%+10.5g", " +123.5"),
("% 10.5g", " 123.5"),
("%#10.5g", " 123.50"),
("%-10.5g", "123.5 "),
("%-+10.5g", "+123.5 "),
("%010.5g", "00000123.5")),
num in (BFloat16(123.5),)
@eval @test @sprintf($fmt, $num) == $val
end
@test( @sprintf( "%10.5g", BFloat16(-123.5) ) == " -123.5")
@test( @sprintf( "%010.5g", BFloat16(-123.5) ) == "-0000123.5")
@test (@sprintf "%a" BFloat16(1.5)) == "0x1.8p+0"
end
@testset "random" begin
x = Array{BFloat16}(undef, 10)
y = Array{BFloat16}(undef, 10)
rand!(x)
rand!(y)
@test x !== y
randn!(x)
randn!(y)
@test x !== y
randexp!(x)
randexp!(y)
@test x !== y
x = rand(BFloat16, 10)
y = rand(BFloat16, 10)
@test x !== y
x = randn(BFloat16, 10)
y = randn(BFloat16, 10)
@test x !== y
x = randexp(BFloat16, 10)
y = randexp(BFloat16, 10)
@test x !== y
end
@testset "round" begin
@test round(Int, BFloat16(3.4)) == 3
end
@testset "Next/prevfloat" begin
for x in (one(BFloat16),
-one(BFloat16),
zero(BFloat16))
@test x == nextfloat(prevfloat(x))
@test x == prevfloat(nextfloat(x))
@test x < nextfloat(x)
@test x > prevfloat(x)
end
@test isnan(nextfloat(BFloat16s.NaNB16))
@test isinf(nextfloat(BFloat16s.InfB16))
@test isnan(prevfloat(BFloat16s.NaNB16))
end
@testset "Decompose BFloat16" begin
for x in randn(100)
bf16 = BFloat16(x)
s,e,d = Base.decompose(bf16)
@test BFloat16(s*2.0^e/d) == bf16
end
end
@testset "Next/prevfloat(x,::Integer)" begin
x = one(BFloat16)
@test x == prevfloat(nextfloat(x,100),100)
@test x == nextfloat(prevfloat(x,100),100)
x = -one(BFloat16)
@test x == prevfloat(nextfloat(x,100),100)
@test x == nextfloat(prevfloat(x,100),100)
x = one(BFloat16)
@test nextfloat(x,5) == prevfloat(x,-5)
@test prevfloat(x,-5) == nextfloat(x,5)
@test isinf(nextfloat(floatmax(BFloat16),5))
@test prevfloat(floatmin(BFloat16),2^8) < 0
@test nextfloat(-floatmin(BFloat16),2^8) > 0
end
@testset "maxintfloat" begin
a = maxintfloat(BFloat16)
@test a+1-1 == a-1 # the first +1 cannot be represented
@test a-1+1 == a # but -1 can
end
@testset "rand sampling" begin
Random.seed!(123)
mi, ma = extrema(rand(BFloat16, 1_000_000))
# zero should be the lowest BFloat16 sampled
@test mi === zero(BFloat16)
# prevfloat(one(BFloat16)) cannot be sampled bc
# prevfloat(BFloat16(2)) - 1 is _two_ before one(BFloat16)
# (a statistical flaw of the [1,2)-1 sampling)
@test ma === prevfloat(one(BFloat16), 2)
end
include("structure.jl")
include("mathfuncs.jl")