hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit ec090a1df4c9f58df7863bca05087e54f56f0dee
parent c3974b628f6b403480e4743d498774a2194e58b6
Author: Bor Grošelj Simić <bgs@turminal.net>
Date:   Fri, 15 Sep 2023 00:00:51 +0200

sort::cmp: use subtraction for 8 and 16 bit numbers

The subtraction is done on words, so there is no risk of overflow.

Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>

Diffstat:
Msort/cmp/cmp.ha | 24++++++++----------------
1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/sort/cmp/cmp.ha b/sort/cmp/cmp.ha @@ -18,34 +18,26 @@ export fn uints(a: const *opaque, b: const *opaque) int = { // [[sort::cmpfunc]] for use with i8. export fn i8s(a: const *opaque, b: const *opaque) int = { - const a = *(a: const *i8), b = *(b: const *i8); - return if (a < b) -1 - else if (a > b) 1 - else 0; + const a = *(a: const *i8): int, b = *(b: const *i8): int; + return a - b; }; // [[sort::cmpfunc]] for use with u8. export fn u8s(a: const *opaque, b: const *opaque) int = { - const a = *(a: const *u8), b = *(b: const *u8); - return if (a < b) -1 - else if (a > b) 1 - else 0; + const a = *(a: const *u8): int, b = *(b: const *u8): int; + return a - b; }; // [[sort::cmpfunc]] for use with i16. export fn i16s(a: const *opaque, b: const *opaque) int = { - const a = *(a: const *i16), b = *(b: const *i16); - return if (a < b) -1 - else if (a > b) 1 - else 0; + const a = *(a: const *i16): int, b = *(b: const *i16): int; + return a - b; }; // [[sort::cmpfunc]] for use with u16. export fn u16s(a: const *opaque, b: const *opaque) int = { - const a = *(a: const *u16), b = *(b: const *u16); - return if (a < b) -1 - else if (a > b) 1 - else 0; + const a = *(a: const *u16): int, b = *(b: const *u16): int; + return a - b; }; // [[sort::cmpfunc]] for use with i32.