-
Notifications
You must be signed in to change notification settings - Fork 854
Support i64.mul_wide_s and i64.mul_wide_u from Wide Arithmetic proposal #8652
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
Open
stevenfontanella
wants to merge
3
commits into
main
Choose a base branch
from
wide-arithmetic-mul
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright 2024 WebAssembly Community Group participants | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "support/int128.h" | ||
|
|
||
| namespace wasm { | ||
|
|
||
| #ifdef __SIZEOF_INT128__ | ||
|
|
||
| Int128 mul_wide_s(uint64_t lhs, uint64_t rhs) { | ||
| __int128 result = static_cast<__int128>(static_cast<int64_t>(lhs)) * | ||
| static_cast<__int128>(static_cast<int64_t>(rhs)); | ||
| return {static_cast<uint64_t>(result >> 64), static_cast<uint64_t>(result)}; | ||
| } | ||
|
|
||
| Int128 mul_wide_u(uint64_t lhs, uint64_t rhs) { | ||
| unsigned __int128 result = | ||
| static_cast<unsigned __int128>(lhs) * static_cast<unsigned __int128>(rhs); | ||
| return {static_cast<uint64_t>(result >> 64), static_cast<uint64_t>(result)}; | ||
| } | ||
|
|
||
| #else | ||
|
|
||
| Int128 mul_wide_s(uint64_t lhs, uint64_t rhs) { | ||
| return detail::mul_wide_s_fallback(lhs, rhs); | ||
| } | ||
|
|
||
| Int128 mul_wide_u(uint64_t lhs, uint64_t rhs) { | ||
| return detail::mul_wide_u_fallback(lhs, rhs); | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
| namespace detail { | ||
|
|
||
| Int128 mul_wide_s_fallback(uint64_t lhs, uint64_t rhs) { | ||
|
stevenfontanella marked this conversation as resolved.
|
||
| auto [high, low] = mul_wide_u_fallback(lhs, rhs); | ||
|
|
||
| if (static_cast<int64_t>(lhs) < 0) { | ||
| high -= rhs; | ||
| } | ||
| if (static_cast<int64_t>(rhs) < 0) { | ||
| high -= lhs; | ||
| } | ||
|
|
||
| return {high, low}; | ||
| } | ||
|
|
||
| Int128 mul_wide_u_fallback(uint64_t lhs, uint64_t rhs) { | ||
| uint32_t lhsLow = lhs & 0xffffffff; | ||
| uint32_t lhsHigh = lhs >> 32; | ||
| uint32_t rhsLow = rhs & 0xffffffff; | ||
| uint32_t rhsHigh = rhs >> 32; | ||
|
|
||
| uint64_t mulLowLow = static_cast<uint64_t>(lhsLow) * rhsLow; | ||
| uint64_t mulLowHigh = static_cast<uint64_t>(lhsLow) * rhsHigh; | ||
| uint64_t mulHighLow = static_cast<uint64_t>(lhsHigh) * rhsLow; | ||
| uint64_t mulHighHigh = static_cast<uint64_t>(lhsHigh) * rhsHigh; | ||
|
|
||
| uint64_t cross = mulLowHigh + (mulLowLow >> 32); | ||
| uint64_t carry = cross >> 32; | ||
| cross = (cross & 0xffffffff) + mulHighLow; | ||
|
stevenfontanella marked this conversation as resolved.
Outdated
|
||
|
|
||
| // The lower 64 bits of the 128-bit result are formed by: | ||
| // (low * low) + ((low * high) << 32) + ((high * low) << 32) | ||
| // | ||
| // The upper 64 bits of the 128-bit result are formed by: | ||
| // (high * high) + the upper 32-bits of (low * high) + the upper 32-bits of | ||
| // (high * low) + carries from the lower 64 bits | ||
| uint64_t lowResult = (cross << 32) | (mulLowLow & 0xffffffff); | ||
| uint64_t highResult = mulHighHigh + carry + (cross >> 32); | ||
|
|
||
| return {highResult, lowResult}; | ||
| } | ||
|
|
||
| } // namespace detail | ||
|
|
||
| } // namespace wasm | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Copyright 2024 WebAssembly Community Group participants | ||
|
stevenfontanella marked this conversation as resolved.
Outdated
|
||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef wasm_support_int128_h | ||
| #define wasm_support_int128_h | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| namespace wasm { | ||
|
|
||
| struct Int128 { | ||
| uint64_t high; | ||
| uint64_t low; | ||
|
|
||
| bool operator==(const Int128& other) const { | ||
| return high == other.high && low == other.low; | ||
| } | ||
| }; | ||
|
|
||
| // Computes the 128-bit product of two signed 64-bit integers. | ||
| Int128 mul_wide_s(uint64_t lhs, uint64_t rhs); | ||
|
|
||
| // Computes the 128-bit product of two unsigned 64-bit integers. | ||
| Int128 mul_wide_u(uint64_t lhs, uint64_t rhs); | ||
|
|
||
| namespace detail { | ||
| // Fallback implementations exposed for testing. | ||
| Int128 mul_wide_s_fallback(uint64_t lhs, uint64_t rhs); | ||
| Int128 mul_wide_u_fallback(uint64_t lhs, uint64_t rhs); | ||
| } // namespace detail | ||
|
|
||
| } // namespace wasm | ||
|
|
||
| #endif // wasm_support_i128_h | ||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.