Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix type-stability bugs in Ryu code #52781

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions base/ryu/exp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function writeexp(buf, pos, v::T,
pos = append_sign(x, plus, space, buf, pos)

# special cases
if x == 0
if iszero(x)
@inbounds buf[pos] = UInt8('0')
pos += 1
if precision > 0 && !trimtrailingzeros
Expand Down Expand Up @@ -42,7 +42,7 @@ function writeexp(buf, pos, v::T,
mant = bits & MANTISSA_MASK
exp = Int((bits >> 52) & EXP_MASK)

if exp == 0
if iszero(exp)
e2 = 1 - 1023 - 52
m2 = mant
else
Expand All @@ -51,7 +51,7 @@ function writeexp(buf, pos, v::T,
end
nonzero = false
precision += 1
digits = 0
digits = zero(UInt32)
printedDigits = 0
availableDigits = 0
e = 0
Expand All @@ -64,14 +64,14 @@ function writeexp(buf, pos, v::T,
j = p10bits - e2
#=@inbounds=# mula, mulb, mulc = POW10_SPLIT[POW10_OFFSET[idx + 1] + i + 1]
digits = mulshiftmod1e9(m2 << 8, mula, mulb, mulc, j + 8)
if printedDigits != 0
if !iszero(printedDigits)
if printedDigits + 9 > precision
availableDigits = 9
break
end
pos = append_nine_digits(digits, buf, pos)
printedDigits += 9
elseif digits != 0
elseif !iszero(digits)
availableDigits = decimallength(digits)
e = i * 9 + availableDigits - 1
if availableDigits > precision
Expand All @@ -93,26 +93,26 @@ function writeexp(buf, pos, v::T,
i -= 1
end
end
if e2 < 0 && availableDigits == 0
if e2 < 0 && iszero(availableDigits)
idx = div(-e2, 16)
i = MIN_BLOCK_2[idx + 1]
i = Int(MIN_BLOCK_2[idx + 1])
while i < 200
j = 120 + (-e2 - 16 * idx)
p = POW10_OFFSET_2[idx + 1] + i - MIN_BLOCK_2[idx + 1]
if p >= POW10_OFFSET_2[idx + 2]
digits = 0
digits = zero(UInt32)
else
#=@inbounds=# mula, mulb, mulc = POW10_SPLIT_2[p + 1]
digits = mulshiftmod1e9(m2 << 8, mula, mulb, mulc, j + 8)
end
if printedDigits != 0
if !iszero(printedDigits)
if printedDigits + 9 > precision
availableDigits = 9
break
end
pos = append_nine_digits(digits, buf, pos)
printedDigits += 9
elseif digits != 0
elseif !iszero(digits)
availableDigits = decimallength(digits)
e = -(i + 1) * 9 + availableDigits - 1
if availableDigits > precision
Expand All @@ -135,14 +135,14 @@ function writeexp(buf, pos, v::T,
end
end
maximum = precision - printedDigits
if availableDigits == 0
digits = 0
if iszero(availableDigits)
digits = zero(UInt32)
end
lastDigit = 0
lastDigit = zero(UInt32)
if availableDigits > maximum
for k = 0:(availableDigits - maximum - 1)
lastDigit = digits % 10
digits = div(digits, 10)
lastDigit = digits % UInt32(10)
digits = div(digits, UInt32(10))
end
end
roundUp = 0
Expand All @@ -159,8 +159,8 @@ function writeexp(buf, pos, v::T,
end
roundUp = trailingZeros ? 2 : 1
end
if printedDigits != 0
if digits == 0
if !iszero(printedDigits)
if iszero(digits)
for _ = 1:maximum
@inbounds buf[pos] = UInt8('0')
pos += 1
Expand All @@ -180,7 +180,7 @@ function writeexp(buf, pos, v::T,
end
end
end
if roundUp != 0
if !iszero(roundUp)
roundPos = pos
while true
roundPos -= 1
Expand All @@ -197,7 +197,7 @@ function writeexp(buf, pos, v::T,
roundUp = 1
continue
else
if roundUp == 2 && UInt8(c) % 2 == 0
if roundUp == 2 && iseven(c)
break
end
@inbounds buf[roundPos] = c + 1
Expand All @@ -224,7 +224,7 @@ function writeexp(buf, pos, v::T,
pos += 1
end
if e >= 100
c = e % 10
c = UInt8(e % 10)
stevengj marked this conversation as resolved.
Show resolved Hide resolved
@inbounds d100 = DIGIT_TABLE16[div(e, 10) + 1]
@inbounds buf[pos] = d100 % UInt8
@inbounds buf[pos + 1] = (d100 >> 0x8) % UInt8
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1145,4 +1145,7 @@ end
@test_throws Printf.InvalidFormatStringError Printf.Format("%z")
end

# issue #52749
@test @sprintf("%.160g", 1.38e-23) == "1.380000000000000060010582465734078799297660966782642624395399644741944111814291318296454846858978271484375e-23"

end # @testset "Printf"