xref: /titanic_51/usr/src/uts/sparc/ml/ip_ocsum.s (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#ident	"%Z%%M%	%I%	%E% SMI"
28
29#include <sys/asm_linkage.h>
30
31#if defined(lint)
32#include <sys/types.h>
33#endif	/* lint */
34
35/*
36 * ip_ocsum(address, halfword_count, sum)
37 * Do a 16 bit one's complement sum of a given number of (16-bit)
38 * halfwords. The halfword pointer must not be odd.
39 *	%o0 address; %o1 count; %o2 sum accumulator; %o4 temp
40 * 	%g2 and %g3 used in main loop
41 *
42 * (from @(#)ocsum.s 1.3 89/02/24 SMI)
43 *
44 */
45
46#if defined(lint)
47
48/* ARGSUSED */
49unsigned int
50ip_ocsum(u_short *address, int halfword_count, unsigned int sum)
51{ return (0); }
52
53#else	/* lint */
54
55	ENTRY(ip_ocsum)
56	cmp	%o1, 31		! less than 62 bytes?
57	bl,a	.dohw		!   just do halfwords
58	tst	%o1		! delay slot, test count
59
60	btst	31, %o0		! (delay slot)
61	bz	2f		! if 32 byte aligned, skip
62	nop
63
64	!
65	! Do first halfwords until 32-byte aligned
66	!
671:
68	lduh	[%o0], %g2	! read data
69	add	%o0, 2, %o0	! increment address
70	add	%o2, %g2, %o2	! add to accumulator, don't need carry yet
71	btst	31, %o0		! 32 byte aligned?
72	bnz	1b
73	sub	%o1, 1, %o1	! decrement count
74	!
75	! loop to add in 32 byte chunks
76	! The loads and adds are staggered to help avoid load/use
77	! interlocks on highly pipelined implementations, and double
78	! loads are used for 64-bit wide memory systems.
79	!
802:
81	sub	%o1, 16, %o1	! decrement count to aid testing
824:
83	ldd	[%o0], %g2	! read data
84	ldd	[%o0+8], %o4	! read more data
85	addcc	%o2, %g2, %o2	! add to accumulator
86	addxcc	%o2, %g3, %o2	! add to accumulator with carry
87	ldd	[%o0+16], %g2	! read more data
88	addxcc	%o2, %o4, %o2	! add to accumulator with carry
89	addxcc	%o2, %o5, %o2	! add to accumulator with carry
90	ldd	[%o0+24], %o4	! read more data
91	addxcc	%o2, %g2, %o2	! add to accumulator with carry
92	addxcc	%o2, %g3, %o2	! add to accumulator with carry
93	addxcc	%o2, %o4, %o2	! add to accumulator
94	addxcc	%o2, %o5, %o2	! add to accumulator with carry
95	addxcc	%o2, 0, %o2	! if final carry, add it in
96	subcc	%o1, 16, %o1	! decrement count (in halfwords)
97	bge	4b
98	add	%o0, 32, %o0	! delay slot, increment address
99
100	add	%o1, 16, %o1	! add back in
101	!
102	! Do any remaining halfwords
103	!
104	b	.dohw
105	tst	%o1		! delay slot, for more to do
106
1073:
108	add	%o0, 2, %o0	! increment address
109	addcc	%o2, %g2, %o2	! add to accumulator
110	addxcc	%o2, 0, %o2	! if carry, add it in
111	subcc	%o1, 1, %o1	! decrement count
112.dohw:
113	bg,a	3b		! more to do?
114	lduh	[%o0], %g2	! read data
115
116	!
117	! at this point the 32-bit accumulator
118	! has the result that needs to be returned in 16-bits
119	!
120	sll	%o2, 16, %o4	! put low halfword in high halfword %o4
121	addcc	%o4, %o2, %o2	! add the 2 halfwords in high %o2, set carry
122	srl	%o2, 16, %o2	! shift to low halfword
123	retl			! return
124	addxcc	%o2, 0, %o0	! add in carry if any. result in %o0
125	SET_SIZE(ip_ocsum)
126
127#endif 	/* lint */
128