[PATCH v3.1 3/9] lib: Introduce macros for bit operations
authorAustin Clements <aclements@csail.mit.edu>
Fri, 24 Oct 2014 12:49:15 +0000 (08:49 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:05:37 +0000 (10:05 -0800)
f7/72d4c25159cc998c82d030eea54086abd72362 [new file with mode: 0644]

diff --git a/f7/72d4c25159cc998c82d030eea54086abd72362 b/f7/72d4c25159cc998c82d030eea54086abd72362
new file mode 100644 (file)
index 0000000..f94b853
--- /dev/null
@@ -0,0 +1,136 @@
+Return-Path: <aclements@csail.mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 2C4C8431FC9\r
+       for <notmuch@notmuchmail.org>; Fri, 24 Oct 2014 05:49:33 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id T+4I7UWAPhti for <notmuch@notmuchmail.org>;\r
+       Fri, 24 Oct 2014 05:49:24 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (dmz-mailsec-scanner-4.mit.edu\r
+       [18.9.25.15])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 20ACB431FBD\r
+       for <notmuch@notmuchmail.org>; Fri, 24 Oct 2014 05:49:24 -0700 (PDT)\r
+X-AuditID: 1209190f-f79aa6d000005b45-f3-544a4ad32ed3\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+       (using TLS with cipher AES256-SHA (256/256 bits))\r
+       (Client did not present a certificate)\r
+       by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 45.7B.23365.3DA4A445; Fri, 24 Oct 2014 08:49:23 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+       by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id s9OCnLsC013437; \r
+       Fri, 24 Oct 2014 08:49:21 -0400\r
+Received: from drake.dyndns.org\r
+       (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+       [216.15.114.40]) (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s9OCnIF4021584\r
+       (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+       Fri, 24 Oct 2014 08:49:20 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+       (envelope-from <aclements@csail.mit.edu>)\r
+       id 1XheIx-0004k2-64; Fri, 24 Oct 2014 08:49:19 -0400\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3.1 3/9] lib: Introduce macros for bit operations\r
+Date: Fri, 24 Oct 2014 08:49:15 -0400\r
+Message-Id: <1414154955-18114-1-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To:\r
+ <CAB+hUn_d3Wp2bie3E6n1gtb9J_NypAv5rHmr3ObAqWsOX-hzFw@mail.gmail.com>\r
+References:\r
+ <CAB+hUn_d3Wp2bie3E6n1gtb9J_NypAv5rHmr3ObAqWsOX-hzFw@mail.gmail.com>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrDIsWRmVeSWpSXmKPExsUixG6nonvZyyvE4Ot1OYum6c4W12/OZHZg\r
+       8rh1/zW7x7NVt5gDmKK4bFJSczLLUov07RK4MiatnsBacIavovvKdOYGxsvcXYycHBICJhLv\r
+       N21hgbDFJC7cW8/WxcjFISQwm0li67rjUM5GRon5Ry4zQTh3mCQub7jDDuEsYZToWfkOrJ9N\r
+       QF9ixdpJrCC2iIC0xM67s4FsDg5mATuJ3Y0BIGFhASeJK8/XMoHYLAKqEhfu72MDsXkF3CXO\r
+       znrICnGGnMSG3f8ZQWxOgUCJb8vvgdUICQRIXPp3gXkCI/8CRoZVjLIpuVW6uYmZOcWpybrF\r
+       yYl5ealFuiZ6uZkleqkppZsYQYHEKcm/g/HbQaVDjAIcjEo8vDdmeIYIsSaWFVfmHmKU5GBS\r
+       EuVl8fQKEeJLyk+pzEgszogvKs1JLT7EKMHBrCTC62UClONNSaysSi3Kh0lJc7AoifNu+sEX\r
+       IiSQnliSmp2aWpBaBJOV4eBQkuDNARkqWJSanlqRlplTgpBm4uAEGc4DNHwTSA1vcUFibnFm\r
+       OkT+FKOilDjvCw+ghABIIqM0D64XFumvGMWBXhHmtQVp5wEmCbjuV0CDmYAGx2/wABlckoiQ\r
+       kmpgXJh19PBKw00PhDr2n9wqvDPBv/hWo3JUQeeCFM/eQzXMWVNDtt8T+d+8VezCgbRwBa26\r
+       IzU3Zla61r5rbdtWdY3LVv53mKXj+upsZ3UJ+SeH92zh74nLdkx1by1jmO1uwD3H0/yc1eX6\r
+       7OrGDZP8F5t9WP+gI6Lx+hGmGY9kl/LLC2sXM8gosRRnJBpqMRcVJwIA1dA9i88CAAA=\r
+Cc: Austin Clements <aclements@csail.mit.edu>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 24 Oct 2014 12:49:33 -0000\r
+\r
+These macros help clarify basic bit-twiddling code and are written to\r
+be robust against C undefined behavior of shift operators.\r
+---\r
+ lib/message.cc        |  6 +++---\r
+ lib/notmuch-private.h | 11 +++++++++++\r
+ 2 files changed, 14 insertions(+), 3 deletions(-)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 38bc929..55d2ff6 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -869,7 +869,7 @@ notmuch_bool_t\r
+ notmuch_message_get_flag (notmuch_message_t *message,\r
+                         notmuch_message_flag_t flag)\r
+ {\r
+-    return message->flags & (1 << flag);\r
++    return NOTMUCH_TEST_BIT (message->flags, flag);\r
+ }\r
\r
+ void\r
+@@ -877,9 +877,9 @@ notmuch_message_set_flag (notmuch_message_t *message,\r
+                         notmuch_message_flag_t flag, notmuch_bool_t enable)\r
+ {\r
+     if (enable)\r
+-      message->flags |= (1 << flag);\r
++      NOTMUCH_SET_BIT (&message->flags, flag);\r
+     else\r
+-      message->flags &= ~(1 << flag);\r
++      NOTMUCH_CLEAR_BIT (&message->flags, flag);\r
+ }\r
\r
+ time_t\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index 36cc12b..b86897c 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -63,6 +63,17 @@ NOTMUCH_BEGIN_DECLS\r
+ #define STRNCMP_LITERAL(var, literal) \\r
+     strncmp ((var), (literal), sizeof (literal) - 1)\r
\r
++/* Robust bit test/set/reset macros */\r
++#define NOTMUCH_TEST_BIT(val, bit) \\r
++    (((bit) < 0 || (bit) >= CHAR_BIT * sizeof (unsigned long long)) ? 0       \\r
++     : !!((val) & (1ull << (bit))))\r
++#define NOTMUCH_SET_BIT(valp, bit) \\r
++    (((bit) < 0 || (bit) >= CHAR_BIT * sizeof (unsigned long long)) ? *(valp) \\r
++     : (*(valp) |= (1ull << (bit))))\r
++#define NOTMUCH_CLEAR_BIT(valp,  bit) \\r
++    (((bit) < 0 || (bit) >= CHAR_BIT * sizeof (unsigned long long)) ? *(valp) \\r
++     : (*(valp) &= ~(1ull << (bit))))\r
++\r
+ #define unused(x) x __attribute__ ((unused))\r
\r
+ #ifdef __cplusplus\r
+-- \r
+2.1.0\r
+\r