Skip to content

Commit

Permalink
Add ⊻ as alternative to xor, and ⊻= as alternative to $=.
Browse files Browse the repository at this point in the history
Please note that `⊻=` is in the add-dots group in the parser, preparing for `.⊻=`.
However, this commit does not implement `.⊻` yet, this should happen in
a separate commit with `.&` and `.|` together.
  • Loading branch information
davidavdav committed Oct 30, 2016
1 parent 5e853bb commit ce94dff
Show file tree
Hide file tree
Showing 33 changed files with 100 additions and 80 deletions.
2 changes: 1 addition & 1 deletion base/associative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ const hasha_seed = UInt === UInt64 ? 0x6d35bb51952d5539 : 0x952d5539
function hash(a::Associative, h::UInt)
h = hash(hasha_seed, h)
for (k,v) in a
h = xor(h, hash(k, hash(v)))
h ⊻= hash(k, hash(v))
end
return h
end
Expand Down
2 changes: 1 addition & 1 deletion base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,7 @@ map!(f::typeof(max), dest::BitArray, A::BitArray, B::BitArray) = map!(|, dest, A
map!(f::typeof(!=), dest::BitArray, A::BitArray, B::BitArray) = map!(xor, dest, A, B)
map!(f::Union{typeof(>=), typeof(^)}, dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> p | ~q), dest, A, B)
map!(f::typeof(<=), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~p | q), dest, A, B)
map!(f::typeof(==), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~xor(p, q)), dest, A, B)
map!(f::typeof(==), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~(p q)), dest, A, B)
map!(f::typeof(<), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> ~p & q), dest, A, B)
map!(f::typeof(>), dest::BitArray, A::BitArray, B::BitArray) = map!(BitChunkFunctor((p, q) -> p & ~q), dest, A, B)

Expand Down
4 changes: 2 additions & 2 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ function broadcast_bitarrays(scalarf, bitf, A::AbstractArray{Bool}, B::AbstractA
return F
end

biteq(a::UInt64, b::UInt64) = xor(~a, b)
biteq(a::UInt64, b::UInt64) = ~a b
bitlt(a::UInt64, b::UInt64) = ~a & b
bitneq(a::UInt64, b::UInt64) = xor(a, b)
bitneq(a::UInt64, b::UInt64) = a b
bitle(a::UInt64, b::UInt64) = ~a | b

.==(A::AbstractArray{Bool}, B::AbstractArray{Bool}) = broadcast_bitarrays(==, biteq, A, B)
Expand Down
2 changes: 1 addition & 1 deletion base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ in(x::Char, y::Char) = x == y
isless(x::Char, y::Char) = UInt32(x) < UInt32(y)

const hashchar_seed = 0xd4d64234
hash(x::Char, h::UInt) = hash_uint64(xor((UInt64(x)+hashchar_seed)<<32, UInt64(h)))
hash(x::Char, h::UInt) = hash_uint64(((UInt64(x)+hashchar_seed)<<32) UInt64(h))

-(x::Char, y::Char) = Int(x) - Int(y)
-(x::Char, y::Integer) = Char(Int32(x) - Int32(y))
Expand Down
2 changes: 1 addition & 1 deletion base/combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function isperm(A)
n = length(A)
used = falses(n)
for a in A
(0 < a <= n) && (used[a] = xor(used[a], true)) || return false
(0 < a <= n) && (used[a] ⊻= true) || return false
end
true
end
Expand Down
4 changes: 2 additions & 2 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ const hash_0_imag = hash(0, h_imag)

function hash(z::Complex, h::UInt)
# TODO: with default argument specialization, this would be better:
# hash(real(z), h $ hash(imag(z), h $ h_imag) $ hash(0, h $ h_imag))
hash(real(z), xor(h, hash(imag(z), h_imag), hash_0_imag))
# hash(real(z), h hash(imag(z), h h_imag) hash(0, h h_imag))
hash(real(z), h hash(imag(z), h_imag) hash_0_imag)
end

## generic functions of complex numbers ##
Expand Down
12 changes: 6 additions & 6 deletions base/dSFMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@ function dsfmt_jump_add!(dest::Vector{UInt64}, src::Vector{UInt64})
while i <= N-diff
j = i*2-1
p = j + diff*2
dest[j] = xor(dest[j], src[p])
dest[j+1] = xor(dest[j+1], src[p+1])
dest[j] ⊻= src[p]
dest[j+1] ⊻= src[p+1]
i += 1
end
while i <= N
j = i*2-1
p = j + (diff - N)*2
dest[j] = xor(dest[j], src[p])
dest[j+1] = xor(dest[j+1], src[p+1])
dest[j] ⊻= src[p]
dest[j+1] ⊻= src[p+1]
i += 1
end
dest[N*2+1] = xor(dest[N*2+1], src[N*2+1])
dest[N*2+2] = xor(dest[N*2+2], src[N*2+2])
dest[N*2+1] ⊻= src[N*2+1]
dest[N*2+2] ⊻= src[N*2+2]
return dest
end

Expand Down
6 changes: 5 additions & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,11 @@ end))
@deprecate ipermutedims(A::AbstractArray,p) permutedims(A, invperm(p))

