summaryrefslogtreecommitdiffstats
path: root/arch/m32r/lib/checksum.S
blob: 0af0360c76d9505f2ce1965f3fc9e337830d1f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		IP/TCP/UDP checksumming routines
 *
 * Authors:	Jorge Cwik, <jorge@laser.satlink.net>
 *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
 *		Tom May, <ftom@netcom.com>
 *              Pentium Pro/II routines:
 *              Alexander Kjeldaas <astor@guardian.no>
 *              Finn Arne Gangstad <finnag@guardian.no>
 *		Lots of code moved from tcp.c and ip.c; see those files
 *		for more names.
 *
 * Changes:     Ingo Molnar, converted csum_partial_copy() to 2.1 exception
 *			     handling.
 *		Andi Kleen,  add zeroing on error
 *                   converted to pure assembler
 *		Hirokazu Takata,Hiroyuki Kondo rewrite for the m32r architecture.
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 */

#include <linux/linkage.h>
#include <asm/assembler.h>
#include <asm/errno.h>

/*
 * computes a partial checksum, e.g. for TCP/UDP fragments
 */

/*
unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
 */


#ifdef CONFIG_ISA_DUAL_ISSUE

	/*
	 * Experiments with Ethernet and SLIP connections show that buff
	 * is aligned on either a 2-byte or 4-byte boundary.  We get at
	 * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
	 * Fortunately, it is easy to convert 2-byte alignment to 4-byte
	 * alignment for the unrolled loop.
	 */

	.text
ENTRY(csum_partial)
	; Function args
	;  r0: unsigned char *buff
	;  r1: int len
	;  r2: unsigned int sum

	push	r2		    ||	ldi	r2, #0
	and3	r7, r0, #1		; Check alignment.
	beqz	r7, 1f	 		; Jump if alignment is ok.
	; 1-byte mis aligned
	ldub	r4, @r0		    ||	addi	r0, #1
	; clear c-bit || Alignment uses up bytes.
	cmp	r0, r0		    ||	addi	r1, #-1
	ldi	r3, #0		    ||	addx	r2, r4
	addx	r2, r3
	.fillinsn
1:
	and3	r4, r0, #2		; Check alignment.
	beqz	r4, 2f	 		; Jump if alignment is ok.
	; clear c-bit || Alignment uses up two bytes.
	cmp	r0, r0		    ||	addi	r1, #-2
	bgtz	r1, 1f			; Jump if we had at least two bytes.
	bra	4f		    ||	addi	r1, #2
	.fillinsn			; len(r1) was < 2.  Deal with it.
1:
	; 2-byte aligned
	lduh	r4, @r0		    ||	ldi	r3, #0
	addx	r2, r4		    ||	addi	r0, #2
	addx	r2, r3
	.fillinsn
2:
	; 4-byte aligned
	cmp	r0, r0			; clear c-bit
	srl3	r6, r1, #5
	beqz	r6, 2f
	.fillinsn

1:	ld	r3, @r0+
	ld	r4, @r0+					; +4
	ld	r5, @r0+					; +8
	ld	r3, @r0+	    ||	addx    r2, r3		; +12
	ld	r4, @r0+	    ||	addx    r2, r4		; +16
	ld	r5, @r0+	    ||	addx    r2, r5		; +20
	ld	r3, @r0+	    ||	addx    r2, r3		; +24
	ld	r4, @r0+	    ||	addx    r2, r4		; +28
	addx	r2, r5		    ||	addi	r6, #-1
	addx	r2, r3
	addx	r2, r4
	bnez	r6, 1b

	addx	r2, r6			; r6=0
	cmp	r0, r0			; This clears c-bit
	.fillinsn
2:	and3	r6, r1, #0x1c		; withdraw len
	beqz	r6, 4f
	srli	r6, #2
	.fillinsn

3:	ld	r4, @r0+	    ||	addi	r6, #-1
	addx	r2, r4
	bnez	r6, 3b

	addx	r2, r6			; r6=0
	cmp	r0, r0			; This clears c-bit
	.fillinsn
4:	and3	r1, r1, #3
	beqz	r1, 7f			; if len == 0 goto end
	and3	r6, r1, #2
	beqz	r6, 5f			; if len < 2  goto 5f(1byte)
	lduh	r4, @r0		    ||	addi	r0, #2
	addi	r1, #-2		    ||	slli    r4, #16
	addx	r2, r4
	beqz	r1, 6f
	.fillinsn
5:	ldub	r4, @r0		    ||	ldi	r1, #0
#ifndef __LITTLE_ENDIAN__
	slli    r4, #8
#endif
	addx	r2, r4
	.fillinsn
6:	addx	r2, r1
	.fillinsn
7:
	and3	r0, r2, #0xffff
	srli	r2, #16
	add	r0, r2
	srl3	r2, r0, #16
	beqz	r2, 1f
	addi	r0, #1
	and3	r0, r0, #0xffff
	.fillinsn
1:
	beqz	r7, 1f			; swap the upper byte for the lower
	and3	r2, r0, #0xff
	srl3	r0, r0, #8
	slli	r2, #8
	or	r0, r2
	.fillinsn
