Project just finished so I've had some free time to investigate this for myself, as I thought it'd be rather odd if it didn't actually work.
Direcly replacing the loads with preloads looked lovely in the asm
- Code:
pld [r3], #0x20
pld [r3], #0x20
pld [r3], #0x20
pld [r3], #0x20
Saving 4 registers which might be needed in a more complex function.
However, very dissapointingly and consistent with other people's findings, this was significantly slower (though faster than not preloading at all). On a h4190 I got :
Seconds
6.97 - No preloading
2.61 - version 2
4.48 - using PLD instruction
I also tried preloading smaller chunks on the assumption that a large number of preload requests might push older requests out of the request list. This didn't have any significant effect.
Wonder if there are any hardware guys that can explain exactly why this happens?
Anyway, to curb my dissapointment, I thought I'd have a go at optimizing the code a bit and came up with these :
- Code:
// MooShort 1.81
int scaleImageRGB565ToHalfRes_MooShort(
const unsigned short* p_sourceData,
unsigned short* p_destData,
unsigned int width,
unsigned int height )
{
unsigned int halfWidth = width>>1;
unsigned short dummy0=0,dummy1=0,dummy2=0,dummy3=0;
do
{
// Preload this line
const unsigned short* p_preloadStart = p_sourceData;
const unsigned short* p_preloadEnd = p_sourceData + width;
do
{
dummy0 = *p_preloadStart;
p_preloadStart += 16;
dummy1 = *p_preloadStart;
p_preloadStart += 16;
dummy2 = *p_preloadStart;
p_preloadStart += 16;
dummy3 = *p_preloadStart;
p_preloadStart += 16;
} while ( p_preloadStart < p_preloadEnd );
unsigned short* p_destLineEnd = p_destData + halfWidth;
do
{
// Copy every other pixel to destination
unsigned short src0 = *p_sourceData;
p_sourceData += 2;
unsigned short src1 = *p_sourceData;
p_sourceData += 2;
unsigned short src2 = *p_sourceData;
p_sourceData += 2;
unsigned short src3 = *p_sourceData;
p_sourceData += 2;
*p_destData++ = src0;
*p_destData++ = src1;
*p_destData++ = src2;
*p_destData++ = src3;
} while ( p_destData < p_destLineEnd );
// Ignore odd lines
p_sourceData += width;
height -= 2;
} while ( height > 0 );
return dummy0+dummy1+dummy2+dummy3;
}
This chappie takes 1.81 secs on my ppc (30% faster)
- Code:
// MooUnrolled - 1.75
__forceinline void CopyPixels( unsigned short*& p_destData, const unsigned short*& p_sourceData )
{
unsigned short src0 = *p_sourceData;
p_sourceData += 2;
unsigned short src1 = *p_sourceData;
p_sourceData += 2;
unsigned short src2 = *p_sourceData;
p_sourceData += 2;
unsigned short src3 = *p_sourceData;
p_sourceData += 2;
// produce 4 RGB pixels
*p_destData++ = src0;
*p_destData++ = src1;
*p_destData++ = src2;
*p_destData++ = src3;
}
int scaleImageRGB565ToHalfRes_MooUnrolled(
const unsigned short* p_sourceData,
unsigned short* p_destData,
unsigned int width,
unsigned int height )
{
unsigned int halfWidth = width>>1;
unsigned short dummy0=0,dummy1=0,dummy2=0,dummy3=0;
do
{
// Preload this line
const unsigned short* p_preloadStart = p_sourceData;
const unsigned short* p_preloadEnd = p_sourceData + width;
do
{
dummy0 = *p_preloadStart;
p_preloadStart += 16;
dummy1 = *p_preloadStart;
p_preloadStart += 16;
dummy2 = *p_preloadStart;
p_preloadStart += 16;
dummy3 = *p_preloadStart;
p_preloadStart += 16;
} while ( p_preloadStart < p_preloadEnd );
// Each step does 8 pixels ( -4 is for the first iteration of the loop
unsigned int numSteps = (halfWidth>>2);
while ( true )
{
if ( numSteps >= 8 )
{
CopyPixels( p_destData, p_sourceData );
CopyPixels( p_destData, p_sourceData );
CopyPixels( p_destData, p_sourceData );
CopyPixels( p_destData, p_sourceData );
CopyPixels( p_destData, p_sourceData );
CopyPixels( p_destData, p_sourceData );
CopyPixels( p_destData, p_sourceData );
CopyPixels( p_destData, p_sourceData );
numSteps -= 8;
}
else if ( numSteps >= 4 )
{
CopyPixels( p_destData, p_sourceData );
CopyPixels( p_destData, p_sourceData );
CopyPixels( p_destData, p_sourceData );
CopyPixels( p_destData, p_sourceData );
numSteps -= 4;
}
else if ( numSteps >= 1 )
{
CopyPixels( p_destData, p_sourceData );
numSteps -= 1;
}
else
{
break;
}
};
// Skip odd lines
p_sourceData += width;
height -= 2;
} while ( height > 0 );
return dummy0+dummy1+dummy2+dummy3;
}
Unrolling the inner loop takes this down to 1.75 (33% faster). (You might have to enable inlining in the project)
Still plenty of room for improvement there, it currently (unnecessarily) spills the width onto the stack, there is the unnecessary calculation at the bottom and thats prolly not the best way of unrolling a loop. Anyway.....