Skip to content

Commit

Permalink
Bug 1299284 - Remove js::IsPowerOfTwo in favor of mozilla::IsPowerOfT…
Browse files Browse the repository at this point in the history
…wo. r=sfink
  • Loading branch information
jswalden committed Jul 21, 2016
1 parent 157f772 commit 9766a60
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 54 deletions.
1 change: 1 addition & 0 deletions js/src/asmjs/AsmJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ using mozilla::Compression::LZ4;
using mozilla::HashGeneric;
using mozilla::IsNaN;
using mozilla::IsNegativeZero;
using mozilla::IsPowerOfTwo;
using mozilla::Maybe;
using mozilla::Move;
using mozilla::PodCopy;
Expand Down
12 changes: 8 additions & 4 deletions js/src/asmjs/WasmBaselineCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
*/

#include "asmjs/WasmBaselineCompile.h"

#include "mozilla/MathAlgorithms.h"

#include "asmjs/WasmBinaryIterator.h"
#include "asmjs/WasmGenerator.h"
#include "asmjs/WasmSignalHandlers.h"
Expand All @@ -114,6 +117,7 @@

using mozilla::DebugOnly;
using mozilla::FloatingPoint;
using mozilla::IsPowerOfTwo;
using mozilla::SpecificNaN;

namespace js {
Expand Down Expand Up @@ -625,9 +629,9 @@ class BaseCompiler

int32_t pushLocal(size_t nbytes) {
if (nbytes == 8)
localSize_ = AlignBytes(localSize_, 8);
localSize_ = AlignBytes(localSize_, 8u);
else if (nbytes == 16)
localSize_ = AlignBytes(localSize_, 16);
localSize_ = AlignBytes(localSize_, 16u);
localSize_ += nbytes;
return localSize_; // Locals grow down so capture base address
}
Expand Down Expand Up @@ -2073,7 +2077,7 @@ class BaseCompiler
ABIArgIter<const ValTypeVector> i(args);
while (!i.done())
i++;
return AlignBytes(i.stackBytesConsumedSoFar(), 16);
return AlignBytes(i.stackBytesConsumedSoFar(), 16u);
}

void startCallArgs(FunctionCall& call, size_t stackArgAreaSize)
Expand Down Expand Up @@ -7112,7 +7116,7 @@ BaseCompiler::init()

varHigh_ = localSize_;

localSize_ = AlignBytes(localSize_, 16);
localSize_ = AlignBytes(localSize_, 16u);

addInterruptCheck();

Expand Down
3 changes: 3 additions & 0 deletions js/src/asmjs/WasmIonCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "asmjs/WasmIonCompile.h"

#include "mozilla/MathAlgorithms.h"

#include "asmjs/WasmBaselineCompile.h"
#include "asmjs/WasmBinaryIterator.h"
#include "asmjs/WasmGenerator.h"
Expand All @@ -30,6 +32,7 @@ using namespace js::jit;
using namespace js::wasm;

using mozilla::DebugOnly;
using mozilla::IsPowerOfTwo;
using mozilla::Maybe;
using mozilla::Nothing;
using mozilla::Some;
Expand Down
1 change: 1 addition & 0 deletions js/src/asmjs/WasmTextToBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ using mozilla::CeilingLog2;
using mozilla::CountLeadingZeroes32;
using mozilla::CheckedInt;
using mozilla::FloatingPoint;
using mozilla::IsPowerOfTwo;
using mozilla::Maybe;
using mozilla::PositiveInfinity;
using mozilla::SpecificNaN;
Expand Down
3 changes: 3 additions & 0 deletions js/src/asmjs/WasmTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "asmjs/WasmTypes.h"

#include "mozilla/MathAlgorithms.h"

#include "fdlibm.h"

#include "jslibmath.h"
Expand All @@ -37,6 +39,7 @@ using namespace js::jit;
using namespace js::wasm;

using mozilla::IsNaN;
using mozilla::IsPowerOfTwo;

