Sorry, an error occurred while loading the content.
Browse Groups

• ## How fast is your GCD code? Here's mine...

(12)
• NextPrevious
• Good luck trying to decrypt this code... it is the fastest GCD code I have at present. //Binary bithacked algorithm, 260 nanosec on iMac 2.0 GHz. //Warren D.
Message 1 of 12 , Jan 3, 2012
View Source
Good luck trying to decrypt this code... it is the fastest
GCD code I have at present.

//Binary bithacked algorithm, 260 nanosec on iMac 2.0 GHz.
//Warren D. Smith December 2011.
//For comparison on same machine x += a%b takes 19.5 nanosec.
uint64 gcd64(uint64 a, uint64 b){
int s;
int64 d;
if( (a==0) || (b==0) ){ return( a|b ); } //gcd(0,x)=x
for(s=0; ((a|b)&1)==0; s++){ a >>= 1; b >>= 1; }
while((a&1)==0){ a >>= 1; }
for(;;){
if( (b|a) < 32 ){ return( ((uint64)SmallGCDarray[a][b]) << s ); } //32x32 precomputed array
while((b&1)==0){ b>>=1; }
d = b; d -= a;
d &= d>>63; //where 63+1=wordsize of uint64s
b -= d+d+a;
a += d; //the obvious "optimization" of this & previous line... makes it slower!
b >>= 1;
if(b==0){ return( a << s ); }
}
}
• ... I can t imagine that d=b-a; (...) a += d ; b -= d+a would be slower. Anyway, the compiler should know (for these 2-3 instances) what is the fastest code
Message 1 of 12 , Jan 3, 2012
View Source
> (...)
> d = b; d -= a;
> (...)
> b -= d+d+a;
> a += d; //the obvious "optimization" of this & previous line... makes it slower!
> (...)

I can't imagine that

d=b-a;
(...)
a += d ; b -= d+a

would be slower.
Anyway, the compiler should know (for these 2-3 instances) what is the
fastest code for a given processor. It may not be the same if you
compile it for another iThing.

Best wishes for the New Year to all of the primenumbers group,

Maximilian
• ... --it is slower! On my computer, anyhow.
Message 1 of 12 , Jan 3, 2012
View Source
--- In primenumbers@yahoogroups.com, Maximilian Hasler <maximilian.hasler@...> wrote:
>
> > (...)
> > d = b; d -= a;
> > (...)
> > b -= d+d+a;
> > a += d; //the obvious "optimization" of this & previous line... makes it slower!
> > (...)
>
>
> I can't imagine that
>
> d=b-a;
> (...)
> a += d ; b -= d+a
>
> would be slower.

