Add C block fill operations for various types
authorRoman Leshchinskiy <rl@cse.unsw.edu.au>
Thu, 26 Jan 2012 21:34:56 +0000 (21:34 +0000)
committerRoman Leshchinskiy <rl@cse.unsw.edu.au>
Thu, 26 Jan 2012 21:34:56 +0000 (21:34 +0000)
cbits/primitive-memops.c
cbits/primitive-memops.h

index e4f6d9f..a6d1959 100644 (file)
@@ -1,4 +1,5 @@
 #include <string.h>
+#include "primitive-memops.h"
 
 void memcpy_off( void *dst, int doff, void *src, int soff, size_t len )
 {
@@ -15,3 +16,42 @@ void memset_off( void *dst, int doff, int c, size_t len )
   memset( (char *)dst + doff, c, len );
 }
 
+#define MEMSET(TYPE, ATYPE)                                                  \
+void hsprimitive_memset_ ## TYPE (Hs ## TYPE *p, int off, int n, ATYPE x)    \
+{                                                                            \
+  p += off;                                                                  \
+  if (x == 0)                                                                \
+    memset(p, 0, n * sizeof(Hs ## TYPE));                                    \
+  else if (sizeof(Hs ## TYPE) == sizeof(int)*2) {                            \
+    int *q = (int *)p;                                                       \
+    const int *r = (const int *)&x;                                          \
+    while (n>0) {                                                            \
+      q[0] = r[0];                                                           \
+      q[1] = r[1];                                                           \
+      q += 2;                                                                \
+      --n;                                                                   \
+    }                                                                        \
+  }                                                                          \
+  else {                                                                     \
+    while (n>0) {                                                            \
+      *p++ = x;                                                              \
+      --n;                                                                   \
+    }                                                                        \
+  }                                                                          \
+}
+
+void hsprimitive_memset_Word8 (HsWord8 *p, int off, int n, HsWord x)
+{
+  memset( (char *)(p+off), x, n );
+}
+
+/* MEMSET(HsWord8, HsWord) */
+MEMSET(Word16, HsWord)
+MEMSET(Word32, HsWord)
+MEMSET(Word64, HsWord64)
+MEMSET(Word, HsWord)
+MEMSET(Ptr, HsPtr)
+MEMSET(Float, HsFloat)
+MEMSET(Double, HsDouble)
+MEMSET(Char, HsChar)
+
index 92dab27..2aad75a 100644 (file)
@@ -2,10 +2,21 @@
 #define haskell_primitive_memops_h
 
 #include <stdlib.h>
+#include <HsFFI.h>
 
 void memcpy_off( void *dst, int doff, void *src, int soff, size_t len );
 void memmove_off( void *dst, int doff, void *src, int soff, size_t len );
 void memset_off( void *dst, int doff, int c, size_t len );
 
+void hsprimitive_memset_Word8 (HsWord8 *, int, int, HsWord);
+void hsprimitive_memset_Word16 (HsWord16 *, int, int, HsWord);
+void hsprimitive_memset_Word32 (HsWord32 *, int, int, HsWord);
+void hsprimitive_memset_Word64 (HsWord64 *, int, int, HsWord64);
+void hsprimitive_memset_Word (HsWord *, int, int, HsWord);
+void hsprimitive_memset_Ptr (HsPtr *, int, int, HsPtr);
+void hsprimitive_memset_Float (HsFloat *, int, int, HsFloat);
+void hsprimitive_memset_Double (HsDouble *, int, int, HsDouble);
+void hsprimitive_memset_Char (HsChar *, int, int, HsChar);
+
 #endif