Skip to content

Commit

Permalink
8303508: Vector.lane() gets wrong value on x86
Browse files Browse the repository at this point in the history
Reviewed-by: eliu, thartmann
  • Loading branch information
Jatin Bhateja committed Mar 24, 2023
1 parent 941a7ac commit d61de14
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/hotspot/cpu/x86/c2_MacroAssembler_x86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,14 @@ XMMRegister C2_MacroAssembler::get_lane(BasicType typ, XMMRegister dst, XMMRegis
}
}

void C2_MacroAssembler::movsxl(BasicType typ, Register dst) {
if (typ == T_BYTE) {
movsbl(dst, dst);
} else if (typ == T_SHORT) {
movswl(dst, dst);
}
}

void C2_MacroAssembler::get_elem(BasicType typ, Register dst, XMMRegister src, int elemindex) {
int esize = type2aelembytes(typ);
int elem_per_lane = 16/esize;
Expand All @@ -2287,13 +2295,11 @@ void C2_MacroAssembler::get_elem(BasicType typ, Register dst, XMMRegister src, i
movq(dst, src);
} else {
movdl(dst, src);
if (typ == T_BYTE)
movsbl(dst, dst);
else if (typ == T_SHORT)
movswl(dst, dst);
movsxl(typ, dst);
}
} else {
extract(typ, dst, src, eindex);
movsxl(typ, dst);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/hotspot/cpu/x86/c2_MacroAssembler_x86.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
XMMRegister get_lane(BasicType typ, XMMRegister dst, XMMRegister src, int elemindex);
void get_elem(BasicType typ, Register dst, XMMRegister src, int elemindex);
void get_elem(BasicType typ, XMMRegister dst, XMMRegister src, int elemindex, XMMRegister vtmp = xnoreg);
void movsxl(BasicType typ, Register dst);

// vector test
void vectortest(BasicType bt, XMMRegister src1, XMMRegister src2, XMMRegister vtmp, int vlen_in_bytes);
Expand Down
77 changes: 77 additions & 0 deletions test/hotspot/jtreg/compiler/vectorapi/Test8303508.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package compiler.vectorapi;

import jdk.incubator.vector.ByteVector;
import jdk.incubator.vector.ShortVector;
import jdk.incubator.vector.VectorSpecies;

/*
* @test
* @bug 8303508
* @summary Vector.lane() gets wrong value on x86
* @modules jdk.incubator.vector
* @library /test/lib
*
* @run main/othervm -Xbatch -XX:-TieredCompilation -ea compiler.vectorapi.Test8303508
*/
public class Test8303508 {

static final VectorSpecies<Byte> BSPECIES_128 = ByteVector.SPECIES_128;
static final VectorSpecies<Short> SSPECIES_128 = ShortVector.SPECIES_128;

static final byte[] ba = {0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15};
static final short[] sa = {0, -1, -2, -3, -4, -5, -6, -7};

private static byte vec_extract_byte(int idx) {
var bv = ByteVector.fromArray(BSPECIES_128, ba, 0);
return bv.lane(idx);
}

private static short vec_extract_short(int idx) {
var sv = ShortVector.fromArray(SSPECIES_128, sa, 0);
return sv.lane(idx);
}

public static void main(String[] args) {
int idx = 0;
int actual = 0;
int expected = 0;
for (int i = 0; i < 10000; i++) {
idx = i & 0xF;
actual = vec_extract_byte(idx);
expected = ba[idx];
if (actual != expected) {
throw new AssertionError("incorrect result byte extraction, actual = " + actual + " expected = " + expected);
}
idx = i & 0x7;
actual = vec_extract_short(idx);
expected = sa[idx];
if (actual != expected) {
throw new AssertionError("incorrect result short extraction, actual = " + actual + " expected = " + expected);
}
}
System.out.println("PASS");
}
}

1 comment on commit d61de14

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.