Conversation
Collaborator
|
Clever! I'm surprised by the magnitude of the speedup, but I guess that means those |
jgravelle-google
approved these changes
Oct 23, 2019
|
|
||
| // fmin etc. are not specced to be sensitive to negative zero, and LLVM does | ||
| // depend on that for optimizations, so check only the absolute value there | ||
| #define TESTS(name) \ |
Contributor
There was a problem hiding this comment.
yay macros that make things easier to read :)
Member
Author
|
I added more tests for negative zero. Wasm and musl do handle it correctly even if the libc spec doesn't require it, so nice to make sure we don't regress that. |
sunfishcode
added a commit
to WebAssembly/wasi-libc
that referenced
this pull request
Oct 25, 2019
Use wasm's builtin min and max operators to implement libc `fmin`, `fmax, `fminf`, and `fmaxf`, by handling the NaN cases explicitly. Credit to emscripten-core/emscripten#9689 for spotting this opportunity!
sunfishcode
added a commit
to WebAssembly/wasi-libc
that referenced
this pull request
Nov 8, 2019
Use wasm's builtin min and max operators to implement libc `fmin`, `fmax, `fminf`, and `fmaxf`, by handling the NaN cases explicitly. Credit to emscripten-core/emscripten#9689 for spotting this opportunity!
sunfishcode
added a commit
to WebAssembly/wasi-libc
that referenced
this pull request
Nov 20, 2019
belraquib
pushed a commit
to belraquib/emscripten
that referenced
this pull request
Dec 23, 2020
The wasm builtins are very similar to the normal libc functions, except that
nans are handled differently. Keep the musl nan handling, and otherwise use the
builtins. This is ~41 bytes less in each fmaxf etc. function, and is 30% faster
on this silly benchmark:
#include <math.h>
#include <stdio.h>
int main() {
union {
int i;
float f;
} u;
float sum = 0;
const int N = 20000;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
u.i = ((i << 15) + j + 5085) ^ j ^ (i >> 2);
sum += fmaxf(u.f, 0.5);
}
}
printf("%.2f\n", sum);
}
After the speedup we are about equal with gcc natively.
Verified this does not change the output of our tests on this, and added more
test coverage, including of negative zero which libc is not guaranteed to get
right, but the implementation actually does, and using wasm builtins preserves
that.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The wasm builtins are very similar to the normal libc functions, except that nans are handled differently. Keep the musl nan handling, and otherwise use the builtins. This is ~41 bytes less in each fmaxf etc. function, and is 30% faster on this silly benchmark:
After the speedup we are about equal with gcc natively.
Verified this does not change the output of our tests on this, and added more test coverage.
cc @sunfishcode