xref: /dragonfly/sys/cpu/x86_64/misc/in_cksum2.s (revision b1d2a2de)
12acda721SJordan Gordeev/*
22acda721SJordan Gordeev * Copyright (c) 2003,2004,2008 The DragonFly Project.  All rights reserved.
32acda721SJordan Gordeev *
42acda721SJordan Gordeev * This code is derived from software contributed to The DragonFly Project
52acda721SJordan Gordeev * by Matthew Dillon <dillon@backplane.com>
62acda721SJordan Gordeev *
72acda721SJordan Gordeev * Redistribution and use in source and binary forms, with or without
82acda721SJordan Gordeev * modification, are permitted provided that the following conditions
92acda721SJordan Gordeev * are met:
102acda721SJordan Gordeev *
112acda721SJordan Gordeev * 1. Redistributions of source code must retain the above copyright
122acda721SJordan Gordeev *    notice, this list of conditions and the following disclaimer.
132acda721SJordan Gordeev * 2. Redistributions in binary form must reproduce the above copyright
142acda721SJordan Gordeev *    notice, this list of conditions and the following disclaimer in
152acda721SJordan Gordeev *    the documentation and/or other materials provided with the
162acda721SJordan Gordeev *    distribution.
172acda721SJordan Gordeev * 3. Neither the name of The DragonFly Project nor the names of its
182acda721SJordan Gordeev *    contributors may be used to endorse or promote products derived
192acda721SJordan Gordeev *    from this software without specific, prior written permission.
202acda721SJordan Gordeev *
212acda721SJordan Gordeev * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
222acda721SJordan Gordeev * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
232acda721SJordan Gordeev * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
242acda721SJordan Gordeev * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
252acda721SJordan Gordeev * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
262acda721SJordan Gordeev * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
272acda721SJordan Gordeev * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
282acda721SJordan Gordeev * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
292acda721SJordan Gordeev * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
302acda721SJordan Gordeev * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
312acda721SJordan Gordeev * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322acda721SJordan Gordeev * SUCH DAMAGE.
332acda721SJordan Gordeev */
342acda721SJordan Gordeev
352acda721SJordan Gordeev#include <machine/asmacros.h>		/* miscellaneous asm macros */
362acda721SJordan Gordeev
372acda721SJordan Gordeev#include "assym.s"
382acda721SJordan Gordeev
392acda721SJordan Gordeev	.text
402acda721SJordan Gordeev
412acda721SJordan Gordeev	/*
422acda721SJordan Gordeev	 * asm_ones32(32bitalignedbuffer, numberof32bitwords)
432acda721SJordan Gordeev	 *
442acda721SJordan Gordeev	 * Returns the 32 bit one complement partial checksum.  This is
452acda721SJordan Gordeev	 * basically a 1's complement checksum without the inversion (~)
462acda721SJordan Gordeev	 * at the end.  A 32 bit value is returned.  If the caller is
472acda721SJordan Gordeev	 * calculating a 16 bit 1's complement checksum the caller must
482acda721SJordan Gordeev	 * collapse the 32 bit return value via:
492acda721SJordan Gordeev	 *
502acda721SJordan Gordeev	 *	result = (result >> 16) + (result & 0xFFFF)
512acda721SJordan Gordeev	 *	if (result > 0xFFFF)
522acda721SJordan Gordeev	 *	    result -= 0xFFFF;	<<< same as (result + 1) & 0xFFFF
532acda721SJordan Gordeev	 *				    within the range of result.
542acda721SJordan Gordeev	 * Note that worst case 0xFFFFFFFF + 0xFFFFFFFF = 0xFFFFFFFE + CARRY,
552acda721SJordan Gordeev	 * so no double-carry ever occurs.
562acda721SJordan Gordeev	 */
572acda721SJordan Gordeev	.p2align 4
582acda721SJordan GordeevENTRY(asm_ones32)
592acda721SJordan Gordeev	movq	%rdi,%rdx	/* %rdx = buffer pointer */
602acda721SJordan Gordeev	movl	%esi,%ecx	/* %ecx = counter */
612acda721SJordan Gordeev	xorl	%eax,%eax	/* %eax = checksum */
622acda721SJordan Gordeev	cmpl	$5,%ecx
632acda721SJordan Gordeev	jl	2f
642acda721SJordan Gordeev1:
652acda721SJordan Gordeev	subl	$5,%ecx
662acda721SJordan Gordeev	addl	(%rdx),%eax
672acda721SJordan Gordeev	adcl	4(%rdx),%eax
682acda721SJordan Gordeev	adcl	8(%rdx),%eax
692acda721SJordan Gordeev	adcl	12(%rdx),%eax
702acda721SJordan Gordeev	adcl	16(%rdx),%eax
712acda721SJordan Gordeev	adcl	$0,%eax
722acda721SJordan Gordeev	addq	$20,%rdx
732acda721SJordan Gordeev	cmpl	$5,%ecx
742acda721SJordan Gordeev	jge	1b
752acda721SJordan Gordeev2:
762acda721SJordan Gordeev	testl	%ecx,%ecx
772acda721SJordan Gordeev	je	4f
782acda721SJordan Gordeev3:
792acda721SJordan Gordeev	addl	(%rdx),%eax
802acda721SJordan Gordeev	adcl	$0,%eax
812acda721SJordan Gordeev	addq	$4,%rdx
822acda721SJordan Gordeev	decl	%ecx
832acda721SJordan Gordeev	jnz	3b
842acda721SJordan Gordeev4:
852acda721SJordan Gordeev	ret
86*b1d2a2deSzrjEND(asm_ones32)
87