Skip to content

Commit f84322d

Browse files
authored
new: log2i, BitInt (#61)
1 parent 17325cc commit f84322d

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/longlonguint.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Base.zero(::Type{LongLongUInt{C}}) where {C} = LongLongUInt{C}(ntuple(_->UInt(0)
2323
Base.zero(::LongLongUInt{C}) where {C} = zero(LongLongUInt{C})
2424
# convert from integers
2525
LongLongUInt{C}(x::T) where {C, T<:Integer} = LongLongUInt{C}(ntuple(i->i==C ? UInt(x) : zero(UInt), Val{C}()))
26+
LongLongUInt{C}(x::LongLongUInt{C}) where {C} = x
2627
Base.promote_rule(::Type{LongLongUInt{C}}, ::Type{Int}) where {C} = LongLongUInt{C}
2728
Base.promote_rule(::Type{LongLongUInt{C}}, ::Type{UInt}) where {C} = LongLongUInt{C}
2829
function Base.mod(x::LongLongUInt{C}, D::Int) where {C}
@@ -135,3 +136,7 @@ end
135136

136137
Base.hash(x::LongLongUInt{1}) = hash(x.content[1])
137138
Base.hash(x::LongLongUInt{C}) where{C} = hash(x.content)
139+
140+
# these APIs will are used in SparseTN
141+
BitBasis.log2i(x::LongLongUInt{C}) where C = floor(Int, log2(Float64(BigInt(x))))
142+
Base.BigInt(x::LongLongUInt{C}) where C = mapfoldl(x -> BigInt(x), (x, y) -> ((x << 64) | y), x.content)

test/longlonguint.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using Test, BitBasis
22

33
@testset "longlonguint" begin
44
x = LongLongUInt((3,))
5+
@test LongLongUInt{1}(x) == x
56
@test Int(x) === 3
67
@test bsizeof(x) == 64
78
@test BitBasis.nint(x) == 1
@@ -124,3 +125,14 @@ end
124125
sorted_xs = sort(xs)
125126
@test sorted_xs == [LongLongUInt((2, 4)), LongLongUInt((3, 6)), LongLongUInt((3, 7))]
126127
end
128+
129+
@testset "patch" begin
130+
@test BigInt(LongLongUInt((3,))) == 3
131+
@test BigInt(LongLongUInt((3, 0))) == BigInt(3) << 64
132+
@test BigInt(LongLongUInt((0, 3))) == 3
133+
@test BigInt(LongLongUInt((1,2))) == (BigInt(1) << 64) + BigInt(2)
134+
@test log2i(LongLongUInt((3,))) == 1
135+
@test log2i(LongLongUInt((1,2))) == 64
136+
@test log2i(LongLongUInt((0,2))) == 1
137+
@test log2i(LongLongUInt((1,0))) == 64
138+
end

0 commit comments

Comments
 (0)