Bug 781313 - Move the euclidGcd and lcm algos form nsStyleAnimation.cpp to mfbt/MathAlgorithms.h r=dbaron,luke

This commit is contained in:
Raphael Catolino 2012-08-26 22:58:23 -03:00
parent 3bfa1737d4
commit 516fde7d59
3 changed files with 50 additions and 28 deletions

View File

@ -6,6 +6,7 @@
/* Utilities for animation of computed style values */
#include "mozilla/Util.h"
#include "mozilla/MathAlgorithms.h"
#include "nsStyleAnimation.h"
#include "nsCOMArray.h"
@ -242,33 +243,6 @@ ToPrimitive(nsCSSValue::Array* aArray)
return arr.forget();
}
// Greatest Common Divisor
static uint32_t
gcd(uint32_t a, uint32_t b)
{
// Euclid's algorithm; O(N) in the worst case. (There are better
// ways, but we don't need them for stroke-dasharray animation.)
NS_ABORT_IF_FALSE(a > 0 && b > 0, "positive numbers expected");
while (a != b) {
if (a > b) {
a = a - b;
} else {
b = b - a;
}
}
return a;
}
// Least Common Multiple
static uint32_t
lcm(uint32_t a, uint32_t b)
{
// Divide first to reduce overflow risk.
return (a / gcd(a, b)) * b;
}
inline void
nscoordToCSSValue(nscoord aCoord, nsCSSValue& aCSSValue)
{
@ -1953,7 +1927,7 @@ nsStyleAnimation::AddWeighted(nsCSSProperty aProperty,
nsAutoPtr<nsCSSValueList> result;
nsCSSValueList **resultTail = getter_Transfers(result);
for (uint32_t i = 0, i_end = lcm(len1, len2); i != i_end; ++i) {
for (uint32_t i = 0, i_end = EuclidLCM<uint32_t>(len1, len2); i != i_end; ++i) {
const nsCSSValue &v1 = list1->mValue;
const nsCSSValue &v2 = list2->mValue;
NS_ABORT_IF_FALSE(v1.GetUnit() == eCSSUnit_Number ||

47
mfbt/MathAlgorithms.h Normal file
View File

@ -0,0 +1,47 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* mfbt maths algorithms. */
#ifndef mozilla_MathAlgorithms_h_
#define mozilla_MathAlgorithms_h_
#include "mozilla/Assertions.h"
namespace mozilla {
// Greatest Common Divisor
template<typename IntegerType>
MOZ_ALWAYS_INLINE IntegerType
EuclidGCD(IntegerType a, IntegerType b)
{
// Euclid's algorithm; O(N) in the worst case. (There are better
// ways, but we don't need them for the current use of this algo.)
MOZ_ASSERT(a > 0);
MOZ_ASSERT(b > 0);
while (a != b) {
if (a > b) {
a = a - b;
} else {
b = b - a;
}
}
return a;
}
// Least Common Multiple
template<typename IntegerType>
MOZ_ALWAYS_INLINE IntegerType
EuclidLCM(IntegerType a, IntegerType b)
{
// Divide first to reduce overflow risk.
return (a / EuclidGCD(a, b)) * b;
}
} /* namespace mozilla */
#endif /* mozilla_MathAlgorithms_h_ */

View File

@ -19,6 +19,7 @@ EXPORTS_mozilla += \
HashFunctions.h \
Likely.h \
LinkedList.h \
MathAlgorithms.h \
MSStdInt.h \
RangedPtr.h \
RefPtr.h \