Blame view

include/linux/reciprocal_div.h 1.01 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  /* SPDX-License-Identifier: GPL-2.0 */
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
2
3
4
5
6
7
  #ifndef _LINUX_RECIPROCAL_DIV_H
  #define _LINUX_RECIPROCAL_DIV_H
  
  #include <linux/types.h>
  
  /*
809fa972f   Hannes Frederic Sowa   reciprocal_divide...
8
9
10
   * This algorithm is based on the paper "Division by Invariant
   * Integers Using Multiplication" by Torbjörn Granlund and Peter
   * L. Montgomery.
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
11
   *
809fa972f   Hannes Frederic Sowa   reciprocal_divide...
12
13
14
   * The assembler implementation from Agner Fog, which this code is
   * based on, can be found here:
   * http://www.agner.org/optimize/asmlib.zip
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
15
   *
809fa972f   Hannes Frederic Sowa   reciprocal_divide...
16
17
18
19
20
   * This optimization for A/B is helpful if the divisor B is mostly
   * runtime invariant. The reciprocal of B is calculated in the
   * slow-path with reciprocal_value(). The fast-path can then just use
   * a much faster multiplication operation with a variable dividend A
   * to calculate the division A/B.
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
21
   */
809fa972f   Hannes Frederic Sowa   reciprocal_divide...
22
23
24
25
  struct reciprocal_value {
  	u32 m;
  	u8 sh1, sh2;
  };
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
26

809fa972f   Hannes Frederic Sowa   reciprocal_divide...
27
  struct reciprocal_value reciprocal_value(u32 d);
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
28

809fa972f   Hannes Frederic Sowa   reciprocal_divide...
29
  static inline u32 reciprocal_divide(u32 a, struct reciprocal_value R)
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
30
  {
809fa972f   Hannes Frederic Sowa   reciprocal_divide...
31
32
  	u32 t = (u32)(((u64)a * R.m) >> 32);
  	return (t + ((a - t) >> R.sh1)) >> R.sh2;
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
33
  }
809fa972f   Hannes Frederic Sowa   reciprocal_divide...
34
35
  
  #endif /* _LINUX_RECIPROCAL_DIV_H */