From 3f622d6c5990a7ae80eb8e52450c309a28bbec77 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 9 Apr 2018 08:30:09 -0700 Subject: - 51e2d3690b9065da84dbbf71fb52f87149670719 Fix `ABSL_HAVE_THREAD_LOCAL` on Apple 32-bit iOS simulato... by Xiaoyi Zhang - d38332e4d6b51542a42ff49fb0fc4f4a756fdd05 Internal change. by Abseil Team GitOrigin-RevId: 51e2d3690b9065da84dbbf71fb52f87149670719 Change-Id: I9403fc6308a6c731f1c8e2cd70fdb382fafd960e --- absl/time/duration.cc | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'absl/time/duration.cc') diff --git a/absl/time/duration.cc b/absl/time/duration.cc index 5e4f93136cdb..108fa490685e 100644 --- a/absl/time/duration.cc +++ b/absl/time/duration.cc @@ -62,6 +62,7 @@ #include #include +#include "absl/base/casts.h" #include "absl/numeric/int128.h" #include "absl/time/time.h" @@ -165,14 +166,16 @@ inline Duration MakeDurationFromU128(uint128 u128, bool is_neg) { return time_internal::MakeDuration(rep_hi, rep_lo); } -// Convert int64_t to uint64_t in twos-complement system. -inline uint64_t EncodeTwosComp(int64_t v) { return static_cast(v); } - -// Convert uint64_t to int64_t in twos-complement system. -inline int64_t DecodeTwosComp(uint64_t v) { - if (v <= kint64max) return static_cast(v); - return static_cast(v - kint64max - 1) + kint64min; -} +// Convert between int64_t and uint64_t, preserving representation. This +// allows us to do arithmetic in the unsigned domain, where overflow has +// well-defined behavior. See operator+=() and operator-=(). +// +// C99 7.20.1.1.1, as referenced by C++11 18.4.1.2, says, "The typedef +// name intN_t designates a signed integer type with width N, no padding +// bits, and a two's complement representation." So, we can convert to +// and from the corresponding uint64_t value using a bit cast. +inline uint64_t EncodeTwosComp(int64_t v) { return bit_cast(v); } +inline int64_t DecodeTwosComp(uint64_t v) { return bit_cast(v); } // Note: The overflow detection in this function is done using greater/less *or // equal* because kint64max/min is too large to be represented exactly in a -- cgit 1.4.1