hotspot/test/compiler/loopopts/TestLogSum.java
author chegar
Tue, 22 Mar 2016 15:26:07 +0000
changeset 36694 182a5e7a519e
parent 24954 9bd47e3d7c69
child 40059 c2304140ed64
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24954
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     1
/*
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     4
 *
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     8
 *
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    13
 * accompanied this code).
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    14
 *
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    18
 *
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    21
 * questions.
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    22
 */
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    23
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    24
/*
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    25
 * @test
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    26
 * @bug 8046516
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    27
 * @summary Segmentation fault in JVM (easily reproducible)
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    28
 * @run main/othervm -XX:-TieredCompilation -Xbatch TestLogSum
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    29
 * @author jackkamm@gmail.com
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    30
 */
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    31
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    32
import java.util.Arrays;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    33
import java.util.HashMap;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    34
import java.util.List;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    35
import java.util.Map;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    36
public class TestLogSum {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    37
  public static void main(String[] args) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    38
    double sum;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    39
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    40
    for (int i = 0; i < 6; i++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    41
        for (int n = 2; n < 30; n++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    42
           for (int j = 1; j <= n; j++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    43
              for (int k = 1; k <= j; k++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    44
                // System.out.println(computeSum(k, j));
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    45
                sum = computeSum(k, j);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    46
              }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    47
           }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    48
        }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    49
      }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    50
   }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    51
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    52
   private static Map<List<Integer>, Double> cache = new HashMap<List<Integer>, Double>();
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    53
   public static double computeSum(int x, int y) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    54
      List<Integer> key = Arrays.asList(new Integer[] {x, y});
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    55
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    56
      if (!cache.containsKey(key)) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    57
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    58
        // explicitly creating/updating a double[] array, instead of using the LogSumArray wrapper object, will prevent the error
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    59
        LogSumArray toReturn = new LogSumArray(x);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    60
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    61
        // changing loop indices will prevent the error
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    62
        // in particular, for(z=0; z<x-1; z++), and then using z+1 in place of z, will not produce error
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    63
        for (int z = 1; z < x+1; z++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    64
           double logSummand = Math.log(z + x + y);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    65
           toReturn.addLogSummand(logSummand);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    66
        }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    67
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    68
        // returning the value here without cacheing it will prevent the segfault
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    69
        cache.put(key, toReturn.retrieveLogSum());
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    70
      }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    71
      return cache.get(key);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    72
   }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    73
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    74
   /*
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    75
    * Given a bunch of logarithms log(X),log(Y),log(Z),...
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    76
    * This class is used to compute the log of the sum, log(X+Y+Z+...)
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    77
    */
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    78
   private static class LogSumArray {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    79
      private double[] logSummandArray;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    80
      private int currSize;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    81
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    82
      private double maxLogSummand;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    83
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    84
      public LogSumArray(int maxEntries) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    85
        this.logSummandArray = new double[maxEntries];
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    86
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    87
        this.currSize = 0;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    88
        this.maxLogSummand = Double.NEGATIVE_INFINITY;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    89
      }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    90
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    91
      public void addLogSummand(double logSummand) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    92
        logSummandArray[currSize] = logSummand;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    93
        currSize++;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    94
        // removing this line will prevent the error
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    95
        maxLogSummand = Math.max(maxLogSummand, logSummand);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    96
      }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    97
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    98
      public double retrieveLogSum() {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    99
        if (maxLogSummand == Double.NEGATIVE_INFINITY) return Double.NEGATIVE_INFINITY;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   100
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   101
        assert currSize <= logSummandArray.length;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   102
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   103
        double factorSum = 0;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   104
        for (int i = 0; i < currSize; i++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   105
           factorSum += Math.exp(logSummandArray[i] - maxLogSummand);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   106
        }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   107
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   108
        return Math.log(factorSum) + maxLogSummand;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   109
      }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   110
   }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   111
}