1 /* 2 * Copyright 2008 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "src/base/SkMathPriv.h" 9 10 #include "include/private/base/SkAssert.h" 11 #include "include/private/base/SkFloatingPoint.h" 12 13 #include <cstddef> 14 #include <cstdint> 15 16 /////////////////////////////////////////////////////////////////////////////// 17 18 /* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf 19 */ SkSqrtBits(int32_t x,int count)20int32_t SkSqrtBits(int32_t x, int count) { 21 SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30); 22 23 uint32_t root = 0; 24 uint32_t remHi = 0; 25 uint32_t remLo = x; 26 27 do { 28 root <<= 1; 29 30 remHi = (remHi<<2) | (remLo>>30); 31 remLo <<= 2; 32 33 uint32_t testDiv = (root << 1) + 1; 34 if (remHi >= testDiv) { 35 remHi -= testDiv; 36 root++; 37 } 38 } while (--count >= 0); 39 40 return root; 41 } 42 43 // Kernighan's method SkPopCount_portable(uint32_t n)44int SkPopCount_portable(uint32_t n) { 45 int count = 0; 46 47 while (n) { 48 n &= (n - 1); // Remove the lowest bit in the integer. 49 count++; 50 } 51 return count; 52 } 53 54 // Here we strip off the unwanted bits and then return the number of trailing zero bits SkNthSet(uint32_t target,int n)55int SkNthSet(uint32_t target, int n) { 56 SkASSERT(n < SkPopCount(target)); 57 58 for (int i = 0; i < n; ++i) { 59 target &= (target - 1); // Remove the lowest bit in the integer. 60 } 61 62 return SkCTZ(target); 63 } 64 65 /////////////////////////////////////////////////////////////////////////////////////////////////// 66 sk_floats_are_unit(const float array[],size_t count)67bool sk_floats_are_unit(const float array[], size_t count) { 68 bool is_unit = true; 69 for (size_t i = 0; i < count; ++i) { 70 is_unit &= (array[i] >= 0) & (array[i] <= 1); 71 } 72 return is_unit; 73 } 74