* process.c (timetick_int_t): Renamed from timetick_giga_count_t.

(gcd_timtick_int): Renamed from gcd_ul and make the arguments
  timetick_giga_count_t.
  (reduce_fraction): Make the arguments timetick_int_t.
  (timetick2integer): Ditto.
  (make_clock_result): Ditto.
  (timetick2dblnum): Fix the return type.
  (rb_clock_gettime): Use timetick_int_t.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42668 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-08-23 12:06:02 +00:00
parent bf9ce0429f
commit f0bf7f7518
2 changed files with 52 additions and 43 deletions

View File

@ -1,3 +1,14 @@
Fri Aug 23 21:00:55 2013 Tanaka Akira <akr@fsij.org>
* process.c (timetick_int_t): Renamed from timetick_giga_count_t.
(gcd_timtick_int): Renamed from gcd_ul and make the arguments
timetick_giga_count_t.
(reduce_fraction): Make the arguments timetick_int_t.
(timetick2integer): Ditto.
(make_clock_result): Ditto.
(timetick2dblnum): Fix the return type.
(rb_clock_gettime): Use timetick_int_t.
Fri Aug 23 20:50:40 2013 Tanaka Akira <akr@fsij.org> Fri Aug 23 20:50:40 2013 Tanaka Akira <akr@fsij.org>
* process.c (gcd_ul): New function. * process.c (gcd_ul): New function.

View File

