Skip to content

Conversation

MaxGraey
Copy link
Member

@MaxGraey MaxGraey commented Jan 11, 2020

I saw some people prefer use Math.pow(x, 2) instead x * x or x ** -0.5 instead 1 / Math.sqrt(x).

Optimally this cases should replaces during std calls simplify transform in compile time. For example LLVM have SimplifyLibCalls transform for that but our Mid-end IR still in progress. So I introduce temporary solution for that with fast paths inside Math.pow / Mathf.pow which will be removed in future.

Binaryen could sometimes reduce this functions with const second argument via DAE pass (dead argument elimination).

Fast and safe cases:

x ** 0   => 1
x ** 0.5 => sqrt(x) + special handling
x ** 1   => x
x ** 2   => x * x

x ** -0.5 => 1 / sqrt(x) + special handling
x ** -1   => 1 / x

// But we can't provide equivalent fast paths for cases presented below
// x ** -2 => 1 / (x * x) <- not safe. May loose precision
// x ** 3  => x * x * x same. May loose precision

@dcodeIO dcodeIO merged commit 38362a8 into AssemblyScript:master Jan 11, 2020
@dcodeIO
Copy link
Member

dcodeIO commented Jan 11, 2020

Thanks!

@MaxGraey MaxGraey deleted the add-fast-path-for-pow branch January 11, 2020 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants