aboutsummaryrefslogtreecommitdiff
path: root/pk/memset.c
diff options
context:
space:
mode:
Diffstat (limited to 'pk/memset.c')
-rw-r--r--pk/memset.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/pk/memset.c b/pk/memset.c
new file mode 100644
index 0000000..d634138
--- /dev/null
+++ b/pk/memset.c
@@ -0,0 +1,56 @@
+#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;
+}