Skip to content

Commit

Permalink
Introduce ⊻, ∧, and ∨ as alternative notations to xor, & and |.
Browse files Browse the repository at this point in the history
Also, allow \xor as latex-entry alternative to \veebar.

Base functions using xor() either use ⊻ or xor, depending on what is easiest
to read.  Minimal use of parentheses is a factor here.

In some tests, both alternatives are explicitly tested, but this is kept to a
small number of occurances since both alternatives should be equivalent.
  • Loading branch information
davidavdav committed Oct 19, 2016
1 parent 07276c0 commit 0b31ae0
Show file tree
Hide file tree
Showing 23 changed files with 98 additions and 62 deletions.
2 changes: 1 addition & 1 deletion base/associative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,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 = 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 @@ -1888,7 +1888,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 @@ -478,9 +478,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] = used[a] true) || return false
end
true
end
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] = dest[j] src[p]
dest[j+1] = 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] = dest[j] src[p]
dest[j+1] = 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] = dest[N*2+1] src[N*2+1]
dest[N*2+2] = 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,6 +1022,10 @@ 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 $

# End deprecations scheduled for 0.6
3 changes: 3 additions & 0 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ exp10

"""
&(x, y)
∧(x, y)
Bitwise and.
Expand Down Expand Up @@ -3330,6 +3331,7 @@ Val

"""
|(x, y)
∨(x, y)
Bitwise or.
Expand Down Expand Up @@ -3413,6 +3415,7 @@ dawson

"""
xor(x, y)
⊻(x, y)
Bitwise exclusive or.
"""
Expand Down
3 changes: 3 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ export
,
,
xor,
,
%,
÷,
&,
,
*,
+,
-,
Expand Down Expand Up @@ -249,6 +251,7 @@ export
\,
^,
|,
,
|>,
~,
:,
Expand Down
6 changes: 3 additions & 3 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 @@ -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
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) 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) 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) 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) 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] = 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] = 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 = 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
8 changes: 4 additions & 4 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 = xu xs
m = leading_zeros(xu)-exponent_bits(T)
xu <<= m
xu = xor(xu, xs)
xu = xu xs
elseif xe == exponent_mask(T) # NaN or Inf
return x
end
Expand All @@ -417,10 +417,10 @@ function frexp{T<:AbstractFloat}(x::T)
if xe == 0 # x is subnormal
x == 0 && return x, 0
xs = xu & sign_mask(T)
xu = xor(xu, xs)
xu = xu xs
m = leading_zeros(xu)-exponent_bits(T)
xu <<= m
xu = xor(xu, xs)
xu = xu xs
k = 1-m
elseif xe == exponent_mask(T) # NaN or Inf
return x, 0
Expand Down
9 changes: 8 additions & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ identity(x) = x
(|)(x::Integer) = x
xor(x::Integer) = x

const = xor
const = &
const = |

# 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 @@ -1040,6 +1044,9 @@ export
,
,
,
,
,
,
colon,
hcat,
vcat,
Expand All @@ -1053,6 +1060,6 @@ import ..this_module: !, !=, xor, %, .%, ÷, .÷, &, *, +, -, .!=, .+, .-, .*, .
.>=, .\, .^, /, //, <, <:, <<, <=, ==, >, >=, >>, .>>, .<<, >>>,
<|, |>, \, ^, |, ~, !==, ===, >:, colon, hcat, vcat, hvcat, getindex, setindex!,
transpose, ctranspose,
, , , .≥, .≤, .≠, , ×, , , , , , , , , , ,
, , , .≥, .≤, .≠, , ×, , , , , , , , , , , , , ,

end
12 changes: 6 additions & 6 deletions base/random.jl
Original file line number Diff line number Diff line change
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 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)
A[n] = A[n] u << 48
A[n-1] = A[n-1] u << 36
A[n-2] = A[n-2] u << 24
A[n-3] = A[n-3] u << 12
n -= 4
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] = 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 = h hash(x)
end
return h
end
Expand Down
Loading

0 comments on commit 0b31ae0

Please sign in to comment.