Skip to content

Commit adfad13

Browse files
authored
fix(core): System_nanotime incorrectly returns double (#1470)
* fix(core): fix System_nanotime cast from long to double to long * feat(core): add from/to uint64_t to Double.carp for Random.carp
1 parent 3f0e4bd commit adfad13

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

core/Double.carp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
(register from-float (Fn [Float] Double))
2222
(register to-long (Fn [Double] Long))
2323
(register from-long (Fn [Long] Double))
24+
(register to-uint64 (Fn [Double] Uint64))
25+
(register from-uint64 (Fn [Uint64] Double))
2426
(register to-bytes (Fn [Double] Long))
2527
(register copy (Fn [(Ref Double)] Double))
2628

core/Random.carp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
(doc seed "seed resets the seed of the random number generator.")
1010
(defn seed []
11-
(set! s (Double.from-long (System.nanotime))))
11+
(set! s (Double.from-uint64 (System.nanotime))))
1212

1313
(doc seed-from "seed-from resets the seed of the random number generator to `new-seed`.")
1414
(defn seed-from [new-seed]

core/System.carp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
(register carp-init-globals (Fn [Int (Ptr (Ptr CChar))] ()) "carp_init_globals")
99
(doc time "Gets the current system time as an integer.")
1010
(register time (Fn [] Int))
11-
(doc nanotime "Gets the current system time in nanoseconds as a long.")
12-
(register nanotime (Fn [] Long))
11+
(doc nanotime "Gets the current system time in nanoseconds as a uint64_t.")
12+
(register nanotime (Fn [] Uint64))
1313
(doc sleep-seconds "Sleeps for a specified number of seconds.")
1414
(register sleep-seconds (Fn [Int] ()))
1515
(doc sleep-seconds "Sleeps for a specified number of microseconds.")

core/carp_double.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ double Double_from_MINUS_long(Long x) {
6060
return (double)x;
6161
}
6262

63+
uint64_t Double_to_MINUS_uint64(double x) {
64+
return (uint64_t)x;
65+
}
66+
67+
double Double_from_MINUS_uint64(uint64_t x) {
68+
return (double)x;
69+
}
70+
6371
double Double_abs(double x) {
6472
return x > 0.0 ? x : -x;
6573
}

core/carp_system.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void System_sleep_MINUS_micros(int t) {
1515
// TODO!
1616
}
1717

18-
double System_nanotime() {
18+
uint64_t System_nanotime() {
1919
return 0;
2020
}
2121
#else
@@ -27,10 +27,10 @@ void System_sleep_MINUS_micros(int t) {
2727
usleep(t);
2828
}
2929

30-
double System_nanotime() {
30+
uint64_t System_nanotime() {
3131
struct timespec tv;
3232
clock_gettime(CLOCK_REALTIME, &tv);
33-
return 1000000000 * tv.tv_sec + tv.tv_nsec;
33+
return (uint64_t)1000000000 * (uint64_t)tv.tv_sec + (uint64_t)tv.tv_nsec;
3434
}
3535
#endif
3636

0 commit comments

Comments
 (0)