bitops api for set_bit function

Wes Edens wes.k.edens at
Sun Jul 21 21:30:25 EDT 2019

Hi all,

I have been looking at the bitops api for various architectures. I am
curious why some architectures define the 'nr' argument as a signed
int and some architectures define it as an unsigned int. there are
even some architectures (mips, sparc) that mix the prototypes between
atomic and non-atomic functions (set_bit and __set_bit). The x86
architecture uses a long as its 'nr' argument.

the atomic operations documentation gives a prototype of the bit
operations using unsigned int for 'nr'.

i've looked at the commit where the sparc architecture changes from
using signed to unsigned, but the commit message didn't have an
explanation on that part of it.

i've dug through the uses of this function and most drivers/kernel
subsystems use this to set bits in a status bitfield where the 'nr'
argument is a #define. it doesn't seem to matter which type the
argument is, but i'm curious why there are different implementations.

asm-generic implementation:
static inline void __set_bit(int nr, volatile unsigned long *addr)
unsigned long mask = BIT_MASK(nr);
unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

*p  |= mask;

sparc implementation:
static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
unsigned long *ADDR, mask;

ADDR = ((unsigned long *) addr) + (nr >> 5);
mask = 1 << (nr & 31);

(void) ___set_bit(ADDR, mask);

thank you,
wes edens

More information about the Kernelnewbies mailing list