Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix MSVC unary minus on unsigned type warning
Summary: Negating an unsigned number is not mathematically meaningful, hence Visual Studio complains with this warning: ``` thrift\lib\cpp\util\cpp-util-headerswindows#header-mode-symlink-tree-only,headers\thrift\lib\cpp\util\varintutils-inl.h(203): warning C4146: unary minus operator applied to unsigned type, result still unsigned thrift\lib\cpp\util\cpp-util-headerswindows#header-mode-symlink-tree-only,headers\thrift\lib\cpp\util\varintutils-inl.h(207): warning C4146: unary minus operator applied to unsigned type, result still unsigned ``` We can revise this code to clarify what the zig-zag operation is actually doing. Basically it decides whether to invert all bits depending on the value of `n & 1` While less compact in C this does not change functionality nor performance on x86_64 or armv7, as confirmed by clang `-O3`, `-Os`, and `-Oz` generating identical instructions. Interestingly though, for aarch64 it generates sequences with one fewer instruction: ``` 0000000000000000 <_Z11zigzagToI32j>: 0: 12000008 and w8, w0, #0x1 4: 4b0803e8 neg w8, w8 8: 4a400500 eor w0, w8, w0, lsr #1 c: d65f03c0 ret --> 0000000000000000 <_Z11zigzagToI32j>: 0: 13000008 sbfx w8, w0, #0, #1 4: 4a400500 eor w0, w8, w0, lsr #1 8: d65f03c0 ret ``` (Again with the identical optimization regardless of `-O3`, `-Os`, or `-Oz`.) Reviewed By: yfeldblum Differential Revision: D17789109 fbshipit-source-id: 1cf0977cd9369402051058c4fb0576505eb05970
- Loading branch information