PDA

Click to See Complete Forum and Search --> : keeping variables in the register


Sam Finch
Jun 9th, 2000, 10:08 PM
Hi, I'm doing some big exiting fractals, all of wich involve the use of the function.


complex newcomplex(double realpart, double imaginarypart)
{

complex retval;

retval.real=realpart;
retval.imaginary=imaginarypart;

return retval;

}

is there a way of declaring retval so that it is not assigned to memory and just stays in the registers as holding it in memory will slow it down (this function could be accessed literaly millions of times drawing a large fractal so speed is important)

i've tried




complex newcomplex(double realpart, double imaginarypart)
{

register complex retval;

retval.real=realpart;
retval.imaginary=imaginarypart;

return retval;

}

and it compiles but there's no real way of knowing if that's doing what I want, MSDN doesn't really say anything usefull.

thanks in adv

Shabble
Jun 9th, 2000, 10:51 PM
Why do you want to hold a complete class in a register? It can't be done.

By the way, in your code you're creating two complex's when you can get away with one (one of them's created on the stack, the other's created as a copy when you exit the function). That will slow it down.

Why don't you use the default copy constructor? That appears to be what you're trying to do anyway.

Sam Finch
Jun 9th, 2000, 11:18 PM
that's not exactly what I'm trying to do. complex is a struct made up of 2 doubles which is probably small enough to stor in a register. The Idea of this function is that I don't need to declare a variable to add a complex number to another one (or with other operators)

ie to add 3 + 2i to a complex number I can go



complex z;
complex temp;

// some stuff setting the value of z

temp.real = 3;
temp.imaginary = 2;

z = z + temp;




but it would be better to go



complex z;

//some stuff setting value of z

z = z + newcomplex(3,2);



without putting anything into memory, you have to do this a huge amount of times in making a fractal and if I can cut out the step of storing this in memory I would make some big speed improvements.

Shabble
Jun 10th, 2000, 05:54 PM
To be honest the overhead of calling the function is going to wipe out any time savings you could possible gain by holding the class in a register. I think you need to review how you want to implement the addition rather than trying to fix the current implementation.

For instance you could just overload the + operator to add z and (say) t which would contain (3,2)

Sam Finch
Jun 10th, 2000, 06:33 PM
sorry, I missed out the inline keyword when I copied the function across.

I've overloaded the + operator already to accept complex arguments, are you saying I can overload it to accept 3 arguments (so I can Go z = z + (3,2)) or are you saying something else.

Basicly how do I add a constant to a complex number without having to store it in memory briefly.