📄 memcmp.s
字号:
/* @(#)memcmp.s 4.1 (ULTRIX) 7/3/90 *//* ------------------------------------------------------------------ *//* | Copyright Unpublished, MIPS Computer Systems, Inc. All Rights | *//* | Reserved. This software contains proprietary and confidential | *//* | information of MIPS and its suppliers. Use, disclosure or | *//* | reproduction is prohibited without the prior express written | *//* | consent of MIPS. | *//* ------------------------------------------------------------------ *//* * Copyright 1985 by MIPS Computer Systems, Inc. *//************************************************************************ * * * Copyright (c) 1989 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//************************************************************************ * * Modification History * * 0000 Ken Lesniak, 26-Sep-1989 * Derived from bcmp.s * ************************************************************************//* memcmp(s1, s2, n) */#include <mips/regdef.h>#include <mips/asm.h>/* * memcmp(src, dst, bcount) * * a0 = src * a1 = dst * a2 = bcount * * MINCMP is minimum number of byte that its worthwhile to try and * align cmp into word transactions * * Calculating MINCMP * Overhead =~ 15 instructions => 90 cycles * Byte cmp =~ 38 cycles/word * Word cmp =~ 17 cycles/word * Breakeven =~ 16 bytes */#define MINCMP 16#define NBPW 4LEAF(memcmp) xor v0,a0,a1 blt a2,MINCMP,bytecmp # too short, just byte cmp and v0,NBPW-1 subu t8,zero,a0 # number of bytes til aligned bne v0,zero,unalgncmp # src and dst not alignable/* * src and dst can be simultaneously word aligned */ and t8,NBPW-1 subu a2,t8 beq t8,zero,wordcmp # already aligned move v0,v1#ifdef MIPSEB lwl v0,0(a0) # cmp unaligned portion lwl v1,0(a1)#endif#ifdef MIPSEL lwr v0,0(a0) lwr v1,0(a1)#endif addu a0,t8 addu a1,t8 bne v0,v1,cmpne/* * word cmp loop */wordcmp: and a3,a2,~(NBPW-1) subu a2,a3 beq a3,zero,bytecmp addu a3,a0 # src1 endpoint1: lw v0,0(a0) lw v1,0(a1) addu a0,NBPW # 1st BDSLOT addu a1,NBPW # 2nd BDSLOT (asm doesn't move) bne v0,v1,cmpne bne a0,a3,1b # at least one more word b bytecmp/* * deal with simultaneously unalignable cmp by aligning one src */unalgncmp: subu a3,zero,a1 # calc byte cnt to get src2 aligned and a3,NBPW-1 subu a2,a3 beq a3,zero,partaligncmp # already aligned addu a3,a0 # src1 endpoint1: lbu v0,0(a0) lbu v1,0(a1) addu a0,1 addu a1,1 bne v0,v1,cmpne bne a0,a3,1b/* * src unaligned, dst aligned loop */partaligncmp: and a3,a2,~(NBPW-1) subu a2,a3 beq a3,zero,bytecmp addu a3,a01:#ifdef MIPSEB lwl v0,0(a0) lwr v0,3(a0)#endif#ifdef MIPSEL lwr v0,0(a0) lwl v0,3(a0)#endif lw v1,0(a1) addu a0,NBPW addu a1,NBPW bne v0,v1,cmpne bne a0,a3,1b/* * brute force byte cmp loop */bytecmp: addu a3,a2,a0 # src1 endpoint; BDSLOT ble a2,zero,2f1: lbu v0,0(a0) lbu v1,0(a1) addu a0,1 addu a1,1 bne v0,v1,3f bne a0,a3,1b2: move v0,zero j ra3: subu v0,v0,v1 j ra/* * A word comparison was not equal, find the byte that differed */cmpne:#ifdef MIPSEB sltu v0,v1,v0 # v0 = 1 => s2 < s1; v0 = 0 => s2 > s1 sll v0,1 # v0 = 2 => s2 < s1; v0 = 0 => s2 > s1 subu v0,1 # v0 = 1 => s2 < s2; v0 = -1 => s2 > s1 j ra#endif#ifdef MIPSEL1: and t0,v0,0xff # extract next byte of s1 and t1,v1,0xff # extract next byte of s2 srl v0,v0,8 # shift next byte of s1 into place srl v1,v1,8 # shift next byte of s2 into place beq t0,t1,1b # loop until we hit the different one subu v0,t0,t1 # return the difference j ra#endif.end memcmp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -