Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib: mul_u64_u64_div_u64(): optimise the divide code

Replace the bit by bit algorithm with one that generates 16 bits per
iteration on 32bit architectures and 32 bits on 64bit ones.

On my zen 5 this reduces the time for the tests (using the generic code)
from ~3350ns to ~1000ns.

Running the 32bit algorithm on 64bit x86 takes ~1500ns. It'll be slightly
slower on a real 32bit system, mostly due to register pressure.

The savings for 32bit x86 are much higher (tested in userspace). The
worst case (lots of bits in the quotient) drops from ~900 clocks to ~130
(pretty much independant of the arguments). Other 32bit architectures may
see better savings.

It is possibly to optimise for divisors that span less than
__LONG_WIDTH__/2 bits. However I suspect they don't happen that often and
it doesn't remove any slow cpu divide instructions which dominate the
result.

Typical improvements for 64bit random divides:
old new
sandy bridge: 470 150
haswell: 400 144
piledriver: 960 467 I think rdpmc is very slow.
zen5: 244 80
(Timing is 'rdpmc; mul_div(); rdpmc' with the multiply depending on the
first rdpmc and the second rdpmc depending on the quotient.)

Object code (64bit x86 test program): old 0x173 new 0x141.

Link: https://lkml.kernel.org/r/20251105201035.64043-9-david.laight.linux@gmail.com
Signed-off-by: David Laight <david.laight.linux@gmail.com>
Reviewed-by: Nicolas Pitre <npitre@baylibre.com>
Cc: Biju Das <biju.das.jz@bp.renesas.com>
Cc: Borislav Betkov <bp@alien8.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Li RongQing <lirongqing@baidu.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleinxer <tglx@linutronix.de>
Cc: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

David Laight and committed by
Andrew Morton
d10bb374 630f96a6

