mirror of
https://github.com/yuzu-emu/unicorn
synced 2024-11-24 23:48:27 +00:00
bitops.h: Implement half-shuffle and half-unshuffle ops
A half-shuffle operation takes a word with zeros in the high half: 0000 0000 0000 0000 ABCD EFGH IJKL MNOP and spreads the bits out so they are in every other bit of the word: 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P A half-unshuffle performs the reverse operation. Provide functions in bitops.h which implement these operations for 32-bit and 64-bit inputs, and add tests for them. Backports commit b355438de52d0782983bf4bdc47936189a0c988b from qemu
This commit is contained in:
parent
851dec945d
commit
f893dacef0
1 changed files with 108 additions and 0 deletions
|
@ -419,4 +419,112 @@ static inline uint64_t deposit64(uint64_t value, int start, int length,
|
|||
return (value & ~mask) | ((fieldval << start) & mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* half_shuffle32:
|
||||
* @value: 32-bit value (of which only the bottom 16 bits are of interest)
|
||||
*
|
||||
* Given an input value:
|
||||
* xxxx xxxx xxxx xxxx ABCD EFGH IJKL MNOP
|
||||
* return the value where the bottom 16 bits are spread out into
|
||||
* the odd bits in the word, and the even bits are zeroed:
|
||||
* 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P
|
||||
*
|
||||
* Any bits set in the top half of the input are ignored.
|
||||
*
|
||||
* Returns: the shuffled bits.
|
||||
*/
|
||||
static inline uint32_t half_shuffle32(uint32_t x)
|
||||
{
|
||||
/* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
|
||||
* It ignores any bits set in the top half of the input.
|
||||
*/
|
||||
x = ((x & 0xFF00) << 8) | (x & 0x00FF);
|
||||
x = ((x << 4) | x) & 0x0F0F0F0F;
|
||||
x = ((x << 2) | x) & 0x33333333;
|
||||
x = ((x << 1) | x) & 0x55555555;
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* half_shuffle64:
|
||||
* @value: 64-bit value (of which only the bottom 32 bits are of interest)
|
||||
*
|
||||
* Given an input value:
|
||||
* xxxx xxxx xxxx .... xxxx xxxx ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
|
||||
* return the value where the bottom 32 bits are spread out into
|
||||
* the odd bits in the word, and the even bits are zeroed:
|
||||
* 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N .... 0U0V 0W0X 0Y0Z 0a0b 0c0d 0e0f
|
||||
*
|
||||
* Any bits set in the top half of the input are ignored.
|
||||
*
|
||||
* Returns: the shuffled bits.
|
||||
*/
|
||||
static inline uint64_t half_shuffle64(uint64_t x)
|
||||
{
|
||||
/* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
|
||||
* It ignores any bits set in the top half of the input.
|
||||
*/
|
||||
x = ((x & 0xFFFF0000ULL) << 16) | (x & 0xFFFF);
|
||||
x = ((x << 8) | x) & 0x00FF00FF00FF00FFULL;
|
||||
x = ((x << 4) | x) & 0x0F0F0F0F0F0F0F0FULL;
|
||||
x = ((x << 2) | x) & 0x3333333333333333ULL;
|
||||
x = ((x << 1) | x) & 0x5555555555555555ULL;
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* half_unshuffle32:
|
||||
* @value: 32-bit value (of which only the odd bits are of interest)
|
||||
*
|
||||
* Given an input value:
|
||||
* xAxB xCxD xExF xGxH xIxJ xKxL xMxN xOxP
|
||||
* return the value where all the odd bits are compressed down
|
||||
* into the low half of the word, and the high half is zeroed:
|
||||
* 0000 0000 0000 0000 ABCD EFGH IJKL MNOP
|
||||
*
|
||||
* Any even bits set in the input are ignored.
|
||||
*
|
||||
* Returns: the unshuffled bits.
|
||||
*/
|
||||
static inline uint32_t half_unshuffle32(uint32_t x)
|
||||
{
|
||||
/* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
|
||||
* where it is called an inverse half shuffle.
|
||||
*/
|
||||
x &= 0x55555555;
|
||||
x = ((x >> 1) | x) & 0x33333333;
|
||||
x = ((x >> 2) | x) & 0x0F0F0F0F;
|
||||
x = ((x >> 4) | x) & 0x00FF00FF;
|
||||
x = ((x >> 8) | x) & 0x0000FFFF;
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* half_unshuffle64:
|
||||
* @value: 64-bit value (of which only the odd bits are of interest)
|
||||
*
|
||||
* Given an input value:
|
||||
* xAxB xCxD xExF xGxH xIxJ xKxL xMxN .... xUxV xWxX xYxZ xaxb xcxd xexf
|
||||
* return the value where all the odd bits are compressed down
|
||||
* into the low half of the word, and the high half is zeroed:
|
||||
* 0000 0000 0000 .... 0000 0000 ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
|
||||
*
|
||||
* Any even bits set in the input are ignored.
|
||||
*
|
||||
* Returns: the unshuffled bits.
|
||||
*/
|
||||
static inline uint64_t half_unshuffle64(uint64_t x)
|
||||
{
|
||||
/* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
|
||||
* where it is called an inverse half shuffle.
|
||||
*/
|
||||
x &= 0x5555555555555555ULL;
|
||||
x = ((x >> 1) | x) & 0x3333333333333333ULL;
|
||||
x = ((x >> 2) | x) & 0x0F0F0F0F0F0F0F0FULL;
|
||||
x = ((x >> 4) | x) & 0x00FF00FF00FF00FFULL;
|
||||
x = ((x >> 8) | x) & 0x0000FFFF0000FFFFULL;
|
||||
x = ((x >> 16) | x) & 0x00000000FFFFFFFFULL;
|
||||
return x;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue