threads / patch / 60608

patchUse ^=1 to toggle between 0 and 1

Subject: [PATCH] Use ^=1 to toggle between 0 and 1

## tl;dr

26 messages between Dec 12, 2023 and Dec 19, 2024. Diffs are folded; open one to read it.

replies: 25people: 7as markdown or json

AtariDreams via GitGitGadget· Dec 12, 2023, 17:17 UTC · lore
From: Seija Kijin <doremylover123@gmail.com>

If it is known that an int is either 1 or 0, doing an exclusive or to switch instead of a modulus makes more sense and is more efficient.

Signed-off-by: Seija Kijin doremylover123@gmail.com
---
    Use ^=1 to toggle between 0 and 1
    
    If it is known that an int is either 1 or 0, doing an exclusive or to
    switch instead of a modulus makes more sense and is more efficient.
    
    Signed-off-by: Seija Kijin doremylover123@gmail.com
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1620%2FAtariDreams%2Fbuffer-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1620/AtariDreams/buffer-v1
Pull-Request: https://github.com/git/git/pull/1620
 builtin/fast-export.c      | 4 ++--
 diff.c                     | 2 +-
 ident.c                    | 2 +-
 t/helper/test-path-utils.c | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)
Show changes to 4 files +5 −5

builtin/fast-export.c, diff.c, ident.c, t/helper/test-path-utils.c

diff --git a/builtin/fast-export.c b/builtin/fast-export.c
index 70aff515acb..f9f2c9dd850 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -593,8 +593,8 @@ static void anonymize_ident_line(const char **beg, const char **end)
 	struct ident_split split;
 	const char *end_of_header;
 
-	out = &buffers[which_buffer++];
-	which_buffer %= ARRAY_SIZE(buffers);
+	out = &buffers[which_buffer];
+	which_buffer ^= 1;
 	strbuf_reset(out);
 
 	/* skip "committer", "author", "tagger", etc */
diff --git a/diff.c b/diff.c
index 2c602df10a3..91842b54753 100644
--- a/diff.c
+++ b/diff.c
@@ -1191,7 +1191,7 @@ static void mark_color_as_moved(struct diff_options *o,
 							    &pmb_nr);
 
 			if (contiguous && pmb_nr && moved_symbol == l->s)
-				flipped_block = (flipped_block + 1) % 2;
+				flipped_block ^= 1;
 			else
 				flipped_block = 0;
 
