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$");
50 * int in_cksum(struct mbuf *m, int len)
56 * NOTE: Assumes 'm' is *never* NULL.
58 /* LINTSTUB: Func: int in_cksum(struct mbuf *, int) */
60 stmfd sp!, {r4-r11,lr}
67 ldr r1, [ip, #(M_LEN)]
68 ldr r0, [ip, #(M_DATA)]
69 ldr ip, [ip, #(M_NEXT)]
77 blne _ASM_LABEL(L_cksumdata)
88 add r0, r0, r8, lsr #16
89 add r0, r0, r0, lsr #16
92 ldmfd sp!, {r4-r11,pc}
96 stmfd sp!, {r4-r7, lr}
99 ldmfd sp!, {r4-r7, pc}
103 * The main in*_cksum() workhorse...
106 * r0 Pointer to buffer
111 * r2 Accumulated 32-bit sum
116 /* LINTSTUB: Ignore */
117 ASENTRY_NP(L_cksumdata)
119 pld [r0] /* Pre-fetch the start of the buffer */
123 /* We first have to word-align the buffer. */
125 beq .Lcksumdata_wordaligned
127 cmp r1, r7 /* Enough bytes left to make it? */
128 blt .Lcksumdata_endgame
130 ldrb r4, [r0], #0x01 /* Fetch 1st byte */
131 ldrgeb r5, [r0], #0x01 /* Fetch 2nd byte */
133 ldrgtb r6, [r0], #0x01 /* Fetch 3rd byte */
135 /* Combine the three bytes depending on endianness and alignment */
137 orreq r2, r5, r4, lsl #8
138 orreq r2, r2, r6, lsl #24
139 orrne r2, r4, r5, lsl #8
140 orrne r2, r2, r6, lsl #16
142 orreq r2, r4, r5, lsl #8
143 orreq r2, r2, r6, lsl #16
144 orrne r2, r5, r4, lsl #8
145 orrne r2, r2, r6, lsl #24
147 subs r1, r1, r7 /* Update length */
148 RETeq /* All done? */
150 /* Buffer is now word aligned */
151 .Lcksumdata_wordaligned:
153 cmp r1, #0x04 /* Less than 4 bytes left? */
154 blt .Lcksumdata_endgame /* Yup */
156 /* Now quad-align, if necessary */
158 ldrne r7, [r0], #0x04
161 blt .Lcksumdata_bigloop_end /* Note: C flag clear if branch taken */
164 * Buffer is now quad aligned. Sum 64 bytes at a time.
165 * Note: First ldrd is hoisted above the loop, together with
166 * setting r6 to zero to avoid stalling for results in the
167 * loop. (r7 is live, from above).
199 ldrged r4, [r0], #0x08
200 bge .Lcksumdata_bigloop
202 adds r2, r2, r6 /* r6/r7 still need summing */
203 .Lcksumdata_bigloop_end:
207 #else /* !_ARM_ARCH_5E */
210 blt .Lcksumdata_bigloop_end
213 ldmia r0!, {r3, r4, r5, r6}
217 ldmia r0!, {r3, r4, r5, r7}
222 ldmia r0!, {r3, r4, r5, r6}
227 ldmia r0!, {r3, r4, r5, r7}
235 bge .Lcksumdata_bigloop
236 .Lcksumdata_bigloop_end:
244 ldrged r4, [r0], #0x08 /* Avoid stalling pld and result */
245 blt .Lcksumdata_less_than_32
256 adcs r2, r2, r6 /* XXX: Unavoidable result stall */
259 blt .Lcksumdata_less_than_32
260 ldmia r0!, {r3, r4, r5, r6}
264 ldmia r0!, {r3, r4, r5, r7}
275 .Lcksumdata_less_than_32:
276 /* There are less than 32 bytes left */
280 adds r4, r4, r4, lsr #1 /* Side effect: Clear carry flag */
285 * Note: We use ldm here, even on armv5e, since the combined issue/result
286 * latencies for ldm and ldrd are the same. Using ldm avoids needless #ifdefs.
288 /* At least 24 bytes remaining... */
293 /* At least 16 bytes remaining... */
298 /* At least 8 bytes remaining... */
303 /* Less than 8 bytes remaining... */
306 blt .Lcksumdata_lessthan4
313 /* Deal with < 4 bytes remaining */
314 .Lcksumdata_lessthan4:
318 /* Deal with 1 to 3 remaining bytes, possibly misaligned */
320 ldrb r3, [r0] /* Fetch first byte */
322 ldrgeb r4, [r0, #0x01] /* Fetch 2nd and 3rd as necessary */
324 ldrgtb r5, [r0, #0x02]
326 /* Combine the three bytes depending on endianness and alignment */
329 orreq r3, r4, r3, lsl #8
330 orreq r3, r3, r5, lsl #24
331 orrne r3, r3, r4, lsl #8
332 orrne r3, r3, r5, lsl #16
334 orreq r3, r3, r4, lsl #8
335 orreq r3, r3, r5, lsl #16
336 orrne r3, r4, r3, lsl #8
337 orrne r3, r3, r5, lsl #24