[haiku-commits] r40360 - haiku/trunk/src/kits/network/libnetapi

  • From: kallisti5@xxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sat, 5 Feb 2011 00:32:58 +0100 (CET)

Author: kallisti5
Date: 2011-02-05 00:32:58 +0100 (Sat, 05 Feb 2011)
New Revision: 40360
Changeset: http://dev.haiku-os.org/changeset/40360

Modified:
   haiku/trunk/src/kits/network/libnetapi/NetworkAddress.cpp
Log:
added optimized gcc builtin bit population count as per Axel's recommendation; 
the optimized function might be used for the ipv6 netmask length as well.. i 
just don't want to touch that double for loop without *LOTS* of testing; lets 
hope this isn't my first build-breaker

Modified: haiku/trunk/src/kits/network/libnetapi/NetworkAddress.cpp
===================================================================
--- haiku/trunk/src/kits/network/libnetapi/NetworkAddress.cpp   2011-02-04 
22:42:56 UTC (rev 40359)
+++ haiku/trunk/src/kits/network/libnetapi/NetworkAddress.cpp   2011-02-04 
23:32:58 UTC (rev 40360)
@@ -17,6 +17,26 @@
 #include <sys/sockio.h>
 
 
+/* The GCC builtin below only exists in > GCC 3.4
+ * Benefits include faster execution time as the builtin
+ * uses a bitcounting cpu instruction if it exists
+ */
+#if __GNUC__ >= 4
+#      define BitCount(buffer) __builtin_popcount(buffer)
+#else
+ssize_t BitCount(uint32 buf)
+{
+       ssize_t result = 0;
+       for (uint8 i = 32; i > 0; i--) {
+               if ((buf & (1 << (i - 1))) == 0)
+                       break;
+               result++;
+       }
+       return result;
+}
+#endif
+
+
 static uint8
 from_hex(char hex)
 {
@@ -763,21 +783,15 @@
                {
                        sockaddr_in& mask = (sockaddr_in&)fAddress;
 
-                       ssize_t result = 0;
                        uint32 hostMask = ntohl(mask.sin_addr.s_addr);
-                       for (uint8 i = 32; i > 0; i--) {
-                               if ((hostMask & (1 << (i - 1))) == 0)
-                                       break;
-                               result++;
-                       }
-
-                       return result;
+                       return BitCount(hostMask);
                }
 
                case AF_INET6:
                {
                        sockaddr_in6& mask = (sockaddr_in6&)fAddress;
 
+                       // TODO : see if we can use the optimized BitCount for 
this
                        ssize_t result = 0;
                        for (uint8 i = 0; i < sizeof(in6_addr); i++) {
                                for (uint8 j = 0; j < 8; j++) {


Other related posts: