Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/string/TestStringLatin1IndexOfChar.java
41153 views
/*1* Copyright Amazon.com Inc. 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 817358526* @summary Test intrinsification of StringLatin1.indexOf(char). Note that27* differing code paths are taken contingent upon the length of the input String.28* Hence we must test against differing string lengths in order to validate29* correct functionality. We also ensure the strings are long enough to trigger30* the looping conditions of the individual code paths.31*32* Run with varing levels of AVX and SSE support, also without the intrinsic at all33*34* @library /compiler/patches /test/lib35* @run main/othervm -Xbatch -XX:Tier4InvocationThreshold=200 -XX:CompileThreshold=100 compiler.intrinsics.string.TestStringLatin1IndexOfChar36* @run main/othervm -Xbatch -XX:Tier4InvocationThreshold=200 -XX:CompileThreshold=100 -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_indexOfL_char compiler.intrinsics.string.TestStringLatin1IndexOfChar37* @run main/othervm -Xbatch -XX:Tier4InvocationThreshold=200 -XX:CompileThreshold=100 -XX:+IgnoreUnrecognizedVMOptions -XX:UseSSE=0 compiler.intrinsics.string.TestStringLatin1IndexOfChar38* @run main/othervm -Xbatch -XX:Tier4InvocationThreshold=200 -XX:CompileThreshold=100 -XX:+IgnoreUnrecognizedVMOptions -XX:UseAVX=1 compiler.intrinsics.string.TestStringLatin1IndexOfChar39* @run main/othervm -Xbatch -XX:Tier4InvocationThreshold=200 -XX:CompileThreshold=100 -XX:+IgnoreUnrecognizedVMOptions -XX:UseAVX=2 compiler.intrinsics.string.TestStringLatin1IndexOfChar40* @run main/othervm -Xbatch -XX:Tier4InvocationThreshold=200 -XX:CompileThreshold=100 -XX:+IgnoreUnrecognizedVMOptions -XX:UseAVX=3 compiler.intrinsics.string.TestStringLatin1IndexOfChar41*/4243package compiler.intrinsics.string;4445import jdk.test.lib.Asserts;4647public class TestStringLatin1IndexOfChar{48private final static int MAX_LENGTH = 2048;//future proof for AVX-512 instructions4950public static void main(String[] args) throws Exception {51for (int i = 0; i < 1_000; ++i) {//repeat such that we enter into C2 code...52findOneItem();53withOffsetTest();54testEmpty();55}56}5758private static void testEmpty(){59Asserts.assertEQ("".indexOf('a'), -1);60}6162private final static char SEARCH_CHAR = 'z';63private final static char INVERLEAVING_CHAR = 'a';64private final static char MISSING_CHAR = 'd';6566private static void findOneItem(){67//test strings of varying length ensuring that for all lengths one instance of the68//search char can be found. We check what happens when the search character is in69//each position of the search string (including first and last positions)70for(int strLength : new int[]{1, 15, 31, 32, 79}){71for(int searchPos = 0; searchPos < strLength; searchPos++){72String totest = makeOneItemStringLatin1(strLength, searchPos);7374int intri = totest.indexOf(SEARCH_CHAR);75int nonintri = indexOfCharNonIntrinsic(totest, SEARCH_CHAR, 0);76Asserts.assertEQ(intri, nonintri);77}78}79}8081private static String makeOneItemStringLatin1(int length, int searchPos){82StringBuilder sb = new StringBuilder(length);8384for(int n =0; n < length; n++){85sb.append(searchPos==n?SEARCH_CHAR:INVERLEAVING_CHAR);86}8788return sb.toString();89}9091private static void withOffsetTest(){92//progressivly move through string checking indexes and starting offset correctly processed93//string is of form azaza, aazaazaa, aaazaaazaaa, etc94//we find n s.t. maxlength = (n*3) + 295int maxaInstances = (MAX_LENGTH-2)/3;9697for(int aInstances = 5; aInstances < MAX_LENGTH; aInstances++){98String totest = makeWithOffsetStringLatin1(aInstances);99100int startoffset;101{102int intri = totest.indexOf(SEARCH_CHAR);103int nonintri = indexOfCharNonIntrinsic(totest, SEARCH_CHAR, 0);104105Asserts.assertEQ(intri, nonintri);106startoffset = intri+1;107}108109{110int intri = totest.indexOf(SEARCH_CHAR, startoffset);111int nonintri = indexOfCharNonIntrinsic(totest, SEARCH_CHAR, startoffset);112113Asserts.assertEQ(intri, nonintri);114startoffset = intri+1;115}116117Asserts.assertEQ(totest.indexOf(SEARCH_CHAR, startoffset), -1);//only two SEARCH_CHAR per string118Asserts.assertEQ(totest.indexOf(MISSING_CHAR), -1);119}120}121122private static String makeWithOffsetStringLatin1(int aInstances){123StringBuilder sb = new StringBuilder((aInstances*3) + 2);124for(int n =0; n < aInstances; n++){125sb.append(INVERLEAVING_CHAR);126}127128sb.append(SEARCH_CHAR);129130for(int n =0; n < aInstances; n++){131sb.append(INVERLEAVING_CHAR);132}133134sb.append(SEARCH_CHAR);135136for(int n =0; n < aInstances; n++){137sb.append(INVERLEAVING_CHAR);138}139return sb.toString();140}141142private static int indexOfCharNonIntrinsic(String value, int ch, int fromIndex) {143//non intrinsic version of indexOfChar144byte c = (byte)ch;145for (int i = fromIndex; i < value.length(); i++) {146if (value.charAt(i) == c) {147return i;148}149}150return -1;151}152}153154155