From: Jim Meyering Date: Thu, 15 Apr 2004 09:12:25 +0000 (+0000) Subject: (gcd, lcm, ptr_align): New functions, moved from od.c. X-Git-Tag: v5.3.0~1771 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0c40916e6f79e7d34bb1b36ef5b5f1dcc3d47c0c;p=thirdparty%2Fcoreutils.git (gcd, lcm, ptr_align): New functions, moved from od.c. --- diff --git a/src/system.h b/src/system.h index c833264d98..978b613d90 100644 --- a/src/system.h +++ b/src/system.h @@ -766,3 +766,43 @@ enum ? fseek (s, o, w) \ : (errno = EOVERFLOW, -1)) #endif + +/* Compute the greatest common divisor of U and V using Euclid's + algorithm. U and V must be nonzero. */ + +static inline size_t +gcd (size_t u, size_t v) +{ + do + { + size_t t = u % v; + u = v; + v = t; + } + while (v); + + return u; +} + +/* Compute the least common multiple of U and V. U and V must be + nonzero. There is no overflow checking, so callers should not + specify outlandish sizes. */ + +static inline size_t +lcm (size_t u, size_t v) +{ + return u * (v / gcd (u, v)); +} + +/* Return PTR, aligned upward to the next multiple of ALIGNMENT. + ALIGNMENT must be nonzero. The caller must arrange for ((char *) + PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable + locations. */ + +static inline void * +ptr_align (void *ptr, size_t alignment) +{ + char *p0 = ptr; + char *p1 = p0 + alignment - 1; + return p1 - (uintptr_t) p1 % alignment; +}