@@ -56,27 +56,47 @@ public static boolean testShouldReplaceCpmUCase1(int x, int m) {
5656 @ Test
5757 @ Arguments (values = {Argument .DEFAULT , Argument .DEFAULT })
5858 @ IR (failOn = IRNode .CMP_U ,
59- phase = CompilePhase .AFTER_PARSING ,
60- applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
59+ phase = CompilePhase .AFTER_PARSING ,
60+ applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
6161 public static boolean testShouldReplaceCpmUCase2 (int x , int m ) {
6262 return !(Integer .compareUnsigned ((m & x ), m ) > 0 );
6363 }
6464
6565 @ Test
66- @ Arguments (values = {Argument .DEFAULT , Argument .DEFAULT })
66+ @ Arguments (values = {Argument .DEFAULT , Argument .RANDOM_EACH })
6767 @ IR (failOn = IRNode .CMP_U ,
68- phase = CompilePhase .AFTER_PARSING ,
69- applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
68+ phase = CompilePhase .AFTER_PARSING ,
69+ applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
7070 public static boolean testShouldReplaceCpmUCase3 (int x , int m ) {
71+ m = Math .max (0 , m );
7172 return Integer .compareUnsigned ((x & m ), m + 1 ) < 0 ;
7273 }
7374
7475 @ Test
75- @ Arguments (values = {Argument .DEFAULT , Argument .DEFAULT })
76+ @ Arguments (values = {Argument .DEFAULT , Argument .RANDOM_EACH })
7677 @ IR (failOn = IRNode .CMP_U ,
77- phase = CompilePhase .AFTER_PARSING ,
78- applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
78+ phase = CompilePhase .AFTER_PARSING ,
79+ applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
7980 public static boolean testShouldReplaceCpmUCase4 (int x , int m ) {
81+ m = Math .max (0 , m );
82+ return Integer .compareUnsigned ((m & x ), m + 1 ) < 0 ;
83+ }
84+
85+ @ Test
86+ @ Arguments (values = {Argument .DEFAULT , Argument .DEFAULT })
87+ @ IR (counts = {IRNode .CMP_U , "1" }, // m could be -1 and thus optimization cannot be applied
88+ phase = CompilePhase .AFTER_PARSING ,
89+ applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
90+ public static boolean testShouldNotReplaceCpmUCase1 (int x , int m ) {
91+ return Integer .compareUnsigned ((x & m ), m + 1 ) < 0 ;
92+ }
93+
94+ @ Test
95+ @ Arguments (values = {Argument .DEFAULT , Argument .DEFAULT })
96+ @ IR (counts = {IRNode .CMP_U , "1" }, // m could be -1 and thus optimization cannot be applied
97+ phase = CompilePhase .AFTER_PARSING ,
98+ applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
99+ public static boolean testShouldNotReplaceCpmUCase2 (int x , int m ) {
80100 return Integer .compareUnsigned ((m & x ), m + 1 ) < 0 ;
81101 }
82102
@@ -92,40 +112,41 @@ public static boolean testShouldHaveCpmUCase1(int x, int m) {
92112 @ Test
93113 @ Arguments (values = {Argument .DEFAULT , Argument .DEFAULT })
94114 @ IR (counts = {IRNode .CMP_U , "1" },
95- phase = CompilePhase .AFTER_PARSING ,
96- applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
115+ phase = CompilePhase .AFTER_PARSING ,
116+ applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
97117 public static boolean testShouldHaveCpmUCase2 (int x , int m ) {
98118 return !(Integer .compareUnsigned ((m & x ), m - 1 ) > 0 );
99119 }
100120
101121 @ Test
102122 @ Arguments (values = {Argument .DEFAULT , Argument .DEFAULT })
103123 @ IR (counts = {IRNode .CMP_U , "1" },
104- phase = CompilePhase .AFTER_PARSING ,
105- applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
124+ phase = CompilePhase .AFTER_PARSING ,
125+ applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
106126 public static boolean testShouldHaveCpmUCase3 (int x , int m ) {
107127 return Integer .compareUnsigned ((x & m ), m + 2 ) < 0 ;
108128 }
109129
110130 @ Test
111131 @ Arguments (values = {Argument .DEFAULT , Argument .DEFAULT })
112132 @ IR (counts = {IRNode .CMP_U , "1" },
113- phase = CompilePhase .AFTER_PARSING ,
114- applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
133+ phase = CompilePhase .AFTER_PARSING ,
134+ applyIfPlatformOr = {"x64" , "true" , "aarch64" , "true" , "riscv64" , "true" })
115135 public static boolean testShouldHaveCpmUCase4 (int x , int m ) {
116136 return Integer .compareUnsigned ((m & x ), m + 2 ) < 0 ;
117137 }
118138
119139 private static void testCorrectness () {
120140 int [] values = {
121- 0 , 1 , 5 , 8 , 16 , 42 , 100 , new Random ().nextInt (0 , Integer .MAX_VALUE ), Integer .MAX_VALUE
141+ -100 , -42 , -16 , -8 , -5 , -1 , 0 , 1 , 5 , 8 , 16 , 42 , 100 ,
142+ new Random ().nextInt (), Integer .MAX_VALUE , Integer .MIN_VALUE
122143 };
123144
124145 for (int x : values ) {
125146 for (int m : values ) {
126- if (!testShouldReplaceCpmUCase1 (x , m ) |
127- !testShouldReplaceCpmUCase2 (x , m ) |
128- !testShouldReplaceCpmUCase3 (x , m ) |
147+ if (!testShouldReplaceCpmUCase1 (x , m ) ||
148+ !testShouldReplaceCpmUCase2 (x , m ) ||
149+ !testShouldReplaceCpmUCase3 (x , m ) ||
129150 !testShouldReplaceCpmUCase4 (x , m )) {
130151 throw new RuntimeException ("Bad result for x = " + x + " and m = " + m + ", expected always true" );
131152 }
0 commit comments