Skip to content

Commit 2331782

Browse files
committed
8345179: RISC-V: Add gtests for weak cmpxchg
Reviewed-by: fyang, mli
1 parent 7c8cec1 commit 2331782

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

test/hotspot/gtest/riscv/test_assembler_riscv.cpp

+129
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,133 @@ TEST_VM(RiscV, cmpxchg_int8_maybe_zacas) {
341341
}
342342
}
343343

344+
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
345+
class WeakCmpxchgTester {
346+
public:
347+
typedef TESTSIZE (*cmpxchg_narrow)(intptr_t addr, TESTSIZE expected, TESTSIZE new_value, TESTSIZE result,
348+
int64_t scratch0, int64_t scratch1, int64_t scratch2);
349+
350+
typedef TESTSIZE (*cmpxchg_func)(intptr_t addr, TESTSIZE expected, TESTSIZE new_value, TESTSIZE result);
351+
352+
static TESTSIZE weak_narrow_cmpxchg(intptr_t addr, TESTSIZE expected, TESTSIZE new_value) {
353+
BufferBlob* bb = BufferBlob::create("riscvTest", 128);
354+
CodeBuffer code(bb);
355+
MacroAssembler _masm(&code);
356+
address entry = _masm.pc();
357+
{
358+
_masm.weak_cmpxchg_narrow_value(/*addr*/ c_rarg0, /*expected*/ c_rarg1, /*new_value*/ c_rarg2,
359+
ASMSIZE, Assembler::relaxed, Assembler::relaxed,
360+
/*result*/ c_rarg3, c_rarg4, c_rarg5, c_rarg6); /* Uses also t0-t1, caller saved */
361+
_masm.mv(c_rarg0, c_rarg3);
362+
_masm.ret();
363+
}
364+
_masm.flush(); // icache invalidate
365+
TESTSIZE ret = ((cmpxchg_narrow)entry)(addr, expected, new_value, /*result*/ 67, -1, -1, -1);
366+
BufferBlob::free(bb);
367+
return ret;
368+
}
369+
370+
static TESTSIZE weak_cmpxchg(intptr_t addr, TESTSIZE expected, TESTSIZE new_value) {
371+
BufferBlob* bb = BufferBlob::create("riscvTest", 128);
372+
CodeBuffer code(bb);
373+
MacroAssembler _masm(&code);
374+
address entry = _masm.pc();
375+
{
376+
_masm.weak_cmpxchg(/*addr*/ c_rarg0, /*expected*/ c_rarg1, /*new_value*/ c_rarg2,
377+
ASMSIZE, Assembler::relaxed, Assembler::relaxed, /*result*/ c_rarg3);
378+
_masm.mv(c_rarg0, c_rarg3);
379+
_masm.ret();
380+
}
381+
_masm.flush(); // icache invalidate
382+
TESTSIZE ret = ((cmpxchg_func)entry)(addr, expected, new_value, /*result*/ 67);
383+
BufferBlob::free(bb);
384+
return ret;
385+
}
386+
};
387+
388+
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
389+
void run_weak_cmpxchg_narrow_value_tests() {
390+
// Assume natural aligned
391+
TESTSIZE data[8];
392+
TESTSIZE ret;
393+
for (int i = 0; i < 7; i++) {
394+
memset(data, -1, sizeof(data));
395+
396+
data[i] = 121;
397+
ret = WeakCmpxchgTester<TESTSIZE, ASMSIZE>::weak_narrow_cmpxchg((intptr_t)&data[i], 121, 42);
398+
ASSERT_EQ(ret, 1);
399+
ASSERT_EQ(data[i], 42);
400+
401+
data[i] = 121;
402+
ret = WeakCmpxchgTester<TESTSIZE, ASMSIZE>::weak_narrow_cmpxchg((intptr_t)&data[i], 120, 42);
403+
ASSERT_EQ(ret, 0);
404+
ASSERT_EQ(data[i], 121);
405+
}
406+
}
407+
408+
TEST_VM(RiscV, weak_cmpxchg_int16_lr_sc) {
409+
bool zacas = UseZacas;
410+
UseZacas = false;
411+
run_weak_cmpxchg_narrow_value_tests<int16_t, Assembler::int16>();
412+
UseZacas = zacas;
413+
}
414+
415+
TEST_VM(RiscV, weak_cmpxchg_int8_lr_sc) {
416+
bool zacas = UseZacas;
417+
UseZacas = false;
418+
run_weak_cmpxchg_narrow_value_tests<int8_t, Assembler::int8>();
419+
UseZacas = zacas;
420+
}
421+
422+
TEST_VM(RiscV, weak_cmpxchg_int16_maybe_zacas) {
423+
if (UseZacas) {
424+
run_weak_cmpxchg_narrow_value_tests<int16_t, Assembler::int16>();
425+
}
426+
}
427+
428+
TEST_VM(RiscV, weak_cmpxchg_int8_maybe_zacas) {
429+
if (UseZacas) {
430+
run_weak_cmpxchg_narrow_value_tests<int8_t, Assembler::int8>();
431+
}
432+
}
433+
434+
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
435+
void run_weak_cmpxchg_tests() {
436+
TESTSIZE data = 121;
437+
TESTSIZE ret = WeakCmpxchgTester<TESTSIZE, ASMSIZE>::weak_cmpxchg((intptr_t)&data, 121, 42);
438+
ASSERT_EQ(ret, 1);
439+
ASSERT_EQ(data, 42);
440+
441+
data = 121;
442+
ret = WeakCmpxchgTester<TESTSIZE, ASMSIZE>::weak_cmpxchg((intptr_t)&data, 120, 42);
443+
ASSERT_EQ(ret, 0);
444+
ASSERT_EQ(data, 121);
445+
}
446+
447+
TEST_VM(RiscV, weak_cmpxchg_int64_lr_sc) {
448+
bool zacas = UseZacas;
449+
UseZacas = false;
450+
run_weak_cmpxchg_tests<int64_t, Assembler::int64>();
451+
UseZacas = zacas;
452+
}
453+
454+
TEST_VM(RiscV, weak_cmpxchg_int64_maybe_zacas) {
455+
if (UseZacas) {
456+
run_weak_cmpxchg_tests<int64_t, Assembler::int64>();
457+
}
458+
}
459+
460+
TEST_VM(RiscV, weak_cmpxchg_int32_lr_sc) {
461+
bool zacas = UseZacas;
462+
UseZacas = false;
463+
run_weak_cmpxchg_tests<int32_t, Assembler::int32>();
464+
UseZacas = zacas;
465+
}
466+
467+
TEST_VM(RiscV, weak_cmpxchg_int32_maybe_zacas) {
468+
if (UseZacas) {
469+
run_weak_cmpxchg_tests<int32_t, Assembler::int32>();
470+
}
471+
}
472+
344473
#endif // RISCV

0 commit comments

Comments
 (0)