aboutsummaryrefslogtreecommitdiff
path: root/src/bigint.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-07-10 14:03:03 -0400
committerAndrew Kelley <superjoe30@gmail.com>2018-07-10 14:03:03 -0400
commitcfaebb20d8906d31cedc59e39e6a9286967a931a (patch)
tree33d0c3994bfb658937e2e692a946bc8a2eca4fe5 /src/bigint.cpp
parentb5d07297dec61a3993dfe91ceee2c87672db1e8e (diff)
parent0ce6934e2631eb3beca817d3bce12ecb13aafa13 (diff)
downloadzig-cfaebb20d8906d31cedc59e39e6a9286967a931a.tar.gz
zig-cfaebb20d8906d31cedc59e39e6a9286967a931a.zip
Merge remote-tracking branch 'origin/master' into llvm7
Diffstat (limited to 'src/bigint.cpp')
-rw-r--r--src/bigint.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/bigint.cpp b/src/bigint.cpp
index bb227a7c3d..bf18b9a1bf 100644
--- a/src/bigint.cpp
+++ b/src/bigint.cpp
@@ -1593,6 +1593,37 @@ void bigint_append_buf(Buf *buf, const BigInt *op, uint64_t base) {
}
}
+size_t bigint_popcount_unsigned(const BigInt *bi) {
+ assert(!bi->is_negative);
+ if (bi->digit_count == 0)
+ return 0;
+
+ size_t count = 0;
+ size_t bit_count = bi->digit_count * 64;
+ for (size_t i = 0; i < bit_count; i += 1) {
+ if (bit_at_index(bi, i))
+ count += 1;
+ }
+ return count;
+}
+
+size_t bigint_popcount_signed(const BigInt *bi, size_t bit_count) {
+ if (bit_count == 0)
+ return 0;
+ if (bi->digit_count == 0)
+ return 0;
+
+ BigInt twos_comp = {0};
+ to_twos_complement(&twos_comp, bi, bit_count);
+
+ size_t count = 0;
+ for (size_t i = 0; i < bit_count; i += 1) {
+ if (bit_at_index(&twos_comp, i))
+ count += 1;
+ }
+ return count;
+}
+
size_t bigint_ctz(const BigInt *bi, size_t bit_count) {
if (bit_count == 0)
return 0;