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
|
2009-10-05 11:05:11 +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
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LNX_MEMZERO_H_
|
|
|
|
#define _LNX_MEMZERO_H_
|
|
|
|
|
2009-09-23 12:53:32 +00:00
|
|
|
// This header contains non-optimized implementation of memzero_ptr and memset8,
|
|
|
|
// memset16, etc.
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
template< u32 data, typename T >
|
2009-09-23 12:53:32 +00:00
|
|
|
static __forceinline void memset32( T& obj )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
// this function works on 32-bit aligned lengths of data only.
|
|
|
|
// 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. ;)
|
|
|
|
|
|
|
|
jASSUME( (sizeof(T) & 0x3) == 0 );
|
|
|
|
|
|
|
|
u32* dest = (u32*)&obj;
|
|
|
|
for( int i=sizeof(T)>>2; i; --i, ++dest )
|
|
|
|
*dest = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
template< uint size >
|
|
|
|
static __forceinline void memzero_ptr( void* dest )
|
|
|
|
{
|
|
|
|
memset( dest, 0, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
template< typename T >
|
2009-09-23 12:53:32 +00:00
|
|
|
static __forceinline void memzero( T& obj )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
memset( &obj, 0, sizeof( T ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template< u8 data, typename T >
|
2009-09-23 12:53:32 +00:00
|
|
|
static __forceinline void memset8( T& obj )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
// Aligned sizes use the optimized 32 bit inline memset. Unaligned sizes use memset.
|
|
|
|
if( (sizeof(T) & 0x3) != 0 )
|
|
|
|
memset( &obj, data, sizeof( T ) );
|
|
|
|
else
|
2009-09-23 12:53:32 +00:00
|
|
|
memset32<data + (data<<8) + (data<<16) + (data<<24)>( obj );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template< u16 data, typename T >
|
2009-09-23 12:53:32 +00:00
|
|
|
static __forceinline void memset16( T& obj )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
if( (sizeof(T) & 0x3) != 0 )
|
2009-09-12 02:58:22 +00:00
|
|
|
_memset16_unaligned( &obj, data, sizeof( T ) );
|
2009-02-09 21:15:56 +00:00
|
|
|
else
|
2009-09-23 12:53:32 +00:00
|
|
|
memset32<data + (data<<16)>( obj );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// An optimized memset for 8 bit destination data.
|
|
|
|
template< u8 data, size_t bytes >
|
|
|
|
static __forceinline void memset_8( void *dest )
|
|
|
|
{
|
|
|
|
if( bytes == 0 ) return;
|
|
|
|
|
|
|
|
if( (bytes & 0x3) != 0 )
|
|
|
|
{
|
|
|
|
// unaligned data length. No point in doing an optimized inline version (too complicated!)
|
|
|
|
// So fall back on the compiler implementation:
|
|
|
|
|
|
|
|
memset( dest, data, bytes );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function only works on 32-bit alignments of data copied.
|
|
|
|
jASSUME( (bytes & 0x3) == 0 );
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
remdat = bytes>>2,
|
|
|
|
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:
|
2009-10-05 11:05:11 +00:00
|
|
|
__asm__ volatile
|
2009-02-09 21:15:56 +00:00
|
|
|
(
|
2009-03-05 21:35:26 +00:00
|
|
|
".intel_syntax noprefix\n"
|
2009-02-09 21:15:56 +00:00
|
|
|
"cld\n"
|
2009-03-19 11:11:30 +00:00
|
|
|
// "mov edi, %[dest]\n"
|
|
|
|
// "mov eax, %[data32]\n"
|
2009-02-09 21:15:56 +00:00
|
|
|
"stosd\n"
|
|
|
|
"stosd\n"
|
|
|
|
"stosd\n"
|
|
|
|
".att_syntax\n"
|
2010-06-01 13:34:55 +00:00
|
|
|
: "=D"(dest)
|
2009-10-05 11:05:11 +00:00
|
|
|
// Input specifiers: D - edi, a -- eax, c ecx
|
2009-03-19 11:11:30 +00:00
|
|
|
: [dest]"D"(dest), [data32]"a"(data32)
|
2009-10-05 11:05:11 +00:00
|
|
|
: "memory"
|
2009-02-09 21:15:56 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 4:
|
2009-10-05 11:05:11 +00:00
|
|
|
__asm__ volatile
|
|
|
|
(
|
2009-03-05 21:35:26 +00:00
|
|
|
".intel_syntax noprefix\n"
|
2009-02-09 21:15:56 +00:00
|
|
|
"cld\n"
|
2009-03-19 11:11:30 +00:00
|
|
|
// "mov edi, %[dest]\n"
|
|
|
|
// "mov eax, %[data32]\n"
|
2009-02-09 21:15:56 +00:00
|
|
|
"stosd\n"
|
|
|
|
"stosd\n"
|
|
|
|
"stosd\n"
|
|
|
|
"stosd\n"
|
|
|
|
".att_syntax\n"
|
2010-06-01 13:34:55 +00:00
|
|
|
: "=D"(dest)
|
2009-03-19 11:11:30 +00:00
|
|
|
: [dest]"D"(dest), [data32]"a"(data32)
|
2009-10-05 11:05:11 +00:00
|
|
|
: "memory"
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 5:
|
2009-10-05 11:05:11 +00:00
|
|
|
__asm__ volatile
|
2009-02-09 21:15:56 +00:00
|
|
|
(
|
2009-03-05 21:35:26 +00:00
|
|
|
".intel_syntax noprefix\n"
|
2009-02-09 21:15:56 +00:00
|
|
|
"cld\n"
|
2009-03-19 11:11:30 +00:00
|
|
|
// "mov edi, %[dest]\n"
|
|
|
|
// "mov eax, %[data32]\n"
|
2009-02-09 21:15:56 +00:00
|
|
|
"stosd\n"
|
|
|
|
"stosd\n"
|
|
|
|
"stosd\n"
|
|
|
|
"stosd\n"
|
|
|
|
"stosd\n"
|
|
|
|
".att_syntax\n"
|
2010-06-01 13:34:55 +00:00
|
|
|
: "=D"(dest)
|
2009-10-05 11:05:11 +00:00
|
|
|
: [dest]"D"(dest), [data32]"a"(data32)
|
|
|
|
: "memory"
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2009-10-05 11:05:11 +00:00
|
|
|
__asm__ volatile
|
2009-02-09 21:15:56 +00:00
|
|
|
(
|
2009-03-05 21:35:26 +00:00
|
|
|
".intel_syntax noprefix\n"
|
2009-02-09 21:15:56 +00:00
|
|
|
"cld\n"
|
2009-03-19 11:11:30 +00:00
|
|
|
// "mov ecx, %[remdat]\n"
|
|
|
|
// "mov edi, %[dest]\n"
|
|
|
|
// "mov eax, %\[data32]n"
|
2009-02-09 21:15:56 +00:00
|
|
|
"rep stosd\n"
|
|
|
|
".att_syntax\n"
|
2010-06-01 13:34:55 +00:00
|
|
|
: "=D"(dest)
|
2009-03-19 11:11:30 +00:00
|
|
|
: [remdat]"c"(remdat), [dest]"D"(dest), [data32]"a"(data32)
|
2009-10-05 11:05:11 +00:00
|
|
|
: "memory"
|
2009-02-09 21:15:56 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|