# 18696
@deprecate ($) xor
function ($)(x, y)
depwarn("`x \$ y` is deprecated. use `xor(x, y)` or `x ⊻ y` instead.", :$)
xor(x, y)
end
export $

@deprecate is (===)

Expand Down
1 change: 1 addition & 0 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3386,6 +3386,7 @@ dawson

"""
xor(x, y)
⊻(x, y)
Bitwise exclusive or.
"""
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export
,
,
xor,
,
%,
÷,
&,
Expand Down
2 changes: 1 addition & 1 deletion base/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ mul_fast{T<:FloatTypes}(x::T, y::T, zs::T...) =
cmp_fast{T<:FloatTypes}(x::T, y::T) = ifelse(x==y, 0, ifelse(x<y, -1, +1))
function mod_fast{T<:FloatTypes}(x::T, y::T)
r = rem(x,y)
ifelse(xor(r > 0, y > 0), r+y, r)
ifelse((r > 0) (y > 0), r+y, r)
end
end

Expand Down
8 changes: 4 additions & 4 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ _default_type(T::Union{Type{Real},Type{AbstractFloat}}) = Float64
## floating point arithmetic ##
-(x::Float64) = box(Float64,neg_float(unbox(Float64,x)))
-(x::Float32) = box(Float32,neg_float(unbox(Float32,x)))
-(x::Float16) = reinterpret(Float16, xor(reinterpret(UInt16,x), 0x8000))
-(x::Float16) = reinterpret(Float16, reinterpret(UInt16,x) 0x8000)

for op in (:+,:-,:*,:/,:\,:^)
@eval ($op)(a::Float16, b::Float16) = Float16(($op)(Float32(a), Float32(b)))
Expand Down Expand Up @@ -381,7 +381,7 @@ function mod{T<:AbstractFloat}(x::T, y::T)
r = rem(x,y)
if r == 0
copysign(r,y)
elseif xor(r > 0, y > 0)
elseif (r > 0) (y > 0)
r+y
else
r
Expand Down Expand Up @@ -512,7 +512,7 @@ const hx_NaN = hx(UInt64(0), NaN, UInt(0 ))

hash(x::UInt64, h::UInt) = hx(x, Float64(x), h)
hash(x::Int64, h::UInt) = hx(reinterpret(UInt64,abs(x)), Float64(x), h)
hash(x::Float64, h::UInt) = isnan(x) ? xor(hx_NaN, h) : hx(box(UInt64,fptoui(unbox(Float64,abs(x)))), x, h)
hash(x::Float64, h::UInt) = isnan(x) ? (hx_NaN h) : hx(box(UInt64,fptoui(unbox(Float64,abs(x)))), x, h)

hash(x::Union{Bool,Int8,UInt8,Int16,UInt16,Int32,UInt32}, h::UInt) = hash(Int64(x), h)
hash(x::Float32, h::UInt) = hash(Float64(x), h)
Expand Down Expand Up @@ -558,7 +558,7 @@ function nextfloat(f::Union{Float16,Float32,Float64}, d::Integer)
fu = fumax
else
du = da % U
if xor(fneg, dneg)
if fneg dneg
if du > fu
fu = min(fumax, du - fu)
fneg = !fneg
Expand Down
2 changes: 1 addition & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function convert{T<:Signed}(::Type{T}, x::BigInt)
else
0 <= n <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError())
y = x % T
xor(x.size > 0, y > 0) && throw(InexactError()) # catch overflow
(x.size > 0) (y > 0) && throw(InexactError()) # catch overflow
y
end
end
Expand Down
18 changes: 9 additions & 9 deletions base/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,34 @@ hash(x::ANY, h::UInt) = 3*object_id(x) - h
function hash_64_64(n::UInt64)
local a::UInt64 = n
a = ~a + a << 21
a = xor(a, a >> 24)
a = a a >> 24
a = a + a << 3 + a << 8
a = xor(a, a >> 14)
a = a a >> 14
a = a + a << 2 + a << 4
a = xor(a, a >> 28)
a = a a >> 28
a = a + a << 31
return a
end

function hash_64_32(n::UInt64)
local a::UInt64 = n
a = ~a + a << 18
a = xor(a, a >> 31)
a = a a >> 31
a = a * 21
a = xor(a, a >> 11)
a = a a >> 11
a = a + a << 6
a = xor(a, a >> 22)
a = a a >> 22
return a % UInt32
end

