Skip to content

Commit

Permalink
Add abs apfloat member to apfloat
Browse files Browse the repository at this point in the history
Follows negate form.

PiperOrigin-RevId: 683438055
  • Loading branch information
jpienaar authored and copybara-github committed Oct 8, 2024
1 parent 26e490a commit 606a892
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs_src/floating_point.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ pub fn one<EXP_SZ:u32, FRACTION_SZ:u32>(sign: bits[1]) -> APFloat<EXP_SZ, FRACTI

Returns one or minus one depending upon the given sign parameter.

### `aploat::abs`

```dslx-snippet
pub fn abs<EXP_SZ:u32, FRACTION_SZ:u32>(x: APFloat<EXP_SZ, FRACTION_SZ>) -> APFloat<EXP_SZ, FRACTION_SZ>
```

Returns the absolute value of `x` unless it is a `NaN`, in which case it will
return a quiet `NaN`.

### `apfloat::negate`

```dslx-snippet
Expand Down
20 changes: 20 additions & 0 deletions xls/dslx/stdlib/apfloat.x
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ pub fn is_neg_inf<EXP_SZ: u32, FRACTION_SZ: u32>(x: APFloat<EXP_SZ, FRACTION_SZ>
is_inf(x) && x.sign
}

// Returns the absolute value of x unless it is a NaN, in which case it will
// return a quiet NaN.
pub fn abs<EXP_SZ: u32, FRACTION_SZ: u32>
(x: APFloat<EXP_SZ, FRACTION_SZ>) -> APFloat<EXP_SZ, FRACTION_SZ> {
APFloat { sign: u1:0, bexp: x.bexp, fraction: x.fraction }
}

#[test]
fn abs_test() {
let expected = APFloat<u32:8, u32:23> { sign: u1:0, bexp: u8:0x7f, fraction: u23:0x0 };
let actual =
abs<u32:8, u32:23>(APFloat<u32:8, u32:23> { sign: u1:1, bexp: u8:0x7f, fraction: u23:0x0 });
assert_eq(actual, expected);

let expected = APFloat<u32:8, u32:23> { sign: u1:0, bexp: u8:0xff, fraction: u23:0x3645A2 };
let actual = abs<u32:8, u32:23>(
APFloat<u32:8, u32:23> { sign: u1:1, bexp: u8:0xff, fraction: u23:0x3645A2 });
assert_eq(actual, expected);
}

// Returns a positive or negative zero depending upon the given sign parameter.
pub fn zero<EXP_SZ: u32, FRACTION_SZ: u32>(sign: bits[1]) -> APFloat<EXP_SZ, FRACTION_SZ> {
APFloat<EXP_SZ, FRACTION_SZ> { sign, bexp: bits[EXP_SZ]:0, fraction: bits[FRACTION_SZ]:0 }
Expand Down

0 comments on commit 606a892

Please sign in to comment.