libnum: num8 optimizations

This commit is contained in:
Lephenixnoir 2022-07-20 00:50:36 +01:00
parent e90abaaabc
commit 967eb034f4
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
3 changed files with 37 additions and 6 deletions

View File

@ -98,16 +98,16 @@ struct num8
/* Comparisons with int */
inline constexpr bool operator==(int const &i) {
return i == 0 && v == 0;
return (v | i) == 0;
}
inline constexpr bool operator<(int const &i) {
return i >= 1;
}
inline constexpr bool operator<=(int const &i) {
return *this < i || *this == i;
return i + !v > 0;
}
inline constexpr bool operator>(int const &i) {
return i + (v == 0) <= 0;
return i + !v <= 0;
}
inline constexpr bool operator>=(int const &i) {
return i <= 0;
@ -346,8 +346,8 @@ concept is_num =
inline constexpr num8::num8(num16 n): v(n.v) {}
/* Casting to unsigned allows the use of shlr instead of shad */
inline constexpr num8::num8(num32 n): v((uint32_t)n.v >> 8) {}
/* Slightly inefficient; acceses both longwords of n.v, only one is needed */
inline constexpr num8::num8(num64 n): v(n.v >> 24) {}
/* Casting to 32-bit eliminates the unused high word */
inline constexpr num8::num8(num64 n): v((uint32_t)n.v >> 24) {}
inline constexpr num16::num16(num8 n): v(n.v) {}
/* Casting to unsigned allows the use of shlr instead of shad */

View File

@ -416,7 +416,7 @@ def printRawFunction(asm, sybl):
# Eliminate labels that are defined but unused
RE_LABEL = re.compile(r"^(\.[a-zA-Z_][a-zA-Z0-9_]*):$", re.MULTILINE)
for label in RE_LABEL.findall(func):
if func.count(f"{label}:\n") == 1:
if func.count(label) == 1:
func = func.replace(f"{label}:\n", "")
print(func.strip())

View File

@ -16,4 +16,35 @@ num8 num8_of_num32(num32 x)
return num8(x);
}
// num8_of_num64: %<=2 && %=[shlr*||shad]
num8 num8_of_num64(num64 x)
{
return num8(x);
}
/* This requires sign extensions because we care about high bits */
// num8_mul: %<=5
num8 num8_mul(num8 x, num8 y)
{
return x * y;
}
// num8_eq: [or] && ![bt* || bf*]
bool num8_eq(num8 x, int i)
{
return x == i;
}
// num8_le: ![bt* || bf*] && %<=4
bool num8_le(num8 x, int i)
{
return x <= i;
}
// num8_gt: ![bt* || bf*] && %<=4
bool num8_gt(num8 x, int i)
{
return x > i;
}
} /* extern "C" */