Blame view

lib/reciprocal_div.c 528 Bytes
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
809fa972f   Hannes Frederic Sowa   reciprocal_divide...
2
  #include <linux/kernel.h>
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
3
4
  #include <asm/div64.h>
  #include <linux/reciprocal_div.h>
8af2a218d   Eric Dumazet   sch_red: Adaptati...
5
  #include <linux/export.h>
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
6

809fa972f   Hannes Frederic Sowa   reciprocal_divide...
7
8
9
10
11
12
  /*
   * For a description of the algorithm please have a look at
   * include/linux/reciprocal_div.h
   */
  
  struct reciprocal_value reciprocal_value(u32 d)
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
13
  {
809fa972f   Hannes Frederic Sowa   reciprocal_divide...
14
15
16
17
18
19
20
21
22
23
24
25
26
  	struct reciprocal_value R;
  	u64 m;
  	int l;
  
  	l = fls(d - 1);
  	m = ((1ULL << 32) * ((1ULL << l) - d));
  	do_div(m, d);
  	++m;
  	R.m = (u32)m;
  	R.sh1 = min(l, 1);
  	R.sh2 = max(l - 1, 0);
  
  	return R;
6a2d7a955   Eric Dumazet   [PATCH] SLAB: use...
27
  }
8af2a218d   Eric Dumazet   sch_red: Adaptati...
28
  EXPORT_SYMBOL(reciprocal_value);