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	.file "strncmp.s"
32#ifdef	__PIC
33	.pic
34#endif
35#ifdef	__PID
36	.pid
37#endif
38/*
39 * (c) copyright 1988,1993 Intel Corp., all rights reserved
40 */
41/*
42	procedure strncmp  (optimized assembler version for the 80960K Series)
43
44	result = strncmp (src1_addr, src2_addr, max_bytes)
45
46	compare the null terminated string pointed to by src1_addr to
47	the string pointed to by src2_addr.  Return 0 iff the strings
48	are equal, -1 if src1_addr is lexicographically less than src2_addr,
49	and 1 if it is lexicographically greater.  Do not compare more than
50	max_bytes bytes.
51
52	Undefined behavior will occur if the end of either source string
53	(i.e. the terminating null byte) is in the last two words of the
54	program's allocated memory space.  This is so because strncmp
55	will fetch ahead.  Disallowing the fetch ahead would impose
56	a severe performance penalty.
57
58	Strategy:
59
60	Fetch and compare the strings by words and go to a character
61	comparison loop as soon as a pair of words differ.  If the
62	words are equal up through either the exhaustion of max_bytes
63	or the presence of the null byte, return 0 (equality).  Otherwise,
64	the character comparator will return -1 or 1 for inequality, or
65	0 if the differing byte is after the null byte or after the
66	exhaustion of max_bytes.
67
68	Tactics:
69
70	1) Do NOT try to fetch the words in a word aligned manner because,
71	in my judgement, the performance degradation experienced due to
72	non-aligned accesses does NOT outweigh the time and complexity added
73	by the preamble and convoluted body that would be necessary to assure
74	alignment.
75*/
76
77	.globl _strncmp
78	.globl __strncmp
79	.leafproc _strncmp,__strncmp
80	.align 2
81
82_strncmp:
83#ifndef __PIC
84	lda	.Lrett,g14
85#else
86	lda	.Lrett-(.+8)(ip),g14
87#endif
88__strncmp:
89	mov	g14,g13
90	ldconst 0,g14
91	cmpibge	0,g2,Lequal_exit		# Lexit early if max_bytes <= 0
92	addo	g2,g0,g2
93.Lwloop:
94	cmpo	g0,g2		# are max_bytes exhausted?
95	ld	(g0), g5	# fetch word of source_1
96	bge	 Lequal_exit	# Lexit (equality) if max_bytes exhausted
97	ld	(g1), g3	# fetch word of source_2
98	addo     4,g0,g0	# post-increment source_1 ptr
99	scanbyte 0,g5		# is a null byte present?
100	addo     4,g1,g1	# post-increment source_1 ptr
101	be 	.Lcloop.a	# perform char comparator if null byte found
102	cmpobe	 g5,g3,.Lwloop	# perform char comparator if words are unequal
103
104.Lcloop.a: subo	4,g0,g0		# adjust max_byte counter
105	ldconst 0xff,g4		# byte extraction mask
106
107.Lcloop: and	g4,g5,g7  	# compare individual bytes
108	and	g4,g3,g6
109	cmpobne	g7,g6,.diff	# if different, return -1 or 1
110	cmpo    0,g6		# they are equal.  are they null?
111	shlo	8,g4,g4		# position mask to extract next byte
112	be	Lequal_exit	# if they are null, Lexit (equality)
113	addo	1,g0,g0		# is max_bytes exhausted?
114	cmpobl	g0,g2,.Lcloop	# if not, loop.  if so, Lexit (equality)
115
116Lequal_exit:
117	mov	0,g0
118	bx	(g13)
119.Lrett:
120	ret
121.diff:	bl	.neg
122        mov     1,g0
123	bx	(g13)
124
125.neg:	subi    1,0,g0
126.Lexit:
127	bx	(g13)
128
129/* end of strncmp */
130