Path: blob/master/dep/vixl/src/compiler-intrinsics-vixl.cc
4253 views
// Copyright 2015, VIXL authors1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are met:5//6// * Redistributions of source code must retain the above copyright notice,7// this list of conditions and the following disclaimer.8// * Redistributions in binary form must reproduce the above copyright notice,9// this list of conditions and the following disclaimer in the documentation10// and/or other materials provided with the distribution.11// * Neither the name of ARM Limited nor the names of its contributors may be12// used to endorse or promote products derived from this software without13// specific prior written permission.14//15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE19// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR21// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER22// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,23// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE24// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2526#include "compiler-intrinsics-vixl.h"2728#include "utils-vixl.h"2930namespace vixl {313233int CountLeadingSignBitsFallBack(int64_t value, int width) {34VIXL_ASSERT(IsPowerOf2(width) && (width <= 64));35if (width < 64) VIXL_ASSERT(IsIntN(width, value));36if (value >= 0) {37return CountLeadingZeros(value, width) - 1;38} else {39return CountLeadingZeros(~value, width) - 1;40}41}424344int CountLeadingZerosFallBack(uint64_t value, int width) {45VIXL_ASSERT(IsPowerOf2(width) && (width <= 64));46if (value == 0) {47return width;48}49int count = 0;50value = value << (64 - width);51if ((value & UINT64_C(0xffffffff00000000)) == 0) {52count += 32;53value = value << 32;54}55if ((value & UINT64_C(0xffff000000000000)) == 0) {56count += 16;57value = value << 16;58}59if ((value & UINT64_C(0xff00000000000000)) == 0) {60count += 8;61value = value << 8;62}63if ((value & UINT64_C(0xf000000000000000)) == 0) {64count += 4;65value = value << 4;66}67if ((value & UINT64_C(0xc000000000000000)) == 0) {68count += 2;69value = value << 2;70}71if ((value & UINT64_C(0x8000000000000000)) == 0) {72count += 1;73}74count += (value == 0);75return count;76}777879int CountSetBitsFallBack(uint64_t value, int width) {80VIXL_ASSERT(IsPowerOf2(width) && (width <= 64));8182// Mask out unused bits to ensure that they are not counted.83value &= (UINT64_C(0xffffffffffffffff) >> (64 - width));8485// Add up the set bits.86// The algorithm works by adding pairs of bit fields together iteratively,87// where the size of each bit field doubles each time.88// An example for an 8-bit value:89// Bits: h g f e d c b a90// \ | \ | \ | \ |91// value = h+g f+e d+c b+a92// \ | \ |93// value = h+g+f+e d+c+b+a94// \ |95// value = h+g+f+e+d+c+b+a96const uint64_t kMasks[] = {97UINT64_C(0x5555555555555555),98UINT64_C(0x3333333333333333),99UINT64_C(0x0f0f0f0f0f0f0f0f),100UINT64_C(0x00ff00ff00ff00ff),101UINT64_C(0x0000ffff0000ffff),102UINT64_C(0x00000000ffffffff),103};104105for (unsigned i = 0; i < (sizeof(kMasks) / sizeof(kMasks[0])); i++) {106int shift = 1 << i;107value = ((value >> shift) & kMasks[i]) + (value & kMasks[i]);108}109110return static_cast<int>(value);111}112113114int CountTrailingZerosFallBack(uint64_t value, int width) {115VIXL_ASSERT(IsPowerOf2(width) && (width <= 64));116int count = 0;117value = value << (64 - width);118if ((value & UINT64_C(0xffffffff)) == 0) {119count += 32;120value = value >> 32;121}122if ((value & 0xffff) == 0) {123count += 16;124value = value >> 16;125}126if ((value & 0xff) == 0) {127count += 8;128value = value >> 8;129}130if ((value & 0xf) == 0) {131count += 4;132value = value >> 4;133}134if ((value & 0x3) == 0) {135count += 2;136value = value >> 2;137}138if ((value & 0x1) == 0) {139count += 1;140}141count += (value == 0);142return count - (64 - width);143}144145146} // namespace vixl147148149