xref: /titanic_52/usr/src/lib/libc/i386/gen/memcmp.s (revision 9a70fc3be3b1e966bf78825cdb8d509963a6f0a1)
17c478bd9Sstevel@tonic-gate/*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*9a70fc3bSMark J. Nelson * Common Development and Distribution License (the "License").
6*9a70fc3bSMark J. Nelson * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate/*
227c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
26*9a70fc3bSMark J. Nelson	.file	"memcmp.s"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate/
297c478bd9Sstevel@tonic-gate/ memcmp(s1, s2, n)
307c478bd9Sstevel@tonic-gate/
317c478bd9Sstevel@tonic-gate/ Compares n bytes:  s1>s2: >0  s1==s2:  0  s1<s2:  <0
327c478bd9Sstevel@tonic-gate/
337c478bd9Sstevel@tonic-gate/ Fast assembly language version of the following C-program strcat
347c478bd9Sstevel@tonic-gate/ which represents the `standard' for the C-library.
357c478bd9Sstevel@tonic-gate/
367c478bd9Sstevel@tonic-gate/	int
377c478bd9Sstevel@tonic-gate/	memcmp(const void *s1, const void *s2, size_t n)
387c478bd9Sstevel@tonic-gate/	{
397c478bd9Sstevel@tonic-gate/		if (s1 != s2 && n != 0) {
407c478bd9Sstevel@tonic-gate/			const unsigned char	*ps1 = s1;
417c478bd9Sstevel@tonic-gate/			const unsigned char	*ps2 = s2;
427c478bd9Sstevel@tonic-gate/
437c478bd9Sstevel@tonic-gate/			do {
447c478bd9Sstevel@tonic-gate/				if (*ps1++ != *ps2++)
457c478bd9Sstevel@tonic-gate/					return (ps1[-1] - ps2[-1]);
467c478bd9Sstevel@tonic-gate/			} while (--n != 0);
477c478bd9Sstevel@tonic-gate/		}
487c478bd9Sstevel@tonic-gate/		return (NULL);
497c478bd9Sstevel@tonic-gate/	}
507c478bd9Sstevel@tonic-gate/
517c478bd9Sstevel@tonic-gate/ This implementation conforms to SVID but does not implement
527c478bd9Sstevel@tonic-gate/ the same algorithm as the portable version because it is
537c478bd9Sstevel@tonic-gate/ inconvenient to get the difference of the differing characters.
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h>
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(memcmp,function)
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate#include "SYS.h"
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate	ENTRY(memcmp)
627c478bd9Sstevel@tonic-gate	pushl	%edi		/ save register variable
637c478bd9Sstevel@tonic-gate	movl	8(%esp), %eax	/ %eax = address of string 1
647c478bd9Sstevel@tonic-gate	movl	12(%esp), %ecx	/ %ecx = address of string 2
657c478bd9Sstevel@tonic-gate	cmpl	%eax, %ecx	/ if the same string
667c478bd9Sstevel@tonic-gate	je	.equal		/ goto .equal
677c478bd9Sstevel@tonic-gate	movl	16(%esp), %edi	/ %edi = length in bytes
687c478bd9Sstevel@tonic-gate	cmpl	$4, %edi	/ if %edi < 4
697c478bd9Sstevel@tonic-gate	jb	.byte_check	/ goto .byte_check
707c478bd9Sstevel@tonic-gate	.align	4
717c478bd9Sstevel@tonic-gate.word_loop:
727c478bd9Sstevel@tonic-gate	movl	(%ecx), %edx	/ move 1 word from (%ecx) to %edx
737c478bd9Sstevel@tonic-gate	leal	-4(%edi), %edi	/ %edi -= 4
747c478bd9Sstevel@tonic-gate	cmpl	(%eax), %edx	/ compare 1 word from (%eax) with %edx
757c478bd9Sstevel@tonic-gate	jne	.word_not_equal	/ if not equal, goto .word_not_equal
767c478bd9Sstevel@tonic-gate	leal	4(%ecx), %ecx	/ %ecx += 4 (next word)
777c478bd9Sstevel@tonic-gate	leal	4(%eax), %eax	/ %eax += 4 (next word)
787c478bd9Sstevel@tonic-gate	cmpl	$4, %edi	/ if %edi >= 4
797c478bd9Sstevel@tonic-gate	jae	.word_loop	/ goto .word_loop
807c478bd9Sstevel@tonic-gate.byte_check:
817c478bd9Sstevel@tonic-gate	cmpl	$0, %edi	/ if %edi == 0
827c478bd9Sstevel@tonic-gate	je	.equal		/ goto .equal
837c478bd9Sstevel@tonic-gate	jmp	.byte_loop	/ goto .byte_loop (checks in bytes)
847c478bd9Sstevel@tonic-gate.word_not_equal:
857c478bd9Sstevel@tonic-gate	leal	4(%edi), %edi	/ %edi += 4 (post-decremented)
867c478bd9Sstevel@tonic-gate	.align	4
877c478bd9Sstevel@tonic-gate.byte_loop:
887c478bd9Sstevel@tonic-gate	movb	(%ecx),	%dl	/ move 1 byte from (%ecx) to %dl
897c478bd9Sstevel@tonic-gate	cmpb	%dl, (%eax)	/ compare %dl with 1 byte from (%eax)
907c478bd9Sstevel@tonic-gate	jne	.not_equal	/ if not equal, goto .not_equal
917c478bd9Sstevel@tonic-gate	incl	%ecx		/ %ecx++ (next byte)
927c478bd9Sstevel@tonic-gate	incl	%eax		/ %eax++ (next byte)
937c478bd9Sstevel@tonic-gate	decl	%edi		/ %edi--
947c478bd9Sstevel@tonic-gate	jnz	.byte_loop	/ if not zero, goto .byte_loop
957c478bd9Sstevel@tonic-gate.equal:
967c478bd9Sstevel@tonic-gate	xorl	%eax, %eax	/ %eax = 0
977c478bd9Sstevel@tonic-gate	popl	%edi		/ restore register variable
987c478bd9Sstevel@tonic-gate	ret			/ return (NULL)
997c478bd9Sstevel@tonic-gate	.align	4
1007c478bd9Sstevel@tonic-gate.not_equal:
1017c478bd9Sstevel@tonic-gate	sbbl	%eax, %eax	/ %eax = 0 if no carry, %eax = -1 if carry
1027c478bd9Sstevel@tonic-gate	orl	$1, %eax	/ %eax = 1 if no carry, %eax = -1 if carry
1037c478bd9Sstevel@tonic-gate	popl	%edi		/ restore register variable
1047c478bd9Sstevel@tonic-gate	ret			/ return (NULL)
1057c478bd9Sstevel@tonic-gate	SET_SIZE(memcmp)
106