Page 1 of 1

Signed bytes not sign extended correctly in arm builds

Posted: Tue Aug 24, 2010 9:42 am
by laser
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

Re: Signed bytes not sign extended correctly in arm builds

Posted: Tue Aug 24, 2010 9:58 am
by fincs
char is for some reason an unsigned type in devkitARM, thus signed * unsigned = unsigned.

Re: Signed bytes not sign extended correctly in arm builds

Posted: Tue Aug 24, 2010 10:07 am
by WinterMute
Not for some reason, signedness of char is platform dependent. If you require signed char then you need to ask for it explicitly.

See http://www.network-theory.co.uk/docs/gc ... ro_71.html

Re: Signed bytes not sign extended correctly in arm builds

Posted: Tue Aug 24, 2010 10:42 am
by laser
yes, type casting with "(signed char)" fixes the problem.

many thanks,

laser