Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Saturation arithmetic for basic types #1472

Open
pmatyja opened this issue Feb 3, 2023 · 2 comments
Open

Proposal: Saturation arithmetic for basic types #1472

pmatyja opened this issue Feb 3, 2023 · 2 comments

Comments

@pmatyja
Copy link

pmatyja commented Feb 3, 2023

Abstract

Fundamental idea is to add new dedicated instructions for saturating arithmetic for all basic types (single input / single output) to prevent each module invent their own saturated logic algorithms and to allow the WebAssembly engines to use modern hardware instructions for such cases.

Relation to other extension

Although the SIMD extension exists that allows the use of saturated logic it may not be used in all general use cases (especially single input / single output scenarios).

Problem

Currently WebAssembly modules have no dedicated instructions for saturated arithmetic and forces all users to create their own algorithms inside the modules which when heavy used will greatly bloat the module size and/or reduce the performance especially on hardware where such hardware instructions do exists.

Proposal

Add dedicated saturation arithmetic instructions for all basic types (signed, unsigned) and if possible all type range limits (1 byte, 2 bytes, 4 bytes, 8 bytes).

Rational

"overflow" and "underflow" are a cause of many software problems that could be avoided and having a dedicated instruction would help to ensure safety in such cases. Even though this behavior could be emulated in code existence of such dedicated instructions could help engine implementors to optimize the generated code and benefit from existing hardware instructions.

Considerations

Floating-point operations that result in "infinity" and "negative infinity" can be considered as saturated logic. I do not know if there is dedicated hardware for saturated arithmetic that would result in "float min/max" values instead as well as if there is a need for such use cases and how useful they would be. I think it should be at least considered if this is possible because any operation on "+/- infinity" will result in "infinity" where "float min/max" values would act normally. But here people with more knowledge and experience would have to step in.

@pmatyja pmatyja changed the title Proposal: Saturated Arithmetic for Basic Types Proposal: Saturation arithmetic for basic types Feb 3, 2023
@tlively
Copy link
Member

tlively commented Feb 3, 2023

How well is saturating arithmetic supported by various ISAs?

@pmatyja
Copy link
Author

pmatyja commented Feb 3, 2023

Unfortunately I'm not able to provide reliably such detailed data across multiple ISAs. I think there is some support in MMX and ARM v6+ and E variants of ARMv5 but my knowledge in this area is limited. That's why I was a bit reluctant for a such proposal.

On the other hand it is difficult to produce WASM code that can run fast on any platform. Engines on the other hand knowing the underlaying hardware can exploit some behaviors and/or other hardware specific instructions to produce more performant code.

So maybe in the end dedicated instructions for such (probably "underused") but atomic operations which can add up very quickly can result in significant performance boost even if emulated in WASM engine.

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

No branches or pull requests

2 participants