seeding the pot for 2.0 features [small vectors]
Joel C. Salomon
JoelCSalomon at Gmail.com
Sun Jan 28 14:32:13 PST 2007
Mikola Lysenko wrote:
> Helix is not a bad project, but in order for a standard vector to be
> useful it needs to come packaged with the compiler. The temptation to
> roll your own is too great otherwise. Even putting it in the standard
> library is not enough to prevent people from reinventing the wheel, as
> we have seen by the numerous variations on C++'s std::list. Ideally,
> small vectors should be in the global namespace, right alongside complex
> numbers and dynamic arrays.
Hmmm… D’s complex types seem to be syntactic sugar over a construct
much like C++’s complex template. I’d think that a template library
ought to come first, with incorporation into the language later.
> Effective vector code needs correct data alignment, instruction
> scheduling and register use. Each of these issues is most effectively
> handled in the compiler/code gen stage, and therefore suggests that at
> the very least the compiler implementation ought to be aware of the
> vector type in some way. By applying the "D Builtin Rationale," it is
> easy to see that vectors meet all the required criteria.
As I understand it, D’s inline assembler would be the tool to use for
this in a library implementation. I don’t think the complex types use
SIMD, so the vectors can be the only things using those registers.
> An optimal strategy would be to have the vector types builtin, with a
> simple per-component multiplication defined, and a separate standard
> math library for doing more complex operations. Here is an example:
>
> import std.vecmath;
>
> float4 a = [1, 2, 3, 4];
> float4 b = [2, 3, 4, 5];
> float4 c = a * b; //c = [2, 6, 12, 20]
> float d = dot(a, b); //Defined in std.vecmath
Considering that the primary vector types for physics or 3-D animation
are 3-tuples (v ∊ ℝ³) and Hamiltonian quaternions (q ∊ ℍ or q ∊ ℝ⁴), and
by extension with the complex types, how about a v prefix for 3-tuples
and an h prefix for quaternions:
vfloat v = [1, 2, 3]; // vfloat ≈ float3
hreal q = [2, 3, 4, 5]; // hreal ≈ real4
and the additional operators needed to handle them “right”:
vfloat a = [1, 2, 3];
vfloat b = [2, 3, 4];
vfloat p = a*b; // element-wise product, [1*2, 2*3, 3*4]
float d = a•b; // dot product, [1*2 + 2*3 + 3*4]
vfloat c = a×b // cross product
Some notation for inverses (a postfix operator ⁻¹, perhaps) and notation
for 3×3 and 4×4 matrices would be useful too, along with defining the
dot and cross product for the complex numbers…
… or this can all go in a library until the interface and implementation
are well defined and understood.
I’m only just learning the language and I’m watching it grow.
--Joel
More information about the Digitalmars-d
mailing list