Skip to content

Commit 9fc0915

Browse files
richmckeevercopybara-github
authored andcommitted
Create a sign-parameterized std::pow() function and retrofit upow and spow to use it.
This way VAST-DSLX translation can stop deciding which one to use via ad hoc type inference, and in most cases, DSLX programmers would probably not need to think about it either. PiperOrigin-RevId: 811535396
1 parent 2a0514c commit 9fc0915

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

docs_src/dslx_std.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,13 +1380,17 @@ positive power of 2. This functionality is the same as
13801380
#### `std::?pow`
13811381

13821382
```dslx-snippet
1383+
pub fn pow<S: bool, N: u32>(x: xN[S][N], n: uN[N]) -> sN[N]
13831384
pub fn upow<N: u32>(x: uN[N], n: uN[N]) -> uN[N]
13841385
pub fn spow<N: u32>(x: sN[N], n: uN[N]) -> sN[N]
13851386
```
13861387

13871388
Performs integer exponentiation as in Hacker's Delight, Section 11-3. Only
13881389
non-negative exponents are allowed, hence the uN parameter for spow.
13891390

1391+
The variants with signedness in the function name predate parameterization of
1392+
signedness as a language feature, and are deprecated.
1393+
13901394
#### `std::clog2`
13911395

13921396
```dslx-snippet

xls/dslx/stdlib/std.x

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,8 @@ fn test_scmps() {
953953

954954
// Performs integer exponentiation as in Hacker's Delight, section 11-3.
955955
// Only nonnegative exponents are allowed, hence the uN parameter for spow.
956-
pub fn upow<N: u32>(x: uN[N], n: uN[N]) -> uN[N] {
957-
let result = uN[N]:1;
956+
pub fn pow<S: bool, N: u32>(x: xN[S][N], n: uN[N]) -> xN[S][N] {
957+
let result = xN[S][N]:1;
958958
let p = x;
959959

960960
let work = for (i, (n, p, result)) in u32:0..N {
@@ -965,17 +965,9 @@ pub fn upow<N: u32>(x: uN[N], n: uN[N]) -> uN[N] {
965965
work.2
966966
}
967967

968-
pub fn spow<N: u32>(x: sN[N], n: uN[N]) -> sN[N] {
969-
let result = sN[N]:1;
970-
let p = x;
968+
pub fn upow<N: u32>(x: uN[N], n: uN[N]) -> uN[N] { pow(x, n) }
971969

972-
let work = for (i, (n, p, result)): (u32, (uN[N], sN[N], sN[N])) in u32:0..N {
973-
let result = if (n & uN[N]:1) == uN[N]:1 { result * p } else { result };
974-
975-
(n >> uN[N]:1, p * p, result)
976-
}((n, p, result));
977-
work.2
978-
}
970+
pub fn spow<N: u32>(x: sN[N], n: uN[N]) -> sN[N] { pow(x, n) }
979971

980972
#[test]
981973
fn test_upow() {
@@ -995,6 +987,20 @@ fn test_spow() {
995987
assert_eq(spow(s32:1, u32:20), s32:0x1);
996988
}
997989

990+
#[test]
991+
fn test_pow() {
992+
assert_eq(pow(u32:2, u32:2), u32:4);
993+
assert_eq(pow(u32:2, u32:20), u32:0x100000);
994+
assert_eq(pow(u32:3, u32:20), u32:0xcfd41b91);
995+
assert_eq(pow(u32:1, u32:20), u32:0x1);
996+
assert_eq(pow(u32:1, u32:20), u32:0x1);
997+
assert_eq(pow(s32:2, u32:2), s32:4);
998+
assert_eq(pow(s32:2, u32:20), s32:0x100000);
999+
assert_eq(pow(s32:3, u32:20), s32:0xcfd41b91);
1000+
assert_eq(pow(s32:1, u32:20), s32:0x1);
1001+
assert_eq(pow(s32:1, u32:20), s32:0x1);
1002+
}
1003+
9981004
// Count the number of bits that are 1.
9991005
pub fn popcount<N: u32>(x: bits[N]) -> bits[N] {
10001006
let acc = for (i, acc): (u32, bits[N]) in u32:0..N {

0 commit comments

Comments
 (0)