void
Val::writePayload(uint8_t* dst) const
Expand Down
1 change: 1 addition & 0 deletions js/src/builtin/TypedObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using mozilla::AssertedCast;
using mozilla::CheckedInt32;
using mozilla::DebugOnly;
using mozilla::IsPowerOfTwo;
using mozilla::PodCopy;
using mozilla::PointerRangeSize;

Expand Down
2 changes: 1 addition & 1 deletion js/src/jit/Ion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ IonScript::New(JSContext* cx, RecompileInfo recompileInfo,
size_t backedgeEntries, size_t sharedStubEntries,
OptimizationLevel optimizationLevel)
{
static const int DataAlignment = sizeof(void*);
constexpr size_t DataAlignment = sizeof(void*);

if (snapshotsListSize >= MAX_BUFFER_SIZE ||
(bailoutEntries >= MAX_BUFFER_SIZE / sizeof(uint32_t)))
Expand Down
3 changes: 2 additions & 1 deletion js/src/jit/MIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ using mozilla::CheckedInt;
using mozilla::NumbersAreIdentical;
using mozilla::IsFloat32Representable;
using mozilla::IsNaN;
using mozilla::IsPowerOfTwo;
using mozilla::Maybe;
using mozilla::DebugOnly;

Expand Down Expand Up @@ -3390,7 +3391,7 @@ MMod::analyzeEdgeCasesForward()

if (rhs()->isConstant()) {
int32_t n = rhs()->toConstant()->toInt32();
if (n > 0 && !IsPowerOfTwo(n))
if (n > 0 && !IsPowerOfTwo(uint32_t(n)))
canBePowerOfTwoDivisor_ = false;
}
}
Expand Down
4 changes: 3 additions & 1 deletion js/src/jit/MacroAssembler-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "jit/MacroAssembler.h"

#include "mozilla/MathAlgorithms.h"

#if defined(JS_CODEGEN_X86)
# include "jit/x86/MacroAssembler-x86-inl.h"
#elif defined(JS_CODEGEN_X64)
Expand Down Expand Up @@ -732,7 +734,7 @@ MacroAssembler::assertStackAlignment(uint32_t alignment, int32_t offset /* = 0 *
{
#ifdef DEBUG
Label ok, bad;
MOZ_ASSERT(IsPowerOfTwo(alignment));
MOZ_ASSERT(mozilla::IsPowerOfTwo(alignment));

// Wrap around the offset to be a non-negative number.
offset %= alignment;
Expand Down
5 changes: 4 additions & 1 deletion js/src/jit/x64/Trampoline-x64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using namespace js;
using namespace js::jit;

using mozilla::IsPowerOfTwo;

// All registers to save and restore. This includes the stack pointer, since we
// use the ability to reference register values on the stack by index.
static const LiveRegisterSet AllRegs =
Expand Down Expand Up @@ -431,8 +433,9 @@ JitRuntime::generateArgumentsRectifier(JSContext* cx, void** returnAddrOut)
"No need to consider the JitFrameLayout for aligning the stack");
static_assert(JitStackAlignment % sizeof(Value) == 0,
"Ensure that we can pad the stack by pushing extra UndefinedValue");
static_assert(IsPowerOfTwo(JitStackValueAlignment),
"must have power of two for masm.andl to do its job");

MOZ_ASSERT(IsPowerOfTwo(JitStackValueAlignment));
masm.addl(Imm32(JitStackValueAlignment - 1 /* for padding */ + 1 /* for |this| */), rcx);
masm.addl(rdx, rcx);
masm.andl(Imm32(~(JitStackValueAlignment - 1)), rcx);
Expand Down
7 changes: 6 additions & 1 deletion js/src/jit/x86/Trampoline-x86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* 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/. */

#include "mozilla/MathAlgorithms.h"

#include "jscompartment.h"

#include "jit/Bailouts.h"
Expand All @@ -22,6 +24,8 @@

#include "jit/MacroAssembler-inl.h"

using mozilla::IsPowerOfTwo;

using namespace js;
using namespace js::jit;

Expand Down Expand Up @@ -413,8 +417,9 @@ JitRuntime::generateArgumentsRectifier(JSContext* cx, void** returnAddrOut)
"No need to consider |this| and the frame pointer and its padding for aligning the stack");
static_assert(JitStackAlignment % sizeof(Value) == 0,
"Ensure that we can pad the stack by pushing extra UndefinedValue");
static_assert(IsPowerOfTwo(JitStackValueAlignment),
"must have power of two for masm.andl to do its job");

MOZ_ASSERT(IsPowerOfTwo(JitStackValueAlignment));
masm.addl(Imm32(JitStackValueAlignment - 1 /* for padding */), ecx);

// Account for newTarget, if necessary.
Expand Down
22 changes: 8 additions & 14 deletions js/src/jsutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "mozilla/Compiler.h"
#include "mozilla/GuardObjects.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/PodOperations.h"

#include <limits.h>
Expand Down Expand Up @@ -207,32 +208,25 @@ class MOZ_RAII AutoScopedAssign
T old;
};

template <typename T>
static inline bool
IsPowerOfTwo(T t)
{
return t && !(t & (t - 1));
}

template <typename T, typename U>
static inline U
ComputeByteAlignment(T bytes, U alignment)
{
MOZ_ASSERT(IsPowerOfTwo(alignment));
static_assert(mozilla::IsUnsigned<U>::value,
"alignment amount must be unsigned");

MOZ_ASSERT(mozilla::IsPowerOfTwo(alignment));
return (alignment - (bytes % alignment)) % alignment;
}

template <typename T, typename U>
static inline T
AlignBytes(T bytes, U alignment)
{
return bytes + ComputeByteAlignment(bytes, alignment);
}
static_assert(mozilla::IsUnsigned<U>::value,
"alignment amount must be unsigned");

static MOZ_ALWAYS_INLINE size_t
UnsignedPtrDiff(const void* bigger, const void* smaller)
{
return size_t(bigger) - size_t(smaller);
return bytes + ComputeByteAlignment(bytes, alignment);
}

/*****************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion mfbt/MathAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ RotateRight(const T aValue, uint_fast8_t aShift)
* Zero is not an integer power of two. (-Inf is not an integer)
*/
template<typename T>
inline bool
constexpr bool
IsPowerOfTwo(T x)
{
static_assert(IsUnsigned<T>::value,
Expand Down
60 changes: 30 additions & 30 deletions mfbt/tests/TestMathAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,40 @@ TestClamp()
static void
TestIsPowerOfTwo()
{
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(0u));
MOZ_RELEASE_ASSERT( IsPowerOfTwo(1u));
MOZ_RELEASE_ASSERT( IsPowerOfTwo(2u));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(3u));
MOZ_RELEASE_ASSERT( IsPowerOfTwo(4u));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(5u));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(6u));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(7u));
MOZ_RELEASE_ASSERT( IsPowerOfTwo(8u));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(9u));
static_assert(!IsPowerOfTwo(0u), "0 isn't a power of two");
static_assert(IsPowerOfTwo(1u), "1 is a power of two");
static_assert(IsPowerOfTwo(2u), "2 is a power of two");
static_assert(!IsPowerOfTwo(3u), "3 isn't a power of two");
static_assert(IsPowerOfTwo(4u), "4 is a power of two");
static_assert(!IsPowerOfTwo(5u), "5 isn't a power of two");
static_assert(!IsPowerOfTwo(6u), "6 isn't a power of two");
static_assert(!IsPowerOfTwo(7u), "7 isn't a power of two");
static_assert(IsPowerOfTwo(8u), "8 is a power of two");
static_assert(!IsPowerOfTwo(9u), "9 isn't a power of two");

MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint8_t(UINT8_MAX/2))); // 127, 0x7f
MOZ_RELEASE_ASSERT( IsPowerOfTwo(uint8_t(UINT8_MAX/2 + 1))); // 128, 0x80
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint8_t(UINT8_MAX/2 + 2))); // 129, 0x81
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint8_t(UINT8_MAX - 1))); // 254, 0xfe
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint8_t(UINT8_MAX))); // 255, 0xff
static_assert(!IsPowerOfTwo(uint8_t(UINT8_MAX/2)), "127, 0x7f isn't a power of two");
static_assert(IsPowerOfTwo(uint8_t(UINT8_MAX/2 + 1)), "128, 0x80 is a power of two");
static_assert(!IsPowerOfTwo(uint8_t(UINT8_MAX/2 + 2)), "129, 0x81 isn't a power of two");
static_assert(!IsPowerOfTwo(uint8_t(UINT8_MAX - 1)), "254, 0xfe isn't a power of two");
static_assert(!IsPowerOfTwo(uint8_t(UINT8_MAX)), "255, 0xff isn't a power of two");

MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint16_t(UINT16_MAX/2))); // 0x7fff
MOZ_RELEASE_ASSERT( IsPowerOfTwo(uint16_t(UINT16_MAX/2 + 1))); // 0x8000
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint16_t(UINT16_MAX/2 + 2))); // 0x8001
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint16_t(UINT16_MAX - 1))); // 0xfffe
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint16_t(UINT16_MAX))); // 0xffff
static_assert(!IsPowerOfTwo(uint16_t(UINT16_MAX/2)), "0x7fff isn't a power of two");
static_assert(IsPowerOfTwo(uint16_t(UINT16_MAX/2 + 1)), "0x8000 is a power of two");
static_assert(!IsPowerOfTwo(uint16_t(UINT16_MAX/2 + 2)), "0x8001 isn't a power of two");
static_assert(!IsPowerOfTwo(uint16_t(UINT16_MAX - 1)), "0xfffe isn't a power of two");
static_assert(!IsPowerOfTwo(uint16_t(UINT16_MAX)), "0xffff isn't a power of two");

MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint32_t(UINT32_MAX/2)));
MOZ_RELEASE_ASSERT( IsPowerOfTwo(uint32_t(UINT32_MAX/2 + 1)));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint32_t(UINT32_MAX/2 + 2)));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint32_t(UINT32_MAX - 1)));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint32_t(UINT32_MAX)));
static_assert(!IsPowerOfTwo(uint32_t(UINT32_MAX/2)), "0x7fffffff isn't a power of two");
static_assert(IsPowerOfTwo(uint32_t(UINT32_MAX/2 + 1)), "0x80000000 is a power of two");
static_assert(!IsPowerOfTwo(uint32_t(UINT32_MAX/2 + 2)), "0x80000001 isn't a power of two");
static_assert(!IsPowerOfTwo(uint32_t(UINT32_MAX - 1)), "0xfffffffe isn't a power of two");
static_assert(!IsPowerOfTwo(uint32_t(UINT32_MAX)), "0xffffffff isn't a power of two");

MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint64_t(UINT64_MAX/2)));
MOZ_RELEASE_ASSERT( IsPowerOfTwo(uint64_t(UINT64_MAX/2 + 1)));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint64_t(UINT64_MAX/2 + 2)));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint64_t(UINT64_MAX - 1)));
MOZ_RELEASE_ASSERT(!IsPowerOfTwo(uint64_t(UINT64_MAX)));
static_assert(!IsPowerOfTwo(uint64_t(UINT64_MAX/2)), "0x7fffffffffffffff isn't a power of two");
static_assert(IsPowerOfTwo(uint64_t(UINT64_MAX/2 + 1)), "0x8000000000000000 is a power of two");
static_assert(!IsPowerOfTwo(uint64_t(UINT64_MAX/2 + 2)), "0x8000000000000001 isn't a power of two");
static_assert(!IsPowerOfTwo(uint64_t(UINT64_MAX - 1)), "0xfffffffffffffffe isn't a power of two");
static_assert(!IsPowerOfTwo(uint64_t(UINT64_MAX)), "0xffffffffffffffff isn't a power of two");
}

int
Expand Down

0 comments on commit 9766a60

Please sign in to comment.