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 1F600431FDD for ; Thu, 23 Oct 2014 21:40:36 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.699 X-Spam-Level: X-Spam-Status: No, score=-0.699 tagged_above=-999 required=5 tests=[HTML_MESSAGE=0.001, RCVD_IN_DNSWL_LOW=-0.7] 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 UnOZiGwkTIwR for ; Thu, 23 Oct 2014 21:40:28 -0700 (PDT) Received: from mail-la0-f48.google.com (mail-la0-f48.google.com [209.85.215.48]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 5F3DB431FDC for ; Thu, 23 Oct 2014 21:40:28 -0700 (PDT) Received: by mail-la0-f48.google.com with SMTP id gi9so1966483lab.21 for ; Thu, 23 Oct 2014 21:40:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=BQi7AgBJzl1M8yMutnXjYAo46AeFFQdOPzxbhPuq1F0=; b=NmDEHvd1y52ANM/FsSu5jU3WzmNWktvPShuTUY7NSFprTwmOTLfIl8tJ2a2IqDXg5v kcRZ1Cpx91Frs/2oXChdYmp/EVWPzZy8Y9GuBA0oZmsV5x2rXfpNU49ygFsW3gPIEOaF lEYdKaXuLQ55kMHCO0oQNZ3A08la/SleSEm0TvSEmk31F7SoCO/HVRDndrUioPMatlCZ xuP07mUKIz61NyMtTaNpqI8o++GJnfICNVqED3CqojDuHiBCozxDS9B/N5N6/q/Igk/e rb4yDGYDbtN/s4zG47pDgQBw42yabM4Qo+gVuylwikEyNq0EsS9uzs0nP5QmQ2QzHOVn jCHg== X-Gm-Message-State: ALoCoQnJKFx4G4udFAZq9Ld8gli2h35wXO3mDfvpC9G7OOj+u+ycR9nXMc+uydlnb40MxoVtZ44m MIME-Version: 1.0 X-Received: by 10.152.7.7 with SMTP id f7mr1666883laa.57.1414125625506; Thu, 23 Oct 2014 21:40:25 -0700 (PDT) Received: by 10.25.145.7 with HTTP; Thu, 23 Oct 2014 21:40:25 -0700 (PDT) Received: by 10.25.145.7 with HTTP; Thu, 23 Oct 2014 21:40:25 -0700 (PDT) In-Reply-To: <1414067441-29054-4-git-send-email-aclements@csail.mit.edu> References: <1414067441-29054-1-git-send-email-aclements@csail.mit.edu> <1414067441-29054-4-git-send-email-aclements@csail.mit.edu> Date: Fri, 24 Oct 2014 07:40:25 +0300 Message-ID: Subject: Re: [PATCH v3 3/9] lib: Introduce macros for bit operations From: Jani Nikula To: Austin Clements Content-Type: multipart/alternative; boundary=001a11c2870a939325050623c78e Cc: Notmuch Mail 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 04:40:36 -0000 --001a11c2870a939325050623c78e Content-Type: text/plain; charset=UTF-8 On Oct 23, 2014 3:31 PM, "Austin Clements" wrote: > > 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))) bit should be in braces in the above, like valp. Jani. > + > #define unused(x) x __attribute__ ((unused)) > > #ifdef __cplusplus > -- > 2.1.0 > > _______________________________________________ > notmuch mailing list > notmuch@notmuchmail.org > http://notmuchmail.org/mailman/listinfo/notmuch --001a11c2870a939325050623c78e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On Oct 23, 2014 3:31 PM, "Austin Clements" <aclements@csail.mit.edu> wrote:
>
> These macros help clarify basic bit-twiddling code and are written to<= br> > be robust against C undefined behavior of shift operators.
> ---
> =C2=A0lib/message.cc=C2=A0 =C2=A0 =C2=A0 =C2=A0 |=C2=A0 6 +++---
> =C2=A0lib/notmuch-private.h | 11 +++++++++++
> =C2=A02 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
> =C2=A0notmuch_message_get_flag (notmuch_message_t *message,
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 notmuch_message_flag_t flag)
> =C2=A0{
> -=C2=A0 =C2=A0 return message->flags & (1 << flag);
> +=C2=A0 =C2=A0 return NOTMUCH_TEST_BIT (message->flags, flag);
> =C2=A0}
>
> =C2=A0void
> @@ -877,9 +877,9 @@ notmuch_message_set_flag (notmuch_message_t *messa= ge,
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 notmuch_message_flag_t flag, notmuch_bool_t enable) > =C2=A0{
> =C2=A0 =C2=A0 =C2=A0if (enable)
> -=C2=A0 =C2=A0 =C2=A0 =C2=A0message->flags |=3D (1 << flag);<= br> > +=C2=A0 =C2=A0 =C2=A0 =C2=A0NOTMUCH_SET_BIT (&message->flags, f= lag);
> =C2=A0 =C2=A0 =C2=A0else
> -=C2=A0 =C2=A0 =C2=A0 =C2=A0message->flags &=3D ~(1 << fl= ag);
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0NOTMUCH_CLEAR_BIT (&message->flags,= flag);
> =C2=A0}
>
> =C2=A0time_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
> =C2=A0#define STRNCMP_LITERAL(var, literal) \
> =C2=A0 =C2=A0 =C2=A0strncmp ((var), (literal), sizeof (literal) - 1) >
> +/* Robust bit test/set/reset macros */
> +#define NOTMUCH_TEST_BIT(val, bit) \
> +=C2=A0 =C2=A0 ((bit < 0 || bit >=3D CHAR_BIT * sizeof (unsigned= long long)) ? 0=C2=A0 =C2=A0 \
> +=C2=A0 =C2=A0 =C2=A0: !!((val) & (1ull << bit)))
> +#define NOTMUCH_SET_BIT(valp, bit) \
> +=C2=A0 =C2=A0 ((bit < 0 || bit >=3D CHAR_BIT * sizeof (unsigned= long long)) ? *(valp) \
> +=C2=A0 =C2=A0 =C2=A0: (*(valp) |=3D (1ull << bit)))
> +#define NOTMUCH_CLEAR_BIT(valp,=C2=A0 bit) \
> +=C2=A0 =C2=A0 ((bit < 0 || bit >=3D CHAR_BIT * sizeof (unsigned= long long)) ? *(valp) \
> +=C2=A0 =C2=A0 =C2=A0: (*(valp) &=3D ~(1ull << bit)))

bit should be in braces in the above, like valp.

Jani.

> +
> =C2=A0#define unused(x) x __attribute__ ((unused))
>
> =C2=A0#ifdef __cplusplus
> --
> 2.1.0
>
> _______________________________________________
> notmuch mailing list
> notmuch@notmuchmail.org=
> http://not= muchmail.org/mailman/listinfo/notmuch

--001a11c2870a939325050623c78e--