From 48b3ec6e8e4be23e0d4620fb32b8c7faf082569d Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 5 Aug 2019 21:29:02 -0700 Subject: 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 --- src/corelib/tools/qbitarray.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qbitarray.cpp b/src/corelib/tools/qbitarray.cpp index 4e8e3c241e..4952090620 100644 --- a/src/corelib/tools/qbitarray.cpp +++ b/src/corelib/tools/qbitarray.cpp @@ -154,8 +154,8 @@ QBitArray::QBitArray(int size, bool value) uchar* c = reinterpret_cast(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(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; } } -- cgit v1.2.3