All of lore.kernel.org
 help / color / mirror / Atom feed
From: Huang Shijie <shijie8@gmail.com>
To: Gerhard Sittig <gsi@denx.de>
Cc: marex@denx.de, Huang Shijie <b32955@freescale.com>,
	computersforpeace@gmail.com, linux-mtd@lists.infradead.org,
	dwmw2@infradead.org
Subject: Re: [PATCH] mtd: spi-nor: fix the wrong dummy value
Date: Thu, 17 Apr 2014 23:57:59 +0800	[thread overview]
Message-ID: <20140417155758.GA14819@localhost.localdomain> (raw)
In-Reply-To: <20140417155507.GT3528@book.gsilab.sittig.org>

On Thu, Apr 17, 2014 at 05:55:07PM +0200, Gerhard Sittig wrote:
> On Thu, 2014-04-17 at 21:41 +0800, Huang Shijie wrote:
> > 
> > The disassemble code for "int dummy = 8; dummy /= 8;" is:
> > --------------------------------------------------
> >     83a6:	2308      	movs	r3, #8
> >     83a8:	607b      	str	r3, [r7, #4]
> >     83aa:	687b      	ldr	r3, [r7, #4]
> >     83ac:	1dda      	adds	r2, r3, #7
> >     83ae:	2b00      	cmp	r3, #0
> >     83b0:	bfb4      	ite	lt
> >     83b2:	4613      	movlt	r3, r2
> >     83b4:	461b      	movge	r3, r3
> >     83b6:	10db      	asrs	r3, r3, #3
> >     83b8:	607b      	str	r3, [r7, #4]
> > --------------------------------------------------
> > 
> > The disassemble code for "int dummy = 8; dummy >>= 3;" is:
> > --------------------------------------------------
> >     83a6:	2308      	movs	r3, #8
> >     83a8:	607b      	str	r3, [r7, #4]
> >     83aa:	687b      	ldr	r3, [r7, #4]
> >     83ac:	10db      	asrs	r3, r3, #3
> >     83ae:	607b      	str	r3, [r7, #4]
> > --------------------------------------------------
> > 
> > Obviously, the "dummy >>= 3" is faster then "dummy /= 8".
> 
> That is because of signedness.  Both forms of "/= 8" and ">>= 3"
> should be identical to the compiler, and generate the same
> output.  Compilers know that division by powers of two can be
> done with a shift.
> 
> Signedness apparently makes a difference.  If you know that the
> number of clocks always is non-negative, use appropriate data
> types.  Or let the compiler carry out the correct instructions
> for the very data type that was declared.  Pick one, don't
> violate abstractions.
> 
> Counter example, matching the expectation:
> 
> 	unsigned int u_div(unsigned int v) {
> 		return v / 8;
> 	}
> 
> 	unsigned int u_shift(unsigned int v) {
> 		return v >> 3;
> 	}
> 
> 	0000001c <u_div>:
> 	  1c:   e1a001a0        lsr     r0, r0, #3
> 	  20:   e12fff1e        bx      lr
> 
> 	00000024 <u_shift>:
> 	  24:   e1a001a0        lsr     r0, r0, #3
> 	  28:   e12fff1e        bx      lr
> 
> 
> Anyway, source code should be written for humans, as it gets read
> more often than written, and maintenance is hard enough already.
> Try to come up with a text search pattern to catch both the 3 and
> 8 values at the same time.  Or try to easily see how they are the
> same when there is no comment.  Is the code path so hot that
> single instructions count so badly, that the downsides should be
> considered acceptable?
okay.

I will change to "dummy /= 8" in the next version. 

thanks
Huang Shijie

  reply	other threads:[~2014-04-17 16:04 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-16  8:18 [PATCH] mtd: spi-nor: fix the wrong dummy value Huang Shijie
2014-04-16 20:08 ` Gerhard Sittig
2014-04-17  4:59   ` Huang Shijie
2014-04-17 11:30     ` Marek Vasut
2014-04-17 13:41   ` Huang Shijie
2014-04-17 14:15     ` Marek Vasut
2014-04-17 15:55     ` Gerhard Sittig
2014-04-17 15:57       ` Huang Shijie [this message]
2014-04-17 18:12         ` Marek Vasut
2014-04-16 23:40 ` Marek Vasut
2014-04-17  5:01   ` Huang Shijie
2014-04-17 11:32     ` Marek Vasut
2014-04-17 12:59       ` Huang Shijie
2014-04-17 14:15         ` Marek Vasut

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140417155758.GA14819@localhost.localdomain \
    --to=shijie8@gmail.com \
    --cc=b32955@freescale.com \
    --cc=computersforpeace@gmail.com \
    --cc=dwmw2@infradead.org \
    --cc=gsi@denx.de \
    --cc=linux-mtd@lists.infradead.org \
    --cc=marex@denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.