Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/misoc
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2a7ff97
Choose a base ref
...
head repository: m-labs/misoc
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 59f4490
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Jun 4, 2012

  1. software/base/limits.h: add some more

    Sebastien Bourdeauducq committed Jun 4, 2012
    Copy the full SHA
    b4f49cc View commit details
  2. base/stdlib.h: abs/labs

    Sebastien Bourdeauducq committed Jun 4, 2012
    Copy the full SHA
    2dc346d View commit details
  3. software/libbase: qsort

    Sebastien Bourdeauducq committed Jun 4, 2012
    Copy the full SHA
    59f4490 View commit details
Showing with 233 additions and 2 deletions.
  1. +6 −0 software/include/base/limits.h
  2. +11 −1 software/include/base/stdlib.h
  3. +1 −1 software/libbase/Makefile
  4. +215 −0 software/libbase/qsort.c
6 changes: 6 additions & 0 deletions software/include/base/limits.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#ifndef __LIMITS_H
#define __LIMITS_H

#define ULONG_MAX 0xffffffff

#define UINT_MAX 0xffffffff
#define INT_MIN 0x80000000
#define INT_MAX 0x7fffffff

#define USHRT_MAX 0xffff
#define SHRT_MIN 0x8000
#define SHRT_MAX 0x7fff

#define UCHAR_MAX 0xff

#define CHAR_BIT 8

#endif /* __LIMITS_H */
12 changes: 11 additions & 1 deletion software/include/base/stdlib.h
Original file line number Diff line number Diff line change
@@ -32,7 +32,15 @@
#define likely(x) x
#define unlikely(x) x

#define abs(x) ((x) > 0 ? (x) : -(x))
static inline int abs(int x)
{
return x > 0 ? x : -x;
}

static inline long int labs(long int x)
{
return x > 0 ? x : -x;
}

unsigned long strtoul(const char *nptr, char **endptr, int base);
int skip_atoi(const char **s);
@@ -52,6 +60,8 @@ unsigned int rand(void);
void srand(unsigned int seed);
void abort(void);

void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));

/*
* The following functions are not provided by this library.
*/
2 changes: 1 addition & 1 deletion software/libbase/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
M2DIR=../..
include $(M2DIR)/software/common.mak

OBJECTS=setjmp.o libc.o errno.o crc16.o crc32.o console.o timer.o system.o board.o uart.o vsnprintf.o strtod.o
OBJECTS=setjmp.o libc.o errno.o crc16.o crc32.o console.o timer.o system.o board.o uart.o vsnprintf.o strtod.o qsort.o

all: libbase.a

215 changes: 215 additions & 0 deletions software/libbase/qsort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/****************************************************************************
* lib/stdlib/lib_qsort.c
*
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Leveraged from:
*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
****************************************************************************/

#include <stdlib.h>

#define min(a, b) (a) < (b) ? a : b

#define swapcode(TYPE, parmi, parmj, n) \
{ \
long i = (n) / sizeof (TYPE); \
register TYPE *pi = (TYPE *) (parmi); \
register TYPE *pj = (TYPE *) (parmj); \
do { \
register TYPE t = *pi; \
*pi++ = *pj; \
*pj++ = t; \
} while (--i > 0); \
}

#define SWAPINIT(a, size) \
swaptype = ((char *)a - (char *)0) % sizeof(long) || \
size % sizeof(long) ? 2 : size == sizeof(long)? 0 : 1;

#define swap(a, b) \
if (swaptype == 0) \
{ \
long t = *(long *)(a); \
*(long *)(a) = *(long *)(b); \
*(long *)(b) = t; \
} \
else \
{ \
swapfunc(a, b, size, swaptype); \
}

#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)

static inline void swapfunc(char *a, char *b, int n, int swaptype);
static inline char *med3(char *a, char *b, char *c,
int (*compar)(const void *, const void *));

static inline void swapfunc(char *a, char *b, int n, int swaptype)
{
if(swaptype <= 1)
{
swapcode(long, a, b, n)
}
else
{
swapcode(char, a, b, n)
}
}

static inline char *med3(char *a, char *b, char *c,
int (*compar)(const void *, const void *))
{
return compar(a, b) < 0 ?
(compar(b, c) < 0 ? b : (compar(a, c) < 0 ? c : a ))
:(compar(b, c) > 0 ? b : (compar(a, c) < 0 ? a : c ));
}

/****************************************************************************
* Name: qsort
*
* Description:
* Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
*
****************************************************************************/

void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *))
{
char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
int d, r, swaptype, swap_cnt;

loop:
SWAPINIT(base, size);
swap_cnt = 0;
if (nmemb < 7)
{
for (pm = (char *) base + size; pm < (char *) base + nmemb * size; pm += size)
{
for (pl = pm; pl > (char *) base && compar(pl - size, pl) > 0; pl -= size)
{
swap(pl, pl - size);
}
}
return;
}

pm = (char *) base + (nmemb / 2) * size;
if (nmemb > 7)
{
pl = base;
pn = (char *) base + (nmemb - 1) * size;
if (nmemb > 40)
{
d = (nmemb / 8) * size;
pl = med3(pl, pl + d, pl + 2 * d, compar);
pm = med3(pm - d, pm, pm + d, compar);
pn = med3(pn - 2 * d, pn - d, pn, compar);
}
pm = med3(pl, pm, pn, compar);
}
swap(base, pm);
pa = pb = (char *) base + size;

pc = pd = (char *) base + (nmemb - 1) * size;
for (;;)
{
while (pb <= pc && (r = compar(pb, base)) <= 0)
{
if (r == 0)
{
swap_cnt = 1;
swap(pa, pb);
pa += size;
}
pb += size;
}
while (pb <= pc && (r = compar(pc, base)) >= 0)
{
if (r == 0)
{
swap_cnt = 1;
swap(pc, pd);
pd -= size;
}
pc -= size;
}

if (pb > pc)
{
break;
}

swap(pb, pc);
swap_cnt = 1;
pb += size;
pc -= size;
}

if (swap_cnt == 0)
{
/* Switch to insertion sort */

for (pm = (char *) base + size; pm < (char *) base + nmemb * size; pm += size)
{
for (pl = pm; pl > (char *) base && compar(pl - size, pl) > 0; pl -= size)
{
swap(pl, pl - size);
}
}
return;
}

pn = (char *) base + nmemb * size;
r = min(pa - (char *)base, pb - pa);
vecswap(base, pb - r, r);
r = min(pd - pc, pn - pd - size);
vecswap(pb, pn - r, r);

if ((r = pb - pa) > size)
{
qsort(base, r / size, size, compar);
}

if ((r = pd - pc) > size)
{
/* Iterate rather than recurse to save stack space */
base = pn - r;
nmemb = r / size;
goto loop;
}
}