Skip to content

Commit fb91075

Browse files
authored
Fix MSVC narrowing-conversion warnings (C4244/C4267) under /W3 (#1431)
Add explicit casts to silence 8 warnings triggered by MSVC /W3: fptostring.cpp: - Add static_cast<int>(precision) where public API (size_t) calls internal template (int) - Add static_cast<int>(...) for pointer-arithmetic result stored in int dragonbox.h: - Add static_cast<uint_least32_t>(n) in umul64() calls inside if-constexpr branches guarded by is_same<UInt, uint_least32_t> to silence MSVC warnings on discarded branches when UInt=uint_least64_t - Change trailing-zero counter 's' from size_t to DecimalExponentType in both remove_trailing_zeros specializations to match exponent type Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> log: fix MSVC narrowing conversion warnings
1 parent 1b60fae commit fb91075

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

src/contrib/dragonbox.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ namespace jkj {
12311231
// reason if we just write n / 10.
12321232
JKJ_IF_CONSTEXPR(stdr::is_same<UInt, stdr::uint_least32_t>::value && N == 1 &&
12331233
n_max <= UINT32_C(1073741828)) {
1234-
return UInt(wuint::umul64(n, UINT32_C(429496730)) >> 32);
1234+
return UInt(wuint::umul64(static_cast<stdr::uint_least32_t>(n), UINT32_C(429496730)) >> 32);
12351235
}
12361236
// Specialize for 64-bit division by 10.
12371237
// Without the bound on n_max (which compilers these days never leverage), the
@@ -1244,7 +1244,7 @@ namespace jkj {
12441244
// It seems compilers tend to generate mov + mul instead of a single imul for an
12451245
// unknown reason if we just write n / 100.
12461246
else JKJ_IF_CONSTEXPR(stdr::is_same<UInt, stdr::uint_least32_t>::value && N == 2) {
1247-
return UInt(wuint::umul64(n, UINT32_C(1374389535)) >> 37);
1247+
return UInt(wuint::umul64(static_cast<stdr::uint_least32_t>(n), UINT32_C(1374389535)) >> 37);
12481248
}
12491249
// Specialize for 64-bit division by 1000.
12501250
// Without the bound on n_max (which compilers these days never leverage), the
@@ -2956,7 +2956,7 @@ namespace jkj {
29562956
auto r = detail::bits::rotr<32>(
29572957
detail::stdr::uint_least32_t(significand * UINT32_C(184254097)), 4);
29582958
auto b = r < UINT32_C(429497);
2959-
auto s = detail::stdr::size_t(b);
2959+
auto s = DecimalExponentType(b);
29602960
significand = b ? r : significand;
29612961

29622962
r = detail::bits::rotr<32>(
@@ -2988,7 +2988,7 @@ namespace jkj {
29882988
auto r = detail::bits::rotr<64>(
29892989
detail::stdr::uint_least64_t(significand * UINT64_C(28999941890838049)), 8);
29902990
auto b = r < UINT64_C(184467440738);
2991-
auto s = detail::stdr::size_t(b);
2991+
auto s = DecimalExponentType(b);
29922992
significand = b ? r : significand;
29932993

29942994
r = detail::bits::rotr<64>(

src/fptostring.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ std::string FpToString(T v, int precision = 0) {
192192
int const zero_digits_ct = before_decimal_digits - digits_ct;
193193

194194
// space left in the output_buffer (-1 because we need it for null-termination)
195-
int const buffer_empty_space = output_buffer.data() + output_buffer.size() - output_ptr - 1;
195+
int const buffer_empty_space = static_cast<int>(
196+
output_buffer.data() + output_buffer.size() - output_ptr - 1);
196197

197198
// print all zeros not fitting into the buffer at the end of the function
198199
overflow_zeros = std::max(0, zero_digits_ct - buffer_empty_space);
@@ -231,11 +232,11 @@ std::string FpToString(T v, int precision = 0) {
231232
}
232233

233234
std::string FpToString(float v, size_t precision) {
234-
return detail::fp_formatting::FpToString(v, precision);
235+
return detail::fp_formatting::FpToString(v, static_cast<int>(precision));
235236
}
236237

237238
std::string FpToString(double v, size_t precision) {
238-
return detail::fp_formatting::FpToString(v, precision);
239+
return detail::fp_formatting::FpToString(v, static_cast<int>(precision));
239240
}
240241

241242
/**

0 commit comments

Comments
 (0)