home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / clib / memmove.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-09  |  3.6 KB  |  249 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: memmove.c,v 1.2 1996/12/11 11:22:35 aros Exp $
  4.  
  5.     Desc: ANSI C function memmove()
  6.     Lang: english
  7. */
  8.  
  9. /*****************************************************************************
  10.  
  11.     NAME */
  12. #include <string.h>
  13.  
  14.     void * memmove (
  15.  
  16. /*  SYNOPSIS */
  17.     void *         dest,
  18.     const void * src,
  19.     size_t         count)
  20.  
  21. /*  FUNCTION
  22.     Copy the contents of a part of memory to another. Both areas
  23.     may overlap.
  24.  
  25.     INPUTS
  26.     dest - The first byte of the destination area in memory
  27.     src - The first byte of the source area in memory
  28.     count - How many bytes to copy
  29.  
  30.     RESULT
  31.     dest.
  32.  
  33.     NOTES
  34.  
  35.     EXAMPLE
  36.     See below.
  37.  
  38.     BUGS
  39.  
  40.     SEE ALSO
  41.     memcpy()
  42.  
  43.     INTERNALS
  44.  
  45.     HISTORY
  46.     24-12-95    digulla created
  47.  
  48. ******************************************************************************/
  49. {
  50.     char * d = (char *)dest,
  51.      * s = (char *)src;
  52.     size_t mis,
  53.        low,
  54.        high;
  55.  
  56.  
  57.     if (s < d)
  58.     {
  59.     d += count-1;
  60.     s += count-1;
  61.  
  62.     mis = sizeof (long) - ((long)s & (sizeof (long) - 1));
  63.  
  64.     if (mis > count)
  65.         mis = count;
  66.  
  67.     count -= mis;
  68.  
  69.     if (mis)
  70.     {
  71.         while (mis--)
  72.         *d-- = *s--;
  73.     }
  74.  
  75.     if (!((long)d & (sizeof (long) - 1)) )
  76.     {
  77.         long * dl = (long *)d;
  78.         long * sl = (long *)s;
  79.         size_t longs;
  80.  
  81.         longs = count / sizeof (long);
  82.  
  83.         low  = longs & 7;
  84.         high = longs >> 3;
  85.  
  86.         while (low--)
  87.         *dl-- = *sl--;
  88.  
  89.         while (high--)
  90.         {
  91.         *dl-- = *sl--;
  92.         *dl-- = *sl--;
  93.         *dl-- = *sl--;
  94.         *dl-- = *sl--;
  95.  
  96.         *dl-- = *sl--;
  97.         *dl-- = *sl--;
  98.         *dl-- = *sl--;
  99.         *dl-- = *sl--;
  100.         }
  101.  
  102.         count -= longs * sizeof (long);
  103.     }
  104.  
  105.     low  = count & 7;
  106.     high = count >> 3;
  107.  
  108.     while (low--)
  109.         *d-- = *s--;
  110.  
  111.     while (high--)
  112.     {
  113.         *d-- = *s--;
  114.         *d-- = *s--;
  115.         *d-- = *s--;
  116.         *d-- = *s--;
  117.  
  118.         *d-- = *s--;
  119.         *d-- = *s--;
  120.         *d-- = *s--;
  121.         *d-- = *s--;
  122.     }
  123.     }
  124.     else
  125.     {
  126.     mis = (long)s & (sizeof (long) - 1);
  127.  
  128.     if (mis > count)
  129.         mis = count;
  130.  
  131.     count -= mis;
  132.  
  133.     if (mis)
  134.     {
  135.         while (mis--)
  136.         *d++ = *s++;
  137.     }
  138.  
  139.     if (!((long)d & (sizeof (long) - 1)) )
  140.     {
  141.         long * dl = (long *)d;
  142.         long * sl = (long *)s;
  143.         size_t longs;
  144.  
  145.         longs = count / sizeof (long);
  146.  
  147.         low  = longs & 7;
  148.         high = longs >> 3;
  149.  
  150.         while (low--)
  151.         *dl++ = *sl++;
  152.  
  153.         while (high--)
  154.         {
  155.         *dl++ = *sl++;
  156.         *dl++ = *sl++;
  157.         *dl++ = *sl++;
  158.         *dl++ = *sl++;
  159.  
  160.         *dl++ = *sl++;
  161.         *dl++ = *sl++;
  162.         *dl++ = *sl++;
  163.         *dl++ = *sl++;
  164.         }
  165.  
  166.         count -= longs * sizeof (long);
  167.     }
  168.  
  169.     low  = count & 7;
  170.     high = count >> 3;
  171.  
  172.     while (low--)
  173.         *d++ = *s++;
  174.  
  175.     while (high--)
  176.     {
  177.         *d++ = *s++;
  178.         *d++ = *s++;
  179.         *d++ = *s++;
  180.         *d++ = *s++;
  181.  
  182.         *d++ = *s++;
  183.         *d++ = *s++;
  184.         *d++ = *s++;
  185.         *d++ = *s++;
  186.     }
  187.     }
  188.  
  189.     return dest;
  190. } /* memmove */
  191.  
  192. #ifdef TEST
  193. #include <stdio.h>
  194.  
  195. unsigned char src[64];
  196. unsigned char dst[64+8];
  197.  
  198. void showresult (void)
  199. {
  200.     int t;
  201.  
  202.     printf ("    %02x%02x%02x%02x,\n", dst[0], dst[1], dst[2], dst[2]);
  203.  
  204.     for (t=0; t<64; t++)
  205.     {
  206.     if ((t&15)==0)
  207.         printf ("    ");
  208.  
  209.     printf ("%02lx", dst[t+4]);
  210.  
  211.     if ((t&15)==15)
  212.         printf ("\n");
  213.     else if ((t&3)==3)
  214.         printf (" ");
  215.     }
  216.  
  217.     printf ("    %02x%02x%02x%02x\n", dst[68], dst[69], dst[70], dst[71]);
  218. }
  219.  
  220. int main (int argc, char ** argv)
  221. {
  222.     char * s = src;
  223.     char * d = &dst[4];
  224.     int t;
  225.  
  226.     for (t=0; t<64; t++)
  227.     src[t] = t+1;
  228.  
  229.     printf ("Initial state:\n");
  230.     showresult ();
  231.  
  232.     printf ("Full copy:\n");
  233.     memmove (d, s, 64);
  234.     showresult ();
  235.  
  236.     printf ("Shift down:\n");
  237.     memmove (d, s, 64);
  238.     memmove (d, d+1, 63);
  239.     showresult ();
  240.  
  241.     printf ("Shift up:\n");
  242.     memmove (d, s, 64);
  243.     memmove (d+1, d, 63);
  244.     showresult ();
  245.  
  246. } /* main */
  247.  
  248. #endif /* TEST */
  249.