Skip to contentSkip to conversation

bug in arch/ppc/kernel/misc.S: __ashrdi3?

DISCUSSION Frank van Maarseveen 4 participants 6 messages Jul 15, 2005 – Jul 18, 2005

lore
Share & tools
lore
#0 lore raw

I don't really grok the code but an operand seems to be missing and the assembler makes something out of it I don't trust:

_GLOBAL(__ashrdi3)

	...
	rlwinm  r8,r7,0,32      # t3 = (count < 32) ? 32 : 0

Trying it out:

$ cat a.c
void f(void)
{
        __asm("rlwinm  8,7,0,32");
}
$ ppc_4xx-gcc -O2 -c a.c
$ ppc-linux-objdump -S a.o

a.o: file format elf32-powerpc

Disassembly of section .text:

00000000 <f>:
f():

   0:   54 e8 06 b4     rlwinm  r8,r7,0,26,26
   4:   4e 80 00 20     blr
Signature · 1 line
Frank
#1 lore raw
Quoted 6 lines · Frank van Maarseveen

I don't really grok the code but an operand seems to be missing and the assembler makes something out of it I don't trust:

_GLOBAL(__ashrdi3)

	...
	rlwinm  r8,r7,0,32      # t3 = (count < 32) ? 32 : 0

32 == 0x80000000 >> 26

Quoted 3 lines

00000000 <f>:
f():

   0:   54 e8 06 b4     rlwinm  r8,r7,0,26,26

The mask begins at bit 26 and ends at bit 26 (counted from the left).

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#2 lore raw
In reply to Frank van Maarseveen
Quoted 6 lines · Frank van Maarseveen

I don't really grok the code but an operand seems to be missing and the assembler makes something out of it I don't trust:

_GLOBAL(__ashrdi3)

	...
	rlwinm  r8,r7,0,32      # t3 = (count < 32) ? 32 : 0

This is equivalent to r8 = r7 & 32. It will definitely not do what the comment says though. If (count < 64), however, it will do something like r8 = (r7 < 32) ? 0 : 32. Paul, maybe we should dbl check what's going in there ?

Quoted 1 line
   0:   54 e8 06 b4     rlwinm  r8,r7,0,26,26

This is the same.

The assembler, when provided with only one argument after the shift count, will generate ME and MB to represent the mask whose value is given in argument. (If you give an argument which doesn't contain a single contiguous range of 1 bits, I suppose it will error out).

Ben.

#3 lore raw
Quoted 4 lines · Benjamin Herrenschmidt
Quoted 3 lines · Frank van Maarseveen

I don't really grok the code but an operand seems to be missing and the assembler makes something out of it I don't trust:

Quoted 8 lines
Quoted 3 lines

_GLOBAL(__ashrdi3)

	...
	rlwinm  r8,r7,0,32      # t3 = (count < 32) ? 32 : 0

This is equivalent to r8 = r7 & 32. It will definitely not do what the comment says though. If (count < 64), however, it will do something like r8 = (r7 < 32) ? 0 : 32. Paul, maybe we should dbl check what's going in there ?

r7 is count + 32, and ((count + 32) & 32) is equivalent to the expression above if count < 64.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#4 lore raw
Quoted 10 lines · Andreas Schwab
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 6 lines · Frank van Maarseveen

I don't really grok the code but an operand seems to be missing and the assembler makes something out of it I don't trust:

_GLOBAL(__ashrdi3)

	...
	rlwinm  r8,r7,0,32      # t3 = (count < 32) ? 32 : 0
Quoted 7 lines
Quoted 4 lines

This is equivalent to r8 = r7 & 32. It will definitely not do what the comment says though. If (count < 64), however, it will do something like r8 = (r7 < 32) ? 0 : 32. Paul, maybe we should dbl check what's going in there ?

r7 is count + 32, and ((count + 32) & 32) is equivalent to the expression above if count < 64.

Ok, with some context it makes more sense :)

Ben.

#5 lore raw
Quoted 16 lines · Benjamin Herrenschmidt
Quoted 15 lines · Andreas Schwab
Quoted 12 lines · Benjamin Herrenschmidt
Quoted 6 lines · Frank van Maarseveen

I don't really grok the code but an operand seems to be missing and the assembler makes something out of it I don't trust:

_GLOBAL(__ashrdi3)

	...
	rlwinm  r8,r7,0,32      # t3 = (count < 32) ? 32 : 0

This is equivalent to r8 = r7 & 32. It will definitely not do what the comment says though. If (count < 64), however, it will do something like r8 = (r7 < 32) ? 0 : 32. Paul, maybe we should dbl check what's going in there ?

Quoted 4 lines
Quoted 2 lines

r7 is count + 32, and ((count + 32) & 32) is equivalent to the expression above if count < 64.

Ok, with some context it makes more sense :)

Well, I wrote this code, to be fair I actually took most of these code sequences from the multi precision shift appendix found in all good PPC instruction set books. The exception is __ashrdi3 which I modified slightly to eliminate a conditional branch.

I tested it fairly exhaustively before submitting it. So the comments might be wrong (and they will definitely look wrong without the correct context), but the code very likely gives the correct result. After all it has been used for quite some time now without any complaint.

Now obviously the function only works for shift counts below 64, since the C language specification clearly states that the result is undefined for shift counts equal or larger than the bit size of the operand (but I felt against rebooting the machine for an operand outside the allowed range, although a simple "twlgti count,63" might do it).

Maybe this last point should be clearly stated in a comment.

Regards,
Gabriel