5#if defined(HAVE_SYS_TIME_H)
9#include "internal/compilers.h"
37#define RB_HRTIME_PER_USEC ((rb_hrtime_t)1000)
38#define RB_HRTIME_PER_MSEC (RB_HRTIME_PER_USEC * (rb_hrtime_t)1000)
39#define RB_HRTIME_PER_SEC (RB_HRTIME_PER_MSEC * (rb_hrtime_t)1000)
40#define RB_HRTIME_MAX UINT64_MAX
41#define RB_HRTIME_MIN ((rb_hrtime_t)0)
47#ifdef MY_RUBY_BUILD_MAY_TIME_TRAVEL
48typedef int128_t rb_hrtime_t;
50typedef uint64_t rb_hrtime_t;
55rb_hrtime_t rb_hrtime_now(
void);
61static inline rb_hrtime_t
62rb_hrtime_mul(rb_hrtime_t a, rb_hrtime_t b)
67 if (ckd_mul(&c, a, b))
70#elif __has_builtin(__builtin_mul_overflow)
71 if (__builtin_mul_overflow(a, b, &c))
74 if (b != 0 && a > RB_HRTIME_MAX / b)
85static inline rb_hrtime_t
86rb_hrtime_add(rb_hrtime_t a, rb_hrtime_t b)
91 if (ckd_add(&c, a, b))
94#elif __has_builtin(__builtin_add_overflow)
95 if (__builtin_add_overflow(a, b, &c))
100 return RB_HRTIME_MAX;
105static inline rb_hrtime_t
106rb_hrtime_sub(rb_hrtime_t a, rb_hrtime_t b)
109 return RB_HRTIME_MIN;
117static inline rb_hrtime_t
118rb_timeval2hrtime(
const struct timeval *tv)
120 rb_hrtime_t s = rb_hrtime_mul((rb_hrtime_t)tv->tv_sec, RB_HRTIME_PER_SEC);
121 rb_hrtime_t u = rb_hrtime_mul((rb_hrtime_t)tv->tv_usec, RB_HRTIME_PER_USEC);
123 return rb_hrtime_add(s, u);
129static inline rb_hrtime_t
130rb_timespec2hrtime(
const struct timespec *ts)
132 rb_hrtime_t s = rb_hrtime_mul((rb_hrtime_t)ts->tv_sec, RB_HRTIME_PER_SEC);
134 return rb_hrtime_add(s, (rb_hrtime_t)ts->tv_nsec);
140static inline rb_hrtime_t
141rb_msec2hrtime(
unsigned long msec)
143 return rb_hrtime_mul((rb_hrtime_t)msec, RB_HRTIME_PER_MSEC);
150static inline rb_hrtime_t
151rb_sec2hrtime(time_t sec)
153 if (sec <= 0)
return 0;
155 return rb_hrtime_mul((rb_hrtime_t)sec, RB_HRTIME_PER_SEC);
163rb_hrtime2timespec(
struct timespec *ts,
const rb_hrtime_t *hrt)
166 ts->tv_sec = (time_t)(*hrt / RB_HRTIME_PER_SEC);
167 ts->tv_nsec = (int32_t)(*hrt % RB_HRTIME_PER_SEC);
178rb_hrtime2timeval(
struct timeval *tv,
const rb_hrtime_t *hrt)
181 tv->tv_sec = (time_t)(*hrt / RB_HRTIME_PER_SEC);
182 tv->tv_usec = (int32_t)((*hrt % RB_HRTIME_PER_SEC)/RB_HRTIME_PER_USEC);
189#include "internal/warnings.h"
190#include "internal/time.h"
199#define TIMESPEC_SEC_MAX TIMET_MAX
200#define TIMESPEC_SEC_MIN TIMET_MIN
203#if __has_warning("-Wimplicit-int-float-conversion")
204COMPILER_WARNING_IGNORED(-Wimplicit-
int-
float-conversion)
205#elif defined(_MSC_VER)
207COMPILER_WARNING_IGNORED(4305)
209static const double TIMESPEC_SEC_MAX_as_double = TIMESPEC_SEC_MAX;
212static inline rb_hrtime_t *
213double2hrtime(rb_hrtime_t *hrt,
double d)
216 const double TIMESPEC_SEC_MAX_PLUS_ONE = 2.0 * (TIMESPEC_SEC_MAX_as_double / 2.0 + 1.0);
218 if (TIMESPEC_SEC_MAX_PLUS_ONE <= d) {
219 *hrt = RB_HRTIME_MAX;
226 *hrt = (rb_hrtime_t)(d * (
double)RB_HRTIME_PER_SEC);
232hrtime2double(rb_hrtime_t hrt)
234 return (
double)hrt / (double)RB_HRTIME_PER_SEC;