2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A .file "memset.s"
2N/A
2N/A/*
2N/A * memset(sp, c, n)
2N/A *
2N/A * Set an array of n chars starting at sp to the character c.
2N/A * Return sp.
2N/A *
2N/A * Fast assembler language version of the following C-program for memset
2N/A * which represents the `standard' for the C-library.
2N/A *
2N/A * void *
2N/A * memset(void *sp1, int c, size_t n)
2N/A * {
2N/A * if (n != 0) {
2N/A * char *sp = sp1;
2N/A * do {
2N/A * *sp++ = (char)c;
2N/A * } while (--n != 0);
2N/A * }
2N/A * return (sp1);
2N/A * }
2N/A *
2N/A *
2N/A *
2N/A * Algorithm used:
2N/A * For small stores (6 or fewer bytes), bytes will be stored one at a time.
2N/A *
2N/A * When setting 15 or more bytes, there will be at least 8 bytes aligned
2N/A * on an 8-byte boundary. So, leading bytes will be set, then as many
2N/A * 8-byte aligned chunks as possible will be set, followed by any trailing
2N/A * bytes.
2N/A *
2N/A * For between 8 and 14 bytes (inclusive), leading odd bytes will be
2N/A * set, followed by 4-byte chunks, followed by trailing bytes.
2N/A *
2N/A * Inputs:
2N/A * o0: pointer to start of area to be set to a given value
2N/A * o1: character used to set memory at location in i0
2N/A * o2: number of bytes to be set
2N/A *
2N/A * Outputs:
2N/A * o0: pointer to start of area set (same as input value in o0)
2N/A *
2N/A */
2N/A
2N/A#include <sys/asm_linkage.h>
2N/A
2N/A ANSI_PRAGMA_WEAK(memset,function)
2N/A
2N/A ENTRY(memset)
2N/A mov %o0, %o5 ! need to return this value
2N/A cmp %o2, 7
2N/A blu,pn %xcc, .wrchar ! small count: just set bytes
2N/A and %o1, 0xff, %o1
2N/A
2N/A sll %o1, 8, %o4 ! generate 4 bytes filled with char
2N/A or %o1, %o4, %o1
2N/A sll %o1, 16, %o4
2N/A cmp %o2, 15
2N/A blu,pn %xcc, .walign ! not enough to guarantee 8-byte align
2N/A or %o1, %o4, %o1
2N/A
2N/A sllx %o1, 32, %o4 ! now fill the other 4 bytes with char
2N/A or %o1, %o4, %o1
2N/A
2N/A.dalign: ! Set bytes until 8-byte aligned
2N/A btst 7, %o5 ! 8-byte aligned?
2N/A bz,a,pn %icc, .wrdbl
2N/A andn %o2, 7, %o3 ! o3 has 8-byte multiple
2N/A
2N/A dec %o2
2N/A stb %o1, [%o5] ! clear a byte
2N/A b .dalign ! go see if aligned yet
2N/A inc %o5
2N/A
2N/A .align 32
2N/A.wrdbl:
2N/A stx %o1, [%o5] ! write aligned 8 bytes
2N/A subcc %o3, 8, %o3
2N/A bnz,pt %xcc, .wrdbl
2N/A inc 8, %o5
2N/A
2N/A b .wrchar ! write the remaining bytes
2N/A and %o2, 7, %o2 ! leftover count, if any
2N/A
2N/A.walign: ! Set bytes until 4-byte aligned
2N/A btst 3, %o5 ! if bigger, align to 4 bytes
2N/A bz,pn %icc, .wrword
2N/A andn %o2, 3, %o3 ! create word sized count in %o3
2N/A
2N/A dec %o2 ! decrement count
2N/A stb %o1, [%o5] ! clear a byte
2N/A b .walign
2N/A inc %o5 ! next byte
2N/A
2N/A.wrword:
2N/A st %o1, [%o5] ! 4-byte writing loop
2N/A subcc %o3, 4, %o3
2N/A bnz,pn %xcc, .wrword
2N/A inc 4, %o5
2N/A
2N/A and %o2, 3, %o2 ! leftover count, if any
2N/A
2N/A.wrchar:
2N/A deccc %o2 ! byte clearing loop
2N/A inc %o5
2N/A bgeu,a,pt %xcc, .wrchar
2N/A stb %o1, [%o5 + -1] ! we've already incremented the address
2N/A
2N/A retl
2N/A sub %o0, %g0, %o0
2N/A
2N/A SET_SIZE(memset)