Skip to content

Commit

Permalink
scatter inbounds
Browse files Browse the repository at this point in the history
  • Loading branch information
green-nsk committed Jul 2, 2021
1 parent 9dbc3f9 commit f864790
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Ryu

import .Base: significand_bits, significand_mask, exponent_bits, exponent_mask, exponent_bias, exponent_max, uinttype
import .Base: with_scratch
import .Base: with_scratch, @propagate_inbounds

include("utils.jl")
include("shortest.jl")
Expand Down Expand Up @@ -47,7 +47,7 @@ function writeshortest(x::T,
typed::Bool=false,
compact::Bool=false) where {T <: Base.IEEEFloat}
with_scratch(neededdigits(T)) do buf
pos = writeshortest(buf, 1, x, plus, space, hash, precision, expchar, padexp, decchar, typed, compact)
pos = @inbounds writeshortest(buf, 1, x, plus, space, hash, precision, expchar, padexp, decchar, typed, compact)
return String(@inbounds view(buf, 1:pos - 1))
end
end
Expand Down Expand Up @@ -76,7 +76,7 @@ function writefixed(x::T,
decchar::UInt8=UInt8('.'),
trimtrailingzeros::Bool=false) where {T <: Base.IEEEFloat}
with_scratch(precision + neededdigits(T)) do buf
pos = writefixed(buf, 1, x, precision, plus, space, hash, decchar, trimtrailingzeros)
pos = @inbounds writefixed(buf, 1, x, precision, plus, space, hash, decchar, trimtrailingzeros)
return String(@inbounds view(buf, 1:pos - 1))
end
end
Expand Down Expand Up @@ -107,7 +107,7 @@ function writeexp(x::T,
decchar::UInt8=UInt8('.'),
trimtrailingzeros::Bool=false) where {T <: Base.IEEEFloat}
with_scratch(precision + neededdigits(T)) do buf
pos = writeexp(buf, 1, x, precision, plus, space, hash, expchar, decchar, trimtrailingzeros)
pos = @inbounds writeexp(buf, 1, x, precision, plus, space, hash, expchar, decchar, trimtrailingzeros)
return String(@inbounds view(buf, 1:pos - 1))
end
end
Expand All @@ -116,7 +116,7 @@ function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false
compact = get(io, :compact, false)::Bool
with_scratch(neededdigits(T)) do buf
typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x)
pos = writeshortest(buf, 1, x, false, false, true, -1,
pos = @inbounds writeshortest(buf, 1, x, false, false, true, -1,
(x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
write(io, @inbounds view(buf, 1:pos - 1))
return
Expand All @@ -125,7 +125,7 @@ end

function Base.string(x::T) where {T <: Base.IEEEFloat}
with_scratch(neededdigits(T)) do buf
pos = writeshortest(buf, 1, x, false, false, true, -1,
pos = @inbounds writeshortest(buf, 1, x, false, false, true, -1,
UInt8('e'), false, UInt8('.'), false, false)
return String(@inbounds view(buf, 1:pos - 1))
end
Expand Down
4 changes: 2 additions & 2 deletions base/ryu/exp.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function writeexp(buf, pos, v::T,
@propagate_inbounds function writeexp(buf, pos, v::T,
precision=-1, plus=false, space=false, hash=false,
expchar=UInt8('e'), decchar=UInt8('.'), trimtrailingzeros=false) where {T <: Base.IEEEFloat}
@assert 0 < pos <= length(buf)
@boundscheck (0 < pos <= length(buf) || throw(BoundsError()))
startpos = pos
x = Float64(v)
pos = append_sign(x, plus, space, buf, pos)
Expand Down
4 changes: 2 additions & 2 deletions base/ryu/fixed.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function writefixed(buf, pos, v::T,
@propagate_inbounds function writefixed(buf, pos, v::T,
precision=-1, plus=false, space=false, hash=false,
decchar=UInt8('.'), trimtrailingzeros=false) where {T <: Base.IEEEFloat}
@assert 0 < pos <= length(buf)
@boundscheck (0 < pos <= length(buf) || throw(BoundsError()))
startpos = pos
x = Float64(v)
pos = append_sign(x, plus, space, buf, pos)
Expand Down
4 changes: 2 additions & 2 deletions base/ryu/shortest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ integer. If a `maxsignif` argument is provided, then `b < maxsignif`.
return b, e10
end

function writeshortest(buf, pos, x::T,
@propagate_inbounds function writeshortest(buf, pos, x::T,
plus=false, space=false, hash=true,
precision=-1, expchar=UInt8('e'), padexp=false, decchar=UInt8('.'),
typed=false, compact=false) where {T}
@assert 0 < pos <= length(buf)
@boundscheck (0 < pos <= length(buf) || throw(BoundsError()))
# special cases
if x == 0
if typed && x isa Float16
Expand Down
12 changes: 6 additions & 6 deletions base/ryu/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Computes `e == 0 ? 1 : ceil(log2(5^e))`. This is valid for `e < 3529` (if perfor
"""
pow5bits(e) = ((e * 1217359) >> 19) + 1

""""
"""
Ryu.mulshift(m::U, mula, j) where {U<:Unsigned}
Compute `(m * mul) >> j`, where `j >= 8*sizeof(U)`. The type of the results is the larger of `U` or `UInt32`.
Expand Down Expand Up @@ -195,7 +195,7 @@ Compute `(m * mul) >> j % 10^9` where `mul = mula + mulb<<64 + mulc<<128`, and `
return (v % UInt32) - UInt32(1000000000) * shifted
end

@inline function append_sign(x, plus, space, buf, pos)
@propagate_inbounds function append_sign(x, plus, space, buf, pos)
if signbit(x) && !isnan(x) # suppress minus sign for signaling NaNs
buf[pos] = UInt8('-')
pos += 1
Expand All @@ -209,7 +209,7 @@ end
return pos
end

@inline function append_n_digits(olength, digits, buf, pos)
@propagate_inbounds function append_n_digits(olength, digits, buf, pos)
i = 0
while digits >= 10000
c = digits % 10000
Expand Down Expand Up @@ -237,7 +237,7 @@ end
return pos + i
end

@inline function append_d_digits(olength, digits, buf, pos, decchar)
@propagate_inbounds function append_d_digits(olength, digits, buf, pos, decchar)
i = 0
while digits >= 10000
c = digits % 10000
Expand Down Expand Up @@ -268,7 +268,7 @@ end
return pos + i
end

@inline function append_c_digits(count, digits, buf, pos)
@propagate_inbounds function append_c_digits(count, digits, buf, pos)
i = 0
while i < count - 1
c = (digits % 100) << 1
Expand All @@ -283,7 +283,7 @@ end
return pos + i
end

@inline function append_nine_digits(digits, buf, pos)
@propagate_inbounds function append_nine_digits(digits, buf, pos)
if digits == 0
for _ = 1:9
buf[pos] = UInt8('0')
Expand Down

0 comments on commit f864790

Please sign in to comment.