* [PATCH 2/4] dsmark: get rid of trivial function [not found] <20080120130542.16720b45@deepthought> @ 2008-01-20 21:25 ` Stephen Hemminger 2008-01-21 0:16 ` Patrick McHardy 0 siblings, 1 reply; 6+ messages in thread From: Stephen Hemminger @ 2008-01-20 21:25 UTC (permalink / raw) To: David Miller; +Cc: Patrick McHardy, netdev Replace loop in dsmark_valid_indices with equivalent bit math. Signed-off-by: Stephen Hemminger <shemminger@vyatta.com> --- a/net/sched/sch_dsmark.c 2008-01-20 13:07:58.000000000 -0800 +++ b/net/sched/sch_dsmark.c 2008-01-20 13:22:54.000000000 -0800 @@ -45,13 +45,8 @@ struct dsmark_qdisc_data { static inline int dsmark_valid_indices(u16 indices) { - while (indices != 1) { - if (indices & 1) - return 0; - indices >>= 1; - } - - return 1; + /* Must have only one bit set */ + return (indices & (indices - 1)) == 0; } static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index) ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/4] dsmark: get rid of trivial function 2008-01-20 21:25 ` [PATCH 2/4] dsmark: get rid of trivial function Stephen Hemminger @ 2008-01-21 0:16 ` Patrick McHardy 2008-01-21 8:39 ` Ilpo Järvinen 2008-01-21 10:22 ` David Miller 0 siblings, 2 replies; 6+ messages in thread From: Patrick McHardy @ 2008-01-21 0:16 UTC (permalink / raw) To: Stephen Hemminger; +Cc: David Miller, netdev Stephen Hemminger wrote: > Replace loop in dsmark_valid_indices with equivalent bit math. > > Signed-off-by: Stephen Hemminger <shemminger@vyatta.com> > > --- a/net/sched/sch_dsmark.c 2008-01-20 13:07:58.000000000 -0800 > +++ b/net/sched/sch_dsmark.c 2008-01-20 13:22:54.000000000 -0800 > @@ -45,13 +45,8 @@ struct dsmark_qdisc_data { > > static inline int dsmark_valid_indices(u16 indices) > { > - while (indices != 1) { > - if (indices & 1) > - return 0; > - indices >>= 1; > - } > - > - return 1; > + /* Must have only one bit set */ > + return (indices & (indices - 1)) == 0; hweight seems easier to understand, it took me a bit to realize that the comment matches the code :) ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/4] dsmark: get rid of trivial function 2008-01-21 0:16 ` Patrick McHardy @ 2008-01-21 8:39 ` Ilpo Järvinen 2008-01-21 10:22 ` David Miller 1 sibling, 0 replies; 6+ messages in thread From: Ilpo Järvinen @ 2008-01-21 8:39 UTC (permalink / raw) To: Patrick McHardy, Stephen Hemminger; +Cc: David Miller, Netdev On Mon, 21 Jan 2008, Patrick McHardy wrote: > Stephen Hemminger wrote: > > Replace loop in dsmark_valid_indices with equivalent bit math. > > > > Signed-off-by: Stephen Hemminger <shemminger@vyatta.com> > > > > --- a/net/sched/sch_dsmark.c 2008-01-20 13:07:58.000000000 -0800 > > +++ b/net/sched/sch_dsmark.c 2008-01-20 13:22:54.000000000 -0800 > > @@ -45,13 +45,8 @@ struct dsmark_qdisc_data { > > > > static inline int dsmark_valid_indices(u16 indices) > > { > > - while (indices != 1) { > > - if (indices & 1) > > - return 0; > > - indices >>= 1; > > - } > > - > > - return 1; > > + /* Must have only one bit set */ > > + return (indices & (indices - 1)) == 0; Isn't there some magic under include/linux to do that btw, I suppose that if the caller side zero check is pushed down there too, the is_power_of_2() is 100% match? :-) > hweight seems easier to understand, it took me a bit > to realize that the comment matches the code :) In addition, the original seems infinite loop with zero indices given but luckily that was checked at the caller site already... -- i. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/4] dsmark: get rid of trivial function 2008-01-21 0:16 ` Patrick McHardy 2008-01-21 8:39 ` Ilpo Järvinen @ 2008-01-21 10:22 ` David Miller 2008-01-22 3:47 ` Stephen Hemminger 1 sibling, 1 reply; 6+ messages in thread From: David Miller @ 2008-01-21 10:22 UTC (permalink / raw) To: kaber; +Cc: shemminger, netdev From: Patrick McHardy <kaber@trash.net> Date: Mon, 21 Jan 2008 01:16:32 +0100 > Stephen Hemminger wrote: > > Replace loop in dsmark_valid_indices with equivalent bit math. > > > > Signed-off-by: Stephen Hemminger <shemminger@vyatta.com> > > > > --- a/net/sched/sch_dsmark.c 2008-01-20 13:07:58.000000000 -0800 > > +++ b/net/sched/sch_dsmark.c 2008-01-20 13:22:54.000000000 -0800 > > @@ -45,13 +45,8 @@ struct dsmark_qdisc_data { > > > > static inline int dsmark_valid_indices(u16 indices) > > { > > - while (indices != 1) { > > - if (indices & 1) > > - return 0; > > - indices >>= 1; > > - } > > - > > - return 1; > > + /* Must have only one bit set */ > > + return (indices & (indices - 1)) == 0; > > hweight seems easier to understand, it took me a bit > to realize that the comment matches the code :) Sounds good. Here is what I ended up checking in. [PKT_SCHED] dsmark: Use hweight32() instead of convoluted loop. Based upon a patch by Stephen Hemminger and suggestions from Patrick McHardy. Signed-off-by: David S. Miller <davem@davemloft.net> diff --git a/net/sched/sch_dsmark.c b/net/sched/sch_dsmark.c index a9732ae..d96eaf0 100644 --- a/net/sched/sch_dsmark.c +++ b/net/sched/sch_dsmark.c @@ -10,6 +10,7 @@ #include <linux/errno.h> #include <linux/skbuff.h> #include <linux/rtnetlink.h> +#include <linux/bitops.h> #include <net/pkt_sched.h> #include <net/dsfield.h> #include <net/inet_ecn.h> @@ -43,17 +44,6 @@ struct dsmark_qdisc_data { int set_tc_index; }; -static inline int dsmark_valid_indices(u16 indices) -{ - while (indices != 1) { - if (indices & 1) - return 0; - indices >>= 1; - } - - return 1; -} - static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index) { return (index <= p->indices && index > 0); @@ -348,7 +338,8 @@ static int dsmark_init(struct Qdisc *sch, struct rtattr *opt) goto errout; indices = RTA_GET_U16(tb[TCA_DSMARK_INDICES-1]); - if (!indices || !dsmark_valid_indices(indices)) + + if (hweight32(indices) != 1) goto errout; if (tb[TCA_DSMARK_DEFAULT_INDEX-1]) ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 2/4] dsmark: get rid of trivial function 2008-01-21 10:22 ` David Miller @ 2008-01-22 3:47 ` Stephen Hemminger 2008-01-22 3:59 ` David Miller 0 siblings, 1 reply; 6+ messages in thread From: Stephen Hemminger @ 2008-01-22 3:47 UTC (permalink / raw) To: David Miller; +Cc: netdev On Mon, 21 Jan 2008 02:22:23 -0800 (PST) David Miller <davem@davemloft.net> wrote: > From: Patrick McHardy <kaber@trash.net> > Date: Mon, 21 Jan 2008 01:16:32 +0100 > > > Stephen Hemminger wrote: > > > Replace loop in dsmark_valid_indices with equivalent bit math. > > > > > > Signed-off-by: Stephen Hemminger <shemminger@vyatta.com> > > > > > > --- a/net/sched/sch_dsmark.c 2008-01-20 13:07:58.000000000 -0800 > > > +++ b/net/sched/sch_dsmark.c 2008-01-20 13:22:54.000000000 -0800 > > > @@ -45,13 +45,8 @@ struct dsmark_qdisc_data { > > > > > > static inline int dsmark_valid_indices(u16 indices) > > > { > > > - while (indices != 1) { > > > - if (indices & 1) > > > - return 0; > > > - indices >>= 1; > > > - } > > > - > > > - return 1; > > > + /* Must have only one bit set */ > > > + return (indices & (indices - 1)) == 0; > > > > hweight seems easier to understand, it took me a bit > > to realize that the comment matches the code :) > > Sounds good. Here is what I ended up checking in. > > [PKT_SCHED] dsmark: Use hweight32() instead of convoluted loop. > > Based upon a patch by Stephen Hemminger and suggestions > from Patrick McHardy. > > Signed-off-by: David S. Miller <davem@davemloft.net> > > diff --git a/net/sched/sch_dsmark.c b/net/sched/sch_dsmark.c > index a9732ae..d96eaf0 100644 > --- a/net/sched/sch_dsmark.c > +++ b/net/sched/sch_dsmark.c > @@ -10,6 +10,7 @@ > #include <linux/errno.h> > #include <linux/skbuff.h> > #include <linux/rtnetlink.h> > +#include <linux/bitops.h> > #include <net/pkt_sched.h> > #include <net/dsfield.h> > #include <net/inet_ecn.h> > @@ -43,17 +44,6 @@ struct dsmark_qdisc_data { > int set_tc_index; > }; > > -static inline int dsmark_valid_indices(u16 indices) > -{ > - while (indices != 1) { > - if (indices & 1) > - return 0; > - indices >>= 1; > - } > - > - return 1; > -} > - > static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index) > { > return (index <= p->indices && index > 0); > @@ -348,7 +338,8 @@ static int dsmark_init(struct Qdisc *sch, struct rtattr *opt) > goto errout; > > indices = RTA_GET_U16(tb[TCA_DSMARK_INDICES-1]); > - if (!indices || !dsmark_valid_indices(indices)) > + > + if (hweight32(indices) != 1) > goto errout; Come on Dave, that is a step backwards. So you took a two instruction thing that any programmer who ever had one of those technical trick interviews would surely understand, and made it call a function... Seems like the thing you would consul others against. Please use !is_power_of_2(indices) instead. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/4] dsmark: get rid of trivial function 2008-01-22 3:47 ` Stephen Hemminger @ 2008-01-22 3:59 ` David Miller 0 siblings, 0 replies; 6+ messages in thread From: David Miller @ 2008-01-22 3:59 UTC (permalink / raw) To: stephen.hemminger; +Cc: netdev From: Stephen Hemminger <stephen.hemminger@vyatta.com> Date: Mon, 21 Jan 2008 19:47:51 -0800 > > > > indices = RTA_GET_U16(tb[TCA_DSMARK_INDICES-1]); > > - if (!indices || !dsmark_valid_indices(indices)) > > + > > + if (hweight32(indices) != 1) > > goto errout; > > Come on Dave, that is a step backwards. Absolutely not. > So you took a two instruction thing that any programmer who ever had > one of those technical trick interviews would surely understand, and > made it call a function... Seems like the thing you would consul > others against. It's counting bits, "hamming weight" is a count of bits. That is more understandable to me than: Oh BTW, power of two values also just so happen to have only 1 bit set. Testing for a power of two obfuscates the meaning of the test. It doesn't want a power-of-two, it wants a bitmask with only one bit set. ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-01-22 3:59 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20080120130542.16720b45@deepthought>
2008-01-20 21:25 ` [PATCH 2/4] dsmark: get rid of trivial function Stephen Hemminger
2008-01-21 0:16 ` Patrick McHardy
2008-01-21 8:39 ` Ilpo Järvinen
2008-01-21 10:22 ` David Miller
2008-01-22 3:47 ` Stephen Hemminger
2008-01-22 3:59 ` David Miller
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).