views:

134

answers:

4

I'm trying to achieve something like the following in C++:

class MyVector; // 3 component vector  class

MyVector const kA = /* ... */;
MyVector const kB = /* ... */;

MyVector const kC = /* ... */;
MyVector const kD = /* ... */;


// I'd like to shorten the remaining lines, ideally making it readable but less code/operations.
MyVector result = kA;

MyVector const kCMinusD = kC - kD;

if(kCMinusD.X <= 0)
{
    result.X = kB.X;
}

if(kCMinusD.Y <= 0)
{
    result.Y = kB.Y;
}

if(kCMinusD.Z <= 0)
{
    result.Z = kB.Z;
}

Paraphrasing the code into English, I have four 'known' vectors. Two of the vectors have values that I may or may not want in my result, and whether I want them or not is contingent on a branch based on the components of two other vectors.

I feel like I should be able to simplify this code with some matrix math and masking, but I can't wrap my head around it.

For now I'm going with the branch, but I'm curious to know if there's a better way that still would be understandable, and less code-verbose.

Edit:

In reference to Mark's comment, I'll explain what I'm trying to do here.

This code is an excerpt from some spring physics I'm working on. The components are as follows:

kC is the springs length currently, and kD is minimum spring length.

kA and kB are two sets of spring tensions, each component of which may be unique per component (i.e., a different spring tension along the X, Y, or Z). kA is the springs tension if it's not fully compressed, and kB is the springs tension if it IS fully compressed.

I'd like to build up a resultant 'vector' that simply is the amalgamation of kC and kD, dependant on whether the spring is compressed or not.

+1  A: 

If your vector elements are ints, you can do:

MyVector result;
MyVector const kCMinusD = kC - kD;
int mask = kCMinusD.X >> 31;  // either 0 or -1
result.X = (kB.X & mask) | (kCMinusD.X & ~mask)
mask = kCMinusD.Y >> 31;
result.X = (kB.Y & mask) | (kCMinusD.Y & ~mask)
mask = kCMinusD.Z >> 31;
result.X = (kB.Z & mask) | (kCMinusD.Z & ~mask)

(note this handles the == 0 case differently, not sure if you care)

If your vector elements are doubles instead of ints, you can do something similar as the sign bit is in the same place, you just have to convert to integers, do the mask, and convert back.

Keith Randall
+2  A: 

Depending on the platform you're on, the compiler might be able to optimize statements like

result.x = (kC.x > kD.x) ? kA.x : kB.x;
result.y = (kC.y > kD.y) ? kA.y : kB.y;
result.z = (kC.z > kD.z) ? kA.z : kB.z;

using fsel (floating point select) instructions or conditional moves. Personally, I think the code looks nicer and more concise this way too, but that's subjective.

If the code is really performance critical, and you don't mind changing your vector class to be 4 floats instead of 3, you could use SIMD (e.g SSE on Intel platforms, VMX on PowerPC) to do the comparison and select the answers. If you went ahead with this, it would like this: (in pseudo code)

// Set each component of mask to be either 0x0 or 0xFFFFFFFF depending on the comparison
MyVector4 mask = vec_compareLessThan(kC, kD);

// Sets each component of result to either kA or kB's component, depending on whether the bits are set in mask
result = vec_select(kA, kb, mask);

This takes a while getting used to, and it might be less readable initially, but you eventually get used to thinking in SIMD mode.

The usual caveats apply, of course - don't optimize before you profile, etc.

celion
Awesome, this is the closest to what I was looking for. The code isn't necessarily performance critical (I haven't profiled it yet, anyhow), but I'd like to keep in mind improvements for this code, or similar code later.Is vec_compareLessThan a SIMD/SSE/VMX instruction? Or is that just a function you've left up to me to implement essentially?
Eddie Parker
It would be _mm_cmplt_ps in SSE (http://msdn.microsoft.com/en-us/library/f330yhc8%28VS.80%29.aspx), vec_cmplt in VMX(http://developer.apple.com/hardwaredrivers/ve/instructions/vec_cmplt.html). There isn't a single instruction for doing the vec_select in SSE (at least not SSE1, maybe a later version), but you can do it with some ANDs/ORs/NOTs. In VMX, it's vec_sel (http://developer.apple.com/hardwaredrivers/ve/instructions/vec_sel.html).
celion
Also, if you go to http://tryhavok.intel.com, you can download the Havok physics SDK that's used in a lot of games (Disclaimer: I work for Havok). There's a vector class that you can look at and see both the SSE and FPU versions.
celion
A: 

Since you are only doing subtraction you are rewrite as below:

MyVector result;
result.x = kD.x > kC.x ? kB.x : kA.x;
result.y = kD.y > kC.y ? kB.y : kA.y;
result.z = kD.z > kC.z ? kB.z : kA.z;
Ketan
+1  A: 

If you're seeking a clean expression in source more than a runtime optimization, you might consider solving this problem from the "toolbox" point of view. So let's say that on MyVector you defined sign, gt (greater-than), and le (less-than-or-equal-to). Then in two lines:

MyVector const kSignCMinusD = (kC - kD).sign();
result = kSignCMinusD.gt(0) * kA + kSignCMinusD.le(0) * kB;

With operator overloading:

MyVector const kSignCMinusD = (kC - kD).sign();
result = (kSignCMinusD > 0) * kA + (kSignCMinusD <= 0) * kB;

For inspiration here's the MatLab function reference. And obviously there are many C++ vector libraries to choose from with such functions.

You can always go in and optimize further if profiling shows it necessary. But often the biggest performance issues are how well you can see the big picture and reuse intermediate computations.

Hostile Fork