Last active
July 26, 2017 02:05
-
-
Save johnmcfarlane/d7c42efe019493355b937b4418dfb83c to your computer and use it in GitHub Desktop.
Examples from Krister Walfridsson's [How undefined signed overflow enables optimizations in GCC](https://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// examples from Krister Walfridsson's "How undefined signed overflow enables optimizations in GCC": | |
// https://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html | |
// https://godbolt.org/g/Hw797e | |
// Other relevant material: | |
// [CppCon 2016: Jon Kalb “unsigned: A Guideline for Better Code"](https://www.youtube.com/watch?v=wvtFGa6XJDU) | |
// [CppCon 2016: Chandler Carruth “Garbage In, Garbage Out: Arguing about Undefined Behavior..."](https://www.youtube.com/watch?v=yG1OZ69H_-o&t=2542s) | |
#include <algorithm> | |
// Eliminate multiplication in comparison with 0 | |
bool example1(T x) { | |
T c = 5; | |
return (x * c) > 0; | |
} | |
// Eliminate division after multiplication | |
T example2(T x) { | |
T c1 = 14; | |
T c2 = 7; | |
return (x * 6) / 3; | |
} | |
// Eliminate negation | |
T example3(T x, T y) { | |
return (-x) / (-y); | |
} | |
// Simplify comparisons that are always true or false | |
bool example4a(T x) { | |
T c = 1; | |
return x + c < x; | |
} | |
bool example4b(T x) { | |
T c = 1; | |
return x + c <= x; | |
} | |
bool example4c(T x) { | |
T c = 1; | |
return x + c > x; | |
} | |
bool example4d(T x) { | |
T c = 1; | |
return x + c >= x; | |
} | |
// Eliminate negation in comparisons | |
bool example5(T x, T y) { | |
return (-x) == (-y); | |
} | |
// Reduce magnitude of constants | |
bool example6a(T x, T y) { | |
T c = 5; | |
return x + c > y; | |
} | |
bool example6b(T x, T y) { | |
T c = 7; | |
return x + c <= y; | |
} | |
// Eliminate constants in comparisons | |
bool example7a(T x, T y) { | |
T c1 = 17, c2 = 23; | |
return (x + c1) < c2; | |
} | |
bool example7b(T x, T y) { | |
T c1 = 17, c2 = 23; | |
return (x + c1) < (y + c2); | |
} | |
// Pointer arithmetic and type promotion | |
bool example8(float * a, T i) { | |
float * e0 = a+i; | |
float * e1 = a+(i+1); | |
return e1 - e0 == 1; | |
} | |
// Value range calculations | |
T example9a(T x) { | |
if (x > 0) { | |
T y = x + 5; | |
T z = y / 4; | |
return z; | |
} | |
} | |
// Changing comparisons x<y to true or false if the ranges for x and y does not overlap | |
bool example9b(T x) { | |
if (x > 0) { | |
T y = x + 5; | |
return x < y; | |
} | |
} | |
// Changing min(x,y) or max(x,y) to x or y if the ranges do not overlap | |
T example9c(T x) { | |
if (x > 0) { | |
T y = x + 5; | |
return std::min(x, y); | |
} | |
} | |
// Changing abs(x) to x or -x if the range does not cross 0 | |
T example9d(T x) { | |
if (x > 0) { | |
return abs(x); | |
} | |
} | |
// Changing x/c to x>>log2(c) if x>0 and the constant c is a power of 2 | |
T example9e(T x) { | |
T c = 8; | |
if (x > 0) { | |
return x / c; | |
} | |
} | |
// Loop analysis and optimization | |
T example10(T m) { | |
int sum = 0; | |
for (short T i = 0; i <= m; ++ i) { | |
sum += i; | |
} | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment