1 /* 2 * Copyright (c) 2011, 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 /** 26 * @test 27 * @bug 6959129 28 * @summary COMPARISON WITH INTEGER.MAX_INT DOES NOT WORK CORRECTLY IN THE CLIENT VM. 29 * 30 * @run main/othervm -ea compiler.c2.Test6959129 31 */ 32 33 package compiler.c2; 34 35 public class Test6959129 { 36 37 public static void main(String[] args) { 38 long start = System.currentTimeMillis(); 39 int min = Integer.MAX_VALUE-30000; 40 int max = Integer.MAX_VALUE; 41 long maxmoves = 0; 42 try { 43 maxmoves = maxMoves(min, max); 44 } catch (AssertionError e) { 45 System.out.println("Passed"); 46 System.exit(95); 47 } 48 System.out.println("maxMove:" + maxmoves); 49 System.out.println("FAILED"); 50 System.exit(97); 51 } 52 /** 53 * Imperative implementation that returns the length hailstone moves 54 * for a given number. 55 */ 56 public static long hailstoneLengthImp(long n) { 57 long moves = 0; 58 while (n != 1) { 59 assert n > 1; 60 if (isEven(n)) { 61 n = n / 2; 62 } else { 63 n = 3 * n + 1; 64 } 65 ++moves; 66 } 67 return moves; 68 } 69 70 private static boolean isEven(long n) { 71 return n % 2 == 0; 72 } 73 74 /** 75 * Returns the maximum length of the hailstone sequence for numbers 76 * between min to max. 77 * 78 * For rec1 - Assume that min is bigger than max. 79 */ 80 public static long maxMoves(int min, int max) { 81 long maxmoves = 0; 82 for (int n = min; n <= max; n++) { 83 if ((n & 1023) == 0) System.out.println(n); 84 long moves = hailstoneLengthImp(n); 85 if (moves > maxmoves) { 86 maxmoves = moves; 87 } 88 } 89 return maxmoves; 90 } 91 } 92