Blame sysdeps/generic/memcopy.h

Packit Service 82fcde
/* memcopy.h -- definitions for memory copy functions.  Generic C version.
Packit Service 82fcde
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
   Contributed by Torbjorn Granlund (tege@sics.se).
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#ifndef _MEMCOPY_H
Packit Service 82fcde
#define _MEMCOPY_H	1
Packit Service 82fcde
Packit Service 82fcde
/* The strategy of the memory functions is:
Packit Service 82fcde
Packit Service 82fcde
     1. Copy bytes until the destination pointer is aligned.
Packit Service 82fcde
Packit Service 82fcde
     2. Copy words in unrolled loops.  If the source and destination
Packit Service 82fcde
     are not aligned in the same way, use word memory operations,
Packit Service 82fcde
     but shift and merge two read words before writing.
Packit Service 82fcde
Packit Service 82fcde
     3. Copy the few remaining bytes.
Packit Service 82fcde
Packit Service 82fcde
   This is fast on processors that have at least 10 registers for
Packit Service 82fcde
   allocation by GCC, and that can access memory at reg+const in one
Packit Service 82fcde
   instruction.
Packit Service 82fcde
Packit Service 82fcde
   I made an "exhaustive" test of this memmove when I wrote it,
Packit Service 82fcde
   exhaustive in the sense that I tried all alignment and length
Packit Service 82fcde
   combinations, with and without overlap.  */
Packit Service 82fcde
Packit Service 82fcde
#include <sys/cdefs.h>
Packit Service 82fcde
#include <endian.h>
Packit Service 82fcde
#include <pagecopy.h>
Packit Service 82fcde
Packit Service 82fcde
/* The macros defined in this file are:
Packit Service 82fcde
Packit Service 82fcde
   BYTE_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_to_copy)
Packit Service 82fcde
Packit Service 82fcde
   BYTE_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_to_copy)
Packit Service 82fcde
Packit Service 82fcde
   WORD_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_remaining, nbytes_to_copy)
Packit Service 82fcde
Packit Service 82fcde
   WORD_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_remaining, nbytes_to_copy)
Packit Service 82fcde
Packit Service 82fcde
   MERGE(old_word, sh_1, new_word, sh_2)
Packit Service 82fcde
     [I fail to understand.  I feel stupid.  --roland]
Packit Service 82fcde
*/
Packit Service 82fcde
Packit Service 82fcde
/* Type to use for aligned memory operations.
Packit Service 82fcde
   This should normally be the biggest type supported by a single load
Packit Service 82fcde
   and store.  */
Packit Service 82fcde
#define	op_t	unsigned long int
Packit Service 82fcde
#define OPSIZ	(sizeof(op_t))
Packit Service 82fcde
Packit Service 82fcde
/* Type to use for unaligned operations.  */
Packit Service 82fcde
typedef unsigned char byte;
Packit Service 82fcde
Packit Service 82fcde
#if __BYTE_ORDER == __LITTLE_ENDIAN
Packit Service 82fcde
#define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
Packit Service 82fcde
#endif
Packit Service 82fcde
#if __BYTE_ORDER == __BIG_ENDIAN
Packit Service 82fcde
#define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
/* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
Packit Service 82fcde
   without any assumptions about alignment of the pointers.  */
Packit Service 82fcde
#define BYTE_COPY_FWD(dst_bp, src_bp, nbytes)				      \
Packit Service 82fcde
  do									      \
Packit Service 82fcde
    {									      \
Packit Service 82fcde
      size_t __nbytes = (nbytes);					      \
Packit Service 82fcde
      while (__nbytes > 0)						      \
Packit Service 82fcde
	{								      \
Packit Service 82fcde
	  byte __x = ((byte *) src_bp)[0];				      \
Packit Service 82fcde
	  src_bp += 1;							      \
Packit Service 82fcde
	  __nbytes -= 1;						      \
Packit Service 82fcde
	  ((byte *) dst_bp)[0] = __x;					      \
Packit Service 82fcde
	  dst_bp += 1;							      \
Packit Service 82fcde
	}								      \
Packit Service 82fcde
    } while (0)
Packit Service 82fcde
Packit Service 82fcde
/* Copy exactly NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
Packit Service 82fcde
   beginning at the bytes right before the pointers and continuing towards
Packit Service 82fcde
   smaller addresses.  Don't assume anything about alignment of the
Packit Service 82fcde
   pointers.  */
Packit Service 82fcde
#define BYTE_COPY_BWD(dst_ep, src_ep, nbytes)				      \
Packit Service 82fcde
  do									      \
Packit Service 82fcde
    {									      \
Packit Service 82fcde
      size_t __nbytes = (nbytes);					      \
Packit Service 82fcde
      while (__nbytes > 0)						      \
Packit Service 82fcde
	{								      \
Packit Service 82fcde
	  byte __x;							      \
Packit Service 82fcde
	  src_ep -= 1;							      \
Packit Service 82fcde
	  __x = ((byte *) src_ep)[0];					      \
Packit Service 82fcde
	  dst_ep -= 1;							      \
Packit Service 82fcde
	  __nbytes -= 1;						      \
Packit Service 82fcde
	  ((byte *) dst_ep)[0] = __x;					      \
Packit Service 82fcde
	}								      \
Packit Service 82fcde
    } while (0)
Packit Service 82fcde
Packit Service 82fcde
/* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
Packit Service 82fcde
   the assumption that DST_BP is aligned on an OPSIZ multiple.  If
Packit Service 82fcde
   not all bytes could be easily copied, store remaining number of bytes
Packit Service 82fcde
   in NBYTES_LEFT, otherwise store 0.  */
Packit Service 82fcde
extern void _wordcopy_fwd_aligned (long int, long int, size_t)
Packit Service 82fcde
  attribute_hidden __THROW;
Packit Service 82fcde
extern void _wordcopy_fwd_dest_aligned (long int, long int, size_t)
Packit Service 82fcde
  attribute_hidden __THROW;
Packit Service 82fcde
#define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes)		      \
Packit Service 82fcde
  do									      \
Packit Service 82fcde
    {									      \
Packit Service 82fcde
      if (src_bp % OPSIZ == 0)						      \
Packit Service 82fcde
	_wordcopy_fwd_aligned (dst_bp, src_bp, (nbytes) / OPSIZ);	      \
Packit Service 82fcde
      else								      \
Packit Service 82fcde
	_wordcopy_fwd_dest_aligned (dst_bp, src_bp, (nbytes) / OPSIZ);	      \
Packit Service 82fcde
      src_bp += (nbytes) & -OPSIZ;					      \
Packit Service 82fcde
      dst_bp += (nbytes) & -OPSIZ;					      \
Packit Service 82fcde
      (nbytes_left) = (nbytes) % OPSIZ;					      \
Packit Service 82fcde
    } while (0)
Packit Service 82fcde
Packit Service 82fcde
/* Copy *up to* NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
Packit Service 82fcde
   beginning at the words (of type op_t) right before the pointers and
Packit Service 82fcde
   continuing towards smaller addresses.  May take advantage of that
Packit Service 82fcde
   DST_END_PTR is aligned on an OPSIZ multiple.  If not all bytes could be
Packit Service 82fcde
   easily copied, store remaining number of bytes in NBYTES_REMAINING,
Packit Service 82fcde
   otherwise store 0.  */
Packit Service 82fcde
extern void _wordcopy_bwd_aligned (long int, long int, size_t)
Packit Service 82fcde
  attribute_hidden __THROW;
Packit Service 82fcde
extern void _wordcopy_bwd_dest_aligned (long int, long int, size_t)
Packit Service 82fcde
  attribute_hidden __THROW;
Packit Service 82fcde
#define WORD_COPY_BWD(dst_ep, src_ep, nbytes_left, nbytes)		      \
Packit Service 82fcde
  do									      \
Packit Service 82fcde
    {									      \
Packit Service 82fcde
      if (src_ep % OPSIZ == 0)						      \
Packit Service 82fcde
	_wordcopy_bwd_aligned (dst_ep, src_ep, (nbytes) / OPSIZ);	      \
Packit Service 82fcde
      else								      \
Packit Service 82fcde
	_wordcopy_bwd_dest_aligned (dst_ep, src_ep, (nbytes) / OPSIZ);	      \
Packit Service 82fcde
      src_ep -= (nbytes) & -OPSIZ;					      \
Packit Service 82fcde
      dst_ep -= (nbytes) & -OPSIZ;					      \
Packit Service 82fcde
      (nbytes_left) = (nbytes) % OPSIZ;					      \
Packit Service 82fcde
    } while (0)
Packit Service 82fcde
Packit Service 82fcde
/* The macro PAGE_COPY_FWD_MAYBE (dstp, srcp, nbytes_left, nbytes) is invoked
Packit Service 82fcde
   like WORD_COPY_FWD et al.  The pointers should be at least word aligned.
Packit Service 82fcde
   This will check if virtual copying by pages can and should be done and do it
Packit Service 82fcde
   if so.  The pointers will be aligned to PAGE_SIZE bytes.  The macro requires
Packit Service 82fcde
   that pagecopy.h defines at least PAGE_COPY_THRESHOLD to 0.  If
Packit Service 82fcde
   PAGE_COPY_THRESHOLD is non-zero, the header must also define PAGE_COPY_FWD
Packit Service 82fcde
   and PAGE_SIZE.
Packit Service 82fcde
*/
Packit Service 82fcde
#if PAGE_COPY_THRESHOLD
Packit Service 82fcde
Packit Service 82fcde
# include <assert.h>
Packit Service 82fcde
Packit Service 82fcde
# define PAGE_COPY_FWD_MAYBE(dstp, srcp, nbytes_left, nbytes)		      \
Packit Service 82fcde
  do									      \
Packit Service 82fcde
    {									      \
Packit Service 82fcde
      if ((nbytes) >= PAGE_COPY_THRESHOLD &&				      \
Packit Service 82fcde
	  PAGE_OFFSET ((dstp) - (srcp)) == 0) 				      \
Packit Service 82fcde
	{								      \
Packit Service 82fcde
	  /* The amount to copy is past the threshold for copying	      \
Packit Service 82fcde
	     pages virtually with kernel VM operations, and the		      \
Packit Service 82fcde
	     source and destination addresses have the same alignment.  */    \
Packit Service 82fcde
	  size_t nbytes_before = PAGE_OFFSET (-(dstp));			      \
Packit Service 82fcde
	  if (nbytes_before != 0)					      \
Packit Service 82fcde
	    {								      \
Packit Service 82fcde
	      /* First copy the words before the first page boundary.  */     \
Packit Service 82fcde
	      WORD_COPY_FWD (dstp, srcp, nbytes_left, nbytes_before);	      \
Packit Service 82fcde
	      assert (nbytes_left == 0);				      \
Packit Service 82fcde
	      nbytes -= nbytes_before;					      \
Packit Service 82fcde
	    }								      \
Packit Service 82fcde
	  PAGE_COPY_FWD (dstp, srcp, nbytes_left, nbytes);		      \
Packit Service 82fcde
	}								      \
Packit Service 82fcde
    } while (0)
Packit Service 82fcde
Packit Service 82fcde
/* The page size is always a power of two, so we can avoid modulo division.  */
Packit Service 82fcde
# define PAGE_OFFSET(n)	((n) & (PAGE_SIZE - 1))
Packit Service 82fcde
Packit Service 82fcde
#else
Packit Service 82fcde
Packit Service 82fcde
# define PAGE_COPY_FWD_MAYBE(dstp, srcp, nbytes_left, nbytes) /* nada */
Packit Service 82fcde
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
/* Threshold value for when to enter the unrolled loops.  */
Packit Service 82fcde
#define	OP_T_THRES	16
Packit Service 82fcde
Packit Service 82fcde
/* Set to 1 if memcpy is safe to use for forward-copying memmove with
Packit Service 82fcde
   overlapping addresses.  This is 0 by default because memcpy implementations
Packit Service 82fcde
   are generally not safe for overlapping addresses.  */
Packit Service 82fcde
#define MEMCPY_OK_FOR_FWD_MEMMOVE 0
Packit Service 82fcde
Packit Service 82fcde
#endif /* memcopy.h */