From 76b798d539cbb2e8760b77e4d5ca63dfa64d9001 Mon Sep 17 00:00:00 2001 From: Jinguo Liu Date: Thu, 18 Jul 2024 01:57:15 +0800 Subject: [PATCH] The minus operation for long integer (#55) * The minus operation for long integer * bump version --- .github/workflows/CI.yml | 9 ++++++--- Project.toml | 2 +- src/longlonguint.jl | 17 +++++++++++++++++ test/longlonguint.jl | 9 +++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 04e92b0..d42bc08 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,7 +1,11 @@ name: CI on: - - push - - pull_request + push: + branches: + - main + tags: ['*'] + pull_request: + workflow_dispatch: jobs: test: name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }} @@ -15,7 +19,6 @@ jobs: os: - ubuntu-latest - macOS-latest - - windows-latest arch: - x64 steps: diff --git a/Project.toml b/Project.toml index 7bb3f41..870d78e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "BitBasis" uuid = "50ba71b6-fa0f-514d-ae9a-0916efc90dcf" -version = "0.9.4" +version = "0.9.5" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/longlonguint.jl b/src/longlonguint.jl index 709cc9c..2f3dcfe 100644 --- a/src/longlonguint.jl +++ b/src/longlonguint.jl @@ -79,7 +79,24 @@ function _sadd(x::NTuple{C,UInt}, y::NTuple{C,UInt}, c::Bool) where {C} return (_sadd(x[1:C-1], y[1:C-1], c1)..., v1) end end +function Base.:(-)(x::LongLongUInt{C}, y::LongLongUInt{C}) where {C} + return LongLongUInt(_ssub(x.content, y.content, false)) +end +function _ssub(x::NTuple{1,UInt}, y::NTuple{1,UInt}, c::Bool) + return (x[1] - y[1] - c,) +end +function _ssub(x::NTuple{C,UInt}, y::NTuple{C,UInt}, c::Bool) where {C} + v1, c1 = Base.sub_with_overflow(x[C], y[C]) + if c + v2, c2 = Base.sub_with_overflow(v1, c) + c = c1 || c2 + return (_ssub(x[1:C-1], y[1:C-1], c)..., v2) + else + return (_ssub(x[1:C-1], y[1:C-1], c1)..., v1) + end +end Base.count_ones(x::LongLongUInt) = sum(count_ones, x.content) +Base.bitstring(x::LongLongUInt) = join(bitstring.(x.content), "") function longinttype(n::Int, D::Int) N = ceil(Int, n * log2(D)) diff --git a/test/longlonguint.jl b/test/longlonguint.jl index 212ae40..b6eecc1 100644 --- a/test/longlonguint.jl +++ b/test/longlonguint.jl @@ -5,8 +5,10 @@ using Test, BitBasis @test Int(x) === 3 @test bsizeof(x) == 64 @test BitBasis.nint(x) == 1 + @test bitstring(x) == "0000000000000000000000000000000000000000000000000000000000000011" x = LongLongUInt((3, 6)) + @test bitstring(x) == "00000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000110" @test zero(x) == LongLongUInt((0, 0)) @test x << 1 == LongLongUInt((6, 12)) @test x >> 1 == LongLongUInt((UInt(1), UInt(3) + UInt(1)<<63)) @@ -19,6 +21,8 @@ using Test, BitBasis @test x | y == LongLongUInt((7, 7)) @test x ⊻ y == LongLongUInt((6, 1)) @test x + y == LongLongUInt((8, 13)) + z = LongLongUInt((5, 4)) + @test z - x == LongLongUInt((UInt64(1), typemax(UInt64)-1)) # add with overflow z = LongLongUInt((UInt(17), typemax(UInt)-1)) @@ -77,4 +81,9 @@ end @test readbit(x, 66) == 1 @test readbit(x, 67) == 0 @test readbit(BitStr{78}(x), 66) == 1 +end + +@testset "bmask" begin + @test bmask(LongLongUInt{1}, 1:1) == LongLongUInt((UInt64(1),)) + @test bmask(LongLongUInt{2}, 1:65) == LongLongUInt((UInt64(1), typemax(UInt64))) end \ No newline at end of file