Re: Improved Swapping Method In sort.c
- From: Sami Farin <safari-kernel@xxxxxxxxxxxxx>
- Date: Wed, 30 Apr 2008 21:03:35 +0300
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.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
- References:
- Improved Swapping Method In sort.c
- From: Soumyadip Das Mahapatra
- Re: Improved Swapping Method In sort.c
- From: Jan Engelhardt
- Improved Swapping Method In sort.c
- Prev by Date: Re: [patch/rfc 2.6.25-git] gpio: sysfs interface
- Next by Date: Re: Problem with 8250_pnp Module and using ttyS0 as console
- Previous by thread: Re: Improved Swapping Method In sort.c
- Next by thread: Re: Improved Swapping Method In sort.c
- Index(es):
Relevant Pages
|