public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Improved Swapping Method In sort.c
@ 2008-04-30 17:09 Soumyadip Das Mahapatra
  2008-04-30 17:30 ` Jan Engelhardt
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Soumyadip Das Mahapatra @ 2008-04-30 17:09 UTC (permalink / raw)
  To: linux-kernel

Hello everybody,
The swapping method (in function void u32_swap() line no.. 14 to 16) in lib/sort.c can be improved by using the following code
*(u32 *)b ^= *(u32 *)a ^= *(u32 *)b ^= *(u32 *)a instead of the code given. This code
is not using third variable thus not consuming another memory. And I dont see any significance in
using *int size* argument. so the function should be
static void u32_swap(void *a, void *b)
{
    *(u32 *)b ^= *(u32 *)a ^= *(u32 *)b ^= *(u32 *)a;
}




      Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Improved Swapping Method In sort.c
  2008-04-30 17:09 Improved Swapping Method In sort.c Soumyadip Das Mahapatra
@ 2008-04-30 17:30 ` Jan Engelhardt
  2008-04-30 17:33 ` Jan Engelhardt
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Jan Engelhardt @ 2008-04-30 17:30 UTC (permalink / raw)
  To: Soumyadip Das Mahapatra; +Cc: linux-kernel


On Wednesday 2008-04-30 19:09, Soumyadip Das Mahapatra wrote:

>Hello everybody, The swapping method (in function void u32_swap() line no.. 14
>to 16) in lib/sort.c can be improved by using the following code *(u32 *)b ^=
>*(u32 *)a ^= *(u32 *)b ^= *(u32 *)a instead of the code given.

GCC is smart enough to optimize the current instructions into a
bswap instruction or similar. While with xor, some are said to fail -
and three xor operations are surely slower than a swap if there is
a swap instruction.

Never heard of that?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Improved Swapping Method In sort.c
  2008-04-30 17:09 Improved Swapping Method In sort.c Soumyadip Das Mahapatra
  2008-04-30 17:30 ` Jan Engelhardt
@ 2008-04-30 17:33 ` Jan Engelhardt
  2008-04-30 18:03   ` Sami Farin
  2008-04-30 18:46 ` David Newall
  2008-05-02  5:05 ` H. Peter Anvin
  3 siblings, 1 reply; 6+ messages in thread
From: Jan Engelhardt @ 2008-04-30 17:33 UTC (permalink / raw)
  To: Soumyadip Das Mahapatra; +Cc: linux-kernel


On Wednesday 2008-04-30 19:09, Soumyadip Das Mahapatra wrote:

>Hello everybody,
>The swapping method (in function void u32_swap() line no.. 14 to 16) in lib/sort.c can be improved by using the following code
>*(u32 *)b ^= *(u32 *)a ^= *(u32 *)b ^= *(u32 *)a instead of the code given. This code
>is not using third variable thus not consuming another memory. And I dont see any significance in
>using *int size* argument. so the function should be
>static void u32_swap(void *a, void *b)
>{
>    *(u32 *)b ^= *(u32 *)a ^= *(u32 *)b ^= *(u32 *)a;
>}

That proposal looks like buggy.

19:32 yaguchi:/dev/shm > cat ui.c 
#include <stdint.h>
#include <stdio.h>

static void u32_swap(void *a, void *b)
{
        *(uint32_t *)b ^= *(uint32_t *)a 
                        ^= *(uint32_t *)b
                        ^= *(uint32_t *)a;
}

int main(void)
{
        uint32_t x = 5, y = 7;
        printf("%u %u\n", x, y);
        u32_swap(&x, &y);
        printf("%u %u\n", x, y);
}
19:32 yaguchi:/dev/shm > ./a.out 
5 7
7 0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Improved Swapping Method In sort.c
  2008-04-30 17:33 ` Jan Engelhardt
@ 2008-04-30 18:03   ` Sami Farin
  0 siblings, 0 replies; 6+ messages in thread
From: Sami Farin @ 2008-04-30 18:03 UTC (permalink / raw)
  To: linux-kernel

