Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: One (silly) question about "C"

Author: Dieter Buerssner

Date: 10:42:46 02/05/02

Go up one level in this thread


On February 05, 2002 at 13:07:02, Miguel A. Ballicora wrote:

>On February 05, 2002 at 12:55:35, Uri Blass wrote:
>
>>On February 05, 2002 at 12:10:21, Tom Likens wrote:
>>
>>>On February 05, 2002 at 08:32:35, Antonio Senatore wrote:
>>>
>>>>Hi friends:
>>>>
>>>>I have one array A[n] and another B[n] (both of the same dimension) and I want
>>>>to make A = B without using a loop like
>>>>
>>>>for (i=0; i < n; i++) A[i] = B[i];
>>>>
>>>>My question is if is it possible to do that without using none kind of loops
>>>>(and as I am working in C, I can't work with vectors or to use the lib
>>>>"algorithm")
>>>>
>>>>Thanks in advance
>>>>
>>>>Antonio
>>>
>>>Here ya go, here's a "simple" way to copy an array :)
>>>Note, from and to are what they imply and count holds the number of
>>>items to copy.
>>>
>>>    register int n=(count+7)/8;
>>>
>>>    switch(count & 7){
>>>        case 0: do { *to++ = *from++;
>>>        case 7:      *to++ = *from++;
>>>        case 6:      *to++ = *from++;
>>>        case 5:      *to++ = *from++;
>>>        case 4:      *to++ = *from++;
>>>        case 3:      *to++ = *from++;
>>>        case 2:      *to++ = *from++;
>>>        case 1:      *to++ = *from++;
>>>                } while(--n > 0);
>>>    }
>>>
>>>This was invented by Tom Duff a number of years back when he was at
>>>Lucas Films.  By the way, just to head off the question before it's
>>>asked, - Yep this is *legal* ANSI C.
>>
>>Is there an advantage relative to the simple way of
>>for (i=0; i < n; i++) A[i] = B[i];
>>or using memcpy?

Probably, with most modern compilers memcpy will be fastest. It is typically
inlined, and efficient code will be produced. This was not allways the case. And
on some architectures, with not well optimizing compilers, the indexed access
(A[i] = B[i]) produced slower code than the pointer incrementing version (*to++
= *from++). I think, one should best forget about this subtle differences and
trust the compilers.

The above technique is called loop unrolling. It saves some branches compared to
the "normal" code. One can easily write it a bit less obfusucating, by not
jumping directly into the middle of the loop, but rather have first a do while,
and then a switch.

Gcc has a switch -unroll-loops, that does similar loop unrolling automatically.
However in some applications I have seen advantages by doing it manually
(because the programmer may know better than the compiler, at which points it is
worthwhile, or may know more restrictions about the length of the loop, than the
compiler).

Regards,
Dieter




This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.