Commit | Line | Data |
---|---|---|
6a2d7a95 ED |
1 | #ifndef _LINUX_RECIPROCAL_DIV_H |
2 | #define _LINUX_RECIPROCAL_DIV_H | |
3 | ||
4 | #include <linux/types.h> | |
5 | ||
6 | /* | |
809fa972 HFS |
7 | * This algorithm is based on the paper "Division by Invariant |
8 | * Integers Using Multiplication" by Torbjörn Granlund and Peter | |
9 | * L. Montgomery. | |
6a2d7a95 | 10 | * |
809fa972 HFS |
11 | * 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 | |
6a2d7a95 | 14 | * |
809fa972 HFS |
15 | * 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. | |
6a2d7a95 ED |
20 | */ |
21 | ||
809fa972 HFS |
22 | struct reciprocal_value { |
23 | u32 m; | |
24 | u8 sh1, sh2; | |
25 | }; | |
6a2d7a95 | 26 | |
809fa972 | 27 | struct reciprocal_value reciprocal_value(u32 d); |
6a2d7a95 | 28 | |
809fa972 | 29 | static inline u32 reciprocal_divide(u32 a, struct reciprocal_value R) |
6a2d7a95 | 30 | { |
809fa972 HFS |
31 | u32 t = (u32)(((u64)a * R.m) >> 32); |
32 | return (t + ((a - t) >> R.sh1)) >> R.sh2; | |
6a2d7a95 | 33 | } |
809fa972 HFS |
34 | |
35 | #endif /* _LINUX_RECIPROCAL_DIV_H */ |