Project

General

Profile

Feature #15166 » rational.c.patch

gcd - ahorek (Pavel Rosický), 12/28/2018 06:32 PM

View differences:

rational.c (working copy)
271 271
inline static long
272 272
i_gcd(long x, long y)
273 273
{
274
    unsigned long u, v, t;
275
    int shift;
274
    unsigned long u, v, uz, vz, shift;
275
    long diff;
276 276

  
277 277
    if (x < 0)
278
	x = -x;
278
        x = -x;
279 279
    if (y < 0)
280
	y = -y;
281

  
280
        y = -y;
282 281
    if (x == 0)
283
	return y;
284
    if (y == 0)
285
	return x;
282
        return y;
283
    if (y == 0 || x == y)
284
        return x;
286 285

  
287 286
    u = (unsigned long)x;
288 287
    v = (unsigned long)y;
289
    for (shift = 0; ((u | v) & 1) == 0; ++shift) {
290
	u >>= 1;
291
	v >>= 1;
292
    }
293

  
294
    while ((u & 1) == 0)
295
	u >>= 1;
296

  
288
    uz = ntz_intptr(u);
289
    vz = ntz_intptr(v);
290
    shift = uz > vz ? vz : uz;
291
    u >>= uz;
297 292
    do {
298
	while ((v & 1) == 0)
299
	    v >>= 1;
293
        v >>= vz;
294
        diff = v - u;
295
        if (diff == 0)
296
            break;
297
        vz = ntz_intptr(diff);
298
        if (v < u)
299
            u = v;
300
        v = diff < 0 ? -diff : diff;
301
    } while(true);
300 302

  
301
	if (u > v) {
302
	    t = v;
303
	    v = u;
304
	    u = t;
305
	}
306
	v = v - u;
307
    } while (v != 0);
308

  
309
    return (long)(u << shift);
303
    return u << shift;
310 304
}
311 305

  
312 306
inline static VALUE