diff --git a/libnum/include/num/num.h b/libnum/include/num/num.h index a5a257f..d5a132a 100644 --- a/libnum/include/num/num.h +++ b/libnum/include/num/num.h @@ -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 */ diff --git a/libnum/test/isel.py b/libnum/test/isel.py index b3be802..9135954 100644 --- a/libnum/test/isel.py +++ b/libnum/test/isel.py @@ -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()) diff --git a/libnum/test/isel_num8.cpp b/libnum/test/isel_num8.cpp index 7fcc0a0..4f5b1d6 100644 --- a/libnum/test/isel_num8.cpp +++ b/libnum/test/isel_num8.cpp @@ -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" */