# High-performance \`fast-powi\` crate

**URL:** <https://users.rust-lang.org/t/high-performance-fast-powi-crate/139575>\
**Category:** code review\
**Created:** [April 14, 2026, 9:28pm UTC](https://users.rust-lang.org/t/high-performance-fast-powi-crate/139575 "2026-04-14T21:28:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikem8891](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mikem8891/32/45778_2.png) [@mikem8891](https://users.rust-lang.org/u/mikem8891)\
**Post date:** [April 14, 2026, 9:28pm UTC](https://users.rust-lang.org/t/high-performance-fast-powi-crate/139575/1 "2026-04-14T21:28:02Z")

</div>

Following [Fast `powi` function](https://users.rust-lang.org/t/fast-powi-function/135166) topic, I made a [fast-powi](https://crates.io/crates/fast-powi) crate. I only implemented 8-bit exponents; i.e, `i8` and `u8`. I don't know of any practical use of exponents greater than 127. This allowed me to hard-code all the shortest "addition-chains" (or multiplication-chains in this case) for all `u8` exponents. I decided to implement the num crate numerical types. It is about 10X faster than the standard library `powi` for floats on my computer. It is about 20% faster than most ints, bigints, and ratios. It is about 50% faster for complex ratio types.

Here is a summary of the benchmarks from my laptop.

```rust
f64_fast time: [765.08 ns 772.03 ns 778.96 ns]
f64_std time: [8.1878 µs 8.2823 µs 8.3878 µs]

bigint_fast time: [70.852 µs 71.336 µs 71.814 µs]
bigint_num time: [89.324 µs 89.779 µs 90.293 µs]

big_ratio_fast time: [303.19 µs 304.39 µs 305.70 µs]
big_ratio_num time: [355.01 µs 357.22 µs 359.59 µs]

u64_fast time: [105.52 ns 107.19 ns 108.99 ns]
u64_num time: [210.32 ns 213.48 ns 218.01 ns]

big_complex_ratio_fast time: [23.048 ms 23.522 ms 24.193 ms]
big_complex_ratio_num time: [32.578 ms 32.965 ms 33.449 ms]

```

You can find the benchmark code here

> **[GitHub - mikem8891/bench-fast-powi: Benchmarks for \`fast-powi\` crate](https://github.com/mikem8891/bench-fast-powi)**
>
> Benchmarks for \`fast-powi\` crate

I probably should have ask for a review before publishing on [crate.io](http://crate.io), but I got "go fever" to publish my first crate. I would appreciate any input.

---

<div class="post-metadata">

**Author:** ![ProgramCrafter](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/programcrafter/32/45916_2.png) [@ProgramCrafter](https://users.rust-lang.org/u/ProgramCrafter)\
**Post date:** [April 14, 2026, 9:36pm UTC](https://users.rust-lang.org/t/high-performance-fast-powi-crate/139575/2 "2026-04-14T21:36:31Z")

</div>

Precision tests? (Extra points if it's more precise according to IEEE754 than standard 😀)

---

<div class="post-metadata">

**Author:** ![mikem8891](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mikem8891/32/45778_2.png) [@mikem8891](https://users.rust-lang.org/u/mikem8891)\
**Post date:** [April 14, 2026, 10:01pm UTC](https://users.rust-lang.org/t/high-performance-fast-powi-crate/139575/3 "2026-04-14T22:01:25Z")

</div>

Good question. It is comparable, but not quite as accurate.

```rust
fn main() {
    let tenth: f64 = 0.1;
    let exp: i8 = 20;
    println!("std: {tenth}^{exp} = {:e}", tenth.powi(exp as i32));
    println!("fast: {tenth}^{exp} = {:e}", tenth.powi8(exp));
}

```

```rust
std: 0.1^20 = 1.0000000000000011e-20
fast: 0.1^20 = 1.0000000000000024e-20

```

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jdahlstrom/32/38017_2.png) [@jdahlstrom](https://users.rust-lang.org/u/jdahlstrom)\
**Post date:** [April 14, 2026, 10:20pm UTC](https://users.rust-lang.org/t/high-performance-fast-powi-crate/139575/4 "2026-04-14T22:20:52Z")

</div>

This could be useful for speeding up some graphics stuff like Phong–Blinn style specular highlights which use an exponent somewhere from 20 to 100 depending on the "shininess" desired.

---

<div class="post-metadata">

**Author:** ![mikem8891](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mikem8891/32/45778_2.png) [@mikem8891](https://users.rust-lang.org/u/mikem8891)\
**Post date:** [April 23, 2026, 11:19pm UTC](https://users.rust-lang.org/t/high-performance-fast-powi-crate/139575/5 "2026-04-23T23:19:50Z")

</div>

Your right. I forgot about Phong–Blinn style specular highlights. I wrote one a few year ago while playing with WebGL, [WebGL Demo 7: Normal Map](https://mikem8891.github.io/WebGL/WebGL_07_Normal_Map.html). In my example I use a power of 256! The power is just beyond what my `fast-powi` crate can handle. The highlight intensity usually get written into the shader, so I am not sure it is applicable to for use with Rust (unless you can compile Rust into shaders, ¯\_(ツ)\_/¯ ).

> <https://github.com/mikem8891/WebGL/blob/2fc5fe01a82a9d5435193d60c6dcbff163c3466f/Demo_7/fshader.glsl#L45>

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [July 22, 2026, 11:20pm UTC](https://users.rust-lang.org/t/high-performance-fast-powi-crate/139575/6 "2026-07-22T23:20:38Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
