Skip to contentSkip to conversation

ffs() compile-time

DISCUSSION Ivo van Doorn 1 participant 1 message May 29, 2008

lore
Share & tools
lore
#0 lore raw

Hi,

In rt2x00 I define all registers through the FIELD32(__mask) defines, which look like this:

struct rt2x00_field32 {
	u32 bit_offset;
	u32 bit_mask;
};

#define FIELD32(__mask)

({
	BUILD_BUG_ON(!(__mask) ||
		     !is_valid_mask(__mask) ||	
		     (__mask) != (u32)(__mask));
	(struct rt2x00_field32) {
		__ffs(__mask), (__mask)
	};
})

As you see everywhere where a define which contains FIELD32() will make a call to __ffs(). However this is suboptimal since the __mask which is passed to __ffs() is a constant during compile time.
So it would be very nice when I can make use of a compile-time version of __ffs() to make GCC find the first bit during compile-time this would increase performance and help reduce the module size of each rt2x00 driver.

However I am a bit lost at what a proper solution would be, GCC provides __builtin_ffs() but would that perform the compile-time calculation or not. Another solution would be creating a macro like:

#define _COMPILE_FFS4(x)\

({			\
	(x) & 0x1 ? 1 :	\
	(x) & 0x2 ? 2 :	\
	(x) & 0x4 ? 3 :	\
	(x) & 0x8 ? 4 :	\
	0;		\
})

And have _COMPILE_FFS8, _COMPILE_FFS16 and _COMPILE_FFS32 use above macro. But I am kind of hesitant about that macro as well, because it just doesn't feel right. :S

Any advice is welcome. :)
Thanks,

Ivo