From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: git@vger.kernel.org, Elijah Newren <newren@gmail.com>
Subject: Re: [PATCH 1/4] merge-recursive: silence -Wxor-used-as-pow warning
Date: Sat, 25 Jan 2020 09:27:36 -0800 [thread overview]
Message-ID: <xmqq4kwjcupj.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <20200125053723.GA744673@coredump.intra.peff.net> (Jeff King's message of "Sat, 25 Jan 2020 00:37:23 -0500")
Jeff King <peff@peff.net> writes:
> The merge-recursive code uses stage number constants like this:
>
> add = &ci->ren1->dst_entry->stages[2 ^ 1];
> ...
> add = &ci->ren2->dst_entry->stages[3 ^ 1];
>
> The xor has the effect of flipping the "1" bit, so that "2 ^ 1" becomes
> "3" and "3 ^ 1" becomes "2", which correspond to the "ours" and "theirs"
> stages respectively.
>
> Unfortunately, clang-10 and up issue a warning for this code:
>
> merge-recursive.c:1759:40: error: result of '2 ^ 1' is 3; did you mean '1 << 1' (2)? [-Werror,-Wxor-used-as-pow]
> add = &ci->ren1->dst_entry->stages[2 ^ 1];
> ~~^~~
> 1 << 1
> merge-recursive.c:1759:40: note: replace expression with '0x2 ^ 1' to silence this warning
>
> We could silence it by using 0x2, as the compiler mentions. Or by just
> using the constants "2" and "3" directly. But after digging into it, I
> do think this bit-flip is telling us something. If we just wrote:
>
> add = &ci->ren2->dst_entry->stages[2];
>
> for the second one, you might think that "ren2" and "2" correspond. But
> they don't. The logic is: ren2 is theirs, which is stage 3, but we
> are interested in the opposite side's stage, so flip it to 2.
So, the logical name for "^1" operator applied to 2 (ours) and 3
(theirs) is "the_other_side()"? the_other_side(theirs) == ours.
I would have written (5 - side) instead of (side ^ 1) if I were
writing this, though.
> So let's keep the bit-flipping, but let's also put it behind a named
> function, which will make its purpose a bit clearer. This also has the
> side effect of suppressing the warning (and an optimizing compiler
> should be able to easily turn it into a constant as before).
OK. Now I see you named it flip_stage(), which is even better than
"the-other-side" above. Makes sense.
I still think ((2 + 3) - two_or_three_to_be_flipped) easier to
reason about than the bit flipping, as the implementation detail,
though.
Thanks.
next prev parent reply other threads:[~2020-01-25 17:29 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-25 5:35 [PATCH 0/4] more clang/sanitizer fixes Jeff King
2020-01-25 5:37 ` [PATCH 1/4] merge-recursive: silence -Wxor-used-as-pow warning Jeff King
2020-01-25 17:27 ` Junio C Hamano [this message]
2020-01-25 19:55 ` Jeff King
2020-01-25 20:50 ` Elijah Newren
2020-01-25 23:57 ` Jeff King
2020-01-27 19:17 ` Junio C Hamano
2020-01-25 5:38 ` [PATCH 2/4] avoid computing zero offsets from NULL pointer Jeff King
2020-01-27 20:03 ` Junio C Hamano
2020-01-27 21:19 ` Jeff King
2020-01-28 18:03 ` Junio C Hamano
2020-01-29 2:31 ` Jeff King
2020-01-29 5:16 ` Junio C Hamano
2020-01-29 5:46 ` Jeff King
2020-01-25 5:39 ` [PATCH 3/4] xdiff: avoid computing non-zero offset " Jeff King
2020-01-25 5:41 ` [PATCH 4/4] obstack: avoid computing offsets " Jeff King
2020-01-25 5:44 ` [PATCH v2 " Jeff King
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqq4kwjcupj.fsf@gitster-ct.c.googlers.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=newren@gmail.com \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).