lkml.org 
[lkml]   [2018]   [Nov]   [27]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRE: [PATCH v2 1/4] x86/hyper-v: move synic/stimer control structures definitions to hyperv-tlfs.h
Date
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

\
 
 \ /
  Last update: 2018-11-27 17:33    [W:0.105 / U:0.232 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site