Path: blob/master/test/hotspot/jtreg/compiler/loopopts/TestLogSum.java
41152 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 804651626* @summary Segmentation fault in JVM (easily reproducible)27*28* @run main/othervm -XX:-TieredCompilation -Xbatch compiler.loopopts.TestLogSum29* @author [email protected]30*/3132package compiler.loopopts;3334import java.util.Arrays;35import java.util.HashMap;36import java.util.List;37import java.util.Map;3839public class TestLogSum {40public static void main(String[] args) {41double sum;4243for (int i = 0; i < 6; i++) {44for (int n = 2; n < 30; n++) {45for (int j = 1; j <= n; j++) {46for (int k = 1; k <= j; k++) {47// System.out.println(computeSum(k, j));48sum = computeSum(k, j);49}50}51}52}53}5455private static Map<List<Integer>, Double> cache = new HashMap<List<Integer>, Double>();56public static double computeSum(int x, int y) {57List<Integer> key = Arrays.asList(new Integer[] {x, y});5859if (!cache.containsKey(key)) {6061// explicitly creating/updating a double[] array, instead of using the LogSumArray wrapper object, will prevent the error62LogSumArray toReturn = new LogSumArray(x);6364// changing loop indices will prevent the error65// in particular, for(z=0; z<x-1; z++), and then using z+1 in place of z, will not produce error66for (int z = 1; z < x+1; z++) {67double logSummand = Math.log(z + x + y);68toReturn.addLogSummand(logSummand);69}7071// returning the value here without cacheing it will prevent the segfault72cache.put(key, toReturn.retrieveLogSum());73}74return cache.get(key);75}7677/*78* Given a bunch of logarithms log(X),log(Y),log(Z),...79* This class is used to compute the log of the sum, log(X+Y+Z+...)80*/81private static class LogSumArray {82private double[] logSummandArray;83private int currSize;8485private double maxLogSummand;8687public LogSumArray(int maxEntries) {88this.logSummandArray = new double[maxEntries];8990this.currSize = 0;91this.maxLogSummand = Double.NEGATIVE_INFINITY;92}9394public void addLogSummand(double logSummand) {95logSummandArray[currSize] = logSummand;96currSize++;97// removing this line will prevent the error98maxLogSummand = Math.max(maxLogSummand, logSummand);99}100101public double retrieveLogSum() {102if (maxLogSummand == Double.NEGATIVE_INFINITY) return Double.NEGATIVE_INFINITY;103104assert currSize <= logSummandArray.length;105106double factorSum = 0;107for (int i = 0; i < currSize; i++) {108factorSum += Math.exp(logSummandArray[i] - maxLogSummand);109}110111return Math.log(factorSum) + maxLogSummand;112}113}114}115116117