git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Junio C Hamano <gitster@pobox.com>
Cc: Git Mailing List <git@vger.kernel.org>
Subject: Re: [PATCH 4/3] Introduce 'convert_path_to_git()'
Date: Sat, 16 May 2009 10:27:33 -0700 (PDT)	[thread overview]
Message-ID: <alpine.LFD.2.01.0905161008190.3301@localhost.localdomain> (raw)
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

  reply	other threads:[~2009-05-16 17:28 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-14 20:42 [PATCH 1/3] dir.c: clean up handling of 'path' parameter in read_directory_recursive() Linus Torvalds
2009-05-14 20:46 ` [PATCH 2/3] Add 'fill_directory()' helper function for directory traversal Linus Torvalds
2009-05-14 20:54   ` [PATCH 3/3] read_directory(): infrastructure for pathname character set conversion Linus Torvalds
2009-05-14 21:23     ` Linus Torvalds
2009-05-14 22:19     ` Johannes Schindelin
2009-05-14 22:36       ` Aaron Cohen
2009-05-14 22:51         ` Linus Torvalds
2009-05-14 22:47       ` Linus Torvalds
2009-05-15 19:01     ` [PATCH 4/3] Introduce 'convert_path_to_git()' Linus Torvalds
2009-05-16  6:40       ` Junio C Hamano
2009-05-16 17:27         ` Linus Torvalds [this message]
2009-05-19 12:20         ` Jens Kilian
2009-05-19 13:31           ` John Koleszar

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=alpine.LFD.2.01.0905161008190.3301@localhost.localdomain \
    --to=torvalds@linux-foundation.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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).