rotate.js 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Flags: --allow-natives-syntax --expose-gc

// Test shift operations that can be replaced by rotate operation.

function SideEffect() {
  with ({}) { }  // not inlinable
}

function Twenty() {
  SideEffect();
  return 20;
}

function Twelve() {
  SideEffect();
  return 12;
}


function ROR(x, sa) {
  return (x >>> sa) | (x << (32 - sa));
}

function ROR1(x, sa) {
  return (x >>> sa) | (x << (32 - sa));
}

function ROR2(x, sa) {
  return (x >>> (32 - sa)) | (x << (sa));
}

function ROR3(x, sa) {
  return (x << (32 - sa)) | (x >>> sa);
}

function ROR4(x, sa) {
  return (x << (sa)) | (x >>> (32 - sa));
}

67
%PrepareFunctionForOptimization(ROR);
68 69 70 71 72
assertEquals(1 << ((2 % 32)), ROR(1, 30));
assertEquals(1 << ((2 % 32)), ROR(1, 30));
%OptimizeFunctionOnNextCall(ROR);
assertEquals(1 << ((2 % 32)), ROR(1, 30));

73
%PrepareFunctionForOptimization(ROR1);
74 75 76 77 78
assertEquals(0xF0000FFF | 0, ROR1(0x0000FFFF, 4));
assertEquals(0xF0000FFF | 0, ROR1(0x0000FFFF, 4));
%OptimizeFunctionOnNextCall(ROR1);
assertEquals(0xF0000FFF | 0, ROR1(0x0000FFFF, 4));

79
%PrepareFunctionForOptimization(ROR1);
80 81 82 83 84
assertEquals(0x0FFFF000 | 0, ROR1(0x0000FFFF, 20));
assertEquals(0x0FFFF000 | 0, ROR1(0x0000FFFF, 20));
%OptimizeFunctionOnNextCall(ROR1);
assertEquals(0x0FFFF000 | 0, ROR1(0x0000FFFF, 20));

85
%PrepareFunctionForOptimization(ROR1);
86 87 88 89 90 91
assertEquals(0x0FFFF000 | 0, ROR1(0x0000FFFF, Twenty()));
assertEquals(0x0FFFF000 | 0, ROR1(0x0000FFFF, Twenty()));
%OptimizeFunctionOnNextCall(ROR1);
assertEquals(0x0FFFF000 | 0, ROR1(0x0000FFFF, Twenty()));

for (var i = 0; i <= 100; i++) {
92
  %PrepareFunctionForOptimization(ROR1);
93 94 95 96 97 98 99
  assertEquals(0xFFFFFFFF | 0, ROR1(0xFFFFFFFF, i));
  assertEquals(0xFFFFFFFF | 0, ROR1(0xFFFFFFFF, i));
  %OptimizeFunctionOnNextCall(ROR1);
  assertEquals(0xFFFFFFFF | 0, ROR1(0xFFFFFFFF, i));
}

for (var i = 0; i <= 100; i++) {
100
  %PrepareFunctionForOptimization(ROR1);
101 102 103 104 105 106 107
  assertEquals(-1, ROR1(-1, i));
  assertEquals(-1, ROR1(-1, i));
  %OptimizeFunctionOnNextCall(ROR1);
  assertEquals(-1, ROR1(-1, i));
}

for (var i = 0; i <= 100; i++) {
108
  %PrepareFunctionForOptimization(ROR1);
109 110 111 112 113 114 115
  assertEquals(1 << (32 - (i % 32)), ROR1(1, i));
  assertEquals(1 << (32 - (i % 32)), ROR1(1, i));
  %OptimizeFunctionOnNextCall(ROR1);
  assertEquals(1 << (32 - (i % 32)), ROR1(1, i));
}

for (var i = 0; i <= 100; i++) {
116
  %PrepareFunctionForOptimization(ROR1);
117 118 119 120 121 122 123 124
  assertEquals(1 << (32 - (i % 32)), ROR1(1.4, i));
  assertEquals(1 << (32 - (i % 32)), ROR1(1.4, i));
  %OptimizeFunctionOnNextCall(ROR1);
  assertEquals(1 << (32 - (i % 32)), ROR1(1.4, i));
}



