Code Monkey home page Code Monkey logo

bitoperations.jl's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bitoperations.jl's Issues

add bflip, mention Int32 bit indexing

It would be helpful to toggle bit[s].

v0.5:
@inline bflip(x::Integer, bit::Integer) = x $ bmask(typeof(x), bit)
@inline bflip{U <: Integer}(x::Integer, bits::UnitRange{U}) = x $ bmask(typeof(x), bits))
v0.6:
@inline bflip(x::Integer, bit::Integer) = xor(x, bmask(typeof(x), bit))
@inline bflip{U <: Integer}(x::Integer, bits::UnitRange{U}) = xor(x, bmask(typeof(x), bits))

Julia codes tighter when the bit, bits, nbits args are Int32 (or Int16 or Int8).

On a 64bit [32bit] system, bmask(Int32, 5) is bmask(Int32, 5::Int64) [bmask(Int32, 5::32)].

#                                                                               using bit::Int32

julia> code_llvm(bmask, (Type{Int32}, Int32))
define i32 @julia_bmask_62946(%jl_value_t*, i32) #0 !dbg !5 {
top:
  %2 = call i32 @"julia_<<_62947"(i32 1, i32 %1) #0
  ret i32 %2
}

julia> code_native(bmask, (Type{Int32}, Int32))
	.text
Filename: bitops.jl
	pushq	%rbp
	movq	%rsp, %rbp
Source line: 32
	movabsq	$"<<", %rax
	movl	$1, %edi
	callq	*%rax
	popq	%rbp
	retq
	nopw	(%rax,%rax)


#                                                                               using bit::Int64

julia> code_llvm(bmask, (Type{Int32}, Int64))
define i32 @julia_bmask_62948(%jl_value_t*, i64) #0 !dbg !5 {
top:
  %2 = icmp slt i64 %1, 0
  %3 = trunc i64 %1 to i32
  %4 = shl i32 1, %3
  %5 = icmp ugt i64 %1, 31
  %6 = select i1 %5, i32 0, i32 %4
  %7 = sub i64 0, %1
  %8 = trunc i64 %7 to i32
  %9 = lshr i32 1, %8
  %10 = icmp ugt i64 %7, 31
  %11 = select i1 %10, i32 0, i32 %9
  %12 = select i1 %2, i32 %11, i32 %6
  ret i32 %12
}

julia> code_native(bmask, (Type{Int32}, Int64))
	.text
Filename: bitops.jl
	pushq	%rbp
	movq	%rsp, %rbp
Source line: 32
	movl	$1, %eax
	movl	$1, %edx
	movl	%esi, %ecx
	shll	%cl, %edx
	xorl	%edi, %edi
	cmpq	$31, %rsi
	cmoval	%edi, %edx
	movq	%rsi, %rcx
	negq	%rcx
	shrl	%cl, %eax
	cmpq	$31, %rcx
	cmoval	%edi, %eax
	testq	%rsi, %rsi
	cmovnsl	%edx, %eax
	popq	%rbp
	retq
	nopw	%cs:(%rax,%rax)

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

UnitRange is not entirely transparent on usage, and StepRanges don't work.

First issue: arrays start at one in julia, but using a range starting with one with bitops returns a range starting with the second bit.

julia> bits = 0b1010100010101100
0xa8ac

julia> bget(bits, 1:4)
0x0006

julia> bget(bits, 0:4)
0x000c

julia> (bitstring(bget(bits, 1:4)), bitstring(bget(bits, 0:4)))
("0000000000000110", "0000000000001100")

Additionally, StepRanges (declared with start:step:stop) do not work. This would be useful to have properly working, for xample with a backwards-stepped range, such as 16:-1:0

julia> bget(bits, 1:2:4)
ERROR: MethodError: no method matching bget(::UInt16, ::StepRange{Int64, Int64})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.