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