blob: e031e9f2f9d85f1330a9f38b1a82c7f322f00f3e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002#ifndef _LINUX_RECIPROCAL_DIV_H
3#define _LINUX_RECIPROCAL_DIV_H
4
5#include <linux/types.h>
6
7/*
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01008 * This algorithm is based on the paper "Division by Invariant
9 * Integers Using Multiplication" by Torbjörn Granlund and Peter
10 * L. Montgomery.
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080011 *
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010012 * The assembler implementation from Agner Fog, which this code is
13 * based on, can be found here:
14 * http://www.agner.org/optimize/asmlib.zip
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080015 *
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010016 * This optimization for A/B is helpful if the divisor B is mostly
17 * runtime invariant. The reciprocal of B is calculated in the
18 * slow-path with reciprocal_value(). The fast-path can then just use
19 * a much faster multiplication operation with a variable dividend A
20 * to calculate the division A/B.
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080021 */
22
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010023struct reciprocal_value {
24 u32 m;
25 u8 sh1, sh2;
26};
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080027
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010028struct reciprocal_value reciprocal_value(u32 d);
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080029
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010030static inline u32 reciprocal_divide(u32 a, struct reciprocal_value R)
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080031{
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010032 u32 t = (u32)(((u64)a * R.m) >> 32);
33 return (t + ((a - t) >> R.sh1)) >> R.sh2;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080034}
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010035
36#endif /* _LINUX_RECIPROCAL_DIV_H */