5#if defined(HAVE_SYS_TIME_H)
35#define RB_HRTIME_PER_USEC ((rb_hrtime_t)1000)
36#define RB_HRTIME_PER_MSEC (RB_HRTIME_PER_USEC * (rb_hrtime_t)1000)
37#define RB_HRTIME_PER_SEC (RB_HRTIME_PER_MSEC * (rb_hrtime_t)1000)
38#define RB_HRTIME_MAX UINT64_MAX
39#define RB_HRTIME_MIN ((rb_hrtime_t)0)
45#ifdef MY_RUBY_BUILD_MAY_TIME_TRAVEL
46typedef int128_t rb_hrtime_t;
48typedef uint64_t rb_hrtime_t;
53rb_hrtime_t rb_hrtime_now(
void);
59static inline rb_hrtime_t
60rb_hrtime_mul(rb_hrtime_t a, rb_hrtime_t b)
64#ifdef HAVE_BUILTIN___BUILTIN_MUL_OVERFLOW
65 if (__builtin_mul_overflow(a, b, &c))
68 if (b != 0 && a > RB_HRTIME_MAX / b)
79static inline rb_hrtime_t
80rb_hrtime_add(rb_hrtime_t a, rb_hrtime_t b)
84#ifdef HAVE_BUILTIN___BUILTIN_ADD_OVERFLOW
85 if (__builtin_add_overflow(a, b, &c))
95static inline rb_hrtime_t
96rb_hrtime_sub(rb_hrtime_t a, rb_hrtime_t b)
107static inline rb_hrtime_t
108rb_timeval2hrtime(
const struct timeval *tv)
110 rb_hrtime_t s = rb_hrtime_mul((rb_hrtime_t)tv->tv_sec, RB_HRTIME_PER_SEC);
111 rb_hrtime_t u = rb_hrtime_mul((rb_hrtime_t)tv->tv_usec, RB_HRTIME_PER_USEC);
113 return rb_hrtime_add(s, u);
119static inline rb_hrtime_t
120rb_timespec2hrtime(
const struct timespec *ts)
122 rb_hrtime_t s = rb_hrtime_mul((rb_hrtime_t)ts->tv_sec, RB_HRTIME_PER_SEC);
124 return rb_hrtime_add(s, (rb_hrtime_t)ts->tv_nsec);
130static inline rb_hrtime_t
131rb_msec2hrtime(
unsigned long msec)
133 return rb_hrtime_mul((rb_hrtime_t)msec, RB_HRTIME_PER_MSEC);
140static inline rb_hrtime_t
141rb_sec2hrtime(time_t sec)
143 if (sec <= 0)
return 0;
145 return rb_hrtime_mul((rb_hrtime_t)sec, RB_HRTIME_PER_SEC);
153rb_hrtime2timespec(
struct timespec *ts,
const rb_hrtime_t *hrt)
156 ts->tv_sec = (time_t)(*hrt / RB_HRTIME_PER_SEC);
157 ts->tv_nsec = (int32_t)(*hrt % RB_HRTIME_PER_SEC);
168rb_hrtime2timeval(
struct timeval *tv,
const rb_hrtime_t *hrt)
171 tv->tv_sec = (time_t)(*hrt / RB_HRTIME_PER_SEC);
172 tv->tv_usec = (int32_t)((*hrt % RB_HRTIME_PER_SEC)/RB_HRTIME_PER_USEC);
179#include "internal/warnings.h"
180#include "internal/time.h"
189#define TIMESPEC_SEC_MAX TIMET_MAX
190#define TIMESPEC_SEC_MIN TIMET_MIN
193#if __has_warning("-Wimplicit-int-float-conversion")
194COMPILER_WARNING_IGNORED(-Wimplicit-
int-
float-conversion)
195#elif defined(_MSC_VER)
197COMPILER_WARNING_IGNORED(4305)
199static const double TIMESPEC_SEC_MAX_as_double = TIMESPEC_SEC_MAX;
202static inline rb_hrtime_t *
203double2hrtime(rb_hrtime_t *hrt,
double d)
206 const double TIMESPEC_SEC_MAX_PLUS_ONE = 2.0 * (TIMESPEC_SEC_MAX_as_double / 2.0 + 1.0);
208 if (TIMESPEC_SEC_MAX_PLUS_ONE <= d) {
209 *hrt = RB_HRTIME_MAX;
216 *hrt = (rb_hrtime_t)(d * (
double)RB_HRTIME_PER_SEC);
222hrtime2double(rb_hrtime_t hrt)
224 return (
double)hrt / (double)RB_HRTIME_PER_SEC;