Skip to content

Commit b4a145e

Browse files
author
Eric Holk
committed
Added a nanosecond timer to time.rs, support for some floating point casts, and a commandline-driven mode for pfib.rs
1 parent 441c7e0 commit b4a145e

File tree

7 files changed

+94
-32
lines changed

7 files changed

+94
-32
lines changed

src/comp/middle/trans.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5113,7 +5113,22 @@ fn trans_cast(&@block_ctxt cx, &@ast::expr e, ast::node_id id) -> result {
51135113
int_cast(e_res.bcx, lldsttype, llsrctype, e_res.val,
51145114
ty::type_is_signed(cx.fcx.lcx.ccx.tcx, t)));
51155115
}
5116-
} else { cx.fcx.lcx.ccx.sess.unimpl("fp cast"); }
5116+
}
5117+
else {
5118+
if (ty::type_is_integral(cx.fcx.lcx.ccx.tcx,
5119+
ty::expr_ty(cx.fcx.lcx.ccx.tcx, e))) {
5120+
if (ty::type_is_signed(cx.fcx.lcx.ccx.tcx,
5121+
ty::expr_ty(cx.fcx.lcx.ccx.tcx, e))) {
5122+
e_res = rslt(e_res.bcx,
5123+
e_res.bcx.build.SIToFP(e_res.val, lldsttype));
5124+
}
5125+
else {
5126+
e_res = rslt(e_res.bcx,
5127+
e_res.bcx.build.UIToFP(e_res.val, lldsttype));
5128+
}
5129+
}
5130+
else { cx.fcx.lcx.ccx.sess.unimpl("fp cast"); }
5131+
}
51175132
ret e_res;
51185133
}
51195134

src/lib/time.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
native "rust" mod rustrt {
44
fn get_time(&mutable u32 sec, &mutable u32 usec);
5+
fn nano_time(&mutable u64 ns);
56
}
67

78
type timeval = rec(u32 sec, u32 usec);
@@ -11,4 +12,14 @@ fn get_time() -> timeval {
1112
auto usec = 0u32;
1213
rustrt::get_time(sec, usec);
1314
ret rec(sec=sec, usec=usec);
14-
}
15+
}
16+
17+
fn precise_time_ns() -> u64 {
18+
auto ns = 0u64;
19+
rustrt::nano_time(ns);
20+
ret ns;
21+
}
22+
23+
fn precise_time_s() -> float {
24+
ret (precise_time_ns() as float) / 1000000000.;
25+
}

src/rt/rust_builtin.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,12 @@ get_time(rust_task *task, uint32_t *sec, uint32_t *usec) {
612612
}
613613
#endif
614614

615+
extern "C" CDECL void
616+
nano_time(rust_task *task, uint64_t *ns) {
617+
timer t;
618+
*ns = t.nano_time();
619+
}
620+
615621
/**
616622
* Preallocates the exact number of bytes in the given interior vector.
617623
*/

src/rt/rustrt.def.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ivec_on_heap
1515
ivec_reserve
1616
ivec_to_ptr
1717
last_os_error
18+
nano_time
1819
pin_task
1920
unpin_task
2021
rand_free

src/rt/sync/timer.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ timer::timer() {
99
#if __WIN32__
1010
uint64_t ticks_per_second;
1111
QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_second);
12-
_ticks_per_us = ticks_per_second / 1000000;
12+
_ticks_per_ns = ticks_per_second / 1000;
1313
#endif
1414
reset(0);
1515
}
@@ -41,26 +41,31 @@ timer::has_timed_out() {
4141
}
4242

