xref: /onnv-gate/usr/src/lib/libc/sparc/gen/memset.s (revision 7298:b69e27387f74)
10Sstevel@tonic-gate/*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56515Sraf * Common Development and Distribution License (the "License").
66515Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216515Sraf
220Sstevel@tonic-gate/*
236515Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*7298SMark.J.Nelson@Sun.COM	.file	"memset.s"
280Sstevel@tonic-gate
290Sstevel@tonic-gate/*
300Sstevel@tonic-gate * char *memset(sp, c, n)
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * Set an array of n chars starting at sp to the character c.
330Sstevel@tonic-gate * Return sp.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * Fast assembler language version of the following C-program for memset
360Sstevel@tonic-gate * which represents the `standard' for the C-library.
370Sstevel@tonic-gate *
380Sstevel@tonic-gate *	void *
390Sstevel@tonic-gate *	memset(void *sp1, int c, size_t n)
400Sstevel@tonic-gate *	{
410Sstevel@tonic-gate *	    if (n != 0) {
420Sstevel@tonic-gate *		char *sp = sp1;
430Sstevel@tonic-gate *		do {
440Sstevel@tonic-gate *		    *sp++ = (char)c;
450Sstevel@tonic-gate *		} while (--n != 0);
460Sstevel@tonic-gate *	    }
470Sstevel@tonic-gate *	    return (sp1);
480Sstevel@tonic-gate *	}
490Sstevel@tonic-gate */
500Sstevel@tonic-gate
510Sstevel@tonic-gate#include <sys/asm_linkage.h>
520Sstevel@tonic-gate
530Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(memset,function)
540Sstevel@tonic-gate
550Sstevel@tonic-gate	ENTRY(memset)
560Sstevel@tonic-gate	mov	%o0, %o5		! copy sp before using it
570Sstevel@tonic-gate	cmp	%o2, 7			! if small counts, just write bytes
580Sstevel@tonic-gate	blu	.wrchar
590Sstevel@tonic-gate	.empty				! following lable is ok in delay slot
600Sstevel@tonic-gate
610Sstevel@tonic-gate.walign:btst	3, %o5			! if bigger, align to 4 bytes
620Sstevel@tonic-gate	bz	.wrword
630Sstevel@tonic-gate	andn	%o2, 3, %o3		! create word sized count in %o3
640Sstevel@tonic-gate	dec	%o2			! decrement count
650Sstevel@tonic-gate	stb	%o1, [%o5]		! clear a byte
660Sstevel@tonic-gate	b	.walign
670Sstevel@tonic-gate	inc	%o5			! next byte
680Sstevel@tonic-gate
690Sstevel@tonic-gate.wrword:and	%o1, 0xff, %o1		! generate a word filled with c
700Sstevel@tonic-gate	sll	%o1, 8, %o4
710Sstevel@tonic-gate	or 	%o1, %o4, %o1
720Sstevel@tonic-gate	sll	%o1, 16, %o4
730Sstevel@tonic-gate	or	%o1, %o4, %o1
740Sstevel@tonic-gate1:	st	%o1, [%o5]		! word writing loop
750Sstevel@tonic-gate	subcc	%o3, 4, %o3
760Sstevel@tonic-gate	bnz	1b
770Sstevel@tonic-gate	inc	4, %o5
780Sstevel@tonic-gate
790Sstevel@tonic-gate	and	%o2, 3, %o2		! leftover count, if any
800Sstevel@tonic-gate.wrchar:deccc	%o2			! byte clearing loop
810Sstevel@tonic-gate	inc	%o5
820Sstevel@tonic-gate	bgeu,a	.wrchar
830Sstevel@tonic-gate	stb	%o1, [%o5 + -1]		! we've already incremented the address
840Sstevel@tonic-gate
850Sstevel@tonic-gate	retl
860Sstevel@tonic-gate	nop
870Sstevel@tonic-gate
880Sstevel@tonic-gate	SET_SIZE(memset)
89