os/kernelhwsrv/kernel/eka/common/x86/cmem.cia
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32\common\x86\cmem.cia
    15 // 
    16 //
    17 
    18 #include "common.h"
    19 
    20 
    21 #if defined(__MEM_MACHINE_CODED__)
    22 extern "C" {
    23 
    24 
    25 // See header file e32cmn.h for the in-source documentation.
    26 EXPORT_C __NAKED__ TAny* memmove(TAny* /*aTrg*/, const TAny* /*aSrc*/, unsigned int /*aLength*/)
    27 	{
    28 	asm("push ebx");
    29 	asm("push esi");
    30 	asm("push edi");
    31 	asm("mov edi,[esp+16]");// destination address into edi
    32 	asm("mov esi,[esp+20]");// source address into esi
    33 	asm("mov ecx,[esp+24]");// byte count into ecx
    34 	asm("pushfd");
    35 
    36 	asm("mov eax,edi");		// return value
    37 	asm("cld");				// assume forward copy initially
    38 	asm("test ecx,ecx");	//
    39 	asm("jz short memcopy0");// if length=0, nothing to do
    40 	asm("xor edx,edx");		//
    41 	asm("cmp edi,esi");		// compare source and dest addresses
    42 	asm("je short memcopy0");// if equal, nothing to do
    43 	asm("jc short memcopy1");// if dest<source, must go forwards
    44 	asm("std");				// else go backwards
    45 	asm("add esi,ecx");		// and start at end of block
    46 	asm("add edi,ecx");		//
    47 	asm("inc edx");			// edx=1 if backwards, 0 if forwards
    48 	asm("memcopy1:");
    49 	asm("cmp ecx,16");		// if length<16 don't bother with alignment check
    50 	asm("jc short memcopy2");//
    51 	asm("mov ebx,edi");		// ebx = destination address
    52 	asm("and ebx,3");		// ebx bottom 2 bits = alignment of destination wrt hardware bus
    53 	asm("jz short memcopy3");// if aligned, proceed with block move
    54 	asm("or edx,edx");		// check direction of move
    55 	asm("jnz short memcopy4");// if backwards, ebx = number of byte moves to align destination
    56 	asm("neg ebx");			// else number of byte moves = 4-ebx
    57 	asm("add ebx,4");		//
    58 	asm("memcopy4:");
    59 	asm("sub ecx,ebx");		// subtract number of bytes from length
    60 	asm("xchg ecx,ebx");	// temporarily put length in ebx
    61 	asm("sub edi,edx");		// adjust if backwards move
    62 	asm("sub esi,edx");		//
    63 	asm("rep movsb");		// move bytes to align destination
    64 	asm("add edi,edx");		// adjust if backwards move
    65 	asm("add esi,edx");		//
    66 	asm("mov ecx,ebx");		// length back into ecx
    67 	asm("memcopy3:");
    68 	asm("mov ebx,ecx");		// save length in ebx
    69 	asm("shl edx,2");		// adjustment 4 for backwards move
    70 	asm("shr ecx,2");		// number of dwords to move into ecx
    71 	asm("sub edi,edx");		// adjust if backwards move
    72 	asm("sub esi,edx");		//
    73 	asm("rep movsd");		// perform DWORD block move
    74 	asm("add edi,edx");		// adjust if backwards move
    75 	asm("add esi,edx");		//
    76 	asm("mov ecx,ebx");		// length back into ecx
    77 	asm("and ecx,3");		// number of remaining bytes to move
    78 	asm("jz short memcopy0");// if zero, we are finished
    79 	asm("shr edx,2");		// adjustment 1 for backwards move
    80 	asm("memcopy2:");		// *** come here for small move
    81 	asm("sub edi,edx");		// adjust if backwards move
    82 	asm("sub esi,edx");		//
    83 	asm("rep movsb");			// move remaining bytes
    84 
    85 	asm("memcopy0:");
    86 	asm("popfd");
    87 	asm("pop edi");
    88 	asm("pop esi");
    89 	asm("pop ebx");
    90 	asm("ret");				// finished - return value in EAX
    91 	}
    92 
    93 
    94 // See header file e32cmn.h for the in-source documentation.
    95 EXPORT_C __NAKED__ TAny* memcpy(TAny* /*aTrg*/, const TAny* /*aSrc*/, unsigned int /*aLength*/)
    96 	{
    97 	asm("jmp %a0": :"i"(&memmove));
    98 	}
    99 
   100 
   101 
   102 
   103 // See header file e32cmn.h for the in-source documentation.
   104 EXPORT_C __NAKED__ TAny* wordmove(TAny* /*aTrg*/, const TAny* /*aSrc*/, unsigned int /*aLength*/)
   105 	{
   106 	asm("push esi");
   107 	asm("push edi");
   108 	asm("mov edi,[esp+12]");// destination address into edi
   109 	asm("mov esi,[esp+16]");// source address into esi
   110 	asm("mov ecx,[esp+20]");// byte count into ecx
   111 	asm("pushfd");
   112 
   113 	asm("mov eax,edi");		// return value
   114 	asm("cld");				//
   115 	asm("test ecx,ecx");	//
   116 	asm("jz short memmove0");// if length=0, nothing to do
   117 	asm("cmp edi,esi");		// compare source and dest addresses
   118 	asm("je short memmove0");// if equal, nothing to do
   119 	asm("jc short memmove1");// if dest<source, must go forwards
   120 	asm("std");				// else go backwards
   121 	asm("lea esi,[esi+ecx-4]"); // and start at end of block - 4
   122 	asm("lea edi,[edi+ecx-4]");	//
   123 	asm("memmove1:");
   124 	asm("shr ecx,2");		// ecx now contains number of dwords to move
   125 	asm("rep movsd");		// do dword block move
   126 
   127 	asm("memmove0:");
   128 	asm("popfd");
   129 	asm("pop edi");
   130 	asm("pop esi");
   131 	asm("ret");				// finished - return value in EAX
   132 	}
   133 
   134 
   135 
   136 // See header file e32cmn.h for the in-source documentation.
   137 EXPORT_C __NAKED__ TAny* memset(TAny* /*aTrg*/, TInt /*aValue*/, unsigned int /*aLength*/)
   138 	{
   139 	asm("push edi");
   140 	asm("mov edi,[esp+8]"); // destination address into edi
   141 	asm("mov al,[esp+12]");	// fill value into al
   142 	asm("mov ecx,[esp+16]");// number of bytes to fill into ecx
   143 	asm("push edi");		// save destination for return value
   144 	asm("pushfd");
   145 	asm("cld");				// go forwards through array
   146 	asm("test ecx,ecx");	//
   147 	asm("jz short memfill0");// if length zero, nothing to do
   148 	asm("cmp ecx,8");		// if array very small, just do byte fills
   149 	asm("jb short memfill1");
   150 
   151 	asm("mov ah,al");		// repeat al in all bytes of eax
   152 	asm("movzx edx,ax");	//
   153 	asm("shl eax,16");		//
   154 	asm("or eax,edx");		//
   155 	asm("mov edx,ecx");		// length into edx
   156 	asm("mov ecx,4");		// number of byte fills to align = 4-[edi mod 4]
   157 	asm("sub ecx,edi");		//
   158 	asm("and ecx,3");		//
   159 	asm("jz short memfill2");// if already aligned, proceed to dword fill
   160 	asm("sub edx,ecx");		// subtract alignment bytes from length
   161 	asm("rep stosb");		// do byte fills to align
   162 	asm("memfill2:");
   163 	asm("mov ecx,edx");		// length remaining into ecx
   164 	asm("shr ecx,2");		// number of dwords to fill into ecx
   165 	asm("rep stosd");		// perform dword fill
   166 	asm("mov ecx,edx");		// calculate number of leftover bytes
   167 	asm("and ecx,3");		// in ecx
   168 	asm("jz short memfill0");// if none left, exit
   169 	asm("memfill1:");
   170 	asm("rep stosb");		// do byte fills to make up correct length
   171 
   172 	asm("memfill0:");
   173 	asm("popfd");
   174 	asm("pop eax");			// return value
   175 	asm("pop edi");
   176 	asm("ret");
   177 	}
   178 
   179 
   180 
   181 
   182 // See header file e32cmn.h for the in-source documentation.
   183 EXPORT_C __NAKED__ TAny* memclr(TAny* /*aTrg*/, unsigned int /*aLength*/)
   184 	{
   185 	asm("push edi");
   186 	asm("mov edi,[esp+8]");	// destination address into edi
   187 	asm("mov ecx,[esp+12]");// number of bytes to fill into ecx
   188 	asm("xor eax, eax");	// fill value
   189 	asm("push edi");		// save destination for return value
   190 	asm("pushfd");
   191 	asm("cld");				// go forwards through array
   192 	asm("test ecx,ecx");	//
   193 	asm("jz short memclr0");// if length zero, nothing to do
   194 	asm("cmp ecx,8");		// if array very small, just do byte fills
   195 	asm("jb short memclr1");
   196 
   197 	asm("mov edx,ecx");		// length into edx
   198 	asm("mov ecx,4");		// number of byte fills to align = 4-[edi mod 4]
   199 	asm("sub ecx,edi");		//
   200 	asm("and ecx,3");		//
   201 	asm("jz short memclr2");// if already aligned, proceed to dword fill
   202 	asm("sub edx,ecx");		// subtract alignment bytes from length
   203 	asm("rep stosb");		// do byte fills to align
   204 	asm("memclr2:");
   205 	asm("mov ecx,edx");		// length remaining into ecx
   206 	asm("shr ecx,2");		// number of dwords to fill into ecx
   207 	asm("rep stosd");		// perform dword fill
   208 	asm("mov ecx,edx");		// calculate number of leftover bytes
   209 	asm("and ecx,3");		// in ecx
   210 	asm("jz short memclr0");// if none left, exit
   211 	asm("memclr1:");
   212 	asm("rep stosb");		// do byte fills to make up correct length
   213 
   214 	asm("memclr0:");
   215 	asm("popfd");
   216 	asm("pop eax");			// return value
   217 	asm("pop edi");
   218 	asm("ret");
   219 	}
   220 
   221 
   222 
   223 //EXPORT_C __NAKED__ TInt Mem::Compare(const TUint8* /*aLeft*/, TInt /*aLeftL*/, const TUint8* /*aRight*/, TInt /*aRightL*/)
   224 //	{
   225 //	asm("jmp _memcompare");
   226 //	}
   227 
   228 // See header file e32cmn.h for the in-source documentation.
   229 EXPORT_C __NAKED__ TInt memcompare(const TUint8* /*aLeft*/, TInt /*aLeftL*/, const TUint8* /*aRight*/, TInt /*aRightL*/)
   230 	{
   231 	asm("push esi");
   232 	asm("push edi");
   233 	asm("mov esi,[esp+12]");// left buffer address into esi
   234 	asm("mov ecx,[esp+16]");// left buffer length into ecx
   235 	asm("mov edi,[esp+20]");// right buffer address into edi
   236 	asm("mov eax,[esp+24]");// right buffer length into eax
   237 	asm("pushfd");
   238 
   239 	asm("mov edx,ecx");		// edx = left len
   240 	asm("cld");				// go forwards through arrays
   241 	asm("sub edx,eax");		// edx = [left len - right len]
   242 	asm("jbe short memcmp1");//
   243 	asm("mov ecx,eax");		// ecx = min[ left len, right len ]
   244 	asm("memcmp1:");
   245 	asm("mov eax,edx");		// save length difference for later use
   246 	asm("test ecx,ecx");	//
   247 	asm("jz short memcmp0");// if length=0, nothing to compare
   248 	asm("cmp ecx,8");		// for small buffers don't bother with alignment check
   249 	asm("jc short memcmp2");//
   250 	asm("mov edx,ecx");		// comparison length into edx
   251 	asm("mov ecx,4");		// number of byte compares to align left ptr = 4-[esi mod 4]
   252 	asm("sub ecx,esi");		//
   253 	asm("and ecx,3");		// number of byte compares to align in ecx
   254 	asm("jz short memcmp3");// if already aligned, proceed with dword compare
   255 	asm("sub edx,ecx");		// subtract number of byte compares from length
   256 	asm("repe cmpsb");		// do byte compares to align
   257 	asm("jne short memcmp4");// if a difference was found, exit
   258 	asm("memcmp3:");
   259 	asm("mov ecx,edx");		// length back into ecx
   260 	asm("shr ecx,2");		// number of dwords to compare
   261 	asm("repe cmpsd");		// perform DWORD block comparison
   262 	asm("je short memcmp5");// if no difference found, proceed with leftover bytes
   263 	asm("mov eax,[esi-4]");	// differing left dword into eax [first char in al]
   264 	asm("mov edx,[edi-4]");	// differing right dword into edx [first char in dl]
   265 	asm("cmp al,dl");		// compare 1st chars
   266 	asm("jne short memcmp7");// if difference found, exit
   267 	asm("cmp ah,dh");		// compare 2nd chars
   268 	asm("jne short memcmp6");// if difference found, exit
   269 	asm("shr eax,16");		// 3rd and 4th chars into al, ah respectively
   270 	asm("shr edx,16");		// 3rd and 4th chars into dl, dh respectively
   271 	asm("cmp al,dl");		// compare 3rd chars
   272 	asm("jne short memcmp7");// if difference found, exit, else 4th chars must differ
   273 	asm("memcmp6:");
   274 	asm("mov al,ah");		// differing bytes into al and dl
   275 	asm("mov dl,dh");		//
   276 	asm("memcmp7:");
   277 	asm("movzx eax,al");	// zero extend al into eax
   278 	asm("movzx edx,dl");	// zero extend dl into edx
   279 	asm("jmp short memcmp8");// and exit with eax = difference of bytes
   280 	asm("memcmp5:");
   281 	asm("mov ecx,edx");		// length back into ecx
   282 	asm("and ecx,3");		// number of remaining bytes to compare
   283 	asm("jz short memcmp0");// if zero, exit with eax=length difference
   284 	asm("memcmp2:");		// *** come here for small buffers
   285 	asm("repe cmpsb");		// compare remaining bytes
   286 	asm("je short memcmp0");// if no difference found, exit with eax=length difference
   287 	asm("memcmp4:");		// *** come here if a byte has been compared and differs
   288 	asm("movzx eax,byte ptr [esi-1]");
   289 	asm("movzx edx,byte ptr [edi-1]");
   290 	asm("memcmp8:");
   291 	asm("sub eax,edx");		// return value is difference of bytes
   292 	
   293 	asm("memcmp0:");		// *** come here if common initial segment identical
   294 	asm("popfd");
   295 	asm("pop edi");
   296 	asm("pop esi");
   297 	asm("ret");
   298 	}
   299 }
   300 #endif