Browse Source

use string.h functions in newlib (-lc)

pull/2/head
Andrew Waterman 13 years ago
parent
commit
8bad38e1a9
  1. 2
      configure
  2. 1
      configure.ac
  3. 58
      pk/memset.c
  4. 2
      pk/pk.mk.in
  5. 33
      pk/strlen.c

2
configure

@ -3955,6 +3955,8 @@ CFLAGS="-Wall -Os -std=gnu99 -Wno-unused"
LIBS="-lgcc"
LIBS="-lc"
{ $as_echo "$as_me:${as_lineno-$LINENO}: adding default configure arg: --host=riscv" >&5

1
configure.ac

@ -73,6 +73,7 @@ AC_HEADER_STDC
AC_SUBST([CFLAGS], ["-Wall -Os -std=gnu99 -Wno-unused"])
AC_SUBST([LIBS], ["-lgcc"])
AC_SUBST([LIBS], ["-lc"])
AX_DEFAULT_CONFIGURE_ARG([--host=riscv])

58
pk/memset.c

@ -1,58 +0,0 @@
// See LICENSE for license details.
#include <limits.h>
#include <string.h>
#if ULONG_MAX != 18446744073709551615UL && ULONG_MAX != 4294967295UL
# error need sizeof(long) == 4 or sizeof(long) == 8
#endif
void* memset(void* m, int ch, size_t s)
{
size_t i;
char* mem = (char*)m;
long* lmem;
if(s < sizeof(long))
{
for(i = 0; i < s; i++)
mem[i] = ch;
return m;
}
long l = ch & 0xFF;
l = l | (l << 8);
l = l | (l << 16);
#if ULONG_MAX == 18446744073709551615UL
l = l | (l << 32);
#endif
while((long)mem & (sizeof(long)-1))
*mem++ = ch, s--;
lmem = (long*)mem;
for(i = 0; i+7 < s/sizeof(long); i += 8)
{
lmem[i+0] = l;
lmem[i+1] = l;
lmem[i+2] = l;
lmem[i+3] = l;
lmem[i+4] = l;
lmem[i+5] = l;
lmem[i+6] = l;
lmem[i+7] = l;
}
lmem += i;
s -= i*sizeof(long);
for(i = 0; i < s/sizeof(long); i++)
lmem[i] = l;
lmem += i;
s -= i*sizeof(long);
mem = (char*)lmem;
for(i = 0; i < s; i++)
mem[i] = ch;
return m;
}

2
pk/pk.mk.in

@ -21,8 +21,6 @@ pk_c_srcs = \
frontend.c \
fp.c \
int.c \
memset.c \
strlen.c \
elf.c \
pk_asm_srcs = \

33
pk/strlen.c

@ -1,33 +0,0 @@
// See LICENSE for license details.
#include <string.h>
#include <limits.h>
#if ULONG_MAX != 18446744073709551615UL && ULONG_MAX != 4294967295UL
# error need sizeof(long) == 4 or sizeof(long) == 8
#endif
// from http://www-graphics.stanford.edu/~seander/bithacks.html
static inline long hasZeroByte(long l)
{
#if ULONG_MAX == 4294967295UL
return (l - 0x01010101UL) & ~l & 0x80808080UL;
#else
return (l - 0x0101010101010101UL) & ~l & 0x8080808080808080UL;
#endif
}
size_t strlen(const char* s)
{
size_t i = 0;
// use optimized version if string starts on a long boundary
if(((long)s & (sizeof(long)-1)) == 0)
while(!hasZeroByte(*(long*)(s+i)))
i += sizeof(long);
while(s[i])
i++;
return i;
}
Loading…
Cancel
Save