Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace custom gcd function with std #2330

Merged
merged 1 commit into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions include/exiv2/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,45 +506,6 @@ T stringTo(const std::string& s, bool& ok) {
template <>
bool stringTo<bool>(const std::string& s, bool& ok);

/*!
@brief Return the greatest common denominator of n and m.
(Implementation from Boost rational.hpp)

@note We use n and m as temporaries in this function, so there is no
value in using const IntType& as we would only need to make a copy
anyway...
*/
template <typename IntType>
IntType gcd(IntType n, IntType m) {
// Avoid repeated construction
IntType zero(0);

// This is abs() - given the existence of broken compilers with Koenig
// lookup issues and other problems, I code this explicitly. (Remember,
// IntType may be a user-defined type).
if (n < zero) {
if (n == std::numeric_limits<IntType>::min()) {
n = std::numeric_limits<IntType>::max();
} else {
n = -n;
}
}
if (m < zero)
m = -m;

// As n and m are now positive, we can be sure that %= returns a
// positive value (the standard guarantees this for built-in types,
// and we require it of user-defined types).
for (;;) {
if (m == zero)
return n;
n %= m;
if (n == zero)
return m;
m %= n;
}
}

} // namespace Exiv2

#endif // #ifndef TYPES_HPP_
3 changes: 2 additions & 1 deletion src/tags_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <algorithm>
#include <cmath>
#include <numeric>

// *****************************************************************************
// local declarations
Expand Down Expand Up @@ -2807,7 +2808,7 @@ std::ostream& print0x9204(std::ostream& os, const Value& value, const ExifData*)
} else if (bias.second <= 0) {
os << "(" << bias.first << "/" << bias.second << ")";
} else {
int32_t d = gcd(bias.first, bias.second);
int32_t d = std::gcd(bias.first, bias.second);
int32_t num = std::abs(bias.first) / d;
int32_t den = bias.second / d;
os << (bias.first < 0 ? "-" : "+") << num;
Expand Down
3 changes: 2 additions & 1 deletion src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cmath>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <utility>

Expand Down Expand Up @@ -629,7 +630,7 @@ Rational floatToRationalCast(float f) {
den = 10000;
}
const auto nom = static_cast<int32_t>(std::round(d * den));
const int32_t g = gcd(nom, den);
const int32_t g = std::gcd(nom, den);

return {nom / g, den / g};
}
Expand Down