Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <algorithm>
  2.  
  3. // Eliminate multiplication in comparison with 0
  4. bool example1(T x) {
  5. T c = 5;
  6. return (x * c) > 0;
  7. }
  8.  
  9. // Eliminate division after multiplication
  10. T example2(T x) {
  11. T c1 = 14;
  12. T c2 = 7;
  13. return (x * 6) / 3;
  14. }
  15.  
  16. // Eliminate negation
  17. T example3(T x, T y) {
  18. return (-x) / (-y);
  19. }
  20.  
  21. // Simplify comparisons that are always true or false
  22. bool example4a(T x) {
  23. T c = 1;
  24. return x + c < x;
  25. }
  26.  
  27. bool example4b(T x) {
  28. T c = 1;
  29. return x + c <= x;
  30. }
  31.  
  32. bool example4c(T x) {
  33. T c = 1;
  34. return x + c > x;
  35. }
  36.  
  37. bool example4d(T x) {
  38. T c = 1;
  39. return x + c >= x;
  40. }
  41.  
  42. // Eliminate negation in comparisons
  43. bool example5(T x, T y) {
  44. return (-x) == (-y);
  45. }
  46.  
  47. // Reduce magnitude of constants
  48. bool example6a(T x, T y) {
  49. T c = 5;
  50. return x + c > y;
  51. }
  52.  
  53. bool example6b(T x, T y) {
  54. T c = 7;
  55. return x + c <= y;
  56. }
  57.  
  58. // Eliminate constants in comparisons
  59. bool example7a(T x, T y) {
  60. T c1 = 17, c2 = 23;
  61. return (x + c1) < c2;
  62. }
  63.  
  64. bool example7b(T x, T y) {
  65. T c1 = 17, c2 = 23;
  66. return (x + c1) < (y + c2);
  67. }
  68.  
  69. // Pointer arithmetic and type promotion
  70. bool example8(float * a, T i) {
  71. float * e0 = a+i;
  72. float * e1 = a+(i+1);
  73. return e1 - e0 == 1;
  74. }
  75.  
  76. // Value range calculations
  77. T example9a(T x) {
  78. if (x > 0) {
  79. T y = x + 5;
  80. T z = y / 4;
  81. return z;
  82. }
  83. }
  84.  
  85. // Changing comparisons x<y to true or false if the ranges for x and y does not overlap
  86. bool example9b(T x) {
  87. if (x > 0) {
  88. T y = x + 5;
  89. return x < y;
  90. }
  91. }
  92.  
  93. // Changing min(x,y) or max(x,y) to x or y if the ranges do not overlap
  94. T example9c(T x) {
  95. if (x > 0) {
  96. T y = x + 5;
  97. return std::min(x, y);
  98. }
  99. }
  100.  
  101. // Changing abs(x) to x or -x if the range does not cross 0
  102. T example9d(T x) {
  103. if (x > 0) {
  104. return abs(x);
  105. }
  106. }
  107.  
  108. // Changing x/c to x>>log2(c) if x>0 and the constant c is a power of 2
  109. T example9e(T x) {
  110. T c = 8;
  111. if (x > 0) {
  112. return x / c;
  113. }
  114. }
  115.  
  116. // Loop analysis and optimization
  117. T example10(T m) {
  118. int sum = 0;
  119. for (short T i = 0; i <= m; ++ i) {
  120. sum += i;
  121. }
  122. return sum;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement