Skip to contentSkip to conversation

do_div vs sector_t

DISCUSSION Matthew Wilcox 9 participants 11 messages Jul 11, 2003 – Jul 14, 2003

Subsystems (2)
lore
Share & tools
lore
#0 lore raw
# define do_div(n,base) ({                              \
        uint32_t __base = (base);                       \
        uint32_t __rem;                                 \
        if (likely(((n) >> 32) == 0)) {                 \

so if we call do_div() on a u32, the compiler emits nasal daemons. and we do this -- in the antcipatory scheduler:

                if (aic->seek_samples) {
                        aic->seek_mean = aic->seek_total + 128;
                        do_div(aic->seek_mean, aic->seek_samples);
                }

seek_mean is a sector_t so sometimes it's 64-bit on a 32-bit platform. so we can't avoid calling do_div().

This almost works (the warning is harmless since gcc optimises away the call)

# define do_div(n,base) ({                                              \
        uint32_t __base = (base);                                       \
        uint32_t __rem;                                                 \
        if ((sizeof(n) < 8) || (likely(((n) >> 32) == 0))) {            \
                __rem = (uint32_t)(n) % __base;                         \
                (n) = (uint32_t)(n) / __base;                           \
        } else                                                          \
                __rem = __div64_32(&(n), __base);                       \
        __rem;                                                          \
 })

Better ideas?

Signature · 3 lines
"It's not Hollywood.  War is real, war is primarily not about defeat or
victory, it is about death.  I've seen thousands and thousands of dead bodies.
Do you think I want to have an academic debate on this subject?" -- Robert Fisk
#1 lore raw
Quoted 13 lines · Matthew Wilcox

This almost works (the warning is harmless since gcc optimises away the call)

# define do_div(n,base) ({                                              \
        uint32_t __base = (base);                                       \
        uint32_t __rem;                                                 \
        if ((sizeof(n) < 8) || (likely(((n) >> 32) == 0))) {            \
                __rem = (uint32_t)(n) % __base;                         \
                (n) = (uint32_t)(n) / __base;                           \
        } else                                                          \
                __rem = __div64_32(&(n), __base);                       \
        __rem;                                                          \
 })

Could we just do:

include/asm-generic/div64.h renamed from include/asm-generic/div64.h~do_div-fix-43 +3 -1
diff -puN include/asm-generic/div64.h~do_div-fix-43 include/asm-generic/div64.h
@@ -34,7 +34,8 @@
 
 extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
 
-# define do_div(n,base) ({				\
+# define do_div(_n,base) ({				\
+	uint64_t n = _n;				\
 	uint32_t __base = (base);			\
 	uint32_t __rem;					\
 	if (likely(((n) >> 32) == 0)) {			\
@@ -42,6 +43,7 @@ extern uint32_t __div64_32(uint64_t *div
 		(n) = (uint32_t)(n) / __base;		\
 	} else 						\
 		__rem = __div64_32(&(n), __base);	\
+	_n = n;						\
 	__rem;						\
  })
 

_

#2 lore raw
In reply to Matthew Wilcox
Quoted 30 lines · On Friday July 11, [email protected] wrote:
# define do_div(n,base) ({                              \
        uint32_t __base = (base);                       \
        uint32_t __rem;                                 \
        if (likely(((n) >> 32) == 0)) {                 \

so if we call do_div() on a u32, the compiler emits nasal daemons. and we do this -- in the antcipatory scheduler:

                if (aic->seek_samples) {
                        aic->seek_mean = aic->seek_total + 128;
                        do_div(aic->seek_mean, aic->seek_samples);
                }

seek_mean is a sector_t so sometimes it's 64-bit on a 32-bit platform. so we can't avoid calling do_div().

This almost works (the warning is harmless since gcc optimises away the call)

# define do_div(n,base) ({                                              \
        uint32_t __base = (base);                                       \
        uint32_t __rem;                                                 \
        if ((sizeof(n) < 8) || (likely(((n) >> 32) == 0))) {            \
                __rem = (uint32_t)(n) % __base;                         \
                (n) = (uint32_t)(n) / __base;                           \
        } else                                                          \
                __rem = __div64_32(&(n), __base);                       \
        __rem;                                                          \
 })
Quoted 1 line

Better ideas?

sector_div, defined in blkdev.h, is probably what you want.

NeilBrown

#3 lore raw

Re: do_div vs sector_t (patch)

