Home | History | Annotate | Line # | Download | only in string
memset2.c revision 1.6
      1  1.2    matt /*-
      2  1.2    matt  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      3  1.2    matt  * All rights reserved.
      4  1.2    matt  *
      5  1.2    matt  * This code is derived from software contributed to The NetBSD Foundation
      6  1.2    matt  * by Matt Thomas <matt (at) 3am-software.com>.
      7  1.2    matt  *
      8  1.2    matt  * Redistribution and use in source and binary forms, with or without
      9  1.2    matt  * modification, are permitted provided that the following conditions
     10  1.2    matt  * are met:
     11  1.2    matt  * 1. Redistributions of source code must retain the above copyright
     12  1.2    matt  *    notice, this list of conditions and the following disclaimer.
     13  1.2    matt  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.2    matt  *    notice, this list of conditions and the following disclaimer in the
     15  1.2    matt  *    documentation and/or other materials provided with the distribution.
     16  1.2    matt  *
     17  1.2    matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.2    matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.2    matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.2    matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.2    matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.2    matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.2    matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.2    matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.2    matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.2    matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.2    matt  * POSSIBILITY OF SUCH DAMAGE.
     28  1.2    matt  */
     29  1.2    matt 
     30  1.4     apb #include <sys/cdefs.h>
     31  1.4     apb #if defined(LIBC_SCCS) && !defined(lint)
     32  1.6  simonb __RCSID("$NetBSD: memset2.c,v 1.6 2021/04/17 05:57:11 simonb Exp $");
     33  1.4     apb #endif /* LIBC_SCCS and not lint */
     34  1.4     apb 
     35  1.2    matt #include <sys/types.h>
     36  1.2    matt 
     37  1.2    matt #if !defined(_KERNEL) && !defined(_STANDALONE)
     38  1.2    matt #include <assert.h>
     39  1.2    matt #include <limits.h>
     40  1.2    matt #include <string.h>
     41  1.2    matt #include <inttypes.h>
     42  1.2    matt #else
     43  1.2    matt #include <lib/libkern/libkern.h>
     44  1.2    matt #include <machine/limits.h>
     45  1.2    matt #endif
     46  1.2    matt 
     47  1.2    matt #include <sys/endian.h>
     48  1.2    matt #include <machine/types.h>
     49  1.2    matt 
     50  1.6  simonb #define __OPTIMIZE_SIZE__	/* other code path is very broken */
     51  1.6  simonb 
     52  1.2    matt #ifdef TEST
     53  1.2    matt #include <assert.h>
     54  1.2    matt #define _DIAGASSERT(a)		assert(a)
     55  1.2    matt #endif
     56  1.2    matt 
     57  1.2    matt #ifdef _FORTIFY_SOURCE
     58  1.2    matt #undef bzero
     59  1.3   joerg #endif
     60  1.2    matt #undef memset
     61  1.2    matt 
     62  1.2    matt /*
     63  1.2    matt  * Assume uregister_t is the widest non-synthetic unsigned type.
     64  1.2    matt  */
     65  1.2    matt typedef uregister_t memword_t;
     66  1.2    matt 
     67  1.4     apb __CTASSERT((~(memword_t)0U >> 1) != ~(memword_t)0U);
     68  1.4     apb 
     69  1.2    matt #ifdef BZERO
     70  1.2    matt static inline
     71  1.2    matt #define	memset memset0
     72  1.2    matt #endif
     73  1.2    matt 
     74  1.2    matt #ifdef TEST
     75  1.2    matt static
     76  1.2    matt #define memset test_memset
     77  1.2    matt #endif
     78  1.2    matt 
     79  1.2    matt void *
     80  1.2    matt memset(void *addr, int c, size_t len)
     81  1.2    matt {
     82  1.2    matt 	memword_t *dstp = addr;
     83  1.2    matt 	memword_t *edstp;
     84  1.2    matt 	memword_t fill;
     85  1.2    matt #ifndef __OPTIMIZE_SIZE__
     86  1.2    matt 	memword_t keep_mask = 0;
     87  1.2    matt #endif
     88  1.2    matt 	size_t fill_count;
     89  1.2    matt 
     90  1.2    matt 	_DIAGASSERT(addr != 0);
     91  1.2    matt 
     92  1.2    matt 	if (__predict_false(len == 0))
     93  1.2    matt 		return addr;
     94  1.2    matt 
     95  1.2    matt 	/*
     96  1.2    matt 	 * Pad out the fill byte (v) across a memword_t.
     97  1.2    matt 	 * The conditional at the end prevents GCC from complaing about
     98  1.2    matt 	 * shift count >= width of type
     99  1.2    matt 	 */
    100  1.2    matt 	fill = c;
    101  1.2    matt 	fill |= fill << 8;
    102  1.2    matt 	fill |= fill << 16;
    103  1.2    matt 	fill |= fill << (sizeof(c) < sizeof(fill) ? 32 : 0);
    104  1.2    matt 
    105  1.2    matt 	/*
    106  1.2    matt 	 * Get the number of unaligned bytes to fill in the first word.
    107  1.2    matt 	 */
    108  1.2    matt 	fill_count = -(uintptr_t)addr & (sizeof(memword_t) - 1);
    109  1.2    matt 
    110  1.2    matt 	if (__predict_false(fill_count != 0)) {
    111  1.2    matt #ifndef __OPTIMIZE_SIZE__
    112  1.2    matt 		/*
    113  1.2    matt 		 * We want to clear <fill_count> trailing bytes in the word.
    114  1.2    matt 		 * On big/little endian, these are the least/most significant,
    115  1.2    matt 		 * bits respectively.  So as we shift, the keep_mask will only
    116  1.2    matt 		 * have bits set for the bytes we won't be filling.
    117  1.2    matt 		 */
    118  1.2    matt #if BYTE_ORDER == BIG_ENDIAN
    119  1.2    matt 		keep_mask = ~(memword_t)0U << (fill_count * 8);
    120  1.2    matt #endif
    121  1.2    matt #if BYTE_ORDER == LITTLE_ENDIAN
    122  1.2    matt 		keep_mask = ~(memword_t)0U >> (fill_count * 8);
    123  1.2    matt #endif
    124  1.2    matt 		/*
    125  1.2    matt 		 * Make sure dstp is aligned to a memword_t boundary.
    126  1.2    matt 		 */
    127  1.2    matt 		dstp = (memword_t *)((uintptr_t)addr & -sizeof(memword_t));
    128  1.2    matt 		if (len >= fill_count) {
    129  1.2    matt 			/*
    130  1.2    matt 			 * If we can fill the rest of this word, then we mask
    131  1.2    matt 			 * off the bytes we are filling and then fill in those
    132  1.2    matt 			 * bytes with the new fill value.
    133  1.2    matt 			 */
    134  1.2    matt 			*dstp = (*dstp & keep_mask) | (fill & ~keep_mask);
    135  1.2    matt 			len -= fill_count;
    136  1.2    matt 			if (__predict_false(len == 0))
    137  1.2    matt 				return addr;
    138  1.2    matt 			/*
    139  1.2    matt 			 * Since we were able to fill the rest of this word,
    140  1.2    matt 			 * we will advance to the next word and thus have no
    141  1.2    matt 			 * bytes to preserve.
    142  1.2    matt 			 *
    143  1.2    matt 			 * If we don't have enough to fill the rest of this
    144  1.2    matt 			 * word, we will fall through the following loop
    145  1.2    matt 			 * (since there are no full words to fill).  Then we
    146  1.2    matt 			 * use the keep_mask above to preserve the leading
    147  1.2    matt 			 * bytes of word.
    148  1.2    matt 			 */
    149  1.2    matt 			dstp++;
    150  1.2    matt 			keep_mask = 0;
    151  1.2    matt 		} else {
    152  1.2    matt 			len += (uintptr_t)addr & (sizeof(memword_t) - 1);
    153  1.2    matt 		}
    154  1.2    matt #else /* __OPTIMIZE_SIZE__ */
    155  1.2    matt 		uint8_t *dp, *ep;
    156  1.2    matt 		if (len < fill_count)
    157  1.2    matt 			fill_count = len;
    158  1.2    matt 		for (dp = (uint8_t *)dstp, ep = dp + fill_count;
    159  1.2    matt 		     dp != ep; dp++)
    160  1.2    matt 			*dp = fill;
    161  1.2    matt 		if ((len -= fill_count) == 0)
    162  1.2    matt 			return addr;
    163  1.2    matt 		dstp = (memword_t *)ep;
    164  1.2    matt #endif /* __OPTIMIZE_SIZE__ */
    165  1.2    matt 	}
    166  1.2    matt 
    167  1.2    matt 	/*
    168  1.2    matt 	 * Simply fill memory one word at time (for as many full words we have
    169  1.2    matt 	 * to write).
    170  1.2    matt 	 */
    171  1.2    matt 	for (edstp = dstp + len / sizeof(memword_t); dstp != edstp; dstp++)
    172  1.2    matt 		*dstp = fill;
    173  1.2    matt 
    174  1.2    matt 	/*
    175  1.2    matt 	 * We didn't subtract out the full words we just filled since we know
    176  1.2    matt 	 * by the time we get here we will have less than a words worth to
    177  1.2    matt 	 * write.  So we can concern ourselves with only the subword len bits.
    178  1.2    matt 	 */
    179  1.2    matt 	len &= sizeof(memword_t)-1;
    180  1.2    matt 	if (len > 0) {
    181  1.2    matt #ifndef __OPTIMIZE_SIZE__
    182  1.2    matt 		/*
    183  1.2    matt 		 * We want to clear <len> leading bytes in the word.
    184  1.2    matt 		 * On big/little endian, these are the most/least significant
    185  1.2    matt 		 * bits, respectively,  But as we want the mask of the bytes to
    186  1.2    matt 		 * keep, we have to complement the mask.  So after we shift,
    187  1.2    matt 		 * the keep_mask will only have bits set for the bytes we won't
    188  1.2    matt 		 * be filling.
    189  1.2    matt 		 *
    190  1.2    matt 		 * But the keep_mask could already have bytes to preserve
    191  1.2    matt 		 * if the amount to fill was less than the amount of traiing
    192  1.2    matt 		 * space in the first word.
    193  1.2    matt 		 */
    194  1.2    matt #if BYTE_ORDER == BIG_ENDIAN
    195  1.2    matt 		keep_mask |= ~(memword_t)0U >> (len * 8);
    196  1.2    matt #endif
    197  1.2    matt #if BYTE_ORDER == LITTLE_ENDIAN
    198  1.2    matt 		keep_mask |= ~(memword_t)0U << (len * 8);
    199  1.2    matt #endif
    200  1.2    matt 		/*
    201  1.2    matt 		 * Now we mask off the bytes we are filling and then fill in
    202  1.2    matt 		 * those bytes with the new fill value.
    203  1.2    matt 		 */
    204  1.2    matt 		*dstp = (*dstp & keep_mask) | (fill & ~keep_mask);
    205  1.2    matt #else /* __OPTIMIZE_SIZE__ */
    206  1.2    matt 		uint8_t *dp, *ep;
    207  1.2    matt 		for (dp = (uint8_t *)dstp, ep = dp + len;
    208  1.2    matt 		     dp != ep; dp++)
    209  1.2    matt 			*dp = fill;
    210  1.2    matt #endif /* __OPTIMIZE_SIZE__ */
    211  1.2    matt 	}
    212  1.2    matt 
    213  1.2    matt 	/*
    214  1.2    matt 	 * Return the initial addr
    215  1.2    matt 	 */
    216  1.2    matt 	return addr;
    217  1.2    matt }
    218  1.2    matt 
    219  1.2    matt #ifdef BZERO
    220  1.2    matt /*
    221  1.2    matt  * For bzero, simply inline memset and let the compiler optimize things away.
    222  1.2    matt  */
    223  1.2    matt void
    224  1.2    matt bzero(void *addr, size_t len)
    225  1.2    matt {
    226  1.2    matt 	memset(addr, 0, len);
    227  1.2    matt }
    228  1.2    matt #endif
    229  1.2    matt 
    230  1.2    matt #ifdef TEST
    231  1.2    matt #include <stdbool.h>
    232  1.2    matt #include <stdio.h>
    233  1.2    matt 
    234  1.2    matt #undef memset
    235  1.2    matt 
    236  1.2    matt static union {
    237  1.2    matt 	uint8_t bytes[sizeof(memword_t) * 4];
    238  1.2    matt 	memword_t words[4];
    239  1.2    matt } testmem;
    240  1.2    matt 
    241  1.2    matt int
    242  1.2    matt main(int argc, char **argv)
    243  1.2    matt {
    244  1.2    matt 	size_t start;
    245  1.2    matt 	size_t len;
    246  1.2    matt 	bool failed = false;
    247  1.2    matt 
    248  1.2    matt 	for (start = 1; start < sizeof(testmem) - 1; start++) {
    249  1.2    matt 		for (len = 1; start + len < sizeof(testmem) - 1; len++) {
    250  1.2    matt 			bool ok = true;
    251  1.2    matt 			size_t i;
    252  1.2    matt 			uint8_t check_value;
    253  1.2    matt 			memset(testmem.bytes, 0xff, sizeof(testmem));
    254  1.2    matt 			test_memset(testmem.bytes + start, 0x00, len);
    255  1.2    matt 			for (i = 0; i < sizeof(testmem); i++) {
    256  1.2    matt 				if (i == 0 || i == start + len)
    257  1.2    matt 					check_value = 0xff;
    258  1.2    matt 				else if (i == start)
    259  1.2    matt 					check_value = 0x00;
    260  1.2    matt 				if (testmem.bytes[i] != check_value) {
    261  1.2    matt 					if (ok)
    262  1.2    matt 						printf("pass @ %zu .. %zu failed",
    263  1.2    matt 						    start, start + len - 1);
    264  1.2    matt 					ok = false;
    265  1.2    matt 					printf(" [%zu]=0x%02x(!0x%02x)",
    266  1.2    matt 					    i, testmem.bytes[i], check_value);
    267  1.2    matt 				}
    268  1.2    matt 			}
    269  1.2    matt 			if (!ok) {
    270  1.2    matt 				printf("\n");
    271  1.2    matt 				failed = 1;
    272  1.2    matt 			}
    273  1.2    matt 		}
    274  1.2    matt 	}
    275  1.2    matt 
    276  1.2    matt 	return failed ? 1 : 0;
    277  1.2    matt }
    278  1.2    matt #endif /* TEST */
    279