To Int or To Uint
summary
Shows concrete x86 assembly (tested on Clang 14 and rechecked on Clang 17) and proves small choices change speed: averaging two numbers uses LEA+logical SHR for unsigned but extra sign-bit math and SAR for signed, making the signed path slightly slower; a handcrafted arithmetic-sum example lets the signed version be optimized into a closed form while the unsigned path suffers a 40Γ slowdown due to UB and compiler conservatism. Recommends using sanitizers to catch signed overflow, prefer C++20/23 helpers (std::make_signed/unsigned, std::ssize), avoid auto for small literals, and enable warnings-as-errors.
AI-written summary Β· about
chapters (11)
- 0:00Introduction and speaker background
- 3:22Basics of signed vs unsigned integers
- 15:14How signed integers are represented
- 23:24Performance differences in division
- 24:05Common pitfalls with integer types
- 39:37Arithmetic series benchmark
- 43:56Undefined behavior risks
- 47:09Compiler flags and sanitizers
- 50:40C++20 tools for integers
- 53:16Strong types and modern practices
- 59:01Q&A session
related talks
01h 00mint != safe && int != β€
19mExploiting Undefined Behavior in C/C++ Programs for Optimization: A Study on the(β¦)
01h 13mPractical Tips for Safer C++
58mCorrect Use of Integers in Safety-critical Systems
01h 30mDangerous Optimizations in C and C++ Programming Languages
33mUndefined Behavior in C++: What Every Programmer Should Know and Fear
01h 00mJust enough Assembly for Compiler Explorer
39mSafety first
