Re: [PATCH 1/8] lib/sort: Heapsort implementation of sort()

From: Paulo Marques (pmarques_at_grupopie.com)
Date: 01/31/05

  • Next message: Adrian Bunk: "[2.6 patch] drivers/char/sonypi.c: make 3 structs static"
    Date:	Mon, 31 Jan 2005 17:30:42 +0000
    To: Andreas Gruenbacher <agruen@suse.de>
    
    

    Andreas Gruenbacher wrote:
    > [...]
    >
    > static inline void swap(void *a, void *b, int size)
    > {
    > if (size % sizeof(long)) {
    > char t;
    > do {
    > t = *(char *)a;
    > *(char *)a++ = *(char *)b;
    > *(char *)b++ = t;
    > } while (--size > 0);
    > } else {
    > long t;
    > do {
    > t = *(long *)a;
    > *(long *)a = *(long *)b;
    > *(long *)b = t;
    > size -= sizeof(long);
    > } while (size > sizeof(long));

    You forgot to increment a and b, and this should be "while (size);", no?

    > }
    > }

    Or better yet,

    static inline void swap(void *a, void *b, int size)
    {
            long tl;
             char t;

            while (size >= sizeof(long)) {
                     tl = *(long *)a;
                     *(long *)a = *(long *)b;
                     *(long *)b = tl;
                    a += sizeof(long);
                    b += sizeof(long);
                     size -= sizeof(long);
            }
            while (size) {
                     t = *(char *)a;
                     *(char *)a++ = *(char *)b;
                     *(char *)b++ = t;
                    size--;
             }
    }

    This works better if the size is not a multiple of sizeof(long), but is
    bigger than a long.

    However it seems that this should be put in a generic library function...

    -- 
    Paulo Marques - www.grupopie.com
    All that is necessary for the triumph of evil is that good men do nothing.
    Edmund Burke (1729 - 1797)
    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at  http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at  http://www.tux.org/lkml/
    

  • Next message: Adrian Bunk: "[2.6 patch] drivers/char/sonypi.c: make 3 structs static"

    Relevant Pages