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

Re: [PATCH] use DIV_ROUND_UP

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Jul 9, 2017, 15:14 UTC
Message-ID
<CAN0heSpaLqfD_ZU=6K=bQs3E4iBj0BjQEVruivc7LXCWoXi2tg@mail.gmail.com>
In-Reply-To
<be5b6900-9e5e-0796-c16a-03c1b72cfa50@web.de>
On 9 July 2017 at 16:01, René Scharfe <l.s.r@web.de> wrote:
Show 46 quoted lines
> Am 09.07.2017 um 15:25 schrieb Martin Ågren:
>> On 8 July 2017 at 12:35, René Scharfe <l.s.r@web.de> wrote:
>>> Convert code that divides and rounds up to use DIV_ROUND_UP to make the
>>> intent clearer and reduce the number of magic constants.
>> ...
>>> diff --git a/sha1_name.c b/sha1_name.c
>>> index e7f7b12ceb..8c513dbff6 100644
>>> --- a/sha1_name.c
>>> +++ b/sha1_name.c
>>> @@ -492,7 +492,7 @@ int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
>>>                   * together we need to divide by 2; but we also want to round
>>>                   * odd numbers up, hence adding one before dividing.
>>>                   */
>>> -               len = (len + 1) / 2;
>>> +               len = DIV_ROUND_UP(len, 2);
>>
>> Since the addition is now an implementation detail of DIV_ROUND_UP,
>> should the comment be adjusted, maybe simply by removing ", hence
>> adding one before dividing"?
>>
>> Or perhaps even better, "... divide by 2; but since len might be odd,
>> we need to make sure we round up as we divide". My thinking being,
>> we're not actually rounding odd numbers up (presumably to even
>> numbers), but we're rounding the result of the division up (to the
>> smallest larger integer).
>
> Good point; perhaps just squash this in?
>
> ---
>  sha1_name.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/sha1_name.c b/sha1_name.c
> index 8c513dbff6..74fcb6d788 100644
> --- a/sha1_name.c
> +++ b/sha1_name.c
> @@ -489,8 +489,7 @@ int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
>                  * We now know we have on the order of 2^len objects, which
>                  * expects a collision at 2^(len/2). But we also care about hex
>                  * chars, not bits, and there are 4 bits per hex. So all
> -                * together we need to divide by 2; but we also want to round
> -                * odd numbers up, hence adding one before dividing.
> +                * together we need to divide by 2 and round up.
>                  */
>                 len = DIV_ROUND_UP(len, 2);
>                 /*
Much better than my suggestions.
Previous: René ScharfeNext: Jeff King
Message 4 of 7 in “use DIV_ROUND_UP”
  1. use DIV_ROUND_UPRené Scharfe, Jul 8, 2017
  2. Martin ÅgrenJul 9, 2017
  3. René ScharfeJul 9, 2017
  4. Martin ÅgrenJul 9, 2017
  5. Jeff KingJul 10, 2017
  6. René ScharfeJul 10, 2017
  7. Jeff KingJul 11, 2017

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.