From 3ba78cfdce5f2638a0e079264c4c0405616bcabf Mon Sep 17 00:00:00 2001 From: Brad Bell Date: Mon, 31 Jan 2022 08:49:55 -0700 Subject: [PATCH] Patch to_string.cpp to fix bug found using -fsanitize=undefined' complier flag --- cppad.spec | 148 ++++++++--------------------------------------------- 1 file changed, 20 insertions(+), 128 deletions(-) diff --git a/cppad.spec b/cppad.spec index 11a29e9..c8abdd8 100644 --- a/cppad.spec +++ b/cppad.spec @@ -96,135 +96,26 @@ sed -i.bak CppAD-%{version}/cmake/set_compile_flags.cmake \ sed -i.bak CppAD-%{version}/test_more/debug_rel/CMakeLists.txt \ -e 's|-DCPPAD_DEBUG_AND_RELEASE||' # ---------------------------------------------------------------------------- -# Temporarly replace to_string.cpp in order to debug ppc64le arch problem -cat << EOF > CppAD-%{version}/example/utility/to_string.cpp -// Examples with fundamental types -# include -namespace { - template - void print_error( - const char* routine_name, - const char* value_name, - Type value, - Type check, - Type eps = Type(0) ) - { std::cout << routine_name << " error : "; - std::cout << value_name << " - check = " << value - check; - std::cout << ", eps = " << eps << "\n"; - return; - } - template - Integer string2signed(const std::string& s) - { Integer result = 0; - size_t index = 0; - if( s[0] == '-' ) - ++index; - while( index < s.size() ) - result = Integer( 10 * result + (s[index++] - '0') ); - if( s[0] == '-' ) - return - result; - return result; - } - template - Integer string2unsigned(const std::string& s) - { Integer result = 0; - size_t index = 0; - while( index < s.size() ) - result = Integer(10 * result + Integer(s[index++]) - '0'); - return result; - } - template - bool signed_integer(void) - { bool ok = true; - // - Integer max = std::numeric_limits::max(); - std::string s = CppAD::to_string(max); - Integer check = string2signed(s); - ok &= max == check; - if( max != check ) - print_error("signed_integer", "max", max, check); - // - Integer min = std::numeric_limits::min(); - s = CppAD::to_string(min); - check = string2signed(s); - ok &= min == check; - if( min != check ) - print_error("signed_integer", "min", min, check); - // - return ok; - } - template - bool unsigned_integer(void) - { bool ok = true; - // - Integer max = std::numeric_limits::max(); - std::string s = CppAD::to_string(max); - Integer check = string2unsigned(s); - ok &= max == check; - if( max != check ) - print_error("unsigned_integer", "max", max, check); - ok &= std::numeric_limits::min() == 0; - if( std::numeric_limits::min() != 0 ) - std::cout << "unsigned_integer: min != 0 \n"; - // - return ok; - } - template - bool floating(void) - { bool ok = true; - Float eps = std::numeric_limits::epsilon(); - Float pi = Float( 4.0 * std::atan(1.0) ); - // - std::string s = CppAD::to_string( pi ); - Float check = Float( std::atof( s.c_str() ) ); - ok &= std::fabs( check / pi - 1.0 ) <= Float(2.0) * eps; - if( ! ok ) - print_error("floating", "pi", pi, check, eps); - // - return ok; - } -} -// Examples with AD types -# include -namespace { - template - bool ad_floating(void) - { bool ok = true; - Base eps = std::numeric_limits::epsilon(); - Base pi = Base( 4.0 * std::atan(1.0) ); - // - std::string s = CppAD::to_string( CppAD::AD( pi ) ); - Base check = Base( std::atof( s.c_str() ) ); - ok &= fabs( check / pi - Base(1.0) ) <= Base( 2.0 ) * eps; - if( ! ok ) - print_error("ad_floating", "pi", pi, check, eps); - // - return ok; - } -} -// Test driver -bool to_string(void) -{ bool ok = true; - - ok &= unsigned_integer(); - ok &= signed_integer(); - // - ok &= unsigned_integer(); - ok &= signed_integer(); - ok &= unsigned_integer(); - ok &= signed_integer(); - // - ok &= floating(); - ok &= floating(); - ok &= floating(); - // - ok &= ad_floating(); - ok &= ad_floating(); - // - return ok; -} +# Patch to fix example/utility/to_string.cpp +# This will no longer be necessary once the upstread source > 20220000.1 +cat << EOF > to_string.patch +32,36c32,39 +< ++index; +< while( index < s.size() ) +< result = Integer( 10 * result + (s[index++] - '0') ); +< if( s[0] == '-' ) +< return - result; +--- +> { ++index; +> while( index < s.size() ) +> result = Integer(10) * result - Integer(s[index++] - '0' ); +> } +> else +> { while( index < s.size() ) +> result = Integer(10) * result + Integer(s[index++] - '0' ); +> } EOF -# +patch CppAD-%{version}/example/utility/to_string.cpp to_string.patch # ----------------------------------------------------------------------------- # build # ----------------------------------------------------------------------------- @@ -318,6 +209,7 @@ make %{?_smp_mflags} check - 1. cppad_test_vector -> cppad_testvector - 2. cppad_max_num_theads -> cppad_max_num_threads - Temporary change to debug ppc64le build failure: replace to_string.cpp +- Replace temporary change by a patch to example/utility/to_string.cpp * Mon Jan 24 2022 Brad Bell - 20220000.1-1 - Advance to upstream source 20220000.1