125
%PrepareFunctionForOptimization(ROR2);
126 127 128 129 130
assertEquals(0xF0000FFF | 0, ROR2(0x0000FFFF, 28));
assertEquals(0xF0000FFF | 0, ROR2(0x0000FFFF, 28));
%OptimizeFunctionOnNextCall(ROR2);
assertEquals(0xF0000FFF | 0, ROR2(0x0000FFFF, 28));

131
%PrepareFunctionForOptimization(ROR2);
132 133 134 135 136
assertEquals(0x0FFFF000 | 0, ROR2(0x0000FFFF, 12));
assertEquals(0x0FFFF000 | 0, ROR2(0x0000FFFF, 12));
%OptimizeFunctionOnNextCall(ROR2);
assertEquals(0x0FFFF000 | 0, ROR2(0x0000FFFF, 12));

137
%PrepareFunctionForOptimization(ROR2);
138 139 140 141 142 143
assertEquals(0x0FFFF000 | 0, ROR2(0x0000FFFF, Twelve()));
assertEquals(0x0FFFF000 | 0, ROR2(0x0000FFFF, Twelve()));
%OptimizeFunctionOnNextCall(ROR2);
assertEquals(0x0FFFF000 | 0, ROR2(0x0000FFFF, Twelve()));

for (var i = 0; i <= 100; i++) {
144
  %PrepareFunctionForOptimization(ROR2);
145 146 147 148 149 150 151
  assertEquals(0xFFFFFFFF | 0, ROR2(0xFFFFFFFF, i));
  assertEquals(0xFFFFFFFF | 0, ROR2(0xFFFFFFFF, i));
  %OptimizeFunctionOnNextCall(ROR2);
  assertEquals(0xFFFFFFFF | 0, ROR2(0xFFFFFFFF, i));
}

for (var i = 0; i <= 100; i++) {
152
  %PrepareFunctionForOptimization(ROR2);
153 154 155 156 157 158 159
  assertEquals(-1, ROR2(-1, i));
  assertEquals(-1, ROR2(-1, i));
  %OptimizeFunctionOnNextCall(ROR2);
  assertEquals(-1, ROR2(-1, i));
}

for (var i = 0; i <= 100; i++) {
160
  %PrepareFunctionForOptimization(ROR2);
161 162 163 164 165 166
  assertEquals(1 << ((i % 32)), ROR2(1, i));
  assertEquals(1 << ((i % 32)), ROR2(1, i));
  %OptimizeFunctionOnNextCall(ROR2);
  assertEquals(1 << ((i % 32)), ROR2(1, i));
}

167
%PrepareFunctionForOptimization(ROR3);
168 169 170 171 172
assertEquals(0xF0000FFF | 0, ROR3(0x0000FFFF, 4));
assertEquals(0xF0000FFF | 0, ROR3(0x0000FFFF, 4));
%OptimizeFunctionOnNextCall(ROR3);
assertEquals(0xF0000FFF | 0, ROR3(0x0000FFFF, 4));

173
%PrepareFunctionForOptimization(ROR3);
174 175 176 177 178
assertEquals(0x0FFFF000 | 0, ROR3(0x0000FFFF, 20));
assertEquals(0x0FFFF000 | 0, ROR3(0x0000FFFF, 20));
%OptimizeFunctionOnNextCall(ROR3);
assertEquals(0x0FFFF000 | 0, ROR3(0x0000FFFF, 20));

179
%PrepareFunctionForOptimization(ROR3);
180 181 182 183 184 185
assertEquals(0x0FFFF000 | 0, ROR3(0x0000FFFF, Twenty()));
assertEquals(0x0FFFF000 | 0, ROR3(0x0000FFFF, Twenty()));
%OptimizeFunctionOnNextCall(ROR3);
assertEquals(0x0FFFF000 | 0, ROR3(0x0000FFFF, Twenty()));

for (var i = 0; i <= 100; i++) {
186
  %PrepareFunctionForOptimization(ROR3);
187 188 189 190 191 192 193
  assertEquals(0xFFFFFFFF | 0, ROR3(0xFFFFFFFF, i));
  assertEquals(0xFFFFFFFF | 0, ROR3(0xFFFFFFFF, i));
  %OptimizeFunctionOnNextCall(ROR3);
  assertEquals(0xFFFFFFFF | 0, ROR3(0xFFFFFFFF, i));
}

