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
Signed bytes not sign extended correctly in arm builds
Re: Signed bytes not sign extended correctly in arm builds
char is for some reason an unsigned type in devkitARM, thus signed * unsigned = unsigned.
Donate to devkitPro - help us stay alive!
-
- Site Admin
- Posts: 1989
- Joined: Tue Aug 09, 2005 3:21 am
- Location: UK
- Contact:
Re: Signed bytes not sign extended correctly in arm builds
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
See http://www.network-theory.co.uk/docs/gc ... ro_71.html
Re: Signed bytes not sign extended correctly in arm builds
yes, type casting with "(signed char)" fixes the problem.
many thanks,
laser
many thanks,
laser
Who is online
Users browsing this forum: No registered users and 0 guests