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

library_int53: range assertion warnings, comments #16879

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/library_int53.js
Original file line number Diff line number Diff line change
@@ -83,18 +83,32 @@ mergeInto(LibraryManager.library, {
// converts it to a JavaScript Number, which can represent 53 integer bits precisely.
// TODO: Add $readI53FromI64Signaling() variant.
$readI53FromI64: function(ptr) {
return HEAPU32[ptr>>2] + HEAP32[ptr+4>>2] * 4294967296;
var result = HEAPU32[ptr>>2] + HEAP32[ptr+4>>2] * 4294967296;
#if ASSERTIONS
var lo = HEAPU32[ptr>>2];
var hi = HEAP32[ptr+4>>2];
if ((result >>> 0) != (lo >>> 0)) warnOnce('readI53FromI64() resulted in rounding lo=0x' + lo.toString(16) + ', hi=0x' + hi.toString(16) + ' to 0x' + result.toString(16));
#endif
return result;
},

// Reads a 64-bit unsigned integer from the WebAssembly heap and
// converts it to a JavaScript Number, which can represent 53 integer bits precisely.
// TODO: Add $readI53FromU64Signaling() variant.
$readI53FromU64: function(ptr) {
return HEAPU32[ptr>>2] + HEAPU32[ptr+4>>2] * 4294967296;
var result = HEAPU32[ptr>>2] + HEAPU32[ptr+4>>2] * 4294967296;
#if ASSERTIONS
var lo = HEAPU32[ptr>>2];
var hi = HEAPU32[ptr+4>>2];
if ((result >>> 0) != (lo >>> 0)) warnOnce('readI53FromU64() resulted in rounding lo=0x' + lo.toString(16) + ', hi=0x' + hi.toString(16) + ' to 0x' + result.toString(16));
#endif
return result;
},

// Converts the given signed 32-bit low-high pair to a JavaScript Number that can
// represent 53 bits of precision.
// Converts the given 32-bit low-high pair to a signed JavaScript number value
// (with rounding beyond +/- 2^53).
// The result will have the sign of the high word.
// The signedness of the low word doesn't matter; it will be bit-cast to u32.
// TODO: Add $convertI32PairToI53Signaling() variant.
$convertI32PairToI53: function(lo, hi) {
#if ASSERTIONS
@@ -103,13 +117,22 @@ mergeInto(LibraryManager.library, {
// convertU32PairToI53())
assert(hi === (hi|0));
#endif
return (lo >>> 0) + hi * 4294967296;
var result = (lo >>> 0) + hi * 4294967296;
#if ASSERTIONS
if ((result >>> 0) != (lo >>> 0)) warnOnce('convertI32PairToI53() resulted in rounding lo=0x' + lo.toString(16) + ', hi=0x' + hi.toString(16) + ' to 0x' + result.toString(16));
#endif
return result;
},

// Converts the given unsigned 32-bit low-high pair to a JavaScript Number that can
// represent 53 bits of precision.
// Converts the given 32-bit low-high pair to an unsigned JavaScript number value
// (with rounding beyond 2^53).
// The signedness of each word doesn't matter; they will be bit-cast to u32.
// TODO: Add $convertU32PairToI53Signaling() variant.
$convertU32PairToI53: function(lo, hi) {
return (lo >>> 0) + (hi >>> 0) * 4294967296;
}
var result = (lo >>> 0) + (hi >>> 0) * 4294967296;
#if ASSERTIONS
if ((result >>> 0) != (lo >>> 0)) warnOnce('convertU32PairToI53() resulted in rounding (lo=0x' + lo + ', hi=0x' + hi + ') to 0x' + result.toString(16));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why wouldn't I want to warn each time this happens? Also, why not make it an assertion?

If there valid use cases for passing out-of-range values to this API, then presumably the developer will want to way to disable the warning or assertion whichever one we go with.

If there are no valid reasons then I think assertion makes more sense.

#endif
return result;
},
});