function hash_32_32(n::UInt32)
local a::UInt32 = n
a = a + 0x7ed55d16 + a << 12
a = xor(a, 0xc761c23c, a >> 19)
a = a 0xc761c23c a >> 19
a = a + 0x165667b1 + a << 5
a = a + xor(0xd3a2646c, a << 9)
a = a + 0xd3a2646c a << 9
a = a + 0xfd7046c5 + a << 3
a = xor(a, 0xb55a4f09, a >> 16)
a = a 0xb55a4f09 a >> 16
return a
end

Expand Down
8 changes: 4 additions & 4 deletions base/hashing2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
## efficient value-based hashing of integers ##

function hash_integer(n::Integer, h::UInt)
h = xor(hash_uint(xor(n % UInt, h)), h)
h ⊻= hash_uint((n % UInt) h)
n = abs(n)
n >>>= sizeof(UInt) << 3
while n != 0
h = xor(hash_uint(xor(n % UInt, h)), h)
h ⊻= hash_uint((n % UInt) h)
n >>>= sizeof(UInt) << 3
end
return h
Expand All @@ -18,9 +18,9 @@ function hash_integer(n::BigInt, h::UInt)
s == 0 && return hash_integer(0, h)
p = convert(Ptr{UInt}, n.d)
b = unsafe_load(p)
h = xor(hash_uint(xor(ifelse(s < 0, -b, b), h)), h)
h ⊻= hash_uint(ifelse(s < 0, -b, b) h)
for k = 2:abs(s)
h = xor(hash_uint(xor(unsafe_load(p, k), h)), h)
h ⊻= hash_uint(unsafe_load(p, k) h)
end
return h
end
Expand Down
4 changes: 2 additions & 2 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ flipsign(x::Signed, y::Float32) = flipsign(x, reinterpret(Int32,y))
flipsign(x::Signed, y::Float64) = flipsign(x, reinterpret(Int64,y))
flipsign(x::Signed, y::Real) = flipsign(x, -oftype(x,signbit(y)))