for (var i = 0; i <= 100; i++) {
194
  %PrepareFunctionForOptimization(ROR3);
195 196 197 198 199 200 201
  assertEquals(-1, ROR3(-1, i));
  assertEquals(-1, ROR3(-1, i));
  %OptimizeFunctionOnNextCall(ROR3);
  assertEquals(-1, ROR3(-1, i));
}

for (var i = 0; i <= 100; i++) {
202
  %PrepareFunctionForOptimization(ROR3);
203 204 205 206 207 208
  assertEquals(1 << (32 - (i % 32)), ROR3(1, i));
  assertEquals(1 << (32 - (i % 32)), ROR3(1, i));
  %OptimizeFunctionOnNextCall(ROR3);
  assertEquals(1 << (32 - (i % 32)), ROR3(1, i));
}

209
%PrepareFunctionForOptimization(ROR4);
210 211 212 213 214
assertEquals(0xF0000FFF | 0, ROR4(0x0000FFFF, 28));
assertEquals(0xF0000FFF | 0, ROR4(0x0000FFFF, 28));
%OptimizeFunctionOnNextCall(ROR4);
assertEquals(0xF0000FFF | 0, ROR4(0x0000FFFF, 28));

215
%PrepareFunctionForOptimization(ROR4);
216 217 218 219 220
assertEquals(0x0FFFF000 | 0, ROR4(0x0000FFFF, 12));
assertEquals(0x0FFFF000 | 0, ROR4(0x0000FFFF, 12));
%OptimizeFunctionOnNextCall(ROR4);
assertEquals(0x0FFFF000 | 0, ROR4(0x0000FFFF, 12));

221
%PrepareFunctionForOptimization(ROR4);
222 223 224 225 226 227
assertEquals(0x0FFFF000 | 0, ROR4(0x0000FFFF, Twelve()));
assertEquals(0x0FFFF000 | 0, ROR4(0x0000FFFF, Twelve()));
%OptimizeFunctionOnNextCall(ROR4);
assertEquals(0x0FFFF000 | 0, ROR4(0x0000FFFF, Twelve()));

for (var i = 0; i <= 100; i++) {
228
  %PrepareFunctionForOptimization(ROR4);
229 230 231 232 233 234 235
  assertEquals(0xFFFFFFFF | 0, ROR4(0xFFFFFFFF, i));
  assertEquals(0xFFFFFFFF | 0, ROR4(0xFFFFFFFF, i));
  %OptimizeFunctionOnNextCall(ROR4);
  assertEquals(0xFFFFFFFF | 0, ROR4(0xFFFFFFFF, i));
}

for (var i = 0; i <= 100; i++) {
236
  %PrepareFunctionForOptimization(ROR4);
237 238 239 240 241 242 243
  assertEquals(-1, ROR4(-1, i));
  assertEquals(-1, ROR4(-1, i));
  %OptimizeFunctionOnNextCall(ROR4);
  assertEquals(-1, ROR4(-1, i));
}

