From 72aa0e6e3891c034863476751b2aefda781de5b2 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 25 Apr 2021 20:22:26 +0100 Subject: Replace many `foreign` llvm calls with intrinsics --- core/math/bits/bits.odin | 160 ++++++----------------------------------------- 1 file changed, 18 insertions(+), 142 deletions(-) (limited to 'core/math/bits') diff --git a/core/math/bits/bits.odin b/core/math/bits/bits.odin index 2ab1de6e7..303a94c1d 100644 --- a/core/math/bits/bits.odin +++ b/core/math/bits/bits.odin @@ -1,5 +1,6 @@ package math_bits +import "intrinsics" import "core:runtime" U8_MIN :: 0; @@ -22,32 +23,10 @@ I16_MAX :: 1 << 15 - 1; I32_MAX :: 1 << 31 - 1; I64_MAX :: 1 << 63 - 1; -@(default_calling_convention="none") -foreign { - @(link_name="llvm.ctpop.i8") count_ones8 :: proc(i: u8) -> u8 --- - @(link_name="llvm.ctpop.i16") count_ones16 :: proc(i: u16) -> u16 --- - @(link_name="llvm.ctpop.i32") count_ones32 :: proc(i: u32) -> u32 --- - @(link_name="llvm.ctpop.i64") count_ones64 :: proc(i: u64) -> u64 --- - @(link_name="llvm.cttz.i8") trailing_zeros8 :: proc(i: u8, is_zero_undef := false) -> u8 --- - @(link_name="llvm.cttz.i16") trailing_zeros16 :: proc(i: u16, is_zero_undef := false) -> u16 --- - @(link_name="llvm.cttz.i32") trailing_zeros32 :: proc(i: u32, is_zero_undef := false) -> u32 --- - @(link_name="llvm.cttz.i64") trailing_zeros64 :: proc(i: u64, is_zero_undef := false) -> u64 --- - - @(link_name="llvm.bitreverse.i8") reverse_bits8 :: proc(i: u8) -> u8 --- - @(link_name="llvm.bitreverse.i16") reverse_bits16 :: proc(i: u16) -> u16 --- - @(link_name="llvm.bitreverse.i32") reverse_bits32 :: proc(i: u32) -> u32 --- - @(link_name="llvm.bitreverse.i64") reverse_bits64 :: proc(i: u64) -> u64 --- -} - - -trailing_zeros_uint :: proc(i: uint) -> uint { - when size_of(uint) == size_of(u64) { - return uint(trailing_zeros64(u64(i))); - } else { - return uint(trailing_zeros32(u32(i))); - } -} +count_ones :: intrinsics.count_ones; +trailing_zeros :: intrinsics.trailing_zeros; +reverse_bits :: intrinsics.reverse_bits; leading_zeros_u8 :: proc(i: u8) -> int { @@ -117,10 +96,17 @@ byte_swap :: proc{ byte_swap_int, }; -count_zeros8 :: proc(i: u8) -> u8 { return 8 - count_ones8(i); } -count_zeros16 :: proc(i: u16) -> u16 { return 16 - count_ones16(i); } -count_zeros32 :: proc(i: u32) -> u32 { return 32 - count_ones32(i); } -count_zeros64 :: proc(i: u64) -> u64 { return 64 - count_ones64(i); } +count_zeros8 :: proc(i: u8) -> u8 { return 8 - count_ones(i); } +count_zeros16 :: proc(i: u16) -> u16 { return 16 - count_ones(i); } +count_zeros32 :: proc(i: u32) -> u32 { return 32 - count_ones(i); } +count_zeros64 :: proc(i: u64) -> u64 { return 64 - count_ones(i); } + +count_zeros :: proc{ + count_zeros8, + count_zeros16, + count_zeros32, + count_zeros64, +}; rotate_left8 :: proc(x: u8, k: int) -> u8 { @@ -176,120 +162,10 @@ to_le_u64 :: proc(i: u64) -> u64 { when ODIN_ENDIAN == "little" { return i; } to_le_uint :: proc(i: uint) -> uint { when ODIN_ENDIAN == "little" { return i; } else { return byte_swap(i); } } -@(default_calling_convention="none") -foreign { - @(link_name="llvm.uadd.with.overflow.i8") overflowing_add_u8 :: proc(lhs, rhs: u8) -> (u8, bool) --- - @(link_name="llvm.sadd.with.overflow.i8") overflowing_add_i8 :: proc(lhs, rhs: i8) -> (i8, bool) --- - @(link_name="llvm.uadd.with.overflow.i16") overflowing_add_u16 :: proc(lhs, rhs: u16) -> (u16, bool) --- - @(link_name="llvm.sadd.with.overflow.i16") overflowing_add_i16 :: proc(lhs, rhs: i16) -> (i16, bool) --- - @(link_name="llvm.uadd.with.overflow.i32") overflowing_add_u32 :: proc(lhs, rhs: u32) -> (u32, bool) --- - @(link_name="llvm.sadd.with.overflow.i32") overflowing_add_i32 :: proc(lhs, rhs: i32) -> (i32, bool) --- - @(link_name="llvm.uadd.with.overflow.i64") overflowing_add_u64 :: proc(lhs, rhs: u64) -> (u64, bool) --- - @(link_name="llvm.sadd.with.overflow.i64") overflowing_add_i64 :: proc(lhs, rhs: i64) -> (i64, bool) --- -} - -overflowing_add_uint :: proc(lhs, rhs: uint) -> (uint, bool) { - when size_of(uint) == size_of(u32) { - x, ok := overflowing_add_u32(u32(lhs), u32(rhs)); - return uint(x), ok; - } else { - x, ok := overflowing_add_u64(u64(lhs), u64(rhs)); - return uint(x), ok; - } -} -overflowing_add_int :: proc(lhs, rhs: int) -> (int, bool) { - when size_of(int) == size_of(i32) { - x, ok := overflowing_add_i32(i32(lhs), i32(rhs)); - return int(x), ok; - } else { - x, ok := overflowing_add_i64(i64(lhs), i64(rhs)); - return int(x), ok; - } -} - -overflowing_add :: proc{ - overflowing_add_u8, overflowing_add_i8, - overflowing_add_u16, overflowing_add_i16, - overflowing_add_u32, overflowing_add_i32, - overflowing_add_u64, overflowing_add_i64, - overflowing_add_uint, overflowing_add_int, -}; -@(default_calling_convention="none") -foreign { - @(link_name="llvm.usub.with.overflow.i8") overflowing_sub_u8 :: proc(lhs, rhs: u8) -> (u8, bool) --- - @(link_name="llvm.ssub.with.overflow.i8") overflowing_sub_i8 :: proc(lhs, rhs: i8) -> (i8, bool) --- - @(link_name="llvm.usub.with.overflow.i16") overflowing_sub_u16 :: proc(lhs, rhs: u16) -> (u16, bool) --- - @(link_name="llvm.ssub.with.overflow.i16") overflowing_sub_i16 :: proc(lhs, rhs: i16) -> (i16, bool) --- - @(link_name="llvm.usub.with.overflow.i32") overflowing_sub_u32 :: proc(lhs, rhs: u32) -> (u32, bool) --- - @(link_name="llvm.ssub.with.overflow.i32") overflowing_sub_i32 :: proc(lhs, rhs: i32) -> (i32, bool) --- - @(link_name="llvm.usub.with.overflow.i64") overflowing_sub_u64 :: proc(lhs, rhs: u64) -> (u64, bool) --- - @(link_name="llvm.ssub.with.overflow.i64") overflowing_sub_i64 :: proc(lhs, rhs: i64) -> (i64, bool) --- -} -overflowing_sub_uint :: proc(lhs, rhs: uint) -> (uint, bool) { - when size_of(uint) == size_of(u32) { - x, ok := overflowing_sub_u32(u32(lhs), u32(rhs)); - return uint(x), ok; - } else { - x, ok := overflowing_sub_u64(u64(lhs), u64(rhs)); - return uint(x), ok; - } -} -overflowing_sub_int :: proc(lhs, rhs: int) -> (int, bool) { - when size_of(int) == size_of(i32) { - x, ok := overflowing_sub_i32(i32(lhs), i32(rhs)); - return int(x), ok; - } else { - x, ok := overflowing_sub_i64(i64(lhs), i64(rhs)); - return int(x), ok; - } -} - -overflowing_sub :: proc{ - overflowing_sub_u8, overflowing_sub_i8, - overflowing_sub_u16, overflowing_sub_i16, - overflowing_sub_u32, overflowing_sub_i32, - overflowing_sub_u64, overflowing_sub_i64, - overflowing_sub_uint, overflowing_sub_int, -}; - -@(default_calling_convention="none") -foreign { - @(link_name="llvm.umul.with.overflow.i8") overflowing_mul_u8 :: proc(lhs, rhs: u8) -> (u8, bool) --- - @(link_name="llvm.smul.with.overflow.i8") overflowing_mul_i8 :: proc(lhs, rhs: i8) -> (i8, bool) --- - @(link_name="llvm.umul.with.overflow.i16") overflowing_mul_u16 :: proc(lhs, rhs: u16) -> (u16, bool) --- - @(link_name="llvm.smul.with.overflow.i16") overflowing_mul_i16 :: proc(lhs, rhs: i16) -> (i16, bool) --- - @(link_name="llvm.umul.with.overflow.i32") overflowing_mul_u32 :: proc(lhs, rhs: u32) -> (u32, bool) --- - @(link_name="llvm.smul.with.overflow.i32") overflowing_mul_i32 :: proc(lhs, rhs: i32) -> (i32, bool) --- - @(link_name="llvm.umul.with.overflow.i64") overflowing_mul_u64 :: proc(lhs, rhs: u64) -> (u64, bool) --- - @(link_name="llvm.smul.with.overflow.i64") overflowing_mul_i64 :: proc(lhs, rhs: i64) -> (i64, bool) --- -} -overflowing_mul_uint :: proc(lhs, rhs: uint) -> (uint, bool) { - when size_of(uint) == size_of(u32) { - x, ok := overflowing_mul_u32(u32(lhs), u32(rhs)); - return uint(x), ok; - } else { - x, ok := overflowing_mul_u64(u64(lhs), u64(rhs)); - return uint(x), ok; - } -} -overflowing_mul_int :: proc(lhs, rhs: int) -> (int, bool) { - when size_of(int) == size_of(i32) { - x, ok := overflowing_mul_i32(i32(lhs), i32(rhs)); - return int(x), ok; - } else { - x, ok := overflowing_mul_i64(i64(lhs), i64(rhs)); - return int(x), ok; - } -} - -overflowing_mul :: proc{ - overflowing_mul_u8, overflowing_mul_i8, - overflowing_mul_u16, overflowing_mul_i16, - overflowing_mul_u32, overflowing_mul_i32, - overflowing_mul_u64, overflowing_mul_i64, - overflowing_mul_uint, overflowing_mul_int, -}; +overflowing_add :: intrinsics.overflow_add; +overflowing_sub :: intrinsics.overflow_sub; +overflowing_mul :: intrinsics.overflow_mul; len_u8 :: proc(x: u8) -> int { -- cgit v1.2.3