copysign(x::Signed, y::Signed) = flipsign(x, xor(x,y))
copysign(x::Signed, y::Signed) = flipsign(x, x y)
copysign(x::Signed, y::Float16) = copysign(x, reinterpret(Int16,y))
copysign(x::Signed, y::Float32) = copysign(x, reinterpret(Int32,y))
copysign(x::Signed, y::Float64) = copysign(x, reinterpret(Int64,y))
Expand Down Expand Up @@ -149,7 +149,7 @@ rem{T<:BitUnsigned64}(x::T, y::T) = box(T,checked_urem_int(unbox(T,x),unbox(T,y)
fld{T<:Unsigned}(x::T, y::T) = div(x,y)
function fld{T<:Integer}(x::T, y::T)
d = div(x,y)
d - (signbit(xor(x,y)) & (d*y!=x))
d - (signbit(x y) & (d*y!=x))
end

# cld(x,y) = div(x,y) + ((x>0) == (y>0) && rem(x,y) != 0 ? 1 : 0)
Expand Down
6 changes: 3 additions & 3 deletions base/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function symdiff!(s::IntSet, n::Integer)
elseif n < 0
throw(ArgumentError("IntSet elements cannot be negative"))
end
s.bits[n>>5 + 1] = xor(s.bits[n>>5 + 1], UInt32(1)<<(n&31))
s.bits[n>>5 + 1] ⊻= UInt32(1)<<(n&31)
return s
end

Expand Down Expand Up @@ -282,14 +282,14 @@ function symdiff!(s::IntSet, s2::IntSet)
end
lim = length(s2.bits)
for n = 1:lim
s.bits[n] = xor(s.bits[n], s2.bits[n])
s.bits[n] ⊻= s2.bits[n]
end
if s2.fill1s
for n=lim+1:length(s.bits)
s.bits[n] = ~s.bits[n]
end
end
s.fill1s = xor(s.fill1s, s2.fill1s)
s.fill1s ⊻= s2.fill1s
s
end

Expand Down
1 change: 1 addition & 0 deletions base/latex_symbols.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const latex_symbols = Dict(
"\\pppprime" => "",
"\\backpprime" => "",
"\\backppprime" => "",
"\\xor" => "",

# Superscripts
"\\^0" => "",
Expand Down
6 changes: 3 additions & 3 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,10 @@ function significand{T<:AbstractFloat}(x::T)
if xe == 0 # x is subnormal
x == 0 && return x
xs = xu & sign_mask(T)
xu = xor(xu, xs)
xu ⊻= xs
m = leading_zeros(xu)-exponent_bits(T)
xu <<= m
xu = xor(xu, xs)
xu ⊻= xs
elseif xe == exponent_mask(T) # NaN or Inf
return x
end
Expand All @@ -419,7 +419,7 @@ function frexp{T<:AbstractFloat}(x::T)
xs == 0 && return x, 0 # +-0
m = unsigned(leading_zeros(xs) - exponent_bits(T))
xs <<= m
xu = xor(xs, (xu & sign_mask(T)))
xu = xs (xu & sign_mask(T))
k = 1 - signed(m)
end
k -= (exponent_bias(T) - 1)
Expand Down
5 changes: 4 additions & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ identity(x) = x
(|)(x::Integer) = x
xor(x::Integer) = x

const = xor

# foldl for argument lists. expand recursively up to a point, then
# switch to a loop. this allows small cases like `a+b+c+d` to be inlined
# efficiently, without a major slowdown for `+(x...)` when `x` is big.
Expand Down Expand Up @@ -1039,6 +1041,7 @@ export
,
,
,
,
colon,
hcat,
vcat,
Expand All @@ -1052,6 +1055,6 @@ import ..this_module: !, !=, xor, %, .%, ÷, .÷, &, *, +, -, .!=, .+, .-, .*, .
.>=, .\, .^, /, //, <, <:, <<, <=, ==, >, >=, >>, .>>, .<<, >>>,
<|, |>, \, ^, |, ~, !==, ===, >:, colon, hcat, vcat, hvcat, getindex, setindex!,
transpose, ctranspose,
, , , .≥, .≤, .≠, , ×, , , , , , , , , , ,
, , , .≥, .≤, .≠, , ×, , , , , , , , , , , ,

end
16 changes: 8 additions & 8 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ rand(r::Union{RandomDevice,MersenneTwister}, ::Type{Float32}) =

function rand(r::MersenneTwister, ::Type{UInt64})
reserve(r, 2)
xor(rand_ui52_raw_inbounds(r) << 32, rand_ui52_raw_inbounds(r))
rand_ui52_raw_inbounds(r) << 32 rand_ui52_raw_inbounds(r)
end

function rand(r::MersenneTwister, ::Type{UInt128})
Expand Down Expand Up @@ -447,7 +447,7 @@ function rand!{T<:Union{Float16, Float32}}(r::MersenneTwister, A::Array{T}, ::Ty
A128 = unsafe_wrap(Array, convert(Ptr{UInt128}, pointer(A)), n128)
@inbounds for i in 1:n128
u = A128[i]
u = xor(u, u << 26)
u ⊻= u << 26
# at this point, the 64 low bits of u, "k" being the k-th bit of A128[i] and "+" the bit xor, are:
# [..., 58+32,..., 53+27, 52+26, ..., 33+7, 32+6, ..., 27+1, 26, ..., 1]
# the bits needing to be random are
Expand Down Expand Up @@ -488,17 +488,17 @@ function rand!(r::MersenneTwister, A::Array{UInt128}, n::Int=length(A))
i = 0
@inbounds while n-i >= 5
u = A[i+=1]
A[n] = xor(A[n], u << 48)
A[n-1] = xor(A[n-1], u << 36)
A[n-2] = xor(A[n-2], u << 24)
A[n-3] = xor(A[n-3], u << 12)
n -= 4
A[n] ⊻= u << 48
A[n-=1] ⊻= u << 36
A[n-=1] ⊻= u << 24
A[n-=1] ⊻= u << 12
n-=1
end
end
if n > 0
u = rand_ui2x52_raw(r)
for i = 1:n
@inbounds A[i] = xor(A[i], u << 12*i)
@inbounds A[i] ⊻= u << 12*i
end
end
A
Expand Down
2 changes: 1 addition & 1 deletion base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const hashs_seed = UInt === UInt64 ? 0x852ada37cfe8e0ce : 0xcfe8e0ce
function hash(s::Set, h::UInt)
h = hash(hashs_seed, h)
for x in s
h = xor(h, hash(x))
h ⊻= hash(x)
end
return h
end
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ const uni_ops = Set{Symbol}([:(+), :(-), :(!), :(¬), :(~), :(<:), :(>:), :(√)
const expr_infix_wide = Set{Symbol}([
:(=), :(+=), :(-=), :(*=), :(/=), :(\=), :(^=), :(&=), :(|=), :(÷=), :(%=), :(>>>=), :(>>=), :(<<=),
:(.=), :(.+=), :(.-=), :(.*=), :(./=), :(.\=), :(.^=), :(.&=), :(.|=), :(.÷=), :(.%=), :(.>>>=), :(.>>=), :(.<<=),
:(&&), :(||), :(<:), :(=>), :($=)])
:(&&), :(||), :(<:), :(=>), :($=), :(⊻=)])
const expr_infix = Set{Symbol}([:(:), :(->), Symbol("::")])
const expr_infix_any = union(expr_infix, expr_infix_wide)
const all_ops = union(quoted_syms, uni_ops, expr_infix_any)
Expand Down
Loading

0 comments on commit ce94dff

Please sign in to comment.