Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / _memmove.c
1 /*-------------------------------------------------------------------------
2   _memmove.c - part of string library functions
3
4              Adapted By -  Erik Petrich  . epetrich@users.sourceforge.net
5              from _memcpy.c which was originally
6              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
7
8    This library is free software; you can redistribute it and/or modify it
9    under the terms of the GNU Library General Public License as published by the
10    Free Software Foundation; either version 2, or (at your option) any
11    later version.
12    
13    This library 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 Library General Public License for more details.
17    
18    You should have received a copy of the GNU Library General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21    
22    In other words, you are welcome to use, share and improve this program.
23    You are forbidden to forbid anyone else to use, share and improve
24    what you give them.   Help stamp out software-hoarding!  
25 -------------------------------------------------------------------------*/
26 #include "string.h" 
27 #include <sdcc-lib.h>
28
29 #ifndef _SDCC_PORT_PROVIDES_MEMMOVE
30 #define _SDCC_PORT_PROVIDES_MEMMOVE 0
31 #endif
32
33 #if !_SDCC_PORT_PROVIDES_MEMMOVE
34
35 void * memmove (
36         void * dst,
37         void * src,
38         size_t acount
39         ) 
40 {
41 #if _SDCC_Z80_STYLE_LIB_OPT
42
43 #pragma noinduction
44
45         char * d;
46         char * s;
47         /* PENDING: Divide first to get around sign problems */
48         int count = -(acount >> 2);
49
50         if (((int)src < (int)dst) && ((((int)src)+acount) > (int)dst)) {
51                 /*
52                  * copy from higher addresses to lower addresses
53                  */
54                 d = ((char *)dst)+acount-1;
55                 s = ((char *)src)+acount-1;
56                 while (count) {
57                         *d-- = *s--;
58                         *d-- = *s--;
59                         *d-- = *s--;
60                         *d-- = *s--;
61                         count++;
62                 }
63
64                 if (acount & 2) {
65                         *d-- = *s--;
66                         *d-- = *s--;
67                 }
68                 if (acount & 1) {
69                         *d-- = *s--;
70                 }
71         }
72         else {
73                 /*
74                  * copy from lower addresses to higher addresses
75                  */
76                 d = dst;
77                 s = src;
78                 while (count) {
79                         *d++ = *s++;
80                         *d++ = *s++;
81                         *d++ = *s++;
82                         *d++ = *s++;
83                         count++;
84                 }
85
86                 if (acount & 2) {
87                         *d++ = *s++; 
88                         *d++ = *s++;
89                 }
90                 if (acount & 1) {
91                         *d++ = *s++;
92                 }
93         }
94         return dst;
95 #else
96         void * ret = dst;
97         char * d;
98         char * s;
99
100         if (((int)src < (int)dst) && ((((int)src)+acount) > (int)dst)) {
101                 /*
102                  * copy from higher addresses to lower addresses
103                  */
104                 d = ((char *)dst)+acount-1;
105                 s = ((char *)src)+acount-1;
106                 while (acount--) {
107                         *d-- = *s--;
108                 }
109         }
110         else {
111                 /*
112                  * copy from lower addresses to higher addresses
113                  */
114                 d = dst;
115                 s = src;
116                 while (acount--) {
117                         *d++ = *s++;
118                 }
119         }
120
121         return(ret);
122 #endif
123 }
124 #endif