OLD | NEW |
| (Empty) |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 #if V8_TARGET_ARCH_A64 | |
29 | |
30 #include "a64/utils-a64.h" | |
31 | |
32 | |
33 namespace v8 { | |
34 namespace internal { | |
35 | |
36 #define __ assm-> | |
37 | |
38 | |
39 int CountLeadingZeros(uint64_t value, int width) { | |
40 // TODO(jbramley): Optimize this for A64 hosts. | |
41 ASSERT((width == 32) || (width == 64)); | |
42 int count = 0; | |
43 uint64_t bit_test = 1UL << (width - 1); | |
44 while ((count < width) && ((bit_test & value) == 0)) { | |
45 count++; | |
46 bit_test >>= 1; | |
47 } | |
48 return count; | |
49 } | |
50 | |
51 | |
52 int CountLeadingSignBits(int64_t value, int width) { | |
53 // TODO(jbramley): Optimize this for A64 hosts. | |
54 ASSERT((width == 32) || (width == 64)); | |
55 if (value >= 0) { | |
56 return CountLeadingZeros(value, width) - 1; | |
57 } else { | |
58 return CountLeadingZeros(~value, width) - 1; | |
59 } | |
60 } | |
61 | |
62 | |
63 int CountTrailingZeros(uint64_t value, int width) { | |
64 // TODO(jbramley): Optimize this for A64 hosts. | |
65 ASSERT((width == 32) || (width == 64)); | |
66 int count = 0; | |
67 while ((count < width) && (((value >> count) & 1) == 0)) { | |
68 count++; | |
69 } | |
70 return count; | |
71 } | |
72 | |
73 | |
74 int CountSetBits(uint64_t value, int width) { | |
75 // TODO(jbramley): Would it be useful to allow other widths? The | |
76 // implementation already supports them. | |
77 ASSERT((width == 32) || (width == 64)); | |
78 | |
79 // Mask out unused bits to ensure that they are not counted. | |
80 value &= (0xffffffffffffffffUL >> (64-width)); | |
81 | |
82 // Add up the set bits. | |
83 // The algorithm works by adding pairs of bit fields together iteratively, | |
84 // where the size of each bit field doubles each time. | |
85 // An example for an 8-bit value: | |
86 // Bits: h g f e d c b a | |
87 // \ | \ | \ | \ | | |
88 // value = h+g f+e d+c b+a | |
89 // \ | \ | | |
90 // value = h+g+f+e d+c+b+a | |
91 // \ | | |
92 // value = h+g+f+e+d+c+b+a | |
93 value = ((value >> 1) & 0x5555555555555555) + (value & 0x5555555555555555); | |
94 value = ((value >> 2) & 0x3333333333333333) + (value & 0x3333333333333333); | |
95 value = ((value >> 4) & 0x0f0f0f0f0f0f0f0f) + (value & 0x0f0f0f0f0f0f0f0f); | |
96 value = ((value >> 8) & 0x00ff00ff00ff00ff) + (value & 0x00ff00ff00ff00ff); | |
97 value = ((value >> 16) & 0x0000ffff0000ffff) + (value & 0x0000ffff0000ffff); | |
98 value = ((value >> 32) & 0x00000000ffffffff) + (value & 0x00000000ffffffff); | |
99 | |
100 return value; | |
101 } | |
102 | |
103 | |
104 int MaskToBit(uint64_t mask) { | |
105 ASSERT(CountSetBits(mask, 64) == 1); | |
106 return CountTrailingZeros(mask, 64); | |
107 } | |
108 | |
109 | |
110 } } // namespace v8::internal | |
111 | |
112 #endif // V8_TARGET_ARCH_A64 | |
OLD | NEW |