Moderator: Andres Valverde
mridul wrote:I dont think I understand what Uri is takling about here ... usually address size == sizeof int ... so amount of 'work' you do for both is same in both cases.
The usual idea behind passing a ptr are (from the top of my head) :
1) Passing a block of mem - the obvious use
2) to avoid a huge block push/pop - usually relevent for struct.
3) Simulating multi valued return.
Indexing array as pointer is faster (slightly) for other reasons - more so in our (chess) case since we can use shifts to calculate the offset faster (a good compiler will do the same anyway).
If you are using pointer increment/decrement style stuff - then you could become faster (leverage instruction pipelining , etc).
Are you sure of the 1% gain/loss uri ? Could be noise.
In your particular case , it should be theoretically worse to use pointer (you have to dereference it to get the value).
Maybe I am missing something here ..
Regards,
Mridul
The problems of pointers is "aliasing": for example, you are inside a function that gets an int* pointer passed. If you change the value the pointer points to, the compiler must reload all int values you use from memory again, since it does not know what the pointer points to and hence any int value could be changed.
It's very hard for compilers to deal with.
int myInt1, myInt2,.... etc.
void anycall(int *ptr){
*ptr = 1;
}
Chan Rasjid wrote:The problems of pointers is "aliasing": for example, you are inside a function that gets an int* pointer passed. If you change the value the pointer points to, the compiler must reload all int values you use from memory again, since it does not know what the pointer points to and hence any int value could be changed.
It's very hard for compilers to deal with.
Gian,
This is an advanced topic unknown to me.
Say I have global variables:-
- Code: Select all
int myInt1, myInt2,.... etc.
void anycall(int *ptr){
*ptr = 1;
}
The probem is that what ptr points to can only be kwown at runtime
and so at compile time such situations require special treatment - like
extra code, extra copy of "all int in the program..." and this may cause great runtime inefficiency.
Is there a simple /rediculous example to illustrate a worst case scenario.
Thanks
Rasjid
BitBoard pow2[64] =
{
0x0000000000000001,0x0000000000000002,0x0000000000000004,0x0000000000000008,
0x0000000000000010,0x0000000000000020,0x0000000000000040,0x0000000000000080,
0x0000000000000100,0x0000000000000200,0x0000000000000400,0x0000000000000800,
0x0000000000001000,0x0000000000002000,0x0000000000004000,0x0000000000008000,
0x0000000000010000,0x0000000000020000,0x0000000000040000,0x0000000000080000,
0x0000000000100000,0x0000000000200000,0x0000000000400000,0x0000000000800000,
0x0000000001000000,0x0000000002000000,0x0000000004000000,0x0000000008000000,
0x0000000010000000,0x0000000020000000,0x0000000040000000,0x0000000080000000,
0x0000000100000000,0x0000000200000000,0x0000000400000000,0x0000000800000000,
0x0000001000000000,0x0000002000000000,0x0000004000000000,0x0000008000000000,
0x0000010000000000,0x0000020000000000,0x0000040000000000,0x0000080000000000,
0x0000100000000000,0x0000200000000000,0x0000400000000000,0x0000800000000000,
0x0001000000000000,0x0002000000000000,0x0004000000000000,0x0008000000000000,
0x0010000000000000,0x0020000000000000,0x0040000000000000,0x0080000000000000,
0x0100000000000000,0x0200000000000000,0x0400000000000000,0x0800000000000000,
0x1000000000000000,0x2000000000000000,0x4000000000000000,0x8000000000000000
};
/* usage:
DeBruijnGenerator db;
db.genDeBruijn(6); // 1..6 2**i-i sequences
*/
class DeBruijnGenerator
{
protected:
BitBoard lck, seq;
unsigned int depth, mask;
int count;
void searchDeBruijn(unsigned int i) { // i = (int)(seq >> depth) & mask;
if ( depth > 1 ) {
unsigned int j = (i+i)&mask; // next even index
lck ^= pow2[i]; // lock current index
depth--; // sequence index right
if ( (lck & pow2[j]) == 0 ) { // next even index unlocked?
seq &= ~pow2[depth]; // "append" zero
searchDeBruijn(j); // do recursive search with next even index
}
if ( (lck & pow2[j+1]) == 0 ) { // next odd index unlocked?
seq |= pow2[depth]; // "append" one
searchDeBruijn(j+1); // do recursive search with next odd index
}
depth++; // sequence index left
lck ^= pow2[i]; // unlock current index
} else if ( (lck & pow2[(i+i+1)&mask]) == 0 ) {
deBruijnFound(seq); // bit zero was already set during initialization
count++;
}
}
virtual void deBruijnFound(BitBoard deBruijn) const {};
public:
DeBruijnGenerator() {count = 0;}
int getNoofFound() const {return count;}
void genDeBruijn(unsigned int expOfTwo) {
if ( expOfTwo > 6 ) {
printf ("%d exceeds max exponent 6 for 64/6 sequence\n", expOfTwo);
expOfTwo = 6;
}
unsigned int powOfTwo = 1 << expOfTwo;
seq = 1; // the initial sequence
lck = pow2[powOfTwo/2]; // lock last index for performance
depth = powOfTwo-expOfTwo; // initial depth for expOfTwo leading zeros
mask = powOfTwo-1; // mask
searchDeBruijn(0); // start with index 0 due to expOfTwo leading zeros
}
};
Chan Rasjid wrote:The problems of pointers is "aliasing": for example, you are inside a function that gets an int* pointer passed. If you change the value the pointer points to, the compiler must reload all int values you use from memory again, since it does not know what the pointer points to and hence any int value could be changed.
It's very hard for compilers to deal with.
Gian,
This is an advanced topic unknown to me.
Say I have global variables:-
- Code: Select all
int myInt1, myInt2,.... etc.
void anycall(int *ptr){
*ptr = 1;
}
The probem is that what ptr points to can only be kwown at runtime
and so at compile time such situations require special treatment - like
extra code, extra copy of "all int in the program..." and this may cause great runtime inefficiency.
Is there a simple /rediculous example to illustrate a worst case scenario.
Thanks
Rasjid
int myInt1, myInt2,.... etc.
void anycall(int *ptr){
int a, b;
a = myInt1;
*ptr = 1;
b = myInt1;
}
My InCheck() routine is similar in kind to TSCP's.
Now, InCheck() is in the top 2 routines when profiling [ IIRC 30-40%]
Return to Programming and Technical Discussions
Users browsing this forum: No registered users and 26 guests