[U-Boot] [PATCH 2/3] Add generic bit operations
Wolfgang Denk
wd at denx.de
Thu Jun 4 13:45:22 CEST 2009
Dear Daniel Mack,
In message <1244111241-32735-3-git-send-email-daniel at caiaq.de> you wrote:
> This adds generic bit operations for all platforms and enables includes
> the implementations from asm-arm.
Be careful. I am not sure if a generic definition is even possible.
In any case, it is extremely risky to use in the wrong way.
It's nothing but faux ami.
My recommendation is NEVER to use any code based on bit numbers (even
if this is what some chip documentation may refer to) but ONLY use
appropriate masks instead.
> new file mode 100644
> index 0000000..088e5da
> --- /dev/null
> +++ b/include/asm-generic/bitops.h
> @@ -0,0 +1,151 @@
> +#ifndef _ASM_GENERIC_BITOPS_H_
> +#define _ASM_GENERIC_BITOPS_H_
> +
> +#include <asm/types.h>
> +
> +#define BIT(nr) (1UL << (nr))
> +#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
> +#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
> +#define BITS_PER_BYTE 8
> +#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
You see, this is plain wrong on PowerPC.
On PowerPC, bit number 0 is the most significant bit, not the least
significant one as you assume here. So using this well-intended code
on a PowerPC system will most likely get you in trouble.
[And you cannot even use a generic definition for PowerPC, as some-
thing like "#define BIT(nr) (0x80000000 >> (nr))" will only work on
32 bit systems but be wrong on 64 bit ones.]
Let's get rid of this stuff, it is confusing.
Best regards,
Wolfgang Denk
--
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"The one charm of marriage is that it makes a life of deception a
neccessity." - Oscar Wilde
More information about the U-Boot
mailing list