1 /*
   2  * Copyright (c) 2011, 2019, 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 8022780
  27  * @summary Test division of large values
  28  * @requires (sun.arch.data.model == "64" & os.maxMemory > 8g)
  29  * @run main/othervm -Xshare:off -Xmx8g DivisionOverflow
  30  * @author Dmitry Nadezhin
  31  */
  32 import java.math.BigInteger;
  33 
  34 public class DivisionOverflow {
  35 
  36     public static void main(String[] args) {
  37         try {
  38             BigInteger a = BigInteger.ONE.shiftLeft(2147483646);
  39             BigInteger b = BigInteger.ONE.shiftLeft(1568);
  40             BigInteger[] qr = a.divideAndRemainder(b);
  41             BigInteger q = qr[0];
  42             BigInteger r = qr[1];
  43             if (!r.equals(BigInteger.ZERO)) {
  44                 throw new RuntimeException("Incorrect signum() of remainder " + r.signum());
  45             }
  46             if (q.bitLength() != 2147482079) {
  47                 throw new RuntimeException("Incorrect bitLength() of quotient " + q.bitLength());
  48             }
  49             System.out.println("Division of large values passed without overflow.");
  50         } catch (OutOfMemoryError e) {
  51             // possible
  52             System.err.println("DivisionOverflow skipped: OutOfMemoryError");
  53             System.err.println("Run jtreg with -javaoption:-Xmx8g");
  54         }
  55     }
  56 }