<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hi,</div><div><br></div><div>The LLVM coding style does not specify anything about the use of signed/unsigned integer, and the codebase is inconsistent (there is a majority of code that is using unsigned index in loops today though).</div><div><br></div><div>I'd like to suggest that we specify to prefer `int` when possible and use `unsigned` only for bitmask and when you intend to rely on wrapping behavior, see: <a href="https://reviews.llvm.org/D63049">https://reviews.llvm.org/D63049</a></div><div><br></div><div>A lot has been written about this, good references are [unsigned: A Guideline for Better Code] <a href="https://www.youtube.com/watch?v=wvtFGa6XJDU">https://www.youtube.com/watch?v=wvtFGa6XJDU</a>) and [Garbage In, Garbage Out: Arguing about Undefined Behavior...](<a href="https://www.youtube.com/watch?v=yG1OZ69H_-o">https://www.youtube.com/watch?v=yG1OZ69H_-o</a>), as well as this panel discussion:</div><div>- <a href="https://www.youtube.com/watch?v=Puio5dly9N8#t=12m12s">https://www.youtube.com/watch?v=Puio5dly9N8#t=12m12s</a></div><div>- <a href="https://www.youtube.com/watch?v=Puio5dly9N8#t=42m40s">https://www.youtube.com/watch?v=Puio5dly9N8#t=42m40s</a></div><div><br></div><div>Other coding guidelines already embrace this:</div><div><br></div><div>- <a href="http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-signed">http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-signed</a></div><div>- <a href="http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-unsigned">http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-unsigned</a></div><div>- <a href="https://google.github.io/styleguide/cppguide.html#Integer_Types">https://google.github.io/styleguide/cppguide.html#Integer_Types</a></div><div><br></div><div><div>It is rare that overflowing (and wrapping) an unsigned integer won't trigger a program bug when the overflow was not intentionally handled. Using signed arithmetic means that you can actually trap on over/underflow and catch these bugs (when using fuzzing for instance).</div><div><br></div><div>Chandler explained this nicely in his CPPCon 2016 talk "Garbage In, Garbage Out: Arguing about Undefined Behavior...". I encourage to watch the full talk but here is one relevant example: <a href="https://youtu.be/yG1OZ69H_-o?t=2006">https://youtu.be/yG1OZ69H_-o?t=2006</a> , and here he mentions how Google experimented with this internally: <a href="https://youtu.be/yG1OZ69H_-o?t=2249">https://youtu.be/yG1OZ69H_-o?t=2249</a></div><div><br></div><div>Unsigned integer also have a discontinuity right to the left of zero. Suppose A, B and C are small positive integers close to zero, say all less than a hundred or so. Then given:<br></div><div>A + B > C</div><div>and knowing elementary school algebra, one can rewrite that as:</div><div>A > B - C</div><div>But C might be greater than B, and the subtraction would produce some huge number. This happens even when working with seemingly harmless numbers like A=2, B=2, and C=3.</div></div><div><br></div><div>-- </div><div>Mehdi</div><div><br></div></div></div></div></div></div></div></div></div></div>