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