1: /* 2: * Copyright (c) 1987 Regents of the University of California. 3: * All rights reserved. The Berkeley software License Agreement 4: * specifies the terms and conditions for redistribution. 5: */ 6: 7: #ifdef LIBC_SCCS 8: <@(#)bcmp.s 1.2 (Berkeley) 1/8/87\0> 9: .even 10: #endif LIBC_SCCS 11: 12: #include "DEFS.h" 13: 14: /* 15: * bcmp(b1, b2, length) 16: * 17: * Compare length bytes starting at b1 and b2 - return 0 if equal, non zero 18: * otherwise. Bcmp uses a cost analysis heuristic to determine if it's worth 19: * while to try for a word comparison. If length > N, then try for a word 20: * comparison. Bcmp will fail if N < 3. 8 is a good value for N: 21: * 22: * Algorithm Cost (in instructions) 23: * byte loop 3 * length 24: * word loop overhead [worst case] 12 25: * word loop 1.5 * length 26: * 27: * N * 3 > 12 + N * 1.5 28: * N > 8 29: */ 30: #define N 8. 31: 32: ENTRY(bcmp) 33: mov 6(sp),r0 / if ((r0 = length) == 0) 34: beq 3f / return(length:r0) 35: mov r2,-(sp) / reserve a register for our use 36: mov 6(sp),r2 / r2 = b2 37: mov 4(sp),r1 / r1 = b1 38: cmp r0,$N / if (length > N) 39: bhi 4f / try words 40: 41: 1: cmpb (r1)+,(r2)+ / do if (*b1++ != *b2++) 42: bne 2f / return(length:r0) {length != 0} 43: sob r0,1b / while (--length) 44: 2: 45: mov (sp)+,r2 / return(length:r0) 46: 3: 47: rts pc 48: 49: /* 50: * The length of the comparison justifies trying a word by word comparison. 51: * If b1 and b2 are of the same parity, we can do a word comparison by 52: * handling any leading and trailing odd bytes separately. 53: */ 54: 4: 55: bit $1,r1 / if (b1&1 != b2&1) 56: beq 5f / do bytes 57: bit $1,r2 58: beq 1b / (b1 odd, b2 even - do bytes) 59: cmpb (r1)+,(r2)+ / if (leading odd bytes equal) 60: bne 2b / return(non_zero::r0) 61: dec r0 / else onto word loop 62: br 6f 63: 5: 64: bit $1,r2 65: bne 1b / (b1 even, b2 odd - do bytes) 66: 6: 67: mov r0,-(sp) / save trailing byte indicator 68: asr r0 / length >>= 1 (unsigned) 69: bic $0100000,r0 / (length is now in remaining words to compare) 70: 7: 71: cmp (r1)+,(r2)+ / do if (*(short *)b1++ != *(short *)b2++) 72: bne 9f / return(length:r0) {length != 0} 73: sob r0,7b / while (--length) 74: 8: 75: mov (sp)+,r0 / if (no odd trailing byte) 76: bic $!1,r0 77: beq 2b / return(0) 78: cmpb (r1)+,(r2)+ / if (trailing odd bytes different) 79: bne 2b / return(non-zero) 80: clr r0 / else return(0) 81: br 2b 82: 9: 83: mov (sp)+,r0 / bad compare in word loop, 84: br 2b / return(non-zero)