Changelog
[darcs-mirrors/primitive.git] / cbits / primitive-memops.c
1 #include <string.h>
2 #include "primitive-memops.h"
3
4 void hsprimitive_memcpy( void *dst, int doff, void *src, int soff, size_t len )
5 {
6 memcpy( (char *)dst + doff, (char *)src + soff, len );
7 }
8
9 void hsprimitive_memmove( void *dst, int doff, void *src, int soff, size_t len )
10 {
11 memmove( (char *)dst + doff, (char *)src + soff, len );
12 }
13
14 #define MEMSET(TYPE, ATYPE) \
15 void hsprimitive_memset_ ## TYPE (Hs ## TYPE *p, int off, int n, ATYPE x) \
16 { \
17 p += off; \
18 if (x == 0) \
19 memset(p, 0, n * sizeof(Hs ## TYPE)); \
20 else if (sizeof(Hs ## TYPE) == sizeof(int)*2) { \
21 int *q = (int *)p; \
22 const int *r = (const int *)&x; \
23 while (n>0) { \
24 q[0] = r[0]; \
25 q[1] = r[1]; \
26 q += 2; \
27 --n; \
28 } \
29 } \
30 else { \
31 while (n>0) { \
32 *p++ = x; \
33 --n; \
34 } \
35 } \
36 }
37
38 void hsprimitive_memset_Word8 (HsWord8 *p, int off, int n, HsWord x)
39 {
40 memset( (char *)(p+off), x, n );
41 }
42
43 /* MEMSET(HsWord8, HsWord) */
44 MEMSET(Word16, HsWord)
45 MEMSET(Word32, HsWord)
46 MEMSET(Word64, HsWord64)
47 MEMSET(Word, HsWord)
48 MEMSET(Ptr, HsPtr)
49 MEMSET(Float, HsFloat)
50 MEMSET(Double, HsDouble)
51 MEMSET(Char, HsChar)
52