| 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 | |
| 3 | #include <commonlib/bsd/gcd.h> |
| 4 | #include <commonlib/bsd/helpers.h> |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | uint64_t gcd(uint64_t a, uint64_t b) |
| 8 | { |
| 9 | uint64_t c; |
| 10 | |
| 11 | if (a == 0 || b == 0) |
| 12 | return MAX(a, b); |
| 13 | |
| 14 | c = a % b; |
| 15 | |
| 16 | while (c > 0) { |
| 17 | a = b; |
| 18 | b = c; |
| 19 | c = a % b; |
| 20 | } |
| 21 | |
| 22 | return b; |
| 23 | } |