summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/runmath.in
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2019-10-06 17:17:34 +0000
committerKarl Berry <karl@freefriends.org>2019-10-06 17:17:34 +0000
commit7f9f7dad00ea5f025578c491005835740f9ffd90 (patch)
tree011a87a78042a82ea3a18f5c08e60f6a3930bf0b /Build/source/utils/asymptote/runmath.in
parent37d8fb68e502daabe6aa5f5ae9fb78191f275742 (diff)
asy 2.56 sources
git-svn-id: svn://tug.org/texlive/trunk@52300 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/utils/asymptote/runmath.in')
-rw-r--r--Build/source/utils/asymptote/runmath.in23
1 files changed, 9 insertions, 14 deletions
diff --git a/Build/source/utils/asymptote/runmath.in b/Build/source/utils/asymptote/runmath.in
index fa9dbf3e692..c1068ce2175 100644
--- a/Build/source/utils/asymptote/runmath.in
+++ b/Build/source/utils/asymptote/runmath.in
@@ -132,21 +132,16 @@ unsigned long long bitreverse64(unsigned long long a)
((unsigned long long) BitReverseTable8[(a >> 56)]);
}
-// From Warren, Jr., Henry S. (2013) [2002]. Hacker's Delight (2 ed.).
-// Addison Wesley - Pearson Education, Inc. pp. 81-96.
-Int popcount(Int a)
-{
- const uint64_t m1 = 0x5555555555555555; //binary: 0101...
- const uint64_t m2 = 0x3333333333333333; //binary: 00110011..
- const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ...
- const uint64_t h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3...
-
-// This algorithm uses 12 arithmetic operations, one of which is a multiply.
- a -= (a >> 1) & m1; //put count of each 2 bits into those 2 bits
- a=(a & m2)+((a >> 2) & m2); //put count of each 4 bits into those 4 bits
- a=(a+(a >> 4)) & m4; //put count of each 8 bits into those 8 bits
- return (a * h01) >> 56; //returns left 8 bits of a+(a << 8)+(a << 16)+(a << 24)+...
+// https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
+#define T unsignedInt
+Int popcount(T a)
+{
+ a=a-((a >> 1) & (T)~(T)0/3);
+ a=(a & (T)~(T)0/15*3)+((a >> 2) & (T)~(T)0/15*3);
+ a=(a+(a >> 4)) & (T)~(T)0/255*15;
+return (T)(a*((T)~(T)0/255)) >> (sizeof(T)-1)*CHAR_BIT;
}
+#undef T
// Return the factorial of a non-negative integer using a lookup table.
Int factorial(Int n)