Skip to content

Commit ca047d2

Browse files
authored
Merge pull request #928 from cppalliance/927
Fix boundary check in to_integer
2 parents cdd7182 + ecdb00f commit ca047d2

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

include/boost/decimal/detail/to_integral.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ constexpr auto to_integral(Decimal val) noexcept
6666

6767
if (expval > 0)
6868
{
69-
if (expval > std::numeric_limits<Conversion_Type>::digits)
69+
if (expval > std::numeric_limits<Conversion_Type>::digits10 + 1)
7070
{
7171
return std::numeric_limits<TargetType>::max();
7272
}
@@ -75,7 +75,7 @@ constexpr auto to_integral(Decimal val) noexcept
7575
}
7676
else if (expval < 0)
7777
{
78-
if (abs_exp_val > std::numeric_limits<Conversion_Type>::digits)
78+
if (abs_exp_val > std::numeric_limits<Conversion_Type>::digits10 + 1)
7979
{
8080
return static_cast<TargetType>(0);
8181
}

test/benchmarks.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ BOOST_DECIMAL_NO_INLINE void test_comparisons(const std::vector<T>& data_vec, co
155155

156156
const auto t2 = std::chrono::steady_clock::now();
157157

158-
std::cout << "comparisons<" << std::left << std::setw(11) << label << ">: " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
158+
std::cerr << "comparisons<" << std::left << std::setw(11) << label << ">: " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
159159
}
160160

161161
template <typename T, typename Func>
@@ -176,7 +176,7 @@ BOOST_DECIMAL_NO_INLINE void test_two_element_operation(const std::vector<T>& da
176176

177177
const auto t2 = std::chrono::steady_clock::now();
178178

179-
std::cout << operation << "<" << std::left << std::setw(11) << type << ">: " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
179+
std::cerr << operation << "<" << std::left << std::setw(11) << type << ">: " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
180180
}
181181

182182
template <typename T, typename Func>
@@ -195,7 +195,7 @@ BOOST_DECIMAL_NO_INLINE void test_one_element_operation(const std::vector<T>& da
195195

196196
const auto t2 = std::chrono::steady_clock::now();
197197

198-
std::cout << operation << "<" << std::left << std::setw(11) << type << ">: " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
198+
std::cerr << operation << "<" << std::left << std::setw(11) << type << ">: " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
199199
}
200200

201201
template <typename T>
@@ -247,7 +247,7 @@ static BOOST_DECIMAL_NO_INLINE void test_boost_to_chars( std::vector<T> const& d
247247

248248
auto t2 = std::chrono::steady_clock::now();
249249

250-
std::cout << "boost::decimal::to_chars<" << std::left << std::setw(11) << type << ">, " << label << ", " << precision << ": " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
250+
std::cerr << "boost::decimal::to_chars<" << std::left << std::setw(11) << type << ">, " << label << ", " << precision << ": " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
251251
}
252252

253253
template <typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
@@ -276,7 +276,7 @@ static BOOST_DECIMAL_NO_INLINE void test_boost_to_chars( std::vector<T> const& d
276276

277277
auto t2 = std::chrono::steady_clock::now();
278278

279-
std::cout << " std::to_chars<" << std::left << std::setw(10) << type << ">, " << label << ", " << precision << ": " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
279+
std::cerr << " std::to_chars<" << std::left << std::setw(10) << type << ">, " << label << ", " << precision << ": " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
280280
}
281281

282282

@@ -363,7 +363,7 @@ BOOST_DECIMAL_NO_INLINE void test_boost_from_chars( std::vector<std::string> con
363363

364364
auto t2 = std::chrono::steady_clock::now();
365365

366-
std::cout << " std::from_chars<" << std::left << std::setw(11) << type << ">, " << label << ": " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
366+
std::cerr << " std::from_chars<" << std::left << std::setw(11) << type << ">, " << label << ": " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
367367
}
368368

369369
template <typename T, std::enable_if_t<!std::is_floating_point<T>::value, bool> = true>
@@ -385,7 +385,7 @@ BOOST_DECIMAL_NO_INLINE void test_boost_from_chars( std::vector<std::string> con
385385

386386
auto t2 = std::chrono::steady_clock::now();
387387

388-
std::cout << "boost::decimal::from_chars<" << std::left << std::setw(11) << type << ">, " << label << ": " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
388+
std::cerr << "boost::decimal::from_chars<" << std::left << std::setw(11) << type << ">, " << label << ": " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
389389
}
390390

391391
template <typename T>
@@ -413,7 +413,7 @@ int main()
413413
const auto dec64_fast_vector = convert_copy_vector<decimal64_fast>(dec64_vector);
414414
const auto dec128_fast_vector = convert_copy_vector<decimal128_fast>(dec128_vector);
415415

416-
std::cout << "===== Comparisons =====\n";
416+
std::cerr << "===== Comparisons =====\n";
417417

418418
test_comparisons(float_vector, "float");
419419
test_comparisons(double_vector, "double");
@@ -424,7 +424,7 @@ int main()
424424
test_comparisons(dec64_fast_vector, "dec64_fast");
425425
test_comparisons(dec128_fast_vector, "dec128_fast");
426426

427-
std::cout << "\n===== Addition =====\n";
427+
std::cerr << "\n===== Addition =====\n";
428428

429429
test_two_element_operation(float_vector, std::plus<>(), "Addition", "float");
430430
test_two_element_operation(double_vector, std::plus<>(), "Addition", "double");
@@ -435,7 +435,7 @@ int main()
435435
test_two_element_operation(dec64_fast_vector, std::plus<>(), "Addition", "dec64_fast");
436436
test_two_element_operation(dec128_fast_vector, std::plus<>(), "Addition", "dec128_fast");
437437

438-
std::cout << "\n===== Subtraction =====\n";
438+
std::cerr << "\n===== Subtraction =====\n";
439439

440440
test_two_element_operation(float_vector, std::minus<>(), "Subtraction", "float");
441441
test_two_element_operation(double_vector, std::minus<>(), "Subtraction", "double");
@@ -446,7 +446,7 @@ int main()
446446
test_two_element_operation(dec64_fast_vector, std::minus<>(), "Subtraction", "dec64_fast");
447447
test_two_element_operation(dec128_fast_vector, std::minus<>(), "Subtraction", "dec128_fast");
448448

449-
std::cout << "\n===== Multiplication =====\n";
449+
std::cerr << "\n===== Multiplication =====\n";
450450

451451
test_two_element_operation(float_vector, std::multiplies<>(), "Multiplication", "float");
452452
test_two_element_operation(double_vector, std::multiplies<>(), "Multiplication", "double");
@@ -457,7 +457,7 @@ int main()
457457
test_two_element_operation(dec64_fast_vector, std::multiplies<>(), "Multiplication", "dec64_fast");
458458
test_two_element_operation(dec128_fast_vector, std::multiplies<>(), "Multiplication", "dec128_fast");
459459

460-
std::cout << "\n===== Division =====\n";
460+
std::cerr << "\n===== Division =====\n";
461461

462462
test_two_element_operation(float_vector, std::divides<>(), "Division", "float");
463463
test_two_element_operation(double_vector, std::divides<>(), "Division", "double");
@@ -469,7 +469,7 @@ int main()
469469
test_two_element_operation(dec128_fast_vector, std::divides<>(), "Division", "dec128_fast");
470470

471471
#if 0
472-
std::cout << "\n===== sqrt =====\n";
472+
std::cerr << "\n===== sqrt =====\n";
473473

474474
test_one_element_operation(float_vector, (float(*)(float))std::sqrt, "sqrt", "float");
475475
test_one_element_operation(double_vector, (double(*)(double))std::sqrt, "sqrt", "double");
@@ -478,15 +478,15 @@ int main()
478478
test_one_element_operation(dec128_vector, (decimal128(*)(decimal128))sqrt, "sqrt", "decimal128");
479479
#endif
480480
#ifdef BOOST_DECIMAL_BENCHMARK_CHARCONV
481-
std::cout << "\n===== <charconv> to_chars =====\n";
481+
std::cerr << "\n===== <charconv> to_chars =====\n";
482482
test_to_chars<float>("float");
483483
test_to_chars<double>("double");
484484
test_to_chars<decimal32>("decimal32");
485485
test_to_chars<decimal64>("decimal64");
486486
test_to_chars<decimal32_fast>("dec32_fast");
487487
test_to_chars<decimal64_fast>("dec64_fast");
488488

489-
std::cout << "\n===== <charconv> from_chars =====\n";
489+
std::cerr << "\n===== <charconv> from_chars =====\n";
490490
test_from_chars<float>(false, "float");
491491
test_from_chars<float>(true, "float");
492492
test_from_chars<double>(false, "double");
@@ -500,7 +500,7 @@ int main()
500500
test_from_chars<decimal64>(false, "dec64_fast");
501501
test_from_chars<decimal64>(true, "dec64_fast");
502502
#endif
503-
std::cout << std::endl;
503+
std::cerr << std::endl;
504504

505505
return 1;
506506
}
@@ -509,7 +509,7 @@ int main()
509509

510510
int main()
511511
{
512-
std::cout << "Benchmarks not run" << std::endl;
512+
std::cerr << "Benchmarks not run" << std::endl;
513513
return 1;
514514
}
515515

0 commit comments

Comments
 (0)