qse/ase/cmn/mem.c

402 lines
7.4 KiB
C
Raw Normal View History

/*
2008-04-26 22:58:10 +00:00
* $Id: mem.c 177 2008-04-26 04:58:10Z baconevi $
*
* {License}
*/
#include <ase/cmn/mem.h>
2008-04-25 07:08:14 +00:00
#if defined(__SPU__)
#include <spu_intrinsics.h>
2008-04-25 22:50:34 +00:00
#define SPU_VUC_SIZE ASE_SIZEOF(vector unsigned char)
2008-04-25 07:08:14 +00:00
#endif
/*#define IS_UNALIGNED(ptr) (((ase_size_t)ptr)%ASE_SIZEOF(ase_size_t))*/
#define IS_UNALIGNED(ptr) (((ase_size_t)ptr)&(ASE_SIZEOF(ase_size_t)-1))
2007-12-25 21:25:45 +00:00
#define IS_ALIGNED(ptr) (!IS_UNALIGNED(ptr))
2007-12-25 21:37:48 +00:00
#define IS_EITHER_UNALIGNED(ptr1,ptr2) \
2008-04-25 07:08:14 +00:00
(((ase_size_t)ptr1|(ase_size_t)ptr2)&(ASE_SIZEOF(ase_size_t)-1))
2007-12-25 21:37:48 +00:00
#define IS_BOTH_ALIGNED(ptr1,ptr2) (!IS_EITHER_UNALIGNED(ptr1,ptr2))
void* ase_memcpy (void* dst, const void* src, ase_size_t n)
{
2008-04-25 06:19:44 +00:00
#if defined(ASE_BUILD_FOR_SIZE)
2008-04-25 22:50:34 +00:00
2008-04-25 06:19:44 +00:00
ase_byte_t* d = (ase_byte_t*)dst;
ase_byte_t* s = (ase_byte_t*)src;
while (n-- > 0) *d++ = *s++;
return dst;
2008-04-25 22:50:34 +00:00
#elif defined(__SPU__)
ase_byte_t* d;
ase_byte_t* s;
2008-04-25 22:59:00 +00:00
if (n >= SPU_VUC_SIZE &&
2008-04-25 22:50:34 +00:00
(((ase_size_t)dst) & (SPU_VUC_SIZE-1)) == 0 &&
(((ase_size_t)src) & (SPU_VUC_SIZE-1)) == 0)
{
vector unsigned char* du = (vector unsigned char*)dst;
vector unsigned char* su = (vector unsigned char*)src;
do
{
*du++ = *su++;
n -= SPU_VUC_SIZE;
}
while (n >= SPU_VUC_SIZE);
d = (ase_byte_t*)du;
s = (ase_byte_t*)su;
}
else
{
d = (ase_byte_t*)dst;
s = (ase_byte_t*)src;
}
while (n-- > 0) *d++ = *s++;
return dst;
2008-04-25 06:19:44 +00:00
#else
2008-04-25 22:50:34 +00:00
2008-04-25 06:19:44 +00:00
ase_byte_t* d;
ase_byte_t* s;
2007-12-25 21:25:45 +00:00
2008-04-25 07:21:17 +00:00
if (n >= ASE_SIZEOF(ase_size_t) && IS_BOTH_ALIGNED(dst,src))
2007-12-25 21:25:45 +00:00
{
2008-04-25 07:21:17 +00:00
ase_size_t* du = (ase_size_t*)dst;
ase_size_t* su = (ase_size_t*)src;
2008-04-25 07:34:17 +00:00
do
2007-12-25 21:25:45 +00:00
{
2008-04-25 06:19:44 +00:00
*du++ = *su++;
2008-04-25 07:21:17 +00:00
n -= ASE_SIZEOF(ase_size_t);
2007-12-25 21:25:45 +00:00
}
2008-04-25 07:34:17 +00:00
while (n >= ASE_SIZEOF(ase_size_t));
2007-12-25 21:25:45 +00:00
2008-04-25 06:19:44 +00:00
d = (ase_byte_t*)du;
s = (ase_byte_t*)su;
2007-12-25 21:25:45 +00:00
}
2008-04-25 06:19:44 +00:00
else
{
2008-04-25 06:19:44 +00:00
d = (ase_byte_t*)dst;
s = (ase_byte_t*)src;
}
2008-04-25 06:19:44 +00:00
while (n-- > 0) *d++ = *s++;
return dst;
2008-04-25 22:50:34 +00:00
2008-04-25 06:19:44 +00:00
#endif
}
2008-04-26 22:58:10 +00:00
void* ase_memmove (void* dst, const void* src, ase_size_t n)
{
const ase_byte_t* sre = (const ase_byte_t*)src + n;
if (dst <= src || dst >= (const void*)sre)
{
ase_byte_t* d = (ase_byte_t*)dst;
const ase_byte_t* s = (const ase_byte_t*)src;
while (n-- > 0) *d++ = *s++;
}
else
{
ase_byte_t* dse = (ase_byte_t*)dst + n;
while (n-- > 0) *--dse = *--sre;
}
return dst;
}
void* ase_memset (void* dst, int val, ase_size_t n)
{
2008-04-25 06:19:44 +00:00
#if defined(ASE_BUILD_FOR_SIZE)
2008-04-25 07:08:14 +00:00
2008-04-25 06:19:44 +00:00
ase_byte_t* d = (ase_byte_t*)dst;
while (n-- > 0) *d++ = (ase_byte_t)val;
return dst;
2008-04-25 07:08:14 +00:00
2008-04-25 06:19:44 +00:00
#elif defined(__SPU__)
2008-04-25 07:08:14 +00:00
2008-04-25 08:25:47 +00:00
ase_byte_t* d;
2008-04-25 07:08:14 +00:00
ase_size_t rem;
2008-04-25 08:25:47 +00:00
if (n <= 0) return dst;
d = (ase_byte_t*)dst;
2008-04-25 07:08:14 +00:00
/* spu SIMD instructions require 16-byte alignment */
2008-04-25 22:50:34 +00:00
rem = ((ase_size_t)dst) & (SPU_VUC_SIZE-1);
2008-04-25 07:08:14 +00:00
if (rem > 0)
{
2008-04-25 22:27:37 +00:00
/* handle leading unaligned part */
2008-04-25 07:21:17 +00:00
do { *d++ = (ase_byte_t)val; }
2008-04-25 22:50:34 +00:00
while (n-- > 0 && ++rem < SPU_VUC_SIZE);
2008-04-25 07:08:14 +00:00
}
/* do the vector copy */
2008-04-25 22:50:34 +00:00
if (n >= SPU_VUC_SIZE)
2008-04-25 07:08:14 +00:00
{
2008-04-25 07:40:41 +00:00
/* a vector of 16 unsigned char cells */
vector unsigned char v16;
/* a pointer to such a vector */
vector unsigned char* vd = (vector unsigned char*)d;
2008-04-25 07:08:14 +00:00
2008-04-25 07:40:41 +00:00
/* fills all 16 unsigned char cells with the same value
* no need to use shift and bitwise-or owing to splats */
v16 = spu_splats((ase_byte_t)val);
do
{
*vd++ = v16;
2008-04-25 22:50:34 +00:00
n -= SPU_VUC_SIZE;
2008-04-25 07:40:41 +00:00
}
2008-04-25 22:50:34 +00:00
while (n >= SPU_VUC_SIZE);
2008-04-25 07:40:41 +00:00
d = (ase_byte_t*)vd;
2008-04-25 06:19:44 +00:00
}
2008-04-25 22:27:37 +00:00
/* handle the trailing part */
2008-04-25 07:40:41 +00:00
while (n-- > 0) *d++ = (ase_byte_t)val;
2008-04-25 07:08:14 +00:00
return dst;
2008-04-25 06:19:44 +00:00
#else
2008-04-25 07:08:14 +00:00
2008-04-25 08:25:47 +00:00
ase_byte_t* d;
2008-04-25 07:21:17 +00:00
ase_size_t rem;
2008-04-25 08:25:47 +00:00
if (n <= 0) return dst;
d = (ase_byte_t*)dst;
2008-04-25 07:21:17 +00:00
rem = IS_UNALIGNED(dst);
if (rem > 0)
{
2008-04-25 07:34:17 +00:00
do { *d++ = (ase_byte_t)val; }
while (n-- > 0 && ++rem < ASE_SIZEOF(ase_size_t));
2008-04-25 07:21:17 +00:00
}
if (n >= ASE_SIZEOF(ase_size_t))
{
2008-04-25 07:34:17 +00:00
ase_size_t* u = (ase_size_t*)d;
2008-04-25 07:21:17 +00:00
ase_size_t uv = 0;
2008-04-25 06:19:44 +00:00
int i;
if (val != 0)
{
2008-04-25 07:21:17 +00:00
for (i = 0; i < ASE_SIZEOF(ase_size_t); i++)
2008-04-25 06:19:44 +00:00
uv = (uv << 8) | (ase_byte_t)val;
}
2008-04-25 07:34:17 +00:00
ASE_ASSERT (IS_ALIGNED(u));
do
2008-04-25 06:19:44 +00:00
{
*u++ = uv;
2008-04-25 07:21:17 +00:00
n -= ASE_SIZEOF(ase_size_t);
2008-04-25 06:19:44 +00:00
}
2008-04-25 07:34:17 +00:00
while (n >= ASE_SIZEOF(ase_size_t));
2008-04-25 06:19:44 +00:00
d = (ase_byte_t*)u;
}
2008-04-25 06:19:44 +00:00
while (n-- > 0) *d++ = (ase_byte_t)val;
return dst;
2008-04-25 07:08:14 +00:00
2008-04-25 06:19:44 +00:00
#endif
}
int ase_memcmp (const void* s1, const void* s2, ase_size_t n)
{
2008-04-25 06:19:44 +00:00
#if defined(ASE_BUILD_FOR_SIZE)
2007-12-29 06:39:01 +00:00
2008-04-25 06:19:44 +00:00
const ase_byte_t* b1 = (const ase_byte_t*)s1;
const ase_byte_t* b2 = (const ase_byte_t*)s2;
while (n-- > 0)
{
if (*b1 != *b2) return *b1 - *b2;
b1++; b2++;
}
return 0;
2008-04-26 01:01:05 +00:00
#elif defined(__SPU__)
const ase_byte_t* b1;
const ase_byte_t* b2;
if (n >= SPU_VUC_SIZE &&
2008-04-26 02:57:55 +00:00
(((ase_size_t)s1) & (SPU_VUC_SIZE-1)) == 0 &&
(((ase_size_t)s2) & (SPU_VUC_SIZE-1)) == 0)
2008-04-26 01:01:05 +00:00
{
2008-04-26 02:57:55 +00:00
vector unsigned char* v1 = (vector unsigned char*)s1;
vector unsigned char* v2 = (vector unsigned char*)s2;
vector unsigned int tmp;
2008-04-26 01:01:05 +00:00
do
{
2008-04-26 02:57:55 +00:00
unsigned int cnt;
unsigned int pat;
2008-04-26 03:18:57 +00:00
/* compare 16 chars at one time */
2008-04-26 02:57:55 +00:00
tmp = spu_gather(spu_cmpeq(*v1,*v2));
2008-04-26 03:18:57 +00:00
/* extract the bit pattern */
pat = spu_extract(tmp, 0);
/* invert the bit patterns */
2008-04-26 02:57:55 +00:00
pat = 0xFFFF & ~pat;
2008-04-26 03:18:57 +00:00
/* put it back to the vector */
2008-04-26 02:57:55 +00:00
tmp = spu_insert (pat, tmp, 0);
2008-04-26 03:18:57 +00:00
/* count the leading zeros */
2008-04-26 02:57:55 +00:00
cnt = spu_extract(spu_cntlz(tmp),0);
2008-04-26 03:18:57 +00:00
/* 32 leading zeros mean that
* all characters are the same */
2008-04-26 02:57:55 +00:00
if (cnt != 32)
2008-04-26 02:06:58 +00:00
{
2008-04-26 03:18:57 +00:00
/* otherwise, calculate the
* unmatching pointer address */
2008-04-26 02:06:58 +00:00
b1 = (const ase_byte_t*)v1 + (cnt - 16);
b2 = (const ase_byte_t*)v2 + (cnt - 16);
break;
}
2008-04-26 01:01:05 +00:00
v1++; v2++;
n -= SPU_VUC_SIZE;
2008-04-26 02:06:58 +00:00
if (n < SPU_VUC_SIZE)
{
b1 = (const ase_byte_t*)v1;
b2 = (const ase_byte_t*)v2;
break;
}
2008-04-26 01:01:05 +00:00
}
2008-04-26 02:06:58 +00:00
while (1);
2008-04-26 01:01:05 +00:00
}
else
{
b1 = (const ase_byte_t*)s1;
b2 = (const ase_byte_t*)s2;
}
while (n-- > 0)
{
if (*b1 != *b2) return *b1 - *b2;
b1++; b2++;
}
return 0;
2008-04-25 06:19:44 +00:00
#else
const ase_byte_t* b1;
const ase_byte_t* b2;
2008-04-25 07:21:17 +00:00
if (n >= ASE_SIZEOF(ase_size_t) && IS_BOTH_ALIGNED(s1,s2))
2008-04-25 06:19:44 +00:00
{
2008-04-25 07:21:17 +00:00
const ase_size_t* u1 = (const ase_size_t*)s1;
const ase_size_t* u2 = (const ase_size_t*)s2;
2008-04-25 06:19:44 +00:00
2008-04-25 07:34:17 +00:00
do
2008-04-25 06:19:44 +00:00
{
2008-04-25 06:33:26 +00:00
if (*u1 != *u2) break;
2008-04-25 06:19:44 +00:00
u1++; u2++;
2008-04-25 07:21:17 +00:00
n -= ASE_SIZEOF(ase_size_t);
2008-04-25 06:19:44 +00:00
}
2008-04-25 07:34:17 +00:00
while (n >= ASE_SIZEOF(ase_size_t));
2008-04-25 06:33:26 +00:00
b1 = (const ase_byte_t*)u1;
b2 = (const ase_byte_t*)u2;
2008-04-25 06:19:44 +00:00
}
else
{
b1 = (const ase_byte_t*)s1;
b2 = (const ase_byte_t*)s2;
}
2007-12-29 06:39:01 +00:00
2008-04-25 06:19:44 +00:00
while (n-- > 0)
2007-12-29 06:39:01 +00:00
{
2008-04-25 06:19:44 +00:00
if (*b1 != *b2) return *b1 - *b2;
b1++; b2++;
2007-12-29 06:39:01 +00:00
}
return 0;
2008-04-25 06:19:44 +00:00
#endif
}
2008-04-26 22:58:10 +00:00
void* ase_memchr (const void* s, int val, ase_size_t n)
{
const ase_byte_t* x = (const ase_byte_t*)s;
while (n-- > 0)
{
if (*x == (ase_byte_t)val) return (void*)x;
x++;
}
return ASE_NULL;
}
void* ase_memrchr (const void* s, int val, ase_size_t n)
{
const ase_byte_t* x = (ase_byte_t*)s + n - 1;
while (n-- > 0)
{
if (*x == (ase_byte_t)val) return (void*)x;
x--;
}
return ASE_NULL;
}
void* ase_memmem (const void* hs, ase_size_t hl, const void* nd, ase_size_t nl)
{
if (nl <= hl)
{
ase_size_t i;
const ase_byte_t* h = (const ase_byte_t*)hs;
for (i = hl - nl + 1; i > 0; i--)
{
if (ase_memcmp(h, nd, nl) == 0) return (void*)h;
h++;
}
}
return ASE_NULL;
}
void* ase_memrmem (const void* hs, ase_size_t hl, const void* nd, ase_size_t nl)
{
if (nl <= hl)
{
ase_size_t i;
const ase_byte_t* h;
/* things are slightly more complacated
* when searching backward */
if (nl == 0)
{
/* when the needle is empty, it returns
* the pointer to the last byte of the haystack.
* this is because ase_memmem returns the pointer
* to the first byte of the haystack when the
* needle is empty. but I'm not so sure if this
* is really desirable behavior */
h = (const ase_byte_t*)hs + hl - 1;
return (void*)h;
}
h = (const ase_byte_t*)hs + hl - nl;
for (i = hl - nl + 1; i > 0; i--)
{
if (ase_memcmp(h, nd, nl) == 0) return (void*)h;
h--;
}
}
return ASE_NULL;
}