1 // Copyright 2013, ARM Limited
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of ARM Limited nor the names of its contributors may be
13 // used to endorse or promote products derived from this software without
14 // specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 uint32_t float_to_rawbits(float value
) {
34 memcpy(&bits
, &value
, 4);
39 uint64_t double_to_rawbits(double value
) {
41 memcpy(&bits
, &value
, 8);
46 float rawbits_to_float(uint32_t bits
) {
48 memcpy(&value
, &bits
, 4);
53 double rawbits_to_double(uint64_t bits
) {
55 memcpy(&value
, &bits
, 8);
60 int CountLeadingZeros(uint64_t value
, int width
) {
61 VIXL_ASSERT((width
== 32) || (width
== 64));
63 uint64_t bit_test
= UINT64_C(1) << (width
- 1);
64 while ((count
< width
) && ((bit_test
& value
) == 0)) {
72 int CountLeadingSignBits(int64_t value
, int width
) {
73 VIXL_ASSERT((width
== 32) || (width
== 64));
75 return CountLeadingZeros(value
, width
) - 1;
77 return CountLeadingZeros(~value
, width
) - 1;
82 int CountTrailingZeros(uint64_t value
, int width
) {
83 VIXL_ASSERT((width
== 32) || (width
== 64));
85 while ((count
< width
) && (((value
>> count
) & 1) == 0)) {
92 int CountSetBits(uint64_t value
, int width
) {
93 // TODO: Other widths could be added here, as the implementation already
95 VIXL_ASSERT((width
== 32) || (width
== 64));
97 // Mask out unused bits to ensure that they are not counted.
98 value
&= (UINT64_C(0xffffffffffffffff) >> (64-width
));
100 // Add up the set bits.
101 // The algorithm works by adding pairs of bit fields together iteratively,
102 // where the size of each bit field doubles each time.
103 // An example for an 8-bit value:
104 // Bits: h g f e d c b a
106 // value = h+g f+e d+c b+a
108 // value = h+g+f+e d+c+b+a
110 // value = h+g+f+e+d+c+b+a
111 const uint64_t kMasks
[] = {
112 UINT64_C(0x5555555555555555),
113 UINT64_C(0x3333333333333333),
114 UINT64_C(0x0f0f0f0f0f0f0f0f),
115 UINT64_C(0x00ff00ff00ff00ff),
116 UINT64_C(0x0000ffff0000ffff),
117 UINT64_C(0x00000000ffffffff),
120 for (unsigned i
= 0; i
< (sizeof(kMasks
) / sizeof(kMasks
[0])); i
++) {
122 value
= ((value
>> shift
) & kMasks
[i
]) + (value
& kMasks
[i
]);
129 uint64_t LowestSetBit(uint64_t value
) {
130 return value
& -value
;
134 bool IsPowerOf2(int64_t value
) {
135 return (value
!= 0) && ((value
& (value
- 1)) == 0);