Move safe_mul_div macro and make it generic on integer types (#1334)

Move safe_mul_div macro and make it generic on integer types
This commit is contained in:
Lexi
2024-08-09 16:54:26 -04:00
committed by GitHub
parent d997445284
commit 696d39b922
3 changed files with 141 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
module std::time::os @if(env::WIN32);
import std::os::win32;
import std::math;
extern fn void win32_GetSystemTimeAsFileTime(Win32_FILETIME* time) @extern("GetSystemTimeAsFileTime");
extern fn Win32_BOOL win32_QueryPerformanceFrequency(Win32_LARGE_INTEGER* lpFrequency) @extern("QueryPerformanceFrequency");
@@ -8,10 +9,6 @@ extern fn Win32_BOOL win32_QueryPerformanceCounter(Win32_LARGE_INTEGER* lpPerfor
const ulong WINDOWS_TICK_US @local = 10;
const ulong WIN_TO_UNIX_EPOCH_US @local = 116444736000000000u64 / WINDOWS_TICK_US;
macro ulong safe_mul_div(ulong a, ulong b, ulong c)
{
return b * (a / c) + b * (a % c) / c;
}
fn Clock native_clock()
{
@@ -24,7 +21,7 @@ fn Clock native_clock()
}
Win32_LARGE_INTEGER counter @noinit;
if (!win32_QueryPerformanceCounter(&counter)) return 0;
return (Clock)safe_mul_div(counter.quadPart, 1_000_000_000, freq.quadPart);
return (Clock)counter.quadPart.mult_div(1_000_000_000, freq.quadPart);
}
fn Time native_timestamp()