1*54c6977eSWolfgang Denk /* 2*54c6977eSWolfgang Denk * Code adapted from uClibc-0.9.30.3 3*54c6977eSWolfgang Denk * 4*54c6977eSWolfgang Denk * It is therefore covered by the GNU LESSER GENERAL PUBLIC LICENSE 5*54c6977eSWolfgang Denk * Version 2.1, February 1999 6*54c6977eSWolfgang Denk * 7*54c6977eSWolfgang Denk * Wolfgang Denk <wd@denx.de> 8*54c6977eSWolfgang Denk */ 9*54c6977eSWolfgang Denk 10*54c6977eSWolfgang Denk /* This code is derived from a public domain shell sort routine by 11*54c6977eSWolfgang Denk * Ray Gardner and found in Bob Stout's snippets collection. The 12*54c6977eSWolfgang Denk * original code is included below in an #if 0/#endif block. 13*54c6977eSWolfgang Denk * 14*54c6977eSWolfgang Denk * I modified it to avoid the possibility of overflow in the wgap 15*54c6977eSWolfgang Denk * calculation, as well as to reduce the generated code size with 16*54c6977eSWolfgang Denk * bcc and gcc. */ 17*54c6977eSWolfgang Denk 18*54c6977eSWolfgang Denk #include <linux/types.h> 19*54c6977eSWolfgang Denk #if 0 20*54c6977eSWolfgang Denk #include <assert.h> 21*54c6977eSWolfgang Denk #else 22*54c6977eSWolfgang Denk #define assert(arg) 23*54c6977eSWolfgang Denk #endif 24*54c6977eSWolfgang Denk 25*54c6977eSWolfgang Denk void qsort(void *base, 26*54c6977eSWolfgang Denk size_t nel, 27*54c6977eSWolfgang Denk size_t width, 28*54c6977eSWolfgang Denk int (*comp)(const void *, const void *)) 29*54c6977eSWolfgang Denk { 30*54c6977eSWolfgang Denk size_t wgap, i, j, k; 31*54c6977eSWolfgang Denk char tmp; 32*54c6977eSWolfgang Denk 33*54c6977eSWolfgang Denk if ((nel > 1) && (width > 0)) { 34*54c6977eSWolfgang Denk assert(nel <= ((size_t)(-1)) / width); /* check for overflow */ 35*54c6977eSWolfgang Denk wgap = 0; 36*54c6977eSWolfgang Denk do { 37*54c6977eSWolfgang Denk wgap = 3 * wgap + 1; 38*54c6977eSWolfgang Denk } while (wgap < (nel-1)/3); 39*54c6977eSWolfgang Denk /* From the above, we know that either wgap == 1 < nel or */ 40*54c6977eSWolfgang Denk /* ((wgap-1)/3 < (int) ((nel-1)/3) <= (nel-1)/3 ==> wgap < nel. */ 41*54c6977eSWolfgang Denk wgap *= width; /* So this can not overflow if wnel doesn't. */ 42*54c6977eSWolfgang Denk nel *= width; /* Convert nel to 'wnel' */ 43*54c6977eSWolfgang Denk do { 44*54c6977eSWolfgang Denk i = wgap; 45*54c6977eSWolfgang Denk do { 46*54c6977eSWolfgang Denk j = i; 47*54c6977eSWolfgang Denk do { 48*54c6977eSWolfgang Denk register char *a; 49*54c6977eSWolfgang Denk register char *b; 50*54c6977eSWolfgang Denk 51*54c6977eSWolfgang Denk j -= wgap; 52*54c6977eSWolfgang Denk a = j + ((char *)base); 53*54c6977eSWolfgang Denk b = a + wgap; 54*54c6977eSWolfgang Denk if ((*comp)(a, b) <= 0) { 55*54c6977eSWolfgang Denk break; 56*54c6977eSWolfgang Denk } 57*54c6977eSWolfgang Denk k = width; 58*54c6977eSWolfgang Denk do { 59*54c6977eSWolfgang Denk tmp = *a; 60*54c6977eSWolfgang Denk *a++ = *b; 61*54c6977eSWolfgang Denk *b++ = tmp; 62*54c6977eSWolfgang Denk } while (--k); 63*54c6977eSWolfgang Denk } while (j >= wgap); 64*54c6977eSWolfgang Denk i += width; 65*54c6977eSWolfgang Denk } while (i < nel); 66*54c6977eSWolfgang Denk wgap = (wgap - width)/3; 67*54c6977eSWolfgang Denk } while (wgap); 68*54c6977eSWolfgang Denk } 69*54c6977eSWolfgang Denk } 70