1 /*
   2  * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8004867
  27  * @summary VM crashing with assert "share/vm/opto/node.hpp:357 - assert(i < _max) failed: oob"
  28  * @modules java.base/jdk.internal.misc
  29  *
  30  * @run main/othervm/timeout=300 -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation
  31  *    -XX:-OptimizeFill
  32  *    compiler.c2.cr8004867.TestIntUnsafeCAS
  33  * @run main/othervm/timeout=300 -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation
  34  *    -XX:+OptimizeFill
  35  *    compiler.c2.cr8004867.TestIntUnsafeCAS
  36  */
  37 
  38 package compiler.c2.cr8004867;
  39 
  40 import jdk.internal.misc.Unsafe;
  41 
  42 import java.lang.reflect.Field;
  43 
  44 public class TestIntUnsafeCAS {
  45   private static final int ARRLEN = 97;
  46   private static final int ITERS  = 11000;
  47   private static final int OFFSET = 3;
  48   private static final int SCALE = 2;
  49   private static final int ALIGN_OFF = 8;
  50   private static final int UNALIGN_OFF = 5;
  51 
  52   private static final Unsafe unsafe = Unsafe.getUnsafe();
  53   private static final int BASE;
  54   static {
  55     try {
  56       BASE = unsafe.arrayBaseOffset(int[].class);
  57     } catch (Exception e) {
  58       InternalError err = new InternalError();
  59       err.initCause(e);
  60       throw err;
  61     }
  62   }
  63 
  64   public static void main(String args[]) {
  65     System.out.println("Testing Integer array unsafe CAS operations");
  66     int errn = test(false);
  67     if (errn > 0) {
  68       System.err.println("FAILED: " + errn + " errors");
  69       System.exit(97);
  70     }
  71     System.out.println("PASSED");
  72   }
  73 
  74   static int test(boolean test_only) {
  75     int[] a1 = new int[ARRLEN];
  76     int[] a2 = new int[ARRLEN];
  77     // Initialize
  78     for (int i=0; i<ARRLEN; i++) {
  79       a1[i] = -1;
  80       a2[i] = -1;
  81     }
  82     System.out.println("Warmup");
  83     for (int i=0; i<ITERS; i++) {
  84       test_ci(a1);
  85       test_vi(a2, 123, -1);
  86       test_cp(a1, a2);
  87       test_2ci(a1, a2);
  88       test_2vi(a1, a2, 123, 103);
  89       test_ci_neg(a1, 123);
  90       test_vi_neg(a2, 123, 103);
  91       test_cp_neg(a1, a2);
  92       test_2ci_neg(a1, a2);
  93       test_2vi_neg(a1, a2, 123, 103);
  94       test_ci_oppos(a1, 123);
  95       test_vi_oppos(a2, 123, 103);
  96       test_cp_oppos(a1, a2);
  97       test_2ci_oppos(a1, a2);
  98       test_2vi_oppos(a1, a2, 123, 103);
  99       test_ci_off(a1, 123);
 100       test_vi_off(a2, 123, 103);
 101       test_cp_off(a1, a2);
 102       test_2ci_off(a1, a2);
 103       test_2vi_off(a1, a2, 123, 103);
 104       test_ci_inv(a1, OFFSET, 123);
 105       test_vi_inv(a2, 123, OFFSET, 103);
 106       test_cp_inv(a1, a2, OFFSET);
 107       test_2ci_inv(a1, a2, OFFSET);
 108       test_2vi_inv(a1, a2, 123, 103, OFFSET);
 109       test_ci_scl(a1, 123);
 110       test_vi_scl(a2, 123, 103);
 111       test_cp_scl(a1, a2);
 112       test_2ci_scl(a1, a2);
 113       test_2vi_scl(a1, a2, 123, 103);
 114       test_cp_alndst(a1, a2);
 115       test_cp_alnsrc(a1, a2);
 116       test_2ci_aln(a1, a2);
 117       test_2vi_aln(a1, a2, 123, 103);
 118       test_cp_unalndst(a1, a2);
 119       test_cp_unalnsrc(a1, a2);
 120       test_2ci_unaln(a1, a2);
 121       test_2vi_unaln(a1, a2, 123, 103);
 122     }
 123     // Initialize
 124     for (int i=0; i<ARRLEN; i++) {
 125       a1[i] = -1;
 126       a2[i] = -1;
 127     }
 128     // Test and verify results
 129     System.out.println("Verification");
 130     int errn = 0;
 131     {
 132       test_ci(a1);
 133       for (int i=0; i<ARRLEN; i++) {
 134         errn += verify("test_ci: a1", i, a1[i], -123);
 135       }
 136       test_vi(a2, 123, -1);
 137       for (int i=0; i<ARRLEN; i++) {
 138         errn += verify("test_vi: a2", i, a2[i], 123);
 139       }
 140       test_cp(a1, a2);
 141       for (int i=0; i<ARRLEN; i++) {
 142         errn += verify("test_cp: a1", i, a1[i], 123);
 143       }
 144       test_2ci(a1, a2);
 145       for (int i=0; i<ARRLEN; i++) {
 146         errn += verify("test_2ci: a1", i, a1[i], -123);
 147         errn += verify("test_2ci: a2", i, a2[i], -103);
 148       }
 149       test_2vi(a1, a2, 123, 103);
 150       for (int i=0; i<ARRLEN; i++) {
 151         errn += verify("test_2vi: a1", i, a1[i], 123);
 152         errn += verify("test_2vi: a2", i, a2[i], 103);
 153       }
 154       // Reset for negative stride
 155       for (int i=0; i<ARRLEN; i++) {
 156         a1[i] = -1;
 157         a2[i] = -1;
 158       }
 159       test_ci_neg(a1, -1);
 160       for (int i=0; i<ARRLEN; i++) {
 161         errn += verify("test_ci_neg: a1", i, a1[i], -123);
 162       }
 163       test_vi_neg(a2, 123, -1);
 164       for (int i=0; i<ARRLEN; i++) {
 165         errn += verify("test_vi_neg: a2", i, a2[i], 123);
 166       }
 167       test_cp_neg(a1, a2);
 168       for (int i=0; i<ARRLEN; i++) {
 169         errn += verify("test_cp_neg: a1", i, a1[i], 123);
 170       }
 171       test_2ci_neg(a1, a2);
 172       for (int i=0; i<ARRLEN; i++) {
 173         errn += verify("test_2ci_neg: a1", i, a1[i], -123);
 174         errn += verify("test_2ci_neg: a2", i, a2[i], -103);
 175       }
 176       test_2vi_neg(a1, a2, 123, 103);
 177       for (int i=0; i<ARRLEN; i++) {
 178         errn += verify("test_2vi_neg: a1", i, a1[i], 123);
 179         errn += verify("test_2vi_neg: a2", i, a2[i], 103);
 180       }
 181       // Reset for opposite stride
 182       for (int i=0; i<ARRLEN; i++) {
 183         a1[i] = -1;
 184         a2[i] = -1;
 185       }
 186       test_ci_oppos(a1, -1);
 187       for (int i=0; i<ARRLEN; i++) {
 188         errn += verify("test_ci_oppos: a1", i, a1[i], -123);
 189       }
 190       test_vi_oppos(a2, 123, -1);
 191       for (int i=0; i<ARRLEN; i++) {
 192         errn += verify("test_vi_oppos: a2", i, a2[i], 123);
 193       }
 194       test_cp_oppos(a1, a2);
 195       for (int i=0; i<ARRLEN; i++) {
 196         errn += verify("test_cp_oppos: a1", i, a1[i], 123);
 197       }
 198       test_2ci_oppos(a1, a2);
 199       for (int i=0; i<ARRLEN; i++) {
 200         errn += verify("test_2ci_oppos: a1", i, a1[i], -123);
 201         errn += verify("test_2ci_oppos: a2", i, a2[i], -103);
 202       }
 203       test_2vi_oppos(a1, a2, 123, 103);
 204       for (int i=0; i<ARRLEN; i++) {
 205         errn += verify("test_2vi_oppos: a1", i, a1[i], 123);
 206         errn += verify("test_2vi_oppos: a2", i, a2[i], 103);
 207       }
 208       // Reset for indexing with offset
 209       for (int i=0; i<ARRLEN; i++) {
 210         a1[i] = -1;
 211         a2[i] = -1;
 212       }
 213       test_ci_off(a1, -1);
 214       for (int i=OFFSET; i<ARRLEN; i++) {
 215         errn += verify("test_ci_off: a1", i, a1[i], -123);
 216       }
 217       test_vi_off(a2, 123, -1);
 218       for (int i=OFFSET; i<ARRLEN; i++) {
 219         errn += verify("test_vi_off: a2", i, a2[i], 123);
 220       }
 221       test_cp_off(a1, a2);
 222       for (int i=OFFSET; i<ARRLEN; i++) {
 223         errn += verify("test_cp_off: a1", i, a1[i], 123);
 224       }
 225       test_2ci_off(a1, a2);
 226       for (int i=OFFSET; i<ARRLEN; i++) {
 227         errn += verify("test_2ci_off: a1", i, a1[i], -123);
 228         errn += verify("test_2ci_off: a2", i, a2[i], -103);
 229       }
 230       test_2vi_off(a1, a2, 123, 103);
 231       for (int i=OFFSET; i<ARRLEN; i++) {
 232         errn += verify("test_2vi_off: a1", i, a1[i], 123);
 233         errn += verify("test_2vi_off: a2", i, a2[i], 103);
 234       }
 235       for (int i=0; i<OFFSET; i++) {
 236         errn += verify("test_2vi_off: a1", i, a1[i], -1);
 237         errn += verify("test_2vi_off: a2", i, a2[i], -1);
 238       }
 239       // Reset for indexing with invariant offset
 240       for (int i=0; i<ARRLEN; i++) {
 241         a1[i] = -1;
 242         a2[i] = -1;
 243       }
 244       test_ci_inv(a1, OFFSET, -1);
 245       for (int i=OFFSET; i<ARRLEN; i++) {
 246         errn += verify("test_ci_inv: a1", i, a1[i], -123);
 247       }
 248       test_vi_inv(a2, 123, OFFSET, -1);
 249       for (int i=OFFSET; i<ARRLEN; i++) {
 250         errn += verify("test_vi_inv: a2", i, a2[i], 123);
 251       }
 252       test_cp_inv(a1, a2, OFFSET);
 253       for (int i=OFFSET; i<ARRLEN; i++) {
 254         errn += verify("test_cp_inv: a1", i, a1[i], 123);
 255       }
 256       test_2ci_inv(a1, a2, OFFSET);
 257       for (int i=OFFSET; i<ARRLEN; i++) {
 258         errn += verify("test_2ci_inv: a1", i, a1[i], -123);
 259         errn += verify("test_2ci_inv: a2", i, a2[i], -103);
 260       }
 261       test_2vi_inv(a1, a2, 123, 103, OFFSET);
 262       for (int i=OFFSET; i<ARRLEN; i++) {
 263         errn += verify("test_2vi_inv: a1", i, a1[i], 123);
 264         errn += verify("test_2vi_inv: a2", i, a2[i], 103);
 265       }
 266       for (int i=0; i<OFFSET; i++) {
 267         errn += verify("test_2vi_inv: a1", i, a1[i], -1);
 268         errn += verify("test_2vi_inv: a2", i, a2[i], -1);
 269       }
 270       // Reset for indexing with scale
 271       for (int i=0; i<ARRLEN; i++) {
 272         a1[i] = -1;
 273         a2[i] = -1;
 274       }
 275       test_ci_scl(a1, -1);
 276       for (int i=0; i<ARRLEN; i++) {
 277         int val = (i%SCALE != 0) ? -1 : -123;
 278         errn += verify("test_ci_scl: a1", i, a1[i], val);
 279       }
 280       test_vi_scl(a2, 123, -1);
 281       for (int i=0; i<ARRLEN; i++) {
 282         int val = (i%SCALE != 0) ? -1 : 123;
 283         errn += verify("test_vi_scl: a2", i, a2[i], val);
 284       }
 285       test_cp_scl(a1, a2);
 286       for (int i=0; i<ARRLEN; i++) {
 287         int val = (i%SCALE != 0) ? -1 : 123;
 288         errn += verify("test_cp_scl: a1", i, a1[i], val);
 289       }
 290       test_2ci_scl(a1, a2);
 291       for (int i=0; i<ARRLEN; i++) {
 292         if (i%SCALE != 0) {
 293           errn += verify("test_2ci_scl: a1", i, a1[i], -1);
 294         } else if (i*SCALE < ARRLEN) {
 295           errn += verify("test_2ci_scl: a1", i*SCALE, a1[i*SCALE], -123);
 296         }
 297         if (i%SCALE != 0) {
 298           errn += verify("test_2ci_scl: a2", i, a2[i], -1);
 299         } else if (i*SCALE < ARRLEN) {
 300           errn += verify("test_2ci_scl: a2", i*SCALE, a2[i*SCALE], -103);
 301         }
 302       }
 303       test_2vi_scl(a1, a2, 123, 103);
 304       for (int i=0; i<ARRLEN; i++) {
 305         if (i%SCALE != 0) {
 306           errn += verify("test_2vi_scl: a1", i, a1[i], -1);
 307         } else if (i*SCALE < ARRLEN) {
 308           errn += verify("test_2vi_scl: a1", i*SCALE, a1[i*SCALE], 123);
 309         }
 310         if (i%SCALE != 0) {
 311           errn += verify("test_2vi_scl: a2", i, a2[i], -1);
 312         } else if (i*SCALE < ARRLEN) {
 313           errn += verify("test_2vi_scl: a2", i*SCALE, a2[i*SCALE], 103);
 314         }
 315       }
 316       // Reset for 2 arrays with relative aligned offset
 317       for (int i=0; i<ARRLEN; i++) {
 318         a1[i] = -1;
 319         a2[i] = -1;
 320       }
 321       test_vi(a2, 123, -1);
 322       test_cp_alndst(a1, a2);
 323       for (int i=0; i<ALIGN_OFF; i++) {
 324         errn += verify("test_cp_alndst: a1", i, a1[i], -1);
 325       }
 326       for (int i=ALIGN_OFF; i<ARRLEN; i++) {
 327         errn += verify("test_cp_alndst: a1", i, a1[i], 123);
 328       }
 329       for (int i=0; i<ALIGN_OFF; i++) {
 330         a1[i] = 123;
 331       }
 332       test_vi(a2, -123, 123);
 333       test_cp_alnsrc(a1, a2);
 334       for (int i=0; i<ARRLEN-ALIGN_OFF; i++) {
 335         errn += verify("test_cp_alnsrc: a1", i, a1[i], -123);
 336       }
 337       for (int i=ARRLEN-ALIGN_OFF; i<ARRLEN; i++) {
 338         errn += verify("test_cp_alnsrc: a1", i, a1[i], 123);
 339       }
 340       for (int i=0; i<ARRLEN; i++) {
 341         a1[i] = -1;
 342         a2[i] = -1;
 343       }
 344       test_2ci_aln(a1, a2);
 345       for (int i=0; i<ALIGN_OFF; i++) {
 346         errn += verify("test_2ci_aln: a1", i, a1[i], -1);
 347       }
 348       for (int i=ALIGN_OFF; i<ARRLEN; i++) {
 349         errn += verify("test_2ci_aln: a1", i, a1[i], -123);
 350       }
 351       for (int i=0; i<ARRLEN-ALIGN_OFF; i++) {
 352         errn += verify("test_2ci_aln: a2", i, a2[i], -103);
 353       }
 354       for (int i=ARRLEN-ALIGN_OFF; i<ARRLEN; i++) {
 355         errn += verify("test_2ci_aln: a2", i, a2[i], -1);
 356       }
 357       for (int i=0; i<ARRLEN; i++) {
 358         a1[i] = -1;
 359         a2[i] = -1;
 360       }
 361       test_2vi_aln(a1, a2, 123, 103);
 362       for (int i=0; i<ARRLEN-ALIGN_OFF; i++) {
 363         errn += verify("test_2vi_aln: a1", i, a1[i], 123);
 364       }
 365       for (int i=ARRLEN-ALIGN_OFF; i<ARRLEN; i++) {
 366         errn += verify("test_2vi_aln: a1", i, a1[i], -1);
 367       }
 368       for (int i=0; i<ALIGN_OFF; i++) {
 369         errn += verify("test_2vi_aln: a2", i, a2[i], -1);
 370       }
 371       for (int i=ALIGN_OFF; i<ARRLEN; i++) {
 372         errn += verify("test_2vi_aln: a2", i, a2[i], 103);
 373       }
 374 
 375       // Reset for 2 arrays with relative unaligned offset
 376       for (int i=0; i<ARRLEN; i++) {
 377         a1[i] = -1;
 378         a2[i] = -1;
 379       }
 380       test_vi(a2, 123, -1);
 381       test_cp_unalndst(a1, a2);
 382       for (int i=0; i<UNALIGN_OFF; i++) {
 383         errn += verify("test_cp_unalndst: a1", i, a1[i], -1);
 384       }
 385       for (int i=UNALIGN_OFF; i<ARRLEN; i++) {
 386         errn += verify("test_cp_unalndst: a1", i, a1[i], 123);
 387       }
 388       test_vi(a2, -123, 123);
 389       test_cp_unalnsrc(a1, a2);
 390       for (int i=0; i<ARRLEN-UNALIGN_OFF; i++) {
 391         errn += verify("test_cp_unalnsrc: a1", i, a1[i], -123);
 392       }
 393       for (int i=ARRLEN-UNALIGN_OFF; i<ARRLEN; i++) {
 394         errn += verify("test_cp_unalnsrc: a1", i, a1[i], 123);
 395       }
 396       for (int i=0; i<ARRLEN; i++) {
 397         a1[i] = -1;
 398         a2[i] = -1;
 399       }
 400       test_2ci_unaln(a1, a2);
 401       for (int i=0; i<UNALIGN_OFF; i++) {
 402         errn += verify("test_2ci_unaln: a1", i, a1[i], -1);
 403       }
 404       for (int i=UNALIGN_OFF; i<ARRLEN; i++) {
 405         errn += verify("test_2ci_unaln: a1", i, a1[i], -123);
 406       }
 407       for (int i=0; i<ARRLEN-UNALIGN_OFF; i++) {
 408         errn += verify("test_2ci_unaln: a2", i, a2[i], -103);
 409       }
 410       for (int i=ARRLEN-UNALIGN_OFF; i<ARRLEN; i++) {
 411         errn += verify("test_2ci_unaln: a2", i, a2[i], -1);
 412       }
 413       for (int i=0; i<ARRLEN; i++) {
 414         a1[i] = -1;
 415         a2[i] = -1;
 416       }
 417       test_2vi_unaln(a1, a2, 123, 103);
 418       for (int i=0; i<ARRLEN-UNALIGN_OFF; i++) {
 419         errn += verify("test_2vi_unaln: a1", i, a1[i], 123);
 420       }
 421       for (int i=ARRLEN-UNALIGN_OFF; i<ARRLEN; i++) {
 422         errn += verify("test_2vi_unaln: a1", i, a1[i], -1);
 423       }
 424       for (int i=0; i<UNALIGN_OFF; i++) {
 425         errn += verify("test_2vi_unaln: a2", i, a2[i], -1);
 426       }
 427       for (int i=UNALIGN_OFF; i<ARRLEN; i++) {
 428         errn += verify("test_2vi_unaln: a2", i, a2[i], 103);
 429       }
 430 
 431       // Reset for aligned overlap initialization
 432       for (int i=0; i<ALIGN_OFF; i++) {
 433         a1[i] = i;
 434       }
 435       for (int i=ALIGN_OFF; i<ARRLEN; i++) {
 436         a1[i] = -1;
 437       }
 438       test_cp_alndst(a1, a1);
 439       for (int i=0; i<ARRLEN; i++) {
 440         int v = i%ALIGN_OFF;
 441         errn += verify("test_cp_alndst_overlap: a1", i, a1[i], v);
 442       }
 443       for (int i=0; i<ALIGN_OFF; i++) {
 444         a1[i+ALIGN_OFF] = -1;
 445       }
 446       test_cp_alnsrc(a1, a1);
 447       for (int i=0; i<ALIGN_OFF; i++) {
 448         errn += verify("test_cp_alnsrc_overlap: a1", i, a1[i], -1);
 449       }
 450       for (int i=ALIGN_OFF; i<ARRLEN; i++) {
 451         int v = i%ALIGN_OFF;
 452         errn += verify("test_cp_alnsrc_overlap: a1", i, a1[i], v);
 453       }
 454       for (int i=0; i<ARRLEN; i++) {
 455         a1[i] = -1;
 456       }
 457       test_2ci_aln(a1, a1);
 458       for (int i=0; i<ARRLEN-ALIGN_OFF; i++) {
 459         errn += verify("test_2ci_aln_overlap: a1", i, a1[i], -103);
 460       }
 461       for (int i=ARRLEN-ALIGN_OFF; i<ARRLEN; i++) {
 462         errn += verify("test_2ci_aln_overlap: a1", i, a1[i], -123);
 463       }
 464       for (int i=0; i<ARRLEN; i++) {
 465         a1[i] = -1;
 466       }
 467       test_2vi_aln(a1, a1, 123, 103);
 468       for (int i=0; i<ARRLEN-ALIGN_OFF; i++) {
 469         errn += verify("test_2vi_aln_overlap: a1", i, a1[i], 123);
 470       }
 471       for (int i=ARRLEN-ALIGN_OFF; i<ARRLEN; i++) {
 472         errn += verify("test_2vi_aln_overlap: a1", i, a1[i], 103);
 473       }
 474 
 475       // Reset for unaligned overlap initialization
 476       for (int i=0; i<UNALIGN_OFF; i++) {
 477         a1[i] = i;
 478       }
 479       for (int i=UNALIGN_OFF; i<ARRLEN; i++) {
 480         a1[i] = -1;
 481       }
 482       test_cp_unalndst(a1, a1);
 483       for (int i=0; i<ARRLEN; i++) {
 484         int v = i%UNALIGN_OFF;
 485         errn += verify("test_cp_unalndst_overlap: a1", i, a1[i], v);
 486       }
 487       for (int i=0; i<UNALIGN_OFF; i++) {
 488         a1[i+UNALIGN_OFF] = -1;
 489       }
 490       test_cp_unalnsrc(a1, a1);
 491       for (int i=0; i<UNALIGN_OFF; i++) {
 492         errn += verify("test_cp_unalnsrc_overlap: a1", i, a1[i], -1);
 493       }
 494       for (int i=UNALIGN_OFF; i<ARRLEN; i++) {
 495         int v = i%UNALIGN_OFF;
 496         errn += verify("test_cp_unalnsrc_overlap: a1", i, a1[i], v);
 497       }
 498       for (int i=0; i<ARRLEN; i++) {
 499         a1[i] = -1;
 500       }
 501       test_2ci_unaln(a1, a1);
 502       for (int i=0; i<ARRLEN-UNALIGN_OFF; i++) {
 503         errn += verify("test_2ci_unaln_overlap: a1", i, a1[i], -103);
 504       }
 505       for (int i=ARRLEN-UNALIGN_OFF; i<ARRLEN; i++) {
 506         errn += verify("test_2ci_unaln_overlap: a1", i, a1[i], -123);
 507       }
 508       for (int i=0; i<ARRLEN; i++) {
 509         a1[i] = -1;
 510       }
 511       test_2vi_unaln(a1, a1, 123, 103);
 512       for (int i=0; i<ARRLEN-UNALIGN_OFF; i++) {
 513         errn += verify("test_2vi_unaln_overlap: a1", i, a1[i], 123);
 514       }
 515       for (int i=ARRLEN-UNALIGN_OFF; i<ARRLEN; i++) {
 516         errn += verify("test_2vi_unaln_overlap: a1", i, a1[i], 103);
 517       }
 518 
 519     }
 520 
 521     if (errn > 0 || test_only)
 522       return errn;
 523 
 524     // Initialize
 525     for (int i=0; i<ARRLEN; i++) {
 526       a1[i] = -1;
 527       a2[i] = -1;
 528     }
 529     System.out.println("Time");
 530     long start, end;
 531     start = System.currentTimeMillis();
 532     for (int i=0; i<ITERS; i++) {
 533       test_ci(a1);
 534     }
 535     end = System.currentTimeMillis();
 536     System.out.println("test_ci: " + (end - start));
 537     start = System.currentTimeMillis();
 538     for (int i=0; i<ITERS; i++) {
 539       test_vi(a2, 123, -1);
 540     }
 541     end = System.currentTimeMillis();
 542     System.out.println("test_vi: " + (end - start));
 543     start = System.currentTimeMillis();
 544     for (int i=0; i<ITERS; i++) {
 545       test_cp(a1, a2);
 546     }
 547     end = System.currentTimeMillis();
 548     System.out.println("test_cp: " + (end - start));
 549     start = System.currentTimeMillis();
 550     for (int i=0; i<ITERS; i++) {
 551       test_2ci(a1, a2);
 552     }
 553     end = System.currentTimeMillis();
 554     System.out.println("test_2ci: " + (end - start));
 555     start = System.currentTimeMillis();
 556     for (int i=0; i<ITERS; i++) {
 557       test_2vi(a1, a2, 123, 103);
 558     }
 559     end = System.currentTimeMillis();
 560     System.out.println("test_2vi: " + (end - start));
 561 
 562     start = System.currentTimeMillis();
 563     for (int i=0; i<ITERS; i++) {
 564       test_ci_neg(a1, 123);
 565     }
 566     end = System.currentTimeMillis();
 567     System.out.println("test_ci_neg: " + (end - start));
 568     start = System.currentTimeMillis();
 569     for (int i=0; i<ITERS; i++) {
 570       test_vi_neg(a2, 123, 103);
 571     }
 572     end = System.currentTimeMillis();
 573     System.out.println("test_vi_neg: " + (end - start));
 574     start = System.currentTimeMillis();
 575     for (int i=0; i<ITERS; i++) {
 576       test_cp_neg(a1, a2);
 577     }
 578     end = System.currentTimeMillis();
 579     System.out.println("test_cp_neg: " + (end - start));
 580     start = System.currentTimeMillis();
 581     for (int i=0; i<ITERS; i++) {
 582       test_2ci_neg(a1, a2);
 583     }
 584     end = System.currentTimeMillis();
 585     System.out.println("test_2ci_neg: " + (end - start));
 586     start = System.currentTimeMillis();
 587     for (int i=0; i<ITERS; i++) {
 588       test_2vi_neg(a1, a2, 123, 103);
 589     }
 590     end = System.currentTimeMillis();
 591     System.out.println("test_2vi_neg: " + (end - start));
 592 
 593     start = System.currentTimeMillis();
 594     for (int i=0; i<ITERS; i++) {
 595       test_ci_oppos(a1, 123);
 596     }
 597     end = System.currentTimeMillis();
 598     System.out.println("test_ci_oppos: " + (end - start));
 599     start = System.currentTimeMillis();
 600     for (int i=0; i<ITERS; i++) {
 601       test_vi_oppos(a2, 123, 103);
 602     }
 603     end = System.currentTimeMillis();
 604     System.out.println("test_vi_oppos: " + (end - start));
 605     start = System.currentTimeMillis();
 606     for (int i=0; i<ITERS; i++) {
 607       test_cp_oppos(a1, a2);
 608     }
 609     end = System.currentTimeMillis();
 610     System.out.println("test_cp_oppos: " + (end - start));
 611     start = System.currentTimeMillis();
 612     for (int i=0; i<ITERS; i++) {
 613       test_2ci_oppos(a1, a2);
 614     }
 615     end = System.currentTimeMillis();
 616     System.out.println("test_2ci_oppos: " + (end - start));
 617     start = System.currentTimeMillis();
 618     for (int i=0; i<ITERS; i++) {
 619       test_2vi_oppos(a1, a2, 123, 103);
 620     }
 621     end = System.currentTimeMillis();
 622     System.out.println("test_2vi_oppos: " + (end - start));
 623 
 624     start = System.currentTimeMillis();
 625     for (int i=0; i<ITERS; i++) {
 626       test_ci_off(a1, 123);
 627     }
 628     end = System.currentTimeMillis();
 629     System.out.println("test_ci_off: " + (end - start));
 630     start = System.currentTimeMillis();
 631     for (int i=0; i<ITERS; i++) {
 632       test_vi_off(a2, 123, 103);
 633     }
 634     end = System.currentTimeMillis();
 635     System.out.println("test_vi_off: " + (end - start));
 636     start = System.currentTimeMillis();
 637     for (int i=0; i<ITERS; i++) {
 638       test_cp_off(a1, a2);
 639     }
 640     end = System.currentTimeMillis();
 641     System.out.println("test_cp_off: " + (end - start));
 642     start = System.currentTimeMillis();
 643     for (int i=0; i<ITERS; i++) {
 644       test_2ci_off(a1, a2);
 645     }
 646     end = System.currentTimeMillis();
 647     System.out.println("test_2ci_off: " + (end - start));
 648     start = System.currentTimeMillis();
 649     for (int i=0; i<ITERS; i++) {
 650       test_2vi_off(a1, a2, 123, 103);
 651     }
 652     end = System.currentTimeMillis();
 653     System.out.println("test_2vi_off: " + (end - start));
 654 
 655     start = System.currentTimeMillis();
 656     for (int i=0; i<ITERS; i++) {
 657       test_ci_inv(a1, OFFSET, 123);
 658     }
 659     end = System.currentTimeMillis();
 660     System.out.println("test_ci_inv: " + (end - start));
 661     start = System.currentTimeMillis();
 662     for (int i=0; i<ITERS; i++) {
 663       test_vi_inv(a2, 123, OFFSET, 103);
 664     }
 665     end = System.currentTimeMillis();
 666     System.out.println("test_vi_inv: " + (end - start));
 667     start = System.currentTimeMillis();
 668     for (int i=0; i<ITERS; i++) {
 669       test_cp_inv(a1, a2, OFFSET);
 670     }
 671     end = System.currentTimeMillis();
 672     System.out.println("test_cp_inv: " + (end - start));
 673     start = System.currentTimeMillis();
 674     for (int i=0; i<ITERS; i++) {
 675       test_2ci_inv(a1, a2, OFFSET);
 676     }
 677     end = System.currentTimeMillis();
 678     System.out.println("test_2ci_inv: " + (end - start));
 679     start = System.currentTimeMillis();
 680     for (int i=0; i<ITERS; i++) {
 681       test_2vi_inv(a1, a2, 123, 103, OFFSET);
 682     }
 683     end = System.currentTimeMillis();
 684     System.out.println("test_2vi_inv: " + (end - start));
 685 
 686     start = System.currentTimeMillis();
 687     for (int i=0; i<ITERS; i++) {
 688       test_ci_scl(a1, 123);
 689     }
 690     end = System.currentTimeMillis();
 691     System.out.println("test_ci_scl: " + (end - start));
 692     start = System.currentTimeMillis();
 693     for (int i=0; i<ITERS; i++) {
 694       test_vi_scl(a2, 123, 103);
 695     }
 696     end = System.currentTimeMillis();
 697     System.out.println("test_vi_scl: " + (end - start));
 698     start = System.currentTimeMillis();
 699     for (int i=0; i<ITERS; i++) {
 700       test_cp_scl(a1, a2);
 701     }
 702     end = System.currentTimeMillis();
 703     System.out.println("test_cp_scl: " + (end - start));
 704     start = System.currentTimeMillis();
 705     for (int i=0; i<ITERS; i++) {
 706       test_2ci_scl(a1, a2);
 707     }
 708     end = System.currentTimeMillis();
 709     System.out.println("test_2ci_scl: " + (end - start));
 710     start = System.currentTimeMillis();
 711     for (int i=0; i<ITERS; i++) {
 712       test_2vi_scl(a1, a2, 123, 103);
 713     }
 714     end = System.currentTimeMillis();
 715     System.out.println("test_2vi_scl: " + (end - start));
 716 
 717     start = System.currentTimeMillis();
 718     for (int i=0; i<ITERS; i++) {
 719       test_cp_alndst(a1, a2);
 720     }
 721     end = System.currentTimeMillis();
 722     System.out.println("test_cp_alndst: " + (end - start));
 723     start = System.currentTimeMillis();
 724     for (int i=0; i<ITERS; i++) {
 725       test_cp_alnsrc(a1, a2);
 726     }
 727     end = System.currentTimeMillis();
 728     System.out.println("test_cp_alnsrc: " + (end - start));
 729     start = System.currentTimeMillis();
 730     for (int i=0; i<ITERS; i++) {
 731       test_2ci_aln(a1, a2);
 732     }
 733     end = System.currentTimeMillis();
 734     System.out.println("test_2ci_aln: " + (end - start));
 735     start = System.currentTimeMillis();
 736     for (int i=0; i<ITERS; i++) {
 737       test_2vi_aln(a1, a2, 123, 103);
 738     }
 739     end = System.currentTimeMillis();
 740     System.out.println("test_2vi_aln: " + (end - start));
 741 
 742     start = System.currentTimeMillis();
 743     for (int i=0; i<ITERS; i++) {
 744       test_cp_unalndst(a1, a2);
 745     }
 746     end = System.currentTimeMillis();
 747     System.out.println("test_cp_unalndst: " + (end - start));
 748     start = System.currentTimeMillis();
 749     for (int i=0; i<ITERS; i++) {
 750       test_cp_unalnsrc(a1, a2);
 751     }
 752     end = System.currentTimeMillis();
 753     System.out.println("test_cp_unalnsrc: " + (end - start));
 754     start = System.currentTimeMillis();
 755     for (int i=0; i<ITERS; i++) {
 756       test_2ci_unaln(a1, a2);
 757     }
 758     end = System.currentTimeMillis();
 759     System.out.println("test_2ci_unaln: " + (end - start));
 760     start = System.currentTimeMillis();
 761     for (int i=0; i<ITERS; i++) {
 762       test_2vi_unaln(a1, a2, 123, 103);
 763     }
 764     end = System.currentTimeMillis();
 765     System.out.println("test_2vi_unaln: " + (end - start));
 766 
 767     return errn;
 768   }
 769 
 770   private final static long byte_offset(int i) {
 771     return ((long)i << 2) + BASE;
 772   }
 773 
 774   static void test_ci(int[] a) {
 775     for (int i = 0; i < ARRLEN; i+=1) {
 776       unsafe.compareAndSwapInt(a, byte_offset(i), -1, -123);
 777     }
 778   }
 779   static void test_vi(int[] a, int b, int old) {
 780     for (int i = 0; i < ARRLEN; i+=1) {
 781       unsafe.compareAndSwapInt(a, byte_offset(i), old, b);
 782     }
 783   }
 784   static void test_cp(int[] a, int[] b) {
 785     for (int i = 0; i < ARRLEN; i+=1) {
 786       unsafe.compareAndSwapInt(a, byte_offset(i), -123, b[i]);
 787     }
 788   }
 789   static void test_2ci(int[] a, int[] b) {
 790     for (int i = 0; i < ARRLEN; i+=1) {
 791       unsafe.compareAndSwapInt(a, byte_offset(i), 123, -123);
 792       unsafe.compareAndSwapInt(b, byte_offset(i), 123, -103);
 793     }
 794   }
 795   static void test_2vi(int[] a, int[] b, int c, int d) {
 796     for (int i = 0; i < ARRLEN; i+=1) {
 797       unsafe.compareAndSwapInt(a, byte_offset(i), -123, c);
 798       unsafe.compareAndSwapInt(b, byte_offset(i), -103, d);
 799     }
 800   }
 801   static void test_ci_neg(int[] a, int old) {
 802     for (int i = ARRLEN-1; i >= 0; i-=1) {
 803       unsafe.compareAndSwapInt(a, byte_offset(i), old, -123);
 804     }
 805   }
 806   static void test_vi_neg(int[] a, int b, int old) {
 807     for (int i = ARRLEN-1; i >= 0; i-=1) {
 808       unsafe.compareAndSwapInt(a, byte_offset(i), old, b);
 809     }
 810   }
 811   static void test_cp_neg(int[] a, int[] b) {
 812     for (int i = ARRLEN-1; i >= 0; i-=1) {
 813       unsafe.compareAndSwapInt(a, byte_offset(i), -123, b[i]);
 814     }
 815   }
 816   static void test_2ci_neg(int[] a, int[] b) {
 817     for (int i = ARRLEN-1; i >= 0; i-=1) {
 818       unsafe.compareAndSwapInt(a, byte_offset(i), 123, -123);
 819       unsafe.compareAndSwapInt(b, byte_offset(i), 123, -103);
 820     }
 821   }
 822   static void test_2vi_neg(int[] a, int[] b, int c, int d) {
 823     for (int i = ARRLEN-1; i >= 0; i-=1) {
 824       unsafe.compareAndSwapInt(a, byte_offset(i), -123, c);
 825       unsafe.compareAndSwapInt(b, byte_offset(i), -103, d);
 826     }
 827   }
 828   static void test_ci_oppos(int[] a, int old) {
 829     int limit = ARRLEN-1;
 830     for (int i = 0; i < ARRLEN; i+=1) {
 831       unsafe.compareAndSwapInt(a, byte_offset(limit-i), old, -123);
 832     }
 833   }
 834   static void test_vi_oppos(int[] a, int b, int old) {
 835     int limit = ARRLEN-1;
 836     for (int i = limit; i >= 0; i-=1) {
 837       unsafe.compareAndSwapInt(a, byte_offset(limit-i), old, b);
 838     }
 839   }
 840   static void test_cp_oppos(int[] a, int[] b) {
 841     int limit = ARRLEN-1;
 842     for (int i = 0; i < ARRLEN; i+=1) {
 843       unsafe.compareAndSwapInt(a, byte_offset(i), -123, b[limit-i]);
 844     }
 845   }
 846   static void test_2ci_oppos(int[] a, int[] b) {
 847     int limit = ARRLEN-1;
 848     for (int i = 0; i < ARRLEN; i+=1) {
 849       unsafe.compareAndSwapInt(a, byte_offset(limit-i), 123, -123);
 850       unsafe.compareAndSwapInt(b, byte_offset(i), 123, -103);
 851     }
 852   }
 853   static void test_2vi_oppos(int[] a, int[] b, int c, int d) {
 854     int limit = ARRLEN-1;
 855     for (int i = limit; i >= 0; i-=1) {
 856       unsafe.compareAndSwapInt(a, byte_offset(i), -123, c);
 857       unsafe.compareAndSwapInt(b, byte_offset(limit-i), -103, d);
 858     }
 859   }
 860   static void test_ci_off(int[] a, int old) {
 861     for (int i = 0; i < ARRLEN-OFFSET; i+=1) {
 862       unsafe.compareAndSwapInt(a, byte_offset(i+OFFSET), old, -123);
 863     }
 864   }
 865   static void test_vi_off(int[] a, int b, int old) {
 866     for (int i = 0; i < ARRLEN-OFFSET; i+=1) {
 867       unsafe.compareAndSwapInt(a, byte_offset(i+OFFSET), old, b);
 868     }
 869   }
 870   static void test_cp_off(int[] a, int[] b) {
 871     for (int i = 0; i < ARRLEN-OFFSET; i+=1) {
 872       unsafe.compareAndSwapInt(a, byte_offset(i+OFFSET), -123, b[i+OFFSET]);
 873     }
 874   }
 875   static void test_2ci_off(int[] a, int[] b) {
 876     for (int i = 0; i < ARRLEN-OFFSET; i+=1) {
 877       unsafe.compareAndSwapInt(a, byte_offset(i+OFFSET), 123, -123);
 878       unsafe.compareAndSwapInt(b, byte_offset(i+OFFSET), 123, -103);
 879     }
 880   }
 881   static void test_2vi_off(int[] a, int[] b, int c, int d) {
 882     for (int i = 0; i < ARRLEN-OFFSET; i+=1) {
 883       unsafe.compareAndSwapInt(a, byte_offset(i+OFFSET), -123, c);
 884       unsafe.compareAndSwapInt(b, byte_offset(i+OFFSET), -103, d);
 885     }
 886   }
 887   static void test_ci_inv(int[] a, int k, int old) {
 888     for (int i = 0; i < ARRLEN-k; i+=1) {
 889       unsafe.compareAndSwapInt(a, byte_offset(i+k), old, -123);
 890     }
 891   }
 892   static void test_vi_inv(int[] a, int b, int k, int old) {
 893     for (int i = 0; i < ARRLEN-k; i+=1) {
 894       unsafe.compareAndSwapInt(a, byte_offset(i+k), old, b);
 895     }
 896   }
 897   static void test_cp_inv(int[] a, int[] b, int k) {
 898     for (int i = 0; i < ARRLEN-k; i+=1) {
 899       unsafe.compareAndSwapInt(a, byte_offset(i+k), -123, b[i+k]);
 900     }
 901   }
 902   static void test_2ci_inv(int[] a, int[] b, int k) {
 903     for (int i = 0; i < ARRLEN-k; i+=1) {
 904       unsafe.compareAndSwapInt(a, byte_offset(i+k), 123, -123);
 905       unsafe.compareAndSwapInt(b, byte_offset(i+k), 123, -103);
 906     }
 907   }
 908   static void test_2vi_inv(int[] a, int[] b, int c, int d, int k) {
 909     for (int i = 0; i < ARRLEN-k; i+=1) {
 910       unsafe.compareAndSwapInt(a, byte_offset(i+k), -123, c);
 911       unsafe.compareAndSwapInt(b, byte_offset(i+k), -103, d);
 912     }
 913   }
 914   static void test_ci_scl(int[] a, int old) {
 915     for (int i = 0; i*SCALE < ARRLEN; i+=1) {
 916       unsafe.compareAndSwapInt(a, byte_offset(i*SCALE), old, -123);
 917     }
 918   }
 919   static void test_vi_scl(int[] a, int b, int old) {
 920     for (int i = 0; i*SCALE < ARRLEN; i+=1) {
 921       unsafe.compareAndSwapInt(a, byte_offset(i*SCALE), old, b);
 922     }
 923   }
 924   static void test_cp_scl(int[] a, int[] b) {
 925     for (int i = 0; i*SCALE < ARRLEN; i+=1) {
 926       unsafe.compareAndSwapInt(a, byte_offset(i*SCALE), -123, b[i*SCALE]);
 927     }
 928   }
 929   static void test_2ci_scl(int[] a, int[] b) {
 930     for (int i = 0; i*SCALE < ARRLEN; i+=1) {
 931       unsafe.compareAndSwapInt(a, byte_offset(i*SCALE), 123, -123);
 932       unsafe.compareAndSwapInt(b, byte_offset(i*SCALE), 123, -103);
 933     }
 934   }
 935   static void test_2vi_scl(int[] a, int[] b, int c, int d) {
 936     for (int i = 0; i*SCALE < ARRLEN; i+=1) {
 937       unsafe.compareAndSwapInt(a, byte_offset(i*SCALE), -123, c);
 938       unsafe.compareAndSwapInt(b, byte_offset(i*SCALE), -103, d);
 939     }
 940   }
 941   static void test_cp_alndst(int[] a, int[] b) {
 942     for (int i = 0; i < ARRLEN-ALIGN_OFF; i+=1) {
 943       unsafe.compareAndSwapInt(a, byte_offset(i+ALIGN_OFF), -1, b[i]);
 944     }
 945   }
 946   static void test_cp_alnsrc(int[] a, int[] b) {
 947     for (int i = 0; i < ARRLEN-ALIGN_OFF; i+=1) {
 948       int old = unsafe.getIntVolatile(a, byte_offset(i));
 949       unsafe.compareAndSwapInt(a, byte_offset(i), old, b[i+ALIGN_OFF]);
 950     }
 951   }
 952   static void test_2ci_aln(int[] a, int[] b) {
 953     for (int i = 0; i < ARRLEN-ALIGN_OFF; i+=1) {
 954       unsafe.compareAndSwapInt(a, byte_offset(i+ALIGN_OFF), -1, -123);
 955       int old = unsafe.getIntVolatile(b, byte_offset(i));
 956       unsafe.compareAndSwapInt(b, byte_offset(i), old, -103);
 957     }
 958   }
 959   static void test_2vi_aln(int[] a, int[] b, int c, int d) {
 960     for (int i = 0; i < ARRLEN-ALIGN_OFF; i+=1) {
 961       int old = unsafe.getIntVolatile(a, byte_offset(i));
 962       unsafe.compareAndSwapInt(a, byte_offset(i), old, c);
 963       old = unsafe.getIntVolatile(b, byte_offset(i+ALIGN_OFF));
 964       unsafe.compareAndSwapInt(b, byte_offset(i+ALIGN_OFF), old, d);
 965     }
 966   }
 967   static void test_cp_unalndst(int[] a, int[] b) {
 968     for (int i = 0; i < ARRLEN-UNALIGN_OFF; i+=1) {
 969       unsafe.compareAndSwapInt(a, byte_offset(i+UNALIGN_OFF), -1, b[i]);
 970     }
 971   }
 972   static void test_cp_unalnsrc(int[] a, int[] b) {
 973     for (int i = 0; i < ARRLEN-UNALIGN_OFF; i+=1) {
 974       int old = unsafe.getIntVolatile(a, byte_offset(i));
 975       unsafe.compareAndSwapInt(a, byte_offset(i), old, b[i+UNALIGN_OFF]);
 976     }
 977   }
 978   static void test_2ci_unaln(int[] a, int[] b) {
 979     for (int i = 0; i < ARRLEN-UNALIGN_OFF; i+=1) {
 980       unsafe.compareAndSwapInt(a, byte_offset(i+UNALIGN_OFF), -1, -123);
 981       int old = unsafe.getIntVolatile(b, byte_offset(i));
 982       unsafe.compareAndSwapInt(b, byte_offset(i), old, -103);
 983     }
 984   }
 985   static void test_2vi_unaln(int[] a, int[] b, int c, int d) {
 986     for (int i = 0; i < ARRLEN-UNALIGN_OFF; i+=1) {
 987       int old = unsafe.getIntVolatile(a, byte_offset(i));
 988       unsafe.compareAndSwapInt(a, byte_offset(i), old, c);
 989       old = unsafe.getIntVolatile(b, byte_offset(i+UNALIGN_OFF));
 990       unsafe.compareAndSwapInt(b, byte_offset(i+UNALIGN_OFF), old, d);
 991     }
 992   }
 993 
 994   static int verify(String text, int i, int elem, int val) {
 995     if (elem != val) {
 996       System.err.println(text + "[" + i + "] = " + elem + " != " + val);
 997       return 1;
 998     }
 999     return 0;
1000   }
1001 }