Signed bytes not sign extended correctly in arm builds

Post Reply
laser
Posts: 17
Joined: Sat Aug 14, 2010 5:49 am

Signed bytes not sign extended correctly in arm builds

Post by laser » 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

User avatar
fincs
( ͡° ͜ʖ ͡°)
Posts: 103
Joined: Mon Jul 12, 2010 9:45 pm
Location: Seville, Spain
Contact:

Re: Signed bytes not sign extended correctly in arm builds

Post by fincs » Tue Aug 24, 2010 9:58 am

char is for some reason an unsigned type in devkitARM, thus signed * unsigned = unsigned.
Donate to devkitPro - help us stay alive!

WinterMute
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

Post by WinterMute » Tue Aug 24, 2010 10:07 am

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
Help keep devkitPro toolchains free, Donate today

Personal Blog

laser
Posts: 17
Joined: Sat Aug 14, 2010 5:49 am

Re: Signed bytes not sign extended correctly in arm builds

Post by laser » Tue Aug 24, 2010 10:42 am

yes, type casting with "(signed char)" fixes the problem.

many thanks,

laser

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests