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

Re: [PATCH maint-1.6.5 v2] block-sha1: avoid pointer conversion that violates alignment constraints

From
MCMichael Cree <mcree@orcon.net.nz>
Date
Jul 16, 2012, 09:53 UTC
Message-ID
<5003E4B1.605@orcon.net.nz>
In-Reply-To
<20120715212731.GG1986@burratino>
On 16/07/12 09:27, Jonathan Nieder wrote:
Show 22 quoted lines
> Michael Cree wrote:
> 
>> On 15/07/2012, at 8:50 AM, Jonathan Nieder wrote:
> 
>>>                   gcc takes full advantage by converting the get_be32
>>> calls back to a load and bswap and producing a whole bunch of
>>> unaligned access traps.
>>
>> Alpha does not have a bswap (or similar) instruction.
> [...]
>>                                    .  If the pointer is unaligned
>> then two neighbouring aligned 32bit loads are required to ensure
>> that all four bytes are loaded.  If the pointer is aligned then a
>> single 32bit load gets all the four bytes.  Having never looked at
>> the generated assembler code, I nevertheless suspect that is the
>> guts of the optimisation --- the compiler can eliminate an access to
>> memory if it knows the pointer is aligned.
> 
> How about:
> 
> 	gcc takes full advantage by using a single 32-bit load,
> 	resulting in a whole bunch of unaligned access traps.
Yes, that's good.

I just checked the generated assembler and I am correct except that I underestimated the number of memory accesses for the a priori known unaligned data case. It is actually doing four long-word memory access, i.e. a memory access for each individual byte despite that it only needs to do a maximum of two, so the optimisation for the a priori known aligned data saves three memory accesses, not just one. (And probably also saves on a load-load replay trap on the EV6 and later CPUs, i.e., a complete and very costly replay of the whole pipeline when the CPU realises just in the nick of time that it has cocked up the interrelationships between reordered load instructions.)

Cheers Michael.

Previous: Jonathan Nieder
Message 10 of 10 in “block-sha1: avoid unaligned accesses on some big-endian systems”
  1. block-sha1: avoid unaligned accesses on some big-endian systemsJonathan Nieder, Jul 14, 2012
  2. Linus TorvaldsJul 14, 2012
  3. Jonathan NiederJul 14, 2012
  4. Linus TorvaldsJul 14, 2012
  5. block-sha1: avoid pointer conversion that violates alignment constraintsJonathan Nieder, Jul 14, 2012
  6. Linus TorvaldsJul 14, 2012
  7. block-sha1: put macro arguments in parenthesesJonathan Nieder, Jul 14, 2012
  8. Michael CreeJul 15, 2012
  9. Jonathan NiederJul 15, 2012
  10. Michael CreeJul 16, 2012

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.