strength_reduce.sol 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // RUN: --target substrate --emit cfg
  2. contract test {
  3. /******************/
  4. /* Multiply tests */
  5. /******************/
  6. // BEGIN-CHECK: test::function::f1
  7. function f1() pure public {
  8. for (uint i = 0; i < 10; i++) {
  9. // this multiply can be done with a 64 bit instruction
  10. print("i:{}".format(i * 100));
  11. }
  12. // CHECK: zext uint256 ((trunc uint64 %i) * uint64 100)
  13. }
  14. // BEGIN-CHECK: test::function::f2
  15. function f2(bool x) pure public {
  16. uint i = 0;
  17. for (;;) {
  18. print("i:{}".format(i * 100));
  19. i += 1;
  20. if (x)
  21. break;
  22. }
  23. // CHECK: %i * uint256 100
  24. }
  25. // BEGIN-CHECK: test::function::f3
  26. function f3(bool x) pure public {
  27. uint i = 0;
  28. for (;;) {
  29. print("i:{}".format((i & 255) * 100));
  30. i += 1;
  31. if (x)
  32. break;
  33. }
  34. // CHECK: (zext uint256 ((trunc uint64 (%i & uint256 255)) * uint64 100))
  35. }
  36. // BEGIN-CHECK: test::function::f4
  37. function f4() pure public {
  38. for (uint i = 0; i < 10; i++) {
  39. // this multiply can be done with a 64 bit instruction
  40. print("i:{}".format(i * 32768));
  41. }
  42. // CHECK: (%i << uint256 15)
  43. }
  44. // BEGIN-CHECK: test::function::f5
  45. function f5() pure public {
  46. for (int i = -50; i < -10; i++) {
  47. // this multiply can be done with a 64 bit instruction
  48. print("i:{}".format(i * 32769));
  49. }
  50. // CHECK: sext int256 ((trunc int64 %i) * int64 32769)
  51. }
  52. /******************/
  53. /* Division tests */
  54. /******************/
  55. // BEGIN-CHECK: test::function::f6
  56. function f6() pure public {
  57. for (uint i = 1E9; i < 1E9+10; i++) {
  58. print("i:{}".format(i / 32768));
  59. }
  60. // CHECK: (%i >> uint256 15)
  61. }
  62. // BEGIN-CHECK: test::function::f7
  63. function f7(uint64 arg1) pure public {
  64. // we're upcasting to 256 bits, but known bits will track this
  65. uint i = arg1;
  66. print("i:{}".format(i / 1e6));
  67. // CHECK: zext uint256 ((trunc uint64 %i) / uint64 1000000))
  68. }
  69. // BEGIN-CHECK: test::function::f8
  70. function f8() pure public {
  71. // too many values to track; (101 values)
  72. for (uint i = 1e9; i < 1e9+101; i++) {
  73. print("i:{}".format(i / 1e6));
  74. }
  75. // CHECK: (%i / uint256 1000000)
  76. }
  77. /****************/
  78. /* Modulo tests */
  79. /****************/
  80. // BEGIN-CHECK: test::function::f9
  81. function f9() pure public {
  82. // too many values to track; (101 values)
  83. for (uint i = 1e9; i < 1e9+101; i++) {
  84. print("i:{}".format(i % 0x1_0000_0000));
  85. }
  86. // CHECK: (%i & uint256 4294967295)
  87. }
  88. // BEGIN-CHECK: test::function::f10
  89. function f10() pure public {
  90. for (int i = 30; i >= 0; i--) {
  91. print("i:{}".format(i % 0x1_0000_0001));
  92. }
  93. // CHECK: (sext int256 ((trunc int64 %i) % int64 4294967297))
  94. }
  95. // BEGIN-CHECK: test::function::f11
  96. function f11() pure public {
  97. for (int i = 0; i != 102; i++) {
  98. print("i:{}".format(i % 0x1_0000_0001));
  99. }
  100. // CHECK: (%i % int256 4294967297)
  101. }
  102. }