This is an archive of the discontinued LLVM Phabricator instance.

Create some helpers for generating bit masks
ClosedPublic

Authored by zturner on Apr 18 2017, 8:49 PM.

Details

Summary

Often you want a bit-mask with the first N bits or the last N bits set to 1. This is surprisingly confusing to get right, in part because X << (8*sizeof(X)) is undefined, so trying to produce a mask of all 1s won't work, and also in part because the branch-free implementation is not obvious.

Diff Detail

Repository
rL LLVM

Event Timeline

zturner created this revision.Apr 18 2017, 8:49 PM
chandlerc accepted this revision.Apr 18 2017, 9:36 PM

Nice, LGTM!

This revision is now accepted and ready to land.Apr 18 2017, 9:36 PM
This revision was automatically updated to reflect the committed changes.
amccarth added inline comments.
llvm/trunk/include/llvm/Support/MathExtras.h
207

Be aware that this can lead to compiler warnings, at least in MSVC, if T is an unsigned type:

C4146: unary minus operator applied to unsigned type, result still unsigned

Depending on the specific details, this can be a level 2 or level 3 warning. I'm surprised the tests aren't triggering it. We must disable this warning somewhere.