summaryrefslogtreecommitdiff
path: root/src/XSecurity.c
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2016-02-25 23:36:50 -0800
committerAlan Coopersmith <alan.coopersmith@oracle.com>2016-03-03 18:24:12 -0800
commit490a25e6f8a4d2482af4364c700b68ad11a4d10b (patch)
tree87cb9f917700aa3dbcc57bfcce012473257b1491 /src/XSecurity.c
parent54ba591149f3a92c5a4a66e428ae2a33d1027053 (diff)
downloadxorg-lib-libXext-490a25e6f8a4d2482af4364c700b68ad11a4d10b.tar.gz
Use __builtin_popcountl if available to replace Ones() in XSecurity.c
If the compiler knows of a better algorithm for counting the number of bits set in a word for the target CPU, let it use that, instead of the classic algorithm optimized for PDP-6. Tested for the range of values used in XSecurity.c and verified results are the same from both: for (unsigned long i = 0; i <= XSecurityAllAuthorizationAttributes; i++) { printf("ones: %d\tpopcnt: %d\n", Ones(i), __builtin_popcountl(i)); } Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Diffstat (limited to 'src/XSecurity.c')
-rw-r--r--src/XSecurity.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/XSecurity.c b/src/XSecurity.c
index 3ca75b1..56b37fc 100644
--- a/src/XSecurity.c
+++ b/src/XSecurity.c
@@ -196,15 +196,24 @@ XSecurityFreeXauth(Xauth *auth)
XFree(auth);
}
-static int
+#ifdef HAVE___BUILTIN_POPCOUNTL
+# define Ones __builtin_popcountl
+#else
+/*
+ * Count the number of bits set to 1 in a 32-bit word.
+ * Algorithm from MIT AI Lab Memo 239: "HAKMEM", ITEM 169.
+ * http://dspace.mit.edu/handle/1721.1/6086
+ */
+static inline int
Ones(Mask mask)
{
register Mask y;
- y = (mask >> 1) &033333333333;
+ y = (mask >> 1) & 033333333333;
y = mask - y - ((y >>1) & 033333333333);
return (((y + (y >> 3)) & 030707070707) % 077);
}
+#endif
Xauth *
XSecurityGenerateAuthorization(