public inbox for libc-ports@sourceware.org
 help / color / mirror / Atom feed
From: "Ondřej Bílka" <neleai@seznam.cz>
To: marcus.shawcroft@linaro.org
Cc: libc-ports@sourceware.org
Subject: memset on aarch64
Date: Mon, 25 Mar 2013 15:33:00 -0000	[thread overview]
Message-ID: <20130325152822.GA23513@domone.kolej.mff.cuni.cz> (raw)

Hi Marcus,

could you try how following memset implementation works on 
aarch64.

It could be faster for small n, I found that on x64 overlapping stores
are best way how handle end conditions. I do not know how arm could handle that.

I do not know how good loop will gcc generate, if this is faster header
you can replace loop in assembly.

#include <stdint.h>
#include <stdlib.h>

/* Align VALUE down by ALIGN bytes.  */
#define ALIGN_DOWN(value, align) \
       ALIGN_DOWN_M1(value, align - 1)
/* Align VALUE down by ALIGN_M1 + 1 bytes.
   Useful if you have precomputed ALIGN - 1.  */
#define ALIGN_DOWN_M1(value, align_m1) \
       (void *)((uintptr_t)(value) \
                & ~(uintptr_t)(align_m1))

/* Align VALUE up by ALIGN bytes.  */
#define ALIGN_UP(value, align) \
       ALIGN_UP_M1(value, align - 1)
/* Align VALUE up by ALIGN_M1 + 1 bytes.
   Useful if you have precomputed ALIGN - 1.  */
#define ALIGN_UP_M1(value, align_m1) \
       (void *)(((uintptr_t)(value) + (uintptr_t)(align_m1)) \
                & ~(uintptr_t)(align_m1))

#define STOREU(x,y) STORE(x,y)
#define STORE(x,y) ((uint64_t*)(x))[0]=y; ((uint64_t*)(x))[1]=y;

static char *memset_small (char *dest, uint64_t c, size_t no, char *ret);
void *memset_new(char *dest, int _c, size_t n)
{
  int i;
  unsigned char c = _c;
  uint64_t vc = 0x0101010101010101ULL*c;
  if (n < 16)
    {
      return memset_small(dest, vc, n, dest);
    }
  else
    {
      STOREU(dest, vc);
      STOREU(dest + n - 16, vc);
      char *to   = ALIGN_DOWN(dest + n, 16);
      dest = ALIGN_DOWN(dest + 16, 16);
      while (dest != to)
        {
          STORE(dest,vc);
          dest += 16;
        }
    }
  return dest;
}

static char *memset_small (char *dest, uint64_t c, size_t no, char *ret)
{
  if (no & (8))
    {
      ((uint64_t *) dest)[0] = c;
      ((uint64_t *)(dest + no - 8))[0] = c;
      return ret;
    }
  if (no & 4)
    {
      ((uint32_t *) dest)[0] = c;
      ((uint32_t *)(dest + no - 4))[0] = c;
      return ret;
    }
  if (no & 1)
    {
      dest[0] = c;
    }
  if (no & 2)
    {
      ((uint16_t *)(dest + no - 2))[0] = c;
    }
  return ret;
}


                 reply	other threads:[~2013-03-25 15:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20130325152822.GA23513@domone.kolej.mff.cuni.cz \
    --to=neleai@seznam.cz \
    --cc=libc-ports@sourceware.org \
    --cc=marcus.shawcroft@linaro.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).