Ok, I agree not a problem with abs(). But from a C programmer's point of
view there is a difference in the way the msp430 is handling the
calculation/conversion. For example from my PC:
#include <stdio.h>
int main()
{
short x = -32768;
unsigned long y = -x;
printf("sizeof short =%d, x=%d, y=%lu\n", sizeof(short), x, y);
}
Output: sizeof short =2, x=-32768, y=32768
While on the msp430 it would give:
Output: sizeof short =2, x=-32768, y=4294934528
If the msp430 was treating the operation as on a short I would expect
the msp430 y variable to have a value in the range of a short as the
result.
I'm not sure why there is a difference and maybe the PC gcc is not doing
the right thing. Do you know where in the C standard it defines the
correct behavior?
Thanks,
Carl
On Tue, 2010-02-23 at 14:36 +0100, JMGross wrote:
> Hi!
>
> This one's easy.
> Looking at the stdlib.h, abs() is defined as
>
>
> extern __inline__ int abs(int __x)
> {
> return (__x < 0) ? -__x : __x;
> }
>
>
> This means, the return value is SIGNED int. And signed (short) int cannot be
> 32768.
>
> If you think that is an unfortunate implementation, you're not alone. But
> this is the C standard definition.
>
> JMGross
>
>
>
>
> --=-bic8ezq2hpUbpjqBInWI
> Content-Type: text/plain
> Content-Transfer-Encoding: 7bit
>
> Here is a sample program show the problem:
>
> #include <io.h>
> int main()
> {
> short i = -32768;
> unsigned long n = abs(i);
> unsigned long o = -i;
> LPM0;
> }
>
> Program received signal SIGINT, Interrupt.
> main () at main2.c:8
> 8 }
> (gdb) p i
> $1 = -32768
> (gdb) p n
> $2 = 4294934528
> (gdb) p o
> $3 = 4294934528
> (gdb) p /x i
> $4 = 0x8000
> (gdb) p /x n
> $5 = 0xffff8000
> (gdb) p /x o
> $6 = 0xffff8000
> (gdb)
>
> The problem occurs independent of gdb and optimization options. Attached
> is the lst output.
>
> Carl
>
>
> --=-bic8ezq2hpUbpjqBInWI
> Content-Disposition: attachment; filename=main2.lst
> Content-Type: text/plain; name=main2.lst; charset=UTF-8
> Content-Transfer-Encoding: 7bit
>
> GAS LISTING /tmp/cckfHnzP.s page 1
>
>
> 1 .file "main2.c"
> 2 .arch msp430x2618
> 3
> 4 /* Hardware multiplier registers: */
> 5 __MPY=0x130
> 6 __MPYS=0x132
> 7 __MAC=0x134
> 8 __MACS=0x136
> 9 __OP2=0x138
> 10 __RESLO=0x13a
> 11 __RESHI=0x13c
> 12 __SUMEXT=0x13e
> 13
> 21 .Ltext0:
> 22 .p2align 1,0
> 23 .global main
> 25 /***********************
> 26 * Function `main'
> 27 ***********************/
> 28 main:
> 29 .LFB0:
> 30 .LM1:
> 31 0000 3140 0000 mov #(__stack-10), r1
> 32 0004 0441 mov r1, r4
> 33 .LCFI0:
> 34 /* prologue ends here (frame size = 10) */
> 35 .L__FrameSize_main=0xa
> 36 .L__FrameOffset_main=0xc
> 37 .LM2:
> 38 0006 B440 0080 mov #llo(-32768), 8(r4)
> 38 0800
> 39 .LM3:
> 40 000c 1F44 0800 mov 8(r4), r15
> 41 0010 0F93 tst r15
> 42 0012 0234 jge .Lae9
> 43 0014 3FE3 inv r15
> 44 0016 1F53 inc r15
> 45 .Lae9:
> 46 0018 844F 0400 mov r15, 4(r4)
> 47 001c 9444 0400 mov 4(r4), 4+2(r4)
> 47 0600
> 48 0022 9454 0600 rla 4+2(r4)
> 48 0600
> 49 0028 9474 0600 subc 4+2(r4), 4+2(r4)
> 49 0600
> 50 002e B4E3 0600 inv 4+2(r4)
> 51 .LM4:
> 52 0032 1F44 0800 mov 8(r4), r15
> 53 0036 3FE3 inv r15
> 54 0038 1F53 add #1, r15
> 55 003a 844F 0000 mov r15, @r4
> 56 003e A444 0200 mov @r4, 2(r4)
> 57 0042 9454 0200 rla 2(r4)
> 57 0200
> 58 0048 9474 0200 subc 2(r4), 2(r4)
> 58 0200
> 59 004e B4E3 0200 inv 2(r4)
>
> GAS LISTING /tmp/cckfHnzP.s page 2
>
>
> 60 .LM5:
> 61 /* #APP */
> 62 ; 7 "main2.c" 1
> 63 0052 32D0 1000 bis #16, r2
> 64 ; 0 "" 2
> 65
> 66 /* epilogue: frame size = 10 */
> 67 .LM6:
> 68 /* #NOAPP */
> 69 0056 3150 0A00 add #10, r1
> 70 .LCFI1:
> 71 005a 3040 0000 br #__stop_progExec__
> 72 .LFE0:
> 73 .Lfe1:
> 75 ;; End of function
> 76
> 108 .Letext0:
>
> GAS LISTING /tmp/cckfHnzP.s page 3
>
>
> DEFINED SYMBOLS
> *ABS*:00000000 main2.c
> /tmp/cckfHnzP.s:5 *ABS*:00000130 __MPY
> /tmp/cckfHnzP.s:6 *ABS*:00000132 __MPYS
> /tmp/cckfHnzP.s:7 *ABS*:00000134 __MAC
> /tmp/cckfHnzP.s:8 *ABS*:00000136 __MACS
> /tmp/cckfHnzP.s:9 *ABS*:00000138 __OP2
> /tmp/cckfHnzP.s:10 *ABS*:0000013a __RESLO
> /tmp/cckfHnzP.s:11 *ABS*:0000013c __RESHI
> /tmp/cckfHnzP.s:12 *ABS*:0000013e __SUMEXT
> /tmp/cckfHnzP.s:28 .text:00000000 main
>
> UNDEFINED SYMBOLS
> __stack
> __stop_progExec__
>
>
>
> ------------------------------------------------------------------------------
> Download Intel® Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________
> Mspgcc-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users