On 10/08/2020 16:51, Guenter Roeck wrote: > On Thu, Aug 06, 2020 at 07:17:43PM +0100, Edward Cree wrote: >> Maybe I should add a >> >> static inline u32 mod_u64(u64 dividend, u32 divisor) >> { >> return do_div(dividend, divisor); >> } > Your proposed function is an exact replicate of do_div() No, because do_div() is a macro that modifies 'dividend', whereas by wrapping it in an inline function mod_u64() implicitly creates a local variable. Thus do_div() cannot be used on a constant, whereas mod_u64() can. > You could try something like > > if (reader->value > EFX_MIN_DMAQ_SIZE || EFX_MIN_DMAQ_SIZE % > (u32)reader->value) I considered that. It's ugly, so while it will work I think it's worthlooking to see if there's a better way. > If EFX_MIN_DMAQ_SIZE is indeed known to be a power of 2, you could also use > the knowledge that a 2^n value can only be divided by a smaller 2^n value, > meaning that reader->value must have exactly one bit set. This would also > avoid divide-by-0 issues if reader->value can be 0. > > if (reader->value > EFX_MIN_DMAQ_SIZE || hweight64(reader->value) != 1) This is also ugly and I don't like relying on the power-of-twoness — it just feels fragile.
But you're right to point out that there's a div/0 issue, and if I'm going to have to check for that, then ugliness is unavoidable. So I think the least painful option available is probably if (!reader->value || reader->value > EFX_MIN_DMAQ_SIZE || EFX_MIN_DMAQ_SIZE % (u32)reader->value) which only assumes EFX_MIN_DMAQ_SIZE <= U32_MAX, an assumption I'm comfortable with baking in. I'll put together a formal patch with that. Thanks for the help. -ed