Skip to content

Some simple integer math opts#1504

Merged
kripken merged 18 commits into
masterfrom
math2
Apr 11, 2018
Merged

Some simple integer math opts#1504
kripken merged 18 commits into
masterfrom
math2

Conversation

@kripken

@kripken kripken commented Apr 10, 2018

Copy link
Copy Markdown
Member

Stuff like x + 5 != 2 => x != -3.

Also some cleanups of utility functions I noticed while writing this, isTypeFloat => isFloatType.

Inspired by

https://github.com/golang/go/blob/master/src/cmd/compile/internal/ssa/gen/generic.rules

Comment thread src/wasm/literal.cpp Outdated
switch (type) {
case Type::i32: return Literal(i32 ^ 0x80000000);
case Type::i64: return Literal(int64_t(i64 ^ 0x8000000000000000ULL));
case Type::i32: return Literal(-i32);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned previously, but both of these are potentially undefined behavior. It would be better to switch Literal to storing uint32_t and uint64_t to avoid this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good catch! Fixed.

Was there a previous discussion of this? In general though we need to cast to signed/unsigned in almost all the operations in this file (e.g. for the signed/unsigned divisions), so not sure it's worth flipping the storage to unsigned or not.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a previous discussion of this?

I thought so a while back, but I could be wrong. I just know that arbitrary operations on signed values get my UB-senses tingling. Using unsigned integers is always wrapping, so it's harder to make a mistake. But if you don't expose the representation publicly then it is probably ok.

@kripken

kripken commented Apr 11, 2018

Copy link
Copy Markdown
Member Author

Oops, fuzzer found I got a float optimization here wrong. It's not ok to change x * -1 to -x because if x is a nan, it will return x, not -x. Fixed.

@kripken kripken merged commit 92afb40 into master Apr 11, 2018
@kripken kripken deleted the math2 branch April 11, 2018 19:02
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