cl430 mangles a nested version of CLIP()
With....
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define CLIP(n, min, max) MIN((MAX((n),(min))), (max))
n = CLIP(n, 0, 4095); preprocesses to:
n = ((((((((n))>((0)))?((n)):((0)))))<((4095)))?((((((n))>((0)))?((n)):((0))))):((4095)));
which expands to:
n =
(
(
(
(
(
(
((n))>((0))
)
?
((n))
:
((0))
)
)
)
<
((4095))
)
?
(
(
((((n))>((0)))
?
((n))
:
((0)))
)
)
:
((4095))
);
and compiles to....
77 000000 0CCB MOVA r12,r11 ; [] |40|
79 000002 430F MOV.W #0,r15 ; [] |41|
80 000004 930B TST.W r11 ; [] |41|
81 000006 3801 JL $C$L1 ; [] |41|
84 000008 431F MOV.W #1,r15 ; [] |41|
86 00000a $C$L1:
87 00000a 0FCC MOVA r15,r12 ; [] |41|
88 00000c 0BCD MOVA r11,r13 ; [] |41|
93 00000e 13B0! CALLA #__mpyi ; [] |41|
000010 0000
95 000012 903C CMP.W #4095,r12 ; [] |41|
96 000016 3803 JL $C$L2 ; [] |41|
99 000018 403B MOV.W #4095,r11 ; [] |41|
100 00001c 3C05 JMP $C$L3 ; [] |41|
103 00001e $C$L2:
104 00001e 0BCC MOVA r11,r12 ; [] |41|
105 000020 0FCD MOVA r15,r13 ; [] |41|
110 000022 13B0! CALLA #__mpyi ; [] |41|
112 000026 0CCB MOVA r12,r11 ; [] |41|
114 000028 $C$L3:
which should actually work, despite the multiplies The non-nested version of the macro elicits a sane behavior.
#define CLIP(n, min, max) (((n)<(min))?(min):(((n)>(max))?(max):(n)))
..... n = (((n)<(0))?(0):(((n)>(4095))?(4095):(n))); ....
77 000000 930C TST.W r12 ; [] |41|
78 000002 3806 JL $C$L1 ; [] |41|
81 000004 903C CMP.W #4096,r12 ; [] |41|
82 000008 3804 JL $C$L2 ; [] |41|
85 00000a 403C MOV.W #4095,r12 ; [] |41|
86 00000e 3C01 JMP $C$L2 ; [] |41|
89 000010 $C$L1:
90 000010 430C MOV.W #0,r12 ; [] |41|
92 000012 $C$L2:
Lots of extra braces in the verbose expansion, but it looks legal. Compiler guys! What's up?
regards Steven Pruzina