+83 -37
+83 -37
lib/math/div64.c
··· 190 190 #define mul_add(a, b, c) add_u64_u32(mul_u32_u32(a, b), c) 191 191 192 192 #if defined(__SIZEOF_INT128__) && !defined(test_mul_u64_add_u64_div_u64) 193 - 194 193 static inline u64 mul_u64_u64_add_u64(u64 *p_lo, u64 a, u64 b, u64 c) 195 194 { 196 195 /* native 64x64=128 bits multiplication */ ··· 198 199 *p_lo = prod; 199 200 return prod >> 64; 200 201 } 201 - 202 202 #else 203 - 204 203 static inline u64 mul_u64_u64_add_u64(u64 *p_lo, u64 a, u64 b, u64 c) 205 204 { 206 205 /* perform a 64x64=128 bits multiplication in 32bit chunks */ ··· 213 216 *p_lo = (y << 32) + (u32)x; 214 217 return add_u64_u32(z, y >> 32); 215 218 } 219 + #endif 216 220 221 + #ifndef BITS_PER_ITER 222 + #define BITS_PER_ITER (__LONG_WIDTH__ >= 64 ? 32 : 16) 223 + #endif 224 + 225 + #if BITS_PER_ITER == 32 226 + #define mul_u64_long_add_u64(p_lo, a, b, c) mul_u64_u64_add_u64(p_lo, a, b, c) 227 + #define add_u64_long(a, b) ((a) + (b)) 228 + #else 229 + #undef BITS_PER_ITER 230 + #define BITS_PER_ITER 16 231 + static inline u32 mul_u64_long_add_u64(u64 *p_lo, u64 a, u32 b, u64 c) 232 + { 233 + u64 n_lo = mul_add(a, b, c); 234 + u64 n_med = mul_add(a >> 32, b, c >> 32); 235 + 236 + n_med = add_u64_u32(n_med, n_lo >> 32); 237 + *p_lo = n_med << 32 | (u32)n_lo; 238 + return n_med >> 32; 239 + } 240 + 241 + #define add_u64_long(a, b) add_u64_u32(a, b) 217 242 #endif 218 243 219 244 u64 mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d) 220 245 { 221 - u64 n_lo, n_hi; 246 + unsigned long d_msig, q_digit; 247 + unsigned int reps, d_z_hi; 248 + u64 quotient, n_lo, n_hi; 249 + u32 overflow; 222 250 223 251 n_hi = mul_u64_u64_add_u64(&n_lo, a, b, c); 224 252 ··· 262 240 return ~0ULL; 263 241 } 264 242 265 - int shift = __builtin_ctzll(d); 266 - 267 - /* try reducing the fraction in case the dividend becomes <= 64 bits */ 268 - if ((n_hi >> shift) == 0) { 269 - u64 n = shift ? (n_lo >> shift) | (n_hi << (64 - shift)) : n_lo; 270 - 271 - return div64_u64(n, d >> shift); 272 - /* 273 - * The remainder value if needed would be: 274 - * res = div64_u64_rem(n, d >> shift, &rem); 275 - * rem = (rem << shift) + (n_lo - (n << shift)); 276 - */ 243 + /* Left align the divisor, shifting the dividend to match */ 244 + d_z_hi = __builtin_clzll(d); 245 + if (d_z_hi) { 246 + d <<= d_z_hi; 247 + n_hi = n_hi << d_z_hi | n_lo >> (64 - d_z_hi); 248 + n_lo <<= d_z_hi; 277 249 } 278 250 279 - /* Do the full 128 by 64 bits division */ 251 + reps = 64 / BITS_PER_ITER; 252 + /* Optimise loop count for small dividends */ 253 + if (!(u32)(n_hi >> 32)) { 254 + reps -= 32 / BITS_PER_ITER; 255 + n_hi = n_hi << 32 | n_lo >> 32; 256 + n_lo <<= 32; 257 + } 258 + #if BITS_PER_ITER == 16 259 + if (!(u32)(n_hi >> 48)) { 260 + reps--; 261 + n_hi = add_u64_u32(n_hi << 16, n_lo >> 48); 262 + n_lo <<= 16; 263 + } 264 + #endif 280 265 281 - shift = __builtin_clzll(d); 282 - d <<= shift; 266 + /* Invert the dividend so we can use add instead of subtract. */ 267 + n_lo = ~n_lo; 268 + n_hi = ~n_hi; 283 269 284 - int p = 64 + shift; 285 - u64 res = 0; 286 - bool carry; 270 + /* 271 + * Get the most significant BITS_PER_ITER bits of the divisor. 272 + * This is used to get a low 'guestimate' of the quotient digit. 273 + */ 274 + d_msig = (d >> (64 - BITS_PER_ITER)) + 1; 287 275 288 - do { 289 - carry = n_hi >> 63; 290 - shift = carry ? 1 : __builtin_clzll(n_hi); 291 - if (p < shift) 292 - break; 293 - p -= shift; 294 - n_hi <<= shift; 295 - n_hi |= n_lo >> (64 - shift); 296 - n_lo <<= shift; 297 - if (carry || (n_hi >= d)) { 298 - n_hi -= d; 299 - res |= 1ULL << p; 276 + /* 277 + * Now do a 'long division' with BITS_PER_ITER bit 'digits'. 278 + * The 'guess' quotient digit can be low and BITS_PER_ITER+1 bits. 279 + * The worst case is dividing ~0 by 0x8000 which requires two subtracts. 280 + */ 281 + quotient = 0; 282 + while (reps--) { 283 + q_digit = (unsigned long)(~n_hi >> (64 - 2 * BITS_PER_ITER)) / d_msig; 284 + /* Shift 'n' left to align with the product q_digit * d */ 285 + overflow = n_hi >> (64 - BITS_PER_ITER); 286 + n_hi = add_u64_u32(n_hi << BITS_PER_ITER, n_lo >> (64 - BITS_PER_ITER)); 287 + n_lo <<= BITS_PER_ITER; 288 + /* Add product to negated divisor */ 289 + overflow += mul_u64_long_add_u64(&n_hi, d, q_digit, n_hi); 290 + /* Adjust for the q_digit 'guestimate' being low */ 291 + while (overflow < 0xffffffff >> (32 - BITS_PER_ITER)) { 292 + q_digit++; 293 + n_hi += d; 294 + overflow += n_hi < d; 300 295 } 301 - } while (n_hi); 302 - /* The remainder value if needed would be n_hi << p */ 296 + quotient = add_u64_long(quotient << BITS_PER_ITER, q_digit); 297 + } 303 298 304 - return res; 299 + /* 300 + * The above only ensures the remainder doesn't overflow, 301 + * it can still be possible to add (aka subtract) another copy 302 + * of the divisor. 303 + */ 304 + if ((n_hi + d) > n_hi) 305 + quotient++; 306 + return quotient; 305 307 } 306 308 #if !defined(test_mul_u64_add_u64_div_u64) 307 309 EXPORT_SYMBOL(mul_u64_add_u64_div_u64);