Quoted 31 lines · Neil Brown
Quoted 29 lines · On Friday July 11, [email protected] wrote:
# define do_div(n,base) ({                              \
       uint32_t __base = (base);                       \
       uint32_t __rem;                                 \
       if (likely(((n) >> 32) == 0)) {                 \

so if we call do_div() on a u32, the compiler emits nasal daemons. and we do this -- in the antcipatory scheduler:

               if (aic->seek_samples) {
                       aic->seek_mean = aic->seek_total + 128;
                       do_div(aic->seek_mean, aic->seek_samples);
               }

seek_mean is a sector_t so sometimes it's 64-bit on a 32-bit platform. so we can't avoid calling do_div().

This almost works (the warning is harmless since gcc optimises away the call)

# define do_div(n,base) ({                                              \
       uint32_t __base = (base);                                       \
       uint32_t __rem;                                                 \
       if ((sizeof(n) < 8) || (likely(((n) >> 32) == 0))) {            \
               __rem = (uint32_t)(n) % __base;                         \
               (n) = (uint32_t)(n) / __base;                           \
       } else                                                          \
               __rem = __div64_32(&(n), __base);                       \
       __rem;                                                          \
})
Quoted 5 lines
Quoted 2 lines

Better ideas?

sector_div, defined in blkdev.h, is probably what you want.

Looks right. Following patch alright?

drivers/block/as-iosched.c renamed from drivers/block/as-iosched.c.orig +1 -1
@@ -837,7 +837,7 @@ static void as_update_iohist(struct as_i
 		aic->seek_total += 256*seek_dist;
 		if (aic->seek_samples) {
 			aic->seek_mean = aic->seek_total + 128;
-			do_div(aic->seek_mean, aic->seek_samples);
+			sector_div(aic->seek_mean, aic->seek_samples);
 		}
 		aic->seek_samples = (aic->seek_samples>>1)
 					+ (aic->seek_samples>>2);
#4 lore raw
In reply to Matthew Wilcox
Quoted 6 lines · Matthew Wilcox
                        aic->seek_mean = aic->seek_total + 128;
                        do_div(aic->seek_mean, aic->seek_samples);
                }

seek_mean is a sector_t so sometimes it's 64-bit on a 32-bit platform. so we can't avoid calling do_div().

That's why we have sector_div, never use do_div on a sector_t.

#5 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Matthew Wilcox
                        aic->seek_mean = aic->seek_total + 128;
                        do_div(aic->seek_mean, aic->seek_samples);
                }

seek_mean is a sector_t so sometimes it's 64-bit on a 32-bit platform. so we can't avoid calling do_div().

That's why we have sector_div, never use do_div on a sector_t.

Thing is, the arith in there can overflow with 32-bit sector_t anyway, so we need 64-bit quantities regardless of the CONFIG_LBD setting.

#7 lore raw
Quoted 5 lines · Richard Henderson
Quoted 1 line · Matthew Wilcox

Better ideas?

          if (likely(((n) >> 31 >> 1) == 0)) {

Beware - luckily I don't have to worry about that on ARM (we do our own thing.) However, with this code:

int foo(unsigned long long n)

{
        if (((n) >> 31 >> 1) == 0) {
                return 1;
        } else {
                return 0;
        }
}

gcc 3.2.2 on ARM (32-bit) produces some not-very-nice code, consisting of 6 shifts and including placing one register on the stack and completely ignoring a register which it could freely use instead.

Signature · 2 lines
Russell King ([email protected])                The developer of ARM Linux
             http://www.arm.linux.org.uk/personal/aboutme.html
#8 lore raw
In reply to Richard Henderson
Quoted 4 lines · Richard Henderson
Quoted 1 line · Matthew Wilcox

Better ideas?

          if (likely(((n) >> 31 >> 1) == 0)) {

Do we still need to fix this? I've already posted a patch to disallow calling do_div() with any divisor that doesn't look like an unsigned 64bit interger.

Signature · 4 lines
  // Bernardo Innocenti - Develer S.r.l., R&D dept.
\X/  http://www.develer.com/

Please don't send Word attachments - http://www.gnu.org/philosophy/no-word-attachments.html
#9 lore raw
Quoted 9 lines · Bernardo Innocenti
Quoted 4 lines · Richard Henderson
Quoted 1 line · Matthew Wilcox

Better ideas?

          if (likely(((n) >> 31 >> 1) == 0)) {

Do we still need to fix this? I've already posted a patch to disallow calling do_div() with any divisor that doesn't look like an unsigned 64bit interger.

No, I think the combination of sector_div() and your patch makes everything happy-happy. Thanks!

Signature · 3 lines
"It's not Hollywood.  War is real, war is primarily not about defeat or
victory, it is about death.  I've seen thousands and thousands of dead bodies.
Do you think I want to have an academic debate on this subject?" -- Robert Fisk
#10 lore raw
In reply to Matthew Wilcox
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Matthew" == Matthew Wilcox <[email protected]> writes:

Matthew> do_div(aic->seek_mean, aic->seek_samples); }

Matthew> seek_mean is a sector_t so sometimes it's 64-bit on a 32-bit Matthew> platform. so we can't avoid calling do_div().

Use sector_div() instead! -- it uses a 63/32 bit divide/remainder if sector_t is 64-bit, and 32/32 if it's 32-bit.

--
Dr Peter Chubb http://www.gelato.unsw.edu.au peterc AT gelato.unsw.edu.au
You are lost in a maze of BitKeeper repositories, all slightly different.