QBitArray: change modulo 8 with bitwise-AND 7

They're the same only for unsigned values. Modulo of negative numbers
since C++11 has an expected behavior and generates more code:

%rax = %rax & 7:
        andl    $7, %eax

%rax = %rax % 8 with GCC:
        cqto
        shrq    $61, %rdx
        addq    %rdx, %rax
        andl    $7, %eax
        subq    %rdx, %rax
[read as ((%rax + (%rax < 0 ? 7 : 0)) & 7) - (%rax < 0 ? 7 : 0))]

With Clang:
        movq    %rax, %rcx
        sarq    $63, %rcx
        shrq    $61, %rcx
        addq    %rax, %rcx
        andq    $-8, %rcx
        subq    %rcx, %rax

Change-Id: Ife213d861bb14c1787e1fffd15b83b004be7eba0
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Thiago Macieira 2019-08-05 21:29:02 -07:00
parent c76b86aec6
commit 48b3ec6e8e

View File

@ -154,8 +154,8 @@ QBitArray::QBitArray(int size, bool value)
uchar* c = reinterpret_cast<uchar*>(d.data());
memset(c + 1, value ? 0xff : 0, d.size() - 1);
*c = d.size()*8 - size;
if (value && size && size % 8)
*(c+1+size/8) &= (1 << (size%8)) - 1;
if (value && size && size & 7)
*(c+1+size/8) &= (1 << (size & 7)) - 1;
}
/*! \fn int QBitArray::size() const
@ -227,8 +227,8 @@ void QBitArray::resize(int size)
uchar* c = reinterpret_cast<uchar*>(d.data());
if (size > (s << 3))
memset(c + s, 0, d.size() - s);
else if ( size % 8)
*(c+1+size/8) &= (1 << (size%8)) - 1;
else if (size & 7)
*(c+1+size/8) &= (1 << (size & 7)) - 1;
*c = d.size()*8 - size;
}
}