public inbox for libc-ports@sourceware.org
 help / color / mirror / Atom feed
* memset on aarch64
@ 2013-03-25 15:33 Ondřej Bílka
  0 siblings, 0 replies; only message in thread
From: Ondřej Bílka @ 2013-03-25 15:33 UTC (permalink / raw)
  To: marcus.shawcroft; +Cc: libc-ports

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;
}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2013-03-25 15:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-25 15:33 memset on aarch64 Ondřej Bílka

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).