Last active
April 23, 2022 13:07
-
-
Save mrbid/c1672cd11ece2ebea11107b0a0542a9a to your computer and use it in GitHub Desktop.
Bitboolbench Solution 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here is also a version with always_inline; https://godbolt.org/z/1hcvv65Pv | |
// performance is basically the same. | |
#include <stdio.h> | |
#define uint unsigned int | |
struct Bits | |
{ | |
unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; | |
}; | |
union CBits | |
{ | |
struct Bits bits; | |
unsigned char byte; | |
}; | |
int main() | |
{ | |
uint i = 0; | |
unsigned char by; | |
union CBits a; | |
// set bools | |
a.bits.b0 = i; | |
a.bits.b1 = i-1; | |
a.bits.b2 = i-2; | |
a.bits.b3 = i-3; | |
a.bits.b4 = i-4; | |
a.bits.b5 = i-5; | |
a.bits.b6 = i-6; | |
a.bits.b7 = i-7; | |
// bools to byte | |
by = a.byte; | |
// byte to bools | |
uint bo[8] ={ | |
a.bits.b0, | |
a.bits.b1, | |
a.bits.b2, | |
a.bits.b3, | |
a.bits.b4, | |
a.bits.b5, | |
a.bits.b6, | |
a.bits.b7, | |
}; | |
printf("%c\n", by + bo[0] + bo[1] + bo[2] + bo[3] + bo[4] + bo[5] + bo[6] + bo[7]); | |
return 0; | |
} | |
/* | |
main: | |
push rbp | |
mov rbp, rsp | |
sub rsp, 48 | |
mov DWORD PTR [rbp-4], 0 | |
// set bools | |
mov eax, DWORD PTR [rbp-4] | |
and eax, 1 | |
and eax, 1 | |
mov edx, eax | |
movzx eax, BYTE PTR [rbp-12] | |
and eax, -2 | |
or eax, edx | |
mov BYTE PTR [rbp-12], al | |
mov eax, DWORD PTR [rbp-4] | |
sub eax, 1 | |
and eax, 1 | |
and eax, 1 | |
lea edx, [rax+rax] | |
movzx eax, BYTE PTR [rbp-12] | |
and eax, -3 | |
or eax, edx | |
mov BYTE PTR [rbp-12], al | |
mov eax, DWORD PTR [rbp-4] | |
sub eax, 2 | |
and eax, 1 | |
and eax, 1 | |
lea edx, [0+rax*4] | |
movzx eax, BYTE PTR [rbp-12] | |
and eax, -5 | |
or eax, edx | |
mov BYTE PTR [rbp-12], al | |
mov eax, DWORD PTR [rbp-4] | |
sub eax, 3 | |
and eax, 1 | |
and eax, 1 | |
lea edx, [0+rax*8] | |
movzx eax, BYTE PTR [rbp-12] | |
and eax, -9 | |
or eax, edx | |
mov BYTE PTR [rbp-12], al | |
mov eax, DWORD PTR [rbp-4] | |
sub eax, 4 | |
and eax, 1 | |
and eax, 1 | |
sal eax, 4 | |
mov edx, eax | |
movzx eax, BYTE PTR [rbp-12] | |
and eax, -17 | |
or eax, edx | |
mov BYTE PTR [rbp-12], al | |
mov eax, DWORD PTR [rbp-4] | |
sub eax, 5 | |
and eax, 1 | |
and eax, 1 | |
sal eax, 5 | |
mov edx, eax | |
movzx eax, BYTE PTR [rbp-12] | |
and eax, -33 | |
or eax, edx | |
mov BYTE PTR [rbp-12], al | |
mov eax, DWORD PTR [rbp-4] | |
sub eax, 6 | |
and eax, 1 | |
and eax, 1 | |
sal eax, 6 | |
mov edx, eax | |
movzx eax, BYTE PTR [rbp-12] | |
and eax, -65 | |
or eax, edx | |
mov BYTE PTR [rbp-12], al | |
mov eax, DWORD PTR [rbp-4] | |
sub eax, 7 | |
and eax, 1 | |
sal eax, 7 | |
mov edx, eax | |
movzx eax, BYTE PTR [rbp-12] | |
and eax, 127 | |
or eax, edx | |
mov BYTE PTR [rbp-12], al | |
// bools to byte | |
movzx eax, BYTE PTR [rbp-12] | |
mov BYTE PTR [rbp-5], al | |
// byte to bools | |
movzx eax, BYTE PTR [rbp-12] | |
and eax, 1 | |
movzx eax, al | |
mov DWORD PTR [rbp-48], eax | |
movzx eax, BYTE PTR [rbp-12] | |
shr al | |
and eax, 1 | |
movzx eax, al | |
mov DWORD PTR [rbp-44], eax | |
movzx eax, BYTE PTR [rbp-12] | |
shr al, 2 | |
and eax, 1 | |
movzx eax, al | |
mov DWORD PTR [rbp-40], eax | |
movzx eax, BYTE PTR [rbp-12] | |
shr al, 3 | |
and eax, 1 | |
movzx eax, al | |
mov DWORD PTR [rbp-36], eax | |
movzx eax, BYTE PTR [rbp-12] | |
shr al, 4 | |
and eax, 1 | |
movzx eax, al | |
mov DWORD PTR [rbp-32], eax | |
movzx eax, BYTE PTR [rbp-12] | |
shr al, 5 | |
and eax, 1 | |
movzx eax, al | |
mov DWORD PTR [rbp-28], eax | |
movzx eax, BYTE PTR [rbp-12] | |
shr al, 6 | |
and eax, 1 | |
movzx eax, al | |
mov DWORD PTR [rbp-24], eax | |
movzx eax, BYTE PTR [rbp-12] | |
shr al, 7 | |
movzx eax, al | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment