Add and use bit rotate functions
cover Add and use bit rotate functions
The first patch was already sent to qemu-devel and is only included here because patch 3 is based on it. Only patch 1 is needed for qemu-stable.
It looks like shift values of 0 or 32/64 work as expected because the compiler "knows" the pattern used to implement the rotate operation, so the code does not need special handling of some shift values.
rol8 and ror8 are currently unused. I added them nevertheless, so they can be used when someone needs them.
[PATCH 1/3] tci: Add implementation of rotl_i64, rotr_i64 [PATCH 2/3] bitops: Add rotate functions (rol8, ror8, ...) [PATCH 3/3] misc: Use new rotate functions
PATCH 1/3 tci: Add implementation of rotl_i64, rotr_i64
It is used by qemu-ppc64 when running Debian's busybox-static.
--- tcg/tci/tcg-target.c | 1 - tci.c | 10 +++++++++- 2 files changed, 9 insertions(+), 2 deletions(-)
tcg/tci/tcg-target.c +0 -1
@@ -670,7 +670,6 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,case INDEX_op_shl_i64:case INDEX_op_shr_i64:case INDEX_op_sar_i64: -/* TODO: Implementation of rotl_i64, rotr_i64 missing in tci.c. */ case INDEX_op_rotl_i64:/* Optional (TCG_TARGET_HAS_rot_i64). */ case INDEX_op_rotr_i64:/* Optional (TCG_TARGET_HAS_rot_i64). */ tcg_out_r (s, args[0 ]);
tci.c +9 -1
@@ -952,8 +952,16 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)break ;#if TCG_TARGET_HAS_rot_i64case INDEX_op_rotl_i64: + t0 = *tb_ptr++; + t1 =tci_read_ri64 (&tb_ptr); + t2 =tci_read_ri64 (&tb_ptr); +tci_write_reg64 (t0, (t1 << t2) | (t1 >> (64 - t2))); +break ;case INDEX_op_rotr_i64: -TODO (); + t0 = *tb_ptr++; + t1 =tci_read_ri64 (&tb_ptr); + t2 =tci_read_ri64 (&tb_ptr); +tci_write_reg64 (t0, (t1 >> t2) | (t1 << (64 - t2)));break ;#endif #if TCG_TARGET_HAS_deposit_i64
Signature · 1 line
1.7.10.4
PATCH 2/3 bitops: Add rotate functions (rol8, ror8, ...)
In reply to Stefan WeilThese functions were copies from include/linux/bitopts.h.
--- include/qemu/bitops.h | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)
include/qemu/bitops.h +80 -0
@@ -184,6 +184,86 @@ static inline unsigned long hweight_long(unsigned long w) }/** +* rol8 - rotate an 8-bit value left +* @word: value to rotate +* @shift: bits to roll +*/ +static inline uint8_t rol8 (uint8_t word,unsigned int shift) +{ +return (word << shift) | (word >> (8 - shift)); +} + +/** +* ror8 - rotate an 8-bit value right +* @word: value to rotate +* @shift: bits to roll +*/ +static inline uint8_t ror8 (uint8_t word,unsigned int shift) +{ +return (word >> shift) | (word << (8 - shift)); +} + +/** +* rol16 - rotate a 16-bit value left +* @word: value to rotate +* @shift: bits to roll +*/ +static inline uint16_t rol16 (uint16_t word,unsigned int shift) +{ +return (word << shift) | (word >> (16 - shift)); +} + +/** +* ror16 - rotate a 16-bit value right +* @word: value to rotate +* @shift: bits to roll +*/ +static inline uint16_t ror16 (uint16_t word,unsigned int shift) +{ +return (word >> shift) | (word << (16 - shift)); +} + +/** +* rol32 - rotate a 32-bit value left +* @word: value to rotate +* @shift: bits to roll +*/ +static inline uint32_t rol32 (uint32_t word,unsigned int shift) +{ +return (word << shift) | (word >> (32 - shift)); +} + +/** +* ror32 - rotate a 32-bit value right +* @word: value to rotate +* @shift: bits to roll +*/ +static inline uint32_t ror32 (uint32_t word,unsigned int shift) +{ +return (word >> shift) | (word << (32 - shift)); +} + +/** +* rol64 - rotate a 64-bit value left +* @word: value to rotate +* @shift: bits to roll +*/ +static inline uint64_t rol64 (uint64_t word,unsigned int shift) +{ +return (word << shift) | (word >> (64 - shift)); +} + +/** +* ror64 - rotate a 64-bit value right +* @word: value to rotate +* @shift: bits to roll +*/ +static inline uint64_t ror64 (uint64_t word,unsigned int shift) +{ +return (word >> shift) | (word << (64 - shift)); +} + +/** * extract32: * @value: the value to extract the bit field from * @start: the lowest bit in the bit field (numbered from 0)
Signature · 1 line
1.7.10.4
Re: [Qemu-devel] [PATCH 2/3] bitops: Add rotate functions (rol8, ror8, ...)
Quoted 6 lines · Stefan Weil
These functions were copies from
include/linux/bitopts.h.Signed-off-by Stefan Weil <[email protected]>--- include/qemu/bitops.h | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)
r~
PATCH 3/3 misc: Use new rotate functions
In reply to Stefan Weil--- target-arm/iwmmxt_helper.c | 2 +- tcg/optimize.c | 12 ++++-------- tci.c | 8 ++++---- 3 files changed, 9 insertions(+), 13 deletions(-)
target-arm/iwmmxt_helper.c +1 -1
@@ -577,7 +577,7 @@ uint64_t HELPER(iwmmxt_rorl)(CPUARMState *env, uint64_t x, uint32_t n)uint64_t HELPER (iwmmxt_rorq )(CPUARMState *env,uint64_t x,uint32_t n) { - x = (x >> n) | (x << (64 - n)); + x =ror64 (x, n); env->iwmmxt.cregs[ARM_IWMMXT_wCASF] =NZBIT64 (x);return x; }
tcg/optimize.c +4 -8
@@ -238,20 +238,16 @@ static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)return (int64_t )x >> (int64_t )y;case INDEX_op_rotr_i32: - x = ((uint32_t )x << (32 - y)) | ((uint32_t )x >> y); -return x; +return ror32 (x, y);case INDEX_op_rotr_i64: - x = ((uint64_t )x << (64 - y)) | ((uint64_t )x >> y); -return x; +return ror64 (x, y);case INDEX_op_rotl_i32: - x = ((uint32_t )x << y) | ((uint32_t )x >> (32 - y)); -return x; +return rol32 (x, y);case INDEX_op_rotl_i64: - x = ((uint64_t )x << y) | ((uint64_t )x >> (64 - y)); -return x; +return rol64 (x, y); CASE_OP_32_64(not):return ~x;
tci.c +4 -4
@@ -688,13 +688,13 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr) t0 = *tb_ptr++; t1 =tci_read_ri32 (&tb_ptr); t2 =tci_read_ri32 (&tb_ptr); -tci_write_reg32 (t0, (t1 << t2) | (t1 >> (32 - t2))); +tci_write_reg32 (t0,rol32 (t1, t2));break ;case INDEX_op_rotr_i32: t0 = *tb_ptr++; t1 =tci_read_ri32 (&tb_ptr); t2 =tci_read_ri32 (&tb_ptr); -tci_write_reg32 (t0, (t1 >> t2) | (t1 << (32 - t2))); +tci_write_reg32 (t0,ror32 (t1, t2));break ;#endif #if TCG_TARGET_HAS_deposit_i32
@@ -955,13 +955,13 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr) t0 = *tb_ptr++; t1 =tci_read_ri64 (&tb_ptr); t2 =tci_read_ri64 (&tb_ptr); -tci_write_reg64 (t0, (t1 << t2) | (t1 >> (64 - t2))); +tci_write_reg64 (t0,rol64 (t1, t2));break ;case INDEX_op_rotr_i64: t0 = *tb_ptr++; t1 =tci_read_ri64 (&tb_ptr); t2 =tci_read_ri64 (&tb_ptr); -tci_write_reg64 (t0, (t1 >> t2) | (t1 << (64 - t2))); +tci_write_reg64 (t0,ror64 (t1, t2));break ;#endif #if TCG_TARGET_HAS_deposit_i64
Signature · 1 line
1.7.10.4
Re: [Qemu-devel] [PATCH 3/3] misc: Use new rotate functions
Quoted 6 lines · Stefan Weil
Signed-off-by Stefan Weil <[email protected]>--- target-arm/iwmmxt_helper.c | 2 +- tcg/optimize.c | 12 ++++-------- tci.c | 8 ++++---- 3 files changed, 9 insertions(+), 13 deletions(-)
Reviewed by: Richard Henderson <[email protected]>
r~
Quoted 6 lines · Stefan Weil
The first patch was already sent to qemu-devel and is only included here because patch 3 is based on it. Only patch 1 is needed for qemu-stable.
It looks like shift values of 0 or 32/64 work as expected because the compiler "knows" the pattern used to implement the rotate operation, so the code does not need special handling of some shift values.
Thinking about this closer, the only two behaviors I know of for x >> n, n >= w, w = width of x, are: shift modulo w, or zero.
Both cases work for this usage:
x << 0 | x >> 32
= x | (x >> 0) modulo
= x | x
= x
= x | 0 zero
= x
AFAIK we never actually observe missile launch as a side effect of an out of range shift. And with that in mind, I think the existing implementation of rotate is fine.
r~