Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement