diff options
author | Jim Meyering <jim@meyering.net> | 2004-04-15 09:12:25 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2004-04-15 09:12:25 +0000 |
commit | 0c40916e6f79e7d34bb1b36ef5b5f1dcc3d47c0c (patch) | |
tree | 2fcc2877d5103dc19a794a70d4c387db7f3f3e45 | |
parent | 1365b2f70840666739f1a2f9cdf05d6608d5a93f (diff) | |
download | coreutils-0c40916e6f79e7d34bb1b36ef5b5f1dcc3d47c0c.tar.xz |
(gcd, lcm, ptr_align): New functions, moved from od.c.
-rw-r--r-- | src/system.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/system.h b/src/system.h index c833264d9..978b613d9 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; +} |