On Wed, Apr 30, 2008 at 19:33:42 +0200, Jan Engelhardt wrote:
> 
> On Wednesday 2008-04-30 19:09, Soumyadip Das Mahapatra wrote:
> 
> >Hello everybody,
> >The swapping method (in function void u32_swap() line no.. 14 to 16) in lib/sort.c can be improved by using the following code
> >*(u32 *)b ^= *(u32 *)a ^= *(u32 *)b ^= *(u32 *)a instead of the code given. This code
> >is not using third variable thus not consuming another memory. And I dont see any significance in
> >using *int size* argument. so the function should be
> >static void u32_swap(void *a, void *b)
> >{
> >    *(u32 *)b ^= *(u32 *)a ^= *(u32 *)b ^= *(u32 *)a;
> >}
> 
> That proposal looks like buggy.
> 
> 19:32 yaguchi:/dev/shm > cat ui.c 
> #include <stdint.h>
> #include <stdio.h>
> 
> static void u32_swap(void *a, void *b)
> {
>         *(uint32_t *)b ^= *(uint32_t *)a 
>                         ^= *(uint32_t *)b
>                         ^= *(uint32_t *)a;
> }
> 
> int main(void)
> {
>         uint32_t x = 5, y = 7;
>         printf("%u %u\n", x, y);
>         u32_swap(&x, &y);
>         printf("%u %u\n", x, y);
> }
> 19:32 yaguchi:/dev/shm > ./a.out 
> 5 7
> 7 0

This happens because u32_swap has undefined behavior.

Try this instead:

static void u32_swap(void *a, void *b)
{
        *(uint32_t *)a ^= *(uint32_t *)b;
        *(uint32_t *)b ^= *(uint32_t *)a;
        *(uint32_t *)a ^= *(uint32_t *)b;
}

But still, this change to Linux is not necessary,
it actually generates worse code. 

-- 
Do what you love because life is too short for anything else.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Improved Swapping Method In sort.c
  2008-04-30 17:09 Improved Swapping Method In sort.c Soumyadip Das Mahapatra
  2008-04-30 17:30 ` Jan Engelhardt
  2008-04-30 17:33 ` Jan Engelhardt
@ 2008-04-30 18:46 ` David Newall
  2008-05-02  5:05 ` H. Peter Anvin
  3 siblings, 0 replies; 6+ messages in thread
From: David Newall @ 2008-04-30 18:46 UTC (permalink / raw)
  To: Soumyadip Das Mahapatra; +Cc: linux-kernel

Soumyadip Das Mahapatra wrote:
> static void u32_swap(void *a, void *b)
> {
>     *(u32 *)b ^= *(u32 *)a ^= *(u32 *)b ^= *(u32 *)a;
> }
>   

That was posted to comp.lang.c 20 years ago or more.  It's not something
that the compiler is likely to be able to optimize (unlike the normal
swap, using a temporary variable.)

Another cute, temp-free swap, from the same era, is: b += a -= b; a = b - a.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Improved Swapping Method In sort.c
  2008-04-30 17:09 Improved Swapping Method In sort.c Soumyadip Das Mahapatra
                   ` (2 preceding siblings ...)
  2008-04-30 18:46 ` David Newall
@ 2008-05-02  5:05 ` H. Peter Anvin
  3 siblings, 0 replies; 6+ messages in thread
From: H. Peter Anvin @ 2008-05-02  5:05 UTC (permalink / raw)
  To: Soumyadip Das Mahapatra; +Cc: linux-kernel

Soumyadip Das Mahapatra wrote:
> Hello everybody,
> The swapping method (in function void u32_swap() line no.. 14 to 16) in lib/sort.c can be improved by using the following code
> *(u32 *)b ^= *(u32 *)a ^= *(u32 *)b ^= *(u32 *)a instead of the code given. This code
> is not using third variable thus not consuming another memory. And I dont see any significance in
> using *int size* argument. so the function should be
> static void u32_swap(void *a, void *b)
> {
>     *(u32 *)b ^= *(u32 *)a ^= *(u32 *)b ^= *(u32 *)a;
> }
> 

Not only is this invalid C, but it produces massively worse code.

	-hpa

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-05-02  5:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-04-30 17:09 Improved Swapping Method In sort.c Soumyadip Das Mahapatra
2008-04-30 17:30 ` Jan Engelhardt
2008-04-30 17:33 ` Jan Engelhardt
2008-04-30 18:03   ` Sami Farin
2008-04-30 18:46 ` David Newall
2008-05-02  5:05 ` H. Peter Anvin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox