Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1717)

Unified Diff: runtime/platform/utils.h

Issue 2974633003: Option to truncate integers to 64 bits, part 1 (core VM changes) (Closed)
Patch Set: Corrections in comments Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/platform/utils.h
diff --git a/runtime/platform/utils.h b/runtime/platform/utils.h
index a1695d4b29117a4b4d5fa9dcd6c2606897c8f484..beed22b5a55968698c887acfc5954ec99f2f2580 100644
--- a/runtime/platform/utils.h
+++ b/runtime/platform/utils.h
@@ -184,6 +184,45 @@ class Utils {
}
+ // Adds two int64_t values with wrapping around
+ // (two's complement arithmetic).
+ static inline int64_t AddWithWrapAround(int64_t a, int64_t b) {
+ // Avoid undefined behavior by doing arithmetic in the unsigned type.
+ return static_cast<int64_t>(static_cast<uint64_t>(a) +
+ static_cast<uint64_t>(b));
+ }
+
+
+ // Subtracts two int64_t values with wrapping around
+ // (two's complement arithmetic).
+ static inline int64_t SubWithWrapAround(int64_t a, int64_t b) {
+ // Avoid undefined behavior by doing arithmetic in the unsigned type.
+ return static_cast<int64_t>(static_cast<uint64_t>(a) -
+ static_cast<uint64_t>(b));
+ }
+
+
+ // Multiplies two int64_t values with wrapping around
+ // (two's complement arithmetic).
+ static inline int64_t MulWithWrapAround(int64_t a, int64_t b) {
+ // Avoid undefined behavior by doing arithmetic in the unsigned type.
+ return static_cast<int64_t>(static_cast<uint64_t>(a) *
+ static_cast<uint64_t>(b));
+ }
+
+
+ // Shifts int64_t value left. Supports any non-negative number of bits and
+ // silently discards shifted out bits.
+ static inline int64_t ShiftLeftWithTruncation(int64_t a, int64_t b) {
+ ASSERT(b >= 0);
+ if (b >= kBitsPerInt64) {
+ return 0;
+ }
+ // Avoid undefined behavior by doing arithmetic in the unsigned type.
+ return static_cast<int64_t>(static_cast<uint64_t>(a) << b);
+ }
+
+
// Utility functions for converting values from host endianness to
// big or little endian values.
static uint16_t HostToBigEndian16(uint16_t host_value);

Powered by Google App Engine
This is Rietveld 408576698