From: Austin Clements Date: Thu, 23 Oct 2014 12:30:35 +0000 (+2000) Subject: [PATCH v3 3/9] lib: Introduce macros for bit operations X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=a2c63dd437ff4307b81b4e01991a57dd79884d41;p=notmuch-archives.git [PATCH v3 3/9] lib: Introduce macros for bit operations --- diff --git a/47/5202e7fddcae3d0ba837799b0504feae88b08b b/47/5202e7fddcae3d0ba837799b0504feae88b08b new file mode 100644 index 000000000..15b81765b --- /dev/null +++ b/47/5202e7fddcae3d0ba837799b0504feae88b08b @@ -0,0 +1,134 @@ +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 1D254429E2F + for ; Thu, 23 Oct 2014 05:31:01 -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 Pjfgh+gUkqfu for ; + Thu, 23 Oct 2014 05:30:52 -0700 (PDT) +Received: from dmz-mailsec-scanner-8.mit.edu (dmz-mailsec-scanner-8.mit.edu + [18.7.68.37]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 46CBA431FD7 + for ; Thu, 23 Oct 2014 05:30:52 -0700 (PDT) +X-AuditID: 12074425-f79e46d000002583-90-5448f4fbe9df +Received: from mailhub-auth-1.mit.edu ( [18.9.21.35]) + (using TLS with cipher AES256-SHA (256/256 bits)) + (Client did not present a certificate) + by dmz-mailsec-scanner-8.mit.edu (Symantec Messaging Gateway) with SMTP + id 0C.F4.09603.BF4F8445; Thu, 23 Oct 2014 08:30:51 -0400 (EDT) +Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) + by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id s9NCUmxd030363; + Thu, 23 Oct 2014 08:30:48 -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 s9NCUhFr008930 + (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); + Thu, 23 Oct 2014 08:30:46 -0400 +Received: from amthrax by drake.dyndns.org with local (Exim 4.84) + (envelope-from ) + id 1XhHXP-0007bk-8D; Thu, 23 Oct 2014 08:30:43 -0400 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH v3 3/9] lib: Introduce macros for bit operations +Date: Thu, 23 Oct 2014 08:30:35 -0400 +Message-Id: <1414067441-29054-4-git-send-email-aclements@csail.mit.edu> +X-Mailer: git-send-email 2.1.0 +In-Reply-To: <1414067441-29054-1-git-send-email-aclements@csail.mit.edu> +References: <1414067441-29054-1-git-send-email-aclements@csail.mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFtrNIsWRmVeSWpSXmKPExsUixCmqrPv7i0eIweffyhar5/JYXL85k9mB + yWPnrLvsHs9W3WIOYIrisklJzcksSy3St0vgyni56CVrwS6+ip7dF9gaGA9ydzFyckgImEis + uNzOBGGLSVy4t54NxBYSmM0ksWRvZRcjF5C9kVHiclcvE4Rzh0ni7tvDzBBVSxglHt8MA7HZ + BPQlVqydxApiiwhIS+y8OxvMZhbwkJjS+grMFhZwkJjd/okRxGYRUJWYO3kK2GZeAXeJzuNr + oa6Qk9iw+z9YDSdQ754dd6AucpfY/vwg6wRG/gWMDKsYZVNyq3RzEzNzilOTdYuTE/PyUot0 + LfRyM0v0UlNKNzGCwojdRXUH44RDSocYBTgYlXh4L6R6hAixJpYVV+YeYpTkYFIS5Y15ABTi + S8pPqcxILM6ILyrNSS0+xCjBwawkwht/ASjHm5JYWZValA+TkuZgURLn3fSDL0RIID2xJDU7 + NbUgtQgmK8PBoSTBywiMFyHBotT01Iq0zJwShDQTByfIcB6g4bc+gwwvLkjMLc5Mh8ifYlSU + EudNBkkIgCQySvPgemFx/opRHOgVYV41kBU8wBQB1/0KaDATyNUbwAaXJCKkpBoY1Zh2nWh6 + NFmGRSzRakl6ySVuzz2mq5mOruz+IGUf4uHk6vL6xYbsIJslNs8PO934JHTWxHTd60cWK3Ja + s6/Glc7TC+v9/o7z1qzrSrfMXKd5axzkkbcta5l8TizZNejGr6hYlXv/f5/fafJ6aVy/yp26 + fNVPeTF6Mk73GJT2OB5vWxdwLfWbEktxRqKhFnNRcSIAeYE5RM4CAAA= +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: Thu, 23 Oct 2014 12:31:01 -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..7250291 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 +