Skip to content

Commit

Permalink
Adjustments for printf life
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Aug 10, 2019
1 parent 303bc54 commit b788d6f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 25 deletions.
4 changes: 2 additions & 2 deletions base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ end

function writefixed(x::T, precision) where {T <: Base.IEEEFloat}
buf = Vector{UInt8}(undef, precision + shortestdigits(T))
pos = writefixed(x, precision, buf, 1)
pos = writefixed(buf, 1, x, false, false, false, precision)
return unsafe_string(pointer(buf), pos-1)
end

function writeexp(x::T, precision) where {T <: Base.IEEEFloat}
buf = Vector{UInt8}(undef, precision + shortestdigits(T))
pos = writeexp(x, precision, buf, 1)
pos = writeexp(buf, 1, x, false, false, false, precision)
return unsafe_string(pointer(buf), pos-1)
end

Expand Down
60 changes: 48 additions & 12 deletions base/ryu/exp.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
@inline function writeexp(v::T, precision, buf, pos) where {T <: Base.IEEEFloat}
@inline function writeexp(buf, pos, v::T,
plus=false, space=false, hash=false,
precision=-1, expchar=UInt8('e'), decchar=UInt8('.')) where {T <: Base.IEEEFloat}
x = Float64(v)
neg = signbit(x)
# special cases
@inbounds if x == 0
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
buf[pos] = UInt8('0')
pos += 1
if precision > 0
buf[pos] = UInt8('.')
buf[pos] = decchar
pos += 1
for _ = 1:precision
buf[pos] = UInt8('0')
pos += 1
end
elseif hash
buf[pos] = decchar
pos += 1
end
buf[pos] = UInt8('e')
buf[pos] = expchar
buf[pos + 1] = UInt8('+')
buf[pos + 2] = UInt8('0')
buf[pos + 3] = UInt8('0')
Expand All @@ -30,11 +41,18 @@
elseif !isfinite(x)
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
buf[pos + neg] = UInt8('I')
buf[pos + neg + 1] = UInt8('n')
buf[pos + neg + 2] = UInt8('f')
return pos + neg + 3
buf[pos] = UInt8('I')
buf[pos + 1] = UInt8('n')
buf[pos + 2] = UInt8('f')
return pos + 3
end

bits = Core.bitcast(UInt64, x)
Expand All @@ -53,6 +71,12 @@
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
digits = 0
printedDigits = 0
Expand Down Expand Up @@ -81,10 +105,14 @@
break
end
if precision > 1
pos = append_d_digits(availableDigits, digits, buf, pos)
pos = append_d_digits(availableDigits, digits, buf, pos, decchar)
else
buf[pos] = UInt8('0') + digits
pos += 1
if hash
buf[pos] = decchar
pos += 1
end
end
printedDigits = availableDigits
availableDigits = 0
Expand Down Expand Up @@ -118,10 +146,14 @@
break
end
if precision > 1
pos = append_d_digits(availableDigits, digits, buf, pos)
pos = append_d_digits(availableDigits, digits, buf, pos, decchar)
else
buf[pos] = UInt8('0') + digits
pos += 1
if hash
buf[pos] = decchar
pos += 1
end
end
printedDigits = availableDigits
availableDigits = 0
Expand Down Expand Up @@ -165,10 +197,14 @@
end
else
if precision > 1
pos = append_d_digits(maximum, digits, buf, pos)
pos = append_d_digits(maximum, digits, buf, pos, decchar)
else
buf[pos] = UInt8('0') + digits
pos += 1
if hash
buf[pos] = decchar
pos += 1
end
end
end
if roundUp != 0
Expand All @@ -181,7 +217,7 @@
break
end
c = roundPos > 0 ? buf[roundPos] : 0x00
if c == UInt8('.')
if c == decchar
continue
elseif c == UInt8('9')
buf[roundPos] = UInt8('0')
Expand All @@ -196,7 +232,7 @@
end
end
end
buf[pos] = UInt8('e')
buf[pos] = expchar
pos += 1
if e < 0
buf[pos] = UInt8('-')
Expand Down
46 changes: 35 additions & 11 deletions base/ryu/fixed.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
@inline function writefixed(v::T, precision, buf, pos, trimtrailingzeros=false) where {T <: Base.IEEEFloat}
@inline function writefixed(buf, pos, v::T,
plus=false, space=false, hash=false,
precision=-1, decchar=UInt8('.'), trimtrailingzeros=false) where {T <: Base.IEEEFloat}
x = Float64(v)
neg = signbit(x)
# special cases
@inbounds if x == 0
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
buf[pos] = UInt8('0')
pos += 1
if precision > 0
buf[pos] = UInt8('.')
buf[pos] = decchar
pos += 1
if trimtrailingzeros
precision = 1
Expand All @@ -19,6 +27,9 @@
buf[pos] = UInt8('0')
pos += 1
end
elseif hash
buf[pos] = decchar
pos += 1
end
return pos
elseif isnan(x)
Expand All @@ -29,11 +40,18 @@
elseif !isfinite(x)
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
buf[pos + neg] = UInt8('I')
buf[pos + neg + 1] = UInt8('n')
buf[pos + neg + 2] = UInt8('f')
return pos + neg + 3
buf[pos] = UInt8('I')
buf[pos + 1] = UInt8('n')
buf[pos + 2] = UInt8('f')
return pos + 3
end

bits = Core.bitcast(UInt64, x)
Expand All @@ -51,6 +69,12 @@
if neg
buf[pos] = UInt8('-')
pos += 1
elseif plus
buf[pos] = UInt8('+')
pos += 1
elseif space
buf[pos] = UInt8(' ')
pos += 1
end
if e2 >= -52
idx = e2 < 0 ? 0 : indexforexp(e2)
Expand All @@ -75,8 +99,8 @@
buf[pos] = UInt8('0')
pos += 1
end
if precision > 0
buf[pos] = UInt8('.')
if precision > 0 || hash
buf[pos] = decchar
pos += 1
end
if e2 < 0
Expand Down Expand Up @@ -144,14 +168,14 @@
buf[roundPos + 1] = UInt8('1')
if dotPos > 1
buf[dotPos] = UInt8('0')
buf[dotPos + 1] = UInt8('.')
buf[dotPos + 1] = decchar
end
buf[pos] = UInt8('0')
pos += 1
break
end
c = roundPos > 0 ? buf[roundPos] : 0x00
if c == UInt8('.')
if c == decchar
dotPos = roundPos
continue
elseif c == UInt8('9')
Expand All @@ -177,7 +201,7 @@
while buf[pos - 1] == UInt8('0')
pos -= 1
end
if buf[pos - 1] == UInt8('.')
if buf[pos - 1] == decchar
pos += 1
end
end
Expand Down

0 comments on commit b788d6f

Please sign in to comment.