Skip to content

Vector normalization could easily be faster #87

@bvisness

Description

@bvisness

When digging into #80, I decided to see how GLM implemented vector normalization. Turns out their approach is pretty clever:

v * inversesqrt(dot(v, v));

This avoids both an actual square root and a division by exploiting the fact that inverse square roots are cheap and that dot(v, v) gives you the magnitude of the vector squared.

I ran some quickbench benchmarks with the following results:

No optimization

mvvejpkmjxu3mpknfoa1whqmzs4

-O1

0zwafdosmgwfq43wf8hckdzw6tw

-O3

myohu5vuuoqvmrfxfiz8zl9xygo

This is a pretty nice speedup for unoptimized builds (about 25%) and it could really help performance on processor architectures that hate square roots and divisions. (See concerns in #80.) So, we should probably do this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions