1 /*
   2  * Copyright 2015 SAP AG.  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 8080190
  27  * @key regression
  28  * @summary Test that the rotate distance used in the rotate instruction is properly masked with 0x1f
  29  * @run main/othervm -Xbatch -XX:-UseOnStackReplacement IntRotateWithImmediate
  30  * @author volker.simonis@gmail.com
  31  */
  32 
  33 public class IntRotateWithImmediate {
  34 
  35   // This is currently the same as Integer.rotateRight()
  36   static int rotateRight(int i, int distance) {
  37     // On some architectures (i.e. x86_64 and ppc64) the following computation is
  38     // matched in the .ad file into a single MachNode which emmits a single rotate
  39     // machine instruction. It is important that the shift amount is masked to match
  40     // corresponding immediate width in the native instruction. On x86_64 the rotate
  41     // left instruction ('rol') encodes an 8-bit immediate while the corresponding
  42     // 'rotlwi' instruction on Power only encodes a 5-bit immediate.
  43     return ((i >>> distance) | (i << -distance));
  44   }
  45 
  46   static int compute(int x) {
  47     return rotateRight(x, 3);
  48   }
  49 
  50   public static void main(String args[]) {
  51     int val = 4096;
  52 
  53     int firstResult = compute(val);
  54 
  55     for (int i = 0; i < 100000; i++) {
  56       int newResult = compute(val);
  57       if (firstResult != newResult) {
  58         throw new InternalError(firstResult + " != " + newResult);
  59       }
  60     }
  61     System.out.println("OK");
  62   }
  63 
  64 }