Skip to content

Commit

Permalink
Implement x64 16-bit bswap with rol (bytecodealliance#1092)
Browse files Browse the repository at this point in the history
x64 bswap instruction is only for 32- or 64-bit registers,
so implement the 16-bit swap as a rotate left by 8
  • Loading branch information
11evan committed Oct 26, 2022
1 parent ec502b5 commit e1f1dde
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 2 additions & 4 deletions cranelift/codegen/src/isa/x64/inst.isle
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@
(Setcc (cc CC)
(dst WritableGpr))

;; TODO xchg impl needed for 2 byte swap
;; Swaps byte order in register
(Bswap (size OperandSize) ;; 4 or 8
(src Gpr)
Expand Down Expand Up @@ -1972,9 +1971,8 @@
(shift_r ty (ShiftKind.ShiftRightArithmetic) src1 src2))

;; Helper for creating byteswap instructions.
;; In x64, 32- and 64-bit registers use BSWAP instruction, however
;; this instruction is undefined for 16-bit registers, which instead
;; use XCHG
;; In x64, 32- and 64-bit registers use BSWAP instruction, and
;; for 16-bit registers one must instead use xchg or rol/ror
(decl x64_bswap (Type Gpr) Gpr)
(rule (x64_bswap ty src)
(let ((dst WritableGpr (temp_writable_gpr))
Expand Down
5 changes: 5 additions & 0 deletions cranelift/codegen/src/isa/x64/lower.isle
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,11 @@

;; Rules for `bswap` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; x64 bswap instruction is only for 32- or 64-bit swaps
;; implement the 16-bit swap as a rotl by 8
(rule (lower (has_type $I16 (bswap src)))
(x64_rotl $I16 src (Imm8Reg.Imm8 8)))

(rule (lower (has_type $I32 (bswap src)))
(x64_bswap $I32 src))

Expand Down

0 comments on commit e1f1dde

Please sign in to comment.