Skip to content

Instantly share code, notes, and snippets.

@saga
Created May 25, 2010 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saga/412650 to your computer and use it in GitHub Desktop.
Save saga/412650 to your computer and use it in GitHub Desktop.
#include "stdafx.h"
#include <stdio.h>
/* fast strcpy -- Copyright (C) 2003 Thomas M. Ogrisegg <tom@hi-tek.fnord.at> */
//#include <string.h>
//#include "dietfeatures.h"
//#include "dietstring.h"
// ----following are dietstring.h content.
//#include <endian.h>
//# define MKW(x) (x|x<<8|x<<16|x<<24)
int MKW(int x)
{
x = x | x<<8 | x << 16 | x << 24;
return x;
}
//# define STRALIGN(x) (((unsigned long)x&3)?4-((unsigned long)x&3):0)
unsigned long STRALIGN(unsigned long xPtr)
{
unsigned long xRet = (unsigned long)xPtr & 3;
if (xRet)
xRet = 4 - ((unsigned long) xPtr & 3);
else
xRet = 0;
return xRet;
}
/* GFC(x) - returns first character */
/* INCSTR(x) - moves to next character */
# define GFC(x) ((x)&0xff)
# define INCSTR(x) do { x >>= 8; } while (0)
//#define UNALIGNED(x,y) (((unsigned long)x & (sizeof (unsigned long)-1)) ^ ((unsigned long)y & (sizeof (unsigned long)-1)))
unsigned long MyUnaligned(unsigned long xPtr, unsigned long yPtr)
{
unsigned long valN1 = sizeof (unsigned long)-1;
unsigned long xVal = (unsigned long) xPtr & valN1;
unsigned long yVal = (unsigned long) yPtr & valN1;
unsigned long retVal = xVal ^ yVal;
return retVal;
}
// ----above are dietstring.h content.
char *
strcpy2 (char *s1, const char *s2)
{
char *res = s1;
int tmp;
unsigned long l;
if (MyUnaligned((unsigned long)s1, (unsigned long)s2)){
while ((*s1++ = *s2++));
return (res);
}
if ((tmp = STRALIGN((unsigned long)s1))){
while (tmp-- && (*s1++ = *s2++));
if (tmp != -1) return (res);
}
while (1) {
unsigned long key1 = MKW(0x1ul);
unsigned long key2 = MKW(0x80ul);
l = *(const unsigned long *) s2;
if (((l - key1) & ~l) & key2) {
while ((*s1++ = GFC(l))) INCSTR(l);
return (res);
}
*(unsigned long *) s1 = l;
s2 += sizeof(unsigned long);
s1 += sizeof(unsigned long);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char* p = (char*)malloc(50* sizeof p);
char* str = "aaaabbbbbcccccc";
strcpy2(p, str);
free(p);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment