1 /* $NetBSD: in_cksum_arm.S,v 1.2 2003/09/23 10:01:36 scw Exp $ */
4 * Copyright 2003 Wasabi Systems, Inc.
7 * Written by Steve C. Woodford for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
40 * Hand-optimised in_cksum() and in4_cksum() implementations for ARM/armv5e
45 #include <machine/asm.h>
47 __FBSDID("$FreeBSD$");
51 * int in_cksum(struct mbuf *m, int len)
57 * NOTE: Assumes 'm' is *never* NULL.
59 /* LINTSTUB: Func: int in_cksum(struct mbuf *, int) */
61 stmfd sp!, {r4-r11,lr}
68 ldr r1, [ip, #(M_LEN)]
69 ldr r0, [ip, #(M_DATA)]
70 ldr ip, [ip, #(M_NEXT)]
78 blne _ASM_LABEL(L_cksumdata)
89 add r0, r0, r8, lsr #16
90 add r0, r0, r0, lsr #16
93 ldmfd sp!, {r4-r11,pc}
97 stmfd sp!, {r4-r7, lr}
100 ldmfd sp!, {r4-r7, pc}
104 * The main in*_cksum() workhorse...
107 * r0 Pointer to buffer
112 * r2 Accumulated 32-bit sum
117 /* LINTSTUB: Ignore */
118 ASENTRY_NP(L_cksumdata)
119 pld [r0] /* Pre-fetch the start of the buffer */
122 /* We first have to word-align the buffer. */
124 beq .Lcksumdata_wordaligned
126 cmp r1, r7 /* Enough bytes left to make it? */
127 blt .Lcksumdata_endgame
129 ldrb r4, [r0], #0x01 /* Fetch 1st byte */
130 ldrbge r5, [r0], #0x01 /* Fetch 2nd byte */
132 ldrbgt r6, [r0], #0x01 /* Fetch 3rd byte */
135 /* Combine the three bytes depending on endianness and alignment */
136 orreq r2, r4, r5, lsl #8
137 orreq r2, r2, r6, lsl #16
138 orrne r2, r5, r4, lsl #8
139 orrne r2, r2, r6, lsl #24
140 subs r1, r1, r7 /* Update length */
141 RETeq /* All done? */
143 /* Buffer is now word aligned */
144 .Lcksumdata_wordaligned:
145 cmp r1, #0x04 /* Less than 4 bytes left? */
146 blt .Lcksumdata_endgame /* Yup */
148 /* Now quad-align, if necessary */
150 ldrne r7, [r0], #0x04
153 blt .Lcksumdata_bigloop_end /* Note: C flag clear if branch taken */
156 * Buffer is now quad aligned. Sum 64 bytes at a time.
157 * Note: First ldrd is hoisted above the loop, together with
158 * setting r6 to zero to avoid stalling for results in the
159 * loop. (r7 is live, from above).
191 ldrdge r4, [r0], #0x08
192 bge .Lcksumdata_bigloop
194 adds r2, r2, r6 /* r6/r7 still need summing */
195 .Lcksumdata_bigloop_end:
203 ldrdge r4, [r0], #0x08 /* Avoid stalling pld and result */
204 blt .Lcksumdata_less_than_32
215 adcs r2, r2, r6 /* XXX: Unavoidable result stall */
221 .Lcksumdata_less_than_32:
222 /* There are less than 32 bytes left */
226 adds r4, r4, r4, lsr #1 /* Side effect: Clear carry flag */
231 * Note: We use ldm here, even on armv5e, since the combined issue/result
232 * latencies for ldm and ldrd are the same. Using ldm avoids needless #ifdefs.
234 /* At least 24 bytes remaining... */
239 /* At least 16 bytes remaining... */
244 /* At least 8 bytes remaining... */
249 /* Less than 8 bytes remaining... */
252 blt .Lcksumdata_lessthan4
259 /* Deal with < 4 bytes remaining */
260 .Lcksumdata_lessthan4:
264 /* Deal with 1 to 3 remaining bytes, possibly misaligned */
266 ldrb r3, [r0] /* Fetch first byte */
268 ldrbge r4, [r0, #0x01] /* Fetch 2nd and 3rd as necessary */
270 ldrbgt r5, [r0, #0x02]
272 /* Combine the three bytes depending on endianness and alignment */
274 orreq r3, r3, r4, lsl #8
275 orreq r3, r3, r5, lsl #16
276 orrne r3, r4, r3, lsl #8
277 orrne r3, r3, r5, lsl #24