2009-09-08 12:08:10 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2009-02-09 21:15:56 +00:00
|
|
|
*/
|
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
#pragma once
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma warning(disable:4063) // case '1' is not a valid value for switch()
|
|
|
|
#endif
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
// These functions are meant for memset operations of constant length only.
|
|
|
|
// For dynamic length clears, use the C-compiler provided memset instead.
|
|
|
|
|
|
|
|
// MemZero Code Strategies:
|
|
|
|
// I use a trick to help the MSVC compiler optimize it's asm code better. The compiler
|
|
|
|
// won't optimize local variables very well because it insists in storing them on the
|
|
|
|
// stack and then loading them out of the stack when I use them from inline ASM, and
|
|
|
|
// it won't allow me to use template parameters in inline asm code either. But I can
|
|
|
|
// assign the template parameters to enums, and then use the enums from asm code.
|
|
|
|
// Yeah, silly, but it works. :D (air)
|
|
|
|
|
|
|
|
// All methods defined in this header use template in combination with the aforementioned
|
|
|
|
// enumerations to generate very efficient and compact inlined code. These optimized
|
|
|
|
// memsets work on the theory that most uses of memset involve static arrays and
|
|
|
|
// structures, which are constant in size, thus allowing us to generate optimal compile-
|
|
|
|
// time code for each use of the function.
|
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
// Use of CLD (Clear Direction Flag):
|
|
|
|
// On Windows platforms the ABI declares that the direction flag should be cleared upon
|
|
|
|
// entry of *any* function. Therefore there is no need to have CLD prior to our use of
|
|
|
|
// rep strosd here.
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
// Notes on XMM0's "storage" area (_xmm_backup):
|
|
|
|
// Unfortunately there's no way to guarantee alignment for this variable. If I use the
|
|
|
|
// __declspec(aligned(16)) decorator, MSVC fails to inline the function since stack
|
|
|
|
// alignment requires prep work. And for the same reason it's not possible to check the
|
|
|
|
// alignment of the stack at compile time, so I'm forced to use movups to store and
|
|
|
|
// retrieve xmm0.
|
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
// MSVC Template Issue:
|
|
|
|
// MSVC treats int template parameters like macro insertions. That is, if you have a
|
|
|
|
// a template parameter in the form of "func<10-5>()", MSVC inserts 10-5 into the
|
|
|
|
// templated function, causing order-of-operation problems (sigh). The normal fix would
|
|
|
|
// be to assign the template parameter to a static const int inside each function, but that
|
|
|
|
// won't fly with the enums optimization. So in order to fix the problem I define a macro
|
|
|
|
// that encapsulates the template parameter inside parenthesis for us:
|
|
|
|
|
|
|
|
#define MZFbytes (_bytes)
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
// An optimized memset for 8 bit destination data.
|
2009-07-23 13:40:13 +00:00
|
|
|
template< u8 data, size_t _bytes >
|
2010-08-09 04:10:38 +00:00
|
|
|
static __fi void memset_8( void *dest )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-23 13:40:13 +00:00
|
|
|
if( MZFbytes == 0 ) return;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
if( (MZFbytes & 0x3) != 0 )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
// unaligned data length. No point in doing an optimized inline version (too complicated!)
|
|
|
|
// So fall back on the compiler implementation:
|
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
memset( dest, data, MZFbytes );
|
2009-02-09 21:15:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
/*static const size_t remainder = MZFbytes & 127;
|
|
|
|
static const size_t bytes128 = MZFbytes / 128;
|
2009-02-09 21:15:56 +00:00
|
|
|
if( bytes128 > 32 )
|
|
|
|
{
|
|
|
|
// This function only works on 128-bit alignments.
|
2010-06-25 02:35:27 +00:00
|
|
|
pxAssume( (MZFbytes & 0xf) == 0 );
|
|
|
|
pxAssume( ((uptr)dest & 0xf) == 0 );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov eax,bytes128
|
|
|
|
mov ecx,dest
|
|
|
|
movss xmm0,data
|
|
|
|
|
|
|
|
align 16
|
|
|
|
|
|
|
|
_loop_8:
|
|
|
|
movaps [ecx],xmm0;
|
|
|
|
movaps [ecx+0x10],xmm0;
|
|
|
|
movaps [ecx+0x20],xmm0;
|
|
|
|
movaps [ecx+0x30],xmm0;
|
|
|
|
movaps [ecx+0x40],xmm0;
|
|
|
|
movaps [ecx+0x50],xmm0;
|
|
|
|
movaps [ecx+0x60],xmm0;
|
|
|
|
movaps [ecx+0x70],xmm0;
|
|
|
|
sub ecx,-128
|
|
|
|
dec eax;
|
|
|
|
jnz _loop_8;
|
|
|
|
}
|
|
|
|
if( remainder != 0 )
|
|
|
|
{
|
|
|
|
// Copy the remainder in reverse (using the decrementing eax as our indexer)
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov eax, remainder
|
|
|
|
|
|
|
|
_loop_10:
|
|
|
|
movaps [ecx+eax],xmm0;
|
|
|
|
sub eax,16;
|
|
|
|
jnz _loop_10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
// This function only works on 32-bit alignments of data copied.
|
2010-06-25 02:35:27 +00:00
|
|
|
pxAssume( (MZFbytes & 0x3) == 0 );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2009-07-23 13:40:13 +00:00
|
|
|
remdat = MZFbytes >> 2,
|
2009-02-09 21:15:56 +00:00
|
|
|
data32 = data + (data<<8) + (data<<16) + (data<<24)
|
|
|
|
};
|
|
|
|
|
|
|
|
// macro to execute the x86/32 "stosd" copies.
|
|
|
|
switch( remdat )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
*(u32*)dest = data32;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
((u32*)dest)[0] = data32;
|
|
|
|
((u32*)dest)[1] = data32;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov ecx, remdat;
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
rep stosd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
template< u16 data, size_t _bytes >
|
2010-08-09 04:10:38 +00:00
|
|
|
static __fi void memset_16( void *dest )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-23 13:40:13 +00:00
|
|
|
if( MZFbytes == 0 ) return;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-06-25 02:35:27 +00:00
|
|
|
// Assertion: data length must be a multiple of 16 or 32 bits
|
|
|
|
pxAssume( (MZFbytes & 0x1) == 0 );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
if( (MZFbytes & 0x3) != 0 )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
// Unaligned data length. No point in doing an optimized inline version (too complicated with
|
|
|
|
// remainders and such).
|
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
_memset16_unaligned( dest, data, MZFbytes );
|
2009-02-09 21:15:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//u64 _xmm_backup[2];
|
|
|
|
|
|
|
|
// This function only works on 32-bit alignments of data copied.
|
2010-06-25 02:35:27 +00:00
|
|
|
pxAssume( (MZFbytes & 0x3) == 0 );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2009-07-23 13:40:13 +00:00
|
|
|
remdat = MZFbytes >> 2,
|
2009-02-09 21:15:56 +00:00
|
|
|
data32 = data + (data<<16)
|
|
|
|
};
|
|
|
|
|
|
|
|
// macro to execute the x86/32 "stosd" copies.
|
|
|
|
switch( remdat )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
*(u32*)dest = data32;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
((u32*)dest)[0] = data32;
|
|
|
|
((u32*)dest)[1] = data32;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov ecx, remdat;
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
rep stosd;
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
template< u32 data, size_t MZFbytes >
|
2010-08-09 04:10:38 +00:00
|
|
|
static __fi void memset_32( void *dest )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-23 13:40:13 +00:00
|
|
|
if( MZFbytes == 0 ) return;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-06-25 02:35:27 +00:00
|
|
|
// Assertion: data length must be a multiple of 32 bits
|
|
|
|
pxAssume( (MZFbytes & 0x3) == 0 );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
//u64 _xmm_backup[2];
|
|
|
|
|
|
|
|
// This function only works on 32-bit alignments of data copied.
|
|
|
|
// If the data length is not a factor of 32 bits, the C++ optimizing compiler will
|
|
|
|
// probably just generate mysteriously broken code in Release builds. ;)
|
|
|
|
|
2010-06-25 02:35:27 +00:00
|
|
|
pxAssume( (MZFbytes & 0x3) == 0 );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2009-07-23 13:40:13 +00:00
|
|
|
remdat = MZFbytes>>2,
|
2009-02-09 21:15:56 +00:00
|
|
|
data32 = data
|
|
|
|
};
|
|
|
|
|
|
|
|
// macro to execute the x86/32 "stosd" copies.
|
|
|
|
switch( remdat )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
*(u32*)dest = data32;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
((u32*)dest)[0] = data32;
|
|
|
|
((u32*)dest)[1] = data32;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
stosd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov ecx, remdat;
|
|
|
|
mov edi, dest;
|
|
|
|
mov eax, data32;
|
|
|
|
rep stosd;
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method can clear any object-like entity -- which is anything that is not a pointer.
|
|
|
|
// Structures, static arrays, etc. No need to include sizeof() crap, this does it automatically
|
|
|
|
// for you!
|
|
|
|
template< typename T >
|
2010-08-09 04:10:38 +00:00
|
|
|
static __fi void memzero( T& object )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2014-08-17 13:23:21 +00:00
|
|
|
memset(&object, 0, sizeof(T));
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method clears an object with the given 8 bit value.
|
|
|
|
template< u8 data, typename T >
|
2010-08-09 04:10:38 +00:00
|
|
|
static __fi void memset8( T& object )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
memset_8<data, sizeof(T)>( &object );
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method clears an object with the given 16 bit value.
|
|
|
|
template< u16 data, typename T >
|
2010-08-09 04:10:38 +00:00
|
|
|
static __fi void memset16( T& object )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
memset_16<data, sizeof(T)>( &object );
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method clears an object with the given 32 bit value.
|
|
|
|
template< u32 data, typename T >
|
2010-08-09 04:10:38 +00:00
|
|
|
static __fi void memset32( T& object )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
memset_32<data, sizeof(T)>( &object );
|
|
|
|
}
|
|
|
|
|
2009-07-23 13:40:13 +00:00
|
|
|
#undef MZFbytes
|