Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-04-14 14:35:01

I agree that some compaction is definitely in order. In the code
for quaternions and octonions, there is lots of repetative code
such as

T at = a / rhs; // exception guard
T bt = b / rhs; // exception guard
T ct = c / rhs; // exception guard
T dt = d / rhs; // exception guard

it would be nicer to write the following instead:

for (int i = 0 i < 4; ++i)
t[i] = data[i] / rhs;

Of course, Hubert didn't use the loop because he doesn't want
the loop overhead. The loop should be unrolled all the way.
(some compilers like KAI C++ will unroll the above loop as
long as the loop count is a small compile time constant,
but many compilers will not).

However, there is another way to do this, which is used in the Blitz tiny
vec stuff and in the MTL "blais" routines. What you do is write the loop
as a recursive function, where the depth of the recursion is controlled by
a compile-time constant (through function overloading). With inlining, the
recursive functions get inlined completely, and the result is the same as
if you wrote it out by hand.

The following is an example of using this technique to write
an STL-like copy function, that copies N elements.

template <int N, class InIter, class OutIter>
inline OutIter
copy(InIter first, count<N>, OutIter result)
{
*result = *first;
return copy(++first, count<N-1>(), ++result);
}

template <class InIter, class OutIter>
inline OutIter
copy(InIter first, count<0>, OutIter result)
{
return result;
}

I've uploaded the file "fast.h" to the vault, which includes a bunch
more STL-like functions like the above.

Cheers,
Jeremy

On Sat, 14 Apr 2001, Daryle Walker wrote:
daryle>
daryle> I can think of ways to compact your code. I want to test my ideas out
daryle> first, though.

----------------------------------------------------------------------
Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
Ph.D. Candidate email: jsiek_at_[hidden]
Univ. of Notre Dame work phone: (219) 631-3906
----------------------------------------------------------------------