~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/math/reciprocal_div.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <linux/bitops.h>
  3 #include <linux/bug.h>
  4 #include <linux/export.h>
  5 #include <linux/limits.h>
  6 #include <linux/math.h>
  7 #include <linux/minmax.h>
  8 #include <linux/types.h>
  9 
 10 #include <linux/reciprocal_div.h>
 11 
 12 /*
 13  * For a description of the algorithm please have a look at
 14  * include/linux/reciprocal_div.h
 15  */
 16 
 17 struct reciprocal_value reciprocal_value(u32 d)
 18 {
 19         struct reciprocal_value R;
 20         u64 m;
 21         int l;
 22 
 23         l = fls(d - 1);
 24         m = ((1ULL << 32) * ((1ULL << l) - d));
 25         do_div(m, d);
 26         ++m;
 27         R.m = (u32)m;
 28         R.sh1 = min(l, 1);
 29         R.sh2 = max(l - 1, 0);
 30 
 31         return R;
 32 }
 33 EXPORT_SYMBOL(reciprocal_value);
 34 
 35 struct reciprocal_value_adv reciprocal_value_adv(u32 d, u8 prec)
 36 {
 37         struct reciprocal_value_adv R;
 38         u32 l, post_shift;
 39         u64 mhigh, mlow;
 40 
 41         /* ceil(log2(d)) */
 42         l = fls(d - 1);
 43         /* NOTE: mlow/mhigh could overflow u64 when l == 32. This case needs to
 44          * be handled before calling "reciprocal_value_adv", please see the
 45          * comment at include/linux/reciprocal_div.h.
 46          */
 47         WARN(l == 32,
 48              "ceil(log2(0x%08x)) == 32, %s doesn't support such divisor",
 49              d, __func__);
 50         post_shift = l;
 51         mlow = 1ULL << (32 + l);
 52         do_div(mlow, d);
 53         mhigh = (1ULL << (32 + l)) + (1ULL << (32 + l - prec));
 54         do_div(mhigh, d);
 55 
 56         for (; post_shift > 0; post_shift--) {
 57                 u64 lo = mlow >> 1, hi = mhigh >> 1;
 58 
 59                 if (lo >= hi)
 60                         break;
 61 
 62                 mlow = lo;
 63                 mhigh = hi;
 64         }
 65 
 66         R.m = (u32)mhigh;
 67         R.sh = post_shift;
 68         R.exp = l;
 69         R.is_wide_m = mhigh > U32_MAX;
 70 
 71         return R;
 72 }
 73 EXPORT_SYMBOL(reciprocal_value_adv);
 74 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php