blob: 8c5a3fb6c6c57821720dc307f3eeec944c56f974 [file] [log] [blame]
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08001#ifndef _LINUX_RECIPROCAL_DIV_H
2#define _LINUX_RECIPROCAL_DIV_H
3
4#include <linux/types.h>
5
6/*
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01007 * This algorithm is based on the paper "Division by Invariant
8 * Integers Using Multiplication" by Torbjörn Granlund and Peter
9 * L. Montgomery.
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080010 *
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010011 * The assembler implementation from Agner Fog, which this code is
12 * based on, can be found here:
13 * http://www.agner.org/optimize/asmlib.zip
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080014 *
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010015 * This optimization for A/B is helpful if the divisor B is mostly
16 * runtime invariant. The reciprocal of B is calculated in the
17 * slow-path with reciprocal_value(). The fast-path can then just use
18 * a much faster multiplication operation with a variable dividend A
19 * to calculate the division A/B.
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080020 */
21
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010022struct reciprocal_value {
23 u32 m;
24 u8 sh1, sh2;
25};
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080026
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010027struct reciprocal_value reciprocal_value(u32 d);
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080028
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010029static inline u32 reciprocal_divide(u32 a, struct reciprocal_value R)
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080030{
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010031 u32 t = (u32)(((u64)a * R.m) >> 32);
32 return (t + ((a - t) >> R.sh1)) >> R.sh2;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -080033}
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010034
35#endif /* _LINUX_RECIPROCAL_DIV_H */