Skip to content

Commit e0e3c63

Browse files
stevengjKristofferC
authored andcommitted
fix type-stability bugs in Ryu code (#52781)
Fixes #52749. (cherry picked from commit 5643c60)
1 parent a4cc920 commit e0e3c63

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

base/ryu/exp.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function writeexp(buf, pos, v::T,
77
pos = append_sign(x, plus, space, buf, pos)
88

99
# special cases
10-
if x == 0
10+
if iszero(x)
1111
@inbounds buf[pos] = UInt8('0')
1212
pos += 1
1313
if precision > 0 && !trimtrailingzeros
@@ -42,7 +42,7 @@ function writeexp(buf, pos, v::T,
4242
mant = bits & MANTISSA_MASK
4343
exp = Int((bits >> 52) & EXP_MASK)
4444

45-
if exp == 0
45+
if iszero(exp)
4646
e2 = 1 - 1023 - 52
4747
m2 = mant
4848
else
@@ -51,7 +51,7 @@ function writeexp(buf, pos, v::T,
5151
end
5252
nonzero = false
5353
precision += 1
54-
digits = 0
54+
digits = zero(UInt32)
5555
printedDigits = 0
5656
availableDigits = 0
5757
e = 0
@@ -64,14 +64,14 @@ function writeexp(buf, pos, v::T,
6464
j = p10bits - e2
6565
#=@inbounds=# mula, mulb, mulc = POW10_SPLIT[POW10_OFFSET[idx + 1] + i + 1]
6666
digits = mulshiftmod1e9(m2 << 8, mula, mulb, mulc, j + 8)
67-
if printedDigits != 0
67+
if !iszero(printedDigits)
6868
if printedDigits + 9 > precision
6969
availableDigits = 9
7070
break
7171
end
7272
pos = append_nine_digits(digits, buf, pos)
7373
printedDigits += 9
74-
elseif digits != 0
74+
elseif !iszero(digits)
7575
availableDigits = decimallength(digits)
7676
e = i * 9 + availableDigits - 1
7777
if availableDigits > precision
@@ -93,26 +93,26 @@ function writeexp(buf, pos, v::T,
9393
i -= 1
9494
end
9595
end
96-
if e2 < 0 && availableDigits == 0
96+
if e2 < 0 && iszero(availableDigits)
9797
idx = div(-e2, 16)
98-
i = MIN_BLOCK_2[idx + 1]
98+
i = Int(MIN_BLOCK_2[idx + 1])
9999
while i < 200
100100
j = 120 + (-e2 - 16 * idx)
101101
p = POW10_OFFSET_2[idx + 1] + i - MIN_BLOCK_2[idx + 1]
102102
if p >= POW10_OFFSET_2[idx + 2]
103-
digits = 0
103+
digits = zero(UInt32)
104104
else
105105
#=@inbounds=# mula, mulb, mulc = POW10_SPLIT_2[p + 1]
106106
digits = mulshiftmod1e9(m2 << 8, mula, mulb, mulc, j + 8)
107107
end
108-
if printedDigits != 0
108+
if !iszero(printedDigits)
109109
if printedDigits + 9 > precision
110110
availableDigits = 9
111111
break
112112
end
113113
pos = append_nine_digits(digits, buf, pos)
114114
printedDigits += 9
115-
elseif digits != 0
115+
elseif !iszero(digits)
116116
availableDigits = decimallength(digits)
117117
e = -(i + 1) * 9 + availableDigits - 1
118118
if availableDigits > precision
@@ -135,14 +135,14 @@ function writeexp(buf, pos, v::T,
135135
end
136136
end
137137
maximum = precision - printedDigits
138-
if availableDigits == 0
139-
digits = 0
138+
if iszero(availableDigits)
139+
digits = zero(UInt32)
140140
end
141-
lastDigit = 0
141+
lastDigit = zero(UInt32)
142142
if availableDigits > maximum
143143
for k = 0:(availableDigits - maximum - 1)
144-
lastDigit = digits % 10
145-
digits = div(digits, 10)
144+
lastDigit = digits % UInt32(10)
145+
digits = div(digits, UInt32(10))
146146
end
147147
end
148148
roundUp = 0
@@ -159,8 +159,8 @@ function writeexp(buf, pos, v::T,
159159
end
160160
roundUp = trailingZeros ? 2 : 1
161161
end
162-
if printedDigits != 0
163-
if digits == 0
162+
if !iszero(printedDigits)
163+
if iszero(digits)
164164
for _ = 1:maximum
165165
@inbounds buf[pos] = UInt8('0')
166166
pos += 1
@@ -180,7 +180,7 @@ function writeexp(buf, pos, v::T,
180180
end
181181
end
182182
end
183-
if roundUp != 0
183+
if !iszero(roundUp)
184184
roundPos = pos
185185
while true
186186
roundPos -= 1
@@ -197,7 +197,7 @@ function writeexp(buf, pos, v::T,
197197
roundUp = 1
198198
continue
199199
else
200-
if roundUp == 2 && UInt8(c) % 2 == 0
200+
if roundUp == 2 && iseven(c)
201201
break
202202
end
203203
@inbounds buf[roundPos] = c + 1
@@ -224,7 +224,7 @@ function writeexp(buf, pos, v::T,
224224
pos += 1
225225
end
226226
if e >= 100
227-
c = e % 10
227+
c = (e % 10) % UInt8
228228
@inbounds d100 = DIGIT_TABLE16[div(e, 10) + 1]
229229
@inbounds buf[pos] = d100 % UInt8
230230
@inbounds buf[pos + 1] = (d100 >> 0x8) % UInt8

stdlib/Printf/test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,4 +1145,7 @@ end
11451145
@test_throws Printf.InvalidFormatStringError Printf.Format("%z")
11461146
end
11471147

1148+
# issue #52749
1149+
@test @sprintf("%.160g", 1.38e-23) == "1.380000000000000060010582465734078799297660966782642624395399644741944111814291318296454846858978271484375e-23"
1150+
11481151
end # @testset "Printf"

0 commit comments

Comments
 (0)