Skip to content

Efficiency: use multiplation over std::pow for squaring #3373

Description

@WardBrian

For example, in prim/fun/square.hpp. Note that some care must be taken to avoid changing the rounding behavior by casting the input before multiplying

With default toolchain settings (gcc/clang default to -fmath-errno), std::pow must set errno on domain/range/overflow errors, so the optimizer cannot rewrite it to a multiply in general.
glibc's pow is a branchy multi-path implementation where x * x is one instruction.

Originally posted by @sims1253 in #3368

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions