Skip to content

Commit

Permalink
chore(runtime): Optimize abs in runtime/bigint (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake authored Dec 31, 2023
1 parent 1a25bf0 commit e50866f
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions stdlib/runtime/bigint.gr
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,20 @@ let minu64 = (a, b) => {
if (a < b) a else b
}

// TODO(#1188): use faster abs algos
// https://stackoverflow.com/questions/664852/which-is-the-fastest-way-to-get-the-absolute-value-of-a-number

@unsafe
let absi32 = n => {
from WasmI32 use { (<), (*) }
if (n < 0n) {
n * -1n
} else {
n
}
from WasmI32 use { (>>), (^), (-) }
let temp = n >> 31n
let n = n ^ temp
n - temp
}

@unsafe
let absi64 = n => {
from WasmI64 use { (<), (*) }
if (n < 0N) {
n * -1N
} else {
n
}
from WasmI64 use { (>>), (^), (-) }
let temp = n >> 63N
let n = n ^ temp
n - temp
}

@unsafe
Expand Down

0 comments on commit e50866f

Please sign in to comment.