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
44 #ifdef MY_RUBY_BUILD_MAY_TIME_TRAVEL
45 typedef int128_t rb_hrtime_t;
47 typedef uint64_t rb_hrtime_t;
52 rb_hrtime_t rb_hrtime_now(
void);
58 static inline rb_hrtime_t
59 rb_hrtime_mul(rb_hrtime_t a, rb_hrtime_t b)
63 #ifdef HAVE_BUILTIN___BUILTIN_MUL_OVERFLOW
64 if (__builtin_mul_overflow(a, b, &c))
67 if (b != 0 && a > RB_HRTIME_MAX / b)
78 static inline rb_hrtime_t
79 rb_hrtime_add(rb_hrtime_t a, rb_hrtime_t b)
83 #ifdef HAVE_BUILTIN___BUILTIN_ADD_OVERFLOW
84 if (__builtin_add_overflow(a, b, &c))
97 static inline rb_hrtime_t
98 rb_timeval2hrtime(
const struct timeval *tv)
100 rb_hrtime_t s = rb_hrtime_mul((rb_hrtime_t)tv->tv_sec, RB_HRTIME_PER_SEC);
101 rb_hrtime_t u = rb_hrtime_mul((rb_hrtime_t)tv->tv_usec, RB_HRTIME_PER_USEC);
103 return rb_hrtime_add(s, u);
109 static inline rb_hrtime_t
110 rb_timespec2hrtime(
const struct timespec *ts)
112 rb_hrtime_t s = rb_hrtime_mul((rb_hrtime_t)ts->tv_sec, RB_HRTIME_PER_SEC);
114 return rb_hrtime_add(s, (rb_hrtime_t)ts->tv_nsec);
120 static inline rb_hrtime_t
121 rb_msec2hrtime(
unsigned long msec)
123 return rb_hrtime_mul((rb_hrtime_t)msec, RB_HRTIME_PER_MSEC);
130 static inline rb_hrtime_t
131 rb_sec2hrtime(time_t sec)
133 if (sec <= 0)
return 0;
135 return rb_hrtime_mul((rb_hrtime_t)sec, RB_HRTIME_PER_SEC);
143 rb_hrtime2timespec(
struct timespec *ts,
const rb_hrtime_t *hrt)
146 ts->tv_sec = (time_t)(*hrt / RB_HRTIME_PER_SEC);
147 ts->tv_nsec = (int32_t)(*hrt % RB_HRTIME_PER_SEC);
158 rb_hrtime2timeval(
struct timeval *tv,
const rb_hrtime_t *hrt)
161 tv->tv_sec = (time_t)(*hrt / RB_HRTIME_PER_SEC);
162 tv->tv_usec = (int32_t)((*hrt % RB_HRTIME_PER_SEC)/RB_HRTIME_PER_USEC);