diff --git a/ident.c b/ident.c
index cc7afdbf819..188826eed63 100644
--- a/ident.c
+++ b/ident.c
@@ -459,7 +459,7 @@ const char *fmt_ident(const char *name, const char *email,
 	int want_name = !(flag & IDENT_NO_NAME);
 
 	struct strbuf *ident = &ident_pool[index];
-	index = (index + 1) % ARRAY_SIZE(ident_pool);
+	index ^= 1;
 
 	if (!email) {
 		if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
index 70396fa3845..241136148a5 100644
--- a/t/helper/test-path-utils.c
+++ b/t/helper/test-path-utils.c
@@ -185,7 +185,7 @@ static int check_dotfile(const char *x, const char **argv,
 	int res = 0, expect = 1;
 	for (; *argv; argv++) {
 		if (!strcmp("--not", *argv))
-			expect = !expect;
+			expect ^= 1;
 		else if (expect != (is_hfs(*argv) || is_ntfs(*argv)))
 			res = error("'%s' is %s.git%s", *argv,
 				    expect ? "not " : "", x);

base-commit: 1a87c842ece327d03d08096395969aca5e0a6996
-- 
gitgitgadget
Dragan Simic· Dec 12, 2023, 17:29 UTC · re: AtariDreams via GitGitGadget · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

On 2023-12-12 18:17, AtariDreams via GitGitGadget wrote:
Show 5 quoted lines
> From: Seija Kijin <doremylover123@gmail.com>
> 
> If it is known that an int is either 1 or 0,
> doing an exclusive or to switch instead of a
> modulus makes more sense and is more efficient.

Quite frankly, this doesn't seem like an improvement to me. It makes the code much less readable, more error-prone, and may even end up producing code that isn't portable.

Regarding the efficiency, such optimizations may be perfectly fine as a trade-off in some critical paths, but these cases don't seem like that.

Show 82 quoted lines
> Signed-off-by: Seija Kijin doremylover123@gmail.com
> ---
>     Use ^=1 to toggle between 0 and 1
> 
>     If it is known that an int is either 1 or 0, doing an exclusive or 
> to
>     switch instead of a modulus makes more sense and is more efficient.
> 
>     Signed-off-by: Seija Kijin doremylover123@gmail.com
> 
> Published-As:
> https://github.com/gitgitgadget/git/releases/tag/pr-git-1620%2FAtariDreams%2Fbuffer-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git
> pr-git-1620/AtariDreams/buffer-v1
> Pull-Request: https://github.com/git/git/pull/1620
> 
>  builtin/fast-export.c      | 4 ++--
>  diff.c                     | 2 +-
>  ident.c                    | 2 +-
>  t/helper/test-path-utils.c | 2 +-
>  4 files changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/builtin/fast-export.c b/builtin/fast-export.c
> index 70aff515acb..f9f2c9dd850 100644
> --- a/builtin/fast-export.c
> +++ b/builtin/fast-export.c
> @@ -593,8 +593,8 @@ static void anonymize_ident_line(const char **beg,
> const char **end)
>  	struct ident_split split;
>  	const char *end_of_header;
> 
> -	out = &buffers[which_buffer++];
> -	which_buffer %= ARRAY_SIZE(buffers);
> +	out = &buffers[which_buffer];
> +	which_buffer ^= 1;
>  	strbuf_reset(out);
> 
>  	/* skip "committer", "author", "tagger", etc */
> diff --git a/diff.c b/diff.c
> index 2c602df10a3..91842b54753 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -1191,7 +1191,7 @@ static void mark_color_as_moved(struct 
> diff_options *o,
>  							    &pmb_nr);
> 
>  			if (contiguous && pmb_nr && moved_symbol == l->s)
> -				flipped_block = (flipped_block + 1) % 2;
> +				flipped_block ^= 1;
>  			else
>  				flipped_block = 0;
> 
> diff --git a/ident.c b/ident.c
> index cc7afdbf819..188826eed63 100644
> --- a/ident.c
> +++ b/ident.c
> @@ -459,7 +459,7 @@ const char *fmt_ident(const char *name, const char 
> *email,
>  	int want_name = !(flag & IDENT_NO_NAME);
> 
>  	struct strbuf *ident = &ident_pool[index];
> -	index = (index + 1) % ARRAY_SIZE(ident_pool);
> +	index ^= 1;
> 
>  	if (!email) {
>  		if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
> diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
> index 70396fa3845..241136148a5 100644
> --- a/t/helper/test-path-utils.c
> +++ b/t/helper/test-path-utils.c
> @@ -185,7 +185,7 @@ static int check_dotfile(const char *x, const char 
> **argv,
>  	int res = 0, expect = 1;
>  	for (; *argv; argv++) {
>  		if (!strcmp("--not", *argv))
> -			expect = !expect;
> +			expect ^= 1;
>  		else if (expect != (is_hfs(*argv) || is_ntfs(*argv)))
>  			res = error("'%s' is %s.git%s", *argv,
>  				    expect ? "not " : "", x);
> 
> base-commit: 1a87c842ece327d03d08096395969aca5e0a6996
Jeff King· Dec 12, 2023, 20:09 UTC · re: AtariDreams via GitGitGadget · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

On Tue, Dec 12, 2023 at 05:17:47PM +0000, AtariDreams via GitGitGadget wrote:
Show 12 quoted lines
> diff --git a/builtin/fast-export.c b/builtin/fast-export.c
> index 70aff515acb..f9f2c9dd850 100644
> --- a/builtin/fast-export.c
> +++ b/builtin/fast-export.c
> @@ -593,8 +593,8 @@ static void anonymize_ident_line(const char **beg, const char **end)
>  	struct ident_split split;
>  	const char *end_of_header;
>  
> -	out = &buffers[which_buffer++];
> -	which_buffer %= ARRAY_SIZE(buffers);
> +	out = &buffers[which_buffer];
> +	which_buffer ^= 1;

In the current code, if the size of "buffers" is increased then everything would just work. But your proposed code (rather subtly) makes the assumption that ARRAY_SIZE(buffers) is 2.

So even leaving aside questions of readability, I think the existing code is much more maintainable.

Show 12 quoted lines
> diff --git a/diff.c b/diff.c
> index 2c602df10a3..91842b54753 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -1191,7 +1191,7 @@ static void mark_color_as_moved(struct diff_options *o,
>  							    &pmb_nr);
>  
>  			if (contiguous && pmb_nr && moved_symbol == l->s)
> -				flipped_block = (flipped_block + 1) % 2;
> +				flipped_block ^= 1;
>  			else
>  				flipped_block = 0;

This one I do not see any problem with changing, though I think it is a matter of opinion on which is more readable (I actually tend to think of "x = 0 - x" as idiomatic for flipping).

Show 13 quoted lines
> diff --git a/ident.c b/ident.c
> index cc7afdbf819..188826eed63 100644
> --- a/ident.c
> +++ b/ident.c
> @@ -459,7 +459,7 @@ const char *fmt_ident(const char *name, const char *email,
>  	int want_name = !(flag & IDENT_NO_NAME);
>  
>  	struct strbuf *ident = &ident_pool[index];
> -	index = (index + 1) % ARRAY_SIZE(ident_pool);
> +	index ^= 1;
>  
>  	if (!email) {
>  		if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
This has the same problem as the first case.
Show 10 quoted lines
> diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
> index 70396fa3845..241136148a5 100644
> --- a/t/helper/test-path-utils.c
> +++ b/t/helper/test-path-utils.c
> @@ -185,7 +185,7 @@ static int check_dotfile(const char *x, const char **argv,
>  	int res = 0, expect = 1;
>  	for (; *argv; argv++) {
>  		if (!strcmp("--not", *argv))
> -			expect = !expect;
> +			expect ^= 1;

This one is not wrong, but IMHO it is more clear to express negation of a boolean using "!" (i.e., what the code is already doing).

So of the four hunks, only the second one seems like a possible improvement, and even there I am not sure the readability is better.

-Peff
René Scharfe· Dec 12, 2023, 22:30 UTC · re: Jeff King · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

Am 12.12.23 um 21:09 schrieb Jeff King:
Show 18 quoted lines
> On Tue, Dec 12, 2023 at 05:17:47PM +0000, AtariDreams via GitGitGadget wrote:
>
>> diff --git a/diff.c b/diff.c
>> index 2c602df10a3..91842b54753 100644
>> --- a/diff.c
>> +++ b/diff.c
>> @@ -1191,7 +1191,7 @@ static void mark_color_as_moved(struct diff_options *o,
>>  							    &pmb_nr);
>>
>>  			if (contiguous && pmb_nr && moved_symbol == l->s)
>> -				flipped_block = (flipped_block + 1) % 2;
>> +				flipped_block ^= 1;
>>  			else
>>  				flipped_block = 0;
>
> This one I do not see any problem with changing, though I think it is a
> matter of opinion on which is more readable (I actually tend to think of
> "x = 0 - x" as idiomatic for flipping).
Did you mean "x = 1 - x"?
    x 0 - x 1 - x
   -- ----- -----
   -1    +1    +2
    0     0    +1
   +1    -1     0
I don't particular like this; it repeats x and seems error-prone. ;-)
I agree with your assessment of the other three cases in the patch.

Can we salvage something from this bikeshedding exercise? I wonder if it's time to use the C99 type _Bool in our code. It would allow documenting that only two possible values exist in cases like the one above. That would be even more useful for function returns, I assume.

René
Jeff King· Dec 13, 2023, 08:01 UTC · re: René Scharfe · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

On Tue, Dec 12, 2023 at 11:30:03PM +0100, René Scharfe wrote:
Show 21 quoted lines
> Am 12.12.23 um 21:09 schrieb Jeff King:
> > On Tue, Dec 12, 2023 at 05:17:47PM +0000, AtariDreams via GitGitGadget wrote:
> >
> >> diff --git a/diff.c b/diff.c
> >> index 2c602df10a3..91842b54753 100644
> >> --- a/diff.c
> >> +++ b/diff.c
> >> @@ -1191,7 +1191,7 @@ static void mark_color_as_moved(struct diff_options *o,
> >>  							    &pmb_nr);
> >>
> >>  			if (contiguous && pmb_nr && moved_symbol == l->s)
> >> -				flipped_block = (flipped_block + 1) % 2;
> >> +				flipped_block ^= 1;
> >>  			else
> >>  				flipped_block = 0;
> >
> > This one I do not see any problem with changing, though I think it is a
> > matter of opinion on which is more readable (I actually tend to think of
> > "x = 0 - x" as idiomatic for flipping).
> 
> Did you mean "x = 1 - x"?
Oops, yes, of course. I'm not sure how I managed to fumble that.
> I don't particular like this; it repeats x and seems error-prone. ;-)
Yes. :)

Without digging into the code, I had just assumed that flipped_block was used as an array index. But it really is a boolean, so I actually think "flipped_block = !flipped_block" would probably be the most clear (but IMHO not really worth the churn).

> Can we salvage something from this bikeshedding exercise?  I wonder if
> it's time to use the C99 type _Bool in our code.  It would allow
> documenting that only two possible values exist in cases like the one
> above.  That would be even more useful for function returns, I assume.

Hmm, possibly. I guess that might have helped my confusion, and I do think returning bool for function returns would help make their meaning more clear (it would help distinguish them from the usual "0 for success" return values).

I don't even know that we'd need much of a weather-balloon patch. I think it would be valid to do:

  #ifndef bool
  #define bool int

to handle pre-C99 compilers (if there even are any these days). Of course we probably need some conditional magic to try to "#include <stdbool.h>" for the actual C99. I guess we could assume C99 by default and then add NO_STDBOOL as an escape hatch if anybody complains.

-Peff
Junio C Hamano· Dec 13, 2023, 15:17 UTC · re: Jeff King · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

Jeff King <peff@peff.net> writes:
> Without digging into the code, I had just assumed that flipped_block was
> used as an array index. But it really is a boolean, so I actually think
> "flipped_block = !flipped_block" would probably be the most clear (but
> IMHO not really worth the churn).
;-)
Show 10 quoted lines
> I don't even know that we'd need much of a weather-balloon patch. I
> think it would be valid to do:
>
>   #ifndef bool
>   #define bool int
>
> to handle pre-C99 compilers (if there even are any these days). Of
> course we probably need some conditional magic to try to "#include
> <stdbool.h>" for the actual C99. I guess we could assume C99 by default
> and then add NO_STDBOOL as an escape hatch if anybody complains.
Sounds good.
René Scharfe· Dec 14, 2023, 13:08 UTC · re: Jeff King · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

Am 13.12.23 um 09:01 schrieb Jeff King:
Show 6 quoted lines
> On Tue, Dec 12, 2023 at 11:30:03PM +0100, René Scharfe wrote:
>
>> I wonder if
>> it's time to use the C99 type _Bool in our code.  It would allow
>> documenting that only two possible values exist in cases like the one
>> above.  That would be even more useful for function returns, I assume.
Show 10 quoted lines
> I don't even know that we'd need much of a weather-balloon patch. I
> think it would be valid to do:
>
>   #ifndef bool
>   #define bool int
>
> to handle pre-C99 compilers (if there even are any these days). Of
> course we probably need some conditional magic to try to "#include
> <stdbool.h>" for the actual C99. I guess we could assume C99 by default
> and then add NO_STDBOOL as an escape hatch if anybody complains.

The semantics are slightly different in edge cases, so that fallback would not be fully watertight. E.g. consider:

   bool b(bool cond) {return cond == true;}
   bool b2(void) {return b(2);}

b() returns false if you give it false and true for anything else. b2() returns true.

With int as the fallback this becomes:
   int b(int cond) {return cond == 1;}
   int b2(void) {return b(2);}
Now only 1 is recognized as true, b2() returns 0 (false).

A coding rule to not compare bools could mitigate that. Or a rule to only use the values true and false in bool context and to only use logical operators on them.

René
Jeff King· Dec 14, 2023, 22:05 UTC · re: René Scharfe · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

On Thu, Dec 14, 2023 at 02:08:31PM +0100, René Scharfe wrote:
Show 16 quoted lines
> > I don't even know that we'd need much of a weather-balloon patch. I
> > think it would be valid to do:
> >
> >   #ifndef bool
> >   #define bool int
> >
> > to handle pre-C99 compilers (if there even are any these days). Of
> > course we probably need some conditional magic to try to "#include
> > <stdbool.h>" for the actual C99. I guess we could assume C99 by default
> > and then add NO_STDBOOL as an escape hatch if anybody complains.
> 
> The semantics are slightly different in edge cases, so that fallback
> would not be fully watertight.  E.g. consider:
> 
>    bool b(bool cond) {return cond == true;}
>    bool b2(void) {return b(2);}

Yeah. b2() is wrong for passing "2" to a bool. I assumed that the compiler would warn of that (at least for people on modern C99 compilers, not the fallback code), but it doesn't seem to. It's been a long time since I've worked on a code base that made us of "bool", but I guess that idea is that silently coercing a non-zero int to a bool is reasonable in many cases (e.g., "bool found_foo = count_foos()").

I guess one could argue that b() is also sub-optimal, as it should just say "return cond" or "return !cond" rather than explicitly comparing to true/false. But I won't be surprised if it happens from time to time.

> A coding rule to not compare bools could mitigate that.  Or a rule to
> only use the values true and false in bool context and to only use
> logical operators on them.

That seems more complex than we want if our goal is just supporting legacy systems that may or may not even exist. Given your example, I'd be more inclined to just do a weather-balloon adding <stdbool.h> to git-compat-util.h, and using "bool" in a single spot in the code. If nobody screams after a few releases, we can consider it OK. If they do, it's a trivial patch to convert back.

-Peff
Phillip Wood· Dec 15, 2023, 14:46 UTC · re: Jeff King · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

On 14/12/2023 22:05, Jeff King wrote:
Show 18 quoted lines
> On Thu, Dec 14, 2023 at 02:08:31PM +0100, René Scharfe wrote:
> 
>>> I don't even know that we'd need much of a weather-balloon patch. I
>>> think it would be valid to do:
>>>
>>>    #ifndef bool
>>>    #define bool int
>>>
>>> to handle pre-C99 compilers (if there even are any these days). Of
>>> course we probably need some conditional magic to try to "#include
>>> <stdbool.h>" for the actual C99. I guess we could assume C99 by default
>>> and then add NO_STDBOOL as an escape hatch if anybody complains.
>>
>> The semantics are slightly different in edge cases, so that fallback
>> would not be fully watertight.  E.g. consider:
>>
>>     bool b(bool cond) {return cond == true;}
>>     bool b2(void) {return b(2);}

Thanks for bring this up René, I had similar concerns when I saw the suggestion of using "int" as a fallback.

> Yeah. b2() is wrong for passing "2" to a bool.

I think it depends what you mean by "wrong" §6.3.1.2 of standard is quite clear that when any non-zero scalar value is converted to _Bool the result is "1"

Show 6 quoted lines
> I assumed that the
> compiler would warn of that (at least for people on modern C99
> compilers, not the fallback code), but it doesn't seem to. It's been a
> long time since I've worked on a code base that made us of "bool", but I
> guess that idea is that silently coercing a non-zero int to a bool is
> reasonable in many cases (e.g., "bool found_foo = count_foos()").

I guess it is also consistent with the way "if" and "while" consider a non-zero scalar value to be "true".

> I guess one could argue that b() is also sub-optimal, as it should just
> say "return cond" or "return !cond" rather than explicitly comparing to
> true/false. But I won't be surprised if it happens from time to time.

Even if it unlikely that we would directly compare a boolean variable to "true" or "false" it is certainly conceivable that we'd compare two boolean variables directly. For the integer fallback to be safe we'd need to write

	if (!cond_a == !cond_b)
rather than
	if (cond_a == cond_b)
Show 10 quoted lines
>> A coding rule to not compare bools could mitigate that.  Or a rule to
>> only use the values true and false in bool context and to only use
>> logical operators on them.
> 
> That seems more complex than we want if our goal is just supporting
> legacy systems that may or may not even exist. Given your example, I'd
> be more inclined to just do a weather-balloon adding <stdbool.h> to
> git-compat-util.h, and using "bool" in a single spot in the code. If
> nobody screams after a few releases, we can consider it OK. If they do,
> it's a trivial patch to convert back.

A weather-balloon seems like the safest route forward. We have been requiring C99 for two years now [1], hopefully there aren't any compilers out that claim to support C99 but don't provide "<stdbool.h>" (I did check online and the compiler on NonStop does support _Bool).

Best Wishes
Phillip

[1] 7bc341e21b (git-compat-util: add a test balloon for C99 support, 2021-12-01)

Junio C Hamano· Dec 15, 2023, 17:09 UTC · re: Phillip Wood · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

Phillip Wood <phillip.wood123@gmail.com> writes:
Show 10 quoted lines
> Even if it unlikely that we would directly compare a boolean variable
> to "true" or "false" it is certainly conceivable that we'd compare two
> boolean variables directly. For the integer fallback to be safe we'd
> need to write
>
> 	if (!cond_a == !cond_b)
>
> rather than
>
> 	if (cond_a == cond_b)

Eek, it defeats the benefit of using true Boolean type if we had to train ourselves to write the former, doesn't it?

Show 12 quoted lines
> A weather-balloon seems like the safest route forward. We have been
> requiring C99 for two years now [1], hopefully there aren't any
> compilers out that claim to support C99 but don't provide
> "<stdbool.h>" (I did check online and the compiler on NonStop does
> support _Bool).
>
> Best Wishes
>
> Phillip
>
> [1] 7bc341e21b (git-compat-util: add a test balloon for C99 support,
> 2021-12-01)
Nice to be reminded of this one.

The cited commit does not start to use any specific feature from C99, other than that we now require that the compiler claims C99 conformance by __STDC_VERSION__ set appropriately. The commit log message says C99 "provides a variety of useful features, including ..., many of which we already use.", which implies that our wish was to officially allow any and all features in C99 to be used in our codebase after a successful flight of this test balloon.

Now, I think we saw a successful flight of this test balloon by now. Is allowing all the C99 the next step we really want to take?

I still personally have an aversion against decl-after-statement and //-comments, not due to portability reasons at all, but because I find that the code is easier to read without it. But in principle, it is powerful to be able to say "OK, as long as the feature is in C99 you can use it", instead of having to decide on individual features, and I am not fundamentally against going that route if it is where people want to go.

Thanks.
René Scharfe· Dec 16, 2023, 10:46 UTC · re: Junio C Hamano · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

Am 15.12.23 um 18:09 schrieb Junio C Hamano:
Show 15 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
>
>> Even if it unlikely that we would directly compare a boolean variable
>> to "true" or "false" it is certainly conceivable that we'd compare two
>> boolean variables directly. For the integer fallback to be safe we'd
>> need to write
>>
>> 	if (!cond_a == !cond_b)
>>
>> rather than
>>
>> 	if (cond_a == cond_b)
>
> Eek, it defeats the benefit of using true Boolean type if we had to
> train ourselves to write the former, doesn't it?
Indeed.
Show 23 quoted lines
>> [1] 7bc341e21b (git-compat-util: add a test balloon for C99 support,
>> 2021-12-01)
>
> Nice to be reminded of this one.
>
> The cited commit does not start to use any specific feature from
> C99, other than that we now require that the compiler claims C99
> conformance by __STDC_VERSION__ set appropriately.  The commit log
> message says C99 "provides a variety of useful features, including
> ..., many of which we already use.", which implies that our wish was
> to officially allow any and all features in C99 to be used in our
> codebase after a successful flight of this test balloon.
>
> Now, I think we saw a successful flight of this test balloon by now.
> Is allowing all the C99 the next step we really want to take?
>
> I still personally have an aversion against decl-after-statement and
> //-comments, not due to portability reasons at all, but because I
> find that the code is easier to read without it. But in principle,
> it is powerful to be able to say "OK, as long as the feature is in
> C99 you can use it", instead of having to decide on individual
> features, and I am not fundamentally against going that route if it
> is where people want to go.

C99 added a lot of features, but we already use several of them. Support for individual features may vary, though -- who knows?

E.g. http://www.compilers.de/vbcc.html claims to support "most of ISO/IEC 9899:1999 (C99)", yet _Bool is not mentioned in its docs (but __STDC_VERSION__ 199901L is). It's not a particularly interesting compiler for us, but still a real-world example of selective C99 support.

The table at the bottom of https://en.cppreference.com/w/c/99 would be useful if it was filled out for more compilers. And it also doesn't mention _Bool and stdbool.h.

TenDRA and the M/o/Vfuscator are the only compilers without stdbool.h support on https://godbolt.org/ as far as I can see, but that website doesn't have a lot of commercial compilers (understandably).

So I guess in practice we still need to check each new feature, even though in theory we should be fine after the two-year test.

René
Phillip Wood· Dec 18, 2023, 16:18 UTC · re: Junio C Hamano · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

Hi Junio
On 15/12/2023 17:09, Junio C Hamano wrote:
Show 15 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
>> Even if it unlikely that we would directly compare a boolean variable
>> to "true" or "false" it is certainly conceivable that we'd compare two
>> boolean variables directly. For the integer fallback to be safe we'd
>> need to write
>>
>> 	if (!cond_a == !cond_b)
>>
>> rather than
>>
>> 	if (cond_a == cond_b)
> 
> Eek, it defeats the benefit of using true Boolean type if we had to
> train ourselves to write the former, doesn't it?

Yes, it's horrible - if for some reason it turns out that we cannot use "#include <stdbool.h>" everywhere I think we should drop it rather than providing a subtly incompatible fallback

Show 25 quoted lines
>> A weather-balloon seems like the safest route forward. We have been
>> requiring C99 for two years now [1], hopefully there aren't any
>> compilers out that claim to support C99 but don't provide
>> "<stdbool.h>" (I did check online and the compiler on NonStop does
>> support _Bool).
>>
>> Best Wishes
>>
>> Phillip
>>
>> [1] 7bc341e21b (git-compat-util: add a test balloon for C99 support,
>> 2021-12-01)
> 
> Nice to be reminded of this one.
> 
> The cited commit does not start to use any specific feature from
> C99, other than that we now require that the compiler claims C99
> conformance by __STDC_VERSION__ set appropriately.  The commit log
> message says C99 "provides a variety of useful features, including
> ..., many of which we already use.", which implies that our wish was
> to officially allow any and all features in C99 to be used in our
> codebase after a successful flight of this test balloon.
> 
> Now, I think we saw a successful flight of this test balloon by now.
> Is allowing all the C99 the next step we really want to take?
 >
Show 7 quoted lines
> I still personally have an aversion against decl-after-statement and
> //-comments, not due to portability reasons at all, but because I
> find that the code is easier to read without it. But in principle,
> it is powerful to be able to say "OK, as long as the feature is in
> C99 you can use it", instead of having to decide on individual
> features, and I am not fundamentally against going that route if it
> is where people want to go.

I'm not sure we necessarily want to say "use anything that is in C99" for several reasons.

  - Some features such as C99's variable length arrays are known to be
    problematic.
  - As you say above there maybe features that we think harm the
    readability of our code.
  - As René points out not all compilers necessarily support all
    features.

I think using _Bool could be useful for the reasons Peff outlined. As for other features I've written code that I think would have benefited from compound literals, but off the top of my head I can't think of any other C99 features that I personally wish we were using. I think that decl-after-statement is occasionally useful to declare a variable near where it is used in a long function body but it is much simpler just to ban it altogether and encourage people to break up long functions to make them more readable.

Best Wishes
Phillip
> Thanks.
> 
> 
René Scharfe· Dec 16, 2023, 10:47 UTC · re: Phillip Wood · lore

[PATCH] git-compat-util: convert skip_{prefix,suffix}{,_mem} to bool

Use the data type bool and its values true and false to document the binary return value of skip_prefix() and friends more explicitly.

This first use of stdbool.h, introduced with C99, is meant to check whether there are platforms that claim support for C99, as tested by 7bc341e21b (git-compat-util: add a test balloon for C99 support, 2021-12-01), but still lack that header for some reason.

A fallback based on a wider type, e.g. int, would have to deal with comparisons somehow to emulate that any non-zero value is true:

   bool b1 = 1;
   bool b2 = 2;
   if (b1 == b2) puts("This is true.");
   int i1 = 1;
   int i2 = 2;
   if (i1 == i2) puts("Not printed.");
   #define BOOLEQ(a, b) (!(a) == !(b))
   if (BOOLEQ(i1, i2)) puts("This is true.");

So we'd be better off using bool everywhere without a fallback, if possible. That's why this patch doesn't include any.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 git-compat-util.h | 42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)
Show changes to git-compat-util.h +22 −21
diff --git a/git-compat-util.h b/git-compat-util.h
index 3e7a59b5ff..603c97e3b3 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -225,6 +225,7 @@ struct strbuf;
 #include <stddef.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include <string.h>
 #ifdef HAVE_STRINGS_H
 #include <strings.h> /* for strcasecmp() */
@@ -684,11 +685,11 @@ report_fn get_warn_routine(void);
 void set_die_is_recursing_routine(int (*routine)(void));

 /*
- * If the string "str" begins with the string found in "prefix", return 1.
+ * If the string "str" begins with the string found in "prefix", return true.
  * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
  * the string right after the prefix).
  *
- * Otherwise, return 0 and leave "out" untouched.
+ * Otherwise, return false and leave "out" untouched.
  *
  * Examples:
  *
@@ -699,57 +700,58 @@ void set_die_is_recursing_routine(int (*routine)(void));
  *   [skip prefix if present, otherwise use whole string]
  *   skip_prefix(name, "refs/heads/", &name);
  */
-static inline int skip_prefix(const char *str, const char *prefix,
-			      const char **out)
+static inline bool skip_prefix(const char *str, const char *prefix,
+			       const char **out)
 {
 	do {
 		if (!*prefix) {
 			*out = str;
-			return 1;
+			return true;
 		}
 	} while (*str++ == *prefix++);
-	return 0;
+	return false;
 }

 /*
  * Like skip_prefix, but promises never to read past "len" bytes of the input
  * buffer, and returns the remaining number of bytes in "out" via "outlen".
  */
-static inline int skip_prefix_mem(const char *buf, size_t len,
-				  const char *prefix,
-				  const char **out, size_t *outlen)
+static inline bool skip_prefix_mem(const char *buf, size_t len,
+				   const char *prefix,
+				   const char **out, size_t *outlen)
 {
 	size_t prefix_len = strlen(prefix);
 	if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) {
 		*out = buf + prefix_len;
 		*outlen = len - prefix_len;
-		return 1;
+		return true;
 	}
-	return 0;
+	return false;
 }

 /*
- * If buf ends with suffix, return 1 and subtract the length of the suffix
- * from *len. Otherwise, return 0 and leave *len untouched.
+ * If buf ends with suffix, return true and subtract the length of the suffix
+ * from *len. Otherwise, return false and leave *len untouched.
  */
-static inline int strip_suffix_mem(const char *buf, size_t *len,
-				   const char *suffix)
+static inline bool strip_suffix_mem(const char *buf, size_t *len,
+				    const char *suffix)
 {
 	size_t suflen = strlen(suffix);
 	if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
-		return 0;
+		return false;
 	*len -= suflen;
-	return 1;
+	return true;
 }

 /*
- * If str ends with suffix, return 1 and set *len to the size of the string
- * without the suffix. Otherwise, return 0 and set *len to the size of the
+ * If str ends with suffix, return true and set *len to the size of the string
+ * without the suffix. Otherwise, return false and set *len to the size of the
  * string.
  *
  * Note that we do _not_ NUL-terminate str to the new length.
  */
-static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
+static inline bool strip_suffix(const char *str, const char *suffix,
+				size_t *len)
 {
 	*len = strlen(str);
 	return strip_suffix_mem(str, len, suffix);
--
2.43.0
Phillip Wood· Dec 18, 2023, 16:23 UTC · re: René Scharfe · lore

Re: [PATCH] git-compat-util: convert skip_{prefix,suffix}{,_mem} to bool

Hi René
On 16/12/2023 10:47, René Scharfe wrote:
Show 23 quoted lines
> Use the data type bool and its values true and false to document the
> binary return value of skip_prefix() and friends more explicitly.
> 
> This first use of stdbool.h, introduced with C99, is meant to check
> whether there are platforms that claim support for C99, as tested by
> 7bc341e21b (git-compat-util: add a test balloon for C99 support,
> 2021-12-01), but still lack that header for some reason.
> 
> A fallback based on a wider type, e.g. int, would have to deal with
> comparisons somehow to emulate that any non-zero value is true:
> 
>     bool b1 = 1;
>     bool b2 = 2;
>     if (b1 == b2) puts("This is true.");
> 
>     int i1 = 1;
>     int i2 = 2;
>     if (i1 == i2) puts("Not printed.");
>     #define BOOLEQ(a, b) (!(a) == !(b))
>     if (BOOLEQ(i1, i2)) puts("This is true.");
> 
> So we'd be better off using bool everywhere without a fallback, if
> possible.  That's why this patch doesn't include any.

Thanks for the comprehensive commit message, I agree that we'd be better off avoiding adding a fallback. The patch looks good, I did wonder if we really need to covert all of these functions for a test-balloon but the patch is still pretty small overall.

Best Wishes
Phillip
Show 111 quoted lines
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>   git-compat-util.h | 42 ++++++++++++++++++++++--------------------
>   1 file changed, 22 insertions(+), 20 deletions(-)
> 
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 3e7a59b5ff..603c97e3b3 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -225,6 +225,7 @@ struct strbuf;
>   #include <stddef.h>
>   #include <stdlib.h>
>   #include <stdarg.h>
> +#include <stdbool.h>
>   #include <string.h>
>   #ifdef HAVE_STRINGS_H
>   #include <strings.h> /* for strcasecmp() */
> @@ -684,11 +685,11 @@ report_fn get_warn_routine(void);
>   void set_die_is_recursing_routine(int (*routine)(void));
> 
>   /*
> - * If the string "str" begins with the string found in "prefix", return 1.
> + * If the string "str" begins with the string found in "prefix", return true.
>    * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
>    * the string right after the prefix).
>    *
> - * Otherwise, return 0 and leave "out" untouched.
> + * Otherwise, return false and leave "out" untouched.
>    *
>    * Examples:
>    *
> @@ -699,57 +700,58 @@ void set_die_is_recursing_routine(int (*routine)(void));
>    *   [skip prefix if present, otherwise use whole string]
>    *   skip_prefix(name, "refs/heads/", &name);
>    */
> -static inline int skip_prefix(const char *str, const char *prefix,
> -			      const char **out)
> +static inline bool skip_prefix(const char *str, const char *prefix,
> +			       const char **out)
>   {
>   	do {
>   		if (!*prefix) {
>   			*out = str;
> -			return 1;
> +			return true;
>   		}
>   	} while (*str++ == *prefix++);
> -	return 0;
> +	return false;
>   }
> 
>   /*
>    * Like skip_prefix, but promises never to read past "len" bytes of the input
>    * buffer, and returns the remaining number of bytes in "out" via "outlen".
>    */
> -static inline int skip_prefix_mem(const char *buf, size_t len,
> -				  const char *prefix,
> -				  const char **out, size_t *outlen)
> +static inline bool skip_prefix_mem(const char *buf, size_t len,
> +				   const char *prefix,
> +				   const char **out, size_t *outlen)
>   {
>   	size_t prefix_len = strlen(prefix);
>   	if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) {
>   		*out = buf + prefix_len;
>   		*outlen = len - prefix_len;
> -		return 1;
> +		return true;
>   	}
> -	return 0;
> +	return false;
>   }
> 
>   /*
> - * If buf ends with suffix, return 1 and subtract the length of the suffix
> - * from *len. Otherwise, return 0 and leave *len untouched.
> + * If buf ends with suffix, return true and subtract the length of the suffix
> + * from *len. Otherwise, return false and leave *len untouched.
>    */
> -static inline int strip_suffix_mem(const char *buf, size_t *len,
> -				   const char *suffix)
> +static inline bool strip_suffix_mem(const char *buf, size_t *len,
> +				    const char *suffix)
>   {
>   	size_t suflen = strlen(suffix);
>   	if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
> -		return 0;
> +		return false;
>   	*len -= suflen;
> -	return 1;
> +	return true;
>   }
> 
>   /*
> - * If str ends with suffix, return 1 and set *len to the size of the string
> - * without the suffix. Otherwise, return 0 and set *len to the size of the
> + * If str ends with suffix, return true and set *len to the size of the string
> + * without the suffix. Otherwise, return false and set *len to the size of the
>    * string.
>    *
>    * Note that we do _not_ NUL-terminate str to the new length.
>    */
> -static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
> +static inline bool strip_suffix(const char *str, const char *suffix,
> +				size_t *len)
>   {
>   	*len = strlen(str);
>   	return strip_suffix_mem(str, len, suffix);
> --
> 2.43.0
> 
Junio C Hamano· Dec 18, 2023, 20:19 UTC · re: Phillip Wood · lore

Re: [PATCH] git-compat-util: convert skip_{prefix,suffix}{,_mem} to bool

Phillip Wood <phillip.wood123@gmail.com> writes:
> Thanks for the comprehensive commit message, I agree that we'd be
> better off avoiding adding a fallback. The patch looks good, I did
> wonder if we really need to covert all of these functions for a
> test-balloon but the patch is still pretty small overall.

I do have to wonder, though, if we want to be a bit more careful than just blindly trusting the platform (i.e. <stdbool.h> might exist and __STDC_VERSION__ may say C99, but under the hood their implementation may be buggy and coerce the result of an assignment of 2 to be different from assigning true).

In any case, this is a good starting place. Let's queue it, see what happens, and then think about longer-term plans.

Thanks.
René Scharfe· Dec 19, 2023, 13:36 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-compat-util: convert skip_{prefix,suffix}{,_mem} to bool

Am 18.12.23 um 21:19 schrieb Junio C Hamano:
Show 12 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
>
>> Thanks for the comprehensive commit message, I agree that we'd be
>> better off avoiding adding a fallback. The patch looks good, I did
>> wonder if we really need to covert all of these functions for a
>> test-balloon but the patch is still pretty small overall.
>
> I do have to wonder, though, if we want to be a bit more careful
> than just blindly trusting the platform (i.e. <stdbool.h> might
> exist and __STDC_VERSION__ may say C99, but under the hood their
> implementation may be buggy and coerce the result of an assignment
> of 2 to be different from assigning true).

We could add a compile-time check like below. I can't decide if this would be prudent or paranoid. It's cheap, though, so perhaps just add this tripwire for non-conforming compilers without making a judgement?

René
Show changes to git-compat-util.h +1 −1
diff --git a/git-compat-util.h b/git-compat-util.h
index 603c97e3b3..8212feaa37 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -705,7 +705,7 @@ static inline bool skip_prefix(const char *str, const char *prefix,
 {
 	do {
 		if (!*prefix) {
-			*out = str;
+			*out = str + BUILD_ASSERT_OR_ZERO((bool)1 == (bool)2);
 			return true;
 		}
 	} while (*str++ == *prefix++);
Jeff King· Dec 21, 2023, 09:59 UTC · re: René Scharfe · lore

Re: [PATCH] git-compat-util: convert skip_{prefix,suffix}{,_mem} to bool

On Sat, Dec 16, 2023 at 11:47:21AM +0100, René Scharfe wrote:
Show 23 quoted lines
> Use the data type bool and its values true and false to document the
> binary return value of skip_prefix() and friends more explicitly.
> 
> This first use of stdbool.h, introduced with C99, is meant to check
> whether there are platforms that claim support for C99, as tested by
> 7bc341e21b (git-compat-util: add a test balloon for C99 support,
> 2021-12-01), but still lack that header for some reason.
> 
> A fallback based on a wider type, e.g. int, would have to deal with
> comparisons somehow to emulate that any non-zero value is true:
> 
>    bool b1 = 1;
>    bool b2 = 2;
>    if (b1 == b2) puts("This is true.");
> 
>    int i1 = 1;
>    int i2 = 2;
>    if (i1 == i2) puts("Not printed.");
>    #define BOOLEQ(a, b) (!(a) == !(b))
>    if (BOOLEQ(i1, i2)) puts("This is true.");
> 
> So we'd be better off using bool everywhere without a fallback, if
> possible.  That's why this patch doesn't include any.

Thanks for putting this together. I agree this is the right spot to end up for now (and that if for whatever reason we find that some platforms can't handle it, we probably should revert and not try the naive fallback).

-Peff
Jeff King· Dec 21, 2023, 09:56 UTC · re: Phillip Wood · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

On Fri, Dec 15, 2023 at 02:46:36PM +0000, Phillip Wood wrote:
Show 5 quoted lines
> > Yeah. b2() is wrong for passing "2" to a bool.
> 
> I think it depends what you mean by "wrong" §6.3.1.2 of standard is quite
> clear that when any non-zero scalar value is converted to _Bool the result
> is "1"

Yeah, sorry, I was being quite sloppy with my wording. I meant "wrong" as in "I would ideally flag this in review for being weird and confusing".

Of course there are many reasonable cases where you might pass an integer "foo" rather than explicitly booleanizing it with "!!foo". So I do agree it's a real potential problem (and I'm sufficiently convinced that we should avoid an "int" fallback if we can).

-Peff
phillip.wood123@gmail.com· Dec 21, 2023, 15:06 UTC · re: Jeff King · lore

Re: [PATCH] Use ^=1 to toggle between 0 and 1

Hi Peff
On 21/12/2023 09:56, Jeff King wrote:
Show 11 quoted lines
> On Fri, Dec 15, 2023 at 02:46:36PM +0000, Phillip Wood wrote:
> 
>>> Yeah. b2() is wrong for passing "2" to a bool.
>>
>> I think it depends what you mean by "wrong" §6.3.1.2 of standard is quite
>> clear that when any non-zero scalar value is converted to _Bool the result
>> is "1"
> 
> Yeah, sorry, I was being quite sloppy with my wording. I meant "wrong"
> as in "I would ideally flag this in review for being weird and
> confusing".
That makes sense, it certainly is confusing
> Of course there are many reasonable cases where you might pass an
> integer "foo" rather than explicitly booleanizing it with "!!foo". So I
> do agree it's a real potential problem (and I'm sufficiently convinced
> that we should avoid an "int" fallback if we can).

I had a look at gnulib the other day and the list of limitations in the documentation of their <stdbool.h> fallback makes it look quite unattractive. They helpfully list some compilers where _Bool is not implemented (IRIX, Tru64) or does not work correctly (HP-UX, AIX). As far as I can see all the bug reports cited are from 2003-2006 on obsolete compiler versions, hopefully _Bool is better supported these days.

Best Wishes
Phillip
> -Peff
AreaZR via GitGitGadget· Dec 18, 2024, 00:16 UTC · re: AtariDreams via GitGitGadget · lore

[PATCH v2] Use ^=1 to toggle between 0 and 1

From: Seija Kijin <doremylover123@gmail.com>

If it is known that an int is either 1 or 0, doing an exclusive or to switch instead of a modulus makes more sense and is more efficient.

Signed-off-by: Seija <doremylover123@gmail.com>
---
    Use ^=1 to toggle between 0 and 1
    
    If it is known that an int is either 1 or 0, doing an exclusive or to
    switch instead of a modulus makes more sense and is more efficient.
    
    Signed-off-by: Seija Kijin doremylover123@gmail.com
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1620%2FAreaZR%2Fbuffer-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1620/AreaZR/buffer-v2
Pull-Request: https://github.com/git/git/pull/1620
Range-diff vs v1:
 1:  1bdc62e1b19 ! 1:  5819a51526b Use ^=1 to toggle between 0 and 1
     @@ Commit message
          doing an exclusive or to switch instead of a
          modulus makes more sense and is more efficient.
      
     -    Signed-off-by: Seija Kijin doremylover123@gmail.com
     -
     - ## builtin/fast-export.c ##
     -@@ builtin/fast-export.c: static void anonymize_ident_line(const char **beg, const char **end)
     - 	struct ident_split split;
     - 	const char *end_of_header;
     - 
     --	out = &buffers[which_buffer++];
     --	which_buffer %= ARRAY_SIZE(buffers);
     -+	out = &buffers[which_buffer];
     -+	which_buffer ^= 1;
     - 	strbuf_reset(out);
     - 
     - 	/* skip "committer", "author", "tagger", etc */
     +    Signed-off-by: Seija <doremylover123@gmail.com>
      
       ## diff.c ##
      @@ diff.c: static void mark_color_as_moved(struct diff_options *o,
     @@ diff.c: static void mark_color_as_moved(struct diff_options *o,
       				flipped_block = 0;
       
      
     - ## ident.c ##
     -@@ ident.c: const char *fmt_ident(const char *name, const char *email,
     - 	int want_name = !(flag & IDENT_NO_NAME);
     - 
     - 	struct strbuf *ident = &ident_pool[index];
     --	index = (index + 1) % ARRAY_SIZE(ident_pool);
     -+	index ^= 1;
     - 
     - 	if (!email) {
     - 		if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
     -
       ## t/helper/test-path-utils.c ##
      @@ t/helper/test-path-utils.c: static int check_dotfile(const char *x, const char **argv,
       	int res = 0, expect = 1;
 diff.c                     | 2 +-
 t/helper/test-path-utils.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Show changes to 2 files +2 −2

diff.c, t/helper/test-path-utils.c

diff --git a/diff.c b/diff.c
index 266ddf18e73..5c2ac8d6fd1 100644
--- a/diff.c
+++ b/diff.c
@@ -1231,7 +1231,7 @@ static void mark_color_as_moved(struct diff_options *o,
 							    &pmb_nr);
 
 			if (contiguous && pmb_nr && moved_symbol == l->s)
-				flipped_block = (flipped_block + 1) % 2;
+				flipped_block ^= 1;
 			else
 				flipped_block = 0;
 
diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
index 3129aa28fd2..0810647c722 100644
--- a/t/helper/test-path-utils.c
+++ b/t/helper/test-path-utils.c
@@ -188,7 +188,7 @@ static int check_dotfile(const char *x, const char **argv,
 	int res = 0, expect = 1;
 	for (; *argv; argv++) {
 		if (!strcmp("--not", *argv))
-			expect = !expect;
+			expect ^= 1;
 		else if (expect != (is_hfs(*argv) || is_ntfs(*argv)))
 			res = error("'%s' is %s.git%s", *argv,
 				    expect ? "not " : "", x);

base-commit: 2ccc89b0c16c51561da90d21cfbb4b58cc877bf6
-- 
gitgitgadget
AreaZR via GitGitGadget· Dec 18, 2024, 00:42 UTC · re: AreaZR via GitGitGadget · lore

[PATCH v3] git: use ^=1 to toggle between 0 and 1

From: Seija Kijin <doremylover123@gmail.com>

If it is known that an int is either 1 or 0, doing an exclusive or to switch instead of a modulus makes more sense and is more efficient.

Signed-off-by: Seija <doremylover123@gmail.com>
---
    git: use ^=1 to toggle between 0 and 1
    
    If it is known that an int is either 1 or 0, doing an exclusive or to
    switch instead of a modulus makes more sense and is more efficient.
    
    Signed-off-by: Seija Kijin doremylover123@gmail.com
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1620%2FAreaZR%2Fbuffer-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1620/AreaZR/buffer-v3
Pull-Request: https://github.com/git/git/pull/1620
Range-diff vs v2:
 1:  5819a51526b ! 1:  f6e75d8eff0 Use ^=1 to toggle between 0 and 1
     @@ Metadata
      Author: Seija Kijin <doremylover123@gmail.com>
      
       ## Commit message ##
     -    Use ^=1 to toggle between 0 and 1
     +    git: use ^=1 to toggle between 0 and 1
      
          If it is known that an int is either 1 or 0,
          doing an exclusive or to switch instead of a
 diff.c                     | 2 +-
 t/helper/test-path-utils.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Show changes to 2 files +2 −2

diff.c, t/helper/test-path-utils.c

diff --git a/diff.c b/diff.c
index 266ddf18e73..5c2ac8d6fd1 100644
--- a/diff.c
+++ b/diff.c
@@ -1231,7 +1231,7 @@ static void mark_color_as_moved(struct diff_options *o,
 							    &pmb_nr);
 
 			if (contiguous && pmb_nr && moved_symbol == l->s)
-				flipped_block = (flipped_block + 1) % 2;
+				flipped_block ^= 1;
 			else
 				flipped_block = 0;
 
diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
index 3129aa28fd2..0810647c722 100644
--- a/t/helper/test-path-utils.c
+++ b/t/helper/test-path-utils.c
@@ -188,7 +188,7 @@ static int check_dotfile(const char *x, const char **argv,
 	int res = 0, expect = 1;
 	for (; *argv; argv++) {
 		if (!strcmp("--not", *argv))
-			expect = !expect;
+			expect ^= 1;
 		else if (expect != (is_hfs(*argv) || is_ntfs(*argv)))
 			res = error("'%s' is %s.git%s", *argv,
 				    expect ? "not " : "", x);

base-commit: 063bcebf0c917140ca0e705cbe0fdea127e90086
-- 
gitgitgadget
AreaZR via GitGitGadget· Dec 18, 2024, 02:38 UTC · re: AreaZR via GitGitGadget · lore

[PATCH v4] git: use ^=1 to toggle between 0 and 1

From: Seija Kijin <doremylover123@gmail.com>

If it is known that an int is either 1 or 0, doing an exclusive or to switch instead of a modulus makes more sense and is more efficient.

Signed-off-by: Seija Kijin <doremylover123@gmail.com>
---
    git: use ^=1 to toggle between 0 and 1
    
    If it is known that an int is either 1 or 0, doing an exclusive or to
    switch instead of a modulus makes more sense and is more efficient.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1620%2FAreaZR%2Fbuffer-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1620/AreaZR/buffer-v4
Pull-Request: https://github.com/git/git/pull/1620
Range-diff vs v3:
 1:  f6e75d8eff0 ! 1:  db1f0b1a323 git: use ^=1 to toggle between 0 and 1
     @@ Commit message
          doing an exclusive or to switch instead of a
          modulus makes more sense and is more efficient.
      
     -    Signed-off-by: Seija <doremylover123@gmail.com>
     +    Signed-off-by: Seija Kijin <doremylover123@gmail.com>
      
       ## diff.c ##
      @@ diff.c: static void mark_color_as_moved(struct diff_options *o,
 diff.c                     | 2 +-
 t/helper/test-path-utils.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Show changes to 2 files +2 −2

diff.c, t/helper/test-path-utils.c

diff --git a/diff.c b/diff.c
index 266ddf18e73..5c2ac8d6fd1 100644
--- a/diff.c
+++ b/diff.c
@@ -1231,7 +1231,7 @@ static void mark_color_as_moved(struct diff_options *o,
 							    &pmb_nr);
 
 			if (contiguous && pmb_nr && moved_symbol == l->s)
-				flipped_block = (flipped_block + 1) % 2;
+				flipped_block ^= 1;
 			else
 				flipped_block = 0;
 
diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
index 3129aa28fd2..0810647c722 100644
--- a/t/helper/test-path-utils.c
+++ b/t/helper/test-path-utils.c
@@ -188,7 +188,7 @@ static int check_dotfile(const char *x, const char **argv,
 	int res = 0, expect = 1;
 	for (; *argv; argv++) {
 		if (!strcmp("--not", *argv))
-			expect = !expect;
+			expect ^= 1;
 		else if (expect != (is_hfs(*argv) || is_ntfs(*argv)))
 			res = error("'%s' is %s.git%s", *argv,
 				    expect ? "not " : "", x);

base-commit: d882f382b3d939d90cfa58d17b17802338f05d66
-- 
gitgitgadget
AreaZR via GitGitGadget· Dec 18, 2024, 16:46 UTC · re: AreaZR via GitGitGadget · lore

[PATCH v5] git: use ^=1 to toggle between 0 and 1

From: Seija Kijin <doremylover123@gmail.com>

If it is known that an int is either 1 or 0, doing an exclusive or to switch instead of a modulus makes more sense and is more efficient.

Signed-off-by: Seija Kijin <doremylover123@gmail.com>
---
    git: use ^=1 to toggle between 0 and 1
    
    If it is known that an int is either 1 or 0, doing an exclusive or to
    switch instead of a modulus makes more sense and is more efficient.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1620%2FAreaZR%2Fbuffer-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1620/AreaZR/buffer-v5
Pull-Request: https://github.com/git/git/pull/1620
Range-diff vs v4:
 1:  db1f0b1a323 ! 1:  5bb2cf10062 git: use ^=1 to toggle between 0 and 1
     @@ diff.c: static void mark_color_as_moved(struct diff_options *o,
       			else
       				flipped_block = 0;
       
     -
     - ## t/helper/test-path-utils.c ##
     -@@ t/helper/test-path-utils.c: static int check_dotfile(const char *x, const char **argv,
     - 	int res = 0, expect = 1;
     - 	for (; *argv; argv++) {
     - 		if (!strcmp("--not", *argv))
     --			expect = !expect;
     -+			expect ^= 1;
     - 		else if (expect != (is_hfs(*argv) || is_ntfs(*argv)))
     - 			res = error("'%s' is %s.git%s", *argv,
     - 				    expect ? "not " : "", x);
 diff.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to diff.c +1 −1
diff --git a/diff.c b/diff.c
index 266ddf18e73..5c2ac8d6fd1 100644
--- a/diff.c
+++ b/diff.c
@@ -1231,7 +1231,7 @@ static void mark_color_as_moved(struct diff_options *o,
 							    &pmb_nr);
 
 			if (contiguous && pmb_nr && moved_symbol == l->s)
-				flipped_block = (flipped_block + 1) % 2;
+				flipped_block ^= 1;
 			else
 				flipped_block = 0;
 

base-commit: d882f382b3d939d90cfa58d17b17802338f05d66
-- 
gitgitgadget
AreaZR via GitGitGadget· Dec 18, 2024, 16:57 UTC · re: AreaZR via GitGitGadget · lore

[PATCH v6] git: use logical-not operator to toggle between 0 and 1

From: Seija Kijin <doremylover123@gmail.com>

If it is known that an int is either 1 or 0, using a logical-not to switch instead of a modulus makes more sense and is more efficient.

Signed-off-by: Seija Kijin <doremylover123@gmail.com>
---
    git: use logical-not operator to toggle between 0 and 1
    
    If it is known that an int is either 1 or 0, doing an exclusive or to
    switch instead of a modulus makes more sense and is more efficient.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1620%2FAreaZR%2Fbuffer-v6
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1620/AreaZR/buffer-v6
Pull-Request: https://github.com/git/git/pull/1620
Range-diff vs v5:
 1:  5bb2cf10062 ! 1:  0fe9e776177 git: use ^=1 to toggle between 0 and 1
     @@ Metadata
      Author: Seija Kijin <doremylover123@gmail.com>
      
       ## Commit message ##
     -    git: use ^=1 to toggle between 0 and 1
     +    git: use logical-not operator to toggle between 0 and 1
      
          If it is known that an int is either 1 or 0,
     -    doing an exclusive or to switch instead of a
     +    using a logical-not to switch instead of a
          modulus makes more sense and is more efficient.
      
          Signed-off-by: Seija Kijin <doremylover123@gmail.com>
     @@ diff.c: static void mark_color_as_moved(struct diff_options *o,
       
       			if (contiguous && pmb_nr && moved_symbol == l->s)
      -				flipped_block = (flipped_block + 1) % 2;
     -+				flipped_block ^= 1;
     ++				flipped_block = !flipped_block;
       			else
       				flipped_block = 0;
       
 diff.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to diff.c +1 −1
diff --git a/diff.c b/diff.c
index 266ddf18e73..48335971a4c 100644
--- a/diff.c
+++ b/diff.c
@@ -1231,7 +1231,7 @@ static void mark_color_as_moved(struct diff_options *o,
 							    &pmb_nr);
 
 			if (contiguous && pmb_nr && moved_symbol == l->s)
-				flipped_block = (flipped_block + 1) % 2;
+				flipped_block = !flipped_block;
 			else
 				flipped_block = 0;
 

base-commit: d882f382b3d939d90cfa58d17b17802338f05d66
-- 
gitgitgadget
Junio C Hamano· Dec 19, 2024, 10:35 UTC · re: AreaZR via GitGitGadget · lore

Re: [PATCH v6] git: use logical-not operator to toggle between 0 and 1

"AreaZR via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Seija Kijin <doremylover123@gmail.com>
>
> If it is known that an int is either 1 or 0,
This justification needs to be updated.

The point of using flip = !flip pattern is not that you do not assume "1 or 0", but follow "zero or non-zero", which is more idiomatic in C.

Other than that, 
>       -				flipped_block = (flipped_block + 1) % 2;
>      -+				flipped_block ^= 1;
>      ++				flipped_block = !flipped_block;
it indeed is a good change to avoid modulo-2 like this.
Thanks.
Junio C Hamano· Dec 18, 2024, 15:46 UTC · re: AreaZR via GitGitGadget · lore

Re: [PATCH v3] git: use ^=1 to toggle between 0 and 1

"AreaZR via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 5 quoted lines
> From: Seija Kijin <doremylover123@gmail.com>
>
> If it is known that an int is either 1 or 0,
> doing an exclusive or to switch instead of a
> modulus makes more sense and is more efficient.
FWIW, it is much more idiomatic in this codebase to say
	foo = !foo;

to flip the polarity of foo when foo is used as a Boolean. It is both more readable than toggling only the bottom bit, and it is much more robust. Here 'used as a Boolean' means 'is it zero, or is it non-zero?', the norm used in the C language.

If the reference to "foo", other than the place where the value of foo is consulted for the sole purpose of fliping between true-false, were to check if it is true or not, i.e.

	if (foo)
		do something;
	else
		do something else;

then at this "real" use site, only the zero-ness of the value matters. foo==0 does something different from foo==1, but the code behaves the same way as the case where foo==1, if foo==2 or foo==3.

But the code that flips by
	foo = 1 - foo;
	foo ^= 1;

makes an assumption different from and stricter than the real use site. It only allows foo==0 and foo==1 without a good reason.

But
	foo = !foo;

keeps the same assumption as the real use site, which is why we prefer that form.

And like it or not, it is natural to assume that 0 is false and everything else is true when writing in C, and especially in the codebase of this project. So let's not flip

	foo = !foo;
into
	foo ^= 1;

just to make it look different. Going the other way, or rewriting rewriting modulo 2 arithmetic into !foo form, would be more preferrable.

The "flipped_block" is used like so:
	if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS)
		set MOVED_LINE_ALT bit in flags word;
so it is very much Boolean whose zero-ness matters.

← back to recent threads