Urgent.News

What's breaking now, across thousands of outlets.

Tech

Faster floating point math with Rust’s new API

Floating point arithmetic, while more versatile than integer arithmetic, tends to be slower due to the compiler's conservative optimization approach. Rust, lacking a stable solution for this issue until version 1.98, now offers a way to tell the compiler to further optimize floating point code, albeit with additional control to maintain precision. This capability can be particularly beneficial when dealing with large numerical arrays, as demonstrated in the article.

The article begins with a baseline example using integers, where the compiler can generate highly optimized CPU instructions for modern x86-64 hardware. By compiling the code with the flag `RUSTFLAGS=-C target-cpu=x86-64-v3`, the Rust function to sum a slice of int64 numbers achieves an impressive speed of 0.5 CPU instructions per value. This performance is often achieved through the use of SIMD (Single Instruction, Multiple Data) CPU instructions, allowing the CPU to process multiple integers simultaneously.

However, when it comes to floating point numbers, the compiler opts for a more cautious approach due to the inherent complexities and potential rounding errors associated with their arithmetic. The article explains that floating point numbers can behave differently when added in different orders, leading the compiler to avoid reordering operations to preserve the intended order and ensure accurate results. Consequently, the floating point sum function is significantly slower compared to its integer counterpart.

The article then delves into the new algebraic arithmetic operators introduced in Rust 1.98, which allow the compiler to optimize floating point operations while adhering to the algebraic properties of real numbers. These operators enable the compiler to reorder operations where appropriate, potentially leading to faster execution.

The article provides an example of implementing a floating point sum function using these new operators, achieving a performance comparable to NumPy's implementation while maintaining the same level of precision.

In summary, Rust's new API in version 1.98 allows developers to instruct the compiler to optimize floating point operations more aggressively, leveraging SIMD instructions and algebraic arithmetic operators. This capability can significantly enhance the performance of numerical algorithms when dealing with large datasets. However, the trade-off lies in the potential for reduced precision, which must be carefully managed depending on the specific requirements of the application.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at pythonspeed.com →

More in Tech

More from Sunday 2 August →