1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* ---- includes ----------------------------------------------------------- */
18 
19 #include "b_BasicEm/Memory.h"
20 #include "b_BasicEm/Functions.h"
21 /*
22 #include <string.h>
23 */
24 /* ---- related objects  --------------------------------------------------- */
25 
26 /* ---- typedefs ----------------------------------------------------------- */
27 
28 /* ---- constants ---------------------------------------------------------- */
29 
30 /* ------------------------------------------------------------------------- */
31 
32 /* ========================================================================= */
33 /*                                                                           */
34 /* ---- \ghd{ external functions } ----------------------------------------- */
35 /*                                                                           */
36 /* ========================================================================= */
37 
38 /* ------------------------------------------------------------------------- */
39 /*
40 void* bbs_memcpy( void* dstA, const void* srcA, uint32 sizeA )
41 {
42 	if( sizeA & 1 )
43 	{
44 		bbs_ERROR0( "bbs_memcpy( .... ): sizeA must be even" );
45 		return NULL;
46 	}
47 	return bbs_memcpy16( dstA, srcA, sizeA >> 1 );
48 }
49 */
50 /* ------------------------------------------------------------------------- */
51 
bbs_memcpy16(void * dstA,const void * srcA,uint32 sizeA)52 void* bbs_memcpy16( void* dstA, const void* srcA, uint32 sizeA )
53 {
54 #ifdef HW_TMS320C5x
55 	if( ( ( int32 ) dstA >> 16 ) == ( ( ( int32 ) dstA + sizeA ) >> 16 ) &&
56 		( ( int32 ) srcA >> 16 ) == ( ( ( int32 ) srcA + sizeA ) >> 16 ) )
57 	{
58 		/* fast version, works only if pointers do not cross page boundary. */
59 		uint16* dstL = ( uint16* )dstA;
60 		const uint16* srcL = ( uint16* )srcA;
61 		uint16 iL;
62 		for( iL = sizeA; iL--; )
63 		{
64 			*dstL++ = *srcL++;
65 		}
66 	}
67 	else
68 	{
69 		/* safe version */
70 		uint32 iL;
71 		for( iL = 0; iL < sizeA; iL++ )
72 		{
73 			*( uint16* ) ( ( int32 ) dstA + iL ) = *( uint16* ) ( ( int32 ) srcA + iL );
74 		}
75 	}
76 	return dstA;
77 #else
78 	uint16* dstL = ( uint16* )dstA;
79 	const uint16* srcL = ( uint16* )srcA;
80 
81 	for( ; sizeA >= 4; sizeA -= 4 )
82 	{
83 		dstL[ 0 ] = srcL[ 0 ];
84 		dstL[ 1 ] = srcL[ 1 ];
85 		dstL[ 2 ] = srcL[ 2 ];
86 		dstL[ 3 ] = srcL[ 3 ];
87 		dstL += 4;
88 		srcL += 4;
89 	}
90 
91 	for( ; sizeA > 0; sizeA-- )
92 	{
93 		*dstL++ = *srcL++;
94 	}
95 
96 	return dstA;
97 #endif
98 }
99 
100 /* ------------------------------------------------------------------------- */
101 
bbs_memcpy32(void * dstA,const void * srcA,uint32 sizeA)102 void* bbs_memcpy32( void* dstA, const void* srcA, uint32 sizeA )
103 {
104 #ifdef HW_TMS320C5x
105 	if( ( ( int32 ) dstA >> 16 ) == ( ( ( int32 ) dstA + ( sizeA << 1 ) ) >> 16 ) &&
106 		( ( int32 ) srcA >> 16 ) == ( ( ( int32 ) srcA + ( sizeA << 1 ) ) >> 16 ) )
107 	{
108 		/* fast version, works only if pointers do not cross page boundary. */
109 		uint32* dstL = ( uint32* )dstA;
110 		const uint32* srcL = ( uint32* )srcA;
111 		uint16 iL;
112 		for( iL = sizeA; iL--; )
113 		{
114 			*dstL++ = *srcL++;
115 		}
116 	}
117 	else
118 	{
119 		/* safe version */
120 		uint32 iL;
121 		sizeA <<= 1;
122 		for( iL = 0; iL < sizeA; iL += 2 )
123 		{
124 			*( uint32* ) ( ( int32 ) dstA + iL ) = *( uint32* ) ( ( int32 ) srcA + iL );
125 		}
126 	}
127 	return dstA;
128 /*
129 	uint16* dstL = ( uint16* )dstA;
130 	const uint16* srcL = ( uint16* )srcA;
131 
132 	// copying with base object-size of 16bit
133 	// is more efficient on 16 bit architecture
134 	sizeA <<= 1;
135 
136 	for( ; sizeA >= 4; sizeA -= 4 )
137 	{
138 		dstL[ 0 ] = srcL[ 0 ];
139 		dstL[ 1 ] = srcL[ 1 ];
140 		dstL[ 2 ] = srcL[ 2 ];
141 		dstL[ 3 ] = srcL[ 3 ];
142 		dstL += 4;
143 		srcL += 4;
144 	}
145 
146 	for( ; sizeA > 0; sizeA-- )
147 	{
148 		*dstL++ = *srcL++;
149 	}
150 
151 	return dstA;
152 */
153 #else	/* 32bit architectures */
154 
155 	uint32* dstL = ( uint32* )dstA;
156 	const uint32* srcL = ( uint32* )srcA;
157 
158 	for( ; sizeA >= 4; sizeA -= 4 )
159 	{
160 		dstL[ 0 ] = srcL[ 0 ];
161 		dstL[ 1 ] = srcL[ 1 ];
162 		dstL[ 2 ] = srcL[ 2 ];
163 		dstL[ 3 ] = srcL[ 3 ];
164 		dstL += 4;
165 		srcL += 4;
166 	}
167 
168 	for( ; sizeA > 0; sizeA-- )
169 	{
170 		*dstL++ = *srcL++;
171 	}
172 
173 	return dstA;
174 
175 #endif
176 }
177 
178 /* ------------------------------------------------------------------------- */
179 
bbs_memset16(void * dstA,uint16 valA,uint32 sizeA)180 void* bbs_memset16( void* dstA, uint16 valA, uint32 sizeA )
181 {
182 	uint32 iL;
183 	uint16* dstL = ( uint16* )dstA;
184 	/* to be optimized */
185 	for( iL = 0; iL < sizeA; iL++ )
186 	{
187 		*dstL++ = valA;
188 	}
189 	return dstA;
190 }
191 
192 /* ------------------------------------------------------------------------- */
193 
bbs_memset32(void * dstA,uint32 valA,uint32 sizeA)194 void* bbs_memset32( void* dstA, uint32 valA, uint32 sizeA )
195 {
196 	uint32 iL;
197 	uint32* dstL = ( uint32* )dstA;
198 	/* to be optimized */
199 	for( iL = 0; iL < sizeA; iL++ )
200 	{
201 		*dstL++ = valA;
202 	}
203 	return dstA;
204 }
205 
206 /* ------------------------------------------------------------------------- */
207 
208