4343
uint64_t
44-
timer::get_time() {
44+
timer::nano_time() {
4545
#ifdef __APPLE__
4646
uint64_t time = mach_absolute_time();
4747
mach_timebase_info_data_t info = {0, 0};
4848
if (info.denom == 0) {
4949
mach_timebase_info(&info);
5050
}
5151
uint64_t time_nano = time * (info.numer / info.denom);
52-
return time_nano / 1000;
52+
return time_nano;
5353
#elif __WIN32__
5454
uint64_t ticks;
5555
QueryPerformanceCounter((LARGE_INTEGER *)&ticks);
56-
return ticks / _ticks_per_us;
56+
return ticks / _ticks_per_ns;
5757
#else
5858
timespec ts;
5959
clock_gettime(CLOCK_MONOTONIC, &ts);
60-
return (ts.tv_sec * 1000000000LL + ts.tv_nsec) / 1000;
60+
return (ts.tv_sec * 1000000000LL + ts.tv_nsec);
6161
#endif
6262
}
6363

64+
uint64_t
65+
timer::get_time() {
66+
return nano_time() / 1000;
67+
}
68+
6469
timer::~timer() {
6570
// Nop.
6671
}

src/rt/sync/timer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class timer {
1111
uint64_t _timeout;
1212
uint64_t get_time();
1313
#if __WIN32__
14-
uint64_t _ticks_per_us;
14+
uint64_t _ticks_per_ns;
1515
#endif
1616
public:
1717
timer();
@@ -20,6 +20,7 @@ class timer {
2020
double get_elapsed_time_in_ms();
2121
int64_t get_timeout();
2222
bool has_timed_out();
23+
uint64_t nano_time();
2324
virtual ~timer();
2425
};
2526

src/test/bench/shootout/pfib.rs

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,61 @@
44
A parallel version of fibonacci numbers.
55
*/
66

7+
use std;
8+
9+
import std::vec;
10+
import std::uint;
11+
import std::time;
12+
import std::str;
13+
714
fn recv[T](&port[T] p) -> T {
8-
let T x;
9-
p |> x;
10-
ret x;
15+
let T x;
16+
p |> x;
17+
ret x;
1118
}
1219

1320
fn fib(int n) -> int {
14-
fn pfib(chan[int] c, int n) {
15-
if (n == 0) {
16-
c <| 0;
17-
}
18-
else if (n <= 2) {
19-
c <| 1;
20-
}
21-
else {
22-
let port[int] p = port();
21+
fn pfib(chan[int] c, int n) {
22+
if (n == 0) {
23+
c <| 0;
24+
}
25+
else if (n <= 2) {
26+
c <| 1;
27+
}
28+
else {
29+
let port[int] p = port();
2330

24-
auto t1 = spawn pfib(chan(p), n - 1);
25-
auto t2 = spawn pfib(chan(p), n - 2);
31+
auto t1 = spawn pfib(chan(p), n - 1);
32+
auto t2 = spawn pfib(chan(p), n - 2);
2633

27-
c <| recv(p) + recv(p);
34+
c <| recv(p) + recv(p);
35+
}
2836
}
29-
}
3037

31-
let port[int] p = port();
32-
auto t = spawn pfib(chan(p), n);
33-
ret recv(p);
38+
let port[int] p = port();
39+
auto t = spawn pfib(chan(p), n);
40+
ret recv(p);
3441
}
3542

36-
fn main() {
37-
assert (fib(8) == 21);
38-
assert (fib(15) == 610);
39-
log fib(8);
40-
log fib(15);
43+
fn main(vec[str] argv) {
44+
if(vec::len(argv) == 1u) {
45+
assert (fib(8) == 21);
46+
assert (fib(15) == 610);
47+
log fib(8);
48+
log fib(15);
49+
}
50+
else {
51+
// Interactive mode! Wooo!!!!
52+
53+
auto n = uint::parse_buf(str::bytes(argv.(1)), 10u) as int;
54+
auto start = time::precise_time_ns();
55+
auto fibn = fib(n);
56+
auto stop = time::precise_time_ns();
57+
58+
auto elapsed = (stop - start) as int;
59+
auto us_task = elapsed / fibn / 1000;
60+
61+
log_err #fmt("Determined that fib(%d) = %d in %d ns (%d us / task)",
62+
n, fibn, elapsed, us_task);
63+
}
4164
}

0 commit comments

Comments
 (0)