hotspot/test/compiler/8005419/Test8005419.java
author kvn
Tue, 08 Jan 2013 11:30:51 -0800
changeset 15117 625397df6f4f
child 22234 da823d78ad65
permissions -rw-r--r--
8005419: Improve intrinsics code performance on x86 by using AVX2 Summary: use 256bit vpxor,vptest instructions in String.compareTo() and equals() intrinsics. Reviewed-by: twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15117
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
     1
/*
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
     4
 *
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
     8
 *
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    13
 * accompanied this code).
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    14
 *
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    18
 *
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    21
 * questions.
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    22
 */
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    23
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    24
/*
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    25
 * @test
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    26
 * @bug 8005419
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    27
 * @summary Improve intrinsics code performance on x86 by using AVX2
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    28
 * @run main/othervm -Xbatch -Xmx64m Test8005419
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    29
 *
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    30
 */
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    31
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    32
public class Test8005419 {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    33
    public static int SIZE = 64;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    34
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    35
    public static void main(String[] args) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    36
        char[] a = new char[SIZE];
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    37
        char[] b = new char[SIZE];
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    38
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    39
        for (int i = 16; i < SIZE; i++) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    40
          a[i] = (char)i;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    41
          b[i] = (char)i;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    42
        }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    43
        String s1 = new String(a);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    44
        String s2 = new String(b);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    45
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    46
        // Warm up
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    47
        boolean failed = false;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    48
        int result = 0;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    49
        for (int i = 0; i < 10000; i++) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    50
          result += test(s1, s2);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    51
        }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    52
        for (int i = 0; i < 10000; i++) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    53
          result += test(s1, s2);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    54
        }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    55
        for (int i = 0; i < 10000; i++) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    56
          result += test(s1, s2);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    57
        }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    58
        if (result != 0) failed = true;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    59
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    60
        System.out.println("Start testing");
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    61
        // Compare same string
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    62
        result = test(s1, s1);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    63
        if (result != 0) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    64
          failed = true;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    65
          System.out.println("Failed same: result = " + result + ", expected 0");
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    66
        }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    67
        // Compare equal strings
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    68
        for (int i = 1; i <= SIZE; i++) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    69
          s1 = new String(a, 0, i);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    70
          s2 = new String(b, 0, i);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    71
          result = test(s1, s2);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    72
          if (result != 0) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    73
            failed = true;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    74
            System.out.println("Failed equals s1[" + i + "], s2[" + i + "]: result = " + result + ", expected 0");
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    75
          }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    76
        }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    77
        // Compare equal strings but different sizes
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    78
        for (int i = 1; i <= SIZE; i++) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    79
          s1 = new String(a, 0, i);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    80
          for (int j = 1; j <= SIZE; j++) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    81
            s2 = new String(b, 0, j);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    82
            result = test(s1, s2);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    83
            if (result != (i-j)) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    84
              failed = true;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    85
              System.out.println("Failed diff size s1[" + i + "], s2[" + j + "]: result = " + result + ", expected " + (i-j));
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    86
            }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    87
          }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    88
        }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    89
        // Compare strings with one char different and different sizes
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    90
        for (int i = 1; i <= SIZE; i++) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    91
          s1 = new String(a, 0, i);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    92
          for (int j = 0; j < i; j++) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    93
            b[j] -= 3; // change char
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    94
            s2 = new String(b, 0, i);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    95
            result = test(s1, s2);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    96
            int chdiff = a[j] - b[j];
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    97
            if (result != chdiff) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    98
              failed = true;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
    99
              System.out.println("Failed diff char s1[" + i + "], s2[" + i + "]: result = " + result + ", expected " + chdiff);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   100
            }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   101
            result = test(s2, s1);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   102
            chdiff = b[j] - a[j];
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   103
            if (result != chdiff) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   104
              failed = true;
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   105
              System.out.println("Failed diff char s2[" + i + "], s1[" + i + "]: result = " + result + ", expected " + chdiff);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   106
            }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   107
            b[j] += 3; // restore
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   108
          }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   109
        }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   110
        if (failed) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   111
          System.out.println("FAILED");
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   112
          System.exit(97);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   113
        }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   114
        System.out.println("PASSED");
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   115
    }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   116
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   117
    private static int test(String str1, String str2) {
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   118
        return str1.compareTo(str2);
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   119
    }
625397df6f4f 8005419: Improve intrinsics code performance on x86 by using AVX2
kvn
parents:
diff changeset
   120
}