dimforge / dimforge/nalgebra

`a.norm_squared()` and `a.dot(&a)` generates different code for vectors of real floats

Open
#1,264 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
4.8k
Forks
565
PR merge metrics
No merged PRs in 30d

Description

`a.norm_squared()` and `a.dot(&a)` should be equivalent, but the former generates longer machine code than the latter.

[Playground link](https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=b81bee846a4a7f29e3395c6ad14e5186)

Looking at the LLVM output, the `a.norm_squared()` case has an extra `fadd` instruction with `0.0`, [which can’t be optimized out](https://discourse.llvm.org/t/how-to-make-the-optimizer-eliminate-fadd-zeroinitializer/42097) because `(-0.0) + 0.0 = 0.0`.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.