@@ -2,6 +2,15 @@ using Test, BFloat16s, Printf, Random
2
2
3
3
@info " Testing BFloat16s" BFloat16s. llvm_storage BFloat16s. llvm_arithmetic
4
4
5
+ @testset " basics" begin
6
+ @test Base. exponent_bits (BFloat16) == 8
7
+ @test Base. significand_bits (BFloat16) == 7
8
+ @test precision (BFloat16) == 8
9
+
10
+ @test typemin (BFloat16) == - BFloat16s. InfB16
11
+ @test typemax (BFloat16) == BFloat16s. InfB16
12
+ end
13
+
5
14
@testset " comparisons" begin
6
15
@test BFloat16 (1 ) < BFloat16 (2 )
7
16
@test BFloat16 (1f0 ) < BFloat16 (2f0 )
@@ -18,17 +27,35 @@ using Test, BFloat16s, Printf, Random
18
27
@test BFloat16 (2 ) != BFloat16 (1 )
19
28
@test BFloat16 (2f0 ) != BFloat16 (1f0 )
20
29
@test BFloat16 (2.0 ) != BFloat16 (1.0 )
30
+ @test BFloat16 (NaN ) != BFloat16 (1.0 )
21
31
@test iszero (BFloat16 (0 )) == true
22
32
@test iszero (BFloat16 (3.45 )) == false
23
33
end
24
34
35
+ @testset " trunc" begin
36
+ bf_val = 5.5
37
+ @testset " $Ti " for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
38
+ @test trunc (Ti, bf_val) == Ti (5 )
39
+ end
40
+
41
+ @test trunc (BFloat16, Float32 (π)) == BFloat16 (3.140625 )
42
+
43
+ # InexactError
44
+ @test_throws InexactError trunc (Int8,BFloat16 (500 ))
45
+ end
46
+
25
47
@testset " conversions" begin
26
48
@test Float32 (BFloat16 (10 )) == 1f1
27
49
@test Float64 (BFloat16 (10 )) == 10.0
28
50
@test Int32 (BFloat16 (10 )) == Int32 (10 )
29
51
@test Int64 (BFloat16 (10 )) == Int64 (10 )
30
52
@test BFloat16 (BigFloat (1 )) == BFloat16 (1 )
31
53
@test BigFloat (BFloat16 (1 )) == BigFloat (1 )
54
+ @test Float16 (BFloat16 (3.140625 )) == Float16 (π)
55
+ @test BFloat16 (Float16 (π)) == BFloat16 (3.140625 )
56
+
57
+ @test_throws InexactError Int8 (BFloat16 (500 ))
58
+ @test_throws InexactError UInt8 (BFloat16 (500 ))
32
59
end
33
60
34
61
@testset " abi" begin
44
71
@test BFloat16 (2 ) ^ BFloat16 (4 ) == BFloat16 (16 )
45
72
@test eps (BFloat16) == BFloat16 (0.0078125 )
46
73
@test sqrt (BFloat16 (4f0 )) == BFloat16 (2f0 )
74
+ @test rem (BFloat16 (3.14 ), Int) == 3
75
+ @test round (BFloat16 (10.4 ), RoundToZero) == BFloat16 (10.0 )
47
76
@test round (BFloat16 (10.4 ), RoundUp) == BFloat16 (11.0 )
48
77
@test round (BFloat16 (10.6 ), RoundDown) == BFloat16 (10.0 )
49
78
@test round (BFloat16 (3.2 ), RoundNearest) == BFloat16 (3.0 )
92
121
@test (@sprintf " %a" BFloat16 (1.5 )) == " 0x1.8p+0"
93
122
end
94
123
124
+ @testset " show" begin
125
+ @test repr (BFloat16 (Inf )) == " InfB16"
126
+ @test repr (BFloat16 (- Inf )) == " -InfB16"
127
+ @test repr (BFloat16 (NaN )) == " NaNB16"
128
+ @test repr (BFloat16 (2 )) == " BFloat16(2.0)"
129
+ end
130
+
95
131
@testset " random" begin
96
132
x = Array {BFloat16} (undef, 10 )
97
133
y = Array {BFloat16} (undef, 10 )
171
207
end
172
208
173
209
@testset " maxintfloat" begin
174
-
210
+
175
211
a = maxintfloat (BFloat16)
176
212
@test a+ 1 - 1 == a- 1 # the first +1 cannot be represented
177
213
@test a- 1 + 1 == a # but -1 can
180
216
@testset " rand sampling" begin
181
217
Random. seed! (123 )
182
218
mi, ma = extrema (rand (BFloat16, 1_000_000 ))
183
-
219
+
184
220
# zero should be the lowest BFloat16 sampled
185
221
@test mi === zero (BFloat16)
186
222
189
225
end
190
226
191
227
include (" structure.jl" )
192
- include (" mathfuncs.jl" )
228
+ include (" mathfuncs.jl" )
0 commit comments