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

Re: [PATCH 4/3] Introduce 'convert_path_to_git()'

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
May 16, 2009, 17:27 UTC
Message-ID
<alpine.LFD.2.01.0905161008190.3301@localhost.localdomain>
In-Reply-To
<7vy6sxpn2q.fsf@alter.siamese.dyndns.org>
On Fri, 15 May 2009, Junio C Hamano wrote:
> 
> "Ugly" is not quite the word I am looking for.  "My gut feels that there
> has to be a way to write this more cleanly, but I am frustrated that I
> cannot come up with one" might be the word...

Well, we can certainly make it even more interesting, and more prone to work even when the word-size grows.

	#define MAX_SHIFT (8*sizeof(unsigned long))
	#define SHIFT_BITS(x,y)  ((x) << ((y) & (MAX_SHIFT-1)))
	#define EXPAND(x,bits) ((x) | SHIFT_BITS(x,bits))
	#define EXPAND2(x,bits) EXPAND(EXPAND(x,bits),bits*2)
	#define EXPAND4(x,bits) EXPAND2(EXPAND2(x,bits),bits*4)
	
	#define MASK80 EXPAND4(0x80808080ul,32)

and now it should work up to 256 bits without warnings or undefined behavior (shifting by the word-size or more is not well-specified, which is why it has the "MAX_SHIFT/SHIFT_BIT" magic)

Untested, of course. But it seems to work on 32-bit and 64-bit cases. I can only hope that it works for 128-bit and 256-bit cases too.

And yes, it depends on "sizeof(unsigned long)" being a power of two. We could avoid that dependency by turning the "& (MAX_SHIFT-1)" into a ?: operation that actually compares with the value, and then it would work for a 6-byte "unsigned long" too.

It fundamentally does depend on 8-bit bytes, of course, but so does the whole algorithm, so that's not much of a dependency.

IOW, I'm not claiming it's "truly portable". Just reasonably so.
			Linus
Previous: Junio C HamanoNext: Jens Kilian
Message 11 of 13 in “dir.c: clean up handling of 'path' parameter in read_directory_recursive()”
  1. 1/3 dir.c: clean up handling of 'path' parameter in read_directory_recursive()Linus Torvalds, May 14, 2009
  2. 2/3 Add 'fill_directory()' helper function for directory traversalLinus Torvalds, May 14, 2009
  3. 3/3 read_directory(): infrastructure for pathname character set conversionLinus Torvalds, May 14, 2009
  4. Linus TorvaldsMay 14, 2009
  5. Johannes SchindelinMay 14, 2009
  6. Aaron CohenMay 14, 2009
  7. Linus TorvaldsMay 14, 2009
  8. Linus TorvaldsMay 14, 2009
  9. 4/3 Introduce 'convert_path_to_git()'Linus Torvalds, May 15, 2009
  10. Junio C HamanoMay 16, 2009
  11. Linus TorvaldsMay 16, 2009
  12. Jens KilianMay 19, 2009
  13. John KoleszarMay 19, 2009

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.