[ create a new paste ] login | about

Link: http://codepad.org/jWk8nqgg    [ raw code | output | fork ]

C, pasted on Apr 6:
#include <stdio.h>

int x_strncmp(char* a, char* b, int len){
	int i=len, dif=0; while(i--!=0&&dif==0){dif=(*a++)-(*b++);} return(dif);
}

int x_strncmp4(char* a, char* b, int len){
	unsigned long* ap=(unsigned long*)a;
	unsigned long* bp=(unsigned long*)b;
	int i=len/4, dif=0; while(i--!=0&&dif==0){ dif=(*ap++)-(*bp++);} return(dif);
}

// len >= 1
int xx_strncmp4(char* a, char* b, int len){
	unsigned long* ap=(unsigned long*)a;
	unsigned long* bp=(unsigned long*)b;
	int i=(len-1)/4, ii=len%4;
	
	int dif=0;
	while(i--!=0&&dif==0){ dif=(*ap++)-(*bp++); }
	
	a+=i*4; b+=i*4;
	while(ii--!=0&&dif==0){ dif=(*a++)-(*b++);} return(dif);
}

// testcode
void main(void){
	printf("\nALL 0 ? -> ");
	printf("[%d] ", x_strncmp( "12345678AAAABBBBthen","12345678AAAABBBBthen",20) );
	printf("[%d] ", x_strncmp4("12345678AAAABBBBthen","12345678AAAABBBBthen",20) );
	printf("[%d] ", x_strncmp(  "12345678AAAABBBBthe","12345678AAAABBBBthe",19) );
	printf("[%d] ", xx_strncmp4("12345678AAAABBBBthe","12345678AAAABBBBthe",19) );
	printf("\n");

	printf("ALL !0 ? -> ");
	printf("[%d] ", x_strncmp( "12345678AAAABBBBthen","12345678AA ABBBBthen",20) );
	printf("[%d] ", x_strncmp4("12345678AAABBBBthen","12345678AA ABBBBthen",20) );
	printf("[%d] ", x_strncmp(  "12345678AAAABBBBthe","12345678AA ABBBBthe",19) );
	printf("[%d] ", xx_strncmp4("12345678AAAABBBBthe","12345678AA ABBBBthe",19) );
	printf("[%d] ", x_strncmp(  "12345678AAAABBBBthe","12345678AAAABBBBth ",19) );
	printf("[%d] ", xx_strncmp4("12345678AAAABBBBthe","12345678AAAABBBBth ",19) );
	printf("\n");

	printf("Additional test !0 ? -> ");
	printf("[%d] ", x_strncmp( "aa", "bb", 2) );
	printf("[%d] ", x_strncmp4("aa", "bb", 2) );
	printf("\n");
	printf("\n");
}


Output:
1
2
3
4
5

ALL 0 ? -> [0] [0] [0] [0] 
ALL !0 ? -> [33] [18939904] [33] [2162688] [69] [-16] 
Additional test !0 ? -> [-1] [0] 



Create a new paste based on this one


Comments: