Skip to content

Commit

Permalink
math: speedup the pure V math.pow implementation for non-fractional p…
Browse files Browse the repository at this point in the history
…owers (#19270)
  • Loading branch information
Le0Developer committed Sep 4, 2023
1 parent 78659f4 commit a3fa575
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions vlib/math/ROADMAP.md
@@ -1,4 +1,4 @@
- [x] Move `vsl/vmath` to `vlib/math` as default backend
- [ ] Implement `log` in pure V
- [ ] Implement `pow` in pure V
- [x] Implement `log` in pure V
- [x] Implement `pow` in pure V
- [ ] Define functions for initial release of hardware implementations
8 changes: 7 additions & 1 deletion vlib/math/math_test.v
Expand Up @@ -776,7 +776,8 @@ fn test_pow() {
[pi, nan()], [inf(1), -pi], [inf(1), -0.0], [inf(1), 0],
[inf(1), 1], [inf(1), pi], [inf(1), nan()], [nan(), -pi],
[nan(), -0.0], [nan(), 0], [nan(), 1], [nan(), pi], [nan(),
nan()]]
nan()],
[5.0, 2.0], [5.0, 3.0], [5.0, 10.0], [5.0, -2.0], [-5.0, -2.0]]
pow_sc_ := [f64(0), // pow(-inf, -pi)
-0.0, // pow(-inf, -3)
1, // pow(-inf, -0)
Expand Down Expand Up @@ -839,6 +840,11 @@ fn test_pow() {
nan(), // pow(nan, 1)
nan(), // pow(nan, pi)
nan(), // pow(nan, nan)
25, // pow(5, 2) => 5 * 5
125, // pow(5, 3) => 5 * 5 * 5
9765625, // pow(5, 10)
0.04, // pow(5, -2)
-0.04, // pow(-5, -2)
]
for i := 0; i < vfpow_sc_.len; i++ {
f := pow(vfpow_sc_[i][0], vfpow_sc_[i][1])
Expand Down
15 changes: 15 additions & 0 deletions vlib/math/pow.v
Expand Up @@ -80,6 +80,10 @@ pub fn pow(x f64, y f64) f64 {
return x
} else if is_nan(x) || is_nan(y) {
return nan()
} else if y == 2 {
return x * x
} else if y == 3 {
return x * x * x
} else if x == 0 {
if y < 0 {
if is_odd_int(y) {
Expand Down Expand Up @@ -133,6 +137,17 @@ pub fn pow(x f64, y f64) f64 {
}
}

if yf == 0.0 {
mut result := x
for _ in 1 .. i64(yi) {
result *= x
}
if y > 0 {
return result
}
return copysign(1, x) / abs(result)
}

// ans = a1 * 2**ae (= 1 for now).
mut a1 := 1.0
mut ae := 0
Expand Down

0 comments on commit a3fa575

Please sign in to comment.