mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-23 10:15:41 +00:00
Bug 1592992 - Part 7: Split bit-array functions from jsutil.h. r=jonco
Differential Revision: https://phabricator.services.mozilla.com/D51366 --HG-- rename : js/src/jsutil.h => js/src/util/BitArray.h extra : moz-landing-system : lando
This commit is contained in:
parent
0b2f713a4a
commit
5b0689fff8
@ -22,6 +22,7 @@
|
||||
#include "js/Initialization.h"
|
||||
#include "js/Utility.h"
|
||||
#include "js/Value.h"
|
||||
#include "util/BitArray.h"
|
||||
#include "util/Poison.h"
|
||||
|
||||
/* Crash diagnostics by default in debug and on nightly channel. */
|
||||
@ -88,56 +89,6 @@ static constexpr T AlignBytes(T bytes, U alignment) {
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
/* A bit array is an array of bits represented by an array of words (size_t). */
|
||||
|
||||
static const size_t BitArrayElementBits = sizeof(size_t) * CHAR_BIT;
|
||||
|
||||
static inline unsigned NumWordsForBitArrayOfLength(size_t length) {
|
||||
return (length + (BitArrayElementBits - 1)) / BitArrayElementBits;
|
||||
}
|
||||
|
||||
static inline unsigned BitArrayIndexToWordIndex(size_t length,
|
||||
size_t bitIndex) {
|
||||
unsigned wordIndex = bitIndex / BitArrayElementBits;
|
||||
MOZ_ASSERT(wordIndex < length);
|
||||
return wordIndex;
|
||||
}
|
||||
|
||||
static inline size_t BitArrayIndexToWordMask(size_t i) {
|
||||
return size_t(1) << (i % BitArrayElementBits);
|
||||
}
|
||||
|
||||
static inline bool IsBitArrayElementSet(const size_t* array, size_t length,
|
||||
size_t i) {
|
||||
return array[BitArrayIndexToWordIndex(length, i)] &
|
||||
BitArrayIndexToWordMask(i);
|
||||
}
|
||||
|
||||
static inline bool IsAnyBitArrayElementSet(const size_t* array, size_t length) {
|
||||
unsigned numWords = NumWordsForBitArrayOfLength(length);
|
||||
for (unsigned i = 0; i < numWords; ++i) {
|
||||
if (array[i]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline void SetBitArrayElement(size_t* array, size_t length, size_t i) {
|
||||
array[BitArrayIndexToWordIndex(length, i)] |= BitArrayIndexToWordMask(i);
|
||||
}
|
||||
|
||||
static inline void ClearBitArrayElement(size_t* array, size_t length,
|
||||
size_t i) {
|
||||
array[BitArrayIndexToWordIndex(length, i)] &= ~BitArrayIndexToWordMask(i);
|
||||
}
|
||||
|
||||
static inline void ClearAllBitArrayElements(size_t* array, size_t length) {
|
||||
for (unsigned i = 0; i < length; ++i) {
|
||||
array[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Placement-new elements of an array. This should optimize away for types with
|
||||
// trivial default initiation.
|
||||
template <typename T>
|
||||
|
71
js/src/util/BitArray.h
Normal file
71
js/src/util/BitArray.h
Normal file
@ -0,0 +1,71 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
||||
* 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/. */
|
||||
|
||||
/*
|
||||
* A bit array is an array of bits represented by an array of words (size_t).
|
||||
*/
|
||||
|
||||
#ifndef util_BitArray_h
|
||||
#define util_BitArray_h
|
||||
|
||||
#include "mozilla/Assertions.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
namespace js {
|
||||
|
||||
static const size_t BitArrayElementBits = sizeof(size_t) * CHAR_BIT;
|
||||
|
||||
static inline unsigned NumWordsForBitArrayOfLength(size_t length) {
|
||||
return (length + (BitArrayElementBits - 1)) / BitArrayElementBits;
|
||||
}
|
||||
|
||||
static inline unsigned BitArrayIndexToWordIndex(size_t length,
|
||||
size_t bitIndex) {
|
||||
unsigned wordIndex = bitIndex / BitArrayElementBits;
|
||||
MOZ_ASSERT(wordIndex < length);
|
||||
return wordIndex;
|
||||
}
|
||||
|
||||
static inline size_t BitArrayIndexToWordMask(size_t i) {
|
||||
return size_t(1) << (i % BitArrayElementBits);
|
||||
}
|
||||
|
||||
static inline bool IsBitArrayElementSet(const size_t* array, size_t length,
|
||||
size_t i) {
|
||||
return array[BitArrayIndexToWordIndex(length, i)] &
|
||||
BitArrayIndexToWordMask(i);
|
||||
}
|
||||
|
||||
static inline bool IsAnyBitArrayElementSet(const size_t* array, size_t length) {
|
||||
unsigned numWords = NumWordsForBitArrayOfLength(length);
|
||||
for (unsigned i = 0; i < numWords; ++i) {
|
||||
if (array[i]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline void SetBitArrayElement(size_t* array, size_t length, size_t i) {
|
||||
array[BitArrayIndexToWordIndex(length, i)] |= BitArrayIndexToWordMask(i);
|
||||
}
|
||||
|
||||
static inline void ClearBitArrayElement(size_t* array, size_t length,
|
||||
size_t i) {
|
||||
array[BitArrayIndexToWordIndex(length, i)] &= ~BitArrayIndexToWordMask(i);
|
||||
}
|
||||
|
||||
static inline void ClearAllBitArrayElements(size_t* array, size_t length) {
|
||||
for (unsigned i = 0; i < length; ++i) {
|
||||
array[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace js */
|
||||
|
||||
#endif /* util_BitArray_h */
|
Loading…
Reference in New Issue
Block a user