https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122101
--- Comment #3 from Arash Partow <arash at partow dot net> ---
@AndrewPinski - I think you're right. Removing the integer mod operation,
brings both in to the same performance range.
This is the modified version:
------ code ------
inline bool is_prime(std::uint32_t i)
{
if (i == 2) return true;
if ((i == 1) || (i % 2 == 0)) return false;
const auto f = static_cast<double>(i);
const auto c = std::ceil(std::sqrt(f));
for (double j = 3.0; j <= c; j += 2.0)
{
const double g = f / j;
if ((g - std::trunc(g)) == 0.0) return false;
}
return true;
}
------ code ------
Note: using float or double doesn't make a difference.