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 "strcmp.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 strcmp  (optimized assembler version for the 80960K Series)
45
46	result = strcmp (src1_addr, src2_addr)
47
48	compare the null terminated string pointed to by src1_addr to
49	the string pointed to by src2_addr.  Return 0 iff the strings
50	are equal, -1 if src1_addr is lexicographically less than src2_addr,
51	and 1 if it is lexicographically greater.
52
53	Undefined behavior will occur if the end of either source string
54	(i.e. the terminating null byte) is in the last two words of the
55	program's allocated memory space.  This is so because strcmp fetches
56	ahead.  Disallowing the fetch ahead would impose a severe performance
57	penalty.
58
59	Strategy:
60
61	Fetch the source strings by words and compare the words until either
62	differing words are found or the null byte is encountered.  In either
63	case, move through the word until either the differing byte if found,
64	in which case return -1 or 1 appropriately;  or the null byte is
65	encountered, in which case, return zero (equality).
66
67	Tactics:
68
69	1) Do NOT try to fetch the words in a word aligned manner because,
70	in my judgement, the performance degradation experienced due to
71	non-aligned accesses does NOT outweigh the time and complexity added
72	by the preamble and convoluted body that would be necessary to assure
73	alignment.  This is supported by the intuition that many source
74	strings will be word aligned to begin with.
75*/
76
77	.globl _strcmp
78	.globl __strcmp
79	.leafproc _strcmp,__strcmp
80	.align 2
81
82_strcmp:
83#ifndef __PIC
84	lda	.Lrett,g14
85#else
86	lda	.Lrett-(.+8)(ip),g14
87#endif
88__strcmp:
89	ld	(g0), g5	# fetch first word of source_1
90	mov	g14,g7		# preserve return address
91	ldconst 0,g14		# conform to register conventions
92	ldconst 0xff,g4		# byte extraction mask
93.Lwloop:
94	addo     4,g0,g0	# post-increment source_1 byte ptr
95	ld	(g1), g3	# fetch word of source_2
96	scanbyte 0,g5		# does word have a null byte?
97        mov      g5,g2		# save a copy of the source_1 word
98	be .Lcloop		# branch if null byte encountered
99	cmpo	 g2,g3		# are the source words the same?
100	addo     4,g1,g1	# post-increment source_2 byte ptr
101	ld	(g0), g5	# fetch ahead next word of source_1
102	be	 .Lwloop		# fall thru if words are unequal
103
104.Lcloop: and	g4,g2,g5  	# extract and compare individual bytes
105	and	g4,g3,g6
106	cmpobne	g5,g6,.diff	# if they differ, go return 1 or -1
107	cmpo    0,g6		# they are the same.  Are they null?
108	shlo	8,g4,g4		# position mask for next extraction
109	bne	.Lcloop		# loop if null not encountered
110
111	mov	0,g0		# return equality
112	bx	(g7)
113.Lrett:
114	ret
115.diff:	bl	.neg
116        mov     1,g0
117	bx	(g7)
118.neg:	subi    1,0,g0
119.Lexit:
120	bx	(g7)
121