michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "jit/BitSet.h" michael@0: michael@0: using namespace js; michael@0: using namespace js::jit; michael@0: michael@0: BitSet * michael@0: BitSet::New(TempAllocator &alloc, unsigned int numBits) michael@0: { michael@0: BitSet *result = new(alloc) BitSet(numBits); michael@0: if (!result->init(alloc)) michael@0: return nullptr; michael@0: return result; michael@0: } michael@0: michael@0: bool michael@0: BitSet::init(TempAllocator &alloc) michael@0: { michael@0: size_t sizeRequired = numWords() * sizeof(*bits_); michael@0: michael@0: bits_ = (uint32_t *)alloc.allocate(sizeRequired); michael@0: if (!bits_) michael@0: return false; michael@0: michael@0: memset(bits_, 0, sizeRequired); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: BitSet::empty() const michael@0: { michael@0: JS_ASSERT(bits_); michael@0: const uint32_t *bits = bits_; michael@0: for (unsigned int i = 0, e = numWords(); i < e; i++) { michael@0: if (bits[i]) michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: BitSet::insertAll(const BitSet *other) michael@0: { michael@0: JS_ASSERT(bits_); michael@0: JS_ASSERT(other->numBits_ == numBits_); michael@0: JS_ASSERT(other->bits_); michael@0: michael@0: uint32_t *bits = bits_; michael@0: const uint32_t *otherBits = other->bits_; michael@0: for (unsigned int i = 0, e = numWords(); i < e; i++) michael@0: bits[i] |= otherBits[i]; michael@0: } michael@0: michael@0: void michael@0: BitSet::removeAll(const BitSet *other) michael@0: { michael@0: JS_ASSERT(bits_); michael@0: JS_ASSERT(other->numBits_ == numBits_); michael@0: JS_ASSERT(other->bits_); michael@0: michael@0: uint32_t *bits = bits_; michael@0: const uint32_t *otherBits = other->bits_; michael@0: for (unsigned int i = 0, e = numWords(); i < e; i++) michael@0: bits[i] &= ~otherBits[i]; michael@0: } michael@0: michael@0: void michael@0: BitSet::intersect(const BitSet *other) michael@0: { michael@0: JS_ASSERT(bits_); michael@0: JS_ASSERT(other->numBits_ == numBits_); michael@0: JS_ASSERT(other->bits_); michael@0: michael@0: uint32_t *bits = bits_; michael@0: const uint32_t *otherBits = other->bits_; michael@0: for (unsigned int i = 0, e = numWords(); i < e; i++) michael@0: bits[i] &= otherBits[i]; michael@0: } michael@0: michael@0: // returns true if the intersection caused the contents of the set to change. michael@0: bool michael@0: BitSet::fixedPointIntersect(const BitSet *other) michael@0: { michael@0: JS_ASSERT(bits_); michael@0: JS_ASSERT(other->numBits_ == numBits_); michael@0: JS_ASSERT(other->bits_); michael@0: michael@0: bool changed = false; michael@0: michael@0: uint32_t *bits = bits_; michael@0: const uint32_t *otherBits = other->bits_; michael@0: for (unsigned int i = 0, e = numWords(); i < e; i++) { michael@0: uint32_t old = bits[i]; michael@0: bits[i] &= otherBits[i]; michael@0: michael@0: if (!changed && old != bits[i]) michael@0: changed = true; michael@0: } michael@0: return changed; michael@0: } michael@0: michael@0: void michael@0: BitSet::complement() michael@0: { michael@0: JS_ASSERT(bits_); michael@0: uint32_t *bits = bits_; michael@0: for (unsigned int i = 0, e = numWords(); i < e; i++) michael@0: bits[i] = ~bits[i]; michael@0: } michael@0: michael@0: void michael@0: BitSet::clear() michael@0: { michael@0: JS_ASSERT(bits_); michael@0: uint32_t *bits = bits_; michael@0: for (unsigned int i = 0, e = numWords(); i < e; i++) michael@0: bits[i] = 0; michael@0: }