aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2018-12-21 10:31:10 +0000
committergingerBill <bill@gingerbill.org>2018-12-21 10:31:10 +0000
commitb4e83a430a8401e915346cd2d6063d8a8a2d1e03 (patch)
tree23b31355f545ed18bd0a5a727c8fd7532847742f
parente3d7e6f76a91aeba351c9d3a1a3add21f2e439ab (diff)
Add `card` procedure to measure cardinality of a bit_set
-rw-r--r--core/runtime/core.odin20
-rw-r--r--examples/demo/demo.odin1
2 files changed, 21 insertions, 0 deletions
diff --git a/core/runtime/core.odin b/core/runtime/core.odin
index a84430731..0599dbbed 100644
--- a/core/runtime/core.odin
+++ b/core/runtime/core.odin
@@ -617,6 +617,26 @@ excl_bit_set :: inline proc(s: ^$S/bit_set[$E; $U], other: S) -> S {
@(builtin) excl :: proc{excl_elem, excl_elems, excl_bit_set};
+@(builtin)
+card :: proc(s: $S/bit_set[$E; $U]) -> int {
+ when size_of(S) == 1 {
+ foreign { @(link_name="llvm.ctpop.i8") count_ones :: proc(i: u8) -> u8 --- }
+ return int(count_ones(transmute(u8)s));
+ } else when size_of(S) == 2 {
+ foreign { @(link_name="llvm.ctpop.i16") count_ones :: proc(i: u16) -> u16 --- }
+ return int(count_ones(transmute(u16)s));
+ } else when size_of(S) == 4 {
+ foreign { @(link_name="llvm.ctpop.i32") count_ones :: proc(i: u32) -> u32 --- }
+ return int(count_ones(transmute(u32)s));
+ } else when size_of(S) == 8 {
+ foreign { @(link_name="llvm.ctpop.i64") count_ones :: proc(i: u64) -> u64 --- }
+ return int(count_ones(transmute(u64)s));
+ } else {
+ #assert(false);
+ return 0;
+ }
+}
+
diff --git a/examples/demo/demo.odin b/examples/demo/demo.odin
index dcfb5a5bc..9c64ea733 100644
--- a/examples/demo/demo.odin
+++ b/examples/demo/demo.odin
@@ -827,6 +827,7 @@ bit_set_type :: proc() {
}
X :: Saturday in WEEKEND; // Constant evaluation
fmt.println(X);
+ fmt.println("Cardinality:", card(e));
}
{
x: bit_set['A'..'Z'];