Problem: use of C-style casts and magic literals

Solution: replace by static_cast, and introduce appropriate constants
This commit is contained in:
Simon Giesecke 2018-05-26 18:45:58 +02:00
parent 089746959d
commit 6ef67f13e3

View File

@ -122,12 +122,16 @@ static f_compatible_get_tick_count64 my_get_tick_count64 =
init_compatible_get_tick_count64 ();
#endif
const uint64_t usecs_per_msec = 1000;
const uint64_t usecs_per_sec = 1000000;
const uint64_t nsecs_per_usec = 1000;
zmq::clock_t::clock_t () :
last_tsc (rdtsc ()),
#ifdef ZMQ_HAVE_WINDOWS
last_time (static_cast<uint64_t> ((*my_get_tick_count64) ()))
#else
last_time (now_us () / 1000)
last_time (now_us () / usecs_per_msec)
#endif
{
}
@ -150,7 +154,8 @@ uint64_t zmq::clock_t::now_us ()
// Convert the tick number into the number of seconds
// since the system was started.
double ticks_div = ticks_per_second.QuadPart / 1000000.0;
const double ticks_div =
static_cast<double> (ticks_per_second.QuadPart) / usecs_per_sec;
return static_cast<uint64_t> (tick.QuadPart / ticks_div);
#elif defined HAVE_CLOCK_GETTIME \
@ -174,14 +179,14 @@ uint64_t zmq::clock_t::now_us ()
struct timeval tv;
int rc = gettimeofday (&tv, NULL);
errno_assert (rc == 0);
return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
return tv.tv_sec * usecs_per_sec + tv.tv_usec;
#endif
}
return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_nsec / 1000);
return tv.tv_sec * usecs_per_sec + tv.tv_nsec / nsecs_per_usec;
#elif defined HAVE_GETHRTIME
return (gethrtime () / 1000);
return gethrtime () / nsecs_per_usec;
#else
@ -189,7 +194,7 @@ uint64_t zmq::clock_t::now_us ()
struct timeval tv;
int rc = gettimeofday (&tv, NULL);
errno_assert (rc == 0);
return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
return tv.tv_sec * usecs_per_sec + tv.tv_usec;
#endif
}
@ -207,7 +212,7 @@ uint64_t zmq::clock_t::now_ms ()
// to its 32 bit limitation.
return static_cast<uint64_t> ((*my_get_tick_count64) ());
#else
return now_us () / 1000;
return now_us () / usecs_per_msec;
#endif
}
@ -221,7 +226,7 @@ uint64_t zmq::clock_t::now_ms ()
#ifdef ZMQ_HAVE_WINDOWS
last_time = static_cast<uint64_t> ((*my_get_tick_count64) ());
#else
last_time = now_us () / 1000;
last_time = now_us () / usecs_per_msec;
#endif
return last_time;
}
@ -233,7 +238,7 @@ uint64_t zmq::clock_t::rdtsc ()
#elif (defined __GNUC__ && (defined __i386__ || defined __x86_64__))
uint32_t low, high;
__asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
return (uint64_t) high << 32 | low;
return static_cast<uint64_t> (high) << 32 | low;
#elif (defined __SUNPRO_CC && (__SUNPRO_CC >= 0x5100) \
&& (defined __i386 || defined __amd64 || defined __x86_64))
union
@ -246,7 +251,7 @@ uint64_t zmq::clock_t::rdtsc ()
#elif defined(__s390__)
uint64_t tsc;
asm("\tstck\t%0\n" : "=Q"(tsc) : : "cc");
return (tsc);
return tsc;
#else
struct timespec ts;
#if defined ZMQ_HAVE_OSX \
@ -255,6 +260,7 @@ uint64_t zmq::clock_t::rdtsc ()
#else
clock_gettime (CLOCK_MONOTONIC, &ts);
#endif
return (uint64_t) (ts.tv_sec) * 1000000000 + ts.tv_nsec;
return static_cast<uint64_t> (ts.tv_sec) * nsecs_per_usec * usecs_per_sec
+ ts.tv_nsec;
#endif
}