short circularpointer ( int idx, int size )
short * temp = (short*) circptr ( 0, ( sizeof ( short ) * idx ) % size, 0, size );
return * temp;
uses ___urem32 instead of the signed counterpart.
Visual DSP++ 5 Update 10, Win7 x64.
dsmtoday wrote: The sizeof operator returns an unsigned value. If you replace sizeof(short) with a 1 or 2 whatever is appropriate for your processor, does the compiler then do the right thing?
The sizeof operator returns an unsigned value. If you replace sizeof(short) with a 1 or 2 whatever is appropriate for your processor, does the compiler then do the right thing?
Yep, that's the issue. As per the "usual arithmetic conversions" defined in the C standard, when mixing operands of signed and unsigned integer types, the signed operand is converted to unsigned (unless the signed type can represent all the values of the unsigned type). Hence the compiler's use of __urem32 is correct here.
Compiled for BlackFin BF535 with Visual DSP ++ 5.0 Update 10.
Working code (use of ___rem32):
template<class T> T& circular_pointer<T>:: operator ( int idx )
int offset = sizeof ( T ) * idx;
T * temp = (T*) circptr ( current_ptr,
offset % size,
Defect code (use of ___urem32):
( sizeof ( T ) * idx ) % size,
typedef signed long INT32;
/// The beginning of the buffer
T * base;
/// Current pointer location within #base
T * current_ptr;
/// #base's size in bytes
The first example (in my first post) triggers the same problem, but is much more simple.
A note to Analog Devices
I wrote this circular_pointer class as a wrapper for circptr() function/operations, to make the code cleaner while supporting operations like this:
- read indexed access
circular_pointer a ( 100 ); // 100 elements
value = a [ 0 ]; // Access currently pointed element
value = a [ -1 ]; // Access the element to the left of currently pointed element, wrap around if necessary
value = a [ 1 ]; // Access the element to the right of currently pointed element, wrap around if necessary
value = a [ 100 ]; // Access currently pointed element since exactly one wrap around occurred
- write indexed access
same requirements as by read indexed access
circular_pointer a ( 100 );
a [ 0 ] = value; // Write the currently pointed element
a [ 100 ] = value; // The same as previous since exactly one wrap around occurred
a [ -1 ] = value; // Write the element to the left of currently pointed element
- pointer access
* a = value; // Access the currently pointed element
value = * a;
- pointer increment
++ a; // These operations are different!
- pointer decrement
-- a; // These operations are different!
- add constant to a pointer
a += increment;
a = a + increment;
- subtract a constant from a pointer
a -= decrement;
a = a - decrement;
Unfortunately, this class leads to code with overhead (the compiler cannot optimize loops), so there is a tradeoff between "clean code" and speed.
Is it possible to add compiler support/optimizations to such templates?
Retrieving data ...