Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURES_BITSET_32_H_ | |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURES_BITSET_32_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 | |
| 10 namespace content { | |
| 11 | |
| 12 // Port of BitSet32 from Android | |
| 13 // * platform/system/core/include/utils/BitSet.h | |
| 14 // * Change-Id: I9bbf41f9d2d4a2593b0e6d7d8be7e283f985bade | |
| 15 // * Changes to this class should be cosmetic only, easing fork maintenance. | |
| 16 struct BitSet32 { | |
| 17 uint32_t value; | |
| 18 | |
| 19 inline BitSet32() : value(0) {} | |
| 20 explicit inline BitSet32(uint32_t value) : value(value) {} | |
| 21 | |
| 22 // Gets the value associated with a particular bit index. | |
| 23 static inline uint32_t value_for_bit(uint32_t n) { return 0x80000000 >> n; } | |
| 24 | |
| 25 // Clears the bit set. | |
| 26 inline void clear() { value = 0; } | |
| 27 | |
| 28 // Returns the number of marked bits in the set. | |
| 29 inline uint32_t count() const { return popcnt(value); } | |
| 30 | |
| 31 // Returns true if the bit set does not contain any marked bits. | |
| 32 inline bool is_empty() const { return !value; } | |
| 33 | |
| 34 // Returns true if the bit set does not contain any unmarked bits. | |
| 35 inline bool is_full() const { return value == 0xffffffff; } | |
| 36 | |
| 37 // Returns true if the specified bit is marked. | |
| 38 inline bool has_bit(uint32_t n) const { return value & value_for_bit(n); } | |
| 39 | |
| 40 // Marks the specified bit. | |
| 41 inline void mark_bit(uint32_t n) { value |= value_for_bit(n); } | |
| 42 | |
| 43 // Clears the specified bit. | |
| 44 inline void clear_bit(uint32_t n) { value &= ~value_for_bit(n); } | |
| 45 | |
| 46 // Finds the first marked bit in the set. | |
| 47 // Result is undefined if all bits are unmarked. | |
| 48 inline uint32_t first_marked_bit() const { return clz(value); } | |
| 49 | |
| 50 // Finds the first unmarked bit in the set. | |
| 51 // Result is undefined if all bits are marked. | |
| 52 inline uint32_t first_unmarked_bit() const { return clz(~value); } | |
| 53 | |
| 54 // Finds the last marked bit in the set. | |
| 55 // Result is undefined if all bits are unmarked. | |
| 56 inline uint32_t last_marked_bit() const { return 31 - ctz(value); } | |
| 57 | |
| 58 // Finds the first marked bit in the set and clears it. Returns the bit | |
| 59 // index. | |
| 60 // Result is undefined if all bits are unmarked. | |
| 61 inline uint32_t clear_first_marked_bit() { | |
| 62 uint32_t n = first_marked_bit(); | |
| 63 clear_bit(n); | |
| 64 return n; | |
| 65 } | |
| 66 | |
| 67 // Finds the first unmarked bit in the set and marks it. Returns the bit | |
| 68 // index. | |
| 69 // Result is undefined if all bits are marked. | |
| 70 inline uint32_t mark_first_unmarked_bit() { | |
| 71 uint32_t n = first_unmarked_bit(); | |
| 72 mark_bit(n); | |
| 73 return n; | |
| 74 } | |
| 75 | |
| 76 // Finds the last marked bit in the set and clears it. Returns the bit index. | |
| 77 // Result is undefined if all bits are unmarked. | |
| 78 inline uint32_t clear_last_marked_bit() { | |
| 79 uint32_t n = last_marked_bit(); | |
| 80 clear_bit(n); | |
| 81 return n; | |
| 82 } | |
| 83 | |
| 84 // Gets the inde of the specified bit in the set, which is the number of | |
|
tdresser
2014/01/08 19:18:21
inde -> index
| |
| 85 // marked bits that appear before the specified bit. | |
| 86 inline uint32_t get_index_of_bit(uint32_t n) const { | |
| 87 return popcnt(value & ~(0xffffffffUL >> n)); | |
| 88 } | |
| 89 | |
| 90 inline bool operator==(const BitSet32& other) const { | |
| 91 return value == other.value; | |
| 92 } | |
| 93 inline bool operator!=(const BitSet32& other) const { | |
| 94 return value != other.value; | |
| 95 } | |
| 96 inline BitSet32 operator&(const BitSet32& other) const { | |
| 97 return BitSet32(value & other.value); | |
| 98 } | |
| 99 inline BitSet32& operator&=(const BitSet32& other) { | |
| 100 value &= other.value; | |
| 101 return *this; | |
| 102 } | |
| 103 inline BitSet32 operator|(const BitSet32& other) const { | |
| 104 return BitSet32(value | other.value); | |
| 105 } | |
| 106 inline BitSet32& operator|=(const BitSet32& other) { | |
| 107 value |= other.value; | |
| 108 return *this; | |
| 109 } | |
| 110 | |
| 111 private: | |
| 112 | |
| 113 #if defined(COMPILER_GCC) || defined(__clang__) | |
| 114 static inline uint32_t popcnt(uint32_t v) { return __builtin_popcount(v); } | |
| 115 static inline uint32_t clz(uint32_t v) { return v ? __builtin_clz(v) : 32; } | |
| 116 static inline uint32_t ctz(uint32_t v) { return __builtin_ctz(v); } | |
| 117 #else | |
| 118 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel | |
| 119 static inline uint32_t popcnt(uint32_t v) { | |
| 120 v = v - ((v >> 1) & 0x55555555); | |
| 121 v = (v & 0x33333333) + ((v >> 2) & 0x33333333); | |
| 122 return (((v + (v >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; | |
| 123 } | |
| 124 static inline uint32_t clz(uint32_t v) { | |
| 125 v |= (v >> 1); | |
| 126 v |= (v >> 2); | |
| 127 v |= (v >> 4); | |
| 128 v |= (v >> 8); | |
| 129 v |= (v >> 16); | |
| 130 return 32 - popcnt(v); | |
| 131 } | |
| 132 static inline uint32_t ctz(uint32_t v) { | |
| 133 return popcnt((v & -v) - 1)); | |
| 134 } | |
| 135 #endif | |
| 136 }; | |
| 137 | |
| 138 } // namespace content | |
| 139 | |
| 140 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURES_VELOCITY_TRACKER_H_ | |
| OLD | NEW |