1:
	pop	r2		    ||	cmp	r0, r0
	addx	r0, r2		    ||	ldi	r2, #0
	addx	r0, r2
	jmp	r14

#else /* not CONFIG_ISA_DUAL_ISSUE */

	/*
	 * Experiments with Ethernet and SLIP connections show that buff
	 * is aligned on either a 2-byte or 4-byte boundary.  We get at
	 * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
	 * Fortunately, it is easy to convert 2-byte alignment to 4-byte
	 * alignment for the unrolled loop.
	 */

	.text
ENTRY(csum_partial)
	; Function args
	;  r0: unsigned char *buff
	;  r1: int len
	;  r2: unsigned int sum

	push	r2
	ldi	r2, #0
	and3	r7, r0, #1		; Check alignment.
	beqz	r7, 1f	 		; Jump if alignment is ok.
	; 1-byte mis aligned
	ldub	r4, @r0
	addi	r0, #1
	addi	r1, #-1			; Alignment uses up bytes.
	cmp	r0, r0			; clear c-bit
	ldi	r3, #0
	addx	r2, r4
	addx	r2, r3
	.fillinsn
1:
	and3	r4, r0, #2		; Check alignment.
	beqz	r4, 2f	 		; Jump if alignment is ok.
	addi	r1, #-2			; Alignment uses up two bytes.
	cmp		r0, r0			; clear c-bit
	bgtz	r1, 1f			; Jump if we had at least two bytes.
	addi	r1, #2			; len(r1) was < 2.  Deal with it.
	bra	4f
	.fillinsn
1:
	; 2-byte aligned
	lduh	r4, @r0
	addi	r0, #2
	ldi		r3, #0
	addx	r2, r4
	addx	r2, r3
	.fillinsn
2:
	; 4-byte aligned
	cmp	r0, r0			; clear c-bit
	srl3	r6, r1, #5
	beqz	r6, 2f
	.fillinsn

1:	ld	r3, @r0+
	ld	r4, @r0+		; +4
	ld	r5, @r0+		; +8
	addx	r2, r3
	addx	r2, r4
	addx	r2, r5
	ld	r3, @r0+		; +12
	ld	r4, @r0+		; +16
	ld	r5, @r0+		; +20
	addx	r2, r3
	addx	r2, r4
	addx	r2, r5
	ld	r3, @r0+		; +24
	ld	r4, @r0+		; +28
	addi	r6, #-1
	addx	r2, r3
	addx	r2, r4
	bnez	r6, 1b
	addx	r2, r6			; r6=0
	cmp	r0, r0			; This clears c-bit
	.fillinsn

2:	and3	r6, r1, #0x1c		; withdraw len
	beqz	r6, 4f
	srli	r6, #2
	.fillinsn

3:	ld	r4, @r0+
	addi	r6, #-1
	addx	r2, r4
	bnez	r6, 3b
	addx	r2, r6			; r6=0
	cmp	r0, r0			; This clears c-bit
	.fillinsn

4:	and3	r1, r1, #3
	beqz	r1, 7f			; if len == 0 goto end
	and3	r6, r1, #2
	beqz	r6, 5f			; if len < 2  goto 5f(1byte)

	lduh	r4, @r0
	addi	r0, #2
	addi	r1, #-2
	slli    r4, #16
	addx	r2, r4
	beqz	r1, 6f
	.fillinsn
5:	ldub	r4, @r0
#ifndef __LITTLE_ENDIAN__
	slli    r4, #8
#endif
	addx	r2, r4
	.fillinsn
6:	ldi	r5, #0
	addx	r2, r5
	.fillinsn
7:
	and3	r0, r2, #0xffff
	srli	r2, #16
	add	r0, r2
	srl3	r2, r0, #16
	beqz	r2, 1f
	addi	r0, #1
	and3	r0, r0, #0xffff
	.fillinsn
1:
	beqz	r7, 1f
	mv	r2, r0
	srl3	r0, r2, #8
	and3	r2, r2, #0xff
	slli	r2, #8
	or	r0, r2
	.fillinsn
1:
	pop	r2
	cmp	r0, r0
	addx	r0, r2
	ldi	r2, #0
	addx	r0, r2
	jmp	r14

#endif /* not CONFIG_ISA_DUAL_ISSUE */

/*
unsigned int csum_partial_copy_generic (const char *src, char *dst,
				  int len, int sum, int *src_err_ptr, int *dst_err_ptr)
 */

/*
 * Copy from ds while checksumming, otherwise like csum_partial
 *
 * The macros SRC and DST specify the type of access for the instruction.
 * thus we can call a custom exception handler for all access types.
 *
 * FIXME: could someone double-check whether I haven't mixed up some SRC and
 *	  DST definitions? It's damn hard to trigger all cases.  I hope I got
 *	  them all but there's no guarantee.
 */

ENTRY(csum_partial_copy_generic)
	nop
	nop
	nop
	nop
	jmp r14
	nop
	nop
	nop

	.end