diff options
author | Nicolas Pitre <npitre@baylibre.com> | 2024-07-07 15:05:20 -0400 |
---|---|---|
committer | Andrew Morton <akpm@linux-foundation.org> | 2024-09-01 20:43:22 -0700 |
commit | 1635e62e75a7bbb1c6274f6b43911cedfe0da60a (patch) | |
tree | 9e79a2900f4571c6b988d1baab8018f70d8d0fd3 /lib/math/div64.c | |
parent | b29a62d87cc0af3e9d134e9e0863b2cb053070b8 (diff) |
mul_u64_u64_div_u64: basic sanity test
Verify that edge cases produce proper results, and some more.
[npitre@baylibre.com: avoid undefined shift value]
Link: https://lkml.kernel.org/r/7rrs9pn1-n266-3013-9q6n-1osp8r8s0rrn@syhkavp.arg
Link: https://lkml.kernel.org/r/20240707190648.1982714-3-nico@fluxnic.net
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Reviewed-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Cc: Biju Das <biju.das.jz@bp.renesas.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'lib/math/div64.c')
-rw-r--r-- | lib/math/div64.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/lib/math/div64.c b/lib/math/div64.c index b7fc75246399..5faa29208bdb 100644 --- a/lib/math/div64.c +++ b/lib/math/div64.c @@ -212,11 +212,18 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c) #endif + /* make sure c is not zero, trigger exception otherwise */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdiv-by-zero" + if (unlikely(c == 0)) + return 1/0; +#pragma GCC diagnostic pop + int shift = __builtin_ctzll(c); /* try reducing the fraction in case the dividend becomes <= 64 bits */ if ((n_hi >> shift) == 0) { - u64 n = (n_lo >> shift) | (n_hi << (64 - shift)); + u64 n = shift ? (n_lo >> shift) | (n_hi << (64 - shift)) : n_lo; return div64_u64(n, c >> shift); /* |