Hi Maarten,

> Can you please verify this also with a larger array? IIRC sdcc checks the
> array size (in bytes) and then decides if it can get away with 8 bit
> calculations.

You are right -- sdcc does check the array length:

demo{1,5,7} work for arr[255], demo{2,3,4,6} fail (my first posting was
wrong: demo2 fails, using only an 8x8->8 MUL).

demo{1,2,4,5,7} work for arr[256], demo{3,6} still fail (which might be
due to idx being less than 256, tricking sdcc into the assumption that
one byte must suffice).

The array length (number of entries) is not the right criterion -- the
array size (number of entries times size per entry) must be less than
256 for this to work AND the array must be contained completely in a
naturally aligned 256-byte block. If the array spans two or more such
blocks, a high byte of the address must be updated to catch overflows
--> proper 16-bit arithmetics required.


Best regards,

Raphael


>> SDCC (not restricted to the pic ports) seems to be buggy with
>> array indices; the problem is visible in the iCode as well.
>>
>> <code>
>> struct s { int a; int b; char c; } arr[5]; /* 255/256 tested above */
>>
>> char demo1(unsigned idx) { return arr[idx].c; }
>> char demo2(unsigned idx) { return arr[idx - 32].c; }
>>
>> char demo3(unsigned char idx) { return arr[idx].c; }
>> char demo4(unsigned char idx) { return arr[idx - 32].c; }
>> char demo5(unsigned char idx) { return arr[(unsigned)(idx - 32)].c; }
>> char demo6(unsigned char idx) { idx -= 32; return arr[idx].c; }
>> char demo7(unsigned char idx) { idx -= 32; return arr[(unsigned)idx].c;  
>> }
>> </code>
>>
>> The problem is the computation of the array index (idx * 5, 5 being
>> sizeof(struct s)), which might overflow a byte and thus should be
>> carried out on (unsigned) ints.
>>
>> demo1 and demo2 work nicely.

Nope: demo1 works nicely.
demo2 implements 8x8->8 SUB (!) and MUL and thus fails.

>> demo3 and demo4 use 8x8->8 SUB and MUL and fail.
>> demo5 implements 16x16->16 SUB (!) and MUL and works.
>> demo6 implements 8x8->8 SUB and MUL and fails.
>> demo7 implements 8x8->8 SUB and 16x16->16 MUL and works nicely.

------------------------------------------------------------------------------
Download Intel&#174; 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
_______________________________________________
Sdcc-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to