mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 16:41:07 +00:00
[platform] Fix Utils::IsInt on unsigned values.
Also add unsigned value tests for IsUint and MagnitudeIsUint to verify they don't have similar issues. TEST=vm/cc/IsInt, vm/cc/IsUint, vm/cc/MagnitudeIsUint Change-Id: I3c8dcb93b9d019117948bb5c3dac0d1d619931b9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/333083 Commit-Queue: Tess Strickland <sstrickl@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
parent
4b08a81c7d
commit
b44987450e
|
@ -300,14 +300,13 @@ class Utils {
|
|||
constexpr intptr_t value_size_in_bits = kBitsPerByte * sizeof(T);
|
||||
if constexpr (std::is_signed<T>::value) {
|
||||
if (N >= value_size_in_bits) return true; // Trivially fits.
|
||||
const T limit = static_cast<T>(1) << (N - 1);
|
||||
return (-limit <= value) && (value < limit);
|
||||
} else {
|
||||
if (N > value_size_in_bits) return true; // Trivially fits.
|
||||
if (N == value_size_in_bits) {
|
||||
return static_cast<typename std::make_signed<T>::type>(value) >= 0;
|
||||
}
|
||||
const T limit = static_cast<T>(1) << (N - 1);
|
||||
return value < limit;
|
||||
}
|
||||
const T limit = static_cast<T>(1) << (N - 1);
|
||||
return (-limit <= value) && (value < limit);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
|
|
@ -276,54 +276,97 @@ VM_UNIT_TEST_CASE(ReverseBitsWord) {
|
|||
|
||||
VM_UNIT_TEST_CASE(IsInt) {
|
||||
EXPECT(Utils::IsInt(8, 16));
|
||||
EXPECT(Utils::IsInt(8, 16U));
|
||||
EXPECT(Utils::IsInt(8, 127));
|
||||
EXPECT(Utils::IsInt(8, 127U));
|
||||
EXPECT(Utils::IsInt(8, -128));
|
||||
EXPECT(!Utils::IsInt(8, static_cast<unsigned int>(-128)));
|
||||
EXPECT(!Utils::IsInt(8, 255));
|
||||
EXPECT(!Utils::IsInt(8, 255U));
|
||||
EXPECT(Utils::IsInt(16, 16));
|
||||
EXPECT(Utils::IsInt(16, 16U));
|
||||
EXPECT(!Utils::IsInt(16, 65535));
|
||||
EXPECT(!Utils::IsInt(16, 65535U));
|
||||
EXPECT(Utils::IsInt(16, 32767));
|
||||
EXPECT(Utils::IsInt(16, 32767U));
|
||||
EXPECT(Utils::IsInt(16, -32768));
|
||||
EXPECT(!Utils::IsInt(16, static_cast<unsigned int>(-32768)));
|
||||
EXPECT(Utils::IsInt(32, 16LL));
|
||||
EXPECT(Utils::IsInt(32, 16ULL));
|
||||
EXPECT(Utils::IsInt(32, 2147483647LL));
|
||||
EXPECT(Utils::IsInt(32, 2147483647ULL));
|
||||
EXPECT(Utils::IsInt(32, -2147483648LL));
|
||||
EXPECT(!Utils::IsInt(32, static_cast<uint64_t>(-2147483648LL)));
|
||||
EXPECT(!Utils::IsInt(32, 4294967295LL));
|
||||
EXPECT(!Utils::IsInt(32, 4294967295ULL));
|
||||
}
|
||||
|
||||
VM_UNIT_TEST_CASE(IsUint) {
|
||||
EXPECT(Utils::IsUint(8, 16));
|
||||
EXPECT(Utils::IsUint(8, 16U));
|
||||
EXPECT(Utils::IsUint(8, 0));
|
||||
EXPECT(Utils::IsUint(8, 0U));
|
||||
EXPECT(Utils::IsUint(8, 255));
|
||||
EXPECT(Utils::IsUint(8, 255U));
|
||||
EXPECT(!Utils::IsUint(8, 256));
|
||||
EXPECT(!Utils::IsUint(8, 256U));
|
||||
EXPECT(Utils::IsUint(16, 16));
|
||||
EXPECT(Utils::IsUint(16, 16U));
|
||||
EXPECT(Utils::IsUint(16, 0));
|
||||
EXPECT(Utils::IsUint(16, 0U));
|
||||
EXPECT(Utils::IsUint(16, 65535));
|
||||
EXPECT(Utils::IsUint(16, 65535U));
|
||||
EXPECT(!Utils::IsUint(16, 65536));
|
||||
EXPECT(!Utils::IsUint(16, 65536U));
|
||||
EXPECT(Utils::IsUint(32, 16LL));
|
||||
EXPECT(Utils::IsUint(32, 16ULL));
|
||||
EXPECT(Utils::IsUint(32, 0LL));
|
||||
EXPECT(Utils::IsUint(32, 0ULL));
|
||||
EXPECT(Utils::IsUint(32, 4294967295LL));
|
||||
EXPECT(Utils::IsUint(32, 4294967295ULL));
|
||||
EXPECT(!Utils::IsUint(32, 4294967296LL));
|
||||
EXPECT(!Utils::IsUint(32, 4294967296ULL));
|
||||
}
|
||||
|
||||
VM_UNIT_TEST_CASE(MagnitudeIsUint) {
|
||||
EXPECT(Utils::MagnitudeIsUint(8, 16));
|
||||
EXPECT(Utils::MagnitudeIsUint(8, 16U));
|
||||
EXPECT(Utils::MagnitudeIsUint(8, 0));
|
||||
EXPECT(Utils::MagnitudeIsUint(8, 0U));
|
||||
EXPECT(Utils::MagnitudeIsUint(8, -128));
|
||||
EXPECT(!Utils::MagnitudeIsUint(8, static_cast<unsigned int>(-128)));
|
||||
EXPECT(Utils::MagnitudeIsUint(8, 255));
|
||||
EXPECT(Utils::MagnitudeIsUint(8, 255U));
|
||||
EXPECT(!Utils::MagnitudeIsUint(8, 256));
|
||||
EXPECT(!Utils::MagnitudeIsUint(8, 256U));
|
||||
EXPECT(Utils::MagnitudeIsUint(12, 4095));
|
||||
EXPECT(Utils::MagnitudeIsUint(12, 4095U));
|
||||
EXPECT(Utils::MagnitudeIsUint(12, -4095));
|
||||
EXPECT(!Utils::MagnitudeIsUint(12, static_cast<unsigned int>(-4095)));
|
||||
EXPECT(!Utils::MagnitudeIsUint(12, 4096));
|
||||
EXPECT(!Utils::MagnitudeIsUint(12, 4096U));
|
||||
EXPECT(!Utils::MagnitudeIsUint(12, -4096));
|
||||
EXPECT(!Utils::MagnitudeIsUint(12, static_cast<unsigned int>(-4096)));
|
||||
EXPECT(Utils::MagnitudeIsUint(16, 16));
|
||||
EXPECT(Utils::MagnitudeIsUint(16, 16U));
|
||||
EXPECT(Utils::MagnitudeIsUint(16, 0));
|
||||
EXPECT(Utils::MagnitudeIsUint(16, 0U));
|
||||
EXPECT(Utils::MagnitudeIsUint(16, 65535));
|
||||
EXPECT(Utils::MagnitudeIsUint(16, 65535U));
|
||||
EXPECT(Utils::MagnitudeIsUint(16, -32768));
|
||||
EXPECT(!Utils::MagnitudeIsUint(16, static_cast<unsigned int>(-32768)));
|
||||
EXPECT(!Utils::MagnitudeIsUint(16, 65536));
|
||||
EXPECT(!Utils::MagnitudeIsUint(16, 65536U));
|
||||
EXPECT(Utils::MagnitudeIsUint(32, 16LL));
|
||||
EXPECT(Utils::MagnitudeIsUint(32, 16ULL));
|
||||
EXPECT(Utils::MagnitudeIsUint(32, 0LL));
|
||||
EXPECT(Utils::MagnitudeIsUint(32, 0ULL));
|
||||
EXPECT(Utils::MagnitudeIsUint(32, -2147483648LL));
|
||||
EXPECT(!Utils::MagnitudeIsUint(32, static_cast<uint64_t>(-2147483648LL)));
|
||||
EXPECT(Utils::MagnitudeIsUint(32, 4294967295LL));
|
||||
EXPECT(Utils::MagnitudeIsUint(32, 4294967295ULL));
|
||||
EXPECT(!Utils::MagnitudeIsUint(32, 4294967296LL));
|
||||
EXPECT(!Utils::MagnitudeIsUint(32, 4294967296ULL));
|
||||
}
|
||||
|
||||
VM_UNIT_TEST_CASE(LowBits) {
|
||||
|
|
Loading…
Reference in a new issue