mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: Re: Optimized C memset [v2]
Date: Tue, 27 Aug 2013 12:22:06 -0400	[thread overview]
Message-ID: <20130827162205.GU20515@brightrain.aerifal.cx> (raw)
In-Reply-To: <20130827083020.GA4503@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 570 bytes --]

Here's version 2 (filename version 6, in honor of glibc ;) of the
memset code. I fixed a bug in the logic for coverage of the tail (the
part past what's covered by the loop) for some values of n and
alignments, and cleaned up the __GNUC__ usage a bit to use less
#ifdeffery. The remaining test at the top for the __GNUC__ version is
ugly, I admit, and should possibly just be removed and replaced by a
configure check to add -D__may_alias__= to the CFLAGS if the compiler
defines __GNUC__ but does not recognize __attribute__((__may_alias__))
-- opinions on this?

Rich

[-- Attachment #2: memset6.c --]
[-- Type: text/plain, Size: 1275 bytes --]

#include <string.h>
#include <stdint.h>

#if defined(__GNUC__) && 100*__GNUC__+__GNUC_MINOR__ < 302
#define __may_alias__
#endif

void *memset(void *dest, int c, size_t n)
{
	unsigned char *s = dest;
	size_t k;

	if (!n) return dest;
	s[0] = s[n-1] = c;
	if (n <= 2) return dest;
	s[1] = s[n-2] = c;
	s[2] = s[n-3] = c;
	if (n <= 6) return dest;
	s[3] = s[n-4] = c;
	if (n <= 8) return dest;

	k = -(uintptr_t)s & 3;
	s += k;
	n -= k;
	n &= -3;

#ifdef __GNUC__
	typedef uint32_t __attribute__((__may_alias__)) u32;
	typedef uint64_t __attribute__((__may_alias__)) u64;

	u32 c32 = ((u32)-1)/255 * (unsigned char)c;

	*(u32 *)(s+0) = c32;
	*(u32 *)(s+n-4) = c32;
	if (n <= 8) return dest;
	*(u32 *)(s+4) = c32;
	*(u32 *)(s+8) = c32;
	*(u32 *)(s+n-12) = c32;
	*(u32 *)(s+n-8) = c32;
	if (n <= 24) return dest;
	*(u32 *)(s+12) = c32;
	*(u32 *)(s+16) = c32;
	*(u32 *)(s+20) = c32;
	*(u32 *)(s+24) = c32;
	*(u32 *)(s+n-28) = c32;
	*(u32 *)(s+n-24) = c32;
	*(u32 *)(s+n-20) = c32;
	*(u32 *)(s+n-16) = c32;

	k = 24 + ((uintptr_t)s & 4);
	s += k;
	n -= k;

	u64 c64 = c32 | ((u64)c32 << 32);
	for (; n >= 32; n-=32, s+=32) {
		*(u64 *)(s+0) = c64;
		*(u64 *)(s+8) = c64;
		*(u64 *)(s+16) = c64;
		*(u64 *)(s+24) = c64;
	}
#else
	for (; n; n--, s++) *s = c;
#endif

	return dest;
}

  parent reply	other threads:[~2013-08-27 16:22 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-27  8:30 Optimized C memset Rich Felker
2013-08-27  8:52 ` Jens Gustedt
2013-08-27  9:17   ` Rich Felker
2013-08-27  9:50     ` Jens Gustedt
2013-08-27 14:21       ` Rich Felker
2013-08-27 14:34         ` Luca Barbato
2013-08-27 14:39           ` Rich Felker
2013-08-27 15:20         ` John Spencer
2013-08-27 15:34           ` Rich Felker
2013-08-27 16:22 ` Rich Felker [this message]
2013-08-27 17:28   ` Optimized C memset [v2] Jeremy Huntwork
2013-08-27 21:27     ` Rich Felker
2013-08-28  0:05   ` Andre Renaud
2013-08-28  1:24     ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130827162205.GU20515@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).