home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / backup / star-1.3.1.tar.gz / star-1.3.1.tar / star-1.3.1 / lib / movebytes.c < prev    next >
C/C++ Source or Header  |  2000-05-07  |  3KB  |  125 lines

  1. /* @(#)movebytes.c    1.12 00/05/07 Copyright 1985 J. Schilling */
  2. /*
  3.  *    move data
  4.  *
  5.  *    Copyright (c) 1985 J. Schilling
  6.  */
  7. /*
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2, or (at your option)
  11.  * any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; see the file COPYING.  If not, write to
  20.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. #include <standard.h>
  24. #include <align.h>
  25. #include <schily.h>
  26.  
  27. #define    DO8(a)    a;a;a;a;a;a;a;a;
  28.  
  29. char *movebytes(fromv, tov, cnt)
  30.     const void    *fromv;
  31.     void        *tov;
  32.     int        cnt;
  33. {
  34.     register const char    *from    = fromv;
  35.     register char        *to    = tov;
  36.     register int        n;
  37.  
  38.     /*
  39.      * If we change cnt to be unsigned, check for == instead of <=
  40.      */
  41.     if ((n = cnt) <= 0)
  42.         return (to);
  43.  
  44.     if (from >= to) {
  45.         /*
  46.          * source is on higher adresses than destination:
  47.          *    move bytes forwards
  48.          */
  49.         if (n >= (int)(8 * sizeof(long))) {
  50.             if (l2aligned(from, to)) {
  51.                 register const long *froml = (const long *)from;
  52.                 register long *tol = (long *)to;
  53.                 register int rem = n % (8 * sizeof (long));
  54.             
  55.                 n /= (8 * sizeof (long));
  56.                 do {
  57.                     DO8 (*tol++ = *froml++);
  58.                 } while (--n > 0);
  59.  
  60.                 from = (const char *)froml;
  61.                 to = (char *)tol;
  62.                 n = rem;
  63.             }
  64.  
  65.             if (n >= 8) {
  66.                 n -= 8;
  67.                 do {
  68.                     DO8 (*to++ = *from++);
  69.                 } while ((n -= 8) >= 0);
  70.                 n += 8;
  71.             }
  72.  
  73.             if (n > 0) do {
  74.                 *to++ = *from++;
  75.             } while (--n > 0);
  76.             return (to);
  77.         }
  78.         if (n > 0) do {
  79.             *to++ = *from++;
  80.         } while (--n > 0);
  81.         return (to);
  82.     } else {
  83.         char *ep;
  84.  
  85.         /*
  86.          * source is on lower adresses than destination:
  87.          *    move bytes backwards
  88.          */
  89.         to += n;
  90.         from += n;
  91.         ep = to;
  92.         if (n >= (int)(8 * sizeof(long))) {
  93.             if (l2aligned(from, to)) {
  94.                 register const long *froml = (const long *)from;
  95.                 register long *tol = (long *)to;
  96.                 register int rem = n % (8 * sizeof (long));
  97.  
  98.                 n /= (8 * sizeof (long));
  99.                 do {
  100.                     DO8 (*--tol = *--froml);
  101.                 } while (--n > 0);
  102.  
  103.                 from = (const char *)froml;
  104.                 to = (char *)tol;
  105.                 n = rem;
  106.             }
  107.             if (n >= 8) {
  108.                 n -= 8;
  109.                 do {
  110.                     DO8 (*--to = *--from);
  111.                 } while ((n -= 8) >= 0);
  112.                 n += 8;
  113.             }
  114.             if (n > 0) do {
  115.                 *--to = *--from;
  116.             } while (--n > 0);
  117.             return (ep);
  118.         }
  119.         if (n > 0) do {
  120.             *--to = *--from;
  121.         } while (--n > 0);
  122.         return (ep);
  123.     }
  124. }
  125.