git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [GSoC] Use unsigned integral type for collection of bits

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 20, 2024, 00:32 UTC
Message-ID
<xmqq1q98ynyr.fsf@gitster.g>
In-Reply-To
<CAPig+cR5K=pQjK+7ZUyGn1M50RZ0pRD6kOPQgmp7qez_LNXcAg@mail.gmail.com>
Eric Sunshine <sunshine@sunshineco.com> writes:
Show 9 quoted lines
>> 'diff_filespec_is_binary' inside 'diff.c' would have to be changed.
>
> The code in question is not being used as a "bag of bits". Rather,
> it's a tristate binary with values "not-set", "true", and "false".
> Whereas a typical binary could be represented by a single bit, this
> one needs the extra bit to handle the "not-set" case. Moreover, it is
> idiomatic in the Git codebase for -1 to represent "not-set", so I
> think this code is fine as-is since its meaning is clear to those
> familiar with the codebase, thus does not need any changes made to it.

Correct. In general, bitfield structure members in our codebase should be already fine. Most of them are "unsigned : 1" and there is not enough room in a single-bit bitfield to go signed.

Show 6 quoted lines
> There are cases in the codebase in which a signed type is being used
> as a "bag of bits" instead of the more desirable unsigned type.
> ...
> So, refresh_index() is correctly expecting an unsigned value for
> `flags` but refresh() in `builtin/add.c` has undesirably declared
> `flags` as signed.

Thanks for a good example. In a signed flag word that is used as a bag of bits, the MSB is special, and unless you take advantage of that special casing (which happens almost never), you should use an unsigned word instead, to document that you are not doing anything funny with the MSB, like "the flag word is negative, so the MSB must be on", "I want to copy the bit immediately below MSB to MSB", etc.

Previous: Eric SunshineNext: eugenio gigante
Message 3 of 4 in “[GSoC] Use unsigned integral type for collection of bits”
  1. eugenio giganteFeb 18, 2024
  2. Eric SunshineFeb 18, 2024
  3. Junio C HamanoFeb 20, 2024
  4. eugenio giganteFeb 19, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.