forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintrinsics.jl
84 lines (73 loc) · 2.83 KB
/
intrinsics.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
# This file is a part of Julia. License is MIT: https://julialang.org/license
# intrinsic functions
# For curmod_*
include("testenv.jl")
# bits types
@test isa((() -> Core.Intrinsics.bitcast(Ptr{Int8}, 0))(), Ptr{Int8})
@test isa(convert(Char, 65), Char)
# runtime intrinsics
let f = Any[Core.Intrinsics.add_int, Core.Intrinsics.sub_int]
@test f[1](1, 1) == 2
@test f[2](1, 1) == 0
end
# issue #4581
primitive type Date4581{T} 64 end
let
x = Core.Intrinsics.bitcast(Date4581{Int}, Int64(1234))
xs = Date4581[x]
ys = copy(xs)
@test ys !== xs
@test ys == xs
end
# issue #6591
function f6591(d)
Core.Intrinsics.bitcast(Int64, d)
return (f -> f(d))(identity)
end
let d = Core.Intrinsics.bitcast(Date4581{Int}, Int64(1))
@test isa(f6591(d), Date4581)
end
# test functionality of non-power-of-2 primitive type constants
primitive type Int24 24 end
Int24(x::Int) = Core.Intrinsics.trunc_int(Int24, x)
Int(x::Int24) = Core.Intrinsics.zext_int(Int, x)
let x, y, f
x = Int24(Int(0x12345678)) # create something (via truncation)
@test Int(0x345678) === Int(x)
f() = Int24(Int(0x02468ace))
y = f() # invoke llvm constant folding
@test Int(0x468ace) === Int(y)
@test x !== y
@test string(y) == "$(curmod_prefix)Int24(0x468ace)"
end
# test nonsensical valid conversions and errors
compiled_addi(x, y) = Core.Intrinsics.add_int(x, y)
@test compiled_addi(C_NULL, C_NULL) === C_NULL
@test_throws ErrorException compiled_addi(C_NULL, 1)
@test_throws ErrorException ((x)->compiled_addi(Float64(x), x))(1)
@test ((x)->compiled_addi(Float64(x), Float64(x)))(2) === -0.0
@test compiled_addi(0.5, 5.0e-323) === 0.5000000000000011
@test_throws ErrorException compiled_addi(Int8(1), UInt8(1))
@test compiled_addi(UInt8(1), UInt8(2)) === UInt8(3)
@test_throws ErrorException compiled_addi(UInt8(1), UInt16(2))
@test compiled_addi(Float32(.125), Float32(10)) === 2.1267648f38
@test compiled_addi(true, true) === false
compiled_addf(x, y) = Core.Intrinsics.add_float(x, y)
@test compiled_addf(C_NULL, C_NULL) === C_NULL
@test_throws ErrorException compiled_addf(C_NULL, 1)
@test compiled_addf(0.5, 5.0e-323) === 0.5
@test_throws ErrorException compiled_addf(im, im)
@test_throws ErrorException compiled_addf(true, true)
function compiled_conv(::Type{T}, x) where T
t = Core.Intrinsics.trunc_int(T, x)
z = Core.Intrinsics.zext_int(typeof(x), t)
s = Core.Intrinsics.sext_int(typeof(x), t)
fpt = Core.Intrinsics.fptrunc(T, x)
fpe = Core.Intrinsics.fpext(typeof(x), fpt)
return (t, z, s, fpt, fpe)
end
@test compiled_conv(UInt32, Int64(0x8000_0000)) ==
(0x80000000, Int64(0x80000000), -Int64(0x80000000), 0x00000000, 0)
@test compiled_conv(UInt32, UInt64(0xC000_BA98_8765_4321)) ==
(0x87654321, 0x0000000087654321, 0xffffffff87654321, 0xc005d4c4, 0xc000ba9880000000)
@test_throws ErrorException compiled_conv(Bool, im)