Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
View Poll Results: Do you find this thread useful?
Yes
1
100.00%
No
0
0%
07-15-2010, 01:15 PM
#1
LQ Newbie
Registered: Jul 2010
Location: Sofia
Posts: 8
Rep:
Karp-Rabin fast implementation
Hello everyone,
I want to share my tuned Karp-Rabin C function with Linux community:
http://encode.ru/threads/612-Fastest...ll=1#post21905
I would be glad to see an improved(more versatile) version,
regards.
07-21-2010, 01:11 PM
#2
LQ Newbie
Registered: Jul 2010
Location: Sofia
Posts: 8
Original Poster
Rep:
The function itself
Hi all,
Code:
#define ulPrime ((unsigned long) 0x00FF00F1)
#define ulBase ((unsigned long) 127)
// 9,223,372,036,854,775,807
// 257^7 = 74,051,159,531,521,793
// 257^8 = 19,031,147,999,601,100,801
// 127^9 = 8,594,754,748,609,397,887
// 57^10 = 362,033,331,456,891,249
// 13^16 = 665,416,609,183,179,841
// 5^12 = 244,140,625
// 13^8 = 815,730,721
long KarpRabinKaze (char * pbTarget,
char * pbPattern,
unsigned long cbTarget,
unsigned long cbPattern)
{
unsigned int i;
char * pbTargetMax = pbTarget + cbTarget;
char * pbPatternMax = pbPattern + cbPattern;
unsigned long ulBaseToPowerMod = 1;
register unsigned long ulHashPattern = 0;
unsigned long ulHashTarget = 0;
long hits = 0;
//unsigned long count;
//char * buf1;
//char * buf2;
if (cbPattern > cbTarget)
return(-1);
// Compute the power of the left most character in base ulBase
//for (i = 1; i < cbPattern; i++) ulBaseToPowerMod = (ulBase * ulBaseToPowerMod);
// Calculate the hash function for the src (and the first dst)
while (pbPattern < pbPatternMax)
{
// Below lines give 366KB/clock for 'underdog':
//ulHashPattern = (ulHashPattern*ulBase + *pbPattern);
//ulHashTarget = (ulHashTarget*ulBase + *pbTarget);
pbPattern++;
pbTarget++;
}
// Below lines give 436KB/clock for 'underdog' + requirement pattern to be 4 chars min.:
//ulHashPattern = ( (*(long *)(pbPattern-cbPattern)) & 0xffffff00 ) + *(pbPattern-1);
//ulHashTarget = ( (*(long *)(pbTarget-cbPattern)) & 0xffffff00 ) + *(pbTarget-1);
// Below lines give 482KB/clock for 'underdog' + requirement pattern to be 2 chars min.:
//ulHashPattern = ( (*(unsigned short *)(pbPattern-cbPattern)) | *(pbPattern-1) );
//ulHashTarget = ( (*(unsigned short *)(pbTarget-cbPattern)) | *(pbTarget-1) );
// Below lines give 482KB/clock for 'underdog' + requirement pattern to be 2 chars min.:
//ulHashPattern = ( (*(unsigned short *)(pbPattern-cbPattern)) & 0xff00 ) + *(pbPattern-1);
//ulHashTarget = ( (*(unsigned short *)(pbTarget-cbPattern)) & 0xff00 ) + *(pbTarget-1);
// Below lines give 605KB/clock for 'underdog' + requirement pattern to be 2 chars min.:
//ulHashPattern = ( (*(unsigned short *)(pbPattern-cbPattern))<<8 ) + *(pbPattern-1);
//ulHashTarget = ( (*(unsigned short *)(pbTarget-cbPattern))<<8 ) + *(pbTarget-1);
// Below lines give 668KB/clock for 'underdog':
ulHashPattern = ( (*(char *)(pbPattern-cbPattern))<<8 ) + *(pbPattern-1);
ulHashTarget = ( (*(char *)(pbTarget-cbPattern))<<8 ) + *(pbTarget-1);
// Dynamically produce hash values for the string as we go
for ( ;; )
{
if ( (ulHashPattern == ulHashTarget) && !memcmp(pbPattern-cbPattern, pbTarget-cbPattern, (unsigned int)cbPattern) )
// if ( ulHashPattern == ulHashTarget ) {
//
// count = cbPattern;
// buf1 = pbPattern-cbPattern;
// buf2 = pbTarget-cbPattern;
// while ( --count && *(char *)buf1 == *(char *)buf2 ) {
// buf1 = (char *)buf1 + 1;
// buf2 = (char *)buf2 + 1;
// }
//
// if ( *((unsigned char *)buf1) - *((unsigned char *)buf2) == 0) hits++;
// }
return((long)(pbTarget-cbPattern));
//hits++;
if (pbTarget == pbTargetMax)
return(-1);
// Below line gives 482KB/clock for 'underdog' + requirement pattern to be 2 chars min.:
//ulHashTarget = ( (*(unsigned short *)(pbTarget+1-cbPattern)) | *pbTarget );
// Below line gives 436KB/clock for 'underdog' + requirement pattern to be 4 chars min.:
//ulHashTarget = ( (*(long *)(pbTarget+1-cbPattern)) & 0xffffff00 ) + *pbTarget;
//; Line 696
// movsx esi, BYTE PTR [ebx]
// mov ecx, DWORD PTR [edx+1]
// and ecx, -256 ; ffffff00H
// add ecx, esi
// Below line gives 482KB/clock for 'underdog' + requirement pattern to be 2 chars min.:
//ulHashTarget = ( (*(unsigned short *)(pbTarget+1-cbPattern)) & 0xff00 ) + *pbTarget;
//; Line 691
// movsx esi, BYTE PTR [ebx]
// xor ecx, ecx
// mov cx, WORD PTR [edx+1]
// and ecx, 65280 ; 0000ff00H
// add ecx, esi
// Below line gives 605KB/clock for 'underdog' + requirement pattern to be 2 chars min.:
//ulHashTarget = ( (*(unsigned short *)(pbTarget+1-cbPattern))<<8 ) + *pbTarget;
// Below line gives 668KB/clock for 'underdog':
ulHashTarget = ( (*(char *)(pbTarget+1-cbPattern))<<8 ) + *pbTarget;
//; Line 718
// movsx ecx, BYTE PTR [eax+1]
// movsx edx, BYTE PTR [ebp]
// shl ecx, 8
// add ecx, edx
// Below line gives 366KB/clock for 'underdog':
//ulHashTarget = (ulHashTarget - *(pbTarget-cbPattern)*ulBaseToPowerMod)*ulBase + *pbTarget;
pbTarget++;
}
}
I wait for any improvements...
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 04:46 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News