for (var i = 0; i <= 100; i++) {
244
  %PrepareFunctionForOptimization(ROR4);
245 246 247 248 249 250
  assertEquals(1 << ((i % 32)), ROR4(1, i));
  assertEquals(1 << ((i % 32)), ROR4(1, i));
  %OptimizeFunctionOnNextCall(ROR4);
  assertEquals(1 << ((i % 32)), ROR4(1, i));
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
//---------------------------------------------------------
// add test cases for constant operand
//---------------------------------------------------------
// constant operand: 20
function ROR1_sa20(x) {
  return (x >>> 20) | (x << 12);
}

function ROR2_sa20(x) {
  return (x >>> 12) | (x << 20);
}

function ROR3_sa20(x, sa) {
  return (x << 12) | (x >>> 20);
}

function ROR4_sa20(x) {
  return (x << 20) | (x >>> 12);
}

// constant operand: 40
function ROR1_sa40(x) {
  return (x >>> 40) | (x << -8);
}

function ROR2_sa40(x) {
  return (x >>> -8) | (x << 40);
}

function ROR3_sa40(x, sa) {
  return (x << -8) | (x >>> 40);
}

function ROR4_sa40(x) {
  return (x << 40) | (x >>> -8);
}

// ROR1_sa20
289
%PrepareFunctionForOptimization(ROR1_sa20);
290 291 292 293 294 295
assertEquals(ROR1(0x0000FFFF, 20), ROR1_sa20(0x0000FFFF));
assertEquals(ROR1(0x0000FFFF, 20), ROR1_sa20(0x0000FFFF));
%OptimizeFunctionOnNextCall(ROR1_sa20);
assertEquals(ROR1(0x0000FFFF, 20), ROR1_sa20(0x0000FFFF));

// ROR1_sa40
296
%PrepareFunctionForOptimization(ROR1_sa40);
297 298 299 300 301 302
assertEquals(ROR1(0x0000FFFF, 40), ROR1_sa40(0x0000FFFF));
assertEquals(ROR1(0x0000FFFF, 40), ROR1_sa40(0x0000FFFF));
%OptimizeFunctionOnNextCall(ROR1_sa40);
assertEquals(ROR1(0x0000FFFF, 40), ROR1_sa40(0x0000FFFF));

// ROR2_sa20
303
%PrepareFunctionForOptimization(ROR2_sa20);
304 305 306 307 308 309
assertEquals(ROR2(0xFFFFFFFF, 20), ROR2_sa20(0xFFFFFFFF));
assertEquals(ROR2(0xFFFFFFFF, 20), ROR2_sa20(0xFFFFFFFF));
%OptimizeFunctionOnNextCall(ROR2_sa20);
assertEquals(ROR2(0xFFFFFFFF, 20), ROR2_sa20(0xFFFFFFFF));

// ROR2_sa40
310
%PrepareFunctionForOptimization(ROR2_sa40);
311 312 313 314 315 316
assertEquals(ROR2(0x0000FFFF, 40), ROR2_sa40(0x0000FFFF));
assertEquals(ROR2(0x0000FFFF, 40), ROR2_sa40(0x0000FFFF));
%OptimizeFunctionOnNextCall(ROR2_sa40);
assertEquals(ROR2(0x0000FFFF, 40), ROR2_sa40(0x0000FFFF));

// ROR3_sa20
317
%PrepareFunctionForOptimization(ROR3_sa20);
318 319 320 321 322 323
assertEquals(ROR3(0x0000FFFF, 20), ROR3_sa20(0x0000FFFF));
assertEquals(ROR3(0x0000FFFF, 20), ROR3_sa20(0x0000FFFF));
%OptimizeFunctionOnNextCall(ROR3_sa20);
assertEquals(ROR3(0x0000FFFF, 20), ROR3_sa20(0x0000FFFF));

// ROR3_sa40
324
%PrepareFunctionForOptimization(ROR3_sa40);
325 326 327 328 329 330
assertEquals(ROR3(0x0000FFFF, 40), ROR3_sa40(0x0000FFFF));
assertEquals(ROR3(0x0000FFFF, 40), ROR3_sa40(0x0000FFFF));
%OptimizeFunctionOnNextCall(ROR3_sa40);
assertEquals(ROR3(0x0000FFFF, 40), ROR3_sa40(0x0000FFFF));

// ROR4_sa20
331
%PrepareFunctionForOptimization(ROR4_sa20);
332 333 334 335 336 337
assertEquals(ROR4(0x0000FFFF, 20), ROR4_sa20(0x0000FFFF));
assertEquals(ROR4(0x0000FFFF, 20), ROR4_sa20(0x0000FFFF));
%OptimizeFunctionOnNextCall(ROR4_sa20);
assertEquals(ROR4(0x0000FFFF, 20), ROR4_sa20(0x0000FFFF));

// ROR4_sa40
338
%PrepareFunctionForOptimization(ROR4_sa40);
339 340 341 342
assertEquals(ROR4(0xFFFFFFFF, 40), ROR4_sa40(0xFFFFFFFF));
assertEquals(ROR4(0xFFFFFFFF, 40), ROR4_sa40(0xFFFFFFFF));
%OptimizeFunctionOnNextCall(ROR4_sa40);
assertEquals(ROR4(0xFFFFFFFF, 40), ROR4_sa40(0xFFFFFFFF));