To Int or to Uint, This is the Question
Talkat CppCon 2024 · on Thursday, 2024-09-19· at 15:15· Adams A, 6700 North Gaylord Rockies Boulevard·
summary
Choosing signed vs unsigned changes generated code and runtime: dividing (a+b)/2 yields a different sequence of instructions because signed arithmetic extracts the sign bit and uses arithmetic shifts while unsigned uses logical shifts; summing an arithmetic series compiles to a closed-form for signed but stays a loop for unsigned and thus one version shows 40× faster behavior; the reason is compilers can exploit or must guard against undefined behavior; practical fixes include overflow sanitizers and build flags plus helpers like size_t/ssize_t and make_signed/make_unsigned on x86.
AI-written summary · about
chapters (12)
- 0:00Introduction
- 2:11Int and uint matter in C++
- 6:06Signed vs unsigned performance
- 15:25How integers are stored
- 24:20Pitfalls of mixing types
- 33:45Arithmetic series performance
- 37:58Undefined behavior and optimizations
- 41:55Compiler improvements and flags
- 44:47C++11 helpers for types
- 46:39Safe comparison functions
- 48:05Best practices for type safety
- 50:34Summary
related talks
05mSigned Integers: Faster and Corrector
55mClean Integral Code
06mLightning Talk: Un-Undefining Undefined Behavior in C++
07mType punning done right
07mThe Perils of Strict Aliasing
48m[a bit noisy] Optimizing Generic Algorithms
52mAdvanced SIMD Algorithms in Pictures
38mEngineering Software Integral Types
