Skip to content

Commit 20cfc99

Browse files
committed
sleep function and limits
1 parent e2a8e9f commit 20cfc99

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Core.scope

+33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "LowLevel/Floats";
2+
import "LowLevel/Limits";
23
import "Math/Rounding";
34
import "Math/Core";
45

@@ -32,6 +33,38 @@ func str input() {
3233
ret o;
3334
}
3435

36+
/%
37+
@asm
38+
39+
Stops the program for @"time" seconds.
40+
@"time" will be clamped between 0 and @"INT_MAX".
41+
%/
42+
func void sleep(dec time) {
43+
// Special cases
44+
45+
if (time <= 0.0) {
46+
ret;
47+
}
48+
49+
if (time > INT_MAX -> dec) {
50+
time = INT_MAX -> dec;
51+
}
52+
53+
// Convert input to seconds and nanoseconds
54+
55+
dec rounded = floor(time);
56+
int seconds = rounded -> int;
57+
int nano = ((time - rounded) * 1'000'000'000.0) -> int;
58+
59+
// Call syscall
60+
61+
assembly {
62+
vlist_get rdi, $seconds$
63+
vlist_get rsi, $nano$
64+
call sleep
65+
}
66+
}
67+
3568
/%
3669
Converts @"b" into a string.
3770

LowLevel/Limits.scope

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace LowLevel;
2+
3+
/%
4+
The minimum value for a variable with the type of `int`.
5+
%/
6+
const int INT_MIN = -9'223'372'036'854'775'808;
7+
/%
8+
The maximum value for a variable with the type of `int`.
9+
%/
10+
const int INT_MAX = 9'223'372'036'854'775'807;

0 commit comments

Comments
 (0)