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: <@(#)bcopy.s 1.2 (Berkeley) 8/21/87\0> 9: .even 10: #endif LIBC_SCCS 11: 12: #include "DEFS.h" 13: 14: /* 15: * bcopy(src, dst, length) 16: * caddr_t src, dst; 17: * u_int length; 18: * 19: * Copy length bytes from src to dst - handles overlapping moves. Bcopy uses 20: * a cost analysis heuristic to determine if it's worth while to see if a word 21: * copy should be tried. The test is if length > N, then try for a word 22: * copy. This routine will fail if N < 8. 10 is a good value for N: 23: * 24: * Algorithm Cost (in instructions) 25: * byte loop cost 2 * length 26: * word loop setup [worst case] 14 (instructions) 27: * word loop cost 0.625 * length, for length > 10 28: * 29: * N * 2 > 14 N * 0.625 30: * N > 10 31: */ 32: #undef N 33: #define N 10. 34: 35: ENTRY(bcopy) 36: mov 6(sp),r0 / if ((r0 = length) == 0) 37: beq 3f / return 38: mov r2,-(sp) / reserve a register for our use 39: mov 6(sp),r2 / r2 = dst 40: mov 4(sp),r1 / r1 = src 41: cmp r2,r1 / if (dst == src) 42: beq 2f / return 43: bhi 9f / if (dst > src) 44: / do backwards copy 45: 46: /* 47: * copy memory from src:r1 to dst:r2 for length:r0 bytes, FORWARDS ... 48: */ 49: cmp r0,$N / if (length >= N) 50: bhi 4f / try words 51: 1: 52: movb (r1)+,(r2)+ / do *dst++ = *src++ 53: sob r0,1b / while (--length) 54: 2: 55: mov (sp)+,r2 / and return 56: 3: 57: rts pc 58: 59: /* 60: * The length of the copy justifies trying a word by word copy. If src and 61: * dst are of the same parity, we can do a word copy by handling any leading 62: * and trailing odd bytes separately. 63: */ 64: 4: 65: bit $1,r1 / if (src&1 != dst&1) 66: beq 5f / do bytes 67: bit $1,r2 68: beq 1b / (src odd, dst even - do bytes) 69: movb (r1)+,(r2)+ / copy leading odd byte 70: dec r0 71: br 6f 72: 5: 73: bit $1,r2 74: bne 1b / (src even, dst odd - do bytes) 75: 6: 76: mov r0,-(sp) / save trailing byte indicator 77: asr r0 / length >>= 1 (unsigned) 78: bic $0100000,r0 / (length is now in remaining words to copy) 79: asr r0 / if (length >>= 1, wasodd(length)) 80: bcc 7f / handle leading non multiple of four 81: mov (r1)+,(r2)+ 82: 7: 83: asr r0 / if (length >>= 1, wasodd(length)) 84: bcc 8f / handle leading non multiple of eight 85: mov (r1)+,(r2)+ 86: mov (r1)+,(r2)+ 87: 8: 88: mov (r1)+,(r2)+ / do 89: mov (r1)+,(r2)+ / move eight bytes 90: mov (r1)+,(r2)+ 91: mov (r1)+,(r2)+ 92: sob r0,8b / while (--length) 93: bit $1,(sp)+ / if (odd trailing byte) 94: beq 2b 95: movb (r1)+,(r2)+ / copy it 96: br 2b / and return 97: 98: /* 99: * copy memory from src:r1 to dst:r2 for length:r0 bytes, BACKWARDS ... 100: */ 101: 9: 102: add r0,r1 / src += length 103: add r0,r2 / dst += length 104: 105: cmp r0,$N / if (length >= N) 106: bhi 4f / try words 107: 1: 108: movb -(r1),-(r2) / do *--dst = *--src 109: sob r0,1b / while (--length) 110: 2: 111: mov (sp)+,r2 / and return 112: 3: 113: rts pc 114: 115: /* 116: * The length of the copy justifies trying a word by word copy. If src and 117: * dst are of the same parity, we can do a word copy by handling any leading 118: * and trailing odd bytes separately. 119: */ 120: 4: 121: bit $1,r1 / if (src&1 != dst&1) 122: beq 5f / do bytes 123: bit $1,r2 124: beq 1b / (src odd, dst even - do bytes) 125: movb -(r1),-(r2) / copy leading odd byte 126: dec r0 127: br 6f 128: 5: 129: bit $1,r2 130: bne 1b / (src even, dst odd - do bytes) 131: 6: 132: mov r0,-(sp) / save trailing byte indicator 133: asr r0 / length >>= 1 (unsigned) 134: bic $0100000,r0 / (length is now in remaining words to copy) 135: asr r0 / if (length >>= 1, wasodd(length)) 136: bcc 7f / handle leading non multiple of four 137: mov -(r1),-(r2) 138: 7: 139: asr r0 / if (length >>= 1, wasodd(length)) 140: bcc 8f / handle leading non multiple of eight 141: mov -(r1),-(r2) 142: mov -(r1),-(r2) 143: 8: 144: mov -(r1),-(r2) / do 145: mov -(r1),-(r2) / move eight bytes 146: mov -(r1),-(r2) 147: mov -(r1),-(r2) 148: sob r0,8b / while (--length) 149: bit $1,(sp)+ / if (odd trailing byte) 150: beq 2b 151: movb -(r1),-(r2) / copy it 152: br 2b / and return