Skip to content

Conversation

Maratyszcza
Copy link
Contributor

GNU STL in Android NDK lacks string conversion functions from C++11, including std::stoul, std::stoi, and std::stod. This patch replaces calls to these functions with C-style equivalents from C++03.

@coveralls
Copy link

coveralls commented May 15, 2018

Coverage Status

Coverage increased (+0.01%) to 87.172% when pulling b9b7b87 on Maratyszcza:fix-android-gnustl into 4c2af07 on google:master.

@AppVeyorBot
Copy link

@dmah42
Copy link
Member

dmah42 commented May 15, 2018

we use c++11 throughout benchmark. perhaps it's worth adding a platform check for this, if it's unique to Android?

@Maratyszcza
Copy link
Contributor Author

I think it is Android-specific problem: its GNU STL library mostly, but not fully C++11-compliant, and I haven't seen similar issues on other platforms. That said, Android is a non-negligible platform.

@LebedevRI
Copy link
Collaborator

I think it may be interesting to define which versions Android suffer from this issue.
Is it the last one? Is it the first one? All of them?

@Maratyszcza
Copy link
Contributor Author

Android STL lives in Android NDK, apps include pre-compiled STL along with their own code. The problem fixed by this patch is present since ancient times (since C++ got support in Android NDK) and up to and including Android NDK r17.

@dmah42
Copy link
Member

dmah42 commented May 15, 2018

there are some semantic differences between the old and new methods. The newer std:: versions will throw exceptions on error, while the old versions will return 0 (or 0.0) and set errno. Strictly speaking, if we're going to use those we should check errno just in case something goes wrong reading the system info.

I think it may be worth wrapping them in a method that uses #ifdef BENCHMARK_OS_ANDROID to call the new or old versions, and check the errno correctly.

@Maratyszcza
Copy link
Contributor Author

@dominichamon Please take another look

@AppVeyorBot
Copy link

Build benchmark 1230 failed (commit c60a676d19 by @Maratyszcza)

@AppVeyorBot
Copy link

return ss.str();
}

#if defined(__ANDROID__) && defined(__GLIBCXX__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you define a BENCHMARK_OS_ANDROID where we define the other OS constants (src/internal_macros.h)?

i'm not sure we currently differentiate on libcxx but maybe we should add something there too.

* strtol, strtod. Note that reimplemented functions are in benchmark::
* namespace, not std:: namespace.
*/
inline unsigned long stoul(const std::string& str, size_t* pos = nullptr,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why inline and why in the header?

@Maratyszcza Maratyszcza force-pushed the fix-android-gnustl branch from 3bc3c22 to 92acab0 Compare May 17, 2018 17:36
@Maratyszcza
Copy link
Contributor Author

@dominichamon Added BENCHMARK_STL_ANDROID_GNUSTL macro, and moved implementation into .cc file

@AppVeyorBot
Copy link

}
}

#ifdef BENCHMARK_STL_ANDROID_GNUSTL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add tests for these please?

you'll have to create a test/string_util_gtest.cc file, and add it to test/CMakeLists.txt. bazel will pick it up automatically.

Copy link
Contributor Author

@Maratyszcza Maratyszcza Jun 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests and replaced log2(n) with 1.44269504088896340736 * log(n) as Android GNUSTL lacks that too.

@Maratyszcza Maratyszcza force-pushed the fix-android-gnustl branch from 92acab0 to cac8645 Compare June 3, 2018 03:38
GNU STL in Android NDK lacks string conversion functions from C++11, including std::stoul, std::stoi, and std::stod.
This patch reimplements these functions in benchmark:: namespace using C-style equivalents from C++03.
@Maratyszcza Maratyszcza force-pushed the fix-android-gnustl branch from cac8645 to 526500c Compare June 3, 2018 03:52
@AppVeyorBot
Copy link

case oLogN:
return [](int64_t n) { return log2(n); };
/* Note: can't use log2 because Android's GNU STL lacks it */
return [](int64_t n) { return 1.44269504088896340736 * log(n); };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you at least add a kLog2E constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Maratyszcza Maratyszcza force-pushed the fix-android-gnustl branch 2 times, most recently from 9d64806 to 23c7137 Compare June 4, 2018 17:00
@AppVeyorBot
Copy link

Build benchmark 1270 failed (commit f7d2f8bcfa by @Maratyszcza)

GNU STL in Android NDK lacks log2 function from C99/C++11.
This patch replaces their use in the code with double log(double) function.
@Maratyszcza Maratyszcza force-pushed the fix-android-gnustl branch from 23c7137 to b9b7b87 Compare June 4, 2018 17:15
@AppVeyorBot
Copy link

@AppVeyorBot
Copy link

@dmah42 dmah42 merged commit 7fb3c56 into google:master Jun 5, 2018
@enh
Copy link
Contributor

enh commented Jul 6, 2018

(note that gnustl is being removed from NDK r18.)

JBakamovic pushed a commit to JBakamovic/benchmark that referenced this pull request Dec 6, 2018
* Fix compilation on Android with GNU STL

GNU STL in Android NDK lacks string conversion functions from C++11, including std::stoul, std::stoi, and std::stod.
This patch reimplements these functions in benchmark:: namespace using C-style equivalents from C++03.

* Avoid use of log2 which doesn't exist in Android GNU STL

GNU STL in Android NDK lacks log2 function from C99/C++11.
This patch replaces their use in the code with double log(double) function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants