[PATCH] powerpc: emulate power5 popcntb instruction

Segher Boessenkool segher at kernel.crashing.org
Fri Aug 18 05:24:44 EST 2006


> +static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
> +{
> +	u32 ra,rs;
> +	u32 countreg,tmp;
> +	u32 x,y;
> +
> +	ra = (instword >> 16) & 0x1f;
> +	rs = (instword >> 21) & 0x1f;
> +	countreg = regs->gpr[rs];
> +	regs->gpr[ra] = 0;
> +
> +	/* y=4 bytes, x=8 bits/byte */
> +	for (y = 0; y < 4; y++) {
> +		for (tmp = 0, x = 0; x < 8; x++)
> +			if (countreg & (0x1 << (x + 8 * y))) tmp++;
> +		regs->gpr[ra] += (tmp <<  8 * y );
> +	}
> +	return 0;
> +}

x in, x out:

	x -= (x >> 1) & 0x5555555555555555;
	x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
	x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f;

(Your code only runs on 4 bytes; shouldn't it be 8?)


Segher




More information about the Linuxppc-dev mailing list