RE: [PATCH v2 1/4] x86/hyper-v: move synic/stimer control structures definitions to hyperv-tlfs.h

From: Vitaly Kuznetsov
Date: Tue Nov 27 2018 - 11:32:15 EST


Out of pure curiosity I decided to check what 'gcc -O3' produces when we
use bitfields and masks. As of 'gcc version 8.2.1 20181105 (Red Hat 8.2.1-5) (GCC)'

1) bitfields:

struct abc {
int enabled:1;
int _pad:7;
int vec:8;
};

int is_good(struct abc *s) {
if (s->enabled)
return s->vec;
else
return 0;
}

results in

is_good:
xorl %eax, %eax
testb $1, (%rdi)
je .L1
movsbl 1(%rdi), %eax
.L1:
ret

2) masks

#include <stdint.h>

#define S_ENABLED 1
#define S_VEC_MASK 0xff00
#define S_VEC_SHIFT 8

int is_good(uint16_t *s) {
if (*s & S_ENABLED)
return (*s & S_VEC_MASK) >> S_VEC_SHIFT;
else
return 0;
}

results in

is_good:
movzwl (%rdi), %edx
movzbl %dh, %eax
andl $1, %edx
movl $0, %edx
cmove %edx, %eax
ret

so bitfields version looks somewhat more efficient. I'm not sure if my
example is too synthetic though.

--
Vitaly