SSE With Swig

Tom Seddon sorry at no.mail.address
Thu Jul 4 17:04:11 EDT 2002


In article <uhs5g8hos5og51 at corp.supernews.com>, ajw126NO at SPAMyork.ac.uk 
says...
> Hi,
> 
> I'm trying to develop a Vector class that uses Intel's SSE extension. It
> works very well when called using c or c++, but when I wrap the class with
> Swig I get a privileged instruction exception error (this is under Windows
> 2000 using MSVC++ 6sp5 with the processor pack installed).
> 
> I've included the code below, any suggestions would be greatly appreciated
> Andrew Wilkinson

You probably need an align(16) in both declarations. Your class will 
come out at 32 bytes in size (4 bytes vtbl 12 bytes filler 16 bytes 
xyzw) but the class in the .I file is only 20 bytes in size. This might 
cause the problem. I assume swig needs to know about the class size to 
be copying the class around?

But, and here is why I'm replying even though I know next to nothing 
about swig... it is bad form to return a pointer from an overloaded 
operator, particularly when that object was allocated on the heap. Who 
deallocates it? It is quite easy to lose the object entirely, producing 
a memory leak. (See Scott Meyers' book "effective C++". The short of it 
is: whilst you can get away from returning a fresh by value object, it 
is a bad idea and will bite you up the arse.) In addition, the time 
taken by new will DWARF the time taken by the add. I don't see the point 
of using SSE here.

My suggestion is to replace your + with:

const Vector operator+(const Vector &rhs) const {
	return Vector(this->x+rhs.x,this->y+rhs.y,this->z+rhs.z,this->
w+rhs.w);
}

Then replace this with the SSE version when this proves to be the 
bottleneck. Save yourself time and effort, and let the PC do the work... 
it won't mind, that's what it's there for.

Also you have a virtual function in your Vector object. Why? This will 
increase the size of each Vector. 

-- 
-- 
--Tom
tom under score seddon snail gmx stop co stop uk
(spam paranoia)

PS effective C++ ISBN is 0-201-92488-9, worth buying.



More information about the Python-list mailing list