udivdi3: 64 bit divide
Subsystems (12)
PATCH 1/1 udivdi3: 64 bit divide
The kernel already has several implmentations and usages of 64 by 64 bit divide.
Although it is significantly slower, there are places that need it so provide one generic version using scaling, and allow existing platform versions to continue.
This looks like good -mm material. Tested on x86_64 and i386.
--- arch/alpha/Kconfig | 4 ++++ arch/arm/Kconfig | 4 ++++ arch/arm26/Kconfig | 4 ++++ arch/avr32/Kconfig | 4 ++++ arch/cris/Kconfig | 4 ++++ arch/frv/Kconfig | 4 ++++ arch/h8300/Kconfig | 4 ++++ arch/i386/Kconfig | 4 ++++ arch/m32r/Kconfig | 4 ++++ arch/m68k/Kconfig | 4 ++++ arch/m68knommu/Kconfig | 4 ++++ arch/mips/Kconfig | 4 ++++ arch/parisc/Kconfig | 4 ++++ arch/powerpc/Kconfig | 4 ++++ arch/ppc/Kconfig | 4 ++++ arch/s390/Kconfig | 4 ++++ arch/sh64/Kconfig | 4 ++++ arch/v850/Kconfig | 3 +++ arch/xtensa/Kconfig | 4 ++++ lib/Makefile | 1 + lib/udivdi3.c | 37 +++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_cubic.c | 24 +----------------------- net/netfilter/xt_connbytes.c | 19 +------------------ 23 files changed, 115 insertions(+), 41 deletions(-)
net/ipv4/tcp_cubic.c +1 -23
@@ -51,7 +51,6 @@module_param (tcp_friendliness, int,0644 );MODULE_PARM_DESC (tcp_friendliness,"turn on/off tcp friendliness" ); -#include <asm/div64.h> /* BIC TCP Parameters */ struct bictcp {
@@ -93,27 +92,6 @@tcp_sk (sk)->snd_ssthresh = initial_ssthresh; } -/* 64bit divisor, dividend and result. dynamic precision */ -static inline u_int64_t div64_64 (u_int64_t dividend,u_int64_t divisor) -{ -u_int32_t d = divisor; - -if (divisor >0xffffffffULL ) { -unsigned int shift =fls (divisor >>32 ); - - d = divisor >> shift; - dividend >>= shift; - } - -/* avoid 64 bit division if possible */ -if (dividend >>32 ) -do_div (dividend, d); -else - dividend = (uint32_t ) dividend / d; - -return dividend; -} -/* * calculate the cubic root of x using Newton-Raphson */
@@ -134,7 +112,7 @@*/ do { x1 = x; - x = (2 * x + (uint32_t )div64_64 (a, x*x)) /3 ; + x = (2 * x + (u32) (a / (x*x))) /3 ; }while (abs (x1 - x) >1 );return x;
net/netfilter/xt_connbytes.c +1 -18
@@ -16,7 +16,6 @@#include <linux/netfilter/x_tables.h> #include <linux/netfilter/xt_connbytes.h> -#include <asm/div64.h> #include <asm/bitops.h> MODULE_LICENSE ("GPL" );
@@ -24,22 +23,6 @@MODULE_DESCRIPTION ("iptables match for matching number of pkts/bytes per connection" );MODULE_ALIAS ("ipt_connbytes" ); -/* 64bit divisor, dividend and result. dynamic precision */ -static u_int64_t div64_64 (u_int64_t dividend,u_int64_t divisor) -{ -u_int32_t d = divisor; - -if (divisor >0xffffffffULL ) { -unsigned int shift =fls (divisor >>32 ); - - d = divisor >> shift; - dividend >>= shift; - } - -do_div (dividend, d); -return dividend; -} - static int match(const struct sk_buff *skb, const struct net_device *in,
@@ -106,7 +89,7 @@break ; }if (pkts !=0 ) - what =div64_64 (bytes, pkts); + what = bytes / pkts;break ; }
lib/Makefile +1 -0
@@ -28,6 +28,7 @@ lib-$(CONFIG_SEMAPHORE_SLEEPERS) += semaphore-sleepers.o lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o +obj-$(CONFIG_GENERIC_UDIVDI3) += udivdi3.o obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o obj-$(CONFIG_PLIST) += plist.o obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o
arch/alpha/Kconfig +4 -0
@@ -33,6 +33,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_FIND_NEXT_BIT bool default y
arch/arm/Kconfig +4 -0
@@ -90,6 +90,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_HWEIGHT bool default y
arch/arm26/Kconfig +4 -0
@@ -49,6 +49,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_HWEIGHT bool default y
arch/avr32/Kconfig +4 -0
@@ -53,6 +53,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_BUST_SPINLOCK bool
arch/cris/Kconfig +4 -0
@@ -28,6 +28,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_FIND_NEXT_BIT bool default y
arch/frv/Kconfig +4 -0
@@ -53,6 +53,10 @@ bool default y +config GENERIC_UDIVDI3 + bool + default y + mainmenu "Fujitsu FR-V Kernel Configuration" source "init/Kconfig"
arch/h8300/Kconfig +4 -0
@@ -41,6 +41,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_FIND_NEXT_BIT bool default y
arch/i386/Kconfig +4 -0
@@ -75,6 +75,10 @@ bool default y +config GENERIC_UDIVDI3 + bool + default y + config ARCH_MAY_HAVE_PC_FDC bool default y
arch/m32r/Kconfig +4 -0
@@ -229,6 +229,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_FIND_NEXT_BIT bool default y
arch/m68k/Kconfig +4 -0
@@ -25,6 +25,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_HWEIGHT bool default y
arch/m68knommu/Kconfig +4 -0
@@ -37,6 +37,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_FIND_NEXT_BIT bool default y
arch/mips/Kconfig +4 -0
@@ -843,6 +843,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_FIND_NEXT_BIT bool default y
arch/parisc/Kconfig +4 -0
@@ -33,6 +33,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_FIND_NEXT_BIT bool default y
arch/powerpc/Kconfig +4 -0
@@ -49,6 +49,10 @@ bool default y if 64BIT +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_HWEIGHT bool default y
arch/ppc/Kconfig +4 -0
@@ -27,6 +27,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_HWEIGHT bool default y
arch/s390/Kconfig +4 -0
@@ -34,6 +34,10 @@ bool default n +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_HWEIGHT bool default y
arch/sh64/Kconfig +4 -0
@@ -21,6 +21,10 @@ bool default y +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_FIND_NEXT_BIT bool default y
arch/v850/Kconfig +3 -0
@@ -19,6 +19,9 @@ config RWSEM_XCHGADD_ALGORITHM bool default n +config GENERIC_UDIVDI3 + bool + default y config GENERIC_FIND_NEXT_BIT bool default y
arch/xtensa/Kconfig +4 -0
@@ -26,6 +26,10 @@ bool default y +config GENERIC_UDIVDI3 + bool + default y + config GENERIC_FIND_NEXT_BIT bool default y
lib/udivdi3.c new file +37 -0
@@ -0,0 +1,37 @@ +/* +* Generic C version of full 64 bit by 64 bit division +* Extracted from version used by netfilter connection tracking +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* version 2 as published by the Free Software Foundation. +* +* Code generated for this function might be very inefficient +* for some CPUs, can be overridden by linking arch-specific +* assembly versions such as arch/sparc/lib/udivdi.S +*/ +#include <linux/types.h> +#include <linux/module.h> +#include <asm/div64.h> + +uint64_t __udivdi3 (uint64_t dividend,uint64_t divisor) +{ +uint32_t d = divisor; + +/* Scale divisor to 32 bits */ +if (divisor >0xffffffffULL ) { +unsigned int shift =fls (divisor >>32 ); + + d = divisor >> shift; + dividend >>= shift; + } + +/* avoid 64 bit division if possible */ +if (dividend >>32 ) +do_div (dividend, d); +else + dividend = (uint32_t ) dividend / d; + +return dividend; +} +EXPORT_SYMBOL (__udivdi3);
Andrew Morton
maintainerQuoted 7 lines · On Mon, 26 Feb 2007 17:35:17 -0800 Stephen Hemminger…
The kernel already has several implmentations and usages of 64 by 64 bit divide.
Although it is significantly slower, there are places that need it so provide one generic version using scaling, and allow existing platform versions to continue.
The reason we implement 64/32 via do_div() is, for better or for worse, to make people think before they use it. And to make it stand out, and so that we discover places that are using it by accident, where they could use something cheaper.
However your implementation of the presumably even more expensive 64/64 allows us to do 64/64 with a plain old "/" operator.
If the do_div() philosophy is any good then we should surely repeat it for 64/64, no?
Quoted 13 lines · On Tue, 27 Feb 2007 12:24:37 -0800 Andrew Morton <[email protected]>…
Quoted 7 lines · On Mon, 26 Feb 2007 17:35:17 -0800 Stephen Hemminger…
The kernel already has several implmentations and usages of 64 by 64 bit divide.
Although it is significantly slower, there are places that need it so provide one generic version using scaling, and allow existing platform versions to continue.
The reason we implement 64/32 via
do_div()is, for better or for worse, to make people think before they use it. And to make it stand out, and so that we discover places that are using it by accident, where they could use something cheaper.
Quoted 6 lines
However your implementation of the presumably even more expensive 64/64 allows us to do 64/64 with a plain old "/" operator.
If the
do_div()philosophy is any good then we should surely repeat it for 64/64, no?
Then we should pull the existing udivdi3 implementations?
Andrew Morton
maintainerQuoted 17 lines · On Tue, 27 Feb 2007 13:18:40 -0800 Stephen Hemminger…
Quoted 13 lines · On Tue, 27 Feb 2007 12:24:37 -0800 Andrew Morton <[email protected]>…
Quoted 7 lines · On Mon, 26 Feb 2007 17:35:17 -0800 Stephen Hemminger…
The kernel already has several implmentations and usages of 64 by 64 bit divide.
Although it is significantly slower, there are places that need it so provide one generic version using scaling, and allow existing platform versions to continue.
The reason we implement 64/32 via
do_div()is, for better or for worse, to make people think before they use it. And to make it stand out, and so that we discover places that are using it by accident, where they could use something cheaper.
Quoted 8 lines
Quoted 6 lines
However your implementation of the presumably even more expensive 64/64 allows us to do 64/64 with a plain old "/" operator.
If the
do_div()philosophy is any good then we should surely repeat it for 64/64, no?Then we should pull the existing udivdi3 implementations?
Not much point really. Some architectures have gone and done that, but x86 has not. x86 has enough coverage for us to pick up most problems, and any remaining problems are obviously in scruffy architectures which don't care about performance ;)
Quoted 7 lines · Andrew Morton
Quoted 1 line · On Tue, 27 Feb 2007 13:18:40 -0800 Stephen Hemminger…
Then we should pull the existing udivdi3 implementations?
Not much point really. Some architectures have gone and done that, but x86 has not. x86 has enough coverage for us to pick up most problems, and any remaining problems are obviously in scruffy architectures which don't care about performance ;)
I doubt arm26 uses udivdi3, but that's something Ian would have to confirm.
Signature · 3 lines
Russell King Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/ maintainer of:
Quoted 10 lines · Russell King
Quoted 6 lines · Andrew Morton
Quoted 1 line · On Tue, 27 Feb 2007 13:18:40 -0800 Stephen Hemminger…
Then we should pull the existing udivdi3 implementations?
Not much point really. Some architectures have gone and done that, but x86 has not. x86 has enough coverage for us to pick up most problems, and any remaining problems are obviously in scruffy architectures which don't care about performance ;)
I doubt arm26 uses udivdi3, but that's something Ian would have to confirm.
I doubt it is used also, however I am not in a position to test this until at least after I have moved house. Please leave alone for now.
Quoted 12 lines · Ian Molton
Quoted 11 lines · Russell King
Quoted 9 lines · Andrew Morton
Quoted 2 lines · On Tue, 27 Feb 2007 13:18:40 -0800 Stephen Hemminger…
Then we should pull the existing udivdi3 implementations?
Not much point really. Some architectures have gone and done that, but x86 has not. x86 has enough coverage for us to pick up most problems, and any remaining problems are obviously in scruffy architectures which don't care about performance ;)
Quoted 6 lines
Quoted 2 lines
I doubt arm26 uses udivdi3, but that's something Ian would have to confirm.
I doubt it is used also, however I am not in a position to test this until at least after I have moved house. Please leave alone for now.
Simple. The non-arch specific code does not use 64/64 divides through the "/" operator (otherwise there would already have been udivdi3 linking errors). So what remains to check is arch/arm26. grep -Pr 'int64|\bu64' returns only a few results to check (kernel/ecard.c, nwfpe/), so the answer is most likely no.
Jan
Quoted 6 lines · Jan Engelhardt
The non-arch specific code does not use 64/64 divides through the "/" operator (otherwise there would already have been udivdi3 linking errors). So what remains to check is
arch/arm26. grep -Pr 'int64|\bu64' returns only a few results to check (kernel/ecard.c, nwfpe/), so the answer is most likely no.
Looks that way. ok, nuke it :)
Quoted 4 lines · Andrew Morton
Quoted 4 lines · On Mon, 26 Feb 2007 17:35:17 -0800 Stephen Hemminger…
The kernel already has several implmentations and usages of 64 by 64 bit divide.
Quoted 8 lines
Quoted 3 lines
Although it is significantly slower, there are places that need it so provide one generic version using scaling, and allow existing platform versions to continue.
The reason we implement 64/32 via
do_div()is, for better or for worse, to make people think before they use it. And to make it stand out, and so that we discover places that are using it by accident, where they could use something cheaper.
IMHO it is even more important that the user of your 64/64 div is aware that it only returns an approximate result.
I certainly don't want to have any code in the kernel that by accident makes an allocation a few bytes short of the actual size of the object (just to make up a drastic example).
Tim