Skip to content

Commit acc8700

Browse files
committed
riscv64: Fix ctz edge case
1 parent e40f5d4 commit acc8700

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

cranelift/codegen/src/isa/riscv64/inst.isle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@
971971
(rule 1 (lower_ctz (fits_in_16 ty) x)
972972
(if-let $true (has_zbb))
973973
(let ((tmp Reg (bseti x (ty_bits ty))))
974-
(alu_rr_funct12 (AluOPRRI.Ctzw) x)))
974+
(alu_rr_funct12 (AluOPRRI.Ctzw) tmp)))
975975

976976
(rule 2 (lower_ctz $I32 x)
977977
(if-let $true (has_zbb))

cranelift/filetests/filetests/isa/riscv64/ctz-zbb-zbs.clif

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ block0(v0: i8):
1111
; VCode:
1212
; block0:
1313
; bseti t2,a0,8
14-
; ctzw a0,a0
14+
; ctzw a0,t2
1515
; ret
1616
;
1717
; Disassembled:
1818
; block0: ; offset 0x0
1919
; .byte 0x93, 0x13, 0x85, 0x28
20-
; .byte 0x1b, 0x15, 0x15, 0x60
20+
; .byte 0x1b, 0x95, 0x13, 0x60
2121
; ret
2222

2323
function %ctz_i16(i16) -> i16 {
@@ -29,13 +29,13 @@ block0(v0: i16):
2929
; VCode:
3030
; block0:
3131
; bseti t2,a0,16
32-
; ctzw a0,a0
32+
; ctzw a0,t2
3333
; ret
3434
;
3535
; Disassembled:
3636
; block0: ; offset 0x0
3737
; .byte 0x93, 0x13, 0x05, 0x29
38-
; .byte 0x1b, 0x15, 0x15, 0x60
38+
; .byte 0x1b, 0x95, 0x13, 0x60
3939
; ret
4040

4141
function %ctz_i32(i32) -> i32 {

cranelift/filetests/filetests/isa/riscv64/ctz-zbb.clif

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ block0(v0: i8):
1212
; VCode:
1313
; block0:
1414
; ori t2,a0,256
15-
; ctzw a0,a0
15+
; ctzw a0,t2
1616
; ret
1717
;
1818
; Disassembled:
1919
; block0: ; offset 0x0
2020
; ori t2, a0, 0x100
21-
; .byte 0x1b, 0x15, 0x15, 0x60
21+
; .byte 0x1b, 0x95, 0x13, 0x60
2222
; ret
2323

2424
function %ctz_i16(i16) -> i16 {
@@ -31,14 +31,14 @@ block0(v0: i16):
3131
; block0:
3232
; lui t2,16
3333
; or a1,a0,t2
34-
; ctzw a0,a0
34+
; ctzw a0,a1
3535
; ret
3636
;
3737
; Disassembled:
3838
; block0: ; offset 0x0
3939
; lui t2, 0x10
4040
; or a1, a0, t2
41-
; .byte 0x1b, 0x15, 0x15, 0x60
41+
; .byte 0x1b, 0x95, 0x15, 0x60
4242
; ret
4343

4444
function %ctz_i32(i32) -> i32 {

cranelift/filetests/filetests/runtests/clz.clif

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ block0(v0: i8):
1212
v1 = clz v0
1313
return v1
1414
}
15+
; run: %clz_i8(0) == 8
1516
; run: %clz_i8(1) == 7
1617
; run: %clz_i8(0x40) == 1
1718
; run: %clz_i8(-1) == 0
@@ -21,6 +22,7 @@ block0(v0: i16):
2122
v1 = clz v0
2223
return v1
2324
}
25+
; run: %clz_i16(0) == 16
2426
; run: %clz_i16(1) == 15
2527
; run: %clz_i16(0x4000) == 1
2628
; run: %clz_i16(-1) == 0
@@ -30,6 +32,7 @@ block0(v0: i32):
3032
v1 = clz v0
3133
return v1
3234
}
35+
; run: %clz_i32(0) == 32
3336
; run: %clz_i32(1) == 31
3437
; run: %clz_i32(0x40000000) == 1
3538
; run: %clz_i32(-1) == 0
@@ -39,6 +42,7 @@ block0(v0: i64):
3942
v1 = clz v0
4043
return v1
4144
}
45+
; run: %clz_i64(0) == 64
4246
; run: %clz_i64(1) == 63
4347
; run: %clz_i64(0x4000000000000000) == 1
4448
; run: %clz_i64(-1) == 0

cranelift/filetests/filetests/runtests/ctz.clif

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ block0(v0: i8):
1313
v1 = ctz v0
1414
return v1
1515
}
16+
; run: %ctz_i8(0) == 8
1617
; run: %ctz_i8(1) == 0
1718
; run: %ctz_i8(0x40) == 6
1819
; run: %ctz_i8(-1) == 0
@@ -22,6 +23,7 @@ block0(v0: i16):
2223
v1 = ctz v0
2324
return v1
2425
}
26+
; run: %ctz_i16(0) == 16
2527
; run: %ctz_i16(1) == 0
2628
; run: %ctz_i16(0x4000) == 14
2729
; run: %ctz_i16(-1) == 0
@@ -31,6 +33,7 @@ block0(v0: i32):
3133
v1 = ctz v0
3234
return v1
3335
}
36+
; run: %ctz_i32(0) == 32
3437
; run: %ctz_i32(1) == 0
3538
; run: %ctz_i32(0x40000000) == 30
3639
; run: %ctz_i32(-1) == 0
@@ -40,6 +43,7 @@ block0(v0: i64):
4043
v1 = ctz v0
4144
return v1
4245
}
46+
; run: %ctz_i64(0) == 64
4347
; run: %ctz_i64(1) == 0
4448
; run: %ctz_i64(0x4000000000000000) == 62
4549
; run: %ctz_i64(-1) == 0

0 commit comments

Comments
 (0)