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

Re: [PATCH] block-sha1: more good unaligned memory access candidates

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Aug 13, 2009, 16:45 UTC
Message-ID
<alpine.LFD.2.01.0908130934400.28882@localhost.localdomain>
In-Reply-To
<alpine.LFD.2.00.0908130017080.10633@xanadu.home>
On Thu, 13 Aug 2009, Nicolas Pitre wrote:
Show 5 quoted lines
>
> In addition to X86, PowerPC and S390 are capable of unaligned memory 
> accesses.
> 
> Signed-off-by: Nicolas Pitre <nico@cam.org>
Ack on all your patches (1-3 + this). Looks fine to me.

I do wonder if we should try to do basically "per-architecture hack header-files", and then have for each architecture a small trivial 'hack-x86.h' kind of thing that just does

	/* x86 hacks */
	#define get_be32(p)	ntohl(*(unsigned int *)(p))
	#define put_be32(p, v)	do { *(unsigned int *)(p) = htonl(v); } while (0)
	#define setW(x, val)	(*(volatile unsigned int *)&W(x) = (val))
	#define SHA_ASM(op, x, n) ({ unsigned int __res; __asm__(op " %1,%0":"=r" (__res):"i" (n), "0" (x)); __res; })
	#define SHA_ROL(x,n)   SHA_ASM("rol", x, n)
	#define SHA_ROR(x,n)   SHA_ASM("ror", x, n)

and then we'd have each architecture separated out. Add a few "generic helpers":

 - be32-generic.h:
	#define get_be32(p)    ( \
		(*((unsigned char *)(p) + 0) << 24) | \
		(*((unsigned char *)(p) + 1) << 16) | \
		(*((unsigned char *)(p) + 2) <<  8) | \
		(*((unsigned char *)(p) + 3) <<  0) )
	#define put_be32(p, v) do { \
		unsigned int __v = (v); \
		*((unsigned char *)(p) + 0) = __v >> 24; \
		*((unsigned char *)(p) + 1) = __v >> 16; \
		*((unsigned char *)(p) + 2) = __v >>  8; \
		*((unsigned char *)(p) + 3) = __v >>  0; } while (0)
 - rotate-generic.h:
	#define SHA_ROT(X,l,r)  (((X) << (l)) | ((X) >> (r)))
	#define SHA_ROL(X,n)    SHA_ROT(X,n,32-(n))
	#define SHA_ROR(X,n)    SHA_ROT(X,32-(n),n)

that architectures could use when they want to use some particular portable version. Then, add a final "hack-generic.h" with the fallback cases that just does

	#include "be32-generic.h"
	#include "rotate-generic.h"
	#define setW(x,val) (W(x) = (val))

and you'd have all the hacks separated out and fairly easily used by different architectures.. (ie the ARM version would just look like

 - hack-arm.h:
	#include "be32-generic.h"  
	#include "rotate-generic.h"
	#define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
and you'd be all done.

Hmm? I don't know if this kind of generalization is strictly needed, so I'm just throwing it out as an idea.

		Linus
Previous: Nicolas PitreNext: Nicolas Pitre
Message 2 of 15 in “block-sha1: more good unaligned memory access candidates”
  1. block-sha1: more good unaligned memory access candidatesNicolas Pitre, Aug 13, 2009
  2. Linus TorvaldsAug 13, 2009
  3. Nicolas PitreAug 13, 2009
  4. Junio C HamanoAug 13, 2009
  5. Linus TorvaldsAug 13, 2009
  6. Nicolas PitreAug 13, 2009
  7. block-sha1/sha1.c: silence compiler complaints by casting void* to uintptr_tBrandon Casey, Aug 14, 2009
  8. Johannes SchindelinAug 15, 2009
  9. Linus TorvaldsAug 15, 2009
  10. Brandon CaseyAug 15, 2009
  11. Junio C HamanoAug 15, 2009
  12. Brandon CaseyAug 15, 2009
  13. Junio C HamanoAug 15, 2009
  14. Linus TorvaldsAug 15, 2009
  15. Brandon CaseyAug 15, 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.