# Unsafe float math by default?

**URL:** <https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472>\
**Category:** Uncategorized\
**Created:** [October 21, 2021, 12:23am UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472 "2021-10-21T00:23:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![davemilter](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/davemilter/32/3493_2.png) [@davemilter](https://internals.rust-lang.org/u/davemilter)\
**Post date:** [October 21, 2021, 12:23am UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472/1 "2021-10-21T00:23:23Z")

</div>

For such code:

```rust
pub fn f(x: f64) -> f64 {
    x.powi(3)
}

```

rustc 1.55.0 with options `-Ctarget-cpu=haswell -O` generates such assembly:

```rust
example::f:
        vmulsd xmm1, xmm0, xmm0
        vmulsd xmm0, xmm0, xmm1
        ret

```

But gcc and clang for equivalent code:

```rust
#include <cmath>

double f(double x)
{
    return std::pow(x, 3);
}

```

can generate the same assemby with only `-ffast-math` (`-O3 -ffast-math -march=haswell`).

`fast-math` is heavy unsafe option that's why it is not default on `clang` and `gcc`. So I wonder is `rustc` pass some kind of `fast-math` option to llvm that allow it generate much better code then `clang`?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [October 21, 2021, 12:40am UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472/2 "2021-10-21T00:40:27Z")

</div>

No optimization fanciness going on here, just `powi` being a special function that's different from C's `powf`. It calls [`llvm.powi.f32.i32`](https://llvm.org/docs/LangRef.html#llvm-powi-intrinsic) which is defined to use "an unspecified sequence of rounding operations".

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [October 21, 2021, 12:41am UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472/3 "2021-10-21T00:41:42Z")

</div>

I'm not surprised that `std::pow(double, double)` would have different codegen than `f64::powi(f64, i32)`, given the type difference.

---

<div class="post-metadata">

**Author:** ![Dushistov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dushistov/32/4046_2.png) [@Dushistov](https://internals.rust-lang.org/u/Dushistov)\
**Post date:** [October 21, 2021, 12:55am UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472/4 "2021-10-21T00:55:27Z")

</div>

> [@CAD97](#):
>
> I'm not surprised that `std::pow(double, double)` would have different codegen than `f64::powi(f64, i32)` , given the type difference.

I have no idea about original topic, but C++ `std::pow` is overload function (or template function depending on standard), see [std::pow, std::powf, std::powl - cppreference.com](https://en.cppreference.com/w/cpp/numeric/math/pow). And it has variant with `int` as second argument.

---

<div class="post-metadata">

**Author:** ![Dushistov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dushistov/32/4046_2.png) [@Dushistov](https://internals.rust-lang.org/u/Dushistov)\
**Post date:** [October 21, 2021, 12:56am UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472/5 "2021-10-21T00:56:12Z")

</div>

> [@scottmcm](#):
>
> different from C's `powf`

This is C++, not C, so there is overload/template that accept `int` as second argument.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [October 21, 2021, 1:20am UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472/6 "2021-10-21T01:20:34Z")

</div>

> [@Dushistov](#):
>
> so there is overload/template that accept `int` as second argument.

But does it actually call something different? From the page you linked,

> A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by 1-3). If any argument has [integral type](https://en.cppreference.com/w/cpp/types/is_integral), it is cast to double. If any argument is long double, then the return type `Promoted` is also long double, otherwise the return type is always double.

So that sounds to me like the `pow(double, int)` version just casts the `int` to a `double` and calls the `double` version.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [October 21, 2021, 1:23am UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472/7 "2021-10-21T01:23:56Z")

</div>

> [@Dushistov](#):
>
> see [std::pow, std::powf, std::powl - cppreference.com](https://en.cppreference.com/w/cpp/numeric/math/pow). And it has variant with `int` as second argument.

> [@Dushistov](#):
>
> there is overload/template that accept `int` as second argument

No, the `std::pow(double, int)` overload was removed in C++11 and replaced with

> A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by 1-3). If any argument has integral type, it is cast to `double`. If any argument is `long double`, then the return type `Promoted` is also `long double`, otherwise the return type is always `double`.

It comes down to the fact that `std::pow(double, double)` and `f64::powi(f64, i32)` have different rounding behavior, though, I believe. It's worth noting that even `x.powi(3)` and `x.powf(3.0)` in Rust generate different ASM, despite having "the same" information to optimize with, due to the fact that `powf` is required to be ±½ULP whereas `powi` is allowed to have intermediate rounding error (i.e. be expanded to a `vmul` instruction fold).

(EDIT: gah, ninja'd again by @scottmcm for doing the extra legwork and checking `f64::powf`'s codegen)

---

<div class="post-metadata">

**Author:** ![davemilter](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/davemilter/32/3493_2.png) [@davemilter](https://internals.rust-lang.org/u/davemilter)\
**Post date:** [October 21, 2021, 9:33am UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472/8 "2021-10-21T09:33:58Z")

</div>

Thanks to all.

I tried build with `-std=c++98` where there is overload with `int` as the second argument and `clang` / `gcc` produces the same code as `rustc` without enabling `fast-math`.

---

<div class="post-metadata">

**Author:** ![tspiteri](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tspiteri/32/7283_2.png) [@tspiteri](https://internals.rust-lang.org/u/tspiteri)\
**Post date:** [October 21, 2021, 1:38pm UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472/9 "2021-10-21T13:38:23Z")

</div>

`f32::powi` and `f64::powi` are equivalent to `__builtin_powi` in GCC and clang.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [January 19, 2022, 1:39pm UTC](https://internals.rust-lang.org/t/unsafe-float-math-by-default/15472/10 "2022-01-19T13:39:15Z")

</div>

Closed on January 19, 2022, 1:39pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
