Signed bytes not sign extended correctly in arm builds
Posted: Tue Aug 24, 2010 9:42 am
I had troubles when multiplying signed bytes over the weekend and found it's a bug with the devkitARM compiler.
Problem is signed bytes (char) are not correctly sign extended. Try the following test program. When built with devkitARM it does not work.
When built in Visual Studio I get the correct answers. I ended up having to manually sign extend bytes in my DS program to get it to work.
*****************************************
#include <nds.h>
#include <stdio.h>
int main(void)
{
char n;
short r, a;
consoleDemoInit();
n = -10;
r = 3 * n; // ** signed bytes are not correctly signed extended **
printf("3 x -10 = %d (oh, hang on...)\n", r);
a = n;
if ((a & 0x80) != 0) // ** manually sign extend signed byte to signed short **
a |= 0xff00;
r = 3 * a;
printf("3 x -10 = %d (that's better)\n", r);
while (1)
swiWaitForVBlank();
return 0;
}
********************************
Many thanks,
laser
Problem is signed bytes (char) are not correctly sign extended. Try the following test program. When built with devkitARM it does not work.
When built in Visual Studio I get the correct answers. I ended up having to manually sign extend bytes in my DS program to get it to work.
*****************************************
#include <nds.h>
#include <stdio.h>
int main(void)
{
char n;
short r, a;
consoleDemoInit();
n = -10;
r = 3 * n; // ** signed bytes are not correctly signed extended **
printf("3 x -10 = %d (oh, hang on...)\n", r);
a = n;
if ((a & 0x80) != 0) // ** manually sign extend signed byte to signed short **
a |= 0xff00;
r = 3 * a;
printf("3 x -10 = %d (that's better)\n", r);
while (1)
swiWaitForVBlank();
return 0;
}
********************************
Many thanks,
laser