Skip to content

Commit

Permalink
sleep function and limits
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed Dec 7, 2022
1 parent e2a8e9f commit 20cfc99
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Core.scope
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "LowLevel/Floats";
import "LowLevel/Limits";
import "Math/Rounding";
import "Math/Core";

Expand Down Expand Up @@ -32,6 +33,38 @@ func str input() {
ret o;
}

/%
@asm

Stops the program for @"time" seconds.
@"time" will be clamped between 0 and @"INT_MAX".
%/
func void sleep(dec time) {
// Special cases

if (time <= 0.0) {
ret;
}

if (time > INT_MAX -> dec) {
time = INT_MAX -> dec;
}

// Convert input to seconds and nanoseconds

dec rounded = floor(time);
int seconds = rounded -> int;
int nano = ((time - rounded) * 1'000'000'000.0) -> int;

// Call syscall

assembly {
vlist_get rdi, $seconds$
vlist_get rsi, $nano$
call sleep
}
}

/%
Converts @"b" into a string.

Expand Down
10 changes: 10 additions & 0 deletions LowLevel/Limits.scope
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace LowLevel;

/%
The minimum value for a variable with the type of `int`.
%/
const int INT_MIN = -9'223'372'036'854'775'808;
/%
The maximum value for a variable with the type of `int`.
%/
const int INT_MAX = 9'223'372'036'854'775'807;

0 comments on commit 20cfc99

Please sign in to comment.