--it is slower! On my computer, anyhow.
• Is it faster if you replace this: for(s=0; ((a|b)&1)==0; s++){ a = 1; b = 1; } while((a&1)==0){ a = 1; } with this: if ((a&1)==0) { s = __builtin_ctz(a|b);
Message 1 of 12 , Jan 3, 2012
View Source
Is it faster if you replace this:

for(s=0; ((a|b)&1)==0; s++){ a>>= 1; b>>= 1; }
while((a&1)==0){ a>>= 1; }

with this:

if ((a&1)==0) {
s = __builtin_ctz(a|b); b>>=s;
a >>= __builtin_ctz(a);
}

(or the equivalent intrinsic for your compiler)?

That would seem to take care of fast-pathing the cases where no
adjustment is made because a is odd, while taking advantage of
the CPU's ability to count trailing zeroes in a single instruction.

On 1/3/2012 11:30 AM, WarrenS wrote:
> Good luck trying to decrypt this code... it is the fastest
> GCD code I have at present.
>
> //Binary bithacked algorithm, 260 nanosec on iMac 2.0 GHz.
> //Warren D. Smith December 2011.
> //For comparison on same machine x += a%b takes 19.5 nanosec.
> uint64 gcd64(uint64 a, uint64 b){
> int s;
> int64 d;
> if( (a==0) || (b==0) ){ return( a|b ); } //gcd(0,x)=x
> for(s=0; ((a|b)&1)==0; s++){ a>>= 1; b>>= 1; }
> while((a&1)==0){ a>>= 1; }
> for(;;){
> if( (b|a)< 32 ){ return( ((uint64)SmallGCDarray[a][b])<< s ); } //32x32 precomputed array
> while((b&1)==0){ b>>=1; }
> d = b; d -= a;
> d&= d>>63; //where 63+1=wordsize of uint64s
> b -= d+d+a;
> a += d; //the obvious "optimization" of this& previous line... makes it slower!
> b>>= 1;
> if(b==0){ return( a<< s ); }
> }
> }
>
>
>
>
> ------------------------------------
>
> Unsubscribe by an email to: primenumbers-unsubscribe@yahoogroups.com
> The Prime Pages : http://www.primepages.org/
>
> Yahoo! Groups Links
>
>
>
>
>
• Make that: s=0; if ((a&1)==0) { s = __builtin_ctz(a|b); b =s; a = __builtin_ctz(a); }
Message 1 of 12 , Jan 3, 2012
View Source
Make that:

s=0; if ((a&1)==0) {
s = __builtin_ctz(a|b); b>>=s;
a>>= __builtin_ctz(a);
}

On 1/3/2012 2:40 PM, Jack Brennen wrote:
> Is it faster if you replace this:
>
> for(s=0; ((a|b)&1)==0; s++){ a>>= 1; b>>= 1; }
> while((a&1)==0){ a>>= 1; }
>
> with this:
>
> if ((a&1)==0) {
> s = __builtin_ctz(a|b); b>>=s;
> a>>= __builtin_ctz(a);
> }
>
> (or the equivalent intrinsic for your compiler)?
>
> That would seem to take care of fast-pathing the cases where no
> adjustment is made because a is odd, while taking advantage of
> the CPU's ability to count trailing zeroes in a single instruction.
>
>
> On 1/3/2012 11:30 AM, WarrenS wrote:
>> Good luck trying to decrypt this code... it is the fastest
>> GCD code I have at present.
>>
>> //Binary bithacked algorithm, 260 nanosec on iMac 2.0 GHz.
>> //Warren D. Smith December 2011.
>> //For comparison on same machine x += a%b takes 19.5 nanosec.
>> uint64 gcd64(uint64 a, uint64 b){
>> int s;
>> int64 d;
>> if( (a==0) || (b==0) ){ return( a|b ); } //gcd(0,x)=x
>> for(s=0; ((a|b)&1)==0; s++){ a>>= 1; b>>= 1; }
>> while((a&1)==0){ a>>= 1; }
>> for(;;){
>> if( (b|a)< 32 ){ return( ((uint64)SmallGCDarray[a][b])<< s ); } //32x32 precomputed array
>> while((b&1)==0){ b>>=1; }
>> d = b; d -= a;
>> d&= d>>63; //where 63+1=wordsize of uint64s
>> b -= d+d+a;
>> a += d; //the obvious "optimization" of this& previous line... makes it slower!
>> b>>= 1;
>> if(b==0){ return( a<< s ); }
>> }
>> }
>>
>>
>>
>>
>> ------------------------------------
>>
>> Unsubscribe by an email to: primenumbers-unsubscribe@yahoogroups.com
>> The Prime Pages : http://www.primepages.org/
>>
>> Yahoo! Groups Links
>>
>>
>>
>>
>>
>
>
>
> ------------------------------------
>
> Unsubscribe by an email to: primenumbers-unsubscribe@yahoogroups.com
> The Prime Pages : http://www.primepages.org/
>
> Yahoo! Groups Links
>
>
>
>
>
• Apologies once again, but I don t want to leave broken code out there... It should be: s=0; if ((a&1)==0) { s = __builtin_ctzll(a|b); b =s; a =
Message 1 of 12 , Jan 3, 2012
View Source
Apologies once again, but I don't want to leave broken code out there...
It should be:

s=0; if ((a&1)==0) {
s = __builtin_ctzll(a|b); b>>=s;
a>>= __builtin_ctzll(a);
}

On 1/3/2012 2:40 PM, Jack Brennen wrote:
> Is it faster if you replace this:
>
> for(s=0; ((a|b)&1)==0; s++){ a>>= 1; b>>= 1; }
> while((a&1)==0){ a>>= 1; }
>
> with this:
>
> if ((a&1)==0) {
> s = __builtin_ctz(a|b); b>>=s;
> a>>= __builtin_ctz(a);
> }
>
> (or the equivalent intrinsic for your compiler)?
>
> That would seem to take care of fast-pathing the cases where no
> adjustment is made because a is odd, while taking advantage of
> the CPU's ability to count trailing zeroes in a single instruction.
>
>
> On 1/3/2012 11:30 AM, WarrenS wrote:
>> Good luck trying to decrypt this code... it is the fastest
>> GCD code I have at present.
>>
>> //Binary bithacked algorithm, 260 nanosec on iMac 2.0 GHz.
>> //Warren D. Smith December 2011.
>> //For comparison on same machine x += a%b takes 19.5 nanosec.
>> uint64 gcd64(uint64 a, uint64 b){
>> int s;
>> int64 d;
>> if( (a==0) || (b==0) ){ return( a|b ); } //gcd(0,x)=x
>> for(s=0; ((a|b)&1)==0; s++){ a>>= 1; b>>= 1; }
>> while((a&1)==0){ a>>= 1; }
>> for(;;){
>> if( (b|a)< 32 ){ return( ((uint64)SmallGCDarray[a][b])<< s ); } //32x32 precomputed array
>> while((b&1)==0){ b>>=1; }
>> d = b; d -= a;
>> d&= d>>63; //where 63+1=wordsize of uint64s
>> b -= d+d+a;
>> a += d; //the obvious "optimization" of this& previous line... makes it slower!
>> b>>= 1;
>> if(b==0){ return( a<< s ); }
>> }
>> }
>>
>>
>>
>>
>> ------------------------------------
>>
>> Unsubscribe by an email to: primenumbers-unsubscribe@yahoogroups.com
>> The Prime Pages : http://www.primepages.org/
>>
>> Yahoo! Groups Links
>>
>>
>>
>>
>>
>
>
>
> ------------------------------------
>
> Unsubscribe by an email to: primenumbers-unsubscribe@yahoogroups.com
> The Prime Pages : http://www.primepages.org/
>
> Yahoo! Groups Links
>
>
>
>
>
• ... --thanks! That hack sped it up from 260 to 258 nanosec. You also need to add an s=0 to your code fragment otherwise s could be used without having been
Message 1 of 12 , Jan 3, 2012
View Source
--- In primenumbers@yahoogroups.com, Jack Brennen <jfb@...> wrote:
>
> Is it faster if you replace this:
>
> for(s=0; ((a|b)&1)==0; s++){ a>>= 1; b>>= 1; }
> while((a&1)==0){ a>>= 1; }
>
> with this:
>
> if ((a&1)==0) {
> s = __builtin_ctz(a|b); b>>=s;
> a >>= __builtin_ctz(a);
> }
>
> (or the equivalent intrinsic for your compiler)?
>
> That would seem to take care of fast-pathing the cases where no
> adjustment is made because a is odd, while taking advantage of
> the CPU's ability to count trailing zeroes in a single instruction.

--thanks! That hack sped it up from 260 to 258 nanosec.
You also need to add an "s=0" to your code fragment otherwise s could be used
without having been zeroed.

Bu then I applied your same idea in the second place and that sped it up to 142
nanosec!!! New code:

//Binary bithacked algorithm, 142 nanosec on iMac 2.0 GHz.
//Warren D. Smith December 2011.
//For comparison on same machine x += a%b takes 19.5 nanosec.
uint64 gcd64(uint64 a, uint64 b){
int s;
int64 d;
if( (a==0) || (b==0) ){ return( a|b ); } //gcd(0,x)=x

s=0;
if ((a&1)==0) { //this code improved here by Jack Brennen
s = __builtin_ctz(a|b); b>>=s;
a >>= __builtin_ctz(a);
}

while( (b|a) > 31 ){
b >>= __builtin_ctz(b);
d = b; d -= a;
d &= d>>63; //where 63+1=wordsize of uint64s
b -= d+d+a;
a += d;
b >>= 1;
if(b==0){ return( a << s ); }
}
return( ((uint64)SmallGCDarray[a][b]) << s ); //32x32 precomputed array
}
• Yes, as per Brennan s bug fix, the ctz s should be ctzll s. That also speeds it up to 139 nanosec.
Message 1 of 12 , Jan 3, 2012
View Source
Yes, as per Brennan's bug fix, the ctz's should be ctzll's.

That also speeds it up to 139 nanosec.
• From: WarrenS ... I think Bob S posted his highly tuned code to sci.math, or just possibly the Mersenne Forum, about 5 years ago. It had special cases for
Message 1 of 12 , Jan 4, 2012
View Source
From: WarrenS
> Yes, as per Brennan's bug fix, the
> ctz's should be ctzll's.
>
> That also speeds it up to 139 nanosec.

I think Bob S posted his highly tuned code to sci.math, or just possibly the Mersenne Forum, about 5 years ago. It had special cases for removing small multiples of the smaller number, which looked like it would annoy branch prediction units, which in theory he should have been taking into account. (I was on an Alpha in those days, and branch prediction was everything, so I was more hypersensitive to such bubbles. However, perhaps the code was good because the ifs were actually quite predictable; you'd need to try it out on a modern machine.)

Phil
• From: Jack Brennen ... Which is classic UB. Nacked-by: Phil
Message 1 of 12 , Jan 4, 2012
View Source
From: Jack Brennen <jfb@...>
> Apologies once again, but I don't
> want to leave broken code out there...

A noble aim. So why did you overlook this:

> >>     int64 d;
> >>       d&= d>>63;     //where 63+1=wordsize of uint64s

Which is classic UB.

Nacked-by: Phil
• From: WarrenS ... It s a sparse enough inner loop that I can easily imagine the increased dependency makes it slower. What s the latency
Message 1 of 12 , Jan 4, 2012
View Source
From: WarrenS <warren.wds@...>
> > > b -= d+d+a;
> > > a += d; //the obvious "optimization" of this
> & previous line... makes it slower!
> > > (...)
> >
> >
> > I can't imagine that
> > a += d ; b -= d+a
> > would be slower.
>
> --it is slower! On my computer, anyhow.

It's a sparse enough inner loop that I can easily imagine the increased dependency makes it slower. What's the latency of an add nowadays? I know it's crept up to about 6 in the past decade (at least on the SIMD units). Something like that's a huge bubble, and should definitely be avoided.

Phil
Your message has been successfully submitted and would be delivered to recipients shortly.
• Changes have not been saved
Press OK to abandon changes or Cancel to continue editing
• Your browser is not supported
Kindly note that Groups does not support 7.0 or earlier versions of Internet Explorer. We recommend upgrading to the latest Internet Explorer, Google Chrome, or Firefox. If you are using IE 9 or later, make sure you turn off Compatibility View.