Skip to content

Commit 6b89954

Browse files
committed
8346475: RISC-V: Small improvement for MacroAssembler::ctzc_bit
Reviewed-by: mli, fjiang
1 parent 00d8407 commit 6b89954

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

src/hotspot/cpu/riscv/c2_MacroAssembler_riscv.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,8 @@ void C2_MacroAssembler::string_indexof_char(Register str1, Register cnt1,
787787
j(NOMATCH);
788788

789789
bind(HIT);
790-
ctzc_bit(trailing_char, match_mask, isL, ch1, result);
790+
// count bits of trailing zero chars
791+
ctzc_bits(trailing_char, match_mask, isL, ch1, result);
791792
srli(trailing_char, trailing_char, 3);
792793
addi(cnt1, cnt1, 8);
793794
ble(cnt1, trailing_char, NOMATCH);
@@ -1536,7 +1537,8 @@ void C2_MacroAssembler::string_compare(Register str1, Register str2,
15361537
// compute their difference.
15371538
bind(DIFFERENCE);
15381539
xorr(tmp3, tmp1, tmp2);
1539-
ctzc_bit(result, tmp3, isLL); // count zero from lsb to msb
1540+
// count bits of trailing zero chars
1541+
ctzc_bits(result, tmp3, isLL);
15401542
srl(tmp1, tmp1, result);
15411543
srl(tmp2, tmp2, result);
15421544
if (isLL) {

src/hotspot/cpu/riscv/macroAssembler_riscv.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5395,28 +5395,26 @@ void MacroAssembler::multiply_to_len(Register x, Register xlen, Register y, Regi
53955395
}
53965396
#endif
53975397

5398-
// Count bits of trailing zero chars from lsb to msb until first non-zero element.
5399-
// For LL case, one byte for one element, so shift 8 bits once, and for other case,
5400-
// shift 16 bits once.
5401-
void MacroAssembler::ctzc_bit(Register Rd, Register Rs, bool isLL, Register tmp1, Register tmp2) {
5398+
// Count bits of trailing zero chars from lsb to msb until first non-zero
5399+
// char seen. For the LL case, shift 8 bits once as there is only one byte
5400+
// per each char. For other cases, shift 16 bits once.
5401+
void MacroAssembler::ctzc_bits(Register Rd, Register Rs, bool isLL,
5402+
Register tmp1, Register tmp2) {
5403+
int step = isLL ? 8 : 16;
54025404
if (UseZbb) {
5403-
assert_different_registers(Rd, Rs, tmp1);
5404-
int step = isLL ? 8 : 16;
54055405
ctz(Rd, Rs);
5406-
andi(tmp1, Rd, step - 1);
5407-
sub(Rd, Rd, tmp1);
5406+
andi(Rd, Rd, -step);
54085407
return;
54095408
}
54105409

5411-
assert_different_registers(Rd, Rs, tmp1, tmp2);
5410+
assert_different_registers(Rd, tmp1, tmp2);
54125411
Label Loop;
5413-
int step = isLL ? 8 : 16;
5414-
mv(Rd, -step);
54155412
mv(tmp2, Rs);
5413+
mv(Rd, -step);
54165414

54175415
bind(Loop);
54185416
addi(Rd, Rd, step);
5419-
andi(tmp1, tmp2, ((1 << step) - 1));
5417+
zext(tmp1, tmp2, step);
54205418
srli(tmp2, tmp2, step);
54215419
beqz(tmp1, Loop);
54225420
}

src/hotspot/cpu/riscv/macroAssembler_riscv.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,8 @@ class MacroAssembler: public Assembler {
13621362
void inflate_lo32(Register Rd, Register Rs, Register tmp1 = t0, Register tmp2 = t1);
13631363
void inflate_hi32(Register Rd, Register Rs, Register tmp1 = t0, Register tmp2 = t1);
13641364

1365-
void ctzc_bit(Register Rd, Register Rs, bool isLL = false, Register tmp1 = t0, Register tmp2 = t1);
1365+
void ctzc_bits(Register Rd, Register Rs, bool isLL = false,
1366+
Register tmp1 = t0, Register tmp2 = t1);
13661367

13671368
void zero_words(Register base, uint64_t cnt);
13681369
address zero_words(Register ptr, Register cnt);

src/hotspot/cpu/riscv/stubGenerator_riscv.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,7 +2568,8 @@ class StubGenerator: public StubCodeGenerator {
25682568
// Find the first different characters in the longwords and
25692569
// compute their difference.
25702570
__ bind(CALCULATE_DIFFERENCE);
2571-
__ ctzc_bit(tmp4, tmp3);
2571+
// count bits of trailing zero chars
2572+
__ ctzc_bits(tmp4, tmp3);
25722573
__ srl(tmp1, tmp1, tmp4);
25732574
__ srl(tmp2, tmp2, tmp4);
25742575
__ zext(tmp1, tmp1, 16);
@@ -2703,7 +2704,8 @@ class StubGenerator: public StubCodeGenerator {
27032704
// Find the first different characters in the longwords and
27042705
// compute their difference.
27052706
__ bind(DIFF2);
2706-
__ ctzc_bit(tmp3, tmp4, isLL); // count zero from lsb to msb
2707+
// count bits of trailing zero chars
2708+
__ ctzc_bits(tmp3, tmp4, isLL);
27072709
__ srl(tmp5, tmp5, tmp3);
27082710
__ srl(cnt1, cnt1, tmp3);
27092711
if (isLL) {
@@ -2716,7 +2718,8 @@ class StubGenerator: public StubCodeGenerator {
27162718
__ sub(result, tmp5, cnt1);
27172719
__ j(LENGTH_DIFF);
27182720
__ bind(DIFF);
2719-
__ ctzc_bit(tmp3, tmp4, isLL); // count zero from lsb to msb
2721+
// count bits of trailing zero chars
2722+
__ ctzc_bits(tmp3, tmp4, isLL);
27202723
__ srl(tmp1, tmp1, tmp3);
27212724
__ srl(tmp2, tmp2, tmp3);
27222725
if (isLL) {
@@ -2862,7 +2865,8 @@ class StubGenerator: public StubCodeGenerator {
28622865
__ beqz(match_mask, NOMATCH);
28632866

28642867
__ bind(L_SMALL_HAS_ZERO_LOOP);
2865-
__ ctzc_bit(trailing_zeros, match_mask, haystack_isL, ch2, tmp); // count trailing zeros
2868+
// count bits of trailing zero chars
2869+
__ ctzc_bits(trailing_zeros, match_mask, haystack_isL, ch2, tmp);
28662870
__ addi(trailing_zeros, trailing_zeros, haystack_isL ? 7 : 15);
28672871
__ mv(ch2, wordSize / haystack_chr_size);
28682872
__ ble(needle_len, ch2, L_SMALL_CMP_LOOP_LAST_CMP2);
@@ -2881,7 +2885,8 @@ class StubGenerator: public StubCodeGenerator {
28812885

28822886
__ bind(L_SMALL_CMP_LOOP_NOMATCH);
28832887
__ beqz(match_mask, NOMATCH);
2884-
__ ctzc_bit(trailing_zeros, match_mask, haystack_isL, tmp, ch2);
2888+
// count bits of trailing zero chars
2889+
__ ctzc_bits(trailing_zeros, match_mask, haystack_isL, tmp, ch2);
28852890
__ addi(trailing_zeros, trailing_zeros, haystack_isL ? 7 : 15);
28862891
__ add(result, result, 1);
28872892
__ add(haystack, haystack, haystack_chr_size);
@@ -2900,7 +2905,8 @@ class StubGenerator: public StubCodeGenerator {
29002905

29012906
__ align(OptoLoopAlignment);
29022907
__ bind(L_HAS_ZERO);
2903-
__ ctzc_bit(trailing_zeros, match_mask, haystack_isL, tmp, ch2);
2908+
// count bits of trailing zero chars
2909+
__ ctzc_bits(trailing_zeros, match_mask, haystack_isL, tmp, ch2);
29042910
__ addi(trailing_zeros, trailing_zeros, haystack_isL ? 7 : 15);
29052911
__ slli(needle_len, needle_len, BitsPerByte * wordSize / 2);
29062912
__ orr(haystack_len, haystack_len, needle_len); // restore needle_len(32bits)
@@ -2929,7 +2935,8 @@ class StubGenerator: public StubCodeGenerator {
29292935

29302936
__ bind(L_CMP_LOOP_NOMATCH);
29312937
__ beqz(match_mask, L_HAS_ZERO_LOOP_NOMATCH);
2932-
__ ctzc_bit(trailing_zeros, match_mask, haystack_isL, needle_len, ch2); // find next "first" char index
2938+
// count bits of trailing zero chars
2939+
__ ctzc_bits(trailing_zeros, match_mask, haystack_isL, needle_len, ch2);
29332940
__ addi(trailing_zeros, trailing_zeros, haystack_isL ? 7 : 15);
29342941
__ add(haystack, haystack, haystack_chr_size);
29352942
__ j(L_HAS_ZERO_LOOP);

0 commit comments

Comments
 (0)