1. ## array swap

Hi
i want to swap the contents of two 2-dimensioned arrays,without copying all the contents one by one.
i try something like this but it doesn't work :

double v[512][8192];
double v_prev[512][8192];

double (*temp)[512][8192];

temp=v;
v_prev=temp;

thanks

2. ## Re: array swap

Originally posted by netmg
Hi
i want to swap the contents of two 2-dimensioned arrays,without copying all the contents one by one.
i try something like this but it doesn't work :

double v[512][8192];
double v_prev[512][8192];

double (*temp)[512][8192];

temp=v;
v_prev=temp;

thanks
Nope, can't do that.

Set up an array
int v_index[512];
with each entry initialized to it's index
Code:
```for (j=0; j<512; j++)
{
v_index[j] = j;
}```
Then when you test v[x][y] with v[x+1][y] you would use:
Code:
```if (v[vindex[x]][y] > v[vindex[x+1]][y])
{
i = vindex[x];
vindex[x] = vindex[x+1];
vindex[x+1] = i;
}```
This way you sort the indecies instead and always reference the array thru the index array.

3. >Nope, can't do that.
Anything can be done with sufficient creativity and a little daring:
Code:
```#include <stdio.h>

void show_vector ( int v[2][5] )
{
int i, j;

for ( i = 0; i < 2; i++ ) {
for ( j = 0; j < 5; j++ )
printf ( "%d ", v[i][j] );
printf ( "\n" );
}
printf ( "\n" );
}

int main ( void )
{
struct vector { int v[2][5]; };
struct vector v = {
{
{1,2,3,4,5},
{6,7,8,9,0}
}
};
struct vector v_prev = {
{
{0,9,8,7,6},
{5,4,3,2,1}
}
};
struct vector temp;

show_vector ( v.v );
show_vector ( v_prev.v );
temp = v;
v = v_prev;
v_prev = v;
show_vector ( v.v );
show_vector ( v_prev.v );

return 0;
}```

4. Originally posted by Prelude
>Nope, can't do that.
Anything can be done with sufficient creativity and a little daring:
[/code]
OK,OK, I stand chastised. Let me rephrase:

Assuming you are new to programming, you will find it difficult and potentially obtuse to use the technique you are attempting to use. There are easier ways for the beginner, such as:
add the rest of my post here

But it can be done if you have enough experience, knowledge, and you don't like the next person that has to look at your code....

5. Depends what you mean by swap

If you're looking to just choose between two arrays, you should just go with
double v[2][512][8192];
int prev = 0; // or prev = 1 if you want the other one

Your array is typically 32MB in size, which you really don't want to be copying if at all possible.
But if you insist....
double v[512][8192];
double v_prev[512][8192];
double temp[512][8192];
memcpy( temp, v, sizeof(temp) );
memcpy( v_prev, v, sizeof(v_prev) );
memcpy( v, temp, sizeof(v) );

6. And in this case I would not use memcpy because it only copies bytes at a time.

This is assuming the arrays are linear, if not -> offset=(row*width+column)
Code:
```//Assumes that Temp is a valid pointer to an array that is the
//same size as Array1 and Array2
//Array1 and Array2 must be the same size
//ArraySize must be specified in total bytes
void CopyArray(double *Array1,double *Array2,double *Temp,unsigned long ArraySize)
{
asm {
//Convert array size to DWORDs
mov    eax,[ArraySize]
shr      eax,2

push   ds

//Copy Array1 to temp - 32 bit copy - copies DWORDs
lds      esi,[Array1]
les      edi,[Temp]
mov    ecx,eax
rep     movsd

//Copy Array2 to Array1
lds      esi,[Array2]
les      edi,[Array1]
mov    ecx,eax
rep     movsd

//Copy Temp to Array2
lds     esi,[Temp]
les     edi,[Array2]
mov   ecx,eax
rep    movsd

//Clean up
pop    ds
}
}```
But this is still a huge copy process. Each movsd only gets half of the actual value since doubles are 64-bits. But this code should be nearly twice as fast as a memcpy.

I did not use a temporary variable for the ArraySize because MSVC will undo stack protection when using temporary's and will cause the code to be slower. I used the eax register to store the size of ArraySize in DWORDs which should be faster than using the stack.

I'm fairly sure this will copy all of the data - if your ArraySize is specified in total bytes - note that 1 double 8 bytes, and movsd only moves 4 bytes at a time. On a 64-bit system you could do a movsq but we can't afford those right now.

I could construct a 64-bit copy using MMX, but MMX cannot transfer from memory to memory so it might actually be slower.