ruby-changes:30566
From: akr <ko1@a...>
Date: Thu, 22 Aug 2013 06:28:07 +0900 (JST)
Subject: [ruby-changes:30566] akr:r42645 (trunk): * process.c (make_clock_result): Extracted from rb_clock_gettime.
akr 2013-08-22 06:27:44 +0900 (Thu, 22 Aug 2013) New Revision: 42645 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42645 Log: * process.c (make_clock_result): Extracted from rb_clock_gettime. Modified files: trunk/ChangeLog trunk/process.c Index: ChangeLog =================================================================== --- ChangeLog (revision 42644) +++ ChangeLog (revision 42645) @@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Aug 22 06:24:54 2013 Tanaka Akira <akr@f...> + + * process.c (make_clock_result): Extracted from rb_clock_gettime. + Wed Aug 21 22:30:51 2013 Tanaka Akira <akr@f...> * process.c (rb_clock_gettime): clock() based CLOCK_PROCESS_CPUTIME_ID Index: process.c =================================================================== --- process.c (revision 42644) +++ process.c (revision 42645) @@ -6659,6 +6659,53 @@ rb_proc_times(VALUE obj) https://github.com/ruby/ruby/blob/trunk/process.c#L6659 #define rb_proc_times rb_f_notimplement #endif +static VALUE +make_clock_result(struct timespec *tsp, VALUE unit) +{ + long factor; + + if (unit == ID2SYM(rb_intern("nanoseconds"))) { + factor = 1000000000; + goto return_integer; + } + else if (unit == ID2SYM(rb_intern("microseconds"))) { + factor = 1000000; + goto return_integer; + } + else if (unit == ID2SYM(rb_intern("milliseconds"))) { + factor = 1000; + goto return_integer; + } + else if (unit == ID2SYM(rb_intern("float_microseconds"))) { + factor = 1000000; + goto return_float; + } + else if (unit == ID2SYM(rb_intern("float_milliseconds"))) { + factor = 1000; + goto return_float; + } + else if (NIL_P(unit) || unit == ID2SYM(rb_intern("float_seconds"))) { + factor = 1; + goto return_float; + } + else { + rb_raise(rb_eArgError, "unexpected unit: %"PRIsVALUE, unit); + } + + return_float: + return DBL2NUM((tsp->tv_sec + 1e-9 * (double)tsp->tv_nsec) / factor); + + return_integer: +#if defined(HAVE_LONG_LONG) + if (!MUL_OVERFLOW_SIGNED_INTEGER_P(factor, (LONG_LONG)tsp->tv_sec, + LLONG_MIN, LLONG_MAX-(factor-1))) { + return LL2NUM(tsp->tv_nsec/(1000000000/factor) + factor * (LONG_LONG)tsp->tv_sec); + } +#endif + return rb_funcall(LONG2FIX(tsp->tv_nsec/(1000000000/factor)), '+', 1, + rb_funcall(LONG2FIX(factor), '*', 1, TIMET2NUM(tsp->tv_sec))); +} + /* * call-seq: * Process.clock_gettime(clock_id [, unit]) -> number @@ -6764,7 +6811,6 @@ rb_clock_gettime(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/process.c#L6811 struct timespec ts; VALUE clk_id, unit; int ret; - long factor; rb_scan_args(argc, argv, "11", &clk_id, &unit); @@ -6881,46 +6927,7 @@ rb_clock_gettime(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/process.c#L6927 rb_sys_fail(0); success: - if (unit == ID2SYM(rb_intern("nanoseconds"))) { - factor = 1000000000; - goto return_integer; - } - else if (unit == ID2SYM(rb_intern("microseconds"))) { - factor = 1000000; - goto return_integer; - } - else if (unit == ID2SYM(rb_intern("milliseconds"))) { - factor = 1000; - goto return_integer; - } - else if (unit == ID2SYM(rb_intern("float_microseconds"))) { - factor = 1000000; - goto return_float; - } - else if (unit == ID2SYM(rb_intern("float_milliseconds"))) { - factor = 1000; - goto return_float; - } - else if (NIL_P(unit) || unit == ID2SYM(rb_intern("float_seconds"))) { - factor = 1; - goto return_float; - } - else { - rb_raise(rb_eArgError, "unexpected unit: %"PRIsVALUE, unit); - } - - return_float: - return DBL2NUM((ts.tv_sec + 1e-9 * (double)ts.tv_nsec) / factor); - - return_integer: -#if defined(HAVE_LONG_LONG) - if (!MUL_OVERFLOW_SIGNED_INTEGER_P(factor, (LONG_LONG)ts.tv_sec, - LLONG_MIN, LLONG_MAX-(factor-1))) { - return LL2NUM(ts.tv_nsec/(1000000000/factor) + factor * (LONG_LONG)ts.tv_sec); - } -#endif - return rb_funcall(LONG2FIX(ts.tv_nsec/(1000000000/factor)), '+', 1, - rb_funcall(LONG2FIX(factor), '*', 1, TIMET2NUM(ts.tv_sec))); + return make_clock_result(&ts, unit); } VALUE rb_mProcess; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/