@ -6667,10 +6667,22 @@ rb_proc_times(VALUE obj)
#define rb_proc_times rb_f_notimplement #define rb_proc_times rb_f_notimplement
#endif #endif
static unsigned long #ifdef HAVE_LONG_LONG
gcd_ul(unsigned long a, unsigned long b) typedef LONG_LONG timetick_int_t;
#define TIMETICK_INT_MIN LLONG_MIN
#define TIMETICK_INT_MAX LLONG_MAX
#define TIMETICK_INT2NUM(v) LL2NUM(v)
#else
typedef long timetick_int_t;
#define TIMETICK_INT_MIN LONG_MIN
#define TIMETICK_INT_MAX LONG_MAX
#define TIMETICK_INT2NUM(v) LONG2NUM(v)
#endif
static timetick_int_t
gcd_timtick_int(timetick_int_t a, timetick_int_t b)
{ {
unsigned long t; timetick_int_t t;
if (a < b) { if (a < b) {
t = a; t = a;
@ -6688,34 +6700,22 @@ gcd_ul(unsigned long a, unsigned long b)
} }
static void static void
reduce_fraction(unsigned long *np, unsigned long *dp) reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
{ {
unsigned long gcd = gcd_ul(*np, *dp); timetick_int_t gcd = gcd_timtick_int(*np, *dp);
if (gcd != 1) { if (gcd != 1) {
*np /= gcd; *np /= gcd;
*dp /= gcd; *dp /= gcd;
} }
} }
#ifdef HAVE_LONG_LONG
typedef LONG_LONG timetick_giga_count_t;
#define TIMETICK_GIGA_COUNT_MIN LLONG_MIN
#define TIMETICK_GIGA_COUNT_MAX LLONG_MAX
#define TIMETICK_GIGA_COUNT2NUM(v) LL2NUM(v)
#else
typedef long timetick_giga_count_t;
#define TIMETICK_GIGA_COUNT_MIN LONG_MIN
#define TIMETICK_GIGA_COUNT_MAX LONG_MAX
#define TIMETICK_GIGA_COUNT2NUM(v) LONG2NUM(v)
#endif
struct timetick { struct timetick {
timetick_giga_count_t giga_count; timetick_int_t giga_count;
long count; /* 0 .. 999999999 */ int32_t count; /* 0 .. 999999999 */
}; };
static double static VALUE
timetick2dblnum(struct timetick *ttp, unsigned long numerator, unsigned long denominator, unsigned long factor) timetick2dblnum(struct timetick *ttp, timetick_int_t numerator, timetick_int_t denominator, timetick_int_t factor)
{ {
if (factor != 1 && denominator != 1) if (factor != 1 && denominator != 1)
reduce_fraction(&factor, &denominator); reduce_fraction(&factor, &denominator);
@ -6728,7 +6728,7 @@ timetick2dblnum(struct timetick *ttp, unsigned long numerator, unsigned long den
#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d)) #define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
static VALUE static VALUE
timetick2integer(struct timetick *ttp, unsigned long numerator, unsigned long denominator, unsigned long factor) timetick2integer(struct timetick *ttp, timetick_int_t numerator, timetick_int_t denominator, timetick_int_t factor)
{ {
VALUE v; VALUE v;
@ -6737,34 +6737,32 @@ timetick2integer(struct timetick *ttp, unsigned long numerator, unsigned long de
if (denominator != 1 && numerator != 1) if (denominator != 1 && numerator != 1)
reduce_fraction(&numerator, &denominator); reduce_fraction(&numerator, &denominator);
if (numerator <= LONG_MAX && factor <= LONG_MAX) { if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count, TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
TIMETICK_GIGA_COUNT_MIN, TIMETICK_GIGA_COUNT_MAX-ttp->count)) { timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
timetick_giga_count_t t = ttp->giga_count * 1000000000 + ttp->count; if (!MUL_OVERFLOW_SIGNED_INTEGER_P(numerator, t,
if (!MUL_OVERFLOW_SIGNED_INTEGER_P((long)numerator, t, TIMETICK_INT_MIN, TIMETICK_INT_MAX)) {
TIMETICK_GIGA_COUNT_MIN, TIMETICK_GIGA_COUNT_MAX)) { t *= numerator;
t *= numerator; if (!MUL_OVERFLOW_SIGNED_INTEGER_P(factor, t,
if (!MUL_OVERFLOW_SIGNED_INTEGER_P((long)factor, t, TIMETICK_INT_MIN, TIMETICK_INT_MAX)) {
TIMETICK_GIGA_COUNT_MIN, TIMETICK_GIGA_COUNT_MAX)) { t *= factor;
t *= factor; t = DIV(t, denominator);
t = DIV(t, denominator); return TIMETICK_INT2NUM(t);
return TIMETICK_GIGA_COUNT2NUM(t);
}
} }
} }
} }
v = TIMETICK_GIGA_COUNT2NUM(ttp->giga_count); v = TIMETICK_INT2NUM(ttp->giga_count);
v = rb_funcall(v, '*', 1, LONG2FIX(1000000000)); v = rb_funcall(v, '*', 1, LONG2FIX(1000000000));
v = rb_funcall(v, '+', 1, LONG2FIX(ttp->count)); v = rb_funcall(v, '+', 1, LONG2FIX(ttp->count));
v = rb_funcall(v, '*', 1, ULONG2NUM(numerator)); v = rb_funcall(v, '*', 1, TIMETICK_INT2NUM(numerator));
v = rb_funcall(v, '*', 1, ULONG2NUM(factor)); v = rb_funcall(v, '*', 1, TIMETICK_INT2NUM(factor));
v = rb_funcall(v, '/', 1, ULONG2NUM(denominator)); /* Ruby's '/' is div. */ v = rb_funcall(v, '/', 1, TIMETICK_INT2NUM(denominator)); /* Ruby's '/' is div. */
return v; return v;
} }
static VALUE static VALUE
make_clock_result(struct timetick *ttp, unsigned long numerator, unsigned long denominator, VALUE unit) make_clock_result(struct timetick *ttp, timetick_int_t numerator, timetick_int_t denominator, VALUE unit)
{ {
if (unit == ID2SYM(rb_intern("nanosecond"))) if (unit == ID2SYM(rb_intern("nanosecond")))
return timetick2integer(ttp, numerator, denominator, 1000000000); return timetick2integer(ttp, numerator, denominator, 1000000000);
@ -6892,8 +6890,8 @@ rb_clock_gettime(int argc, VALUE *argv)
int ret; int ret;
struct timetick tt; struct timetick tt;
unsigned long numerator; timetick_int_t numerator;
unsigned long denominator; timetick_int_t denominator;
rb_scan_args(argc, argv, "11", &clk_id, &unit); rb_scan_args(argc, argv, "11", &clk_id, &unit);
@ -7005,7 +7003,7 @@ rb_clock_gettime(int argc, VALUE *argv)
tt.count = t % 1000000000; tt.count = t % 1000000000;
tt.giga_count = t / 1000000000; tt.giga_count = t / 1000000000;
numerator = sTimebaseInfo.numer; numerator = sTimebaseInfo.numer;
denominator = sTimebaseInfo.denom * 1000000000; denominator = sTimebaseInfo.denom * (timetick_int_t)1000000000;
goto success; goto success;
} }
#endif #endif