Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MVTGeometryWriter: encode point difference in a C/C++ compliant way
cppcheck warns that vx >> 31 is undefined behaviour when vx is signed. More generally, bitwise operations on signed integers are implementation defined behaviour. See https://wiki.sei.cmu.edu/confluence/display/c/INT13-C.+Use+bitwise+operators+only+on+unsigned+operands So use the ( quint32 )( -( qint32 )( ( quint32 )vx >> 31 ) ) ) trick instead. Demo: - if vx is negative, (quint32)vx >> 31 leads to 1, which then becomes -1, which then becomes 0xFFFFFFFF when cast to unsigned. - if vx is >= 0, this leads to 0. gcc compiles both non-compliant and compliant versions identically. Given test.cpp with ``` unsigned foo(int x) { return x >> 31; } unsigned bar(int x) { return (unsigned)(-(int)((unsigned)x >> 31)); } ``` g++ -O2 test.cpp -c && objdump -d test.o : ``` 0000000000000000 <_Z3fooi>: 0: 89 f8 mov %edi,%eax 2: c1 f8 1f sar $0x1f,%eax 5: c3 retq 6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) d: 00 00 00 0000000000000010 <_Z3bari>: 10: 89 f8 mov %edi,%eax 12: c1 f8 1f sar $0x1f,%eax 15: c3 retq ```
- Loading branch information