```Issue #15166 has been updated by jzakiya (Jabari Zakiya).

Hi

I just submitted this issue feature request:

https://bugs.ruby-lang.org/issues/15172

to deal with the issue of using (or not) the ``__builtin_ctz`` compiler directive.

I implemented code that mimicked it that also greatly increases the ruby ``gcd`` performance.
I included the new code and benchmarks to the gist I previously linked.

https://gist.github.com/jzakiya/44eae4feeda8f6b048e19ff41a0c6566

```
[jzakiya@jabari-pc ~]\$ ./gcd2
gcd between numbers in [1 and 2000]

gcdwikipedia7fast32   :  time = 73
gcdwikipedia4fast     :  time = 113
gcdFranke             :  time = 133
gcdwikipedia3fast     :  time = 139
gcdwikipedia2fastswap :  time = 162
gcdwikipedia5fast     :  time = 140
gcdwikipedia7fast     :  time = 129
gcdwikipedia2fast     :  time = 161
gcdwikipedia6fastxchg :  time = 145
gcdwikipedia2fastxchg :  time = 168
gcd_iterative_mod     :  time = 230
gcd_recursive         :  time = 232
basicgcd              :  time = 234
rubygcd               :  time = 305
gcdwikipedia2         :  time = 312
gcdwikipedia7fast32_a :  time = 129
gcdwikipedia4fast_a   :  time = 149
rubygcd_a             :  time = 193
rubygcd_b             :  time = 169
gcd between numbers in [1000000001 and 1000002000]

gcdwikipedia7fast32   :  time = 76
gcdwikipedia4fast     :  time = 106
gcdFranke             :  time = 121
gcdwikipedia3fast     :  time = 127
gcdwikipedia2fastswap :  time = 153
gcdwikipedia5fast     :  time = 126
gcdwikipedia7fast     :  time = 118
gcdwikipedia2fast     :  time = 148
gcdwikipedia6fastxchg :  time = 134
gcdwikipedia2fastxchg :  time = 154
gcd_iterative_mod     :  time = 215
gcd_recursive         :  time = 214
basicgcd              :  time = 220
rubygcd               :  time = 287
gcdwikipedia2         :  time = 289
gcdwikipedia7fast32_a :  time = 116
gcdwikipedia4fast_a   :  time = 142
rubygcd_a             :  time = 180
rubygcd_b             :  time = 155
```

Note using the ``__builtin_ctz`` mimicking code, instead of the directive itself,
still makes the ``gcdwikipedia7fast32_a`` the third fastest version, and obviously
the preferred implementation if not using ``__builtin_ctz``.

I present this in asking you how you want me to proceed, because I don't really know
C code and how to do PRs to Ruby. If you can lay out a detailed process for me to do
that maybe I can assess what is in my capacity to do.

At minimum, the code for ``rubygcd_a`` could|can be incorporated into the codebase
without dealing right now with the ``__builtin_ctz`` directive issue.

----------------------------------------
Feature #15166: 2.5 times faster implementation than current gcd implmentation
https://bugs.ruby-lang.org/issues/15166#change-74224

* Author: jzakiya (Jabari Zakiya)
* Status: Assigned
* Priority: Normal
* Assignee: watson1978 (Shizuo Fujita)
* Target version:
----------------------------------------
This is to be more explicit (and accurate) than https://bugs.ruby-lang.org/issues/15161

This is my modified gcd benchmarks code, originally presented by Daniel Lemire (see 15161).

https://gist.github.com/jzakiya/44eae4feeda8f6b048e19ff41a0c6566

Ruby's current implementation of Stein's gcd algorithm is only slightly faster than the
code posted on the wikepedia page, and over 2.5 times slower than the fastest implementation
in the benchmarks.

```
[jzakiya@localhost ~]\$ ./gcdbenchmarks
gcd between numbers in [1 and 2000]

gcdwikipedia7fast32   :  time = 99
gcdwikipedia4fast     :  time = 121
gcdFranke             :  time = 126
gcdwikipedia3fast     :  time = 134
gcdwikipedia2fastswap :  time = 136
gcdwikipedia5fast     :  time = 139
gcdwikipedia7fast     :  time = 138
gcdwikipedia2fast     :  time = 136
gcdwikipedia6fastxchg :  time = 144
gcdwikipedia2fastxchg :  time = 156
gcd_iterative_mod     :  time = 210
gcd_recursive         :  time = 215
basicgcd              :  time = 211
rubygcd               :  time = 267
gcdwikipedia2         :  time = 321
gcd between numbers in [1000000001 and 1000002000]

gcdwikipedia7fast32   :  time = 100
gcdwikipedia4fast     :  time = 121
gcdFranke             :  time = 126
gcdwikipedia3fast     :  time = 134
gcdwikipedia2fastswap :  time = 136
gcdwikipedia5fast     :  time = 138
gcdwikipedia7fast     :  time = 138
gcdwikipedia2fast     :  time = 136
gcdwikipedia6fastxchg :  time = 144
gcdwikipedia2fastxchg :  time = 156
gcd_iterative_mod     :  time = 210
gcd_recursive         :  time = 215
basicgcd              :  time = 211
rubygcd               :  time = 269
gcdwikipedia2         :  time = 323
```

This is Ruby's code per: https://github.com/ruby/ruby/blob/3abbaab1a7a97d18f481164c7dc48749b86d7f39/rational.c#L285-L307
which is basically the wikepedia implementation.

```
inline static long
i_gcd(long x, long y)
{
unsigned long u, v, t;
int shift;

if (x < 0)
x = -x;
if (y < 0)
y = -y;

if (x == 0)
return y;
if (y == 0)
return x;

u = (unsigned long)x;
v = (unsigned long)y;
for (shift = 0; ((u | v) & 1) == 0; ++shift) {
u >>= 1;
v >>= 1;
}

while ((u & 1) == 0)
u >>= 1;

do {
while ((v & 1) == 0)
v >>= 1;

if (u > v) {
t = v;
v = u;
u = t;
}
v = v - u;
} while (v != 0);

return (long)(u << shift);
}
```

This is the fastest implementation from the benchmarks. (I originally, wrongly, cited
the implementation in the article, which is 4|5th fastest in benchmarks, but
still almost 2x faster than the Ruby implementation.)

```
// based on wikipedia's article,
// fixed by D. Lemire,  K. Willets
unsigned int gcdwikipedia7fast32(unsigned int u, unsigned int v)
{
int shift, uz, vz;
if ( u == 0) return v;
if ( v == 0) return u;
uz = __builtin_ctz(u);
vz = __builtin_ctz(v);
shift = uz > vz ? vz : uz;
u >>= uz;
do {
v >>= vz;
int diff = v;
diff -= u;
if ( diff == 0 ) break;
vz = __builtin_ctz(diff);
if ( v <  u ) u = v;
v = abs(diff);

} while( 1 );
return u << shift;
}
```

The key to speeding up all the algorithms is using the ``__builtin_ctz(x)`` directive
to determine the number of trailing binary '0's.

--
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7155598,"math_prob":0.9843443,"size":5689,"snap":"2020-34-2020-40","text_gpt3_token_len":1820,"char_repetition_ratio":0.24151275,"word_repetition_ratio":0.15081967,"special_character_ratio":0.36034453,"punctuation_ratio":0.19017763,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9883404,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-25T14:42:00Z\",\"WARC-Record-ID\":\"<urn:uuid:a7d1b371-4063-4113-ae5d-6e43093b5696>\",\"Content-Length\":\"11145\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0fc8769d-7f8b-45dc-a372-43b9cd8abf53>\",\"WARC-Concurrent-To\":\"<urn:uuid:19c18c15-337b-48d3-b38e-bf7d40dc4702>\",\"WARC-IP-Address\":\"133.44.98.95\",\"WARC-Target-URI\":\"http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/89199\",\"WARC-Payload-Digest\":\"sha1:ERTY6NMK57O5SQ77IPY5FP7XPZBP6N5J\",\"WARC-Block-Digest\":\"sha1:XTT5QPVHWIBXNHHXYVQ2RO266SWMZCTJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400226381.66_warc_CC-MAIN-20200925115553-20200925145553-00509.warc.gz\"}"}