1/*******************************************************************************
2 *
3 * Copyright (c) 1993 Intel Corporation
4 *
5 * Intel hereby grants you permission to copy, modify, and distribute this
6 * software and its documentation.  Intel grants this permission provided
7 * that the above copyright notice appears in all copies and that both the
8 * copyright notice and this permission notice appear in supporting
9 * documentation.  In addition, Intel grants this permission provided that
10 * you prominently mark as "not part of the original" any modifications
11 * made to this software or documentation, and that the name of Intel
12 * Corporation not be used in advertising or publicity pertaining to
13 * distribution of the software or the documentation without specific,
14 * written prior permission.
15 *
16 * Intel Corporation provides this AS IS, WITHOUT ANY WARRANTY, EXPRESS OR
17 * IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY
18 * OR FITNESS FOR A PARTICULAR PURPOSE.  Intel makes no guarantee or
19 * representations regarding the use of, or the results of the use of,
20 * the software and documentation in terms of correctness, accuracy,
21 * reliability, currentness, or otherwise; and you rely on the software,
22 * documentation and results solely at your own risk.
23 *
24 * IN NO EVENT SHALL INTEL BE LIABLE FOR ANY LOSS OF USE, LOSS OF BUSINESS,
25 * LOSS OF PROFITS, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES
26 * OF ANY KIND.  IN NO EVENT SHALL INTEL'S TOTAL LIABILITY EXCEED THE SUM
27 * PAID TO INTEL FOR THE PRODUCT LICENSED HEREUNDER.
28 *
29 ******************************************************************************/
30
31#include <picolibc.h>
32
33	.file "memcmp.s"
34#ifdef	__PIC
35	.pic
36#endif
37#ifdef	__PID
38	.pid
39#endif
40/*
41 * (c) copyright 1988,1993 Intel Corp., all rights reserved
42 */
43/*
44	procedure memcmp  (optimized assembler version for the 80960K series)
45
46	result = memcmp (src1_addr, src2_addr, max_bytes)
47
48	compare the byte array pointed to by src1_addr to the byte array
49	pointed to by src2_addr.  Return 0 iff the arrays are equal, -1 iff
50	src1_addr is lexicographically less than src2_addr, and 1 iff it is
51	lexicographically greater.  Do not compare more than max_bytes bytes.
52
53	Undefined behavior will occur if the end of either source array
54	is in the last two words of the program's allocated memory space.
55	This is so because memcmp fetches ahead.  Disallowing the fetch ahead
56	would impose a severe performance penalty.
57
58	Strategy:
59
60	Fetch the source strings by words and compare the words until either
61	a differing word is found or max_bytes is exhausted.  In the former
62	case, move through the words to find the differing byte and return
63	plus or minus one, appropriately.  In the latter case, return zero
64	(equality).
65
66	Tactics:
67
68	1) Do NOT try to fetch the words in a word aligned manner because,
69	in my judgement, the performance degradation experienced due to
70	non-aligned accesses does NOT outweigh the time and complexity added
71	by the preamble that would be necessary to assure alignment.  This
72	is supported by the intuition that most source arrays (even more
73	true of most big source arrays) will be word aligned to begin with.
74
75	2) Rather than decrementing max_bytes to zero, I calculate the
76	address of the byte after the last byte of the source_1 array, and
77	quit when the source byte pointer passes that.
78*/
79
80	.globl _memcmp
81	.globl __memcmp
82	.leafproc _memcmp,__memcmp
83	.align 2
84
85_memcmp:
86#ifndef __PIC
87	lda	.Lrett,g14
88#else
89	lda	.Lrett-(.+8)(ip),g14
90#endif
91__memcmp:
92	mov	g14,g13		# preserve return address
93	ldconst 0,g14		# conform to register conventions
94	cmpibge	0,g2,Lequal_exit	# quit if max_bytes <= 0
95	addo	g0,g2,g2	# calculate byte addr of byte after last in src1
96
97.Lwloop:
98	cmpo	 g0,g2
99	ld	(g0), g5	# fetch word of source_1
100	bge 	 Lequal_exit	# quit (equal) if max_bytes exhausted
101	ld	(g1), g3	# fetch word of source_2
102	addo     4,g0,g0	# post-increment source_1 byte ptr
103	addo     4,g1,g1       	# post-increment source_2 byte ptr
104	cmpobe	 g5,g3,.Lwloop	# branch if source words are equal
105
106	ldconst 0xff,g4		# byte extraction mask
107	subo     4,g0,g0	# back up src1 pointer
108
109.Lcloop: and	g4,g5,g7  	# extract and compare individual bytes
110	and	g4,g3,g6
111	cmpobne	g7,g6,.diff	# branch if they are different
112	shlo	8,g4,g4		# position mask for next extraction
113	addo	1,g0,g0
114	cmpobl	g0,g2,.Lcloop	# quit if max_bytes is exhausted
115
116Lequal_exit:
117	mov	0,g0
118	bx	(g13)
119.Lrett:
120	ret
121
122.diff:	bl	.neg		# arrays differ at current byte.
123				/* return 1 or -1 appropriately */
124        mov     1,g0
125	bx	(g13)
126.neg:	subi    1,0,g0
127.Lexit:
128	bx	(g13)
129
130/* end or memcmp */
131