From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111c93,6373b9200cc9eb65 X-Google-Attributes: gid111c93,public From: Bradley DeJong Subject: Re: assembly programming on alphas Date: 1998/12/12 Message-ID: <36729BE0.1A77@umn.edu> X-Deja-AN: 421548159 Content-Transfer-Encoding: 7bit References: <36704B54.55298E9F@xs4all.nl> Content-Type: text/plain; charset=us-ascii Organization: [poster's organization not specified] Mime-Version: 1.0 Reply-To: abuse@localhost Newsgroups: comp.os.linux.alpha This isn't my real e-mail address. Please respond to the news group. I don't think that there is a way to get egcs to generate asm with the C code as comments. You could write it? :^) The Alpha is strictly a load-store architecture. One of the implications of that is that to do a memory-to-memory copy, you have to load a value into a register and then store the contents of that register to a new location. As for the "fastest" way to do a large memcpy, that depends on implementation. For example, if BWX is supported, the code may/will look different. Check the chapter on "Software Considerations" in the Alpha Architecture Handbook to get a better idea of all the options. For the unaligned prefix, you are probably better off loading the naturallly aligned quadwords containing both the first source and first target bytes, then shifting and masking before writing the result. After that prefetch cache blocks and unroll the read/write loop to take advantage of all the memory bandwidth. The problem of course is dealing with arbitrarily aligned data. (See A.3 in the Alpha Architecture Handbook for more detail on the software issues involved with read/write performance.) Assuming that you want to move a block of 40 bytes (5 quad words) that: 1. is not naturally aligned 2. straddles a cacheline boundary 3. does not straddle a page boundary (don't want to worry about page faults) To a block of 40 bytes that: 1. is not naturally aligned 2. does not have the same alignment within its quadword 3. straddles a cacheline boundary 4. does not straddle a page boundary 5. does not overlap with the source block 6. does not cause cache conflicts with the first block (is not mapped to the same cacheline as any part of the first block) This gives you data that looks something like this: Src = 11111aaa aaaaabbb bbbbbccc cccccddd dddddeee eeeee111 Dest = 222yyyyy xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx zzz22222 Where: 1: is a byte value that should not be copied 2: is a byte value that should remain unchanged a, b, c, d, and e: are byte values that are copied x, y, and z: are byte values that will be overwritten Then, the code does something similar to: load the 1st Src quadword (11111aaa) load the 1st Dest quadword (222yyyyy) prefetch the first quadword of the next cacheline of Src prefetch the first quadword of the next cacheline of Dest Doing these 4 loads 1st ensures that we encounter as few waits for the memory system as possible. All of the desired memory is now being loaded into the L1 cache. load the 2nd Src quadword (aaaaabbb) load the 3rd Src quadword (bbbbbccc) load the 4th Src quadword (cccccddd) load the 5th Src quadword (dddddeee) (this gives us 8 aligned quadword loads including the 2 touches. Digital recommends using no more than 8 consecutive aligned quadword loads to avoid overwhelming the memory subsystem, so now do some calculation before loading the last 2 quadwords) Start by using the 1st value that was loaded. Don't use it immediately after loading, because you need to allow for memory latency. shift (11111aaa) to (aaa00000) shift (aaa00000) back to (000aaa00) shift (222yyyyy) to (00000222) shift (00000222) back to (22200000) shift (aaaaabbb) to (000000aa) shift (aaaaabbb) to (aaabbb00) AND (000000aa) and (000aaa00) to (000aaaaa) AND (000aaaaa) ans (22200000) to (222aaaaa) store (222aaaaa) to the 1st quadword of Dest shift (bbbbbccc) to (000000bb) shift (bbbbbccc) to (bbbccc00) AND (aaabbb00) and (000000bb) to (aaabbbbb) store (aaabbbbb) to the 2nd quadword of Dest If this doesn't allow enough time since the previous 8 loads, move these loads to a later slot. load the 6th Src quadword (eeeee111) load the 6th Dest quadword (zzz22222) continue shifting and storing until done. Compare that code sequence to the code needed to do a memcpy between 2 non-overlapping blocks of memory that are aligned on 128 byte boundaries (sufficient to ensure alignment with cache blocks for all current Alpha implementations) and that have no cacheline conflicts. prefetch 1st quadword of Dest load 1st quadword of Src load 2nd quadword of Src load 3rd quadword of Src load 4th quadword of Src prefetch 5th quadword of Dest load 5th quadword of Src store 1st quadword to Dest store 2nd quadword to Dest store 3rd quadword to Dest store 4th quadword to Dest store 5th quadword to Dest You can see that if the compiler follows the additional alignment restrictions, the code can be much simpler/faster. Of course this also means that the compiler has to be smart enough to generate good code. Additional considerations due to calling conventions: With the Alpha architecture, you have 31 usable registers, so you can do this kind of load/manipulate/store sequence without running out of registers. Of course, you have to keep in mind the calling conventions of the OS you are using (does anybody know what they are on Linux/Alpha?). For example, 1 register is probably reserved for function call return addresses, 1 for compiler use, 8 for values that are maintained across function call boundaries, 8 for scratch use within a function, 8 for parameter passing, .... Even so, you can usually work out a code sequence that gives good performance. As an additional reference/case study, www.microway.com has a nice white paper on the kind of optimizations their compilers use for floating point code on Alpha. Bram wrote: > > Hello, > > I've been reading DEC's Assembly Language Programmer's Guide. > But I have a couple of questions. > > How can I have egcs generate more verbose assembly output? > The samples in the guide show the C-source as comments in > the assembly file. > The output from egcc -S does not give these. > > Also, isn't there a more convenient method for memory > to memory copying? It seems that the only way to move > data from memory to memory, is by loading, and then > storing registers. > On Intel, I use movsb/movsw/movsl a lot, but there > does not seem to be an alpha-equivalent. > > I am curious how memcpy is implemented on the alpha. > What is the fastest way to copy, say, 32 bytes or more > from one location to another? And what if the data > is not alligned? > > thanks in advance, > > Bram Stolk