Fix style issues; Use new attribute @(cold) where appropriate in the new sync package

This commit is contained in:
gingerBill
2021-04-14 20:19:02 +01:00
parent d24784074c
commit 05a181d719
3 changed files with 88 additions and 86 deletions
+84 -84
View File
@@ -2,134 +2,134 @@ package runtime
@(link_name="__umodti3") @(link_name="__umodti3")
umodti3 :: proc "c" (a, b: u128) -> u128 { umodti3 :: proc "c" (a, b: u128) -> u128 {
r: u128 = ---; r: u128 = ---;
_ = udivmod128(a, b, &r); _ = udivmod128(a, b, &r);
return r; return r;
} }
@(link_name="__udivmodti4") @(link_name="__udivmodti4")
udivmodti4 :: proc "c" (a, b: u128, rem: ^u128) -> u128 { udivmodti4 :: proc "c" (a, b: u128, rem: ^u128) -> u128 {
return udivmod128(a, b, rem); return udivmod128(a, b, rem);
} }
@(link_name="__udivti3") @(link_name="__udivti3")
udivti3 :: proc "c" (a, b: u128) -> u128 { udivti3 :: proc "c" (a, b: u128) -> u128 {
return udivmodti4(a, b, nil); return udivmodti4(a, b, nil);
} }
@(link_name="__modti3") @(link_name="__modti3")
modti3 :: proc "c" (a, b: i128) -> i128 { modti3 :: proc "c" (a, b: i128) -> i128 {
s_a := a >> (128 - 1); s_a := a >> (128 - 1);
s_b := b >> (128 - 1); s_b := b >> (128 - 1);
an := (a ~ s_a) - s_a; an := (a ~ s_a) - s_a;
bn := (b ~ s_b) - s_b; bn := (b ~ s_b) - s_b;
r: u128 = ---; r: u128 = ---;
_ = udivmod128(transmute(u128)an, transmute(u128)bn, &r); _ = udivmod128(transmute(u128)an, transmute(u128)bn, &r);
return (transmute(i128)r ~ s_a) - s_a; return (transmute(i128)r ~ s_a) - s_a;
} }
@(link_name="__divmodti4") @(link_name="__divmodti4")
divmodti4 :: proc "c" (a, b: i128, rem: ^i128) -> i128 { divmodti4 :: proc "c" (a, b: i128, rem: ^i128) -> i128 {
u := udivmod128(transmute(u128)a, transmute(u128)b, cast(^u128)rem); u := udivmod128(transmute(u128)a, transmute(u128)b, cast(^u128)rem);
return transmute(i128)u; return transmute(i128)u;
} }
@(link_name="__divti3") @(link_name="__divti3")
divti3 :: proc "c" (a, b: i128) -> i128 { divti3 :: proc "c" (a, b: i128) -> i128 {
u := udivmodti4(transmute(u128)a, transmute(u128)b, nil); u := udivmodti4(transmute(u128)a, transmute(u128)b, nil);
return transmute(i128)u; return transmute(i128)u;
} }
@(link_name="__fixdfti") @(link_name="__fixdfti")
fixdfti :: proc(a: u64) -> i128 { fixdfti :: proc(a: u64) -> i128 {
significandBits :: 52; significandBits :: 52;
typeWidth :: (size_of(u64)*8); typeWidth :: (size_of(u64)*8);
exponentBits :: (typeWidth - significandBits - 1); exponentBits :: (typeWidth - significandBits - 1);
maxExponent :: ((1 << exponentBits) - 1); maxExponent :: ((1 << exponentBits) - 1);
exponentBias :: (maxExponent >> 1); exponentBias :: (maxExponent >> 1);
implicitBit :: (u64(1) << significandBits); implicitBit :: (u64(1) << significandBits);
significandMask :: (implicitBit - 1); significandMask :: (implicitBit - 1);
signBit :: (u64(1) << (significandBits + exponentBits)); signBit :: (u64(1) << (significandBits + exponentBits));
absMask :: (signBit - 1); absMask :: (signBit - 1);
exponentMask :: (absMask ~ significandMask); exponentMask :: (absMask ~ significandMask);
// Break a into sign, exponent, significand // Break a into sign, exponent, significand
aRep := a; aRep := a;
aAbs := aRep & absMask; aAbs := aRep & absMask;
sign := i128(-1 if aRep & signBit != 0 else 1); sign := i128(-1 if aRep & signBit != 0 else 1);
exponent := (aAbs >> significandBits) - exponentBias; exponent := (aAbs >> significandBits) - exponentBias;
significand := (aAbs & significandMask) | implicitBit; significand := (aAbs & significandMask) | implicitBit;
// If exponent is negative, the result is zero. // If exponent is negative, the result is zero.
if exponent < 0 { if exponent < 0 {
return 0; return 0;
} }
// If the value is too large for the integer type, saturate. // If the value is too large for the integer type, saturate.
if exponent >= size_of(i128) * 8 { if exponent >= size_of(i128) * 8 {
return max(i128) if sign == 1 else min(i128); return max(i128) if sign == 1 else min(i128);
} }
// If 0 <= exponent < significandBits, right shift to get the result. // If 0 <= exponent < significandBits, right shift to get the result.
// Otherwise, shift left. // Otherwise, shift left.
if exponent < significandBits { if exponent < significandBits {
return sign * i128(significand >> (significandBits - exponent)); return sign * i128(significand >> (significandBits - exponent));
} else { } else {
return sign * (i128(significand) << (exponent - significandBits)); return sign * (i128(significand) << (exponent - significandBits));
} }
} }
@(default_calling_convention = "none") @(default_calling_convention = "none")
foreign { foreign {
@(link_name="llvm.ctlz.i128") _clz_i128 :: proc(x: i128, is_zero_undef := false) -> i128 --- @(link_name="llvm.ctlz.i128") _clz_i128 :: proc(x: i128, is_zero_undef := false) -> i128 ---
} }
@(link_name="__floattidf") @(link_name="__floattidf")
floattidf :: proc(a: i128) -> f64 { floattidf :: proc(a: i128) -> f64 {
DBL_MANT_DIG :: 53; DBL_MANT_DIG :: 53;
if a == 0 { if a == 0 {
return 0.0; return 0.0;
} }
a := a; a := a;
N :: size_of(i128) * 8; N :: size_of(i128) * 8;
s := a >> (N-1); s := a >> (N-1);
a = (a ~ s) - s; a = (a ~ s) - s;
sd: = N - _clz_i128(a); // number of significant digits sd: = N - _clz_i128(a); // number of significant digits
e := u32(sd - 1); // exponent e := u32(sd - 1); // exponent
if sd > DBL_MANT_DIG { if sd > DBL_MANT_DIG {
switch sd { switch sd {
case DBL_MANT_DIG + 1: case DBL_MANT_DIG + 1:
a <<= 1; a <<= 1;
case DBL_MANT_DIG + 2: case DBL_MANT_DIG + 2:
// okay // okay
case: case:
a = i128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) | a = i128(u128(a) >> u128(sd - (DBL_MANT_DIG+2))) |
i128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0); i128(u128(a) & (~u128(0) >> u128(N + DBL_MANT_DIG+2 - sd)) != 0);
}; };
a |= i128((a & 4) != 0); a |= i128((a & 4) != 0);
a += 1; a += 1;
a >>= 2; a >>= 2;
if a & (1 << DBL_MANT_DIG) != 0 { if a & (1 << DBL_MANT_DIG) != 0 {
a >>= 1; a >>= 1;
e += 1; e += 1;
} }
} else { } else {
a <<= u128(DBL_MANT_DIG - sd); a <<= u128(DBL_MANT_DIG - sd);
} }
fb: [2]u32; fb: [2]u32;
fb[1] = (u32(s) & 0x80000000) | // sign fb[1] = (u32(s) & 0x80000000) | // sign
((e + 1023) << 20) | // exponent ((e + 1023) << 20) | // exponent
((u32(a) >> 32) & 0x000FFFFF); // mantissa-high ((u32(a) >> 32) & 0x000FFFFF); // mantissa-high
fb[1] = u32(a); // mantissa-low fb[1] = u32(a); // mantissa-low
return transmute(f64)fb; return transmute(f64)fb;
} }
+2 -1
View File
@@ -38,7 +38,7 @@ _mutex_try_lock :: proc(m: ^Mutex) -> bool {
} }
@(cold)
_mutex_lock_slow :: proc(m: ^Mutex, curr_state: _Mutex_State) { _mutex_lock_slow :: proc(m: ^Mutex, curr_state: _Mutex_State) {
new_state := curr_state; // Make a copy of it new_state := curr_state; // Make a copy of it
@@ -68,6 +68,7 @@ _mutex_lock_slow :: proc(m: ^Mutex, curr_state: _Mutex_State) {
} }
@(cold)
_mutex_unlock_slow :: proc(m: ^Mutex) { _mutex_unlock_slow :: proc(m: ^Mutex) {
// TODO(bill): Use a Futex here for Linux to improve performance and error handling // TODO(bill): Use a Futex here for Linux to improve performance and error handling
} }
+2 -1
View File
@@ -232,6 +232,7 @@ PROGRESS_CONTINUE: DWORD : 0;
ERROR_FILE_NOT_FOUND: DWORD : 2; ERROR_FILE_NOT_FOUND: DWORD : 2;
ERROR_PATH_NOT_FOUND: DWORD : 3; ERROR_PATH_NOT_FOUND: DWORD : 3;
ERROR_ACCESS_DENIED: DWORD : 5; ERROR_ACCESS_DENIED: DWORD : 5;
ERROR_NOT_ENOUGH_MEMORY: DWORD : 8;
ERROR_INVALID_HANDLE: DWORD : 6; ERROR_INVALID_HANDLE: DWORD : 6;
ERROR_NO_MORE_FILES: DWORD : 18; ERROR_NO_MORE_FILES: DWORD : 18;
ERROR_SHARING_VIOLATION: DWORD : 32; ERROR_SHARING_VIOLATION: DWORD : 32;
@@ -1233,4 +1234,4 @@ NET_API_STATUS :: enum DWORD {
PasswordTooLong = 2703, PasswordTooLong = 2703,
PasswordNotComplexEnough = 2704, PasswordNotComplexEnough = 2704,
PasswordFilterError = 2705, PasswordFilterError = 2705,
} }